1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/* original: https://www.mail-archive.com/devel@xfree86.org/msg05806.html */
#include <X11/Xutil.h>
#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xinerama.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAX(a,b) ((a > b) ? a : b)
#define MIN(a,b) ((a < b) ? a : b)
extern char **environ;
static const char prefix[] = "WINPL_";
static const int prefixlen = sizeof(prefix) - 1;
static void *lib_xlib = NULL;
static void (*setwmname)(Display *, Window, XTextProperty *) = NULL;
static int (*changeprop)(Display *, Window, Atom, Atom, int, int, const unsigned char *, int) = NULL;
static bool hook_name = false;
static void
warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "winpl: warn: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
static void
err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "winpl: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
static char **
iter_args(char **env, const char **key, size_t *keylen, const char **val)
{
const char *tok;
for (; *env; env++) { \
if (strncmp(*env, prefix, prefixlen))
continue;
*key = *env + prefixlen;
tok = strchr(*env, '=');
if (!tok) continue;
*keylen = tok - *env - prefixlen;
*val = tok + 1;
return env + 1;
}
return NULL;
}
static int
intersection_area(XineramaScreenInfo *info, XWindowAttributes *wa)
{
int dx, dy;
dx = MIN(wa->x + wa->width, info->x_org + info->width);
dx -= MAX(wa->x, info->x_org);
dy = MIN(wa->y + wa->height, info->y_org + info->height);
dy -= MAX(wa->y, info->y_org);
return MAX(0, dx) * MAX(0, dy);
}
static int
monitor_from_pointer(XineramaScreenInfo *info, int mcount,
Display *display, Window window)
{
Window dummy;
int x = 0, y = 0, di, i, screen;
unsigned int dui;
screen = -1;
XQueryPointer(display, window, &dummy, &dummy,
&x, &y, &di, &di, &dui);
for (i = 0; i < mcount; i++) {
if (x >= info[i].x_org && y >= info[i].y_org
&& x < info[i].x_org + info[i].width
&& y < info[i].y_org + info[i].height) {
screen = i;
break;
}
}
return screen;
}
static int
monitor_from_focussed(Display *display, XineramaScreenInfo *info, int mcount)
{
Window w, root, *dws, dw, pw;
XWindowAttributes wa;
int di, area, maxarea, i;
unsigned du;
int screen;
screen = -1;
root = XDefaultRootWindow(display);
/* check if a focussed window exists.. */
XGetInputFocus(display, &w, &di);
/* modified snippet from dmenu-4.9 source */
if (w != root && w != PointerRoot && w != None) {
do {
if (XQueryTree(display, (pw = w), &dw, &w, &dws, &du) && dws)
XFree(dws);
} while (w != root && w != pw);
/* find xinerama screen with largest screen intersection */
if (XGetWindowAttributes(display, pw, &wa)) {
for (i = 0; i < mcount; i++) {
area = intersection_area(&info[i], &wa);
if (area > maxarea) {
maxarea = area;
screen = i;
}
}
}
}
return screen;
}
static void
set_prop(Display *display, Window window, const char *name,
int type, size_t size, void *val, int count)
{
int atom;
atom = XInternAtom(display, name, False);
changeprop(display, window, atom, type, size,
PropModeReplace, val, count);
}
static void
set_props(Display *display, Window window)
{
int wx, wy, mx, my;
unsigned int ww, wh, mw, mh;
const char *key, *val;
unsigned int border, depth;
XWMHints hints;
size_t keylen;
char **env;
Window root;
Atom atom;
pid_t pid, ppid;
uid_t uid;
uid = getuid();
pid = getpid();
ppid = getppid();
if (!XGetGeometry(display, window, &root,
&wx, &wy, &ww, &wh, &border, &depth))
err("Failed to get window geometry");
if (!XGetGeometry(display, root, &root,
&mx, &my, &mw, &mh, &border, &depth))
err("Failed to get screen geometry");
if (XineramaIsActive(display)) {
XineramaScreenInfo *info;
int mcount, mon;
if (!(info = XineramaQueryScreens(display, &mcount)))
err("Failed to query xinerama");
mon = -1;
env = environ;
while ((env = iter_args(env, &key, &keylen, &val))) {
if (!strncmp("MON_NUM", key, keylen)) {
mon = strtoul(val, NULL, 0);
if (mon < 0 || mon >= mcount)
err("Monitor number OOB");
} else if (!strncmp("MON_PTR", key, keylen)) {
mon = monitor_from_pointer(info,
mcount, display, window);
} else if (!strncmp("MON_FOCUS", key, keylen)) {
mon = monitor_from_focussed(display,
info, mcount);
} else if (keylen >= 4 && !strncmp(key, "MON_", 4)) {
err("Unsupported env var: WINPL_%.*s",
(int) keylen, key);
}
}
if (mon == -1)
mon = monitor_from_focussed(display, info, mcount);
if (mon == -1)
mon = monitor_from_pointer(info, mcount,
display, window);
if (mon == -1)
err("Failed to get monitor");
mx = info[mon].x_org;
my = info[mon].y_org;
mw = info[mon].width;
mh = info[mon].height;
} else {
env = environ;
while ((env = iter_args(env, &key, &keylen, &val))) {
if (keylen >= 4 && !strncmp(key, "MON_", 4)) {
warn("Xinerama is not active");
break;
}
}
}
env = environ;
while ((env = iter_args(env, &key, &keylen, &val))) {
if (!strncmp(key, "WX", keylen)) {
/* absolute window x */
wx = strtoul(val, NULL, 0);
} else if (!strncmp(key, "WY", keylen)) {
/* absolute window y */
wy = strtoul(val, NULL, 0);
} else if (!strncmp(key, "RWX", keylen)) {
/* window x relative to monitor size */
wx = mw * strtof(val, NULL);
} else if (!strncmp(key, "RWY", keylen)) {
/* window y relative to monitor size */
wy = mh * strtof(val, NULL);
} else if (!strncmp(key, "MWX", keylen)) {
/* window x from monitor top left */
wx = mx + strtoul(val, NULL, 0);
} else if (!strncmp(key, "MWY", keylen)) {
/* window y from monitor top left */
wy = my + strtoul(val, NULL, 0);
} else if (!strncmp(key, "WW", keylen)) {
/* window width */
ww = strtoul(val, NULL, 0);
} else if (!strncmp(key, "WH", keylen)) {
/* window height */
wh = strtoul(val, NULL, 0);
} else if (!strncmp(key, "RWW", keylen)) {
/* window width relative to monitor size */
ww = mw * strtof(val, NULL);
} else if (!strncmp(key, "RWH", keylen)) {
/* window height relative to monitor size */
wh = mh * strtof(val, NULL);
} else if (!strncmp(key, "CENTER", keylen)) {
/* window centered in monitor */
wx = mx + (mw - ww) / 2.f;
wy = my + (mh - wh) / 2.f;
} else if (!strncmp(key, "FLOAT", keylen)) {
/* window 'floating' in tiled WM */
atom = XInternAtom(display,
"_NET_WM_WINDOW_TYPE_DIALOG", False);
set_prop(display, window, "_NET_WM_WINDOW_TYPE",
XA_ATOM, 32, &atom, 1);
} else if (!strncmp(key, "NAME", keylen)) {
/* window name / title */
hook_name = true;
XTextProperty name;
XStringListToTextProperty((char**)&val, 1, &name);
setwmname(display, window, &name);
set_prop(display, window, "_NET_WM_NAME",
XInternAtom(display, "UTF8_STRING", False),
8, (void*)val, strlen(val));
set_prop(display, window, "WM_NAME",
XA_STRING, 8, (void*)val, strlen(val));
} else if (!strncmp(key, "NO_INPUT", keylen)) {
/* never take input (focus) */
hints.flags = InputHint;
hints.input = false;
XSetWMHints(display, window, &hints);
} else if (keylen < 4 || strncmp(key, "MON_", 4)) {
err("Unsupported env var: WINPL_%.*s",
(int) keylen, key);
}
}
/* set basic properties */
set_prop(display, window, "_NET_WM_UID", XA_CARDINAL, 32, &uid, 1);
set_prop(display, window, "_NET_WM_PID", XA_CARDINAL, 32, &pid, 1);
set_prop(display, window, "_NET_WM_PPID", XA_CARDINAL, 32, &ppid, 1);
/* update window pos and geometry */
XMoveResizeWindow(display, window, wx, wy, ww, wh);
}
Window
XCreateWindow(Display *display, Window parent, int x, int y,
unsigned int width, unsigned int height, unsigned int border_width,
int depth, unsigned int class, Visual *visual, unsigned long valuemask,
XSetWindowAttributes *attributes)
{
static Window (*func)(Display *display, Window parent, int x, int y,
unsigned int width, unsigned int height,
unsigned int border_width, int depth, unsigned int class,
Visual *visual, unsigned long valuemask,
XSetWindowAttributes *attributes) = NULL;
Window window;
int i;
if (!func) func = dlsym(lib_xlib, "XCreateWindow");
for (i = 0; i < ScreenCount(display); i++) {
/* for toplevel windows */
if (parent == RootWindow(display, i)) {
window = (*func) (display, parent, x, y, width, height,
border_width, depth, class, visual, valuemask, attributes);
set_props(display, window);
return window;
}
}
/* create window as usual */
return (*func) (display, parent, x, y, width, height, border_width, depth,
class, visual, valuemask, attributes);
}
Window
XCreateSimpleWindow(Display *display, Window parent, int x, int y,
unsigned int width, unsigned int height, unsigned int border_width,
unsigned long border, unsigned long background)
{
static Window (*func)(Display *display, Window parent, int x, int y,
unsigned int width, unsigned int height,
unsigned int border_width, unsigned long border,
unsigned long background) = NULL;
Window window;
int i;
if (!func) func = dlsym(lib_xlib, "XCreateSimpleWindow");
for (i = 0; i < ScreenCount(display); i++) {
/* for toplevel windows */
if (parent == RootWindow(display, i)) {
window = (*func) (display, parent, x, y, width, height,
border_width, border, background);
set_props(display, window);
return window;
}
}
/* create window as usual */
return (*func) (display, parent, x, y, width, height,
border_width, border, background);
}
int
XReparentWindow(Display *display, Window window, Window parent, int x, int y)
{
static int (*func)(Display *display, Window window, Window parent,
int x, int y) = NULL;
int i;
if (!func) func = dlsym(lib_xlib, "XReparentWindow");
for (i = 0; i < ScreenCount(display); i++) {
/* for toplevel windows */
if (parent == RootWindow(display, i)) {
set_props(display, window);
return (*func)(display, window, parent, x, y);
}
}
/* reparent as usual */
return (*func) (display, window, parent, x, y);
}
void
XSetWMName(Display *display, Window window, XTextProperty *text)
{
if (!hook_name) setwmname(display, window, text);
}
int
XChangeProperty(Display *display, Window window, Atom property, Atom type, int format, int mode, const unsigned char *data, int n)
{
static Atom name_atom = 0;
static Atom net_name_atom = 0;
if (!name_atom) name_atom = XInternAtom(display, "WM_NAME", False);
if (!net_name_atom) net_name_atom = XInternAtom(display, "_NET_WM_NAME", False);
if (hook_name && property == name_atom) return 0;
if (hook_name && property == net_name_atom) return 0;
return changeprop(display, window, property, type, format, mode, data, n);
}
void
winpl_init(void)
{
if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
if (!setwmname) setwmname = dlsym(lib_xlib, "XSetWMName");
if (!changeprop) changeprop = dlsym(lib_xlib, "XChangeProperty");
}
|