forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.c
1113 lines (1000 loc) · 35.7 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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* event.c - event handlers
*
* Copyright © 2007-2008 Julien Danjou <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "event.h"
#include "awesome.h"
#include "property.h"
#include "objects/tag.h"
#include "objects/drawin.h"
#include "xwindow.h"
#include "ewmh.h"
#include "objects/client.h"
#include "keygrabber.h"
#include "mousegrabber.h"
#include "luaa.h"
#include "systray.h"
#include "xkb.h"
#include "objects/screen.h"
#include "common/atoms.h"
#include "common/xutil.h"
#include <xcb/xcb.h>
#include <xcb/randr.h>
#include <xcb/shape.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_event.h>
#include <xcb/xkb.h>
#define DO_EVENT_HOOK_CALLBACK(type, xcbtype, xcbeventprefix, arraytype, match) \
static void \
event_##xcbtype##_callback(xcb_##xcbtype##_press_event_t *ev, \
arraytype *arr, \
lua_State *L, \
int oud, \
int nargs, \
void *data) \
{ \
int abs_oud = oud < 0 ? ((lua_gettop(L) + 1) + oud) : oud; \
int item_matching = 0; \
foreach(item, *arr) \
if(match(ev, *item, data)) \
{ \
if(oud) \
luaA_object_push_item(L, abs_oud, *item); \
else \
luaA_object_push(L, *item); \
item_matching++; \
} \
for(; item_matching > 0; item_matching--) \
{ \
switch(ev->response_type) \
{ \
case xcbeventprefix##_PRESS: \
for(int i = 0; i < nargs; i++) \
lua_pushvalue(L, - nargs - item_matching); \
luaA_object_emit_signal(L, - nargs - 1, "press", nargs); \
break; \
case xcbeventprefix##_RELEASE: \
for(int i = 0; i < nargs; i++) \
lua_pushvalue(L, - nargs - item_matching); \
luaA_object_emit_signal(L, - nargs - 1, "release", nargs); \
break; \
} \
lua_pop(L, 1); \
} \
lua_pop(L, nargs); \
}
static bool
event_key_match(xcb_key_press_event_t *ev, keyb_t *k, void *data)
{
assert(data);
xcb_keysym_t keysym = *(xcb_keysym_t *) data;
return (((k->keycode && ev->detail == k->keycode)
|| (k->keysym && keysym == k->keysym))
&& (k->modifiers == XCB_BUTTON_MASK_ANY || k->modifiers == ev->state));
}
static bool
event_button_match(xcb_button_press_event_t *ev, button_t *b, void *data)
{
return ((!b->button || ev->detail == b->button)
&& (b->modifiers == XCB_BUTTON_MASK_ANY || b->modifiers == ev->state));
}
DO_EVENT_HOOK_CALLBACK(button_t, button, XCB_BUTTON, button_array_t, event_button_match)
DO_EVENT_HOOK_CALLBACK(keyb_t, key, XCB_KEY, key_array_t, event_key_match)
/** Handle an event with mouse grabber if needed
* \param x The x coordinate.
* \param y The y coordinate.
* \param mask The mask buttons.
* \return True if the event was handled.
*/
static bool
event_handle_mousegrabber(int x, int y, uint16_t mask)
{
if(globalconf.mousegrabber != LUA_REFNIL)
{
lua_State *L = globalconf_get_lua_State();
mousegrabber_handleevent(L, x, y, mask);
lua_rawgeti(L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
if(!luaA_dofunction(L, 1, 1))
{
warn("Stopping mousegrabber.");
luaA_mousegrabber_stop(L);
} else {
if(!lua_isboolean(L, -1) || !lua_toboolean(L, -1))
luaA_mousegrabber_stop(L);
lua_pop(L, 1); /* pop returned value */
}
return true;
}
return false;
}
/** Emit a button signal.
* The top of the lua stack has to be the object on which to emit the event.
* \param L The Lua VM state.
* \param ev The event to handle.
*/
static void
event_emit_button(lua_State *L, xcb_button_press_event_t *ev)
{
const char *name;
switch(XCB_EVENT_RESPONSE_TYPE(ev))
{
case XCB_BUTTON_PRESS:
name = "button::press";
break;
case XCB_BUTTON_RELEASE:
name = "button::release";
break;
default:
fatal("Invalid event type");
}
/* Push the event's info */
lua_pushinteger(L, ev->event_x);
lua_pushinteger(L, ev->event_y);
lua_pushinteger(L, ev->detail);
luaA_pushmodifiers(L, ev->state);
/* And emit the signal */
luaA_object_emit_signal(L, -5, name, 4);
}
/** The button press event handler.
* \param ev The event.
*/
static void
event_handle_button(xcb_button_press_event_t *ev)
{
lua_State *L = globalconf_get_lua_State();
client_t *c;
drawin_t *drawin;
globalconf.timestamp = ev->time;
{
/* ev->state contains the state before the event. Compute the state
* after the event for the mousegrabber.
*/
uint16_t state = ev->state, change = 1 << (ev->detail - 1 + 8);
if (XCB_EVENT_RESPONSE_TYPE(ev) == XCB_BUTTON_PRESS)
state |= change;
else
state &= ~change;
if(event_handle_mousegrabber(ev->root_x, ev->root_y, state))
return;
}
/* ev->state is
* button status (8 bits) + modifiers status (8 bits)
* we don't care for button status that we get, especially on release, so
* drop them */
ev->state &= 0x00ff;
if((drawin = drawin_getbywin(ev->event))
|| (drawin = drawin_getbywin(ev->child)))
{
/* If the drawin is child, then x,y are
* relative to root window */
if(drawin->window == ev->child)
{
ev->event_x -= drawin->geometry.x + drawin->border_width;
ev->event_y -= drawin->geometry.y + drawin->border_width;
}
/* Push the drawable */
luaA_object_push(L, drawin);
luaA_object_push_item(L, -1, drawin->drawable);
/* and handle the button raw button event */
event_emit_button(L, ev);
lua_pop(L, 1);
/* check if any button object matches */
event_button_callback(ev, &drawin->buttons, L, -1, 1, NULL);
/* Either we are receiving this due to ButtonPress/Release on the root
* window or because we grabbed the button on the window. In the later
* case we have to call AllowEvents.
* Use AsyncPointer instead of ReplayPointer so that the event is
* "eaten" instead of being handled again on the root window.
*/
if(ev->child == XCB_NONE)
xcb_allow_events(globalconf.connection,
XCB_ALLOW_ASYNC_POINTER,
ev->time);
}
else if((c = client_getbyframewin(ev->event)) || (c = client_getbywin(ev->event)))
{
/* For clicks inside of c->window, we get two events. Once because of a
* passive grab on c->window and then again for c->frame_window.
* Ignore the second event (identifiable by ev->child != XCB_NONE).
*/
if (ev->event != c->frame_window || ev->child == XCB_NONE)
{
luaA_object_push(L, c);
if (c->window == ev->event)
{
/* Button event into the client itself (not titlebar), translate
* into the frame window.
*/
ev->event_x += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
ev->event_y += c->titlebar[CLIENT_TITLEBAR_TOP].size;
}
/* And handle the button raw button event */
event_emit_button(L, ev);
/* then check if a titlebar was "hit" */
if (c->frame_window == ev->event)
{
int x = ev->event_x, y = ev->event_y;
drawable_t *d = client_get_drawable_offset(c, &x, &y);
if (d)
{
/* Copy the event so that we can fake x/y */
xcb_button_press_event_t event = *ev;
event.event_x = x;
event.event_y = y;
luaA_object_push_item(L, -1, d);
event_emit_button(L, &event);
lua_pop(L, 1);
}
}
/* then check if any button objects match */
event_button_callback(ev, &c->buttons, L, -1, 1, NULL);
}
xcb_allow_events(globalconf.connection,
XCB_ALLOW_REPLAY_POINTER,
ev->time);
}
else if(ev->child == XCB_NONE)
if(globalconf.screen->root == ev->event)
{
event_button_callback(ev, &globalconf.buttons, L, 0, 0, NULL);
return;
}
}
static void
event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
{
uint16_t config_win_mask = 0;
uint32_t config_win_vals[7];
unsigned short i = 0;
if(ev->value_mask & XCB_CONFIG_WINDOW_X)
{
config_win_mask |= XCB_CONFIG_WINDOW_X;
config_win_vals[i++] = ev->x;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
{
config_win_mask |= XCB_CONFIG_WINDOW_Y;
config_win_vals[i++] = ev->y;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
{
config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
config_win_vals[i++] = ev->width;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
{
config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
config_win_vals[i++] = ev->height;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
{
config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
config_win_vals[i++] = ev->border_width;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
{
config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
config_win_vals[i++] = ev->sibling;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
{
config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
config_win_vals[i++] = ev->stack_mode;
}
xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
}
/** The configure event handler.
* \param ev The event.
*/
static void
event_handle_configurerequest(xcb_configure_request_event_t *ev)
{
client_t *c;
if((c = client_getbywin(ev->window)))
{
area_t geometry = c->geometry;
uint16_t bw = c->border_width;
uint16_t tb_left = c->titlebar[CLIENT_TITLEBAR_LEFT].size;
uint16_t tb_right = c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
uint16_t tb_top = c->titlebar[CLIENT_TITLEBAR_TOP].size;
uint16_t tb_bottom = c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
uint16_t deco_left = bw + tb_left;
uint16_t deco_right = bw + tb_right;
uint16_t deco_top = bw + tb_top;
uint16_t deco_bottom = bw + tb_bottom;
int16_t diff_w = 0, diff_h = 0, diff_border = 0;
lua_State *L = globalconf_get_lua_State();
if(ev->value_mask & XCB_CONFIG_WINDOW_X)
{
int16_t diff = 0;
geometry.x = ev->x;
xwindow_translate_for_gravity(c->size_hints.win_gravity, deco_left, 0, deco_right, 0, &diff, NULL);
geometry.x += diff;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
{
int16_t diff = 0;
geometry.y = ev->y;
xwindow_translate_for_gravity(c->size_hints.win_gravity, 0, deco_top, 0, deco_bottom, NULL, &diff);
geometry.y += diff;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
{
uint16_t old_w = geometry.width;
geometry.width = ev->width;
/* The ConfigureRequest specifies the size of the client window, we want the frame */
geometry.width += tb_left + tb_right;
diff_w = geometry.width - old_w;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
{
uint16_t old_h = geometry.height;
geometry.height = ev->height;
/* The ConfigureRequest specifies the size of the client window, we want the frame */
geometry.height += tb_top + tb_bottom;
diff_h = geometry.height - old_h;
}
if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
{
diff_border = ev->border_width - bw;
diff_h += diff_border;
diff_w += diff_border;
luaA_object_push(L, c);
window_set_border_width(L, -1, ev->border_width);
lua_pop(L, 1);
}
/* If the client resizes without moving itself, apply window gravity */
if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY)
{
int16_t diff_x = 0, diff_y = 0;
xwindow_translate_for_gravity(c->size_hints.win_gravity, diff_border, diff_border, diff_w, diff_h, &diff_x, &diff_y);
if(!(ev->value_mask & XCB_CONFIG_WINDOW_X))
geometry.x += diff_x;
if(!(ev->value_mask & XCB_CONFIG_WINDOW_Y))
geometry.y += diff_y;
}
c->got_configure_request = true;
/* Request the changes to be applied */
luaA_object_push(L, c);
lua_pushstring(L, "ewmh"); /* context */
lua_newtable(L); /* props */
/* area, it needs to be directly in the `hints` table to comply with
the "protocol"
*/
lua_pushstring(L, "x");
lua_pushinteger(L, geometry.x);
lua_rawset(L, -3);
lua_pushstring(L, "y");
lua_pushinteger(L, geometry.y);
lua_rawset(L, -3);
lua_pushstring(L, "width");
lua_pushinteger(L, geometry.width);
lua_rawset(L, -3);
lua_pushstring(L, "height");
lua_pushinteger(L, geometry.height);
lua_rawset(L, -3);
luaA_object_emit_signal(L, -3, "request::geometry", 2);
lua_pop(L, 1);
}
else if (xembed_getbywin(&globalconf.embedded, ev->window))
{
/* Ignore this so that systray icons cannot resize themselves.
* We decide their size!
* However, Xembed says that we act like a WM to the embedded window and
* thus we have to send a synthetic configure notify informing the
* window that its configure request was denied.
*/
xcb_get_geometry_cookie_t geom_cookie =
xcb_get_geometry_unchecked(globalconf.connection, ev->window);
xcb_translate_coordinates_cookie_t coords_cookie =
xcb_translate_coordinates_unchecked(globalconf.connection,
ev->window, globalconf.screen->root, 0, 0);
xcb_get_geometry_reply_t *geom =
xcb_get_geometry_reply(globalconf.connection, geom_cookie, NULL);
xcb_translate_coordinates_reply_t *coords =
xcb_translate_coordinates_reply(globalconf.connection, coords_cookie, NULL);
if (geom && coords)
{
xwindow_configure(ev->window,
(area_t) { .x = coords->dst_x,
.y = coords->dst_y,
.width = geom->width,
.height = geom->height },
0);
}
p_delete(&geom);
p_delete(&coords);
}
else
event_handle_configurerequest_configure_window(ev);
}
/** The configure notify event handler.
* \param ev The event.
*/
static void
event_handle_configurenotify(xcb_configure_notify_event_t *ev)
{
xcb_screen_t *screen = globalconf.screen;
if(ev->window == screen->root)
globalconf.screen_need_refresh = true;
/* Copy what XRRUpdateConfiguration() would do: Update the configuration */
if(ev->window == screen->root) {
screen->width_in_pixels = ev->width;
screen->height_in_pixels = ev->height;
}
}
/** The destroy notify event handler.
* \param ev The event.
*/
static void
event_handle_destroynotify(xcb_destroy_notify_event_t *ev)
{
client_t *c;
if((c = client_getbywin(ev->window)))
client_unmanage(c, false);
else
for(int i = 0; i < globalconf.embedded.len; i++)
if(globalconf.embedded.tab[i].win == ev->window)
{
xembed_window_array_take(&globalconf.embedded, i);
luaA_systray_invalidate();
}
}
/** Record that the given drawable contains the pointer.
*/
void
event_drawable_under_mouse(lua_State *L, int ud)
{
void *d;
lua_pushvalue(L, ud);
d = luaA_object_ref(L, -1);
if (d == globalconf.drawable_under_mouse)
{
/* Nothing to do */
luaA_object_unref(L, d);
return;
}
if (globalconf.drawable_under_mouse != NULL)
{
/* Emit leave on previous drawable */
luaA_object_push(L, globalconf.drawable_under_mouse);
luaA_object_emit_signal(L, -1, "mouse::leave", 0);
lua_pop(L, 1);
/* Unref the previous drawable */
luaA_object_unref(L, globalconf.drawable_under_mouse);
globalconf.drawable_under_mouse = NULL;
}
if (d != NULL)
{
/* Reference the drawable for leave event later */
globalconf.drawable_under_mouse = d;
/* Emit enter */
luaA_object_emit_signal(L, ud, "mouse::enter", 0);
}
}
/** The motion notify event handler.
* \param ev The event.
*/
static void
event_handle_motionnotify(xcb_motion_notify_event_t *ev)
{
lua_State *L = globalconf_get_lua_State();
drawin_t *w;
client_t *c;
globalconf.timestamp = ev->time;
if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
return;
if((c = client_getbyframewin(ev->event)))
{
luaA_object_push(L, c);
lua_pushinteger(L, ev->event_x);
lua_pushinteger(L, ev->event_y);
luaA_object_emit_signal(L, -3, "mouse::move", 2);
/* now check if a titlebar was "hit" */
int x = ev->event_x, y = ev->event_y;
drawable_t *d = client_get_drawable_offset(c, &x, &y);
if (d)
{
luaA_object_push_item(L, -1, d);
event_drawable_under_mouse(L, -1);
lua_pushinteger(L, x);
lua_pushinteger(L, y);
luaA_object_emit_signal(L, -3, "mouse::move", 2);
lua_pop(L, 1);
}
lua_pop(L, 1);
}
if((w = drawin_getbywin(ev->event)))
{
luaA_object_push(L, w);
luaA_object_push_item(L, -1, w->drawable);
event_drawable_under_mouse(L, -1);
lua_pushinteger(L, ev->event_x);
lua_pushinteger(L, ev->event_y);
luaA_object_emit_signal(L, -3, "mouse::move", 2);
lua_pop(L, 2);
}
}
/** The leave notify event handler.
* \param ev The event.
*/
static void
event_handle_leavenotify(xcb_leave_notify_event_t *ev)
{
lua_State *L = globalconf_get_lua_State();
client_t *c;
globalconf.timestamp = ev->time;
if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
return;
/* Ignore leave with detail inferior (we were left for a window contained in
* our window, so technically the pointer is still inside of this window).
*/
if(ev->detail != XCB_NOTIFY_DETAIL_INFERIOR && (c = client_getbyframewin(ev->event)))
{
luaA_object_push(L, c);
luaA_object_emit_signal(L, -1, "mouse::leave", 0);
lua_pop(L, 1);
}
lua_pushnil(L);
event_drawable_under_mouse(L, -1);
lua_pop(L, 1);
}
/** The enter notify event handler.
* \param ev The event.
*/
static void
event_handle_enternotify(xcb_enter_notify_event_t *ev)
{
lua_State *L = globalconf_get_lua_State();
client_t *c;
drawin_t *drawin;
globalconf.timestamp = ev->time;
if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
return;
if((drawin = drawin_getbywin(ev->event)))
{
luaA_object_push(L, drawin);
luaA_object_push_item(L, -1, drawin->drawable);
event_drawable_under_mouse(L, -1);
lua_pop(L, 2);
}
if((c = client_getbyframewin(ev->event)))
{
luaA_object_push(L, c);
/* Ignore enter with detail inferior: The pointer was previously inside
* of a child window, so technically this isn't a 'real' enter.
*/
if (ev->detail != XCB_NOTIFY_DETAIL_INFERIOR)
luaA_object_emit_signal(L, -1, "mouse::enter", 0);
drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
if (d)
{
luaA_object_push_item(L, -1, d);
event_drawable_under_mouse(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1);
}
else if (ev->event == globalconf.screen->root) {
/* When there are multiple X screens with awesome running separate
* instances, reset focus.
*/
globalconf.focus.need_update = true;
}
}
/** The focus in event handler.
* \param ev The event.
*/
static void
event_handle_focusin(xcb_focus_in_event_t *ev)
{
if (ev->mode == XCB_NOTIFY_MODE_GRAB
|| ev->mode == XCB_NOTIFY_MODE_UNGRAB)
/* Ignore focus changes due to keyboard grabs */
return;
/* Events that we are interested in: */
switch(ev->detail)
{
/* These are events that jump between root windows.
*/
case XCB_NOTIFY_DETAIL_ANCESTOR:
case XCB_NOTIFY_DETAIL_INFERIOR:
/* These are events that jump between clients.
* Virtual events ensure we always get an event on our top-level window.
*/
case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
case XCB_NOTIFY_DETAIL_NONLINEAR:
{
client_t *c;
if((c = client_getbywin(ev->event))) {
/* If there is still a pending focus change, do it now. */
client_focus_refresh();
client_focus_update(c);
}
}
/* all other events are ignored */
default:
break;
}
}
/** The expose event handler.
* \param ev The event.
*/
static void
event_handle_expose(xcb_expose_event_t *ev)
{
drawin_t *drawin;
client_t *client;
if((drawin = drawin_getbywin(ev->window)))
drawin_refresh_pixmap_partial(drawin,
ev->x, ev->y,
ev->width, ev->height);
if ((client = client_getbyframewin(ev->window)))
client_refresh_partial(client, ev->x, ev->y, ev->width, ev->height);
}
/** The key press event handler.
* \param ev The event.
*/
static void
event_handle_key(xcb_key_press_event_t *ev)
{
lua_State *L = globalconf_get_lua_State();
globalconf.timestamp = ev->time;
if(globalconf.keygrabber != LUA_REFNIL)
{
if(keygrabber_handlekpress(L, ev))
{
lua_rawgeti(L, LUA_REGISTRYINDEX, globalconf.keygrabber);
if(!luaA_dofunction(L, 3, 0))
{
warn("Stopping keygrabber.");
luaA_keygrabber_stop(L);
}
}
}
else
{
/* get keysym ignoring all modifiers */
xcb_keysym_t keysym = xcb_key_symbols_get_keysym(globalconf.keysyms, ev->detail, 0);
client_t *c;
if((c = client_getbywin(ev->event)) || (c = client_getbynofocuswin(ev->event)))
{
luaA_object_push(L, c);
event_key_callback(ev, &c->keys, L, -1, 1, &keysym);
}
else
event_key_callback(ev, &globalconf.keys, L, 0, 0, &keysym);
}
}
/** The map request event handler.
* \param ev The event.
*/
static void
event_handle_maprequest(xcb_map_request_event_t *ev)
{
client_t *c;
xembed_window_t *em;
xcb_get_window_attributes_cookie_t wa_c;
xcb_get_window_attributes_reply_t *wa_r;
xcb_get_geometry_cookie_t geom_c;
xcb_get_geometry_reply_t *geom_r;
wa_c = xcb_get_window_attributes_unchecked(globalconf.connection, ev->window);
if(!(wa_r = xcb_get_window_attributes_reply(globalconf.connection, wa_c, NULL)))
return;
if(wa_r->override_redirect)
goto bailout;
if((em = xembed_getbywin(&globalconf.embedded, ev->window)))
{
xcb_map_window(globalconf.connection, ev->window);
xembed_window_activate(globalconf.connection, ev->window, globalconf.timestamp);
/* The correct way to set this is via the _XEMBED_INFO property. Neither
* of the XEMBED not the systray spec talk about mapping windows.
* Apparently, Qt doesn't care and does not set an _XEMBED_INFO
* property. Let's simulate the XEMBED_MAPPED bit.
*/
em->info.flags |= XEMBED_MAPPED;
luaA_systray_invalidate();
}
else if((c = client_getbywin(ev->window)))
{
/* Check that it may be visible, but not asked to be hidden */
if(client_on_selected_tags(c) && !c->hidden)
{
lua_State *L = globalconf_get_lua_State();
luaA_object_push(L, c);
client_set_minimized(L, -1, false);
lua_pop(L, 1);
/* it will be raised, so just update ourself */
client_raise(c);
}
}
else
{
geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
{
goto bailout;
}
client_manage(ev->window, geom_r, wa_r);
p_delete(&geom_r);
}
bailout:
p_delete(&wa_r);
}
/** The unmap notify event handler.
* \param ev The event.
*/
static void
event_handle_unmapnotify(xcb_unmap_notify_event_t *ev)
{
client_t *c;
if((c = client_getbywin(ev->window)))
client_unmanage(c, true);
}
/** The randr screen change notify event handler.
* \param ev The event.
*/
static void
event_handle_randr_screen_change_notify(xcb_randr_screen_change_notify_event_t *ev)
{
/* Ignore events for other roots (do we get them at all?) */
if (ev->root != globalconf.screen->root)
return;
/* Do (part of) what XRRUpdateConfiguration() would do (update our state) */
if (ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270)) {
globalconf.screen->width_in_pixels = ev->height;
globalconf.screen->width_in_millimeters = ev->mheight;
globalconf.screen->height_in_pixels = ev->width;
globalconf.screen->height_in_millimeters = ev->mwidth;
} else {
globalconf.screen->width_in_pixels = ev->width;
globalconf.screen->width_in_millimeters = ev->mwidth;
globalconf.screen->height_in_pixels = ev->height;
globalconf.screen->height_in_millimeters = ev->mheight;;
}
globalconf.screen_need_refresh = true;
}
/** XRandR event handler for RRNotify subtype XRROutputChangeNotifyEvent
*/
static void
event_handle_randr_output_change_notify(xcb_randr_notify_event_t *ev)
{
if(ev->subCode == XCB_RANDR_NOTIFY_OUTPUT_CHANGE) {
xcb_randr_output_t output = ev->u.oc.output;
uint8_t connection = ev->u.oc.connection;
const char *connection_str = NULL;
xcb_randr_get_output_info_reply_t *info = NULL;
lua_State *L = globalconf_get_lua_State();
/* The following explicitly uses XCB_CURRENT_TIME since we want to know
* the final state of the connection. There could be more notification
* events underway and using some "old" timestamp causes problems.
*/
info = xcb_randr_get_output_info_reply(globalconf.connection,
xcb_randr_get_output_info_unchecked(globalconf.connection,
output,
XCB_CURRENT_TIME),
NULL);
if(!info)
return;
switch(connection) {
case XCB_RANDR_CONNECTION_CONNECTED:
connection_str = "Connected";
break;
case XCB_RANDR_CONNECTION_DISCONNECTED:
connection_str = "Disconnected";
break;
default:
connection_str = "Unknown";
break;
}
lua_pushlstring(L, (char *)xcb_randr_get_output_info_name(info), xcb_randr_get_output_info_name_length(info));
lua_pushstring(L, connection_str);
signal_object_emit(L, &global_signals, "screen::change", 2);
p_delete(&info);
/* The docs for RRSetOutputPrimary say we get this signal */
screen_update_primary();
}
}
/** The shape notify event handler.
* \param ev The event.
*/
static void
event_handle_shape_notify(xcb_shape_notify_event_t *ev)
{
client_t *c = client_getbywin(ev->affected_window);
if (c)
{
lua_State *L = globalconf_get_lua_State();
luaA_object_push(L, c);
if (ev->shape_kind == XCB_SHAPE_SK_BOUNDING)
luaA_object_emit_signal(L, -1, "property::shape_client_bounding", 0);
if (ev->shape_kind == XCB_SHAPE_SK_CLIP)
luaA_object_emit_signal(L, -1, "property::shape_client_clip", 0);
lua_pop(L, 1);
}
}
/** The client message event handler.
* \param ev The event.
*/
static void
event_handle_clientmessage(xcb_client_message_event_t *ev)
{
/* check for startup notification messages */
if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
return;
if(ev->type == WM_CHANGE_STATE)
{
client_t *c;
if((c = client_getbywin(ev->window))
&& ev->format == 32
&& ev->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC)
{
lua_State *L = globalconf_get_lua_State();
luaA_object_push(L, c);
client_set_minimized(L, -1, true);
lua_pop(L, 1);
}
}
else if(ev->type == _XEMBED)
xembed_process_client_message(ev);
else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
systray_process_client_message(ev);
else
ewmh_process_client_message(ev);
}
static void
event_handle_reparentnotify(xcb_reparent_notify_event_t *ev)
{
client_t *c;
if((c = client_getbywin(ev->window)) && c->frame_window != ev->parent)
{
/* Ignore reparents to the root window, they *might* be caused by
* ourselves if a client quickly unmaps and maps itself again. */
if (ev->parent != globalconf.screen->root)
client_unmanage(c, true);
}
else if (ev->parent != globalconf.systray.window) {
/* Embedded window moved elsewhere, end of embedding */
for(int i = 0; i < globalconf.embedded.len; i++)
if(globalconf.embedded.tab[i].win == ev->window)
{
xembed_window_array_take(&globalconf.embedded, i);
xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, ev->window);
luaA_systray_invalidate();
}
}
}
static void
event_handle_selectionclear(xcb_selection_clear_event_t *ev)
{
if(ev->selection == globalconf.selection_atom)
{
warn("Lost WM_Sn selection, exiting...");
g_main_loop_quit(globalconf.loop);
}
}
/** \brief awesome xerror function.
* There's no way to check accesses to destroyed windows, thus those cases are
* ignored (especially on UnmapNotify's).
* \param e The error event.
*/
static void
xerror(xcb_generic_error_t *e)
{
/* ignore this */
if(e->error_code == XCB_WINDOW
|| (e->error_code == XCB_MATCH