change codename
[awesome.git] / event.c
blobd9f84a2714e60bc8631035df5fbc771b7da3a954
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))
284 /* All the wiboxes (may) need to be repositioned. */
285 if(client_hasstrut(c))
286 wibox_update_positions();
287 client_need_arrange(c);
289 else
291 /* Resize wasn't officially needed, but we don't want to break expectations. */
292 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
293 window_configure(c->win, geometry, c->border);
297 else
298 event_handle_configurerequest_configure_window(ev);
300 return 0;
303 /** The configure notify event handler.
304 * \param data currently unused.
305 * \param connection The connection to the X server.
306 * \param ev The event.
308 static int
309 event_handle_configurenotify(void *data __attribute__ ((unused)),
310 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
312 int screen_nbr;
313 const xcb_screen_t *screen;
315 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
316 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
317 && ev->window == screen->root
318 && (ev->width != screen->width_in_pixels
319 || ev->height != screen->height_in_pixels))
320 /* it's not that we panic, but restart */
321 awesome_restart();
323 return 0;
326 /** The destroy notify event handler.
327 * \param data currently unused.
328 * \param connection The connection to the X server.
329 * \param ev The event.
331 static int
332 event_handle_destroynotify(void *data __attribute__ ((unused)),
333 xcb_connection_t *connection __attribute__ ((unused)),
334 xcb_destroy_notify_event_t *ev)
336 client_t *c;
338 if((c = client_getbywin(ev->window)))
339 client_unmanage(c);
340 else
341 for(int i = 0; i < globalconf.embedded.len; i++)
342 if(globalconf.embedded.tab[i].win == ev->window)
344 xembed_window_array_take(&globalconf.embedded, i);
345 foreach(screen, globalconf.screens)
346 widget_invalidate_bytype(screen, widget_systray);
349 return 0;
352 /** Handle a motion notify over widgets.
353 * \param object The object.
354 * \param mouse_over The pointer to the registered mouse over widget.
355 * \param widget The new widget the mouse is over.
357 static void
358 event_handle_widget_motionnotify(void *object,
359 widget_t **mouse_over,
360 widget_t *widget)
362 if(widget != *mouse_over)
364 if(*mouse_over)
366 if((*mouse_over)->mouse_leave != LUA_REFNIL)
368 /* call mouse leave function on old widget */
369 widget_push(globalconf.L, *mouse_over);
370 luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
373 if(widget)
375 /* call mouse enter function on new widget and register it */
376 *mouse_over = widget;
377 if(widget->mouse_enter != LUA_REFNIL)
379 widget_push(globalconf.L, widget);
380 luaA_dofunction(globalconf.L, widget->mouse_enter, 1, 0);
386 /** The motion notify event handler.
387 * \param data currently unused.
388 * \param connection The connection to the X server.
389 * \param ev The event.
391 static int
392 event_handle_motionnotify(void *data __attribute__ ((unused)),
393 xcb_connection_t *connection,
394 xcb_motion_notify_event_t *ev)
396 wibox_t *wibox;
398 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
399 return 0;
401 if((wibox = wibox_getbywin(ev->event)))
403 widget_t *w = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
404 wibox->sw.geometry.width,
405 wibox->sw.geometry.height,
406 &ev->event_x, &ev->event_y);
407 if(w)
408 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
411 return 0;
414 /** The leave notify event handler.
415 * \param data currently unused.
416 * \param connection The connection to the X server.
417 * \param ev The event.
419 static int
420 event_handle_leavenotify(void *data __attribute__ ((unused)),
421 xcb_connection_t *connection,
422 xcb_leave_notify_event_t *ev)
424 wibox_t *wibox;
425 client_t *c;
427 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
428 return 0;
430 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
431 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
433 client_push(globalconf.L, c);
434 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
437 if((wibox = wibox_getbywin(ev->event)))
439 if(wibox->mouse_over)
441 if(wibox->mouse_over->mouse_leave != LUA_REFNIL)
443 /* call mouse leave function on widget the mouse was over */
444 wibox_push(globalconf.L, wibox);
445 luaA_dofunction(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
447 wibox->mouse_over = NULL;
450 if(wibox->mouse_leave != LUA_REFNIL)
451 luaA_dofunction(globalconf.L, wibox->mouse_leave, 0, 0);
454 return 0;
457 /** The enter notify event handler.
458 * \param data currently unused.
459 * \param connection The connection to the X server.
460 * \param ev The event.
462 static int
463 event_handle_enternotify(void *data __attribute__ ((unused)),
464 xcb_connection_t *connection,
465 xcb_enter_notify_event_t *ev)
467 client_t *c;
468 wibox_t *wibox;
470 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
471 return 0;
473 if((wibox = wibox_getbywin(ev->event)))
475 widget_t *w = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
476 wibox->sw.geometry.width,
477 wibox->sw.geometry.height,
478 &ev->event_x, &ev->event_y);
479 if(w)
480 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
482 if(wibox->mouse_enter != LUA_REFNIL)
483 luaA_dofunction(globalconf.L, wibox->mouse_enter, 0, 0);
486 if((c = client_getbytitlebarwin(ev->event))
487 || (c = client_getbywin(ev->event)))
488 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
490 client_push(globalconf.L, c);
491 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
494 return 0;
497 /** The focus in event handler.
498 * \param data currently unused.
499 * \param connection The connection to the X server.
500 * \param ev The event.
502 static int
503 event_handle_focusin(void *data __attribute__ ((unused)),
504 xcb_connection_t *connection,
505 xcb_focus_in_event_t *ev)
507 client_t *c;
509 /* Events that we are interested in: */
510 switch(ev->detail)
512 /* These are events that jump between root windows.
514 case XCB_NOTIFY_DETAIL_ANCESTOR:
515 case XCB_NOTIFY_DETAIL_INFERIOR:
517 /* These are events that jump between clients.
518 * Virtual events ensure we always get an event on our top-level window.
520 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
521 case XCB_NOTIFY_DETAIL_NONLINEAR:
522 if((c = client_getbytitlebarwin(ev->event))
523 || (c = client_getbywin(ev->event)))
524 client_focus_update(c);
525 /* all other events are ignored */
526 default:
527 break;
529 return 0;
532 /** The expose 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_expose(void *data __attribute__ ((unused)),
539 xcb_connection_t *connection __attribute__ ((unused)),
540 xcb_expose_event_t *ev)
542 wibox_t *wibox;
544 if((wibox = wibox_getbywin(ev->window)))
545 simplewindow_refresh_pixmap_partial(&wibox->sw,
546 ev->x, ev->y,
547 ev->width, ev->height);
549 return 0;
552 /** The key press event handler.
553 * \param data currently unused.
554 * \param connection The connection to the X server.
555 * \param ev The event.
557 static int
558 event_handle_key(void *data __attribute__ ((unused)),
559 xcb_connection_t *connection __attribute__ ((unused)),
560 xcb_key_press_event_t *ev)
562 if(globalconf.keygrabber != LUA_REFNIL)
564 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
565 if(keygrabber_handlekpress(globalconf.L, ev))
567 if(lua_pcall(globalconf.L, 3, 1, 0))
569 warn("error running function: %s", lua_tostring(globalconf.L, -1));
570 luaA_keygrabber_stop(globalconf.L);
572 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
573 luaA_keygrabber_stop(globalconf.L);
575 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
577 else
579 /* get keysym ignoring all modifiers */
580 xcb_keysym_t keysym = key_getkeysym(ev->detail, 0);
581 client_t *c;
582 if((c = client_getbywin(ev->event)))
584 client_push(globalconf.L, c);
585 event_key_callback(ev, &c->keys, 1, &keysym);
587 else
588 event_key_callback(ev, &globalconf.keys, 0, &keysym);
591 return 0;
594 /** The map request event handler.
595 * \param data currently unused.
596 * \param connection The connection to the X server.
597 * \param ev The event.
599 static int
600 event_handle_maprequest(void *data __attribute__ ((unused)),
601 xcb_connection_t *connection, xcb_map_request_event_t *ev)
603 int phys_screen, ret = 0;
604 client_t *c;
605 xcb_get_window_attributes_cookie_t wa_c;
606 xcb_get_window_attributes_reply_t *wa_r;
607 xcb_get_geometry_cookie_t geom_c;
608 xcb_get_geometry_reply_t *geom_r;
610 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
612 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
613 return -1;
615 if(wa_r->override_redirect)
616 goto bailout;
618 if(xembed_getbywin(&globalconf.embedded, ev->window))
620 xcb_map_window(connection, ev->window);
621 xembed_window_activate(connection, ev->window);
623 else if((c = client_getbywin(ev->window)))
625 /* Check that it may be visible, but not asked to be hidden */
626 if(client_maybevisible(c, c->screen) && !c->ishidden)
628 client_setminimized(c, false);
629 /* it will be raised, so just update ourself */
630 client_raise(c);
633 else
635 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
637 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
639 ret = -1;
640 goto bailout;
643 phys_screen = xutil_root2screen(connection, geom_r->root);
645 client_manage(ev->window, geom_r, phys_screen, false);
647 p_delete(&geom_r);
650 bailout:
651 p_delete(&wa_r);
652 return ret;
655 /** The unmap notify event handler.
656 * \param data currently unused.
657 * \param connection The connection to the X server.
658 * \param ev The event.
660 static int
661 event_handle_unmapnotify(void *data __attribute__ ((unused)),
662 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
664 client_t *c;
666 if((c = client_getbywin(ev->window)))
668 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
669 && XCB_EVENT_SENT(ev)
670 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
671 client_unmanage(c);
673 else
674 for(int i = 0; i < globalconf.embedded.len; i++)
675 if(globalconf.embedded.tab[i].win == ev->window)
677 xembed_window_array_take(&globalconf.embedded, i);
678 foreach(screen, globalconf.screens)
679 widget_invalidate_bytype(screen, widget_systray);
682 return 0;
685 /** The randr screen change notify event handler.
686 * \param data currently unused.
687 * \param connection The connection to the X server.
688 * \param ev The event.
690 static int
691 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
692 xcb_connection_t *connection __attribute__ ((unused)),
693 xcb_randr_screen_change_notify_event_t *ev)
695 if(!globalconf.have_randr)
696 return -1;
698 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
699 * (only the code relevant to RRScreenChangeNotify) as the latter
700 * doesn't provide this kind of function */
701 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
702 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
703 ev->mheight, ev->mwidth);
704 else
705 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
706 ev->mwidth, ev->mheight);
708 /* XRRUpdateConfiguration also executes the following instruction
709 * but it's not useful because SubpixelOrder is not used at all at
710 * the moment
712 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
715 awesome_restart();
717 return 0;
720 /** The client message event handler.
721 * \param data currently unused.
722 * \param connection The connection to the X server.
723 * \param ev The event.
725 static int
726 event_handle_clientmessage(void *data __attribute__ ((unused)),
727 xcb_connection_t *connection,
728 xcb_client_message_event_t *ev)
730 /* check for startup notification messages */
731 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
732 return 0;
734 if(ev->type == WM_CHANGE_STATE)
736 client_t *c;
737 if((c = client_getbywin(ev->window))
738 && ev->format == 32
739 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
740 client_setminimized(c, true);
742 else if(ev->type == _XEMBED)
743 return xembed_process_client_message(ev);
744 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
745 return systray_process_client_message(ev);
746 return ewmh_process_client_message(ev);
749 /** The keymap change notify event handler.
750 * \param data Unused data.
751 * \param connection The connection to the X server.
752 * \param ev The event.
753 * \return Status code, 0 if everything's fine.
755 static int
756 event_handle_mappingnotify(void *data,
757 xcb_connection_t *connection,
758 xcb_mapping_notify_event_t *ev)
760 if(ev->request == XCB_MAPPING_MODIFIER
761 || ev->request == XCB_MAPPING_KEYBOARD)
763 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
764 xcb_get_modifier_mapping_unchecked(globalconf.connection);
766 /* Free and then allocate the key symbols */
767 xcb_key_symbols_free(globalconf.keysyms);
768 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
770 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
771 globalconf.keysyms, &globalconf.numlockmask,
772 &globalconf.shiftlockmask, &globalconf.capslockmask,
773 &globalconf.modeswitchmask);
775 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
777 /* regrab everything */
778 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
780 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
781 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
782 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
783 window_grabkeys(s->root, &globalconf.keys);
786 foreach(_c, globalconf.clients)
788 client_t *c = *_c;
789 xcb_ungrab_key(connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
790 window_grabkeys(c->win, &c->keys);
794 return 0;
797 static int
798 event_handle_reparentnotify(void *data,
799 xcb_connection_t *connection,
800 xcb_reparent_notify_event_t *ev)
802 client_t *c;
804 if((c = client_getbywin(ev->window)))
805 client_unmanage(c);
807 return 0;
810 void a_xcb_set_event_handlers(void)
812 const xcb_query_extension_reply_t *randr_query;
814 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
815 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
816 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
817 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
818 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
819 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
820 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
821 xcb_event_set_focus_in_handler(&globalconf.evenths, event_handle_focusin, NULL);
822 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
823 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
824 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
825 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
826 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
827 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
828 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
829 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
830 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
832 /* check for randr extension */
833 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
834 if((globalconf.have_randr = randr_query->present))
835 xcb_event_set_handler(&globalconf.evenths,
836 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
837 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
838 NULL);
842 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80