forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.c
More file actions
609 lines (544 loc) · 17.7 KB
/
Copy pathevent.c
File metadata and controls
609 lines (544 loc) · 17.7 KB
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
/*
* event.c - event handlers
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <xcb/xcb.h>
#include <xcb/randr.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_event.h>
#include "event.h"
#include "property.h"
#include "objects/tag.h"
#include "xwindow.h"
#include "ewmh.h"
#include "keyresolv.h"
#include "systray.h"
#include "screen.h"
#include "spawn.h"
#include "screen.h"
#include "common/atoms.h"
#include "screen.h"
#include "common/xutil.h"
static window_t *
window_getbywin(xcb_window_t window)
{
if(_G_screen->root == window)
return _G_screens.tab[0].root;
return (window_t *) ewindow_getbywin(window);
}
/** Push a modifier set to a Lua table.
* \param L The Lua VM state.
* \param modifiers The modifier.
* \return The number of elements pushed on stack.
*/
static int
luaA_pushmodifiers(lua_State *L, uint16_t modifiers)
{
lua_createtable(L, 8, 8);
int i = 1;
for(uint32_t mask = XCB_MOD_MASK_SHIFT; mask <= XCB_KEY_BUT_MASK_BUTTON_5; mask <<= 1)
{
const char *mod;
size_t slen;
xutil_key_mask_tostr(mask, &mod, &slen);
if(mask & modifiers)
{
lua_pushlstring(L, mod, slen);
lua_rawseti(L, -2, i++);
}
lua_pushlstring(L, mod, slen);
lua_pushboolean(L, mask & modifiers);
lua_rawset(L, -3);
}
return 1;
}
/** The button press event handler.
* \param ev The event.
*/
static void
event_handle_button(xcb_button_press_event_t *ev)
{
luaA_pushmodifiers(_G_L, ev->state);
lua_pushinteger(_G_L, ev->detail);
switch(ev->response_type)
{
case XCB_BUTTON_PRESS:
window_emit_signal_noret(_G_L, window_getbywin(ev->event), "button::press", 2);
break;
case XCB_BUTTON_RELEASE:
window_emit_signal_noret(_G_L, window_getbywin(ev->event), "button::release", 2);
break;
default: /* wtf? */
lua_pop(_G_L, 2);
break;
}
}
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(_G_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;
if(ev->value_mask & XCB_CONFIG_WINDOW_X)
geometry.x = ev->x;
if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
geometry.y = ev->y;
if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
geometry.width = ev->width;
if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
geometry.height = ev->height;
if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
ewindow_set_border_width(_G_L, (ewindow_t *) c, ev->border_width);
if(!window_set_geometry(_G_L, (window_t *) c, geometry))
/* ICCCM 4.1.5 / 4.2.3, if nothing was changed, send an event saying so */
xwindow_configure(c->window, geometry, c->border_width);
}
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)
{
if(ev->window == _G_screen->root)
{
bool geometry_has_changed = false;
if(_G_root->geometry.width != ev->width)
{
_G_root->geometry.width = ev->width;
window_emit_signal_noret(_G_L, _G_root, "property::width", 0);
geometry_has_changed = true;
}
if(_G_root->geometry.height != ev->height)
{
_G_root->geometry.height = ev->height;
window_emit_signal_noret(_G_L, _G_root, "property:height", 0);
geometry_has_changed = true;
}
if(geometry_has_changed)
window_emit_signal_noret(_G_L, _G_root, "property::geometry", 0);
}
}
/** 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(_G_L, c);
}
/** The motion notify event handler.
* \param ev The event.
*/
static void
event_handle_motionnotify(xcb_motion_notify_event_t *ev)
{
window_t *window;
if(ev->child)
window = window_getbywin(ev->child);
else
window = window_getbywin(ev->event);
luaA_pushmodifiers(_G_L, ev->state);
lua_pushinteger(_G_L, ev->event_x);
lua_pushinteger(_G_L, ev->event_y);
lua_pushinteger(_G_L, ev->root_x);
lua_pushinteger(_G_L, ev->root_y);
window_emit_signal_noret(_G_L, window, "mouse::move", 5);
}
/** The enter and leave notify event handler.
* \param data currently unused.
* \param connection The connection to the X server.
* \param ev The event.
*/
static void
event_handle_enterleavenotify(xcb_enter_notify_event_t *ev)
{
if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
return;
luaA_pushmodifiers(_G_L, ev->state);
lua_pushinteger(_G_L, ev->event_x);
lua_pushinteger(_G_L, ev->event_y);
lua_pushinteger(_G_L, ev->root_x);
lua_pushinteger(_G_L, ev->root_y);
switch(ev->response_type)
{
case XCB_ENTER_NOTIFY:
window_emit_signal_noret(_G_L, window_getbywin(ev->event), "mouse::enter", 5);
break;
case XCB_LEAVE_NOTIFY:
window_emit_signal_noret(_G_L, window_getbywin(ev->event), "mouse::leave", 5);
break;
default: /* wtf */
lua_pop(_G_L, 3);
break;
}
}
/** The focus in event handler.
* \param ev The event.
*/
static void
event_handle_focusin(xcb_focus_in_event_t *ev)
{
/* 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:
{
window_t *window = window_getbywin(ev->event);
if(window)
window_focus_update(window);
}
/* all other events are ignored */
default:
break;
}
}
/** The focus out event handler.
* \param data currently unused.
* \param connection The connection to the X server.
* \param ev The event.
*/
static int
event_handle_focusout(xcb_focus_in_event_t *ev)
{
/* 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:
{
window_t *window = window_getbywin(ev->event);
if(window)
window_unfocus_update(window);
}
/* all other events are ignored */
default:
break;
}
return 0;
}
/** The expose event handler.
* \param ev The event.
*/
static void
event_handle_expose(xcb_expose_event_t *ev)
{
wibox_t *wibox;
/* If the wibox got need_update set, skip this because it will be repainted
* soon anyway. Without this we could be painting garbage to the screen!
*/
if((wibox = wibox_getbywin(ev->window)) && !wibox->need_update)
wibox_refresh_pixmap_partial(wibox,
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)
{
/* get keysym ignoring all modifiers */
xcb_keysym_t keysym = keyresolv_get_keysym(ev->detail, ev->state);
/* Push modifiers */
luaA_pushmodifiers(_G_L, ev->state);
/* Push keycode */
lua_pushinteger(_G_L, ev->detail);
/* Push keysym */
char buf[MAX(MB_LEN_MAX, 64)];
if(keyresolv_keysym_to_string(keysym, buf, sizeof(buf)))
lua_pushstring(_G_L, buf);
else
lua_pushnil(_G_L);
lua_pushinteger(_G_L, ev->event_x);
lua_pushinteger(_G_L, ev->event_y);
lua_pushinteger(_G_L, ev->root_x);
lua_pushinteger(_G_L, ev->root_y);
switch(ev->response_type)
{
case XCB_KEY_PRESS:
window_emit_signal_noret(_G_L, window_getbywin(ev->event), "key::press", 7);
break;
case XCB_KEY_RELEASE:
window_emit_signal_noret(_G_L, window_getbywin(ev->event), "key::release", 7);
break;
default: /* wtf? */
lua_pop(_G_L, 7);
break;
}
}
/** The map request event handler.
* \param ev The event.
*/
static void
event_handle_maprequest(xcb_map_request_event_t *ev)
{
client_t *c;
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(_G_connection, ev->window);
if(!(wa_r = xcb_get_window_attributes_reply(_G_connection, wa_c, NULL)))
return;
if(wa_r->override_redirect)
goto bailout;
if((c = client_getbywin(ev->window)))
{
/* Check that it may be visible, but not asked to be hidden */
if(ewindow_isvisible((ewindow_t *) c))
{
ewindow_set_minimized(_G_L, (ewindow_t *) c, false);
/* it will be raised, so just update ourself */
stack_window_raise(_G_L, (window_t *) c);
}
}
else
{
geom_c = xcb_get_geometry_unchecked(_G_connection, ev->window);
if(!(geom_r = xcb_get_geometry_reply(_G_connection, geom_c, NULL)))
{
goto bailout;
}
client_manage(ev->window, geom_r, false);
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(_G_L, c);
}
/** 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)
{
/* Code of XRRUpdateConfiguration Xlib function ported to XCB
* (only the code relevant to RRScreenChangeNotify) as the latter
* doesn't provide this kind of function */
if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
xcb_randr_set_screen_size(_G_connection, ev->root, ev->height, ev->width,
ev->mheight, ev->mwidth);
else
xcb_randr_set_screen_size(_G_connection, ev->root, ev->width, ev->height,
ev->mwidth, ev->mheight);
/* XRRUpdateConfiguration also executes the following instruction
* but it's not useful because SubpixelOrder is not used at all at
* the moment
*
* XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
*/
awesome_restart();
}
/** 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(_G_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_WM_STATE_ICONIC)
ewindow_set_minimized(_G_L, (ewindow_t *) c, true);
}
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);
}
/** The keymap change notify event handler.
* \param ev The event.
*/
static void
event_handle_mappingnotify(xcb_mapping_notify_event_t *ev)
{
if(ev->request == XCB_MAPPING_MODIFIER
|| ev->request == XCB_MAPPING_KEYBOARD)
{
xcb_get_modifier_mapping_cookie_t xmapping_cookie =
xcb_get_modifier_mapping_unchecked(_G_connection);
/* Free and then allocate the key symbols */
xcb_key_symbols_free(_G_keysyms);
_G_keysyms = xcb_key_symbols_alloc(_G_connection);
keyresolv_lock_mask_refresh(_G_connection, xmapping_cookie, _G_keysyms);
}
}
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 != _G_screen->root)
client_unmanage(_G_L, c);
}
}
/** \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_EVENT_ERROR_BAD_WINDOW
|| (e->error_code == XCB_EVENT_ERROR_BAD_MATCH
&& e->major_code == XCB_SET_INPUT_FOCUS)
|| (e->error_code == XCB_EVENT_ERROR_BAD_VALUE
&& e->major_code == XCB_KILL_CLIENT)
|| (e->major_code == XCB_CONFIGURE_WINDOW
&& e->error_code == XCB_EVENT_ERROR_BAD_MATCH))
return;
warn("X error: request=%s, error=%s",
xcb_event_get_request_label(e->major_code),
xcb_event_get_error_label(e->error_code));
return;
}
void event_handle(xcb_generic_event_t *event)
{
uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
if(response_type == 0)
{
/* This is an error, not a event */
xerror((xcb_generic_error_t *) event);
return;
}
switch(response_type)
{
#define EVENT(type, callback) case type: callback((void *) event); return
EVENT(XCB_BUTTON_PRESS, event_handle_button);
EVENT(XCB_BUTTON_RELEASE, event_handle_button);
EVENT(XCB_CONFIGURE_REQUEST, event_handle_configurerequest);
EVENT(XCB_CONFIGURE_NOTIFY, event_handle_configurenotify);
EVENT(XCB_DESTROY_NOTIFY, event_handle_destroynotify);
EVENT(XCB_ENTER_NOTIFY, event_handle_enterleavenotify);
EVENT(XCB_CLIENT_MESSAGE, event_handle_clientmessage);
EVENT(XCB_EXPOSE, event_handle_expose);
EVENT(XCB_FOCUS_IN, event_handle_focusin);
EVENT(XCB_FOCUS_OUT, event_handle_focusout);
EVENT(XCB_KEY_PRESS, event_handle_key);
EVENT(XCB_KEY_RELEASE, event_handle_key);
EVENT(XCB_LEAVE_NOTIFY, event_handle_enterleavenotify);
EVENT(XCB_MAPPING_NOTIFY, event_handle_mappingnotify);
EVENT(XCB_MAP_REQUEST, event_handle_maprequest);
EVENT(XCB_MOTION_NOTIFY, event_handle_motionnotify);
EVENT(XCB_PROPERTY_NOTIFY, property_handle_propertynotify);
EVENT(XCB_REPARENT_NOTIFY, event_handle_reparentnotify);
EVENT(XCB_UNMAP_NOTIFY, event_handle_unmapnotify);
#undef EVENT
}
static uint8_t randr_screen_change_notify = 0;
if(randr_screen_change_notify == 0)
{
/* check for randr extension */
const xcb_query_extension_reply_t *randr_query;
randr_query = xcb_get_extension_data(_G_connection, &xcb_randr_id);
if(randr_query->present)
randr_screen_change_notify = randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY;
}
if (response_type == randr_screen_change_notify)
event_handle_randr_screen_change_notify((void *) event);
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80