Files
geminux/src/geminux-apps/geminux-wallpaper-x11.h
T

758 lines
24 KiB
C
Raw Normal View History

/*
* 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 */