/* * Geminux Wallpaper configuration, image filtering, and placement geometry helpers. * Pure C11/POSIX with zero X11 or Cairo dependencies for unit-testability. */ #ifndef GEMINUX_WALLPAPER_H #define GEMINUX_WALLPAPER_H #include #include #include #include #include #include #include #include typedef enum { GEMINUX_WP_IMAGE = 0, GEMINUX_WP_SOLID = 1 } geminux_wp_type_t; typedef enum { GEMINUX_WP_FILL = 0, GEMINUX_WP_FIT = 1, GEMINUX_WP_CENTER = 2, GEMINUX_WP_TILE = 3, GEMINUX_WP_STRETCH = 4, GEMINUX_WP_SPAN = 5, GEMINUX_WP_MODE_COUNT = 6 } geminux_wp_mode_t; typedef struct { geminux_wp_type_t type; geminux_wp_mode_t mode; char path[768]; char color[16]; bool use_theme_bg; } geminux_wp_cfg_t; static inline bool geminux_wp_ci_eq(const char *a, const char *b) { if (!a || !b) return false; while (*a && *b) { if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) return false; a++; b++; } return *a == '\0' && *b == '\0'; } static inline void geminux_wp_cfg_init(geminux_wp_cfg_t *cfg) { if (!cfg) return; memset(cfg, 0, sizeof(*cfg)); cfg->type = GEMINUX_WP_IMAGE; cfg->mode = GEMINUX_WP_FILL; snprintf(cfg->color, sizeof(cfg->color), "#08090b"); cfg->use_theme_bg = true; } static inline const char *geminux_wp_mode_name(geminux_wp_mode_t m) { switch (m) { case GEMINUX_WP_FILL: return "fill"; case GEMINUX_WP_FIT: return "fit"; case GEMINUX_WP_CENTER: return "center"; case GEMINUX_WP_TILE: return "tile"; case GEMINUX_WP_STRETCH: return "stretch"; case GEMINUX_WP_SPAN: return "span"; default: return "fill"; } } static inline const char *geminux_wp_mode_label(geminux_wp_mode_t m) { switch (m) { case GEMINUX_WP_FILL: return "Fill"; case GEMINUX_WP_FIT: return "Fit"; case GEMINUX_WP_CENTER: return "Center"; case GEMINUX_WP_TILE: return "Tile"; case GEMINUX_WP_STRETCH: return "Stretch"; case GEMINUX_WP_SPAN: return "Span"; default: return "Fill"; } } static inline bool geminux_wp_mode_parse(const char *s, geminux_wp_mode_t *out) { if (!s || !s[0]) return false; geminux_wp_mode_t m; if (geminux_wp_ci_eq(s, "fill") || geminux_wp_ci_eq(s, "zoom")) m = GEMINUX_WP_FILL; else if (geminux_wp_ci_eq(s, "fit") || geminux_wp_ci_eq(s, "max")) m = GEMINUX_WP_FIT; else if (geminux_wp_ci_eq(s, "center") || geminux_wp_ci_eq(s, "centered")) m = GEMINUX_WP_CENTER; else if (geminux_wp_ci_eq(s, "tile") || geminux_wp_ci_eq(s, "tiled")) m = GEMINUX_WP_TILE; else if (geminux_wp_ci_eq(s, "stretch") || geminux_wp_ci_eq(s, "scale")) m = GEMINUX_WP_STRETCH; else if (geminux_wp_ci_eq(s, "span")) m = GEMINUX_WP_SPAN; else return false; if (out) *out = m; return true; } static inline int geminux_wp_hex_digit(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); if (c >= 'A' && c <= 'F') return 10 + (c - 'A'); return -1; } static inline bool geminux_wp_parse_hex(const char *s, int *r, int *g, int *b) { if (!s) return false; if (s[0] == '#') s++; for (int i = 0; i < 6; i++) { if (geminux_wp_hex_digit(s[i]) < 0) return false; } if (s[6] != '\0') return false; int rv = (geminux_wp_hex_digit(s[0]) << 4) | geminux_wp_hex_digit(s[1]); int gv = (geminux_wp_hex_digit(s[2]) << 4) | geminux_wp_hex_digit(s[3]); int bv = (geminux_wp_hex_digit(s[4]) << 4) | geminux_wp_hex_digit(s[5]); if (r) *r = rv; if (g) *g = gv; if (b) *b = bv; return true; } static inline void geminux_wp_format_hex(int r, int g, int b, char *out, size_t sz) { if (!out || sz == 0) return; if (r < 0) r = 0; else if (r > 255) r = 255; if (g < 0) g = 0; else if (g > 255) g = 255; if (b < 0) b = 0; else if (b > 255) b = 255; snprintf(out, sz, "#%02x%02x%02x", r, g, b); } static inline bool geminux_wp_is_image_name(const char *name) { if (!name || !name[0]) return false; const char *slash = strrchr(name, '/'); const char *base = slash ? (slash + 1) : name; const char *dot = strrchr(base, '.'); if (!dot || dot == base || dot[1] == '\0') return false; const char *ext = dot + 1; static const char *const EXTS[] = { "png", "jpg", "jpeg", "webp", "bmp", "gif", "tif", "tiff", "svg", "pnm", "ppm", "pgm", "xbm", "xpm" }; for (size_t i = 0; i < sizeof(EXTS) / sizeof(EXTS[0]); i++) { if (geminux_wp_ci_eq(ext, EXTS[i])) return true; } return false; } static inline void geminux_wp_calc_placement(int img_w, int img_h, int dst_w, int dst_h, geminux_wp_mode_t mode, double *out_x, double *out_y, double *out_w, double *out_h) { double x = 0.0, y = 0.0, w = 0.0, h = 0.0; if (img_w > 0 && img_h > 0 && dst_w > 0 && dst_h > 0) { double sx = (double)dst_w / (double)img_w; double sy = (double)dst_h / (double)img_h; switch (mode) { case GEMINUX_WP_FILL: case GEMINUX_WP_SPAN: { double s = (sx > sy) ? sx : sy; w = (double)img_w * s; h = (double)img_h * s; x = ((double)dst_w - w) * 0.5; y = ((double)dst_h - h) * 0.5; break; } case GEMINUX_WP_FIT: { double s = (sx < sy) ? sx : sy; w = (double)img_w * s; h = (double)img_h * s; x = ((double)dst_w - w) * 0.5; y = ((double)dst_h - h) * 0.5; break; } case GEMINUX_WP_CENTER: w = (double)img_w; h = (double)img_h; x = ((double)dst_w - w) * 0.5; y = ((double)dst_h - h) * 0.5; break; case GEMINUX_WP_STRETCH: x = 0.0; y = 0.0; w = (double)dst_w; h = (double)dst_h; break; case GEMINUX_WP_TILE: default: x = 0.0; y = 0.0; w = (double)img_w; h = (double)img_h; break; } } if (out_x) *out_x = x; if (out_y) *out_y = y; if (out_w) *out_w = w; if (out_h) *out_h = h; } static inline void geminux_wp_mkdir_p(const char *dir) { if (!dir || !dir[0]) return; char tmp[1024]; snprintf(tmp, sizeof(tmp), "%.*s", (int)(sizeof(tmp) - 1), dir); size_t len = strlen(tmp); if (len > 1 && tmp[len - 1] == '/') tmp[len - 1] = '\0'; for (char *p = tmp + 1; *p; p++) { if (*p == '/') { *p = '\0'; mkdir(tmp, 0755); *p = '/'; } } mkdir(tmp, 0755); } static inline bool geminux_wp_cfg_load(const char *conf_path, geminux_wp_cfg_t *cfg) { if (!cfg) return false; geminux_wp_cfg_init(cfg); if (!conf_path || !conf_path[0]) return false; FILE *f = fopen(conf_path, "r"); if (!f) return false; char line[1024]; while (fgets(line, sizeof(line), f)) { size_t n = strlen(line); while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r')) line[--n] = '\0'; if (line[0] == '\0' || line[0] == '#') continue; if (strncmp(line, "TYPE=", 5) == 0) { const char *v = line + 5; if (geminux_wp_ci_eq(v, "solid")) cfg->type = GEMINUX_WP_SOLID; else if (geminux_wp_ci_eq(v, "image")) cfg->type = GEMINUX_WP_IMAGE; } else if (strncmp(line, "PATH=", 5) == 0) { snprintf(cfg->path, sizeof(cfg->path), "%.*s", (int)(sizeof(cfg->path) - 1), line + 5); } else if (strncmp(line, "MODE=", 5) == 0) { geminux_wp_mode_t pm = GEMINUX_WP_FILL; if (geminux_wp_mode_parse(line + 5, &pm)) cfg->mode = pm; } else if (strncmp(line, "COLOR=", 6) == 0) { int r = 0, g = 0, b = 0; if (geminux_wp_parse_hex(line + 6, &r, &g, &b)) { geminux_wp_format_hex(r, g, b, cfg->color, sizeof(cfg->color)); } } else if (strncmp(line, "USE_THEME_BG=", 13) == 0) { cfg->use_theme_bg = (line[13] == '1'); } } fclose(f); return true; } static inline bool geminux_wp_cfg_save(const char *conf_path, const geminux_wp_cfg_t *cfg) { if (!conf_path || !conf_path[0] || !cfg) return false; const char *slash = strrchr(conf_path, '/'); if (slash && slash > conf_path) { char pdir[1024]; size_t dlen = (size_t)(slash - conf_path); if (dlen >= sizeof(pdir)) dlen = sizeof(pdir) - 1; memcpy(pdir, conf_path, dlen); pdir[dlen] = '\0'; geminux_wp_mkdir_p(pdir); } char clean_path[768]; size_t pi = 0; for (size_t i = 0; cfg->path[i] && pi + 1 < sizeof(clean_path); i++) { if (cfg->path[i] != '\n' && cfg->path[i] != '\r') clean_path[pi++] = cfg->path[i]; } clean_path[pi] = '\0'; char hex[16]; int r = 8, g = 9, b = 11; if (geminux_wp_parse_hex(cfg->color, &r, &g, &b)) { geminux_wp_format_hex(r, g, b, hex, sizeof(hex)); } else { snprintf(hex, sizeof(hex), "#08090b"); } char tmp[1024]; snprintf(tmp, sizeof(tmp), "%.*s.tmp.%ld", (int)(sizeof(tmp) - 32), conf_path, (long)getpid()); FILE *f = fopen(tmp, "w"); if (!f) return false; fprintf(f, "# Geminux wallpaper configuration\n"); fprintf(f, "TYPE=%s\n", cfg->type == GEMINUX_WP_SOLID ? "solid" : "image"); fprintf(f, "PATH=%s\n", clean_path); fprintf(f, "MODE=%s\n", geminux_wp_mode_name(cfg->mode)); fprintf(f, "COLOR=%s\n", hex); fprintf(f, "USE_THEME_BG=%d\n", cfg->use_theme_bg ? 1 : 0); if (fflush(f) != 0) { fclose(f); remove(tmp); return false; } fclose(f); if (rename(tmp, conf_path) != 0) { remove(tmp); return false; } return true; } #endif /* GEMINUX_WALLPAPER_H */