Backport WM/dmenu xerror & UTF-8 fixes, add wallpaper manager, and set Anodized default
- wm,dmenu: backport ANODE a92e196 fixes for xerror recursion guards, non-fatal RENDER error suppression, bounded getservertime() polling, pointer ungrab/AsyncBoth/GrabModeAsync grab safety, FC_COLOR=FcFalse substitution, and UTF-8 sanitization + Pango ellipsization in drw.c - wallpaper: port native X11 + Cairo wallpaper engine (geminux-wallpaper.h, geminux-wallpaper-x11.h) with pure-C gdk-pixbuf/PNG/JPEG/WebP/magick decoding, Wallpaper tab + CLI in geminux-themes, geminux-apply-wallpaper helper, and session/theme/HDMI/launcher hooks - theme: set Anodized as default theme in /etc/skel/.Xresources and compiled-in/script fallbacks across libgemui, dwm, dmenu, st, webarchy, geminux-sync-gtk-theme, and geminux-sync-dunst-theme - verify: extend check-rebuild.sh and test-theme-dunst-sync.py, prune stale skel theme artifacts in apply-geminux-overlay.sh, and rebuild prebuilt binaries
This commit is contained in:
1 parent
6fcc263256
commit
09c1e2ad36
32 files changed
+2852
-304
No files matched your search
@@ -38,8 +38,8 @@ geminux-net: geminux-net.c geminux-wifi-list.h geminux-bt-list.h ../geminux-daem
|
||||
geminux-audio: geminux-audio.c geminux-audio-parse.h ../libgemui/gemui.h
|
||||
$(CC) $(CFLAGS) -o $@ $< $(LIBS)
|
||||
|
||||
geminux-themes: geminux-themes.c geminux-xth.h ../libgemui/gemui.h
|
||||
$(CC) $(CFLAGS) -o $@ $< $(LIBS) -lm
|
||||
geminux-themes: geminux-themes.c geminux-xth.h geminux-wallpaper.h geminux-wallpaper-x11.h ../libgemui/gemui.h
|
||||
$(CC) $(CFLAGS) -DHAVE_LIBJPEG=1 -DHAVE_LIBWEBP=1 -o $@ $< $(LIBS) -lXrandr -lXinerama -ljpeg -lwebp -lm
|
||||
|
||||
geminux-%: geminux-%.c ../libgemui/gemui.h
|
||||
$(CC) $(CFLAGS) -o $@ $< $(LIBS)
|
||||
|
||||
@@ -58,6 +58,22 @@ enum {
|
||||
WORK_BT_REM,
|
||||
};
|
||||
|
||||
static void reapply_saved_wallpaper(void) {
|
||||
const char *xdg = getenv("XDG_CONFIG_HOME");
|
||||
const char *home = getenv("HOME");
|
||||
char conf[560];
|
||||
if (xdg && xdg[0]) {
|
||||
snprintf(conf, sizeof(conf), "%s/geminux/wallpaper.conf", xdg);
|
||||
} else {
|
||||
snprintf(conf, sizeof(conf), "%s/.config/geminux/wallpaper.conf",
|
||||
(home && home[0]) ? home : "/home/user");
|
||||
}
|
||||
if (access(conf, R_OK) == 0) {
|
||||
const char *wpv[] = {"geminux-apply-wallpaper", "--restore", NULL};
|
||||
gemui_spawn_detached(wpv);
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
char col1[128];
|
||||
char col2[96];
|
||||
@@ -1662,6 +1678,7 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
|
||||
const char *argv[] = {"xrandr", "--output", "HDMI-1", st->hdmi_on ? "--off" : "--auto", NULL};
|
||||
gemui_run_cmd_timeout(argv, NULL, 0, 2000);
|
||||
st->hdmi_on = !st->hdmi_on;
|
||||
reapply_saved_wallpaper();
|
||||
refresh_tab(st);
|
||||
}
|
||||
} else if (hit->id == HIT_ACTION) {
|
||||
@@ -1700,6 +1717,7 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
|
||||
} else if (hit->val == 4) {
|
||||
const char *argv[] = {"xrandr", "--output", "HDMI-1", "--same-as", "DSI-1", NULL};
|
||||
gemui_run_cmd_timeout(argv, NULL, 0, 2000);
|
||||
reapply_saved_wallpaper();
|
||||
refresh_tab(st);
|
||||
}
|
||||
}
|
||||
|
||||
+1062
-59
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,758 @@
|
||||
/*
|
||||
* Geminux X11 + Cairo image loader & native root Pixmap wallpaper engine.
|
||||
* Included by src/geminux-apps/geminux-themes.c (uses pure-C gdk-pixbuf dlopen,
|
||||
* Cairo PNG, libjpeg, libwebp, or ImageMagick fallback).
|
||||
*/
|
||||
#ifndef GEMINUX_WALLPAPER_X11_H
|
||||
#define GEMINUX_WALLPAPER_X11_H
|
||||
|
||||
#include "geminux-wallpaper.h"
|
||||
#include "../libgemui/gemui.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <stdint.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <X11/Xresource.h>
|
||||
#include <X11/extensions/Xrandr.h>
|
||||
#include <X11/extensions/Xinerama.h>
|
||||
#include <cairo/cairo.h>
|
||||
#include <cairo/cairo-xlib.h>
|
||||
|
||||
#ifdef HAVE_LIBJPEG
|
||||
#include <jpeglib.h>
|
||||
#include <setjmp.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LIBWEBP
|
||||
#include <webp/decode.h>
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
int w;
|
||||
int h;
|
||||
} geminux_wp_mon_t;
|
||||
|
||||
static inline bool geminux_wp_have_cmd(const char *cmd) {
|
||||
if (!cmd || !cmd[0]) return false;
|
||||
if (strchr(cmd, '/')) return access(cmd, X_OK) == 0;
|
||||
const char *path_env = getenv("PATH");
|
||||
if (!path_env || !path_env[0]) return false;
|
||||
|
||||
const char *p = path_env;
|
||||
while (*p) {
|
||||
const char *colon = strchr(p, ':');
|
||||
size_t dlen = colon ? (size_t)(colon - p) : strlen(p);
|
||||
char cand[768];
|
||||
if (dlen == 0) {
|
||||
snprintf(cand, sizeof(cand), "./%s", cmd);
|
||||
} else if (dlen < sizeof(cand) - 2) {
|
||||
memcpy(cand, p, dlen);
|
||||
if (cand[dlen - 1] == '/') cand[dlen] = '\0';
|
||||
else {
|
||||
cand[dlen] = '/';
|
||||
cand[dlen + 1] = '\0';
|
||||
}
|
||||
size_t cur = strlen(cand);
|
||||
snprintf(cand + cur, sizeof(cand) - cur, "%s", cmd);
|
||||
} else {
|
||||
cand[0] = '\0';
|
||||
}
|
||||
if (cand[0] && access(cand, X_OK) == 0) return true;
|
||||
if (!colon) break;
|
||||
p = colon + 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline cairo_surface_t *geminux_wp_rgba_to_cairo(const unsigned char *px,
|
||||
int w, int h,
|
||||
int nc, int stride) {
|
||||
if (!px || w <= 0 || h <= 0 || (nc != 3 && nc != 4) || stride < w * nc) return NULL;
|
||||
cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
|
||||
if (!surf || cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS) {
|
||||
if (surf) cairo_surface_destroy(surf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cairo_surface_flush(surf);
|
||||
unsigned char *dst_data = cairo_image_surface_get_data(surf);
|
||||
int dst_stride = cairo_image_surface_get_stride(surf);
|
||||
|
||||
for (int y = 0; y < h; y++) {
|
||||
const unsigned char *sp = px + (size_t)y * (size_t)stride;
|
||||
uint32_t *dp = (uint32_t *)(dst_data + (size_t)y * (size_t)dst_stride);
|
||||
for (int x = 0; x < w; x++) {
|
||||
uint32_t r = sp[0];
|
||||
uint32_t g = sp[1];
|
||||
uint32_t b = sp[2];
|
||||
uint32_t a = (nc == 4) ? sp[3] : 255U;
|
||||
if (a == 0U) {
|
||||
dp[x] = 0U;
|
||||
} else if (a == 255U) {
|
||||
dp[x] = (255U << 24) | (r << 16) | (g << 8) | b;
|
||||
} else {
|
||||
uint32_t pr = (r * a + 127U) / 255U;
|
||||
uint32_t pg = (g * a + 127U) / 255U;
|
||||
uint32_t pb_c = (b * a + 127U) / 255U;
|
||||
dp[x] = (a << 24) | (pr << 16) | (pg << 8) | pb_c;
|
||||
}
|
||||
sp += nc;
|
||||
}
|
||||
}
|
||||
|
||||
cairo_surface_mark_dirty(surf);
|
||||
return surf;
|
||||
}
|
||||
|
||||
static inline cairo_surface_t *geminux_wp_try_cairo_png(const char *path) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
unsigned char sig[8];
|
||||
size_t nr = fread(sig, 1, sizeof(sig), f);
|
||||
fclose(f);
|
||||
if (nr != 8) return NULL;
|
||||
static const unsigned char PNG_SIG[8] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'};
|
||||
if (memcmp(sig, PNG_SIG, 8) != 0) return NULL;
|
||||
|
||||
cairo_surface_t *s = cairo_image_surface_create_from_png(path);
|
||||
if (!s || cairo_surface_status(s) != CAIRO_STATUS_SUCCESS ||
|
||||
cairo_image_surface_get_width(s) <= 0 || cairo_image_surface_get_height(s) <= 0) {
|
||||
if (s) cairo_surface_destroy(s);
|
||||
return NULL;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
typedef void *(*geminux_gp_new_fn)(const char *, void **);
|
||||
typedef void *(*geminux_gp_orient_fn)(void *);
|
||||
typedef int (*geminux_gp_int_fn)(const void *);
|
||||
typedef const unsigned char *(*geminux_gp_pixels_fn)(const void *);
|
||||
typedef void (*geminux_gp_unref_fn)(void *);
|
||||
|
||||
static inline void geminux_wp_dlsym_copy(void *dst_fn_ptr, void *handle, const char *sym) {
|
||||
void *p = handle ? dlsym(handle, sym) : NULL;
|
||||
memcpy(dst_fn_ptr, &p, sizeof(p));
|
||||
}
|
||||
|
||||
static inline cairo_surface_t *geminux_wp_try_gdk_pixbuf(const char *path) {
|
||||
static int init_state = 0; /* 0=uninit, 1=ready, -1=unavailable */
|
||||
static geminux_gp_new_fn fn_new = NULL;
|
||||
static geminux_gp_orient_fn fn_orient = NULL;
|
||||
static geminux_gp_int_fn fn_w = NULL;
|
||||
static geminux_gp_int_fn fn_h = NULL;
|
||||
static geminux_gp_int_fn fn_nc = NULL;
|
||||
static geminux_gp_int_fn fn_bps = NULL;
|
||||
static geminux_gp_int_fn fn_stride = NULL;
|
||||
static geminux_gp_pixels_fn fn_pixels = NULL;
|
||||
static geminux_gp_unref_fn fn_unref = NULL;
|
||||
|
||||
if (init_state == 0) {
|
||||
void *pb_dl = dlopen("libgdk_pixbuf-2.0.so.0", RTLD_LAZY);
|
||||
if (!pb_dl) {
|
||||
init_state = -1;
|
||||
return NULL;
|
||||
}
|
||||
void *gobj_dl = dlopen("libgobject-2.0.so.0", RTLD_LAZY);
|
||||
geminux_wp_dlsym_copy(&fn_new, pb_dl, "gdk_pixbuf_new_from_file");
|
||||
geminux_wp_dlsym_copy(&fn_orient, pb_dl, "gdk_pixbuf_apply_embedded_orientation");
|
||||
geminux_wp_dlsym_copy(&fn_w, pb_dl, "gdk_pixbuf_get_width");
|
||||
geminux_wp_dlsym_copy(&fn_h, pb_dl, "gdk_pixbuf_get_height");
|
||||
geminux_wp_dlsym_copy(&fn_nc, pb_dl, "gdk_pixbuf_get_n_channels");
|
||||
geminux_wp_dlsym_copy(&fn_bps, pb_dl, "gdk_pixbuf_get_bits_per_sample");
|
||||
geminux_wp_dlsym_copy(&fn_stride, pb_dl, "gdk_pixbuf_get_rowstride");
|
||||
geminux_wp_dlsym_copy(&fn_pixels, pb_dl, "gdk_pixbuf_read_pixels");
|
||||
geminux_wp_dlsym_copy(&fn_unref, pb_dl, "g_object_unref");
|
||||
if (!fn_unref && gobj_dl) {
|
||||
geminux_wp_dlsym_copy(&fn_unref, gobj_dl, "g_object_unref");
|
||||
}
|
||||
if (!fn_new || !fn_w || !fn_h || !fn_nc || !fn_bps || !fn_stride || !fn_pixels || !fn_unref) {
|
||||
if (gobj_dl) dlclose(gobj_dl);
|
||||
dlclose(pb_dl);
|
||||
init_state = -1;
|
||||
return NULL;
|
||||
}
|
||||
init_state = 1;
|
||||
}
|
||||
if (init_state < 0) return NULL;
|
||||
|
||||
void *pb = fn_new(path, NULL);
|
||||
if (!pb) return NULL;
|
||||
if (fn_orient) {
|
||||
void *ori = fn_orient(pb);
|
||||
if (ori) {
|
||||
fn_unref(pb);
|
||||
pb = ori;
|
||||
}
|
||||
}
|
||||
|
||||
int w = fn_w(pb);
|
||||
int h = fn_h(pb);
|
||||
int nc = fn_nc(pb);
|
||||
int bps = fn_bps(pb);
|
||||
int stride = fn_stride(pb);
|
||||
const unsigned char *px = fn_pixels(pb);
|
||||
|
||||
cairo_surface_t *surf = (bps == 8) ? geminux_wp_rgba_to_cairo(px, w, h, nc, stride) : NULL;
|
||||
fn_unref(pb);
|
||||
return surf;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBJPEG
|
||||
typedef struct {
|
||||
struct jpeg_error_mgr pub;
|
||||
jmp_buf setjmp_buf;
|
||||
} geminux_wp_jpeg_err_t;
|
||||
|
||||
static void geminux_wp_jpeg_error_exit(j_common_ptr cinfo) {
|
||||
geminux_wp_jpeg_err_t *jerr = (geminux_wp_jpeg_err_t *)cinfo->err;
|
||||
longjmp(jerr->setjmp_buf, 1);
|
||||
}
|
||||
|
||||
static inline cairo_surface_t *geminux_wp_try_libjpeg(const char *path) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
unsigned char magic[2];
|
||||
size_t nr = fread(magic, 1, sizeof(magic), f);
|
||||
rewind(f);
|
||||
if (nr != 2 || magic[0] != 0xff || magic[1] != 0xd8) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct jpeg_decompress_struct cinfo;
|
||||
geminux_wp_jpeg_err_t jerr;
|
||||
cairo_surface_t *volatile surf = NULL;
|
||||
unsigned char *volatile row_buf = NULL;
|
||||
|
||||
cinfo.err = jpeg_std_error(&jerr.pub);
|
||||
jerr.pub.error_exit = geminux_wp_jpeg_error_exit;
|
||||
if (setjmp(jerr.setjmp_buf)) {
|
||||
free(row_buf);
|
||||
if (surf) cairo_surface_destroy(surf);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jpeg_create_decompress(&cinfo);
|
||||
jpeg_stdio_src(&cinfo, f);
|
||||
jpeg_read_header(&cinfo, TRUE);
|
||||
cinfo.out_color_space = JCS_RGB;
|
||||
jpeg_start_decompress(&cinfo);
|
||||
|
||||
int w = (int)cinfo.output_width;
|
||||
int h = (int)cinfo.output_height;
|
||||
int nc = (int)cinfo.output_components;
|
||||
if (w <= 0 || h <= 0 || nc != 3) {
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
|
||||
if (cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS) {
|
||||
cairo_surface_destroy(surf);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
row_buf = malloc((size_t)w * 3U);
|
||||
if (!row_buf) {
|
||||
cairo_surface_destroy(surf);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cairo_surface_flush(surf);
|
||||
unsigned char *dst_data = cairo_image_surface_get_data(surf);
|
||||
int dst_stride = cairo_image_surface_get_stride(surf);
|
||||
|
||||
while (cinfo.output_scanline < cinfo.output_height) {
|
||||
int y = (int)cinfo.output_scanline;
|
||||
JSAMPROW ptr = row_buf;
|
||||
jpeg_read_scanlines(&cinfo, &ptr, 1);
|
||||
uint32_t *dp = (uint32_t *)(dst_data + (size_t)y * (size_t)dst_stride);
|
||||
for (int x = 0; x < w; x++) {
|
||||
uint32_t r = row_buf[x * 3];
|
||||
uint32_t g = row_buf[x * 3 + 1];
|
||||
uint32_t b = row_buf[x * 3 + 2];
|
||||
dp[x] = (255U << 24) | (r << 16) | (g << 8) | b;
|
||||
}
|
||||
}
|
||||
|
||||
cairo_surface_mark_dirty(surf);
|
||||
free(row_buf);
|
||||
jpeg_finish_decompress(&cinfo);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
fclose(f);
|
||||
return surf;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LIBWEBP
|
||||
static inline cairo_surface_t *geminux_wp_try_libwebp(const char *path) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
unsigned char hdr[12];
|
||||
size_t nr = fread(hdr, 1, sizeof(hdr), f);
|
||||
if (nr != sizeof(hdr) || memcmp(hdr, "RIFF", 4) != 0 || memcmp(hdr + 8, "WEBP", 4) != 0) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct stat sb;
|
||||
if (fstat(fileno(f), &sb) != 0 || !S_ISREG(sb.st_mode) ||
|
||||
sb.st_size <= 12 || (uint64_t)sb.st_size > 64ULL * 1024ULL * 1024ULL) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t fsz = (size_t)sb.st_size;
|
||||
uint8_t *raw = malloc(fsz);
|
||||
if (!raw) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
rewind(f);
|
||||
size_t got = fread(raw, 1, fsz, f);
|
||||
fclose(f);
|
||||
if (got != fsz) {
|
||||
free(raw);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int w = 0, h = 0;
|
||||
uint8_t *rgba = WebPDecodeRGBA(raw, fsz, &w, &h);
|
||||
free(raw);
|
||||
if (!rgba) return NULL;
|
||||
|
||||
cairo_surface_t *surf = geminux_wp_rgba_to_cairo(rgba, w, h, 4, w * 4);
|
||||
WebPFree(rgba);
|
||||
return surf;
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline cairo_surface_t *geminux_wp_try_magick_fallback(const char *path) {
|
||||
const char *cmd = geminux_wp_have_cmd("magick") ? "magick"
|
||||
: (geminux_wp_have_cmd("convert") ? "convert" : NULL);
|
||||
if (!cmd) return NULL;
|
||||
|
||||
const char *tmpdir = getenv("TMPDIR");
|
||||
if (!tmpdir || !tmpdir[0]) tmpdir = "/tmp";
|
||||
char tmpl[320];
|
||||
snprintf(tmpl, sizeof(tmpl), "%.*s/geminux-wp-XXXXXX", (int)(sizeof(tmpl) - 24), tmpdir);
|
||||
int fd = mkstemp(tmpl);
|
||||
if (fd < 0) return NULL;
|
||||
close(fd);
|
||||
|
||||
char out_spec[336];
|
||||
snprintf(out_spec, sizeof(out_spec), "png:%s", tmpl);
|
||||
const char *av[] = {cmd, path, out_spec, NULL};
|
||||
cairo_surface_t *s = NULL;
|
||||
if (gemui_run_cmd_timeout(av, NULL, 0, 8000) == 0) {
|
||||
s = geminux_wp_try_cairo_png(tmpl);
|
||||
}
|
||||
unlink(tmpl);
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline cairo_surface_t *geminux_wp_load_surface(const char *path,
|
||||
int *out_orig_w,
|
||||
int *out_orig_h) {
|
||||
if (out_orig_w) *out_orig_w = 0;
|
||||
if (out_orig_h) *out_orig_h = 0;
|
||||
if (!path || !path[0]) return NULL;
|
||||
|
||||
struct stat sb;
|
||||
if (stat(path, &sb) != 0 || !S_ISREG(sb.st_mode)) return NULL;
|
||||
|
||||
cairo_surface_t *s = geminux_wp_try_gdk_pixbuf(path);
|
||||
if (!s) s = geminux_wp_try_cairo_png(path);
|
||||
#ifdef HAVE_LIBJPEG
|
||||
if (!s) s = geminux_wp_try_libjpeg(path);
|
||||
#endif
|
||||
#ifdef HAVE_LIBWEBP
|
||||
if (!s) s = geminux_wp_try_libwebp(path);
|
||||
#endif
|
||||
if (!s) s = geminux_wp_try_magick_fallback(path);
|
||||
|
||||
if (s) {
|
||||
if (out_orig_w) *out_orig_w = cairo_image_surface_get_width(s);
|
||||
if (out_orig_h) *out_orig_h = cairo_image_surface_get_height(s);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline cairo_surface_t *geminux_wp_make_preview_surface(const char *path,
|
||||
int max_w, int max_h,
|
||||
int *out_orig_w,
|
||||
int *out_orig_h) {
|
||||
int ow = 0, oh = 0;
|
||||
cairo_surface_t *full = geminux_wp_load_surface(path, &ow, &oh);
|
||||
if (out_orig_w) *out_orig_w = ow;
|
||||
if (out_orig_h) *out_orig_h = oh;
|
||||
if (!full || ow <= 0 || oh <= 0) return full;
|
||||
|
||||
if (max_w <= 0 || max_h <= 0 || (ow <= max_w && oh <= max_h)) {
|
||||
return full;
|
||||
}
|
||||
|
||||
double sx = (double)max_w / (double)ow;
|
||||
double sy = (double)max_h / (double)oh;
|
||||
double sc = (sx < sy) ? sx : sy;
|
||||
int tw = (int)((double)ow * sc + 0.5);
|
||||
int th = (int)((double)oh * sc + 0.5);
|
||||
if (tw < 1) tw = 1;
|
||||
if (th < 1) th = 1;
|
||||
|
||||
cairo_surface_t *thumb = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, tw, th);
|
||||
if (cairo_surface_status(thumb) != CAIRO_STATUS_SUCCESS) {
|
||||
cairo_surface_destroy(thumb);
|
||||
return full;
|
||||
}
|
||||
cairo_t *cr = cairo_create(thumb);
|
||||
cairo_scale(cr, (double)tw / (double)ow, (double)th / (double)oh);
|
||||
cairo_set_source_surface(cr, full, 0.0, 0.0);
|
||||
cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BEST);
|
||||
cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_PAD);
|
||||
cairo_paint(cr);
|
||||
cairo_destroy(cr);
|
||||
cairo_surface_destroy(full);
|
||||
return thumb;
|
||||
}
|
||||
|
||||
static inline void geminux_wp_render_onto_cairo(cairo_t *cr,
|
||||
double rx, double ry,
|
||||
double rw, double rh,
|
||||
const geminux_wp_cfg_t *cfg,
|
||||
cairo_surface_t *img,
|
||||
const char *resolved_bg_hex) {
|
||||
if (!cr || rw <= 0.0 || rh <= 0.0) return;
|
||||
|
||||
int r = 8, g = 9, b = 11;
|
||||
if (!geminux_wp_parse_hex(resolved_bg_hex, &r, &g, &b)) {
|
||||
r = 8; g = 9; b = 11;
|
||||
}
|
||||
|
||||
cairo_save(cr);
|
||||
cairo_rectangle(cr, rx, ry, rw, rh);
|
||||
cairo_clip(cr);
|
||||
|
||||
cairo_set_source_rgb(cr, (double)r / 255.0, (double)g / 255.0, (double)b / 255.0);
|
||||
cairo_paint(cr);
|
||||
|
||||
if (cfg && cfg->type == GEMINUX_WP_IMAGE && img &&
|
||||
cairo_surface_status(img) == CAIRO_STATUS_SUCCESS) {
|
||||
int iw = cairo_image_surface_get_width(img);
|
||||
int ih = cairo_image_surface_get_height(img);
|
||||
if (iw > 0 && ih > 0) {
|
||||
if (cfg->mode == GEMINUX_WP_TILE) {
|
||||
cairo_translate(cr, rx, ry);
|
||||
cairo_set_source_surface(cr, img, 0.0, 0.0);
|
||||
cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_REPEAT);
|
||||
cairo_paint(cr);
|
||||
} else {
|
||||
double dx = 0.0, dy = 0.0, dw = 0.0, dh = 0.0;
|
||||
geminux_wp_calc_placement(iw, ih, (int)(rw + 0.5), (int)(rh + 0.5),
|
||||
cfg->mode, &dx, &dy, &dw, &dh);
|
||||
if (dw > 0.0 && dh > 0.0) {
|
||||
cairo_rectangle(cr, rx + dx, ry + dy, dw, dh);
|
||||
cairo_clip(cr);
|
||||
cairo_translate(cr, rx + dx, ry + dy);
|
||||
cairo_scale(cr, dw / (double)iw, dh / (double)ih);
|
||||
cairo_set_source_surface(cr, img, 0.0, 0.0);
|
||||
cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BEST);
|
||||
cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_PAD);
|
||||
cairo_paint(cr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cairo_restore(cr);
|
||||
}
|
||||
|
||||
static inline void geminux_wp_read_theme_bg(Display *dpy_opt, char *out_hex, size_t sz) {
|
||||
if (!out_hex || sz == 0) return;
|
||||
snprintf(out_hex, sz, "#08090b");
|
||||
|
||||
Display *dpy = dpy_opt ? dpy_opt : XOpenDisplay(NULL);
|
||||
bool found = false;
|
||||
if (dpy) {
|
||||
XrmInitialize();
|
||||
char *prop_rms = NULL;
|
||||
const char *rms = NULL;
|
||||
Atom actual_type = None;
|
||||
int actual_fmt = 0;
|
||||
unsigned long nitems = 0, bytes_after = 0;
|
||||
unsigned char *prop_data = NULL;
|
||||
Window root = DefaultRootWindow(dpy);
|
||||
if (XGetWindowProperty(dpy, root, XA_RESOURCE_MANAGER, 0, 1024L * 1024L, False, XA_STRING,
|
||||
&actual_type, &actual_fmt, &nitems, &bytes_after, &prop_data) == Success) {
|
||||
if (prop_data && actual_fmt == 8 && nitems > 0) {
|
||||
prop_rms = (char *)prop_data;
|
||||
rms = prop_rms;
|
||||
} else if (prop_data) {
|
||||
XFree(prop_data);
|
||||
}
|
||||
}
|
||||
if (!rms || !rms[0]) {
|
||||
rms = XResourceManagerString(dpy);
|
||||
}
|
||||
if (rms && rms[0]) {
|
||||
XrmDatabase db = XrmGetStringDatabase(rms);
|
||||
if (db) {
|
||||
char *rtype = NULL;
|
||||
XrmValue rval;
|
||||
if (XrmGetResource(db, "theme.bgMain", "Theme.BgMain", &rtype, &rval) && rval.addr) {
|
||||
int r = 0, g = 0, b = 0;
|
||||
if (geminux_wp_parse_hex(rval.addr, &r, &g, &b)) {
|
||||
geminux_wp_format_hex(r, g, b, out_hex, sz);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
XrmDestroyDatabase(db);
|
||||
}
|
||||
}
|
||||
if (prop_rms) XFree(prop_rms);
|
||||
if (!dpy_opt) XCloseDisplay(dpy);
|
||||
}
|
||||
|
||||
if (found) return;
|
||||
|
||||
const char *home = getenv("HOME");
|
||||
if (!home || !home[0]) return;
|
||||
char xpath[640];
|
||||
snprintf(xpath, sizeof(xpath), "%s/.Xresources", home);
|
||||
FILE *f = fopen(xpath, "r");
|
||||
if (!f) return;
|
||||
|
||||
char line[512];
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
const char *p = line;
|
||||
while (*p == ' ' || *p == '\t') p++;
|
||||
if (strncmp(p, "theme.bgMain:", 13) == 0) {
|
||||
p += 13;
|
||||
while (*p == ' ' || *p == '\t') p++;
|
||||
char tok[32];
|
||||
size_t ti = 0;
|
||||
while (p[ti] && !isspace((unsigned char)p[ti]) && ti + 1 < sizeof(tok)) {
|
||||
tok[ti] = p[ti];
|
||||
ti++;
|
||||
}
|
||||
tok[ti] = '\0';
|
||||
int r = 0, g = 0, b = 0;
|
||||
if (geminux_wp_parse_hex(tok, &r, &g, &b)) {
|
||||
geminux_wp_format_hex(r, g, b, out_hex, sz);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
static inline int geminux_wp_noop_xerr(Display *d, XErrorEvent *e) {
|
||||
(void)d;
|
||||
(void)e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int geminux_wp_query_monitors(Display *dpy, Window root, int sw, int sh,
|
||||
geminux_wp_mon_t *out, int max_out) {
|
||||
int n = 0;
|
||||
XRRScreenResources *res = XRRGetScreenResourcesCurrent(dpy, root);
|
||||
if (!res) res = XRRGetScreenResources(dpy, root);
|
||||
if (res) {
|
||||
RRCrtc seen_crtcs[16];
|
||||
int n_seen = 0;
|
||||
for (int i = 0; i < res->noutput && n < max_out; i++) {
|
||||
XRROutputInfo *oi = XRRGetOutputInfo(dpy, res, res->outputs[i]);
|
||||
if (!oi) continue;
|
||||
if (oi->connection == RR_Connected && oi->crtc != None) {
|
||||
bool dup = false;
|
||||
for (int s = 0; s < n_seen; s++) {
|
||||
if (seen_crtcs[s] == oi->crtc) { dup = true; break; }
|
||||
}
|
||||
if (!dup && n_seen < 16) {
|
||||
seen_crtcs[n_seen++] = oi->crtc;
|
||||
XRRCrtcInfo *ci = XRRGetCrtcInfo(dpy, res, oi->crtc);
|
||||
if (ci) {
|
||||
if (ci->width > 0 && ci->height > 0) {
|
||||
out[n].x = ci->x;
|
||||
out[n].y = ci->y;
|
||||
out[n].w = (int)ci->width;
|
||||
out[n].h = (int)ci->height;
|
||||
n++;
|
||||
}
|
||||
XRRFreeCrtcInfo(ci);
|
||||
}
|
||||
}
|
||||
}
|
||||
XRRFreeOutputInfo(oi);
|
||||
}
|
||||
XRRFreeScreenResources(res);
|
||||
}
|
||||
|
||||
if (n <= 0 && XineramaIsActive(dpy)) {
|
||||
int nx = 0;
|
||||
XineramaScreenInfo *xs = XineramaQueryScreens(dpy, &nx);
|
||||
if (xs) {
|
||||
for (int i = 0; i < nx && n < max_out; i++) {
|
||||
if (xs[i].width > 0 && xs[i].height > 0) {
|
||||
out[n].x = xs[i].x_org;
|
||||
out[n].y = xs[i].y_org;
|
||||
out[n].w = xs[i].width;
|
||||
out[n].h = xs[i].height;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
XFree(xs);
|
||||
}
|
||||
}
|
||||
|
||||
if (n <= 0 && max_out > 0) {
|
||||
out[0].x = 0;
|
||||
out[0].y = 0;
|
||||
out[0].w = sw;
|
||||
out[0].h = sh;
|
||||
n = 1;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline bool geminux_wp_read_root_pmap(Display *dpy, Window root, Atom prop, unsigned long *out_id) {
|
||||
Atom actual_type = None;
|
||||
int actual_fmt = 0;
|
||||
unsigned long nitems = 0, bytes_after = 0;
|
||||
unsigned char *data = NULL;
|
||||
bool ok = false;
|
||||
|
||||
if (XGetWindowProperty(dpy, root, prop, 0, 1, False, XA_PIXMAP,
|
||||
&actual_type, &actual_fmt, &nitems, &bytes_after, &data) == Success) {
|
||||
if (actual_type == XA_PIXMAP && actual_fmt == 32 && nitems == 1 && data) {
|
||||
unsigned long val = 0;
|
||||
memcpy(&val, data, sizeof(unsigned long));
|
||||
if (val != 0UL) {
|
||||
*out_id = val;
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
if (data) XFree(data);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static inline bool geminux_wp_apply_x11(const geminux_wp_cfg_t *cfg,
|
||||
const char *theme_bg_hex,
|
||||
char *err_buf, size_t err_sz) {
|
||||
if (err_buf && err_sz > 0) err_buf[0] = '\0';
|
||||
if (!cfg) {
|
||||
if (err_buf && err_sz > 0) snprintf(err_buf, err_sz, "Invalid wallpaper config");
|
||||
return false;
|
||||
}
|
||||
|
||||
char bg_hex[16];
|
||||
int r = 0, g = 0, b = 0;
|
||||
if (cfg->use_theme_bg && theme_bg_hex && geminux_wp_parse_hex(theme_bg_hex, &r, &g, &b)) {
|
||||
geminux_wp_format_hex(r, g, b, bg_hex, sizeof(bg_hex));
|
||||
} else if (geminux_wp_parse_hex(cfg->color, &r, &g, &b)) {
|
||||
geminux_wp_format_hex(r, g, b, bg_hex, sizeof(bg_hex));
|
||||
} else {
|
||||
snprintf(bg_hex, sizeof(bg_hex), "#08090b");
|
||||
}
|
||||
|
||||
cairo_surface_t *img = NULL;
|
||||
if (cfg->type == GEMINUX_WP_IMAGE) {
|
||||
img = geminux_wp_load_surface(cfg->path, NULL, NULL);
|
||||
if (!img) {
|
||||
if (err_buf && err_sz > 0) {
|
||||
snprintf(err_buf, err_sz, "Unable to load image: %.180s",
|
||||
cfg->path[0] ? cfg->path : "(no file selected)");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* Dedicated Display connection so RetainPermanent never affects libgemui's main Display */
|
||||
Display *wdpy = XOpenDisplay(NULL);
|
||||
if (!wdpy) {
|
||||
if (img) cairo_surface_destroy(img);
|
||||
if (err_buf && err_sz > 0) snprintf(err_buf, err_sz, "Failed to open X11 display");
|
||||
return false;
|
||||
}
|
||||
|
||||
int scr = DefaultScreen(wdpy);
|
||||
Window root = RootWindow(wdpy, scr);
|
||||
int sw = DisplayWidth(wdpy, scr);
|
||||
int sh = DisplayHeight(wdpy, scr);
|
||||
if (sw <= 0 || sh <= 0) {
|
||||
if (img) cairo_surface_destroy(img);
|
||||
XCloseDisplay(wdpy);
|
||||
if (err_buf && err_sz > 0) snprintf(err_buf, err_sz, "Invalid X11 screen dimensions");
|
||||
return false;
|
||||
}
|
||||
|
||||
geminux_wp_mon_t mons[16];
|
||||
int n_mons = geminux_wp_query_monitors(wdpy, root, sw, sh, mons, 16);
|
||||
|
||||
Pixmap pmap = XCreatePixmap(wdpy, root, (unsigned int)sw, (unsigned int)sh,
|
||||
(unsigned int)DefaultDepth(wdpy, scr));
|
||||
cairo_surface_t *xsurf = cairo_xlib_surface_create(wdpy, pmap, DefaultVisual(wdpy, scr), sw, sh);
|
||||
cairo_t *cr = cairo_create(xsurf);
|
||||
|
||||
geminux_wp_cfg_t fill_only;
|
||||
geminux_wp_cfg_init(&fill_only);
|
||||
fill_only.type = GEMINUX_WP_SOLID;
|
||||
fill_only.use_theme_bg = false;
|
||||
geminux_wp_render_onto_cairo(cr, 0.0, 0.0, (double)sw, (double)sh, &fill_only, NULL, bg_hex);
|
||||
|
||||
if (cfg->type == GEMINUX_WP_IMAGE) {
|
||||
if (cfg->mode == GEMINUX_WP_SPAN || cfg->mode == GEMINUX_WP_TILE) {
|
||||
geminux_wp_render_onto_cairo(cr, 0.0, 0.0, (double)sw, (double)sh, cfg, img, bg_hex);
|
||||
} else {
|
||||
for (int i = 0; i < n_mons; i++) {
|
||||
geminux_wp_render_onto_cairo(cr, (double)mons[i].x, (double)mons[i].y,
|
||||
(double)mons[i].w, (double)mons[i].h,
|
||||
cfg, img, bg_hex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cairo_destroy(cr);
|
||||
cairo_surface_flush(xsurf);
|
||||
cairo_surface_destroy(xsurf);
|
||||
if (img) cairo_surface_destroy(img);
|
||||
|
||||
Atom a_xroot = XInternAtom(wdpy, "_XROOTPMAP_ID", False);
|
||||
Atom a_eset = XInternAtom(wdpy, "ESETROOT_PMAP_ID", False);
|
||||
|
||||
XGrabServer(wdpy);
|
||||
unsigned long old_xroot = 0, old_eset = 0;
|
||||
if (geminux_wp_read_root_pmap(wdpy, root, a_xroot, &old_xroot) &&
|
||||
geminux_wp_read_root_pmap(wdpy, root, a_eset, &old_eset) &&
|
||||
old_xroot == old_eset && old_xroot != (unsigned long)pmap) {
|
||||
XErrorHandler prev = XSetErrorHandler(geminux_wp_noop_xerr);
|
||||
XKillClient(wdpy, (XID)old_xroot);
|
||||
XSync(wdpy, False);
|
||||
XSetErrorHandler(prev);
|
||||
}
|
||||
|
||||
unsigned long pval = (unsigned long)pmap;
|
||||
XSetWindowBackgroundPixmap(wdpy, root, pmap);
|
||||
XChangeProperty(wdpy, root, a_xroot, XA_PIXMAP, 32, PropModeReplace, (unsigned char *)&pval, 1);
|
||||
XChangeProperty(wdpy, root, a_eset, XA_PIXMAP, 32, PropModeReplace, (unsigned char *)&pval, 1);
|
||||
XClearWindow(wdpy, root);
|
||||
XUngrabServer(wdpy);
|
||||
XSync(wdpy, False);
|
||||
|
||||
XSetCloseDownMode(wdpy, RetainPermanent);
|
||||
XCloseDisplay(wdpy);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif /* GEMINUX_WALLPAPER_X11_H */
|
||||
@@ -0,0 +1,303 @@
|
||||
/*
|
||||
* 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 <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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 */
|
||||
Reference in new issue
Block a user