-
Notifications
You must be signed in to change notification settings - Fork 4
/
qpwm.c
378 lines (295 loc) · 8.37 KB
/
qpwm.c
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
// qpwm - quite powerful window manager.
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#define win (client *t = 0, *c = list; c && t != list->prev; t = c, c = c->next)
#define WS_SAVE(W) ws_list[W] = list
#define WS_SEL(W) list = ws_list[ws = W]
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define WIN_SIZE(W, gx, gy, gw, gh) \
XGetGeometry(d, W, &(Window){0}, gx, gy, gw, gh, &(unsigned int){0}, \
&(unsigned int){0})
#define MOD_CLEAN(mask) \
(mask & ~(numlock | LockMask) & \
(ShiftMask | ControlMask | Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | \
Mod5Mask))
typedef struct {
const char **com;
const int i;
const Window w;
} Arg;
struct key {
unsigned int mod;
KeySym keysym;
void (*function)(const Arg arg);
const Arg arg;
};
typedef struct client {
struct client *next, *prev;
int f, wx, wy;
unsigned int ww, wh;
Window w;
} client;
void button_press(XEvent *e);
void button_release(XEvent *e);
void configure_request(XEvent *e);
void input_grab(Window root);
void key_press(XEvent *e);
void map_request(XEvent *e);
void mapping_notify(XEvent *e);
void notify_destroy(XEvent *e);
void notify_enter(XEvent *e);
void notify_motion(XEvent *e);
void run(const Arg arg);
void win_add(Window w);
void win_center(const Arg arg);
void win_del(Window w);
void win_fs(const Arg arg);
void win_focus(client *c);
void win_kill(const Arg arg);
void win_prev(const Arg arg);
void win_next(const Arg arg);
void win_to_ws(const Arg arg);
void ws_go(const Arg arg);
void quit(const Arg arg);
static int xerror() { return 0; }
static client *list = {0}, *ws_list[10] = {0}, *cur;
static int ws = 1, sw, sh, wx, wy, numlock = 0;
static unsigned int ww, wh;
static Display *d;
static XButtonEvent mouse;
static Window root;
static int running = 1;
static void (*events[LASTEvent])(XEvent *e) = {
[ButtonPress] = button_press,
[ButtonRelease] = button_release,
[ConfigureRequest] = configure_request,
[KeyPress] = key_press,
[MapRequest] = map_request,
[MappingNotify] = mapping_notify,
[DestroyNotify] = notify_destroy,
[EnterNotify] = notify_enter,
[MotionNotify] = notify_motion};
#include "config.h"
void win_focus(client *c) {
cur = c;
XSetInputFocus(d, cur->w, RevertToParent, CurrentTime);
}
void notify_destroy(XEvent *e) {
win_del(e->xdestroywindow.window);
if (list)
win_focus(list->prev);
}
void notify_enter(XEvent *e) {
while (XCheckTypedEvent(d, EnterNotify, e));
for (client *t = 0, *c = list; c && t != list->prev; t = c, c = c->next) {
if (c->w == e->xcrossing.window)
win_focus(c);
}
}
void notify_motion(XEvent *e) {
if (!mouse.subwindow || cur->f)
return;
while (XCheckTypedEvent(d, MotionNotify, e))
;
int xd = e->xbutton.x_root - mouse.x_root;
int yd = e->xbutton.y_root - mouse.y_root;
XMoveResizeWindow(d, mouse.subwindow, wx + (mouse.button == 1 ? xd : 0),
wy + (mouse.button == 1 ? yd : 0),
MAX(1, ww + (mouse.button == 3 ? xd : 0)),
MAX(1, wh + (mouse.button == 3 ? yd : 0)));
}
void key_press(XEvent *e) {
KeySym keysym = XkbKeycodeToKeysym(d, e->xkey.keycode, 0, 0);
for (unsigned int i = 0; i < sizeof(keys) / sizeof(*keys); ++i)
if (keys[i].keysym == keysym &&
MOD_CLEAN(keys[i].mod) == MOD_CLEAN(e->xkey.state))
keys[i].function(keys[i].arg);
}
void button_press(XEvent *e) {
if (!e->xbutton.subwindow)
return;
WIN_SIZE(e->xbutton.subwindow, &wx, &wy, &ww, &wh);
XRaiseWindow(d, e->xbutton.subwindow);
mouse = e->xbutton;
}
void button_release(XEvent *e) { mouse.subwindow = 0; }
void win_add(Window w) {
client *c;
if (!(c = (client *)calloc(1, sizeof(client))))
exit(1);
c->w = w;
if (list) {
list->prev->next = c;
c->prev = list->prev;
list->prev = c;
c->next = list;
} else {
list = c;
list->prev = list->next = list;
}
WS_SAVE(ws);
}
void win_del(Window w) {
client *x = 0;
for
win if (c->w == w) x = c;
if (!list || !x)
return;
if (x->prev == x)
list = 0;
if (list == x)
list = x->next;
if (x->next)
x->next->prev = x->prev;
if (x->prev)
x->prev->next = x->next;
free(x);
WS_SAVE(ws);
}
void win_kill(const Arg arg) {
if (cur)
XKillClient(d, cur->w);
}
void win_center(const Arg arg) {
if (!cur)
return;
WIN_SIZE(cur->w, &(int){0}, &(int){0}, &ww, &wh);
XMoveWindow(d, cur->w, (sw - ww) / 2, (sh - wh) / 2);
}
void win_fs(const Arg arg) {
if (!cur)
return;
if ((cur->f = cur->f ? 0 : 1)) {
WIN_SIZE(cur->w, &cur->wx, &cur->wy, &cur->ww, &cur->wh);
XMoveResizeWindow(d, cur->w, 0, 0, sw, sh);
} else {
XMoveResizeWindow(d, cur->w, cur->wx, cur->wy, cur->ww, cur->wh);
}
}
void win_to_ws(const Arg arg) {
int tmp = ws;
if (arg.i == tmp)
return;
WS_SEL(arg.i);
win_add(cur->w);
WS_SAVE(arg.i);
WS_SEL(tmp);
win_del(cur->w);
XUnmapWindow(d, cur->w);
WS_SAVE(tmp);
if (list)
win_focus(list);
}
void win_prev(const Arg arg) {
if (!cur)
return;
XRaiseWindow(d, cur->prev->w);
win_focus(cur->prev);
}
void win_next(const Arg arg) {
if (!cur)
return;
XRaiseWindow(d, cur->next->w);
win_focus(cur->next);
}
void ws_go(const Arg arg) {
int tmp = ws;
if (arg.i == ws)
return;
WS_SAVE(ws);
WS_SEL(arg.i);
for
win XMapWindow(d, c->w);
WS_SEL(tmp);
for
win XUnmapWindow(d, c->w);
WS_SEL(arg.i);
if (list)
win_focus(list);
else
cur = 0;
}
void configure_request(XEvent *e) {
XConfigureRequestEvent *ev = &e->xconfigurerequest;
XConfigureWindow(d, ev->window, ev->value_mask,
&(XWindowChanges){.x = ev->x,
.y = ev->y,
.width = ev->width,
.height = ev->height,
.sibling = ev->above,
.stack_mode = ev->detail});
}
void map_request(XEvent *e) {
Window w = e->xmaprequest.window;
XSelectInput(d, w, StructureNotifyMask | EnterWindowMask);
WIN_SIZE(w, &wx, &wy, &ww, &wh);
win_add(w);
cur = list->prev;
if (wx + wy == 0)
win_center((Arg){0});
XMapWindow(d, w);
win_focus(list->prev);
}
void mapping_notify(XEvent *e) {
XMappingEvent *ev = &e->xmapping;
if (ev->request == MappingKeyboard || ev->request == MappingModifier) {
XRefreshKeyboardMapping(ev);
input_grab(root);
}
}
void run(const Arg arg) {
if (fork())
return;
if (d)
close(ConnectionNumber(d));
setsid();
execvp((char *)arg.com[0], (char **)arg.com);
}
void input_grab(Window root) {
unsigned int i, j, modifiers[] = {0, LockMask, numlock, numlock | LockMask};
XModifierKeymap *modmap = XGetModifierMapping(d);
KeyCode code;
for (i = 0; i < 8; i++)
for (int k = 0; k < modmap->max_keypermod; k++)
if (modmap->modifiermap[i * modmap->max_keypermod + k] ==
XKeysymToKeycode(d, 0xff7f))
numlock = (1 << i);
XUngrabKey(d, AnyKey, AnyModifier, root);
for (i = 0; i < sizeof(keys) / sizeof(*keys); i++)
if ((code = XKeysymToKeycode(d, keys[i].keysym)))
for (j = 0; j < sizeof(modifiers) / sizeof(*modifiers); j++)
XGrabKey(d, code, keys[i].mod | modifiers[j], root, True, GrabModeAsync,
GrabModeAsync);
for (i = 1; i < 4; i += 2)
for (j = 0; j < sizeof(modifiers) / sizeof(*modifiers); j++)
XGrabButton(d, i, MOD | modifiers[j], root, True,
ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
GrabModeAsync, GrabModeAsync, 0, 0);
XFreeModifiermap(modmap);
}
void quit(const Arg arg) {
if (!arg.i) {
running = 0;
}
}
int main(void) {
XEvent ev;
if (!(d = XOpenDisplay(0)))
exit(1);
signal(SIGCHLD, SIG_IGN);
XSetErrorHandler(xerror);
int s = DefaultScreen(d);
root = RootWindow(d, s);
sw = XDisplayWidth(d, s);
sh = XDisplayHeight(d, s);
XSelectInput(d, root, SubstructureRedirectMask);
XDefineCursor(d, root, XCreateFontCursor(d, 68));
input_grab(root);
while (running && !XNextEvent(d, &ev)) // 1 && will forever be here.
if (events[ev.type])
events[ev.type](&ev);
}