-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.c
702 lines (590 loc) · 18.1 KB
/
event.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#include "extern.h"
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
static void observemap (Display *, XContext, Window, struct layout *);
static void interceptconfigure (struct pane *, struct layout *, XContext, Window, XConfigureRequestEvent);
static void observe_unmap(Display *, XContext, Window, struct layout *);
static void handle_button_release (XEvent *, struct layout *);
static void handle_button_press (Display *, XContext, XEvent *, struct layout *);
static void snap_pane (XEvent *, struct layout *);
static void draw(Window w, struct layout *l);
int
handle_event(Display *display, XEvent *event, XContext context,
struct layout *layout)
{
int op, target;
if (event->type != MotionNotify && /* Avoid logging too much */
event->type != Expose && event->type != ConfigureNotify)
TRACE_BEGIN("event: %s (xany.window: %lx)", EVENT_STR(event),
event->xany.window);
if (event->type == KeyRelease) {
if ((op = find_binding(&(event->xkey), &target)) == -1) {
TRACE("key without registered action " \
"- how this can happen?");
return NoAction;
}
TRACE("handling user action");
return handle_action(display, context, op, target, layout);
}
switch (event->type) {
case ButtonPress: {
struct pane *p;
p = find_pane_by_window(event->xbutton.window, layout);
if (p && p->window != event->xbutton.window) {
#if 0
XAllowEvents(layout->display, AsyncPointer, CurrentTime);
#endif
#if 0
XAllowEvents(layout->display, SyncPointer, CurrentTime);
#endif
#if 0
/* TODO: This does nothing, remove? */
XChangeActivePointerGrab(display, ButtonMotionMask | ButtonReleaseMask, None, CurrentTime);
#endif
XGrabServer(display);
layout->outline_y = p->y;
layout->outline_x = p->column->x;
handle_button_press(display, context, event, layout);
} else {
if (!(p->flags & PF_FOCUSED)) {
focus_pane(p, layout);
XAllowEvents(layout->display, SyncPointer, CurrentTime);
} else {
TRACE("Buttonpress here, let's replay pointer");
XAllowEvents(layout->display, ReplayPointer, CurrentTime);
}
}
}
break;
case ButtonRelease: {
struct pane *p;
p = find_pane_by_window(event->xbutton.window, layout);
if (p && event->xbutton.window != p->window)
/*event->xbutton.y < 20) */ {
#if 0
XAllowEvents(layout->display, SyncPointer, CurrentTime);
#endif
if (layout->has_outline) {
draw_outline(layout->active,
layout->outline_x,
layout->outline_y,
layout);
layout->has_outline = false;
}
XUngrabServer(display);
handle_button_release(event, layout);
} else {
#if 0
struct pane *p;
p = find_pane_by_window(event->xbutton.window, layout);
#endif
TRACE("buttonrelease, lets replay pointer");
XAllowEvents(layout->display, ReplayPointer, CurrentTime);
/* XSync(layout->display, False);*/
/* XSendEvent(layout->display, p->window, False, ButtonReleaseMask, event);*/
}
}
break;
case MotionNotify: {
if (1 || event->xbutton.y < 20) {
/*
* TODO: Disallow motion events unless we've
* got an active click to right place
*/
#if 0
assert(layout->active != NULL);
#endif
if (layout->active != NULL) {
layout->dblclick_time = 0;
/* Undraw previous frame */
if (layout->has_outline)
draw_outline(layout->active,
layout->outline_x,
layout->outline_y,
layout);
/* Draw */
layout->outline_x = event->xmotion.x_root -
layout->active_x;
layout->outline_y = event->xmotion.y_root -
layout->active_y;
draw_outline(layout->active,
layout->outline_x,
layout->outline_y, layout);
layout->has_outline = true;
}
}
}
break;
case Expose:
if (event->xexpose.count == 0)
draw(event->xexpose.window, layout);
break;
case DestroyNotify:
TRACE("xdestroywindow.window: %lx",
event->xdestroywindow.window);
observedestroy(display, context, event->xdestroywindow.window, layout); /* was xany.window */
break;
case UnmapNotify:
TRACE("xmap.window: %lx", event->xmap.window);
observe_unmap(display, context, event->xmap.window, layout);
break;
case ConfigureRequest: {
struct pane *p;
p = find_pane_by_window(event->xconfigurerequest.window,
layout);
TRACE("xconfigurerequest.window: %lx",
event->xconfigurerequest.window);
interceptconfigure(p, layout, context,
event->xconfigurerequest.window,
event->xconfigurerequest);
break;
} case ConfigureNotify: {
TRACE("- configurenotify - xconfigure.window: %lx",
event->xconfigure.window);
#if 0
struct pane *p;
/*
* We catch have taken control of Configure so
* we need to remember to relay this event back
* to the original client in case they want to
* do something with it.
*/
p = find_pane_by_window(event->xbutton.window, layout);
if (p != NULL && p->window == event->xconfigure.window) {
XSendEvent(layout->display, p->window, False, StructureNotifyMask, event);
TRACE("sent the configurenotify forward");
}
#endif
break;
}
case ResizeRequest: {
struct pane *p;
TRACE("- resize request");
p = find_pane_by_window(event->xbutton.window, layout);
if (p != NULL && !(p->flags & PF_MINIMIZED)) {
TRACE("from pane %s", PANE_STR(p));
#if 0
p->height = event->xresizerequest.height + 20;
#else
#if 0
resize_adjust(p->column, p, event->xresizerequest.height + layout->titlebar_height_px - p->height);
#endif
#endif
TRACE("...height is set to: %d", p->height);
}
if (p->column != NULL)
resize_relayout(p->column);
#if 0
XSync(layout->display, False);
#endif
break;
}
case CreateNotify: {
TRACE("CreateNotify %dx%d+%d+%d bw=%d override=%d",
event->xcreatewindow.width, event->xcreatewindow.height,
event->xcreatewindow.x, event->xcreatewindow.y,
event->xcreatewindow.border_width,
event->xcreatewindow.override_redirect);
break;
}
case PropertyNotify: {
struct pane *p;
p = find_pane_by_window(event->xproperty.window, layout);
if (p != NULL) {
TRACE("handling property notify the ugly way");
XFetchName(layout->display, p->window, &p->name);
XGetIconName(layout->display, p->window, &p->icon_name);
draw_frame(p, layout);
}
break;
}
case MapRequest: {
struct pane *p;
/*
* MapRequest for <window A> creates <pane B>.
* When <pane B> is created we get CreateNotify.
* In MapNotify we Reparent <window A> to <pane B>.
* When <window A> is reparented, we get ReparentNotify.
* In ReparentNotify we resize.
*/
p = find_pane_by_window(event->xmaprequest.window, layout);
if (p == NULL)
create_pane(event->xmaprequest.window, layout);
else
TRACE("double map, ignore");
break;
} case MapNotify:
TRACE("MapNotify xmap.window: %lx", event->xmap.window);
/* add_window */
observemap(display, context, event->xmap.window,
layout);
break;
case ReparentNotify: {
struct pane *p;
TRACE("ReparentNotify xreparent.window: %lx (parent %lx)",
event->xreparent.window, event->xreparent.parent);
p = find_pane_by_window(event->xreparent.window, layout);
if (p != NULL) {
p->flags |= PF_REPARENTED;
XMapWindow(display, event->xreparent.window);
resize_relayout(p->column);
}
/*
* At this point we can safely focus and get events
* because the window is mapped and reparented.
*/
XSelectInput(display, event->xreparent.window,
SubstructureNotifyMask | ResizeRedirectMask | PropertyChangeMask);
XSelectInput(display, event->xreparent.parent,
ButtonPressMask | ButtonReleaseMask |
ButtonMotionMask | ExposureMask |
SubstructureNotifyMask);
break;
} default:
TRACE_ERR("unhandled event: %s", EVENT_STR(event));
break;
}
return NoAction;
}
static void
handle_button_release(XEvent *event, struct layout *layout)
{
XEvent junk;
if (event->xbutton.y > 20 && layout->active != NULL) {
TRACE("button release event was over titlebar area!");
/*
XSendEvent(layout->display, layout->active->window, True, ButtonReleaseMask, event);
*/
/* return;*/
}
/*
* We may have got motion notify events in the queue,
* let's get rid of those.
*/
XCheckMaskEvent(layout->display, ButtonMotionMask, &junk);
TRACE("buttonrelease - buttonpress time: %ld",
event->xbutton.time - layout->dblclick_time);
if (layout->active != NULL &&
event->xbutton.time - layout->dblclick_time < 500) {
TRACE("minimize on doubleclick");
layout->active->flags ^= PF_MINIMIZED;
layout->dblclick_time = 0;
minimize(layout->active, layout);
return;
} else {
TRACE("not minimize time is: %ld", event->xbutton.time);
layout->dblclick_time = event->xbutton.time;
}
snap_pane(event, layout);
}
static void
handle_button_press(Display *display, XContext context, XEvent *event,
struct layout *layout)
{
struct pane *pane;
TRACE("button press on %lx\n", event->xbutton.window);
layout->active = NULL;
pane = find_pane_by_window(event->xbutton.window, layout);
if (pane == NULL) {
TRACE_ERR("NULL mw in buttonpress");
} else {
if (event->xbutton.window == pane->maximize_button) {
TRACE("button was maximize button");
pane->flags ^= PF_KEEP_OPEN;
draw(event->xbutton.window, layout);
resize_relayout(pane->column);
return;
} else if (event->xbutton.window == pane->close_button) {
TRACE("close button pressed");
close_pane(pane, layout);
return;
}
TRACE("...was pane %ld %s", PANE_NUMBER(pane), pane->name);
TRACE("pane->frame = %lx, pane->window = %lx "
"event->xbutton.window = %lx",
pane->frame, pane->window,
event->xbutton.window);
/*
* TODO: Remove this or actually implement adding motionmask
* handling only whenever required
*/
if (pane->frame == event->xbutton.window) {
TRACE("added motionmask");
}
/*
* Raising window in a tiling window manager sounds stupid
* but ... we're doing it just in case ;)
*/
XRaiseWindow(display, pane->frame);
TRACE("lets focus it");
focus_pane(pane, layout);
/*
* TODO: This is a bit ugly. Actually active window is
* same as focused window i.e. we could make it so.
*/
layout->active = pane;
layout->active_x = event->xbutton.x;
layout->active_y = event->xbutton.y;
if (event->xbutton.y > 20) {
TRACE("event was over titlebar area!");
/*
XSendEvent(layout->display, pane->window, True, ButtonPressMask, event);
*/
}
}
}
static void draw(Window w, struct layout *l)
{
struct pane *p;
assert(l != NULL);
if ((p = find_pane_by_window(w, l)) == NULL)
return;
if (w == p->frame)
draw_frame(p, l);
else if (w == p->close_button)
draw_close_button(p, l);
else if (w == p->maximize_button)
draw_maximize_button(p, l);
}
static void
snap_pane(XEvent *event, struct layout *layout)
{
struct column *column;
struct pane *pane;
int dpx;
if ((pane = layout->active) == NULL)
return;
TRACE("snap %s", PANE_STR(pane));
column = find_column(layout->head, event->xbutton.x_root);
TRACE("...xbutton.x_root=%d", event->xbutton.x_root);
TRACE("...active column x was=%d", layout->active->column->x);
if (column != layout->active->column) {
TRACE("snap pane column changed?");
remove_pane(layout->active, 1);
manage_pane(layout->active, column, NULL);
focus_pane(layout->active, layout);
} else if (layout->outline_y != pane->y) {
dpx = layout->outline_y - pane->y;
if (pane->prev != NULL) {
resize_adjust(pane->column, pane->prev, dpx);
resize_relayout(pane->column);
}
TRACE("Pane moved vertically outline_y=%d pane_y=%d dpx=%d y_adj=%d",
layout->outline_y, pane->y, dpx, pane->y_adj);
}
layout->active = NULL;
}
static void
observemap(Display *display, XContext context, Window window,
struct layout *layout)
{
struct pane *pane;
struct column *column;
XWindowAttributes attrib;
pane = find_pane_by_window(window, layout);
if (pane == NULL) {
TRACE_ERR("NULL pane in observemap");
return;
}
TRACE("observemap for %s", PANE_STR(pane));
pane->flags |= (PF_MAPPED | PF_DIRTY);
if (XGetWindowAttributes(display, window, &attrib) == 0)
assert(0);
column = find_column_by_hpos(attrib.x, layout->head);
if (column != NULL && pane->column == NULL) {
TRACE("add pane from observemap");
/* find_previous_focus(l->head) */
if (!(pane->flags & PF_CAPTURE_EXISTING) &&
layout->column != NULL)
manage_pane(pane, layout->column,
layout->focus);
else
manage_pane(pane, column, column->last);
pane->flags &= ~PF_CAPTURE_EXISTING;
if (pane->flags & PF_REPARENTED) {
TRACE("focus pane from observemap (reparent ok)");
focus_pane(pane, layout);
pane->flags &= ~PF_REPARENTED;
}
} else {
TRACE("Got back old window from withdrawn/iconified?");
if (pane->column != NULL) {
resize_relayout(pane->column);
}
TRACE("...resize layout complete");
if (pane->flags & PF_REPARENTED) {
TRACE("focus pane from observemap 2 (reparent ok)");
focus_pane(pane, layout);
pane->flags &= ~PF_REPARENTED;
}
}
if (window == pane->frame && !(pane->flags & PF_EMPTY)) {
TRACE("Reparent from MapNotify");
XReparentWindow(display, pane->window, pane->frame, 0, 20);
}
draw_frame(pane, layout);
draw_close_button(pane, layout);
draw_maximize_button(pane, layout);
}
static void
observe_unmap(Display *display, XContext context, Window window,
struct layout *l)
{
struct pane *pane;
#if 0
XEvent event;
#endif
if ((pane = find_pane_by_window(window, l)) == NULL) {
TRACE_ERR("destroy: NULL mw in observe_unmap");
return;
}
if (pane->window == window) {
TRACE("destroy: unmapping window %lx from %s",
pane->window, PANE_STR(pane));
#if 0
pane->flags &= PF_MINIMIZED;
#endif
pane->flags &= ~PF_MAPPED;
#if 0
minimize(pane, l);
#endif
#if 0
if (XCheckTypedEvent(display, UnmapNotify, &event) ==
True) {
TRACE("destroy: there was another unmap in queue");
XPutBackEvent(display, &event);
remove_pane(pane, 0); /* No resize, please */
} else {
remove_pane(pane, 1); /* Want resize */
}
#endif
#if 0
if (pane->column != NULL)
remove_pane(pane, 1);
l->focus = NULL;
/* Need to move focus to somewhere else */
if (!(pane->flags & PF_WANT_RESTART))
focus_pane(find_previous_focus(l->head, NULL), l);
#endif
} else {
TRACE_ERR("destroy: did not find window to unmap from %s",
PANE_STR(pane));
}
}
void
observedestroy(Display *display, XContext context, Window window, struct layout *l)
{
struct pane *pane, *prev;
struct column *c;
int flags;
#if 0
XEvent event;
#endif
TRACE("destroy begin");
if ((pane = find_pane_by_window(window, l)) == NULL) {
TRACE_ERR("destroy: NULL mw in observedestroy");
return;
}
prev = pane->prev;
/*
* TODO: remove_pane calls resize column, which does operations with
* windows such as moving and resizing windows. However, if multiple
* windows have been "simultaneously" destroyed, meaning we have
* DestroyNotify's in the queue, we may have "stale" windows attached
* to a column. We could either XGetGeometry to find out whether
* the window still lives, or we could process the queue with
* XCheckEvent or similar to find out if we have pending
* DestroyNotify. Using XGetGeometry requires round-trips and is
* probably the slower approach.
* TODO: Remove this comment if it works now
* TODO: Remove this logic because it might not be needed if
* the unmapping logic does the job. In practice:
* 1. Unmap removes the window from the frame and runs resizing
* 2. Destroy removes the frame and runs resizing
* And this works because we control destroying the frame.
* Two modes are needed: unmapped and minimized. Minimized
* is always also unmapped.
*/
c = pane->column;
#if 0
if (XCheckTypedEvent(display, DestroyNotify, &event) == True) {
TRACE("destroy: there was another destroy in queue");
XPutBackEvent(display, &event);
remove_pane(pane, 0); /* No resize, please */
} else {
remove_pane(pane, 0); /* Want resize */
}
#elif 1
remove_pane(pane, 1);
#endif
if (pane->flags & PF_WANT_RESTART) {
if (c != NULL)
focus_column(c, l);
restart_pane(pane, l->display);
}
flags = pane->flags;
if (pane->frame) {
TRACE("destroy: destroying frame %lx in %s", pane->frame,
PANE_STR(pane));
/*
* This also destroys its children, obviously.
*/
XDestroyWindow(display, pane->frame);
}
XDeleteContext(display, pane->window, context);
XDeleteContext(display, pane->frame, context);
XDeleteContext(display, pane->maximize_button, context);
XDeleteContext(display, pane->close_button, context);
if (pane->name != NULL)
XFree(pane->name);
if (pane->icon_name != NULL)
XFree(pane->icon_name);
free(pane);
#if 1
/*
* This is currently a special case handling. We could do
* this inside other functions automatically, but we don't
* wish to clear focus and re-set it all the time in the
* normal cases, so we clear focus only here.
*/
l->focus = NULL;
/* Need to move focus to somewhere else */
if (!(flags & PF_WANT_RESTART))
focus_pane(find_previous_focus(l->head, NULL), l);
else if (prev != NULL)
focus_pane(prev, l);
#endif
}
static void
interceptconfigure(struct pane *p, struct layout *l, XContext context, Window w,
XConfigureRequestEvent e)
{
XWindowChanges xwc;
unsigned long xwcm;
if (p != NULL) {
TRACE("intercepted configure for managed pane, resize column");
if (p->column)
resize_relayout(p->column);
return;
}
TRACE("intercepted %d,%d to %d,%d", e.x, e.y, e.width, e.height);
xwcm = e.value_mask &
(CWX | CWY | CWWidth | CWHeight | CWBorderWidth);
xwc.x = l->column->x;
xwc.y = 0;
#if 0
xwc.width = e.width;
#else
/*
* Forcing all to be same width regardless of what is requested.
*/
xwc.width = l->column->width;
#endif
xwc.height = e.height;
xwc.border_width = 0;
TRACE("configuring window %lx to %d,%d, bw width %d",
e.window, xwc.width, xwc.height, xwc.border_width);
XConfigureWindow(e.display, e.window, xwcm, &xwc);
}