hooks: add exit hook
[awesome.git] / event.c
blob0b44f5d26b1ad2c7d13a32ac9ac94acb1860b6b9
1 /*
2 * event.c - event handlers
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <xcb/xcb.h>
23 #include <xcb/randr.h>
24 #include <xcb/xcb_atom.h>
25 #include <xcb/xcb_icccm.h>
26 #include <xcb/xcb_event.h>
28 #include "awesome.h"
29 #include "event.h"
30 #include "tag.h"
31 #include "window.h"
32 #include "ewmh.h"
33 #include "client.h"
34 #include "widget.h"
35 #include "titlebar.h"
36 #include "key.h"
37 #include "keygrabber.h"
38 #include "mousegrabber.h"
39 #include "luaa.h"
40 #include "systray.h"
41 #include "screen.h"
42 #include "common/atoms.h"
43 #include "common/xutil.h"
45 #define DO_EVENT_HOOK_CALLBACK(xcbtype, xcbeventprefix, type, arraytype, match) \
46 static void \
47 event_##xcbtype##_callback(xcb_##xcbtype##_press_event_t *ev, \
48 arraytype *arr, \
49 int nargs, \
50 void *data) \
51 { \
52 foreach(item, *arr) \
53 if(match(ev, *item, data)) \
54 switch(ev->response_type) \
55 { \
56 case xcbeventprefix##_PRESS: \
57 if((*item)->press != LUA_REFNIL) \
58 { \
59 for(int i = 0; i < nargs; i++) \
60 lua_pushvalue(globalconf.L, - nargs); \
61 luaA_dofunction(globalconf.L, (*item)->press, nargs, 0); \
62 } \
63 break; \
64 case xcbeventprefix##_RELEASE: \
65 if((*item)->release != LUA_REFNIL) \
66 { \
67 for(int i = 0; i < nargs; i++) \
68 lua_pushvalue(globalconf.L, - nargs); \
69 luaA_dofunction(globalconf.L, (*item)->release, nargs, 0); \
70 } \
71 break; \
72 } \
73 lua_pop(globalconf.L, nargs); \
76 static bool
77 event_button_match(xcb_button_press_event_t *ev, button_t *b, void *data)
79 return ((!b->button || ev->detail == b->button)
80 && (b->mod == XCB_BUTTON_MASK_ANY || b->mod == ev->state));
83 static bool
84 event_key_match(xcb_key_press_event_t *ev, keyb_t *k, void *data)
86 assert(data);
87 xcb_keysym_t keysym = *(xcb_keysym_t *) data;
88 return (((k->keycode && ev->detail == k->keycode)
89 || (k->keysym && keysym == k->keysym))
90 && (k->mod == XCB_BUTTON_MASK_ANY || k->mod == ev->state));
93 DO_EVENT_HOOK_CALLBACK(button, XCB_BUTTON, button_t, button_array_t, event_button_match)
94 DO_EVENT_HOOK_CALLBACK(key, XCB_KEY, keyb_t, key_array_t, event_key_match)
96 /** Handle an event with mouse grabber if needed
97 * \param x The x coordinate.
98 * \param y The y coordinate.
99 * \param mask The mask buttons.
100 * \return True if the event was handled.
102 static bool
103 event_handle_mousegrabber(int x, int y, uint16_t mask)
105 if(globalconf.mousegrabber != LUA_REFNIL)
107 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
108 mousegrabber_handleevent(globalconf.L, x, y, mask);
109 if(lua_pcall(globalconf.L, 1, 1, 0))
111 warn("error running function: %s", lua_tostring(globalconf.L, -1));
112 luaA_mousegrabber_stop(globalconf.L);
114 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
115 luaA_mousegrabber_stop(globalconf.L);
116 lua_pop(globalconf.L, 1); /* pop returned value */
117 return true;
119 return false;
122 /** The button press event handler.
123 * \param data The type of mouse event.
124 * \param connection The connection to the X server.
125 * \param ev The event.
127 static int
128 event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev)
130 int screen;
131 const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection));
132 client_t *c;
133 wibox_t *wibox;
135 if(event_handle_mousegrabber(ev->root_x, ev->root_y, 1 << (ev->detail - 1 + 8)))
136 return 0;
138 /* ev->state is
139 * button status (8 bits) + modifiers status (8 bits)
140 * we don't care for button status that we get, especially on release, so
141 * drop them */
142 ev->state &= 0x00ff;
144 if((wibox = wibox_getbywin(ev->event))
145 || (wibox = wibox_getbywin(ev->child)))
147 /* If the wibox is child, then x,y are
148 * relative to root window */
149 if(wibox->sw.window == ev->child)
151 ev->event_x -= wibox->sw.geometry.x;
152 ev->event_y -= wibox->sw.geometry.y;
155 wibox_push(globalconf.L, wibox);
156 event_button_callback(ev, &wibox->buttons, 1, NULL);
158 /* then try to match a widget binding */
159 widget_t *w = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
160 wibox->sw.geometry.width,
161 wibox->sw.geometry.height,
162 &ev->event_x, &ev->event_y);
163 if(w)
165 widget_push(globalconf.L, w);
166 wibox_push(globalconf.L, wibox);
167 event_button_callback(ev, &w->buttons, 2, NULL);
170 else if((c = client_getbywin(ev->event)))
172 client_push(globalconf.L, c);
173 event_button_callback(ev, &c->buttons, 1, NULL);
174 xcb_allow_events(globalconf.connection,
175 XCB_ALLOW_REPLAY_POINTER,
176 XCB_CURRENT_TIME);
178 else if(ev->child == XCB_NONE)
179 for(screen = 0; screen < nb_screen; screen++)
180 if(xutil_screen_get(connection, screen)->root == ev->event)
182 event_button_callback(ev, &globalconf.buttons, 0, NULL);
183 return 0;
186 return 0;
189 static void
190 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
192 uint16_t config_win_mask = 0;
193 uint32_t config_win_vals[7];
194 unsigned short i = 0;
196 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
198 config_win_mask |= XCB_CONFIG_WINDOW_X;
199 config_win_vals[i++] = ev->x;
201 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
203 config_win_mask |= XCB_CONFIG_WINDOW_Y;
204 config_win_vals[i++] = ev->y;
206 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
208 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
209 config_win_vals[i++] = ev->width;
211 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
213 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
214 config_win_vals[i++] = ev->height;
216 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
218 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
219 config_win_vals[i++] = ev->border_width;
221 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
223 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
224 config_win_vals[i++] = ev->sibling;
226 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
228 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
229 config_win_vals[i++] = ev->stack_mode;
232 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
235 /** The configure event handler.
236 * \param data currently unused.
237 * \param connection The connection to the X server.
238 * \param ev The event.
240 static int
241 event_handle_configurerequest(void *data __attribute__ ((unused)),
242 xcb_connection_t *connection, xcb_configure_request_event_t *ev)
244 client_t *c;
245 area_t geometry;
247 if((c = client_getbywin(ev->window)))
249 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
251 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
252 geometry.x = ev->x;
253 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
254 geometry.y = ev->y;
255 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
256 geometry.width = ev->width;
257 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
258 geometry.height = ev->height;
260 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
261 client_setborder(c, ev->border_width);
263 /* Clients are not allowed to directly mess with stacking parameters. */
264 ev->value_mask &= ~(XCB_CONFIG_WINDOW_SIBLING |
265 XCB_CONFIG_WINDOW_STACK_MODE);
267 if(c->isbanned)
269 /* We'll be sending protocol geometry, so don't readd borders and titlebar. */
270 /* We do have to ensure the windows don't end up in the visible screen. */
271 ev->x = geometry.x = - (geometry.width + 2*c->border);
272 ev->y = geometry.y = - (geometry.height + 2*c->border);
273 window_configure(c->win, geometry, c->border);
274 event_handle_configurerequest_configure_window(ev);
276 else
278 /** Configure request are sent with size relative to real (internal)
279 * window size, i.e. without titlebars and borders. */
280 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
282 if(client_resize(c, geometry, false))
283 client_need_arrange(c);
284 else
286 /* Resize wasn't officially needed, but we don't want to break expectations. */
287 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
288 window_configure(c->win, geometry, c->border);
292 else
293 event_handle_configurerequest_configure_window(ev);
295 return 0;
298 /** The configure notify event handler.
299 * \param data currently unused.
300 * \param connection The connection to the X server.
301 * \param ev The event.
303 static int
304 event_handle_configurenotify(void *data __attribute__ ((unused)),
305 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
307 int screen_nbr;
308 const xcb_screen_t *screen;
310 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
311 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
312 && ev->window == screen->root
313 && (ev->width != screen->width_in_pixels
314 || ev->height != screen->height_in_pixels))
315 /* it's not that we panic, but restart */
316 awesome_restart();
318 return 0;
321 /** The destroy notify event handler.
322 * \param data currently unused.
323 * \param connection The connection to the X server.
324 * \param ev The event.
326 static int
327 event_handle_destroynotify(void *data __attribute__ ((unused)),
328 xcb_connection_t *connection __attribute__ ((unused)),
329 xcb_destroy_notify_event_t *ev)
331 client_t *c;
333 if((c = client_getbywin(ev->window)))
334 client_unmanage(c);
335 else
336 for(int i = 0; i < globalconf.embedded.len; i++)
337 if(globalconf.embedded.tab[i].win == ev->window)
339 xembed_window_array_take(&globalconf.embedded, i);
340 widget_invalidate_bytype(widget_systray);
343 return 0;
346 /** Handle a motion notify over widgets.
347 * \param object The object.
348 * \param mouse_over The pointer to the registered mouse over widget.
349 * \param widget The new widget the mouse is over.
351 static void
352 event_handle_widget_motionnotify(void *object,
353 widget_t **mouse_over,
354 widget_t *widget)
356 if(widget != *mouse_over)
358 if(*mouse_over)
360 if((*mouse_over)->mouse_leave != LUA_REFNIL)
362 /* call mouse leave function on old widget */
363 widget_push(globalconf.L, *mouse_over);
364 luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
367 if(widget)
369 /* call mouse enter function on new widget and register it */
370 *mouse_over = widget;
371 if(widget->mouse_enter != LUA_REFNIL)
373 widget_push(globalconf.L, widget);
374 luaA_dofunction(globalconf.L, widget->mouse_enter, 1, 0);
380 /** The motion notify event handler.
381 * \param data currently unused.
382 * \param connection The connection to the X server.
383 * \param ev The event.
385 static int
386 event_handle_motionnotify(void *data __attribute__ ((unused)),
387 xcb_connection_t *connection,
388 xcb_motion_notify_event_t *ev)
390 wibox_t *wibox;
392 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
393 return 0;
395 if((wibox = wibox_getbywin(ev->event)))
397 widget_t *w = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
398 wibox->sw.geometry.width,
399 wibox->sw.geometry.height,
400 &ev->event_x, &ev->event_y);
401 if(w)
402 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
405 return 0;
408 /** The leave notify event handler.
409 * \param data currently unused.
410 * \param connection The connection to the X server.
411 * \param ev The event.
413 static int
414 event_handle_leavenotify(void *data __attribute__ ((unused)),
415 xcb_connection_t *connection,
416 xcb_leave_notify_event_t *ev)
418 wibox_t *wibox;
419 client_t *c;
421 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
422 return 0;
424 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
425 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
427 client_push(globalconf.L, c);
428 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
431 if((wibox = wibox_getbywin(ev->event)))
433 if(wibox->mouse_over)
435 if(wibox->mouse_over->mouse_leave != LUA_REFNIL)
437 /* call mouse leave function on widget the mouse was over */
438 wibox_push(globalconf.L, wibox);
439 luaA_dofunction(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
441 wibox->mouse_over = NULL;
444 if(wibox->mouse_leave != LUA_REFNIL)
445 luaA_dofunction(globalconf.L, wibox->mouse_leave, 0, 0);
448 return 0;
451 /** The enter notify event handler.
452 * \param data currently unused.
453 * \param connection The connection to the X server.
454 * \param ev The event.
456 static int
457 event_handle_enternotify(void *data __attribute__ ((unused)),
458 xcb_connection_t *connection,
459 xcb_enter_notify_event_t *ev)
461 client_t *c;
462 wibox_t *wibox;
464 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
465 return 0;
467 if((wibox = wibox_getbywin(ev->event)))
469 widget_t *w = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
470 wibox->sw.geometry.width,
471 wibox->sw.geometry.height,
472 &ev->event_x, &ev->event_y);
473 if(w)
474 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
476 if(wibox->mouse_enter != LUA_REFNIL)
477 luaA_dofunction(globalconf.L, wibox->mouse_enter, 0, 0);
480 if((c = client_getbytitlebarwin(ev->event))
481 || (c = client_getbywin(ev->event)))
482 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
484 client_push(globalconf.L, c);
485 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
488 return 0;
491 /** The focus in event handler.
492 * \param data currently unused.
493 * \param connection The connection to the X server.
494 * \param ev The event.
496 static int
497 event_handle_focusin(void *data __attribute__ ((unused)),
498 xcb_connection_t *connection,
499 xcb_focus_in_event_t *ev)
501 client_t *c;
503 /* Events that we are interested in: */
504 switch(ev->detail)
506 /* These are events that jump between root windows.
508 case XCB_NOTIFY_DETAIL_ANCESTOR:
509 case XCB_NOTIFY_DETAIL_INFERIOR:
511 /* These are events that jump between clients.
512 * Virtual events ensure we always get an event on our top-level window.
514 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
515 case XCB_NOTIFY_DETAIL_NONLINEAR:
516 if((c = client_getbytitlebarwin(ev->event))
517 || (c = client_getbywin(ev->event)))
518 client_focus_update(c);
519 /* all other events are ignored */
520 default:
521 break;
523 return 0;
526 /** The expose event handler.
527 * \param data currently unused.
528 * \param connection The connection to the X server.
529 * \param ev The event.
531 static int
532 event_handle_expose(void *data __attribute__ ((unused)),
533 xcb_connection_t *connection __attribute__ ((unused)),
534 xcb_expose_event_t *ev)
536 wibox_t *wibox;
538 if((wibox = wibox_getbywin(ev->window)))
539 simplewindow_refresh_pixmap_partial(&wibox->sw,
540 ev->x, ev->y,
541 ev->width, ev->height);
543 return 0;
546 /** The key press event handler.
547 * \param data currently unused.
548 * \param connection The connection to the X server.
549 * \param ev The event.
551 static int
552 event_handle_key(void *data __attribute__ ((unused)),
553 xcb_connection_t *connection __attribute__ ((unused)),
554 xcb_key_press_event_t *ev)
556 if(globalconf.keygrabber != LUA_REFNIL)
558 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
559 if(keygrabber_handlekpress(globalconf.L, ev))
561 if(lua_pcall(globalconf.L, 3, 1, 0))
563 warn("error running function: %s", lua_tostring(globalconf.L, -1));
564 luaA_keygrabber_stop(globalconf.L);
566 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
567 luaA_keygrabber_stop(globalconf.L);
569 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
571 else
573 /* get keysym ignoring all modifiers */
574 xcb_keysym_t keysym = key_getkeysym(ev->detail, 0);
575 client_t *c;
576 if((c = client_getbywin(ev->event)))
578 client_push(globalconf.L, c);
579 event_key_callback(ev, &c->keys, 1, &keysym);
581 else
582 event_key_callback(ev, &globalconf.keys, 0, &keysym);
585 return 0;
588 /** The map request event handler.
589 * \param data currently unused.
590 * \param connection The connection to the X server.
591 * \param ev The event.
593 static int
594 event_handle_maprequest(void *data __attribute__ ((unused)),
595 xcb_connection_t *connection, xcb_map_request_event_t *ev)
597 int phys_screen, ret = 0;
598 client_t *c;
599 xcb_get_window_attributes_cookie_t wa_c;
600 xcb_get_window_attributes_reply_t *wa_r;
601 xcb_get_geometry_cookie_t geom_c;
602 xcb_get_geometry_reply_t *geom_r;
604 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
606 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
607 return -1;
609 if(wa_r->override_redirect)
610 goto bailout;
612 if(xembed_getbywin(&globalconf.embedded, ev->window))
614 xcb_map_window(connection, ev->window);
615 xembed_window_activate(connection, ev->window);
617 else if((c = client_getbywin(ev->window)))
619 /* Check that it may be visible, but not asked to be hidden */
620 if(client_maybevisible(c, c->screen) && !c->ishidden)
622 client_setminimized(c, false);
623 /* it will be raised, so just update ourself */
624 client_raise(c);
627 else
629 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
631 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
633 ret = -1;
634 goto bailout;
637 phys_screen = xutil_root2screen(connection, geom_r->root);
639 client_manage(ev->window, geom_r, phys_screen, false);
641 p_delete(&geom_r);
644 bailout:
645 p_delete(&wa_r);
646 return ret;
649 /** The unmap notify event handler.
650 * \param data currently unused.
651 * \param connection The connection to the X server.
652 * \param ev The event.
654 static int
655 event_handle_unmapnotify(void *data __attribute__ ((unused)),
656 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
658 client_t *c;
660 if((c = client_getbywin(ev->window)))
662 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
663 && XCB_EVENT_SENT(ev)
664 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
665 client_unmanage(c);
667 else
668 for(int i = 0; i < globalconf.embedded.len; i++)
669 if(globalconf.embedded.tab[i].win == ev->window)
671 xembed_window_array_take(&globalconf.embedded, i);
672 widget_invalidate_bytype(widget_systray);
675 return 0;
678 /** The randr screen change notify event handler.
679 * \param data currently unused.
680 * \param connection The connection to the X server.
681 * \param ev The event.
683 static int
684 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
685 xcb_connection_t *connection __attribute__ ((unused)),
686 xcb_randr_screen_change_notify_event_t *ev)
688 if(!globalconf.have_randr)
689 return -1;
691 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
692 * (only the code relevant to RRScreenChangeNotify) as the latter
693 * doesn't provide this kind of function */
694 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
695 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
696 ev->mheight, ev->mwidth);
697 else
698 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
699 ev->mwidth, ev->mheight);
701 /* XRRUpdateConfiguration also executes the following instruction
702 * but it's not useful because SubpixelOrder is not used at all at
703 * the moment
705 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
708 awesome_restart();
710 return 0;
713 /** The client message event handler.
714 * \param data currently unused.
715 * \param connection The connection to the X server.
716 * \param ev The event.
718 static int
719 event_handle_clientmessage(void *data __attribute__ ((unused)),
720 xcb_connection_t *connection,
721 xcb_client_message_event_t *ev)
723 /* check for startup notification messages */
724 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
725 return 0;
727 if(ev->type == WM_CHANGE_STATE)
729 client_t *c;
730 if((c = client_getbywin(ev->window))
731 && ev->format == 32
732 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
733 client_setminimized(c, true);
735 else if(ev->type == _XEMBED)
736 return xembed_process_client_message(ev);
737 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
738 return systray_process_client_message(ev);
739 return ewmh_process_client_message(ev);
742 /** The keymap change notify event handler.
743 * \param data Unused data.
744 * \param connection The connection to the X server.
745 * \param ev The event.
746 * \return Status code, 0 if everything's fine.
748 static int
749 event_handle_mappingnotify(void *data,
750 xcb_connection_t *connection,
751 xcb_mapping_notify_event_t *ev)
753 if(ev->request == XCB_MAPPING_MODIFIER
754 || ev->request == XCB_MAPPING_KEYBOARD)
756 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
757 xcb_get_modifier_mapping_unchecked(globalconf.connection);
759 /* Free and then allocate the key symbols */
760 xcb_key_symbols_free(globalconf.keysyms);
761 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
763 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
764 globalconf.keysyms, &globalconf.numlockmask,
765 &globalconf.shiftlockmask, &globalconf.capslockmask,
766 &globalconf.modeswitchmask);
768 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
770 /* regrab everything */
771 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
773 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
774 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
775 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
776 window_grabkeys(s->root, &globalconf.keys);
779 foreach(_c, globalconf.clients)
781 client_t *c = *_c;
782 xcb_ungrab_key(connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
783 window_grabkeys(c->win, &c->keys);
787 return 0;
790 static int
791 event_handle_reparentnotify(void *data,
792 xcb_connection_t *connection,
793 xcb_reparent_notify_event_t *ev)
795 client_t *c;
797 if((c = client_getbywin(ev->window)))
798 client_unmanage(c);
800 return 0;
803 void a_xcb_set_event_handlers(void)
805 const xcb_query_extension_reply_t *randr_query;
807 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
808 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
809 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
810 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
811 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
812 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
813 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
814 xcb_event_set_focus_in_handler(&globalconf.evenths, event_handle_focusin, NULL);
815 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
816 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
817 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
818 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
819 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
820 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
821 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
822 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
823 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
825 /* check for randr extension */
826 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
827 if((globalconf.have_randr = randr_query->present))
828 xcb_event_set_handler(&globalconf.evenths,
829 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
830 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
831 NULL);
835 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80