awful.widget.taglist: use attached_add_signal
[awesome.git] / event.c
blob629b3286af7434c013134f26d76376a9edccd178
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(type, xcbtype, xcbeventprefix, arraytype, match) \
46 static void \
47 event_##xcbtype##_callback(xcb_##xcbtype##_press_event_t *ev, \
48 arraytype *arr, \
49 int oud, \
50 int nargs, \
51 void *data) \
52 { \
53 int abs_oud = oud < 0 ? ((lua_gettop(globalconf.L) + 1) + oud) : oud; \
54 int item_matching = 0; \
55 foreach(item, *arr) \
56 if(match(ev, *item, data)) \
57 { \
58 if(oud) \
59 luaA_object_push_item(globalconf.L, abs_oud, *item); \
60 else \
61 luaA_object_push(globalconf.L, *item); \
62 item_matching++; \
63 } \
64 for(; item_matching > 0; item_matching--) \
65 { \
66 switch(ev->response_type) \
67 { \
68 case xcbeventprefix##_PRESS: \
69 for(int i = 0; i < nargs; i++) \
70 lua_pushvalue(globalconf.L, - nargs - item_matching); \
71 luaA_object_emit_signal(globalconf.L, - nargs - 1, "press", nargs); \
72 break; \
73 case xcbeventprefix##_RELEASE: \
74 for(int i = 0; i < nargs; i++) \
75 lua_pushvalue(globalconf.L, - nargs - item_matching); \
76 luaA_object_emit_signal(globalconf.L, - nargs - 1, "release", nargs); \
77 break; \
78 } \
79 lua_pop(globalconf.L, 1); \
80 } \
81 lua_pop(globalconf.L, nargs); \
84 static bool
85 event_key_match(xcb_key_press_event_t *ev, keyb_t *k, void *data)
87 assert(data);
88 xcb_keysym_t keysym = *(xcb_keysym_t *) data;
89 return (((k->keycode && ev->detail == k->keycode)
90 || (k->keysym && keysym == k->keysym))
91 && (k->modifiers == XCB_BUTTON_MASK_ANY || k->modifiers == ev->state));
94 static bool
95 event_button_match(xcb_button_press_event_t *ev, button_t *b, void *data)
97 return ((!b->button || ev->detail == b->button)
98 && (b->modifiers == XCB_BUTTON_MASK_ANY || b->modifiers == ev->state));
101 DO_EVENT_HOOK_CALLBACK(button_t, button, XCB_BUTTON, button_array_t, event_button_match)
102 DO_EVENT_HOOK_CALLBACK(keyb_t, key, XCB_KEY, key_array_t, event_key_match)
104 /** Handle an event with mouse grabber if needed
105 * \param x The x coordinate.
106 * \param y The y coordinate.
107 * \param mask The mask buttons.
108 * \return True if the event was handled.
110 static bool
111 event_handle_mousegrabber(int x, int y, uint16_t mask)
113 if(globalconf.mousegrabber != LUA_REFNIL)
115 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
116 mousegrabber_handleevent(globalconf.L, x, y, mask);
117 if(lua_pcall(globalconf.L, 1, 1, 0))
119 warn("error running function: %s", lua_tostring(globalconf.L, -1));
120 luaA_mousegrabber_stop(globalconf.L);
122 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
123 luaA_mousegrabber_stop(globalconf.L);
124 lua_pop(globalconf.L, 1); /* pop returned value */
125 return true;
127 return false;
130 /** The button press event handler.
131 * \param data The type of mouse event.
132 * \param connection The connection to the X server.
133 * \param ev The event.
135 static int
136 event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev)
138 int screen;
139 const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection));
140 client_t *c;
141 wibox_t *wibox;
143 if(event_handle_mousegrabber(ev->root_x, ev->root_y, 1 << (ev->detail - 1 + 8)))
144 return 0;
146 /* ev->state is
147 * button status (8 bits) + modifiers status (8 bits)
148 * we don't care for button status that we get, especially on release, so
149 * drop them */
150 ev->state &= 0x00ff;
152 if((wibox = wibox_getbywin(ev->event))
153 || (wibox = wibox_getbywin(ev->child)))
155 /* If the wibox is child, then x,y are
156 * relative to root window */
157 if(wibox->window == ev->child)
159 ev->event_x -= wibox->geometry.x;
160 ev->event_y -= wibox->geometry.y;
163 luaA_object_push(globalconf.L, wibox);
164 event_button_callback(ev, &wibox->buttons, -1, 1, NULL);
166 /* then try to match a widget binding */
167 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
168 wibox->geometry.width,
169 wibox->geometry.height,
170 &ev->event_x, &ev->event_y);
171 if(w)
173 luaA_object_push(globalconf.L, w);
174 luaA_object_push(globalconf.L, wibox);
175 event_button_callback(ev, &w->buttons, -2, 2, NULL);
178 else if((c = client_getbywin(ev->event)))
180 luaA_object_push(globalconf.L, c);
181 event_button_callback(ev, &c->buttons, -1, 1, NULL);
182 xcb_allow_events(globalconf.connection,
183 XCB_ALLOW_REPLAY_POINTER,
184 XCB_CURRENT_TIME);
186 else if(ev->child == XCB_NONE)
187 for(screen = 0; screen < nb_screen; screen++)
188 if(xutil_screen_get(connection, screen)->root == ev->event)
190 event_button_callback(ev, &globalconf.buttons, 0, 0, NULL);
191 return 0;
194 return 0;
197 static void
198 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
200 uint16_t config_win_mask = 0;
201 uint32_t config_win_vals[7];
202 unsigned short i = 0;
204 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
206 config_win_mask |= XCB_CONFIG_WINDOW_X;
207 config_win_vals[i++] = ev->x;
209 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
211 config_win_mask |= XCB_CONFIG_WINDOW_Y;
212 config_win_vals[i++] = ev->y;
214 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
216 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
217 config_win_vals[i++] = ev->width;
219 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
221 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
222 config_win_vals[i++] = ev->height;
224 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
226 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
227 config_win_vals[i++] = ev->border_width;
229 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
231 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
232 config_win_vals[i++] = ev->sibling;
234 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
236 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
237 config_win_vals[i++] = ev->stack_mode;
240 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
243 /** The configure event handler.
244 * \param data currently unused.
245 * \param connection The connection to the X server.
246 * \param ev The event.
248 static int
249 event_handle_configurerequest(void *data __attribute__ ((unused)),
250 xcb_connection_t *connection, xcb_configure_request_event_t *ev)
252 client_t *c;
253 area_t geometry;
255 if((c = client_getbywin(ev->window)))
257 geometry = titlebar_geometry_remove(c->titlebar, c->border_width, c->geometry);
259 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
260 geometry.x = ev->x;
261 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
262 geometry.y = ev->y;
263 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
264 geometry.width = ev->width;
265 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
266 geometry.height = ev->height;
268 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
270 luaA_object_push(globalconf.L, c);
271 client_set_border_width(globalconf.L, -1, ev->border_width);
272 lua_pop(globalconf.L, 1);
275 /* Clients are not allowed to directly mess with stacking parameters. */
276 ev->value_mask &= ~(XCB_CONFIG_WINDOW_SIBLING |
277 XCB_CONFIG_WINDOW_STACK_MODE);
279 /** Configure request are sent with size relative to real (internal)
280 * window size, i.e. without titlebars and borders. */
281 geometry = titlebar_geometry_add(c->titlebar, c->border_width, geometry);
283 if(!client_resize(c, geometry, false))
285 /* Resize wasn't officially needed, but we don't want to break expectations. */
286 geometry = titlebar_geometry_remove(c->titlebar, c->border_width, c->geometry);
287 window_configure(c->window, geometry, c->border_width);
290 else
291 event_handle_configurerequest_configure_window(ev);
293 return 0;
296 /** The configure notify event handler.
297 * \param data currently unused.
298 * \param connection The connection to the X server.
299 * \param ev The event.
301 static int
302 event_handle_configurenotify(void *data __attribute__ ((unused)),
303 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
305 int screen_nbr;
306 const xcb_screen_t *screen;
308 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
309 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
310 && ev->window == screen->root
311 && (ev->width != screen->width_in_pixels
312 || ev->height != screen->height_in_pixels))
313 /* it's not that we panic, but restart */
314 awesome_restart();
316 return 0;
319 /** The destroy notify event handler.
320 * \param data currently unused.
321 * \param connection The connection to the X server.
322 * \param ev The event.
324 static int
325 event_handle_destroynotify(void *data __attribute__ ((unused)),
326 xcb_connection_t *connection __attribute__ ((unused)),
327 xcb_destroy_notify_event_t *ev)
329 client_t *c;
331 if((c = client_getbywin(ev->window)))
332 client_unmanage(c);
333 else
334 for(int i = 0; i < globalconf.embedded.len; i++)
335 if(globalconf.embedded.tab[i].win == ev->window)
337 xembed_window_array_take(&globalconf.embedded, i);
338 widget_invalidate_bytype(widget_systray);
341 return 0;
344 /** Handle a motion notify over widgets.
345 * \param object The object.
346 * \param mouse_over The pointer to the registered mouse over widget.
347 * \param widget The new widget the mouse is over.
349 static void
350 event_handle_widget_motionnotify(void *object,
351 widget_t **mouse_over,
352 widget_t *widget)
354 if(widget != *mouse_over)
356 if(*mouse_over)
358 /* call mouse leave function on old widget */
359 luaA_object_push(globalconf.L, *mouse_over);
360 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
361 lua_pop(globalconf.L, 1);
363 if(widget)
365 /* emit mouse::enter signal on new widget and register it */
366 *mouse_over = widget;
367 luaA_object_push(globalconf.L, widget);
368 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
369 lua_pop(globalconf.L, 1);
374 /** The motion notify event handler.
375 * \param data currently unused.
376 * \param connection The connection to the X server.
377 * \param ev The event.
379 static int
380 event_handle_motionnotify(void *data __attribute__ ((unused)),
381 xcb_connection_t *connection,
382 xcb_motion_notify_event_t *ev)
384 wibox_t *wibox;
386 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
387 return 0;
389 if((wibox = wibox_getbywin(ev->event)))
391 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
392 wibox->geometry.width,
393 wibox->geometry.height,
394 &ev->event_x, &ev->event_y);
395 if(w)
396 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
399 return 0;
402 /** The leave notify event handler.
403 * \param data currently unused.
404 * \param connection The connection to the X server.
405 * \param ev The event.
407 static int
408 event_handle_leavenotify(void *data __attribute__ ((unused)),
409 xcb_connection_t *connection,
410 xcb_leave_notify_event_t *ev)
412 wibox_t *wibox;
413 client_t *c;
415 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
416 return 0;
418 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
419 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
421 luaA_object_push(globalconf.L, c);
422 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
425 if((wibox = wibox_getbywin(ev->event)))
427 if(wibox->mouse_over)
429 luaA_object_push(globalconf.L, wibox->mouse_over);
430 /* emit mouse::leave signal on widget the mouse was over */
431 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
432 lua_pop(globalconf.L, 1);
433 wibox->mouse_over = NULL;
436 luaA_object_push(globalconf.L, wibox);
437 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
438 lua_pop(globalconf.L, 1);
441 return 0;
444 /** The enter notify event handler.
445 * \param data currently unused.
446 * \param connection The connection to the X server.
447 * \param ev The event.
449 static int
450 event_handle_enternotify(void *data __attribute__ ((unused)),
451 xcb_connection_t *connection,
452 xcb_enter_notify_event_t *ev)
454 client_t *c;
455 wibox_t *wibox;
457 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
458 return 0;
460 if((wibox = wibox_getbywin(ev->event)))
462 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
463 wibox->geometry.width,
464 wibox->geometry.height,
465 &ev->event_x, &ev->event_y);
466 if(w)
467 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
469 luaA_object_push(globalconf.L, wibox);
470 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
471 lua_pop(globalconf.L, 1);
474 if((c = client_getbytitlebarwin(ev->event))
475 || (c = client_getbywin(ev->event)))
476 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
478 luaA_object_push(globalconf.L, c);
479 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
482 return 0;
485 /** The focus in event handler.
486 * \param data currently unused.
487 * \param connection The connection to the X server.
488 * \param ev The event.
490 static int
491 event_handle_focusin(void *data __attribute__ ((unused)),
492 xcb_connection_t *connection,
493 xcb_focus_in_event_t *ev)
495 client_t *c;
497 /* Events that we are interested in: */
498 switch(ev->detail)
500 /* These are events that jump between root windows.
502 case XCB_NOTIFY_DETAIL_ANCESTOR:
503 case XCB_NOTIFY_DETAIL_INFERIOR:
505 /* These are events that jump between clients.
506 * Virtual events ensure we always get an event on our top-level window.
508 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
509 case XCB_NOTIFY_DETAIL_NONLINEAR:
510 if((c = client_getbytitlebarwin(ev->event))
511 || (c = client_getbywin(ev->event)))
512 client_focus_update(c);
513 /* all other events are ignored */
514 default:
515 break;
517 return 0;
520 /** The expose event handler.
521 * \param data currently unused.
522 * \param connection The connection to the X server.
523 * \param ev The event.
525 static int
526 event_handle_expose(void *data __attribute__ ((unused)),
527 xcb_connection_t *connection __attribute__ ((unused)),
528 xcb_expose_event_t *ev)
530 wibox_t *wibox;
532 if((wibox = wibox_getbywin(ev->window)))
533 wibox_refresh_pixmap_partial(wibox,
534 ev->x, ev->y,
535 ev->width, ev->height);
537 return 0;
540 /** The key press event handler.
541 * \param data currently unused.
542 * \param connection The connection to the X server.
543 * \param ev The event.
545 static int
546 event_handle_key(void *data __attribute__ ((unused)),
547 xcb_connection_t *connection __attribute__ ((unused)),
548 xcb_key_press_event_t *ev)
550 if(globalconf.keygrabber != LUA_REFNIL)
552 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
553 if(keygrabber_handlekpress(globalconf.L, ev))
555 if(lua_pcall(globalconf.L, 3, 1, 0))
557 warn("error running function: %s", lua_tostring(globalconf.L, -1));
558 luaA_keygrabber_stop(globalconf.L);
560 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
561 luaA_keygrabber_stop(globalconf.L);
563 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
565 else
567 /* get keysym ignoring all modifiers */
568 xcb_keysym_t keysym = key_getkeysym(ev->detail, 0);
569 client_t *c;
570 if((c = client_getbywin(ev->event)))
572 luaA_object_push(globalconf.L, c);
573 event_key_callback(ev, &c->keys, -1, 1, &keysym);
575 else
576 event_key_callback(ev, &globalconf.keys, 0, 0, &keysym);
579 return 0;
582 /** The map request event handler.
583 * \param data currently unused.
584 * \param connection The connection to the X server.
585 * \param ev The event.
587 static int
588 event_handle_maprequest(void *data __attribute__ ((unused)),
589 xcb_connection_t *connection, xcb_map_request_event_t *ev)
591 int phys_screen, ret = 0;
592 client_t *c;
593 xcb_get_window_attributes_cookie_t wa_c;
594 xcb_get_window_attributes_reply_t *wa_r;
595 xcb_get_geometry_cookie_t geom_c;
596 xcb_get_geometry_reply_t *geom_r;
598 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
600 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
601 return -1;
603 if(wa_r->override_redirect)
604 goto bailout;
606 if(xembed_getbywin(&globalconf.embedded, ev->window))
608 xcb_map_window(connection, ev->window);
609 xembed_window_activate(connection, ev->window);
611 else if((c = client_getbywin(ev->window)))
613 /* Check that it may be visible, but not asked to be hidden */
614 if(client_maybevisible(c, c->screen) && !c->hidden)
616 luaA_object_push(globalconf.L, c);
617 client_set_minimized(globalconf.L, -1, false);
618 lua_pop(globalconf.L, 1);
619 /* it will be raised, so just update ourself */
620 client_raise(c);
623 else
625 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
627 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
629 ret = -1;
630 goto bailout;
633 phys_screen = xutil_root2screen(connection, geom_r->root);
635 client_manage(ev->window, geom_r, phys_screen, false);
637 p_delete(&geom_r);
640 bailout:
641 p_delete(&wa_r);
642 return ret;
645 /** The unmap notify event handler.
646 * \param data currently unused.
647 * \param connection The connection to the X server.
648 * \param ev The event.
650 static int
651 event_handle_unmapnotify(void *data __attribute__ ((unused)),
652 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
654 client_t *c;
656 if((c = client_getbywin(ev->window)))
658 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
659 && XCB_EVENT_SENT(ev)
660 && window_state_get_reply(window_state_get_unchecked(c->window)) == XCB_WM_STATE_NORMAL)
661 client_unmanage(c);
663 else
664 for(int i = 0; i < globalconf.embedded.len; i++)
665 if(globalconf.embedded.tab[i].win == ev->window)
667 xembed_window_array_take(&globalconf.embedded, i);
668 widget_invalidate_bytype(widget_systray);
671 return 0;
674 /** The randr screen change notify event handler.
675 * \param data currently unused.
676 * \param connection The connection to the X server.
677 * \param ev The event.
679 static int
680 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
681 xcb_connection_t *connection __attribute__ ((unused)),
682 xcb_randr_screen_change_notify_event_t *ev)
684 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
685 * (only the code relevant to RRScreenChangeNotify) as the latter
686 * doesn't provide this kind of function */
687 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
688 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
689 ev->mheight, ev->mwidth);
690 else
691 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
692 ev->mwidth, ev->mheight);
694 /* XRRUpdateConfiguration also executes the following instruction
695 * but it's not useful because SubpixelOrder is not used at all at
696 * the moment
698 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
701 awesome_restart();
703 return 0;
706 /** The client message event handler.
707 * \param data currently unused.
708 * \param connection The connection to the X server.
709 * \param ev The event.
711 static int
712 event_handle_clientmessage(void *data __attribute__ ((unused)),
713 xcb_connection_t *connection,
714 xcb_client_message_event_t *ev)
716 /* check for startup notification messages */
717 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
718 return 0;
720 if(ev->type == WM_CHANGE_STATE)
722 client_t *c;
723 if((c = client_getbywin(ev->window))
724 && ev->format == 32
725 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
727 luaA_object_push(globalconf.L, c);
728 client_set_minimized(globalconf.L, -1, true);
729 lua_pop(globalconf.L, 1);
732 else if(ev->type == _XEMBED)
733 return xembed_process_client_message(ev);
734 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
735 return systray_process_client_message(ev);
736 return ewmh_process_client_message(ev);
739 /** The keymap change notify event handler.
740 * \param data Unused data.
741 * \param connection The connection to the X server.
742 * \param ev The event.
743 * \return Status code, 0 if everything's fine.
745 static int
746 event_handle_mappingnotify(void *data,
747 xcb_connection_t *connection,
748 xcb_mapping_notify_event_t *ev)
750 if(ev->request == XCB_MAPPING_MODIFIER
751 || ev->request == XCB_MAPPING_KEYBOARD)
753 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
754 xcb_get_modifier_mapping_unchecked(globalconf.connection);
756 /* Free and then allocate the key symbols */
757 xcb_key_symbols_free(globalconf.keysyms);
758 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
760 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
761 globalconf.keysyms, &globalconf.numlockmask,
762 &globalconf.shiftlockmask, &globalconf.capslockmask,
763 &globalconf.modeswitchmask);
765 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
767 /* regrab everything */
768 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
770 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
771 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
772 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
773 window_grabkeys(s->root, &globalconf.keys);
776 foreach(_c, globalconf.clients)
778 client_t *c = *_c;
779 xcb_ungrab_key(connection, XCB_GRAB_ANY, c->window, XCB_BUTTON_MASK_ANY);
780 window_grabkeys(c->window, &c->keys);
784 return 0;
787 static int
788 event_handle_reparentnotify(void *data,
789 xcb_connection_t *connection,
790 xcb_reparent_notify_event_t *ev)
792 client_t *c;
794 if((c = client_getbywin(ev->window)))
795 client_unmanage(c);
797 return 0;
800 void a_xcb_set_event_handlers(void)
802 const xcb_query_extension_reply_t *randr_query;
804 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
805 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
806 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
807 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
808 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
809 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
810 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
811 xcb_event_set_focus_in_handler(&globalconf.evenths, event_handle_focusin, NULL);
812 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
813 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
814 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
815 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
816 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
817 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
818 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
819 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
820 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
822 /* check for randr extension */
823 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
824 if(randr_query->present)
825 xcb_event_set_handler(&globalconf.evenths,
826 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
827 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
828 NULL);
832 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80