3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Copyright (C) 2005-2007 Carsten Haitzler
20 * Copyright (C) 2006-2007 Kim Woelders
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
29 * The above copyright notice and this permission notice shall be included in
30 * all copies of the Software, its documentation and marketing & publicity
31 * materials, and acknowledgment shall be given in the documentation, materials
32 * and software packages that this Software was used.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
37 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
38 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 * Basic hack mechanism (dlopen etc.) taken from e_hack.c in e17.
50 #include <X11/Xatom.h>
51 #include <X11/Intrinsic.h>
53 /* dlopened libs so we can find the symbols in the real one to call them */
54 static void *lib_xlib = NULL;
55 static void *lib_xtlib = NULL;
57 static Window root = None;
59 static Display *dpy = NULL;
61 /* Find our root window */
70 root = DefaultRootWindow(dpy);
72 s = getenv("ENL_WM_ROOT");
76 sscanf(s, "%lx", &root);
81 typedef Atom (XIA) (Display *display, char *atom_name, Bool
84 typedef int (XCP) (Display *display, Window w, Atom property,
85 Atom type, int format, int mode, unsigned char *data,
88 #define SWM_PROPLEN (16)
90 set_property(Display *dpy, Window id, char *name, char *val)
93 char prop[SWM_PROPLEN];
94 static XIA *xia = NULL;
95 static XCP *xcp = NULL;
97 /* find the real Xlib and the real X function */
99 lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
101 xia = (XIA *) dlsym(lib_xlib, "XInternAtom");
103 xcp = (XCP *) dlsym(lib_xlib, "XChangeProperty");
105 /* Try to update the window's workspace property */
106 atom = (*xia)(dpy, name, False);
108 if (snprintf(prop, SWM_PROPLEN, "%s", val) < SWM_PROPLEN)
109 (*xcp)(dpy, id, atom, XA_STRING,
110 8, PropModeReplace, (unsigned char *)prop, SWM_PROPLEN);
113 typedef Window(CWF) (Display * _display, Window _parent, int _x,
114 int _y, unsigned int _width,
115 unsigned int _height,
116 unsigned int _border_width, int _depth,
117 unsigned int _class, Visual * _visual,
118 unsigned long _valuemask,
119 XSetWindowAttributes * _attributes);
121 /* XCreateWindow intercept hack */
123 XCreateWindow(Display * display, Window parent, int x, int y,
124 unsigned int width, unsigned int height,
125 unsigned int border_width,
126 int depth, unsigned int clss, Visual * visual,
127 unsigned long valuemask, XSetWindowAttributes * attributes)
129 static CWF *func = NULL;
133 /* find the real Xlib and the real X function */
135 lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
137 func = (CWF *) dlsym(lib_xlib, "XCreateWindow");
141 if (parent == DefaultRootWindow(display))
142 parent = MyRoot(display);
144 id = (*func) (display, parent, x, y, width, height, border_width,
145 depth, clss, visual, valuemask, attributes);
148 if ((env = getenv("_SWM_WS")) != NULL)
149 set_property(display, id, "_SWM_WS", env);
150 if ((env = getenv("_SWM_PID")) != NULL)
151 set_property(display, id, "_SWM_PID", env);
152 if ((env = getenv("_SWM_XTERM_FONTADJ")) != NULL) {
153 unsetenv("_SWM_XTERM_FONTADJ");
160 typedef Window(CSWF) (Display * _display, Window _parent, int _x,
161 int _y, unsigned int _width,
162 unsigned int _height,
163 unsigned int _border_width,
164 unsigned long _border,
165 unsigned long _background);
167 /* XCreateSimpleWindow intercept hack */
169 XCreateSimpleWindow(Display * display, Window parent, int x, int y,
170 unsigned int width, unsigned int height,
171 unsigned int border_width,
172 unsigned long border, unsigned long background)
174 static CSWF *func = NULL;
178 /* find the real Xlib and the real X function */
180 lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
182 func = (CSWF *) dlsym(lib_xlib, "XCreateSimpleWindow");
184 if (parent == DefaultRootWindow(display))
185 parent = MyRoot(display);
187 id = (*func) (display, parent, x, y, width, height,
188 border_width, border, background);
191 if ((env = getenv("_SWM_WS")) != NULL)
192 set_property(display, id, "_SWM_WS", env);
193 if ((env = getenv("_SWM_PID")) != NULL)
194 set_property(display, id, "_SWM_PID", env);
195 if ((env = getenv("_SWM_XTERM_FONTADJ")) != NULL) {
196 unsetenv("_SWM_XTERM_FONTADJ");
203 typedef int (RWF) (Display * _display, Window _window, Window _parent,
206 /* XReparentWindow intercept hack */
208 XReparentWindow(Display * display, Window window, Window parent, int x, int y)
210 static RWF *func = NULL;
212 /* find the real Xlib and the real X function */
214 lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
216 func = (RWF *) dlsym(lib_xlib, "XReparentWindow");
218 if (parent == DefaultRootWindow(display))
219 parent = MyRoot(display);
221 return (*func) (display, window, parent, x, y);
224 typedef void (ANEF) (XtAppContext app_context, XEvent *event_return);
228 * XtAppNextEvent Intercept Hack
229 * Normally xterm rejects "synthetic" (XSendEvent) events to prevent spoofing.
230 * We don't want to disable this completely, it's insecure. But hook here
231 * and allow these mostly harmless ones that we use to adjust fonts.
234 XtAppNextEvent(XtAppContext app_context, XEvent *event_return)
236 static ANEF *func = NULL;
237 static int kp_add = 0, kp_subtract = 0;
239 /* find the real Xlib and the real X function */
241 lib_xtlib = dlopen("libXt.so", RTLD_GLOBAL | RTLD_LAZY);
243 func = (ANEF *) dlsym(lib_xtlib, "XtAppNextEvent");
245 kp_add = XKeysymToKeycode(dpy, XK_KP_Add);
246 kp_subtract = XKeysymToKeycode(dpy, XK_KP_Subtract);
250 (*func) (app_context, event_return);
252 /* Return here if it's not an Xterm. */
256 /* Allow spoofing of font change keystrokes. */
257 if ((event_return->type == KeyPress ||
258 event_return->type == KeyRelease) &&
259 event_return->xkey.state == ShiftMask &&
260 (event_return->xkey.keycode == kp_add ||
261 event_return->xkey.keycode == kp_subtract))
262 event_return->xkey.send_event = 0;