diff --git a/src/common.h b/src/common.h index 9314584..8e8d092 100644 --- a/src/common.h +++ b/src/common.h @@ -12,6 +12,8 @@ #define MONITOR_NAME_MAX 100 #define WALLPAPER_NAME_MAX 100 +#define DEFAULT_LINUX_PREFIX "/usr/local" + typedef struct { int x; @@ -63,9 +65,37 @@ typedef struct int targetFps; } AppConfig; -MonitorInfo *scanMonitors(int *count); +// +// paths.c +// + +#define APP_DIR_BIN 0 +#define APP_DIR_SHARE 1 +#define APP_DIR_USER_SETTINGS 2 + +void getAppDir(char *buff, int type); + +void getMonitorCfgPath(char *buff, const char *name); +void getWlpCfgPath(char *buff, const char *dirPath); +void getAppCfgPath(char *buff); +void getLogPath(char *buff); + +// +// monitorScanner.c +// + +MonitorInfo *scanMonitors(int *count); + +// +// wallpaperScanner.c +// + WallpaperInfo *scanWallpapers(int *count); +// +// config.c +// + void saveMonitorConfig(const char *name, MonitorConfig *mc); int loadMonitorConfig(const char *name, MonitorConfig *mc); diff --git a/src/common/config.c b/src/common/config.c index 259af27..bfd2356 100644 --- a/src/common/config.c +++ b/src/common/config.c @@ -5,9 +5,9 @@ #include "../common.h" #define CONFIG_DEFAULT 0 -#define CONFIG_USER 1 +#define CONFIG_USER 1 -void getMonitorConfigPath(const char *name, char *path) { +/*void getMonitorConfigPath(const char *name, char *path) { #ifdef __WIN32 sprintf(path, "%s\\lwp\\monitors\\%s.cfg", g_get_user_data_dir(), name); #else @@ -15,11 +15,11 @@ void getMonitorConfigPath(const char *name, char *path) { #endif } -void getWallpaperConfigPath(const char *dirName, char *path, int type) { +void getWallpaperConfigPath(const char *dirPath, char *path, int type) { if (type == CONFIG_DEFAULT) - sprintf(path, "%s/wallpaper.cfg", dirName); + sprintf(path, "%s/wallpaper.cfg", dirPath); else - sprintf(path, "%s/wallpaper.cfg", dirName); + sprintf(path, "%s/wallpaper.cfg", dirPath); } void getAppConfigPath(char *path, int type) { @@ -34,9 +34,10 @@ void getAppConfigPath(char *path, int type) { else sprintf(path, "%s/.config/lwp/lwp.cfg", g_get_home_dir()); #endif -} +}*/ -static void generateEmptyMonitorConfig(MonitorConfig *mc) { +static void generateEmptyMonitorConfig(MonitorConfig *mc) +{ sprintf(mc->wlpName, ""); mc->wlpBounds.x = 0; mc->wlpBounds.y = 0; @@ -44,8 +45,9 @@ static void generateEmptyMonitorConfig(MonitorConfig *mc) { mc->wlpBounds.h = 1080; } -void saveMonitorConfig(const char *name, MonitorConfig *mc) { - config_t cfg; +void saveMonitorConfig(const char *name, MonitorConfig *mc) +{ + config_t cfg; config_setting_t *root, *setting; config_init(&cfg); @@ -64,9 +66,10 @@ void saveMonitorConfig(const char *name, MonitorConfig *mc) { config_setting_set_int(setting, mc->wlpBounds.h); char path[PATH_MAX]; - getMonitorConfigPath(name, path); + getMonitorCfgPath(path, name); - if (!config_write_file(&cfg, path)) { + if (!config_write_file(&cfg, path)) + { fprintf(stderr, "Error while writing file.\n"); config_destroy(&cfg); } @@ -74,36 +77,37 @@ void saveMonitorConfig(const char *name, MonitorConfig *mc) { config_destroy(&cfg); } -int loadMonitorConfig(const char *name, MonitorConfig *mc) { +int loadMonitorConfig(const char *name, MonitorConfig *mc) +{ mc->loaded = 0; - config_t cfg; + config_t cfg; config_setting_t *root, *setting; char path[PATH_MAX]; - getMonitorConfigPath(name, path); + getMonitorCfgPath(path, name); - if (!g_file_test(path, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS)) { + if (!g_file_test(path, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS)) + { generateEmptyMonitorConfig(mc); saveMonitorConfig(name, mc); return 1; } config_init(&cfg); - if (config_read_file(&cfg, path) == CONFIG_FALSE) - return 0; + if (config_read_file(&cfg, path) == CONFIG_FALSE) return 0; root = config_root_setting(&cfg); setting = config_setting_get_member(root, "wlpName"); strcpy(mc->wlpName, config_setting_get_string(setting)); - setting = config_setting_get_member(root, "x"); + setting = config_setting_get_member(root, "x"); mc->wlpBounds.x = config_setting_get_int(setting); - setting = config_setting_get_member(root, "y"); + setting = config_setting_get_member(root, "y"); mc->wlpBounds.y = config_setting_get_int(setting); - setting = config_setting_get_member(root, "w"); + setting = config_setting_get_member(root, "w"); mc->wlpBounds.w = config_setting_get_int(setting); - setting = config_setting_get_member(root, "h"); + setting = config_setting_get_member(root, "h"); mc->wlpBounds.h = config_setting_get_int(setting); config_destroy(&cfg); @@ -112,26 +116,27 @@ int loadMonitorConfig(const char *name, MonitorConfig *mc) { return 1; } -int loadAppConfig(AppConfig *ac) { - config_t cfg; +int loadAppConfig(AppConfig *ac) +{ + config_t cfg; config_setting_t *root, *setting; char path[PATH_MAX]; - getAppConfigPath(path, CONFIG_USER); + getAppCfgPath(path); config_init(&cfg); - if (config_read_file(&cfg, path) == CONFIG_FALSE) { - getAppConfigPath(path, CONFIG_DEFAULT); - if (config_read_file(&cfg, path) == CONFIG_FALSE) - return 0; + if (config_read_file(&cfg, path) == CONFIG_FALSE) + { + // todo: set default config values + return 0; } root = config_root_setting(&cfg); #ifdef __LINUX - setting = config_setting_get_member(root, "draw_on_rootwindow"); + setting = config_setting_get_member(root, "draw_on_rootwindow"); ac->drawOnRootWindow = config_setting_get_int(setting); #endif - setting = config_setting_get_member(root, "target_fps"); + setting = config_setting_get_member(root, "target_fps"); ac->targetFps = config_setting_get_int(setting); config_destroy(&cfg); @@ -139,39 +144,41 @@ int loadAppConfig(AppConfig *ac) { return 1; } -int loadWallpaperConfig(const char *dirName, WallpaperConfig *wc) { +int loadWallpaperConfig(const char *dirPath, WallpaperConfig *wc) +{ wc->loaded = 0; - config_t cfg; + config_t cfg; config_setting_t *root, *setting; config_init(&cfg); char path[PATH_MAX]; - getWallpaperConfigPath(dirName, path, CONFIG_USER); + getWlpCfgPath(path, dirPath); - if (config_read_file(&cfg, path) == CONFIG_FALSE) { - getWallpaperConfigPath(dirName, path, CONFIG_DEFAULT); - if (config_read_file(&cfg, path) == CONFIG_FALSE) - return 0; + if (config_read_file(&cfg, path) == CONFIG_FALSE) + { + getWlpCfgPath(path, dirPath); + if (config_read_file(&cfg, path) == CONFIG_FALSE) return 0; } root = config_root_setting(&cfg); - setting = config_setting_get_member(root, "count"); + setting = config_setting_get_member(root, "count"); wc->layersCount = config_setting_get_int(setting); - setting = config_setting_get_member(root, "repeat_x"); - wc->repeatX = config_setting_get_int(setting); - setting = config_setting_get_member(root, "repeat_y"); - wc->repeatY = config_setting_get_int(setting); + setting = config_setting_get_member(root, "repeat_x"); + wc->repeatX = config_setting_get_int(setting); + setting = config_setting_get_member(root, "repeat_y"); + wc->repeatY = config_setting_get_int(setting); wc->layerConfigs = malloc(wc->layersCount * sizeof(LayerConfig)); - setting = config_setting_get_member(root, "movement_x"); + setting = config_setting_get_member(root, "movement_x"); float movX = config_setting_get_float(setting); - setting = config_setting_get_member(root, "movement_y"); + setting = config_setting_get_member(root, "movement_y"); float movY = config_setting_get_float(setting); - for (int i = 0; i < wc->layersCount; i++) { + for (int i = 0; i < wc->layersCount; i++) + { LayerConfig *lc = wc->layerConfigs + i; lc->sensitivityX = movX * i; diff --git a/src/common/monitorScanner.c b/src/common/monitorScanner.c index 39882a7..dac0208 100644 --- a/src/common/monitorScanner.c +++ b/src/common/monitorScanner.c @@ -13,8 +13,8 @@ #ifdef __WIN32 int monitorEnumIndex = 0; -static BOOL monitorenumproc(HMONITOR monitor, HDC hdc, LPRECT rect, - LPARAM param) { +static BOOL monitorenumproc(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM param) +{ MONITORINFO info; info.cbSize = sizeof(MONITORINFO); @@ -22,12 +22,17 @@ static BOOL monitorenumproc(HMONITOR monitor, HDC hdc, LPRECT rect, MonitorInfo *mi = (MonitorInfo *)param + monitorEnumIndex; - snprintf(mi->name, MONITOR_NAME_MAX, "Monitor %d%s", monitorEnumIndex + 1, - info.dwFlags == MONITORINFOF_PRIMARY ? " (main)" : ""); - mi->bounds.x = info.rcWork.left; - mi->bounds.y = info.rcWork.top; - mi->bounds.w = info.rcWork.right - info.rcWork.left; - mi->bounds.h = info.rcWork.bottom - info.rcWork.top; + snprintf( + mi->name, + MONITOR_NAME_MAX, + "Monitor %d%s", + monitorEnumIndex + 1, + info.dwFlags == MONITORINFOF_PRIMARY ? " (main)" : "" + ); + mi->bounds.x = info.rcWork.left; + mi->bounds.y = info.rcWork.top; + mi->bounds.w = info.rcWork.right - info.rcWork.left; + mi->bounds.h = info.rcWork.bottom - info.rcWork.top; mi->config.loaded = 0; monitorEnumIndex++; @@ -35,31 +40,34 @@ static BOOL monitorenumproc(HMONITOR monitor, HDC hdc, LPRECT rect, } #endif -MonitorInfo *scanMonitors(int *count) { +MonitorInfo *scanMonitors(int *count) +{ MonitorInfo *m = NULL; #ifdef __WIN32 monitorEnumIndex = 0; - *count = GetSystemMetrics(SM_CMONITORS); - m = malloc(sizeof(MonitorInfo) * (*count)); + *count = GetSystemMetrics(SM_CMONITORS); + m = malloc(sizeof(MonitorInfo) * (*count)); EnumDisplayMonitors(NULL, NULL, &monitorenumproc, (LPARAM)m); #else - Display *display = XOpenDisplay((getenv("DISPLAY"))); - Window wnd = DefaultRootWindow(display); - XRRMonitorInfo *info = XRRGetMonitors(display, wnd, 0, &monitorCount); + int monitorCount; + + Display *display = XOpenDisplay((getenv("DISPLAY"))); + Window wnd = DefaultRootWindow(display); + XRRMonitorInfo *info = XRRGetMonitors(display, wnd, 0, &monitorCount); m = malloc(sizeof(MonitorInfo) * monitorCount); int i = 0; - while (i < monitorCount) { - snprintf(m[i].name, MONITOR_NAME_MAX, "%s", - XGetAtomName(display, info->name)); - m[i].bounds.x = info->x; - m[i].bounds.y = info->y; - m[i].bounds.w = info->width; - m[i].bounds.h = info->height; + while (i < monitorCount) + { + snprintf(m[i].name, MONITOR_NAME_MAX, "%s", XGetAtomName(display, info->name)); + m[i].bounds.x = info->x; + m[i].bounds.y = info->y; + m[i].bounds.w = info->width; + m[i].bounds.h = info->height; m[i].config.loaded = 0; info++; diff --git a/src/common/paths.c b/src/common/paths.c new file mode 100644 index 0000000..2f5c502 --- /dev/null +++ b/src/common/paths.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include + +#include "../common.h" + +static void removeLastPathEntry(char *path) +{ + char *ptr = path + strlen(path) - 1; + while (*ptr != '\\') ptr--; + *ptr = '\0'; +} + +void getAppDir(char *buff, int type) +{ +#ifdef __LINUX + + char prefix[PATH_MAX]; + + char *prefixEnv = getenv("LWP_PREFIX"); + strcpy(prefix, prefixEnv ? prefixEnv : DEFAULT_LINUX_PREFIX); + +#endif + + if (type == APP_DIR_USER_SETTINGS) + { +#ifdef __LINUX + sprintf(buff, "%s/.config/lwp", g_get_home_dir()); +#elif __WIN32 + sprintf(buff, "%s\\lwp", g_get_user_data_dir()); +#endif + } + +#ifdef __LINUX + if (type == APP_DIR_BIN) + { + sprintf(buff, "%s/%s", prefix, "bin"); + } + else if (type == APP_DIR_SHARE) + { + sprintf(buff, "%s/%s", prefix, "share/lwp"); + } +#elif __WIN32 + GetModuleFileNameA(NULL, buff, PATH_MAX); + removeLastPathEntry(buff); +#elif __DARWIN +#endif +} + +void getMonitorCfgPath(char *buff, const char *name) +{ + getAppDir(buff, APP_DIR_USER_SETTINGS); + +#ifdef __WIN32 + const char *format = "%s\\monitors\\%s.cfg"; +#else + const char *format = "%s/monitors/%s.cfg"; +#endif + + sprintf(buff, format, buff, name); +} + +void getWlpCfgPath(char *buff, const char *dirPath) +{ +#ifdef __WIN32 + const char *format = "%s\\wallpaper.cfg"; +#else + const char *format = "%s/wallpaper.cfg"; +#endif + + sprintf(buff, format, dirPath); +} + +void getAppCfgPath(char *buff) +{ + getAppDir(buff, APP_DIR_USER_SETTINGS); + +#ifdef __WIN32 + const char *format = "%s\\lwp.cfg"; +#else + const char *format = "%s/lwp.cfg"; +#endif + + sprintf(buff, format, buff); +} + +void getLogPath(char *buff) +{ + getAppDir(buff, APP_DIR_USER_SETTINGS); + +#ifdef __WIN32 + const char *format = "%s\\log.txt"; +#else + const char *format = "%s/log"; +#endif + + sprintf(buff, format, buff); +} diff --git a/src/common/wallpaperScanner.c b/src/common/wallpaperScanner.c index 981b128..c45f60e 100644 --- a/src/common/wallpaperScanner.c +++ b/src/common/wallpaperScanner.c @@ -4,74 +4,62 @@ #include "../common.h" #define WALLPAPERS_SYSTEM 0 -#define WALLPAPERS_USER 1 +#define WALLPAPERS_USER 1 + +WallpaperInfo *scanWallpapers(int *count) +{ + *count = 0; -static void getWallpapersDirPath(char *path, int type) { #ifdef __WIN32 - if (type == WALLPAPERS_USER) - sprintf(path, "%s\\lwp\\wallpapers", g_get_user_data_dir()); - else { - char currentDirPath[PATH_MAX]; - GetModuleFileNameA(NULL, currentDirPath, PATH_MAX); - char *ptr = currentDirPath+strlen(currentDirPath)-1; - while(*ptr != '\\') - ptr--; - *ptr = '\0'; - sprintf(path, "%s\\wallpapers", currentDirPath); - } + const char *format = "%s\\%s"; #else - if (type == WALLPAPERS_USER) - sprintf(path, "%s/.config/lwp/wallpapers", g_get_home_dir()); - else - sprintf(path, "/usr/local/share/lwp/wallpapers"); + const char *format = "%s/%s"; #endif -} - -WallpaperInfo *scanWallpapers(int *count) { - *count = 0; char userWlpPath[PATH_MAX]; - getWallpapersDirPath(userWlpPath, WALLPAPERS_USER); + getAppDir(userWlpPath, APP_DIR_USER_SETTINGS); + sprintf(userWlpPath, format, userWlpPath, "wallpapers"); + char systemWlpPath[PATH_MAX]; - getWallpapersDirPath(systemWlpPath, WALLPAPERS_SYSTEM); + getAppDir(systemWlpPath, APP_DIR_SHARE); + sprintf(systemWlpPath, format, systemWlpPath, "wallpapers"); - GDir *dir; - GError *error; + GDir *dir; + GError *error; const gchar *filename; dir = g_dir_open(systemWlpPath, 0, &error); - while ((g_dir_read_name(dir))) - (*count)++; + while ((g_dir_read_name(dir))) (*count)++; dir = g_dir_open(userWlpPath, 0, &error); - while ((g_dir_read_name(dir))) - (*count)++; + while ((g_dir_read_name(dir))) (*count)++; WallpaperInfo *list = malloc(sizeof(WallpaperInfo) * (*count)); - WallpaperInfo *ptr = list; + WallpaperInfo *ptr = list; dir = g_dir_open(systemWlpPath, 0, &error); - while ((filename = g_dir_read_name(dir))) { + while ((filename = g_dir_read_name(dir))) + { sprintf(ptr->name, "%s", filename); #ifdef __WIN32 sprintf(ptr->dirPath, "%s\\%s", systemWlpPath, filename); - #else sprintf(ptr->dirPath, "%s/%s", systemWlpPath, filename); #endif - ptr->isDefault = 1; + ptr->isDefault = 1; ptr->config.loaded = 0; ptr++; } dir = g_dir_open(userWlpPath, 0, &error); - while ((filename = g_dir_read_name(dir))) { + while ((filename = g_dir_read_name(dir))) + { sprintf(ptr->name, "%s", filename); #ifdef __WIN32 sprintf(ptr->dirPath, "%s\\%s", userWlpPath, filename); #else sprintf(ptr->dirPath, "%s/%s", userWlpPath, filename); #endif - ptr->isDefault = 0; + ptr->isDefault = 0; ptr->config.loaded = 0; ptr++; } diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index cc0a414..3d3fb22 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,12 +1,13 @@ set(_SOURCE_FILES + ../common/paths.c + ../common/config.c + ../common/wallpaperScanner.c + ../common/monitorScanner.c main.c windowHandlers.c wlp.c trayIcon.c - ../common/monitorScanner.c - ../common/config.c - ../common/wallpaperScanner.c - ) +) # Windows resource file if(_UNAME STREQUAL "WIN32") diff --git a/src/core/main.c b/src/core/main.c index 8514ce4..e50d34f 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -64,7 +64,16 @@ static void activate(GtkApplication *app, gpointer userdata) { alreadyRunning = 1; - builder = gtk_builder_new_from_file(MAIN_WINDOW_TEMPLATE_PATH); + char gladefilePath[PATH_MAX]; + getAppDir(gladefilePath, APP_DIR_SHARE); +#ifdef __WIN32 + const char *format = "%s\\window_templates\\main.glade"; +#else + const char *format = "%s/window_templates/main.glade"; +#endif + sprintf(gladefilePath, format, gladefilePath); + + builder = gtk_builder_new_from_file(gladefilePath); gtk_builder_connect_signals(builder, NULL); mainWnd = (GtkWidget *)gtk_builder_get_object(builder, "MainWindow"); @@ -103,14 +112,18 @@ int main(int argc, char *argv[]) { int status; +#ifdef __WIN32 initTrayIcon(); +#endif app = gtk_application_new("com.github.jszczerbinsky.lwp", G_APPLICATION_DEFAULT_FLAGS); g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); status = g_application_run(G_APPLICATION(app), argc, argv); g_object_unref(app); +#ifdef __WIN32 removeTrayIcon(); +#endif return status; } diff --git a/src/wlp/CMakeLists.txt b/src/wlp/CMakeLists.txt index 6e03f2d..13df9ba 100644 --- a/src/wlp/CMakeLists.txt +++ b/src/wlp/CMakeLists.txt @@ -3,8 +3,9 @@ if(SDL2_RUNTIME_DIR) endif() set(_SOURCE_FILES - ../common/monitorScanner.c + ../common/paths.c ../common/config.c + ../common/monitorScanner.c ../common/wallpaperScanner.c main.c debug.c diff --git a/src/wlp/debug.c b/src/wlp/debug.c index d8b15c3..5e51f3a 100644 --- a/src/wlp/debug.c +++ b/src/wlp/debug.c @@ -1,22 +1,13 @@ +#include #include #include -#include #include "main.h" -void getLogFilePath(char *path) -{ - #ifdef __WIN32 - sprintf(path, "%s\\lwp\\lwp.log", g_get_user_data_dir()); - #else - sprintf(path, "/home/cziken/.config/lwp/log"); - #endif -} - void lwpLog(int type, const char *str, ...) { char path[PATH_MAX]; - getLogFilePath(path); + getLogPath(path); FILE *file = fopen(path, "a");