Use a libev prepare watcher for calling awesome_refresh()
[awesome.git] / event.c
blob7dc3a1747403f4ba48b63795558c314a53064c43
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_from_registry(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_from_registry(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 /** Configure request are sent with size relative to real (internal)
268 * window size, i.e. without titlebars and borders. */
269 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
271 if(!client_resize(c, geometry, false))
273 /* Resize wasn't officially needed, but we don't want to break expectations. */
274 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
275 window_configure(c->win, geometry, c->border);
278 else
279 event_handle_configurerequest_configure_window(ev);
281 return 0;
284 /** The configure notify event handler.
285 * \param data currently unused.
286 * \param connection The connection to the X server.
287 * \param ev The event.
289 static int
290 event_handle_configurenotify(void *data __attribute__ ((unused)),
291 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
293 int screen_nbr;
294 const xcb_screen_t *screen;
296 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
297 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
298 && ev->window == screen->root
299 && (ev->width != screen->width_in_pixels
300 || ev->height != screen->height_in_pixels))
301 /* it's not that we panic, but restart */
302 awesome_restart();
304 return 0;
307 /** The destroy notify event handler.
308 * \param data currently unused.
309 * \param connection The connection to the X server.
310 * \param ev The event.
312 static int
313 event_handle_destroynotify(void *data __attribute__ ((unused)),
314 xcb_connection_t *connection __attribute__ ((unused)),
315 xcb_destroy_notify_event_t *ev)
317 client_t *c;
319 if((c = client_getbywin(ev->window)))
320 client_unmanage(c);
321 else
322 for(int i = 0; i < globalconf.embedded.len; i++)
323 if(globalconf.embedded.tab[i].win == ev->window)
325 xembed_window_array_take(&globalconf.embedded, i);
326 widget_invalidate_bytype(widget_systray);
329 return 0;
332 /** Handle a motion notify over widgets.
333 * \param object The object.
334 * \param mouse_over The pointer to the registered mouse over widget.
335 * \param widget The new widget the mouse is over.
337 static void
338 event_handle_widget_motionnotify(void *object,
339 widget_t **mouse_over,
340 widget_t *widget)
342 if(widget != *mouse_over)
344 if(*mouse_over)
346 if((*mouse_over)->mouse_leave != LUA_REFNIL)
348 /* call mouse leave function on old widget */
349 widget_push(globalconf.L, *mouse_over);
350 luaA_dofunction_from_registry(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
353 if(widget)
355 /* call mouse enter function on new widget and register it */
356 *mouse_over = widget;
357 if(widget->mouse_enter != LUA_REFNIL)
359 widget_push(globalconf.L, widget);
360 luaA_dofunction_from_registry(globalconf.L, widget->mouse_enter, 1, 0);
366 /** The motion notify event handler.
367 * \param data currently unused.
368 * \param connection The connection to the X server.
369 * \param ev The event.
371 static int
372 event_handle_motionnotify(void *data __attribute__ ((unused)),
373 xcb_connection_t *connection,
374 xcb_motion_notify_event_t *ev)
376 wibox_t *wibox;
378 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
379 return 0;
381 if((wibox = wibox_getbywin(ev->event)))
383 widget_t *w = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
384 wibox->sw.geometry.width,
385 wibox->sw.geometry.height,
386 &ev->event_x, &ev->event_y);
387 if(w)
388 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
391 return 0;
394 /** The leave notify event handler.
395 * \param data currently unused.
396 * \param connection The connection to the X server.
397 * \param ev The event.
399 static int
400 event_handle_leavenotify(void *data __attribute__ ((unused)),
401 xcb_connection_t *connection,
402 xcb_leave_notify_event_t *ev)
404 wibox_t *wibox;
405 client_t *c;
407 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
408 return 0;
410 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
411 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
413 client_push(globalconf.L, c);
414 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
417 if((wibox = wibox_getbywin(ev->event)))
419 if(wibox->mouse_over)
421 if(wibox->mouse_over->mouse_leave != LUA_REFNIL)
423 /* call mouse leave function on widget the mouse was over */
424 wibox_push(globalconf.L, wibox);
425 luaA_dofunction_from_registry(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
427 wibox->mouse_over = NULL;
430 if(wibox->mouse_leave != LUA_REFNIL)
431 luaA_dofunction_from_registry(globalconf.L, wibox->mouse_leave, 0, 0);
434 return 0;
437 /** The enter notify event handler.
438 * \param data currently unused.
439 * \param connection The connection to the X server.
440 * \param ev The event.
442 static int
443 event_handle_enternotify(void *data __attribute__ ((unused)),
444 xcb_connection_t *connection,
445 xcb_enter_notify_event_t *ev)
447 client_t *c;
448 wibox_t *wibox;
450 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
451 return 0;
453 if((wibox = wibox_getbywin(ev->event)))
455 widget_t *w = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
456 wibox->sw.geometry.width,
457 wibox->sw.geometry.height,
458 &ev->event_x, &ev->event_y);
459 if(w)
460 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
462 if(wibox->mouse_enter != LUA_REFNIL)
463 luaA_dofunction_from_registry(globalconf.L, wibox->mouse_enter, 0, 0);
466 if((c = client_getbytitlebarwin(ev->event))
467 || (c = client_getbywin(ev->event)))
468 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
470 client_push(globalconf.L, c);
471 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
474 return 0;
477 /** The focus in event handler.
478 * \param data currently unused.
479 * \param connection The connection to the X server.
480 * \param ev The event.
482 static int
483 event_handle_focusin(void *data __attribute__ ((unused)),
484 xcb_connection_t *connection,
485 xcb_focus_in_event_t *ev)
487 client_t *c;
489 /* Events that we are interested in: */
490 switch(ev->detail)
492 /* These are events that jump between root windows.
494 case XCB_NOTIFY_DETAIL_ANCESTOR:
495 case XCB_NOTIFY_DETAIL_INFERIOR:
497 /* These are events that jump between clients.
498 * Virtual events ensure we always get an event on our top-level window.
500 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
501 case XCB_NOTIFY_DETAIL_NONLINEAR:
502 if((c = client_getbytitlebarwin(ev->event))
503 || (c = client_getbywin(ev->event)))
504 client_focus_update(c);
505 /* all other events are ignored */
506 default:
507 break;
509 return 0;
512 /** The expose event handler.
513 * \param data currently unused.
514 * \param connection The connection to the X server.
515 * \param ev The event.
517 static int
518 event_handle_expose(void *data __attribute__ ((unused)),
519 xcb_connection_t *connection __attribute__ ((unused)),
520 xcb_expose_event_t *ev)
522 wibox_t *wibox;
524 if((wibox = wibox_getbywin(ev->window)))
525 simplewindow_refresh_pixmap_partial(&wibox->sw,
526 ev->x, ev->y,
527 ev->width, ev->height);
529 return 0;
532 /** The key press event handler.
533 * \param data currently unused.
534 * \param connection The connection to the X server.
535 * \param ev The event.
537 static int
538 event_handle_key(void *data __attribute__ ((unused)),
539 xcb_connection_t *connection __attribute__ ((unused)),
540 xcb_key_press_event_t *ev)
542 if(globalconf.keygrabber != LUA_REFNIL)
544 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
545 if(keygrabber_handlekpress(globalconf.L, ev))
547 if(lua_pcall(globalconf.L, 3, 1, 0))
549 warn("error running function: %s", lua_tostring(globalconf.L, -1));
550 luaA_keygrabber_stop(globalconf.L);
552 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
553 luaA_keygrabber_stop(globalconf.L);
555 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
557 else
559 /* get keysym ignoring all modifiers */
560 xcb_keysym_t keysym = key_getkeysym(ev->detail, 0);
561 client_t *c;
562 if((c = client_getbywin(ev->event)))
564 client_push(globalconf.L, c);
565 event_key_callback(ev, &c->keys, 1, &keysym);
567 else
568 event_key_callback(ev, &globalconf.keys, 0, &keysym);
571 return 0;
574 /** The map request event handler.
575 * \param data currently unused.
576 * \param connection The connection to the X server.
577 * \param ev The event.
579 static int
580 event_handle_maprequest(void *data __attribute__ ((unused)),
581 xcb_connection_t *connection, xcb_map_request_event_t *ev)
583 int phys_screen, ret = 0;
584 client_t *c;
585 xcb_get_window_attributes_cookie_t wa_c;
586 xcb_get_window_attributes_reply_t *wa_r;
587 xcb_get_geometry_cookie_t geom_c;
588 xcb_get_geometry_reply_t *geom_r;
590 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
592 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
593 return -1;
595 if(wa_r->override_redirect)
596 goto bailout;
598 if(xembed_getbywin(&globalconf.embedded, ev->window))
600 xcb_map_window(connection, ev->window);
601 xembed_window_activate(connection, ev->window);
603 else if((c = client_getbywin(ev->window)))
605 /* Check that it may be visible, but not asked to be hidden */
606 if(client_maybevisible(c, c->screen) && !c->ishidden)
608 client_setminimized(c, false);
609 /* it will be raised, so just update ourself */
610 client_raise(c);
613 else
615 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
617 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
619 ret = -1;
620 goto bailout;
623 phys_screen = xutil_root2screen(connection, geom_r->root);
625 client_manage(ev->window, geom_r, phys_screen, false);
627 p_delete(&geom_r);
630 bailout:
631 p_delete(&wa_r);
632 return ret;
635 /** The unmap notify event handler.
636 * \param data currently unused.
637 * \param connection The connection to the X server.
638 * \param ev The event.
640 static int
641 event_handle_unmapnotify(void *data __attribute__ ((unused)),
642 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
644 client_t *c;
646 if((c = client_getbywin(ev->window)))
648 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
649 && XCB_EVENT_SENT(ev)
650 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
651 client_unmanage(c);
653 else
654 for(int i = 0; i < globalconf.embedded.len; i++)
655 if(globalconf.embedded.tab[i].win == ev->window)
657 xembed_window_array_take(&globalconf.embedded, i);
658 widget_invalidate_bytype(widget_systray);
661 return 0;
664 /** The randr screen change notify event handler.
665 * \param data currently unused.
666 * \param connection The connection to the X server.
667 * \param ev The event.
669 static int
670 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
671 xcb_connection_t *connection __attribute__ ((unused)),
672 xcb_randr_screen_change_notify_event_t *ev)
674 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
675 * (only the code relevant to RRScreenChangeNotify) as the latter
676 * doesn't provide this kind of function */
677 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
678 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
679 ev->mheight, ev->mwidth);
680 else
681 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
682 ev->mwidth, ev->mheight);
684 /* XRRUpdateConfiguration also executes the following instruction
685 * but it's not useful because SubpixelOrder is not used at all at
686 * the moment
688 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
691 awesome_restart();
693 return 0;
696 /** The client message event handler.
697 * \param data currently unused.
698 * \param connection The connection to the X server.
699 * \param ev The event.
701 static int
702 event_handle_clientmessage(void *data __attribute__ ((unused)),
703 xcb_connection_t *connection,
704 xcb_client_message_event_t *ev)
706 /* check for startup notification messages */
707 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
708 return 0;
710 if(ev->type == WM_CHANGE_STATE)
712 client_t *c;
713 if((c = client_getbywin(ev->window))
714 && ev->format == 32
715 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
716 client_setminimized(c, true);
718 else if(ev->type == _XEMBED)
719 return xembed_process_client_message(ev);
720 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
721 return systray_process_client_message(ev);
722 return ewmh_process_client_message(ev);
725 /** The keymap change notify event handler.
726 * \param data Unused data.
727 * \param connection The connection to the X server.
728 * \param ev The event.
729 * \return Status code, 0 if everything's fine.
731 static int
732 event_handle_mappingnotify(void *data,
733 xcb_connection_t *connection,
734 xcb_mapping_notify_event_t *ev)
736 if(ev->request == XCB_MAPPING_MODIFIER
737 || ev->request == XCB_MAPPING_KEYBOARD)
739 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
740 xcb_get_modifier_mapping_unchecked(globalconf.connection);
742 /* Free and then allocate the key symbols */
743 xcb_key_symbols_free(globalconf.keysyms);
744 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
746 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
747 globalconf.keysyms, &globalconf.numlockmask,
748 &globalconf.shiftlockmask, &globalconf.capslockmask,
749 &globalconf.modeswitchmask);
751 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
753 /* regrab everything */
754 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
756 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
757 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
758 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
759 window_grabkeys(s->root, &globalconf.keys);
762 foreach(_c, globalconf.clients)
764 client_t *c = *_c;
765 xcb_ungrab_key(connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
766 window_grabkeys(c->win, &c->keys);
770 return 0;
773 static int
774 event_handle_reparentnotify(void *data,
775 xcb_connection_t *connection,
776 xcb_reparent_notify_event_t *ev)
778 client_t *c;
780 if((c = client_getbywin(ev->window)))
781 client_unmanage(c);
783 return 0;
786 void a_xcb_set_event_handlers(void)
788 const xcb_query_extension_reply_t *randr_query;
790 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
791 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
792 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
793 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
794 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
795 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
796 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
797 xcb_event_set_focus_in_handler(&globalconf.evenths, event_handle_focusin, NULL);
798 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
799 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
800 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
801 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
802 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
803 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
804 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
805 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
806 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
808 /* check for randr extension */
809 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
810 if(randr_query->present)
811 xcb_event_set_handler(&globalconf.evenths,
812 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
813 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
814 NULL);
818 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80