]> code.delx.au - spectrwm/blobdiff - lib/swm_hack.c
Change libswmhack.so to use RTLD_NEXT functionality when _GNU_SOURCE is defined,...
[spectrwm] / lib / swm_hack.c
index bb895459a17918300f17addddce5dc63afbca894..40fa2246322261cc313b966ac3f95b6f9fd86fb7 100644 (file)
@@ -57,6 +57,8 @@ static Window         root = None;
 static int             xterm = 0;
 static Display         *display = NULL;
 
+void   set_property(Display *, Window, char *, char *);
+
 /* Find our root window */
 static              Window
 MyRoot(Display * dpy)
@@ -93,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);
@@ -130,25 +145,38 @@ 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(display))
-               parent = MyRoot(display);
+       if (parent == DefaultRootWindow(dpy))
+               parent = MyRoot(dpy);
        
-       id = (*func) (display, parent, x, y, width, height, border_width,
+       id = (*func) (dpy, parent, x, y, width, height, border_width,
            depth, clss, visual, valuemask, attributes);
 
        if (id) {
                if ((env = getenv("_SWM_WS")) != NULL) 
-                       set_property(display, id, "_SWM_WS", env);
+                       set_property(dpy, id, "_SWM_WS", env);
                if ((env = getenv("_SWM_PID")) != NULL)
-                       set_property(display, id, "_SWM_PID", env);
+                       set_property(dpy, id, "_SWM_PID", env);
                if ((env = getenv("_SWM_XTERM_FONTADJ")) != NULL) {
                        unsetenv("_SWM_XTERM_FONTADJ");
                        xterm = 1;
@@ -175,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);
@@ -209,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);
@@ -236,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);