diff --git a/src/common.h b/src/common.h index c228b1e..1a898be 100644 --- a/src/common.h +++ b/src/common.h @@ -12,46 +12,40 @@ #define DEFAULT_LINUX_PREFIX "/usr/local" -typedef struct -{ +typedef struct { int x; int y; int w; int h; } Bounds; -typedef struct -{ +typedef struct { float sensitivityX; float sensitivityY; } LayerConfig; -typedef struct -{ +typedef struct { int loaded; int repeatX; int repeatY; int layersCount; - LayerConfig *layerConfigs; + LayerConfig* layerConfigs; } WallpaperConfig; -typedef struct -{ +typedef struct { char name[WALLPAPER_NAME_MAX]; char dirPath[PATH_MAX]; int isDefault; WallpaperConfig config; } WallpaperInfo; -typedef struct -{ +typedef struct { int loaded; char wlpName[WALLPAPER_NAME_MAX]; Bounds wlpBounds; } MonitorConfig; -typedef struct -{ +typedef struct { char displayName[MONITOR_NAME_MAX]; char name[MONITOR_NAME_MAX]; Bounds pixelBounds; @@ -60,11 +54,11 @@ typedef struct MonitorConfig config; } MonitorInfo; -typedef struct -{ +typedef struct { int targetFps; char renderQuality[8]; int unfocusedComeback; + int wndTargetPoint; } AppConfig; // @@ -75,12 +69,12 @@ typedef struct #define APP_DIR_SHARE 1 #define APP_DIR_USER_SETTINGS 2 -void getAppDir(char *buff, int type); +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); +void getMonitorCfgPath(char* buff, const char* name); +void getWlpCfgPath(char* buff, const char* dirPath); +void getAppCfgPath(char* buff); +void getLogPath(char* buff); void createUserDirs(); @@ -88,24 +82,24 @@ void createUserDirs(); // monitorScanner.c // -MonitorInfo *scanMonitors(int *count); +MonitorInfo* scanMonitors(int* count); // // wallpaperScanner.c // -WallpaperInfo *scanWallpapers(int *count); +WallpaperInfo* scanWallpapers(int* count); // // config.c // -void saveMonitorConfig(const char *name, MonitorConfig *mc); -int loadMonitorConfig(const char *name, MonitorConfig *mc); +void saveMonitorConfig(const char* name, MonitorConfig* mc); +int loadMonitorConfig(const char* name, MonitorConfig* mc); -int loadAppConfig(AppConfig *ac); -void saveAppConfig(AppConfig *ac); +int loadAppConfig(AppConfig* ac); +void saveAppConfig(AppConfig* ac); -int loadWallpaperConfig(const char *dirName, WallpaperConfig *wc); +int loadWallpaperConfig(const char* dirName, WallpaperConfig* wc); #endif diff --git a/src/common/config.c b/src/common/config.c index c5becb9..a81c6f7 100644 --- a/src/common/config.c +++ b/src/common/config.c @@ -7,8 +7,7 @@ #define CONFIG_DEFAULT 0 #define CONFIG_USER 1 -static void generateEmptyMonitorConfig(MonitorConfig *mc) -{ +static void generateEmptyMonitorConfig(MonitorConfig* mc) { sprintf(mc->wlpName, ""); mc->wlpBounds.x = 0; mc->wlpBounds.y = 0; @@ -16,8 +15,7 @@ static void generateEmptyMonitorConfig(MonitorConfig *mc) mc->wlpBounds.h = 1080; } -void saveMonitorConfig(const char *name, MonitorConfig *mc) -{ +void saveMonitorConfig(const char* name, MonitorConfig* mc) { config_t cfg; config_setting_t *root, *setting; @@ -39,8 +37,7 @@ void saveMonitorConfig(const char *name, MonitorConfig *mc) char path[PATH_MAX]; 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); } @@ -48,8 +45,7 @@ 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; @@ -58,15 +54,15 @@ int loadMonitorConfig(const char *name, MonitorConfig *mc) char path[PATH_MAX]; 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"); @@ -87,15 +83,14 @@ int loadMonitorConfig(const char *name, MonitorConfig *mc) return 1; } -static void useDefaultAppCfg(AppConfig *ac) -{ +static void useDefaultAppCfg(AppConfig* ac) { ac->targetFps = 60; strcpy(ac->renderQuality, "best"); + ac->wndTargetPoint = 0; ac->unfocusedComeback = 1; } -void saveAppConfig(AppConfig *ac) -{ +void saveAppConfig(AppConfig* ac) { config_t cfg; config_setting_t *root, *setting; @@ -108,12 +103,13 @@ void saveAppConfig(AppConfig *ac) config_setting_set_string(setting, ac->renderQuality); setting = config_setting_add(root, "unfocused_comeback", CONFIG_TYPE_INT); config_setting_set_int(setting, ac->unfocusedComeback); + setting = config_setting_add(root, "wnd_target_point", CONFIG_TYPE_INT); + config_setting_set_int(setting, ac->wndTargetPoint); char path[PATH_MAX]; getAppCfgPath(path); - if (!config_write_file(&cfg, path)) - { + if (!config_write_file(&cfg, path)) { fprintf(stderr, "Error while writing file.\n"); config_destroy(&cfg); } @@ -121,8 +117,7 @@ void saveAppConfig(AppConfig *ac) config_destroy(&cfg); } -int loadAppConfig(AppConfig *ac) -{ +int loadAppConfig(AppConfig* ac) { config_t cfg; config_setting_t *root, *setting; @@ -130,8 +125,7 @@ int loadAppConfig(AppConfig *ac) getAppCfgPath(path); config_init(&cfg); - if (config_read_file(&cfg, path) == CONFIG_FALSE) - { + if (config_read_file(&cfg, path) == CONFIG_FALSE) { useDefaultAppCfg(ac); return 1; } @@ -155,11 +149,16 @@ int loadAppConfig(AppConfig *ac) else ac->unfocusedComeback = config_setting_get_int(setting); + setting = config_setting_get_member(root, "wnd_target_point"); + if (setting == NULL) + ac->wndTargetPoint = 0; + else + ac->wndTargetPoint = config_setting_get_int(setting); + return 1; } -int loadWallpaperConfig(const char *dirPath, WallpaperConfig *wc) -{ +int loadWallpaperConfig(const char* dirPath, WallpaperConfig* wc) { wc->loaded = 0; config_t cfg; @@ -170,10 +169,10 @@ int loadWallpaperConfig(const char *dirPath, WallpaperConfig *wc) char path[PATH_MAX]; getWlpCfgPath(path, dirPath); - if (config_read_file(&cfg, path) == CONFIG_FALSE) - { + if (config_read_file(&cfg, path) == CONFIG_FALSE) { getWlpCfgPath(path, dirPath); - 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); @@ -191,9 +190,8 @@ int loadWallpaperConfig(const char *dirPath, WallpaperConfig *wc) setting = config_setting_get_member(root, "movement_y"); float movY = config_setting_get_float(setting); - for (int i = 0; i < wc->layersCount; i++) - { - LayerConfig *lc = wc->layerConfigs + i; + for (int i = 0; i < wc->layersCount; i++) { + LayerConfig* lc = wc->layerConfigs + i; lc->sensitivityX = movX * i; lc->sensitivityY = movY * i; diff --git a/src/wlp/CMakeLists.txt b/src/wlp/CMakeLists.txt index 354ccfb..f98bdb1 100644 --- a/src/wlp/CMakeLists.txt +++ b/src/wlp/CMakeLists.txt @@ -9,6 +9,7 @@ set(_SOURCE_FILES ../common/wallpaperScanner.c main.c debug.c + targetPoint.c render.c window.c ) diff --git a/src/wlp/main.h b/src/wlp/main.h index 5138d82..9710b6b 100644 --- a/src/wlp/main.h +++ b/src/wlp/main.h @@ -27,47 +27,44 @@ #define LOG_INFO 1 #define LOG_WARNING 2 -typedef struct -{ +typedef struct { float x; float y; } Point; -typedef struct -{ - SDL_Texture *tex; +typedef struct { + SDL_Texture* tex; } Layer; -typedef struct -{ +typedef struct { WallpaperInfo info; int originalW; int originalH; - SDL_Texture *tex; - Layer *layers; + SDL_Texture* tex; + Layer* layers; } Wallpaper; -typedef struct -{ +typedef struct { MonitorInfo info; - SDL_Texture *tex; + SDL_Texture* tex; Wallpaper wlp; - SDL_Window *window; - SDL_Renderer *renderer; + SDL_Window* window; + SDL_Renderer* renderer; Point currentPoint; int aborted; } Monitor; -typedef struct -{ +typedef struct { AppConfig config; int monitorsCount; - Monitor *monitors; + Monitor* monitors; } App; -void lwpLog(int type, const char *str, ...); +void lwpLog(int type, const char* str, ...); -void initWindow(App *app, Monitor *monitor); -void runWallpaperLoop(App *app); +void initWindow(App* app, Monitor* monitor); +void runWallpaperLoop(App* app); -#endif // MAIN_H +void getTargetPoint(App* app, Point* p); + +#endif // MAIN_H diff --git a/src/wlp/render.c b/src/wlp/render.c index 078d521..0b48e92 100644 --- a/src/wlp/render.c +++ b/src/wlp/render.c @@ -2,59 +2,53 @@ #include "main.h" -static int lerp(int a, int b, float t) -{ - if (t > 1) t = 1; +static int lerp(int a, int b, float t) { + if (t > 1) + t = 1; return (int)((float)a + (float)t * ((float)b - (float)a)); } -static float clamp(float a, float min, float max) -{ - if (a < min) return min; - if (a > max) return max; +static float clamp(float a, float min, float max) { + if (a < min) + return min; + if (a > max) + return max; return a; } -static void lerpTargetPoint(Point *p, Point *target, float dT) -{ - p->x = lerp(p->x, target->x, dT * 4); // 4: smooth +static void lerpTargetPoint(Point* p, Point* target, float dT) { + p->x = lerp(p->x, target->x, dT * 4); // 4: smooth p->y = lerp(p->y, target->y, dT * 4); } -static void getRelativeTargetPoint( - Point *dest, const Point *globalTargetPoint, const Monitor *m -) -{ +static void getRelativeTargetPoint(Point* dest, const Point* globalTargetPoint, + const Monitor* m) { dest->x = globalTargetPoint->x - m->info.clientBounds.x; dest->y = globalTargetPoint->y - m->info.clientBounds.y; } -static void clampTargetPoint(Point *p, Monitor *m) -{ +static void clampTargetPoint(Point* p, Monitor* m) { p->x = clamp(p->x, 0, m->info.clientBounds.w); p->y = clamp(p->y, 0, m->info.clientBounds.h); } -static void comeBackTargetPoint(Point *p, Monitor *m) -{ +static void comeBackTargetPoint(Point* p, Monitor* m) { if (p->x < 0 || p->x > m->info.clientBounds.w || p->y < 0 || - p->y > m->info.clientBounds.h) - { + p->y > m->info.clientBounds.h) { p->x = m->info.clientBounds.w / 2; p->y = m->info.clientBounds.h / 2; } } -static float distanceSquared(const Point *p, const Point *q) -{ +static float distanceSquared(const Point* p, const Point* q) { return (p->x - q->x) * (p->x - q->x) + (p->y - q->y) * (p->y - q->y); } -static void renderMonitor( - App *app, Monitor *monitor, const Point *globalTargetPoint, float dT -) -{ - if (!monitor->info.config.loaded || !monitor->wlp.info.config.loaded) return; +static void renderMonitor(App* app, Monitor* monitor, + const Point* globalTargetPoint, float dT, + int firstRender) { + if (!monitor->info.config.loaded || !monitor->wlp.info.config.loaded) + return; Point targetPoint; getRelativeTargetPoint(&targetPoint, globalTargetPoint, monitor); @@ -66,18 +60,17 @@ static void renderMonitor( lerpTargetPoint(&monitor->currentPoint, &targetPoint, dT); - if (distanceSquared(&monitor->currentPoint, &targetPoint) < 1) return; + if (distanceSquared(&monitor->currentPoint, &targetPoint) < 1 && !firstRender) + return; - if (SDL_SetRenderTarget(monitor->renderer, monitor->wlp.tex) != 0) - { + if (SDL_SetRenderTarget(monitor->renderer, monitor->wlp.tex) != 0) { lwpLog(LOG_ERROR, "Error setting the renderer target: %s", SDL_GetError()); monitor->aborted = 1; return; } SDL_RenderClear(monitor->renderer); - for (int i = 0; i < monitor->wlp.info.config.layersCount; i++) - { + for (int i = 0; i < monitor->wlp.info.config.layersCount; i++) { SDL_Rect src = { .x = 0, .y = 0, @@ -85,21 +78,15 @@ static void renderMonitor( .h = monitor->wlp.originalH, }; - int x = - -((monitor->currentPoint.x - monitor->info.clientBounds.w / 2) * - monitor->wlp.info.config.layerConfigs[i].sensitivityX); - int y = - -((monitor->currentPoint.y - monitor->info.clientBounds.h / 2) * - monitor->wlp.info.config.layerConfigs[i].sensitivityY); + int x = -((monitor->currentPoint.x - monitor->info.clientBounds.w / 2) * + monitor->wlp.info.config.layerConfigs[i].sensitivityX); + int y = -((monitor->currentPoint.y - monitor->info.clientBounds.h / 2) * + monitor->wlp.info.config.layerConfigs[i].sensitivityY); for (int k = -monitor->wlp.info.config.repeatY; - k <= monitor->wlp.info.config.repeatY; - k++) - { + k <= monitor->wlp.info.config.repeatY; k++) { for (int j = -monitor->wlp.info.config.repeatX; - j <= monitor->wlp.info.config.repeatX; - j++) - { + j <= monitor->wlp.info.config.repeatX; j++) { SDL_Rect dest = { .x = x + j * monitor->info.config.wlpBounds.w, .y = y + k * monitor->info.config.wlpBounds.h, @@ -107,10 +94,8 @@ static void renderMonitor( .h = monitor->info.config.wlpBounds.h, }; - if (SDL_RenderCopy( - monitor->renderer, monitor->wlp.layers[i].tex, &src, &dest - ) != 0) - { + if (SDL_RenderCopy(monitor->renderer, monitor->wlp.layers[i].tex, &src, + &dest) != 0) { lwpLog(LOG_ERROR, "Error rendering copy: %s", SDL_GetError()); monitor->aborted = 1; } @@ -118,8 +103,7 @@ static void renderMonitor( } } - if (SDL_SetRenderTarget(monitor->renderer, monitor->tex) != 0) - { + if (SDL_SetRenderTarget(monitor->renderer, monitor->tex) != 0) { lwpLog(LOG_ERROR, "Error setting the renderer target: %s", SDL_GetError()); monitor->aborted = 1; } @@ -138,16 +122,14 @@ static void renderMonitor( .h = monitor->info.config.wlpBounds.h, }; - if (SDL_RenderCopy(monitor->renderer, monitor->wlp.tex, &src, &dest) != 0) - { + if (SDL_RenderCopy(monitor->renderer, monitor->wlp.tex, &src, &dest) != 0) { lwpLog(LOG_ERROR, "Error rendering copy: %s", SDL_GetError()); monitor->aborted = 1; } SDL_SetRenderTarget(monitor->renderer, NULL); - if (SDL_RenderCopy(monitor->renderer, monitor->tex, NULL, NULL) != 0) - { + if (SDL_RenderCopy(monitor->renderer, monitor->tex, NULL, NULL) != 0) { lwpLog(LOG_ERROR, "Error rendering copy: %s", SDL_GetError()); monitor->aborted = 1; } @@ -156,36 +138,20 @@ static void renderMonitor( SDL_Delay(1000 / app->config.targetFps); } -static void getInput(int *quit) -{ +static void getInput(int* quit) { SDL_Event event; while (SDL_PollEvent(&event)) - if (event.type == SDL_QUIT) (*quit) = 1; + if (event.type == SDL_QUIT) + (*quit) = 1; } -static void getTargetPoint(Point *p) -{ -#ifdef __WIN32 - POINT mPos; - GetCursorPos(&mPos); - p->x = mPos.x - GetSystemMetrics(SM_XVIRTUALSCREEN); - p->y = mPos.y - GetSystemMetrics(SM_YVIRTUALSCREEN); -#else - int x, y; - SDL_GetGlobalMouseState(&x, &y); - p->x = x; - p->y = y; -#endif -} - -void runWallpaperLoop(App *app) -{ +void runWallpaperLoop(App* app) { Point targetPoint; - int quit = 0; - while (!quit) - { + int firstRender = 1; + int quit = 0; + while (!quit) { static int lastTicks = 0; int ticks = SDL_GetTicks(); @@ -194,10 +160,12 @@ void runWallpaperLoop(App *app) getInput(&quit); - getTargetPoint(&targetPoint); + getTargetPoint(app, &targetPoint); - for (int m = 0; m < app->monitorsCount; m++) + for (int m = 0; m < app->monitorsCount; m++) { if (!app->monitors[m].aborted) - renderMonitor(app, app->monitors + m, &targetPoint, dT); + renderMonitor(app, app->monitors + m, &targetPoint, dT, firstRender); + } + firstRender = 0; } } diff --git a/src/wlp/targetPoint.c b/src/wlp/targetPoint.c new file mode 100644 index 0000000..1589765 --- /dev/null +++ b/src/wlp/targetPoint.c @@ -0,0 +1,34 @@ +#include "main.h" + +void getTargetPoint(App* app, Point* p) { + if (app->config.wndTargetPoint) { + +#ifdef __WIN32 + HWND focusedWnd = GetForegroundWindow(); + + if (focusedWnd) { + RECT rect; + + GetWindowRect(focusedWnd, &rect); + + p->x = rect.left + (rect.right - rect.left) / 2; + p->y = rect.top + (rect.bottom - rect.top) / 2; + } +#else +#endif + + } else { + +#ifdef __WIN32 + POINT mPos; + GetCursorPos(&mPos); + p->x = mPos.x - GetSystemMetrics(SM_XVIRTUALSCREEN); + p->y = mPos.y - GetSystemMetrics(SM_YVIRTUALSCREEN); +#else + int x, y; + SDL_GetGlobalMouseState(&x, &y); + p->x = x; + p->y = y; +#endif + } +} \ No newline at end of file