From 7cf190e692d531970af77bcec898e19d71c5d612 Mon Sep 17 00:00:00 2001 From: James <86387140+Gad-James@users.noreply.github.com> Date: Tue, 26 Mar 2024 16:31:37 +1000 Subject: [PATCH] ok --- Makefile | 11 ++++++- api.h | 18 +++++++++++ main.c | 11 ++++++- ms_sleep.c | 14 ++++++++ systray.c | 18 +++++++++++ taskmgr.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ userpaths.c | 25 +++++++++++++++ 7 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 ms_sleep.c create mode 100644 taskmgr.c create mode 100644 userpaths.c diff --git a/Makefile b/Makefile index dc2ef26..b3b3152 100644 --- a/Makefile +++ b/Makefile @@ -111,7 +111,10 @@ PUR_OBJS= \ build$(PS)main.obj \ build$(PS)error.obj \ build$(PS)stats.obj \ -build$(PS)systray.obj +build$(PS)systray.obj \ +build$(PS)ms_sleep.obj \ +build$(PS)taskmgr.obj \ +build$(PS)userpaths.obj build$(PS)main.obj: build Makefile main.c api.h include$(PS)SDL.h include$(PS)SDL_image.h $(CCOBJ) main.c $(OBJOUT)$@ @@ -121,6 +124,12 @@ build$(PS)error.obj: build Makefile api.h error.c $(CCOBJ) error.c $(OBJOUT)$@ build$(PS)systray.obj: build pf.ico pf.png api.h tray$(PS)tray.h $(CCOBJ) systray.c $(OBJOUT)$@ +build$(PS)ms_sleep.obj: build ms_sleep.c + $(CCOBJ) ms_sleep.c $(OBJOUT)$@ +build$(PS)taskmgr.obj: build api.h taskmgr.c + $(CCOBJ) taskmgr.c $(OBJOUT)$@ +build$(PS)userpaths.obj: build api.h userpaths.c + $(CCOBJ) userpaths.c $(OBJOUT)$@ build: mkdir build diff --git a/api.h b/api.h index 136c3cc..64bf6aa 100644 --- a/api.h +++ b/api.h @@ -1,8 +1,26 @@ #ifndef api_h #define api_h +extern int app_is_live; + #include void reportError(const char *fmt,...); enum ps { ps_startup,ps_shutdown}; void print_stats(enum ps ps); + +unsigned taskmgr_ms_till_next(void); +void taskmgr_run(unsigned for_max_ms); + + +void ms_sleep(unsigned ms); +/**designed for relative time keeping, not recommended for use for absolute times*/ +unsigned gettime_ms(void); + + +const char * get_config_file_path(void); +const char * get_app_dir(void); + +int path_exists(const char *folder_or_file_path); +void make_dir(const char *folderpath); + #endif//api_h diff --git a/main.c b/main.c index 1b251f6..e38e768 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,8 @@ #include "api.h" +int app_is_live=0; + SDL_Window * purrwin; void purrer(void) { SDL_Init(SDL_INIT_TIMER|SDL_INIT_EVENTS|SDL_INIT_VIDEO); @@ -20,10 +22,17 @@ void purrer(void) { SDL_Quit(); } int main(int argc,char**argv){ + app_is_live=1; puts("hello world"); SDL_SetMainReady(); print_stats(ps_startup); - purrer(); + systray_initialscan(); + //purrer(); + while(app_is_live) { + unsigned ms_to_next_run = taskmgr_ms_till_next(); + ms_sleep(ms_to_next_run); + taskmgr_run(0); + } print_stats(ps_shutdown); return 0; } diff --git a/ms_sleep.c b/ms_sleep.c new file mode 100644 index 0000000..6c880c3 --- /dev/null +++ b/ms_sleep.c @@ -0,0 +1,14 @@ + +#ifdef _MSC_VER +#include +void ms_sleep(unsigned ms) { + Sleep(ms); +} +unsigned gettime_ms(void){ + SYSTEMTIME st; + FILETIME ft={0,0}; + GetSystemTime(&st); + SystemTimeToFileTime(&st,&ft); + return ft.dwLowDateTime; +} +#endif diff --git a/systray.c b/systray.c index 4baa2a6..6ac57cc 100644 --- a/systray.c +++ b/systray.c @@ -1,4 +1,22 @@ +#include + + #include "tray/tray.h" #include "api.h" +void systray_initialscan(void){ + FILE *cfg; + if(!path_exists(get_app_dir())){ + /*--create directory--*/ + make_dir(get_app_dir()); + } + /*--load list of tracked files--*/ + cfg = fopen(get_config_file_path(),"rb"); + if(!cfg) { + perror(get_config_file_path()); + return; + } + fclose(cfg); + +} diff --git a/taskmgr.c b/taskmgr.c new file mode 100644 index 0000000..928f552 --- /dev/null +++ b/taskmgr.c @@ -0,0 +1,92 @@ +#include "api.h" + +#ifndef TASKMGR_MAX_TASKS +#define TASKMGR_MAX_TASKS 12 +#endif + +unsigned taskmgr_ms_till_next(void){ + return 10; +} + +static unsigned right_here=0; + +struct run_tag { + int oneshot;/** 0 && effective_passed < run_tab[i].frequency_ms) { + run_tab[i].awaiting = run_tab[i].frequency_ms - effective_passed; + } else if(effective_passed > 0) { + backed_up=1; + if(run_tab[i].stack_up) { + /*--stack up logic--*/ + run_tab[i].awaiting=0; + run_tab[i].deficit = effective_passed - run_tab[i].frequency_ms; + } else { + run_tab[i].awaiting=0; + } + } + if(run_tab[i].oneshot) { + run_tab[i].cb = NULL; + run_tab[i].ud = NULL; + } + task2run(run_tab[i].ud); + } else { + run_tab[i].awaiting -= effective_passed; + } + /*--deal with SLOW,LONG running callbacks vs for_max_ms "run for" issue--*/ + if(time_passed(right_now,gettime_ms())>for_max_ms)break; + } + return backed_up; +} +void taskmgr_run(unsigned for_max_ms){ + unsigned total_passed=0; + unsigned initial_moment = gettime_ms(); + int backed_up = one_loop(for_max_ms); + if(for_max_ms > 0) { + while(backed_up) { + total_passed=time_passed(initial_moment,gettime_ms()); + if(total_passed>=for_max_ms) break; + backed_up=one_loop(total_passed < for_max_ms ? for_max_ms - total_passed : 1); + } + } +} diff --git a/userpaths.c b/userpaths.c new file mode 100644 index 0000000..ba56166 --- /dev/null +++ b/userpaths.c @@ -0,0 +1,25 @@ +#include +#include + +char servers_ini_buf[260+1]; +char app_path_buf[260+1]; + +#ifdef _MSC_VER +#include +const char * get_app_dir(void){ + const char *appdata = getenv("APPDATA"); + snprintf(app_path_buf,sizeof app_path_buf,"%s",appdata?appdata:"."); + return app_path_buf; +} +const char * get_config_file_path(void){ + snprintf(servers_ini_buf,sizeof servers_ini_buf,"%s\\purforce\\servers.ini",get_app_dir()); + return servers_ini_buf; +} +int path_exists(const char *folder_or_file_path){ + int ret = _access(folder_or_file_path,00);//Existence only + return ret==0; +} +void make_dir(const char *folderpath){ + _mkdir(folderpath); +} +#endif