chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / lib / e16_hack.c
1 /*
2  * Copyright (C) 2005-2007 Carsten Haitzler
3  * Copyright (C) 2006-2007 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 /*
25  * Basic hack mechanism (dlopen etc.) taken from e_hack.c in e17.
26  */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <dlfcn.h>
30 #include <X11/Xlib.h>
31 #include <X11/X.h>
32
33 #include "config.h"
34 #include "util.h"
35
36 /* dlopened xlib so we can find the symbols in the real xlib to call them */
37 static void        *lib_xlib = NULL;
38
39 static Window       root = None;
40
41 /* Find our root window */
42 static              Window
43 MyRoot(Display * dpy)
44 {
45    char               *s;
46
47    if (root != None)
48       return root;
49
50    root = DefaultRootWindow(dpy);
51
52    s = getenv("ENL_WM_ROOT");
53    if (!s)
54       return root;
55
56    sscanf(s, "%lx", &root);
57    return root;
58 }
59
60 typedef             Window(CWF) (Display * _display, Window _parent, int _x,
61                                  int _y, unsigned int _width,
62                                  unsigned int _height,
63                                  unsigned int _border_width, int _depth,
64                                  unsigned int _class, Visual * _visual,
65                                  unsigned long _valuemask,
66                                  XSetWindowAttributes * _attributes);
67
68 /* XCreateWindow intercept hack */
69 __EXPORT__          Window
70 XCreateWindow(Display * display, Window parent, int x, int y,
71               unsigned int width, unsigned int height,
72               unsigned int border_width,
73               int depth, unsigned int clss, Visual * visual,
74               unsigned long valuemask, XSetWindowAttributes * attributes)
75 {
76    static CWF         *func = NULL;
77
78    /* find the real Xlib and the real X function */
79    if (!lib_xlib)
80       lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
81    if (!func)
82       func = (CWF *) dlsym(lib_xlib, "XCreateWindow");
83
84    if (parent == DefaultRootWindow(display))
85       parent = MyRoot(display);
86
87    return (*func) (display, parent, x, y, width, height, border_width, depth,
88                    clss, visual, valuemask, attributes);
89 }
90
91 typedef             Window(CSWF) (Display * _display, Window _parent, int _x,
92                                   int _y, unsigned int _width,
93                                   unsigned int _height,
94                                   unsigned int _border_width,
95                                   unsigned long _border,
96                                   unsigned long _background);
97
98 /* XCreateSimpleWindow intercept hack */
99 __EXPORT__          Window
100 XCreateSimpleWindow(Display * display, Window parent, int x, int y,
101                     unsigned int width, unsigned int height,
102                     unsigned int border_width,
103                     unsigned long border, unsigned long background)
104 {
105    static CSWF        *func = NULL;
106
107    /* find the real Xlib and the real X function */
108    if (!lib_xlib)
109       lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
110    if (!func)
111       func = (CSWF *) dlsym(lib_xlib, "XCreateSimpleWindow");
112
113    if (parent == DefaultRootWindow(display))
114       parent = MyRoot(display);
115
116    return (*func) (display, parent, x, y, width, height,
117                    border_width, border, background);
118 }
119
120 typedef int         (RWF) (Display * _display, Window _window, Window _parent,
121                            int x, int y);
122
123 /* XReparentWindow intercept hack */
124 __EXPORT__ int
125 XReparentWindow(Display * display, Window window, Window parent, int x, int y)
126 {
127    static RWF         *func = NULL;
128
129    /* find the real Xlib and the real X function */
130    if (!lib_xlib)
131       lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
132    if (!func)
133       func = (RWF *) dlsym(lib_xlib, "XReparentWindow");
134
135    if (parent == DefaultRootWindow(display))
136       parent = MyRoot(display);
137
138    return (*func) (display, window, parent, x, y);
139 }