From 1fbe5348a32bb40b9e3cd26ab168233d2c59f274 Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Wed, 6 May 2015 20:36:37 +0200 Subject: [PATCH] Change libswmhack.so to use RTLD_NEXT functionality when _GNU_SOURCE is defined, and add some error handling. --- lib/swm_hack.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/lib/swm_hack.c b/lib/swm_hack.c index c5f5b5b..40fa224 100644 --- a/lib/swm_hack.c +++ b/lib/swm_hack.c @@ -95,13 +95,26 @@ set_property(Display *dpy, Window id, char *name, char *val) static XIA *xia = NULL; static XCP *xcp = NULL; +#ifdef _GNU_SOURCE + /* load the function pointer with RTLD_NEXT + * this might be the real X function or another + * preloaded intercept + */ + if (!lib_xlib) + lib_xlib = RTLD_NEXT; +#else /* find the real Xlib and the real X function */ if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY); - if (!xia) +#endif + if (lib_xlib && !xia) xia = (XIA *) dlsym(lib_xlib, "XInternAtom"); - if (!xcp) + if (lib_xlib && !xcp) xcp = (XCP *) dlsym(lib_xlib, "XChangeProperty"); + if (!xia || !xcp) { + fprintf(stderr, "libswmhack.so: ERROR: %s\n", dlerror()); + return; + } /* Try to update the window's workspace property */ atom = (*xia)(dpy, name, False); @@ -132,13 +145,26 @@ XCreateWindow(Display *dpy, Window parent, int x, int y, char *env; Window id; +#ifdef _GNU_SOURCE + /* load the function pointer with RTLD_NEXT + * this might be the real X function or another + * preloaded intercept + */ + if (!lib_xlib) + lib_xlib = RTLD_NEXT; +#else /* find the real Xlib and the real X function */ if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY); - if (!func) { +#endif + if (lib_xlib && !func) { func = (CWF *) dlsym(lib_xlib, "XCreateWindow"); display = dpy; } + if (!func) { + fprintf(stderr, "libswmhack.so: ERROR: %s\n", dlerror()); + return BadImplementation; + } if (parent == DefaultRootWindow(dpy)) parent = MyRoot(dpy); @@ -177,11 +203,24 @@ XCreateSimpleWindow(Display *dpy, Window parent, int x, int y, char *env; Window id; +#ifdef _GNU_SOURCE + /* load the function pointer with RTLD_NEXT + * this might be the real X function or another + * preloaded intercept + */ + if (!lib_xlib) + lib_xlib = RTLD_NEXT; +#else /* find the real Xlib and the real X function */ if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY); - if (!func) +#endif + if (lib_xlib && !func) func = (CSWF *) dlsym(lib_xlib, "XCreateSimpleWindow"); + if (!func) { + fprintf(stderr, "libswmhack.so: ERROR: %s\n", dlerror()); + return BadImplementation; + } if (parent == DefaultRootWindow(dpy)) parent = MyRoot(dpy); @@ -211,11 +250,24 @@ XReparentWindow(Display *dpy, Window window, Window parent, int x, int y) { static RWF *func = NULL; +#ifdef _GNU_SOURCE + /* load the function pointer with RTLD_NEXT + * this might be the real X function or another + * preloaded intercept + */ + if (!lib_xlib) + lib_xlib = RTLD_NEXT; +#else /* find the real Xlib and the real X function */ if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY); - if (!func) +#endif + if (lib_xlib && !func) func = (RWF *) dlsym(lib_xlib, "XReparentWindow"); + if (!func) { + fprintf(stderr, "libswmhack.so: ERROR: %s\n", dlerror()); + return BadImplementation; + } if (parent == DefaultRootWindow(dpy)) parent = MyRoot(dpy); @@ -238,16 +290,29 @@ XtAppNextEvent(XtAppContext app_context, XEvent *event_return) static ANEF *func = NULL; static KeyCode kp_add = 0, kp_subtract = 0; +#ifdef _GNU_SOURCE + /* load the function pointer with RTLD_NEXT + * this might be the real X function or another + * preloaded intercept + */ + if (!lib_xtlib) + lib_xtlib = RTLD_NEXT; +#else /* find the real Xlib and the real X function */ if (!lib_xtlib) lib_xtlib = dlopen("libXt.so", RTLD_GLOBAL | RTLD_LAZY); - if (!func) { +#endif + if (lib_xtlib && !func) { func = (ANEF *) dlsym(lib_xtlib, "XtAppNextEvent"); if (display != NULL) { kp_add = XKeysymToKeycode(display, XK_KP_Add); kp_subtract = XKeysymToKeycode(display, XK_KP_Subtract); } } + if (!func) { + fprintf(stderr, "libswmhack.so: ERROR: %s\n", dlerror()); + return; + } (*func) (app_context, event_return); -- 2.39.2