client: _really_ honor size hints
[awesome.git] / event.c
blobbda0cc5a26cbbe71135fb8c4dfc0d5d399d0586b
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 luaA_object_unref(globalconf.L, *mouse_over);
364 *mouse_over = NULL;
366 if(widget)
368 /* Get a ref on this widget so that it can't be unref'd from
369 * underneath us (-> invalid pointer dereference). */
370 luaA_object_push(globalconf.L, widget);
371 luaA_object_ref(globalconf.L, -1);
372 *mouse_over = widget;
374 /* emit mouse::enter signal on new widget */
375 luaA_object_push(globalconf.L, widget);
376 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
377 lua_pop(globalconf.L, 1);
382 /** The motion notify event handler.
383 * \param data currently unused.
384 * \param connection The connection to the X server.
385 * \param ev The event.
387 static int
388 event_handle_motionnotify(void *data __attribute__ ((unused)),
389 xcb_connection_t *connection,
390 xcb_motion_notify_event_t *ev)
392 wibox_t *wibox;
394 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
395 return 0;
397 if((wibox = wibox_getbywin(ev->event)))
399 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
400 wibox->geometry.width,
401 wibox->geometry.height,
402 &ev->event_x, &ev->event_y);
403 if(w)
404 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
407 return 0;
410 /** The leave notify event handler.
411 * \param data currently unused.
412 * \param connection The connection to the X server.
413 * \param ev The event.
415 static int
416 event_handle_leavenotify(void *data __attribute__ ((unused)),
417 xcb_connection_t *connection,
418 xcb_leave_notify_event_t *ev)
420 wibox_t *wibox;
421 client_t *c;
423 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
424 return 0;
426 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
428 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
430 luaA_object_push(globalconf.L, c);
431 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
433 luaA_object_push(globalconf.L, c);
434 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
435 lua_pop(globalconf.L, 1);
438 if((wibox = wibox_getbywin(ev->event)))
440 if(wibox->mouse_over)
442 luaA_object_push(globalconf.L, wibox->mouse_over);
443 /* emit mouse::leave signal on widget the mouse was over */
444 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
445 lua_pop(globalconf.L, 1);
446 wibox_clear_mouse_over(wibox);
449 luaA_object_push(globalconf.L, wibox);
450 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
451 lua_pop(globalconf.L, 1);
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->orientation, &wibox->widgets,
476 wibox->geometry.width,
477 wibox->geometry.height,
478 &ev->event_x, &ev->event_y);
479 if(w)
480 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
482 luaA_object_push(globalconf.L, wibox);
483 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
484 lua_pop(globalconf.L, 1);
487 if((c = client_getbytitlebarwin(ev->event))
488 || (c = client_getbywin(ev->event)))
490 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
492 luaA_object_push(globalconf.L, c);
493 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
495 luaA_object_push(globalconf.L, c);
496 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
497 lua_pop(globalconf.L, 1);
500 return 0;
503 /** The focus in event handler.
504 * \param data currently unused.
505 * \param connection The connection to the X server.
506 * \param ev The event.
508 static int
509 event_handle_focusin(void *data __attribute__ ((unused)),
510 xcb_connection_t *connection,
511 xcb_focus_in_event_t *ev)
513 client_t *c;
515 /* Events that we are interested in: */
516 switch(ev->detail)
518 /* These are events that jump between root windows.
520 case XCB_NOTIFY_DETAIL_ANCESTOR:
521 case XCB_NOTIFY_DETAIL_INFERIOR:
523 /* These are events that jump between clients.
524 * Virtual events ensure we always get an event on our top-level window.
526 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
527 case XCB_NOTIFY_DETAIL_NONLINEAR:
528 if((c = client_getbytitlebarwin(ev->event))
529 || (c = client_getbywin(ev->event)))
530 client_focus_update(c);
531 /* all other events are ignored */
532 default:
533 break;
535 return 0;
538 /** The expose event handler.
539 * \param data currently unused.
540 * \param connection The connection to the X server.
541 * \param ev The event.
543 static int
544 event_handle_expose(void *data __attribute__ ((unused)),
545 xcb_connection_t *connection __attribute__ ((unused)),
546 xcb_expose_event_t *ev)
548 wibox_t *wibox;
550 /* If the wibox got need_update set, skip this because it will be repainted
551 * soon anyway. Without this we could be painting garbage to the screen!
553 if((wibox = wibox_getbywin(ev->window)) && !wibox->need_update)
554 wibox_refresh_pixmap_partial(wibox,
555 ev->x, ev->y,
556 ev->width, ev->height);
558 return 0;
561 /** The key press event handler.
562 * \param data currently unused.
563 * \param connection The connection to the X server.
564 * \param ev The event.
566 static int
567 event_handle_key(void *data __attribute__ ((unused)),
568 xcb_connection_t *connection __attribute__ ((unused)),
569 xcb_key_press_event_t *ev)
571 if(globalconf.keygrabber != LUA_REFNIL)
573 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
574 if(keygrabber_handlekpress(globalconf.L, ev))
576 if(lua_pcall(globalconf.L, 3, 1, 0))
578 warn("error running function: %s", lua_tostring(globalconf.L, -1));
579 luaA_keygrabber_stop(globalconf.L);
581 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
582 luaA_keygrabber_stop(globalconf.L);
584 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
586 else
588 /* get keysym ignoring all modifiers */
589 xcb_keysym_t keysym = key_getkeysym(ev->detail, 0);
590 client_t *c;
591 if((c = client_getbywin(ev->event)))
593 luaA_object_push(globalconf.L, c);
594 event_key_callback(ev, &c->keys, -1, 1, &keysym);
596 else
597 event_key_callback(ev, &globalconf.keys, 0, 0, &keysym);
600 return 0;
603 /** The map request event handler.
604 * \param data currently unused.
605 * \param connection The connection to the X server.
606 * \param ev The event.
608 static int
609 event_handle_maprequest(void *data __attribute__ ((unused)),
610 xcb_connection_t *connection, xcb_map_request_event_t *ev)
612 int phys_screen, ret = 0;
613 client_t *c;
614 xcb_get_window_attributes_cookie_t wa_c;
615 xcb_get_window_attributes_reply_t *wa_r;
616 xcb_get_geometry_cookie_t geom_c;
617 xcb_get_geometry_reply_t *geom_r;
619 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
621 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
622 return -1;
624 if(wa_r->override_redirect)
625 goto bailout;
627 if(xembed_getbywin(&globalconf.embedded, ev->window))
629 xcb_map_window(connection, ev->window);
630 xembed_window_activate(connection, ev->window);
632 else if((c = client_getbywin(ev->window)))
634 /* Check that it may be visible, but not asked to be hidden */
635 if(client_maybevisible(c, c->screen) && !c->hidden)
637 luaA_object_push(globalconf.L, c);
638 client_set_minimized(globalconf.L, -1, false);
639 lua_pop(globalconf.L, 1);
640 /* it will be raised, so just update ourself */
641 client_raise(c);
644 else
646 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
648 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
650 ret = -1;
651 goto bailout;
654 phys_screen = xutil_root2screen(connection, geom_r->root);
656 client_manage(ev->window, geom_r, phys_screen, false);
658 p_delete(&geom_r);
661 bailout:
662 p_delete(&wa_r);
663 return ret;
666 /** The unmap notify event handler.
667 * \param data currently unused.
668 * \param connection The connection to the X server.
669 * \param ev The event.
671 static int
672 event_handle_unmapnotify(void *data __attribute__ ((unused)),
673 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
675 client_t *c;
677 if((c = client_getbywin(ev->window)))
679 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
680 && XCB_EVENT_SENT(ev))
682 client_unmanage(c);
683 xcb_unmap_window(connection, ev->window);
686 else
687 for(int i = 0; i < globalconf.embedded.len; i++)
688 if(globalconf.embedded.tab[i].win == ev->window)
690 xembed_window_array_take(&globalconf.embedded, i);
691 widget_invalidate_bytype(widget_systray);
694 return 0;
697 /** The randr screen change notify event handler.
698 * \param data currently unused.
699 * \param connection The connection to the X server.
700 * \param ev The event.
702 static int
703 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
704 xcb_connection_t *connection __attribute__ ((unused)),
705 xcb_randr_screen_change_notify_event_t *ev)
707 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
708 * (only the code relevant to RRScreenChangeNotify) as the latter
709 * doesn't provide this kind of function */
710 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
711 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
712 ev->mheight, ev->mwidth);
713 else
714 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
715 ev->mwidth, ev->mheight);
717 /* XRRUpdateConfiguration also executes the following instruction
718 * but it's not useful because SubpixelOrder is not used at all at
719 * the moment
721 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
724 awesome_restart();
726 return 0;
729 /** The client message event handler.
730 * \param data currently unused.
731 * \param connection The connection to the X server.
732 * \param ev The event.
734 static int
735 event_handle_clientmessage(void *data __attribute__ ((unused)),
736 xcb_connection_t *connection,
737 xcb_client_message_event_t *ev)
739 /* check for startup notification messages */
740 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
741 return 0;
743 if(ev->type == WM_CHANGE_STATE)
745 client_t *c;
746 if((c = client_getbywin(ev->window))
747 && ev->format == 32
748 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
750 luaA_object_push(globalconf.L, c);
751 client_set_minimized(globalconf.L, -1, true);
752 lua_pop(globalconf.L, 1);
755 else if(ev->type == _XEMBED)
756 return xembed_process_client_message(ev);
757 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
758 return systray_process_client_message(ev);
759 return ewmh_process_client_message(ev);
762 /** The keymap change notify event handler.
763 * \param data Unused data.
764 * \param connection The connection to the X server.
765 * \param ev The event.
766 * \return Status code, 0 if everything's fine.
768 static int
769 event_handle_mappingnotify(void *data,
770 xcb_connection_t *connection,
771 xcb_mapping_notify_event_t *ev)
773 if(ev->request == XCB_MAPPING_MODIFIER
774 || ev->request == XCB_MAPPING_KEYBOARD)
776 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
777 xcb_get_modifier_mapping_unchecked(globalconf.connection);
779 /* Free and then allocate the key symbols */
780 xcb_key_symbols_free(globalconf.keysyms);
781 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
783 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
784 globalconf.keysyms, &globalconf.numlockmask,
785 &globalconf.shiftlockmask, &globalconf.capslockmask,
786 &globalconf.modeswitchmask);
788 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
790 /* regrab everything */
791 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
793 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
794 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
795 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
796 window_grabkeys(s->root, &globalconf.keys);
799 foreach(_c, globalconf.clients)
801 client_t *c = *_c;
802 xcb_ungrab_key(connection, XCB_GRAB_ANY, c->window, XCB_BUTTON_MASK_ANY);
803 window_grabkeys(c->window, &c->keys);
807 return 0;
810 static int
811 event_handle_reparentnotify(void *data,
812 xcb_connection_t *connection,
813 xcb_reparent_notify_event_t *ev)
815 client_t *c;
817 if((c = client_getbywin(ev->window)))
818 client_unmanage(c);
820 return 0;
823 void a_xcb_set_event_handlers(void)
825 const xcb_query_extension_reply_t *randr_query;
827 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
828 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
829 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
830 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
831 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
832 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
833 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
834 xcb_event_set_focus_in_handler(&globalconf.evenths, event_handle_focusin, NULL);
835 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
836 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
837 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
838 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
839 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
840 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
841 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
842 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
843 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
845 /* check for randr extension */
846 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
847 if(randr_query->present)
848 xcb_event_set_handler(&globalconf.evenths,
849 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
850 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
851 NULL);
855 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80