-
Notifications
You must be signed in to change notification settings - Fork 7
/
events.c
591 lines (558 loc) · 14.2 KB
/
events.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/* evilwm - Minimalist Window Manager for X
* Copyright (C) 1999-2015 Ciaran Anscomb <[email protected]>
* see README for license and other details. */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/select.h>
#include <X11/XKBlib.h>
#include "evilwm.h"
#include "log.h"
static int interruptibleXNextEvent(XEvent *event);
#ifdef DEBUG
const char *debug_atom_name(Atom a);
const char *debug_atom_name(Atom a) {
static char buf[48];
char *atom_name = XGetAtomName(dpy, a);
strncpy(buf, atom_name, sizeof(buf));
buf[sizeof(buf)-1] = 0;
return buf;
}
#endif
static void handle_key_event(XKeyEvent *e) {
KeySym key = XkbKeycodeToKeysym(dpy, e->keycode, 0, 0);
Client *c;
int width_inc, height_inc;
ScreenInfo *current_screen = find_current_screen();
switch(key) {
case KEY_NEW:
spawn((const char *const *)opt_term);
break;
case KEY_NEXT:
next();
if (XGrabKeyboard(dpy, e->root, False, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) {
XEvent ev;
do {
XMaskEvent(dpy, KeyPressMask|KeyReleaseMask, &ev);
if (ev.type == KeyPress && XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0) == KEY_NEXT)
next();
} while (ev.type == KeyPress || XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0) == KEY_NEXT);
XUngrabKeyboard(dpy, CurrentTime);
}
ewmh_select_client(current);
break;
case KEY_DOCK_TOGGLE:
set_docks_visible(current_screen, !current_screen->docks_visible);
break;
#ifdef VWM
case XK_1: case XK_2: case XK_3: case XK_4:
case XK_5: case XK_6: case XK_7: case XK_8:
switch_vdesk(current_screen, KEY_TO_VDESK(key));
break;
case KEY_PREVDESK:
if (current_screen->vdesk > 0) {
switch_vdesk(current_screen,
current_screen->vdesk - 1);
}
break;
case KEY_NEXTDESK:
if (current_screen->vdesk < VDESK_MAX) {
switch_vdesk(current_screen,
current_screen->vdesk + 1);
}
break;
case KEY_TOGGLEDESK:
switch_vdesk(current_screen, current_screen->old_vdesk);
break;
#endif
default: break;
}
c = current;
if (c == NULL) return;
width_inc = (c->width_inc > 1) ? c->width_inc : 16;
height_inc = (c->height_inc > 1) ? c->height_inc : 16;
switch (key) {
case KEY_LEFT:
if (e->state & altmask) {
if ((c->width - width_inc) >= c->min_width)
c->width -= width_inc;
} else {
c->x -= 16;
}
goto move_client;
case KEY_DOWN:
if (e->state & altmask) {
if (!c->max_height || (c->height + height_inc) <= c->max_height)
c->height += height_inc;
} else {
c->y += 16;
}
goto move_client;
case KEY_UP:
if (e->state & altmask) {
if ((c->height - height_inc) >= c->min_height)
c->height -= height_inc;
} else {
c->y -= 16;
}
goto move_client;
case KEY_RIGHT:
if (e->state & altmask) {
if (!c->max_width || (c->width + width_inc) <= c->max_width)
c->width += width_inc;
} else {
c->x += 16;
}
goto move_client;
case KEY_TOPLEFT:
c->x = c->border;
c->y = c->border;
goto move_client;
case KEY_TOPRIGHT:
c->x = DisplayWidth(dpy, c->screen->screen)
- c->width-c->border;
c->y = c->border;
goto move_client;
case KEY_BOTTOMLEFT:
c->x = c->border;
c->y = DisplayHeight(dpy, c->screen->screen)
- c->height-c->border;
goto move_client;
case KEY_BOTTOMRIGHT:
c->x = DisplayWidth(dpy, c->screen->screen)
- c->width-c->border;
c->y = DisplayHeight(dpy, c->screen->screen)
- c->height-c->border;
goto move_client;
case KEY_KILL:
send_wm_delete(c, e->state & altmask);
break;
case KEY_LOWER: case KEY_ALTLOWER:
client_lower(c);
break;
case KEY_INFO:
show_info(c, e->keycode);
break;
case KEY_MAX:
maximise_client(c, NET_WM_STATE_TOGGLE, MAXIMISE_HORZ|MAXIMISE_VERT);
break;
case KEY_MAXVERT:
maximise_client(c, NET_WM_STATE_TOGGLE, MAXIMISE_VERT);
break;
#ifdef VWM
case KEY_FIX:
if (is_fixed(c)) {
client_to_vdesk(c, current_screen->vdesk);
} else {
client_to_vdesk(c, VDESK_FIXED);
}
break;
#endif
default: break;
}
return;
move_client:
if (abs(c->x) == c->border && c->oldw != 0)
c->x = 0;
if (abs(c->y) == c->border && c->oldh != 0)
c->y = 0;
moveresize(c);
#ifdef WARP_POINTER
setmouse(c->window, c->width + c->border - 1,
c->height + c->border - 1);
#endif
discard_enter_events(c);
return;
}
static void handle_button_event(XButtonEvent *e) {
Client *c = find_client(e->window);
if (c) {
switch (e->button) {
case Button1:
drag(c); break;
case Button2:
sweep(c); break;
case Button3:
client_lower(c); break;
default: break;
}
}
}
static void do_window_changes(int value_mask, XWindowChanges *wc, Client *c,
int gravity) {
if (gravity == 0)
gravity = c->win_gravity_hint;
c->win_gravity = gravity;
if (value_mask & CWX) c->x = wc->x;
if (value_mask & CWY) c->y = wc->y;
if (value_mask & (CWWidth|CWHeight)) {
int dw = 0, dh = 0;
if (!(value_mask & (CWX|CWY))) {
gravitate_border(c, -c->border);
}
if (value_mask & CWWidth) {
int neww = wc->width;
if (neww < c->min_width)
neww = c->min_width;
if (c->max_width && neww > c->max_width)
neww = c->max_width;
dw = neww - c->width;
c->width = neww;
}
if (value_mask & CWHeight) {
int newh = wc->height;
if (newh < c->min_height)
newh = c->min_height;
if (c->max_height && newh > c->max_height)
newh = c->max_height;
dh = newh - c->height;
c->height = newh;
}
/* only apply position fixes if not being explicitly moved */
if (!(value_mask & (CWX|CWY))) {
switch (gravity) {
default:
case NorthWestGravity:
break;
case NorthGravity:
c->x -= (dw / 2);
break;
case NorthEastGravity:
c->x -= dw;
break;
case WestGravity:
c->y -= (dh / 2);
break;
case CenterGravity:
c->x -= (dw / 2);
c->y -= (dh / 2);
break;
case EastGravity:
c->x -= dw;
c->y -= (dh / 2);
break;
case SouthWestGravity:
c->y -= dh;
break;
case SouthGravity:
c->x -= (dw / 2);
c->y -= dh;
break;
case SouthEastGravity:
c->x -= dw;
c->y -= dh;
break;
}
value_mask |= CWX|CWY;
gravitate_border(c, c->border);
}
}
wc->x = c->x - c->border;
wc->y = c->y - c->border;
wc->border_width = c->border;
XConfigureWindow(dpy, c->parent, value_mask, wc);
XMoveResizeWindow(dpy, c->window, 0, 0, c->width, c->height);
if ((value_mask & (CWX|CWY)) && !(value_mask & (CWWidth|CWHeight))) {
send_config(c);
}
}
static void handle_configure_request(XConfigureRequestEvent *e) {
Client *c = find_client(e->window);
XWindowChanges wc;
wc.x = e->x;
wc.y = e->y;
wc.width = e->width;
wc.height = e->height;
wc.border_width = 0;
wc.sibling = e->above;
wc.stack_mode = e->detail;
if (c) {
if (e->value_mask & CWStackMode && e->value_mask & CWSibling) {
Client *sibling = find_client(e->above);
if (sibling) {
wc.sibling = sibling->parent;
}
}
do_window_changes(e->value_mask, &wc, c, 0);
if (c == current) {
discard_enter_events(c);
}
} else {
LOG_XENTER("XConfigureWindow(window=%lx, value_mask=%lx)", (unsigned int)e->window, e->value_mask);
XConfigureWindow(dpy, e->window, e->value_mask, &wc);
LOG_XLEAVE();
}
}
static void handle_map_request(XMapRequestEvent *e) {
Client *c = find_client(e->window);
LOG_ENTER("handle_map_request(window=%lx)", e->window);
if (c) {
#ifdef VWM
if (!is_fixed(c) && c->vdesk != c->screen->vdesk)
switch_vdesk(c->screen, c->vdesk);
#endif
client_show(c);
client_raise(c);
} else {
XWindowAttributes attr;
XGetWindowAttributes(dpy, e->window, &attr);
make_new_client(e->window, find_screen(attr.root));
}
LOG_LEAVE();
}
static void handle_unmap_event(XUnmapEvent *e) {
Client *c = find_client(e->window);
LOG_ENTER("handle_unmap_event(window=%lx)", e->window);
if (c) {
if (c->ignore_unmap) {
c->ignore_unmap--;
LOG_DEBUG("ignored (%d ignores remaining)\n", c->ignore_unmap);
} else {
LOG_DEBUG("flagging client for removal\n");
c->remove = 1;
need_client_tidy = 1;
}
} else {
LOG_DEBUG("unknown client!\n");
}
LOG_LEAVE();
}
static void handle_colormap_change(XColormapEvent *e) {
Client *c = find_client(e->window);
if (c && e->new) {
c->cmap = e->colormap;
XInstallColormap(dpy, c->cmap);
}
}
static void handle_property_change(XPropertyEvent *e) {
Client *c = find_client(e->window);
if (c) {
LOG_ENTER("handle_property_change(window=%lx, atom=%s)", e->window, debug_atom_name(e->atom));
if (e->atom == XA_WM_NORMAL_HINTS) {
get_wm_normal_hints(c);
LOG_DEBUG("geometry=%dx%d+%d+%d\n", c->width, c->height, c->x, c->y);
} else if (e->atom == xa_net_wm_window_type) {
get_window_type(c);
if (!c->is_dock
#ifdef VWM
&& (is_fixed(c) || (c->vdesk == c->screen->vdesk))
#endif
) {
client_show(c);
}
}
LOG_LEAVE();
}
}
static void handle_enter_event(XCrossingEvent *e) {
Client *c;
if ((c = find_client(e->window))) {
#ifdef VWM
if (!is_fixed(c) && c->vdesk != c->screen->vdesk)
return;
#endif
select_client(c);
ewmh_select_client(c);
}
}
static void handle_mappingnotify_event(XMappingEvent *e) {
XRefreshKeyboardMapping(e);
if (e->request == MappingKeyboard) {
int i;
for (i = 0; i < num_screens; i++) {
grab_keys_for_screen(&screens[i]);
}
}
}
#ifdef SHAPE
static void handle_shape_event(XShapeEvent *e) {
Client *c = find_client(e->window);
if (c)
set_shape(c);
}
#endif
static void handle_client_message(XClientMessageEvent *e) {
#ifdef VWM
ScreenInfo *s = find_current_screen();
#endif
Client *c;
LOG_ENTER("handle_client_message(window=%lx, format=%d, type=%s)", e->window, e->format, debug_atom_name(e->message_type));
#ifdef VWM
if (e->message_type == xa_net_current_desktop) {
switch_vdesk(s, e->data.l[0]);
LOG_LEAVE();
return;
}
#endif
c = find_client(e->window);
if (!c && e->message_type == xa_net_request_frame_extents) {
ewmh_set_net_frame_extents(e->window);
LOG_LEAVE();
return;
}
if (!c) {
LOG_LEAVE();
return;
}
if (e->message_type == xa_net_active_window) {
/* Only do this if it came from direct user action */
if (e->data.l[0] == 2) {
#ifdef VWM
if (c->screen == s)
#endif
select_client(c);
}
LOG_LEAVE();
return;
}
if (e->message_type == xa_net_close_window) {
/* Only do this if it came from direct user action */
if (e->data.l[1] == 2) {
send_wm_delete(c, 0);
}
LOG_LEAVE();
return;
}
if (e->message_type == xa_net_moveresize_window) {
/* Only do this if it came from direct user action */
int source_indication = (e->data.l[0] >> 12) & 3;
if (source_indication == 2) {
int value_mask = (e->data.l[0] >> 8) & 0x0f;
int gravity = e->data.l[0] & 0xff;
XWindowChanges wc;
wc.x = e->data.l[1];
wc.y = e->data.l[2];
wc.width = e->data.l[3];
wc.height = e->data.l[4];
do_window_changes(value_mask, &wc, c, gravity);
}
LOG_LEAVE();
return;
}
if (e->message_type == xa_net_restack_window) {
/* Only do this if it came from direct user action */
if (e->data.l[0] == 2) {
XWindowChanges wc;
wc.sibling = e->data.l[1];
wc.stack_mode = e->data.l[2];
do_window_changes(CWSibling | CWStackMode, &wc, c, c->win_gravity);
}
LOG_LEAVE();
return;
}
#ifdef VWM
if (e->message_type == xa_net_wm_desktop) {
/* Only do this if it came from direct user action */
if (e->data.l[1] == 2) {
client_to_vdesk(c, e->data.l[0]);
}
LOG_LEAVE();
return;
}
#endif
if (e->message_type == xa_net_wm_state) {
int i, maximise_hv = 0;
/* Message can contain up to two state changes: */
for (i = 1; i <= 2; i++) {
if ((Atom)e->data.l[i] == xa_net_wm_state_maximized_vert) {
maximise_hv |= MAXIMISE_VERT;
} else if ((Atom)e->data.l[i] == xa_net_wm_state_maximized_horz) {
maximise_hv |= MAXIMISE_HORZ;
} else if ((Atom)e->data.l[i] == xa_net_wm_state_fullscreen) {
maximise_hv |= MAXIMISE_VERT|MAXIMISE_HORZ;
}
}
if (maximise_hv) {
maximise_client(c, e->data.l[0], maximise_hv);
}
LOG_LEAVE();
return;
}
LOG_LEAVE();
}
void event_main_loop(void) {
union {
XEvent xevent;
#ifdef SHAPE
XShapeEvent xshape;
#endif
} ev;
/* main event loop here */
while (!wm_exit) {
if (interruptibleXNextEvent(&ev.xevent)) {
switch (ev.xevent.type) {
case KeyPress:
handle_key_event(&ev.xevent.xkey); break;
case ButtonPress:
handle_button_event(&ev.xevent.xbutton); break;
case ConfigureRequest:
handle_configure_request(&ev.xevent.xconfigurerequest); break;
case MapRequest:
handle_map_request(&ev.xevent.xmaprequest); break;
case ColormapNotify:
handle_colormap_change(&ev.xevent.xcolormap); break;
case EnterNotify:
handle_enter_event(&ev.xevent.xcrossing); break;
case PropertyNotify:
handle_property_change(&ev.xevent.xproperty); break;
case UnmapNotify:
handle_unmap_event(&ev.xevent.xunmap); break;
case MappingNotify:
handle_mappingnotify_event(&ev.xevent.xmapping); break;
case ClientMessage:
handle_client_message(&ev.xevent.xclient); break;
default:
#ifdef SHAPE
if (have_shape && ev.xevent.type == shape_event) {
handle_shape_event(&ev.xshape);
}
#endif
#ifdef RANDR
if (have_randr && ev.xevent.type == randr_event_base + RRScreenChangeNotify) {
XRRUpdateConfiguration(&ev.xevent);
}
#endif
break;
}
}
if (need_client_tidy) {
struct list *iter, *niter;
need_client_tidy = 0;
for (iter = clients_tab_order; iter; iter = niter) {
Client *c = iter->data;
niter = iter->next;
if (c->remove)
remove_client(c);
}
}
}
}
/* interruptibleXNextEvent() is taken from the Blender source and comes with
* the following copyright notice: */
/* Copyright (c) Mark J. Kilgard, 1994, 1995, 1996. */
/* This program is freely distributable without licensing fees
* and is provided without guarantee or warrantee expressed or
* implied. This program is -not- in the public domain. */
/* Unlike XNextEvent, if a signal arrives, interruptibleXNextEvent will
* return zero. */
static int interruptibleXNextEvent(XEvent *event) {
fd_set fds;
int rc;
int dpy_fd = ConnectionNumber(dpy);
for (;;) {
if (XPending(dpy)) {
XNextEvent(dpy, event);
return 1;
}
FD_ZERO(&fds);
FD_SET(dpy_fd, &fds);
rc = select(dpy_fd + 1, &fds, NULL, NULL, NULL);
if (rc < 0) {
if (errno == EINTR) {
return 0;
} else {
LOG_ERROR("interruptibleXNextEvent(): select()\n");
}
}
}
}