build: do not use common as include dir
[awesome.git] / event.c
blobdb837faa4e35c88021981254e90317b37f2f2fc3
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, prefix, xcbtype, xcbeventprefix, match) \
46 static int \
47 event_##xcbtype##_callback(xcb_##xcbtype##_press_event_t *ev, \
48 void *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, item_matched = 0; \
55 if(oud) \
56 luaA_object_push_item(globalconf.L, abs_oud, arr); \
57 else \
58 luaA_object_push(globalconf.L, arr); \
59 lua_pushnil(globalconf.L); \
60 while(luaA_next(globalconf.L, -2)) \
61 { \
62 type *item = luaA_toudata(globalconf.L, -1, #prefix); \
63 if(item && match(ev, item, data)) \
64 { \
65 lua_insert(globalconf.L, -3); \
66 item_matching++; \
67 } \
68 else \
69 lua_pop(globalconf.L, 1); \
70 } \
71 /* remove the table */ \
72 lua_pop(globalconf.L, 1); \
73 for(item_matched = item_matching; item_matching > 0; item_matching--) \
74 { \
75 type *item = luaL_checkudata(globalconf.L, -1, #prefix); \
76 switch(ev->response_type) \
77 { \
78 case xcbeventprefix##_PRESS: \
79 if(item->press) \
80 { \
81 for(int i = 0; i < nargs; i++) \
82 lua_pushvalue(globalconf.L, - nargs - item_matching); \
83 luaA_object_push_item(globalconf.L, - nargs - 1, item->press); \
84 luaA_dofunction(globalconf.L, nargs, 0); \
85 } \
86 break; \
87 case xcbeventprefix##_RELEASE: \
88 if(item->release) \
89 { \
90 for(int i = 0; i < nargs; i++) \
91 lua_pushvalue(globalconf.L, - nargs - item_matching); \
92 luaA_object_push_item(globalconf.L, - nargs - 1, item->release); \
93 luaA_dofunction(globalconf.L, nargs, 0); \
94 } \
95 break; \
96 } \
97 lua_pop(globalconf.L, 1); \
98 } \
99 lua_pop(globalconf.L, nargs); \
100 return item_matched; \
103 static bool
104 event_button_match(xcb_button_press_event_t *ev, button_t *b, void *data)
106 return ((!b->button || ev->detail == b->button)
107 && (b->mod == XCB_BUTTON_MASK_ANY || b->mod == ev->state));
110 static bool
111 event_key_match(xcb_key_press_event_t *ev, keyb_t *k, void *data)
113 assert(data);
114 xcb_keysym_t keysym = *(xcb_keysym_t *) data;
115 return (((k->keycode && ev->detail == k->keycode)
116 || (k->keysym && keysym == k->keysym))
117 && (k->mod == XCB_BUTTON_MASK_ANY || k->mod == ev->state));
120 DO_EVENT_HOOK_CALLBACK(button_t, button, button, XCB_BUTTON, event_button_match)
121 DO_EVENT_HOOK_CALLBACK(keyb_t, key, key, XCB_KEY, event_key_match)
123 /** Handle an event with mouse grabber if needed
124 * \param x The x coordinate.
125 * \param y The y coordinate.
126 * \param mask The mask buttons.
127 * \return True if the event was handled.
129 static bool
130 event_handle_mousegrabber(int x, int y, uint16_t mask)
132 if(globalconf.mousegrabber != LUA_REFNIL)
134 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
135 mousegrabber_handleevent(globalconf.L, x, y, mask);
136 if(lua_pcall(globalconf.L, 1, 1, 0))
138 warn("error running function: %s", lua_tostring(globalconf.L, -1));
139 luaA_mousegrabber_stop(globalconf.L);
141 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
142 luaA_mousegrabber_stop(globalconf.L);
143 lua_pop(globalconf.L, 1); /* pop returned value */
144 return true;
146 return false;
149 /** The button press event handler.
150 * \param data The type of mouse event.
151 * \param connection The connection to the X server.
152 * \param ev The event.
154 static int
155 event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev)
157 int screen;
158 const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection));
159 client_t *c;
160 wibox_t *wibox;
162 if(event_handle_mousegrabber(ev->root_x, ev->root_y, 1 << (ev->detail - 1 + 8)))
163 return 0;
165 /* ev->state is
166 * button status (8 bits) + modifiers status (8 bits)
167 * we don't care for button status that we get, especially on release, so
168 * drop them */
169 ev->state &= 0x00ff;
171 if((wibox = wibox_getbywin(ev->event))
172 || (wibox = wibox_getbywin(ev->child)))
174 /* If the wibox is child, then x,y are
175 * relative to root window */
176 if(wibox->window == ev->child)
178 ev->event_x -= wibox->geometry.x;
179 ev->event_y -= wibox->geometry.y;
182 wibox_push(globalconf.L, wibox);
183 event_button_callback(ev, wibox->buttons, -1, 1, NULL);
185 /* then try to match a widget binding */
186 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
187 wibox->geometry.width,
188 wibox->geometry.height,
189 &ev->event_x, &ev->event_y);
190 if(w)
192 widget_push(globalconf.L, w);
193 wibox_push(globalconf.L, wibox);
194 event_button_callback(ev, w->buttons, -2, 2, NULL);
197 else if((c = client_getbywin(ev->event)))
199 client_push(globalconf.L, c);
200 event_button_callback(ev, c->buttons, -1, 1, NULL);
201 xcb_allow_events(globalconf.connection,
202 XCB_ALLOW_REPLAY_POINTER,
203 XCB_CURRENT_TIME);
205 else if(ev->child == XCB_NONE)
206 for(screen = 0; screen < nb_screen; screen++)
207 if(xutil_screen_get(connection, screen)->root == ev->event)
209 event_button_callback(ev, globalconf.buttons, 0, 0, NULL);
210 return 0;
213 return 0;
216 static void
217 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
219 uint16_t config_win_mask = 0;
220 uint32_t config_win_vals[7];
221 unsigned short i = 0;
223 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
225 config_win_mask |= XCB_CONFIG_WINDOW_X;
226 config_win_vals[i++] = ev->x;
228 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
230 config_win_mask |= XCB_CONFIG_WINDOW_Y;
231 config_win_vals[i++] = ev->y;
233 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
235 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
236 config_win_vals[i++] = ev->width;
238 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
240 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
241 config_win_vals[i++] = ev->height;
243 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
245 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
246 config_win_vals[i++] = ev->border_width;
248 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
250 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
251 config_win_vals[i++] = ev->sibling;
253 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
255 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
256 config_win_vals[i++] = ev->stack_mode;
259 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
262 /** The configure event handler.
263 * \param data currently unused.
264 * \param connection The connection to the X server.
265 * \param ev The event.
267 static int
268 event_handle_configurerequest(void *data __attribute__ ((unused)),
269 xcb_connection_t *connection, xcb_configure_request_event_t *ev)
271 client_t *c;
272 area_t geometry;
274 if((c = client_getbywin(ev->window)))
276 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
278 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
279 geometry.x = ev->x;
280 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
281 geometry.y = ev->y;
282 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
283 geometry.width = ev->width;
284 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
285 geometry.height = ev->height;
287 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
288 client_setborder(c, ev->border_width);
290 /* Clients are not allowed to directly mess with stacking parameters. */
291 ev->value_mask &= ~(XCB_CONFIG_WINDOW_SIBLING |
292 XCB_CONFIG_WINDOW_STACK_MODE);
294 /** Configure request are sent with size relative to real (internal)
295 * window size, i.e. without titlebars and borders. */
296 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
298 if(!client_resize(c, geometry, false))
300 /* Resize wasn't officially needed, but we don't want to break expectations. */
301 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
302 window_configure(c->win, geometry, c->border);
305 else
306 event_handle_configurerequest_configure_window(ev);
308 return 0;
311 /** The configure notify event handler.
312 * \param data currently unused.
313 * \param connection The connection to the X server.
314 * \param ev The event.
316 static int
317 event_handle_configurenotify(void *data __attribute__ ((unused)),
318 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
320 int screen_nbr;
321 const xcb_screen_t *screen;
323 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
324 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
325 && ev->window == screen->root
326 && (ev->width != screen->width_in_pixels
327 || ev->height != screen->height_in_pixels))
328 /* it's not that we panic, but restart */
329 awesome_restart();
331 return 0;
334 /** The destroy notify event handler.
335 * \param data currently unused.
336 * \param connection The connection to the X server.
337 * \param ev The event.
339 static int
340 event_handle_destroynotify(void *data __attribute__ ((unused)),
341 xcb_connection_t *connection __attribute__ ((unused)),
342 xcb_destroy_notify_event_t *ev)
344 client_t *c;
346 if((c = client_getbywin(ev->window)))
347 client_unmanage(c);
348 else
349 for(int i = 0; i < globalconf.embedded.len; i++)
350 if(globalconf.embedded.tab[i].win == ev->window)
352 xembed_window_array_take(&globalconf.embedded, i);
353 widget_invalidate_bytype(widget_systray);
356 return 0;
359 /** Handle a motion notify over widgets.
360 * \param object The object.
361 * \param mouse_over The pointer to the registered mouse over widget.
362 * \param widget The new widget the mouse is over.
364 static void
365 event_handle_widget_motionnotify(void *object,
366 widget_t **mouse_over,
367 widget_t *widget)
369 if(widget != *mouse_over)
371 if(*mouse_over)
373 if((*mouse_over)->mouse_leave)
375 /* call mouse leave function on old widget */
376 widget_push(globalconf.L, *mouse_over);
377 widget_push_item(globalconf.L, *mouse_over, (*mouse_over)->mouse_leave);
378 luaA_dofunction(globalconf.L, 1, 0);
381 if(widget)
383 /* call mouse enter function on new widget and register it */
384 *mouse_over = widget;
385 if(widget->mouse_enter)
387 widget_push(globalconf.L, widget);
388 widget_push_item(globalconf.L, widget, widget->mouse_enter);
389 luaA_dofunction(globalconf.L, 1, 0);
395 /** The motion notify event handler.
396 * \param data currently unused.
397 * \param connection The connection to the X server.
398 * \param ev The event.
400 static int
401 event_handle_motionnotify(void *data __attribute__ ((unused)),
402 xcb_connection_t *connection,
403 xcb_motion_notify_event_t *ev)
405 wibox_t *wibox;
407 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
408 return 0;
410 if((wibox = wibox_getbywin(ev->event)))
412 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
413 wibox->geometry.width,
414 wibox->geometry.height,
415 &ev->event_x, &ev->event_y);
416 if(w)
417 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
420 return 0;
423 /** The leave notify event handler.
424 * \param data currently unused.
425 * \param connection The connection to the X server.
426 * \param ev The event.
428 static int
429 event_handle_leavenotify(void *data __attribute__ ((unused)),
430 xcb_connection_t *connection,
431 xcb_leave_notify_event_t *ev)
433 wibox_t *wibox;
434 client_t *c;
436 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
437 return 0;
439 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
440 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
442 client_push(globalconf.L, c);
443 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
446 if((wibox = wibox_getbywin(ev->event)))
448 if(wibox->mouse_over)
450 if(wibox->mouse_over->mouse_leave)
452 /* call mouse leave function on widget the mouse was over */
453 wibox_push(globalconf.L, wibox);
454 widget_push_item(globalconf.L, wibox->mouse_over, wibox->mouse_over->mouse_leave);
455 luaA_dofunction(globalconf.L, 1, 0);
457 wibox->mouse_over = NULL;
460 if(wibox->mouse_leave)
462 wibox_push_item(globalconf.L, wibox, wibox->mouse_leave);
463 luaA_dofunction(globalconf.L, 0, 0);
467 return 0;
470 /** The enter notify event handler.
471 * \param data currently unused.
472 * \param connection The connection to the X server.
473 * \param ev The event.
475 static int
476 event_handle_enternotify(void *data __attribute__ ((unused)),
477 xcb_connection_t *connection,
478 xcb_enter_notify_event_t *ev)
480 client_t *c;
481 wibox_t *wibox;
483 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
484 return 0;
486 if((wibox = wibox_getbywin(ev->event)))
488 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
489 wibox->geometry.width,
490 wibox->geometry.height,
491 &ev->event_x, &ev->event_y);
492 if(w)
493 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
495 if(wibox->mouse_enter)
497 wibox_push_item(globalconf.L, wibox, wibox->mouse_enter);
498 luaA_dofunction(globalconf.L, 0, 0);
502 if((c = client_getbytitlebarwin(ev->event))
503 || (c = client_getbywin(ev->event)))
504 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
506 client_push(globalconf.L, c);
507 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
510 return 0;
513 /** The focus in event handler.
514 * \param data currently unused.
515 * \param connection The connection to the X server.
516 * \param ev The event.
518 static int
519 event_handle_focusin(void *data __attribute__ ((unused)),
520 xcb_connection_t *connection,
521 xcb_focus_in_event_t *ev)
523 client_t *c;
525 /* Events that we are interested in: */
526 switch(ev->detail)
528 /* These are events that jump between root windows.
530 case XCB_NOTIFY_DETAIL_ANCESTOR:
531 case XCB_NOTIFY_DETAIL_INFERIOR:
533 /* These are events that jump between clients.
534 * Virtual events ensure we always get an event on our top-level window.
536 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
537 case XCB_NOTIFY_DETAIL_NONLINEAR:
538 if((c = client_getbytitlebarwin(ev->event))
539 || (c = client_getbywin(ev->event)))
540 client_focus_update(c);
541 /* all other events are ignored */
542 default:
543 break;
545 return 0;
548 /** The expose event handler.
549 * \param data currently unused.
550 * \param connection The connection to the X server.
551 * \param ev The event.
553 static int
554 event_handle_expose(void *data __attribute__ ((unused)),
555 xcb_connection_t *connection __attribute__ ((unused)),
556 xcb_expose_event_t *ev)
558 wibox_t *wibox;
560 if((wibox = wibox_getbywin(ev->window)))
561 wibox_refresh_pixmap_partial(wibox,
562 ev->x, ev->y,
563 ev->width, ev->height);
565 return 0;
568 /** The key press event handler.
569 * \param data currently unused.
570 * \param connection The connection to the X server.
571 * \param ev The event.
573 static int
574 event_handle_key(void *data __attribute__ ((unused)),
575 xcb_connection_t *connection __attribute__ ((unused)),
576 xcb_key_press_event_t *ev)
578 if(globalconf.keygrabber != LUA_REFNIL)
580 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
581 if(keygrabber_handlekpress(globalconf.L, ev))
583 if(lua_pcall(globalconf.L, 3, 1, 0))
585 warn("error running function: %s", lua_tostring(globalconf.L, -1));
586 luaA_keygrabber_stop(globalconf.L);
588 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
589 luaA_keygrabber_stop(globalconf.L);
591 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
593 else
595 /* get keysym ignoring all modifiers */
596 xcb_keysym_t keysym = key_getkeysym(ev->detail, 0);
597 client_t *c;
598 if((c = client_getbywin(ev->event)))
600 client_push(globalconf.L, c);
601 if(!event_key_callback(ev, c->keys, -1, 1, &keysym))
602 if(!event_key_callback(ev, globalconf.keys, 0, 0, &keysym))
603 xcb_allow_events(globalconf.connection,
604 XCB_ALLOW_REPLAY_KEYBOARD,
605 XCB_CURRENT_TIME);
608 else
609 event_key_callback(ev, globalconf.keys, 0, 0, &keysym);
611 xcb_allow_events(globalconf.connection,
612 XCB_ALLOW_SYNC_KEYBOARD,
613 XCB_CURRENT_TIME);
616 return 0;
619 /** The map request event handler.
620 * \param data currently unused.
621 * \param connection The connection to the X server.
622 * \param ev The event.
624 static int
625 event_handle_maprequest(void *data __attribute__ ((unused)),
626 xcb_connection_t *connection, xcb_map_request_event_t *ev)
628 int phys_screen, ret = 0;
629 client_t *c;
630 xcb_get_window_attributes_cookie_t wa_c;
631 xcb_get_window_attributes_reply_t *wa_r;
632 xcb_get_geometry_cookie_t geom_c;
633 xcb_get_geometry_reply_t *geom_r;
635 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
637 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
638 return -1;
640 if(wa_r->override_redirect)
641 goto bailout;
643 if(xembed_getbywin(&globalconf.embedded, ev->window))
645 xcb_map_window(connection, ev->window);
646 xembed_window_activate(connection, ev->window);
648 else if((c = client_getbywin(ev->window)))
650 /* Check that it may be visible, but not asked to be hidden */
651 if(client_maybevisible(c, c->screen) && !c->ishidden)
653 client_setminimized(c, false);
654 /* it will be raised, so just update ourself */
655 client_raise(c);
658 else
660 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
662 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
664 ret = -1;
665 goto bailout;
668 phys_screen = xutil_root2screen(connection, geom_r->root);
670 client_manage(ev->window, geom_r, phys_screen, false);
672 p_delete(&geom_r);
675 bailout:
676 p_delete(&wa_r);
677 return ret;
680 /** The unmap notify event handler.
681 * \param data currently unused.
682 * \param connection The connection to the X server.
683 * \param ev The event.
685 static int
686 event_handle_unmapnotify(void *data __attribute__ ((unused)),
687 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
689 client_t *c;
691 if((c = client_getbywin(ev->window)))
693 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
694 && XCB_EVENT_SENT(ev)
695 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
696 client_unmanage(c);
698 else
699 for(int i = 0; i < globalconf.embedded.len; i++)
700 if(globalconf.embedded.tab[i].win == ev->window)
702 xembed_window_array_take(&globalconf.embedded, i);
703 widget_invalidate_bytype(widget_systray);
706 return 0;
709 /** The randr screen change notify event handler.
710 * \param data currently unused.
711 * \param connection The connection to the X server.
712 * \param ev The event.
714 static int
715 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
716 xcb_connection_t *connection __attribute__ ((unused)),
717 xcb_randr_screen_change_notify_event_t *ev)
719 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
720 * (only the code relevant to RRScreenChangeNotify) as the latter
721 * doesn't provide this kind of function */
722 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
723 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
724 ev->mheight, ev->mwidth);
725 else
726 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
727 ev->mwidth, ev->mheight);
729 /* XRRUpdateConfiguration also executes the following instruction
730 * but it's not useful because SubpixelOrder is not used at all at
731 * the moment
733 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
736 awesome_restart();
738 return 0;
741 /** The client message event handler.
742 * \param data currently unused.
743 * \param connection The connection to the X server.
744 * \param ev The event.
746 static int
747 event_handle_clientmessage(void *data __attribute__ ((unused)),
748 xcb_connection_t *connection,
749 xcb_client_message_event_t *ev)
751 /* check for startup notification messages */
752 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
753 return 0;
755 if(ev->type == WM_CHANGE_STATE)
757 client_t *c;
758 if((c = client_getbywin(ev->window))
759 && ev->format == 32
760 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
761 client_setminimized(c, true);
763 else if(ev->type == _XEMBED)
764 return xembed_process_client_message(ev);
765 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
766 return systray_process_client_message(ev);
767 return ewmh_process_client_message(ev);
770 /** The keymap change notify event handler.
771 * \param data Unused data.
772 * \param connection The connection to the X server.
773 * \param ev The event.
774 * \return Status code, 0 if everything's fine.
776 static int
777 event_handle_mappingnotify(void *data,
778 xcb_connection_t *connection,
779 xcb_mapping_notify_event_t *ev)
781 if(ev->request == XCB_MAPPING_MODIFIER
782 || ev->request == XCB_MAPPING_KEYBOARD)
784 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
785 xcb_get_modifier_mapping_unchecked(globalconf.connection);
787 /* Free and then allocate the key symbols */
788 xcb_key_symbols_free(globalconf.keysyms);
789 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
791 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
792 globalconf.keysyms, &globalconf.numlockmask,
793 &globalconf.shiftlockmask, &globalconf.capslockmask,
794 &globalconf.modeswitchmask);
797 return 0;
800 static int
801 event_handle_reparentnotify(void *data,
802 xcb_connection_t *connection,
803 xcb_reparent_notify_event_t *ev)
805 client_t *c;
807 if((c = client_getbywin(ev->window)))
808 client_unmanage(c);
810 return 0;
813 void a_xcb_set_event_handlers(void)
815 const xcb_query_extension_reply_t *randr_query;
817 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
818 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
819 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
820 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
821 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
822 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
823 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
824 xcb_event_set_focus_in_handler(&globalconf.evenths, event_handle_focusin, NULL);
825 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
826 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
827 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
828 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
829 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
830 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
831 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
832 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
833 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
835 /* check for randr extension */
836 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
837 if(randr_query->present)
838 xcb_event_set_handler(&globalconf.evenths,
839 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
840 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
841 NULL);
845 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80