change codename
[awesome.git] / event.c
blob8527ec5a5b0fcab2ecb01ca765a88a9d0c6a6272
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 "property.h"
33 #include "ewmh.h"
34 #include "client.h"
35 #include "widget.h"
36 #include "titlebar.h"
37 #include "key.h"
38 #include "keygrabber.h"
39 #include "mousegrabber.h"
40 #include "luaa.h"
41 #include "systray.h"
42 #include "screen.h"
43 #include "common/atoms.h"
44 #include "common/xutil.h"
46 #define DO_EVENT_HOOK_CALLBACK(type, xcbtype, xcbeventprefix, arraytype, match) \
47 static void \
48 event_##xcbtype##_callback(xcb_##xcbtype##_press_event_t *ev, \
49 arraytype *arr, \
50 int oud, \
51 int nargs, \
52 void *data) \
53 { \
54 int abs_oud = oud < 0 ? ((lua_gettop(globalconf.L) + 1) + oud) : oud; \
55 int item_matching = 0; \
56 foreach(item, *arr) \
57 if(match(ev, *item, data)) \
58 { \
59 if(oud) \
60 luaA_object_push_item(globalconf.L, abs_oud, *item); \
61 else \
62 luaA_object_push(globalconf.L, *item); \
63 item_matching++; \
64 } \
65 for(; item_matching > 0; item_matching--) \
66 { \
67 switch(ev->response_type) \
68 { \
69 case xcbeventprefix##_PRESS: \
70 for(int i = 0; i < nargs; i++) \
71 lua_pushvalue(globalconf.L, - nargs - item_matching); \
72 luaA_object_emit_signal(globalconf.L, - nargs - 1, "press", nargs); \
73 break; \
74 case xcbeventprefix##_RELEASE: \
75 for(int i = 0; i < nargs; i++) \
76 lua_pushvalue(globalconf.L, - nargs - item_matching); \
77 luaA_object_emit_signal(globalconf.L, - nargs - 1, "release", nargs); \
78 break; \
79 } \
80 lua_pop(globalconf.L, 1); \
81 } \
82 lua_pop(globalconf.L, nargs); \
85 static bool
86 event_key_match(xcb_key_press_event_t *ev, keyb_t *k, void *data)
88 assert(data);
89 xcb_keysym_t keysym = *(xcb_keysym_t *) data;
90 return (((k->keycode && ev->detail == k->keycode)
91 || (k->keysym && keysym == k->keysym))
92 && (k->modifiers == XCB_BUTTON_MASK_ANY || k->modifiers == ev->state));
95 static bool
96 event_button_match(xcb_button_press_event_t *ev, button_t *b, void *data)
98 return ((!b->button || ev->detail == b->button)
99 && (b->modifiers == XCB_BUTTON_MASK_ANY || b->modifiers == ev->state));
102 DO_EVENT_HOOK_CALLBACK(button_t, button, XCB_BUTTON, button_array_t, event_button_match)
103 DO_EVENT_HOOK_CALLBACK(keyb_t, key, XCB_KEY, key_array_t, event_key_match)
105 /** Handle an event with mouse grabber if needed
106 * \param x The x coordinate.
107 * \param y The y coordinate.
108 * \param mask The mask buttons.
109 * \return True if the event was handled.
111 static bool
112 event_handle_mousegrabber(int x, int y, uint16_t mask)
114 if(globalconf.mousegrabber != LUA_REFNIL)
116 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
117 mousegrabber_handleevent(globalconf.L, x, y, mask);
118 if(lua_pcall(globalconf.L, 1, 1, 0))
120 warn("error running function: %s", lua_tostring(globalconf.L, -1));
121 luaA_mousegrabber_stop(globalconf.L);
123 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
124 luaA_mousegrabber_stop(globalconf.L);
125 lua_pop(globalconf.L, 1); /* pop returned value */
126 return true;
128 return false;
131 /** The button press event handler.
132 * \param ev The event.
134 static void
135 event_handle_button(xcb_button_press_event_t *ev)
137 int screen;
138 const int nb_screen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
139 client_t *c;
140 wibox_t *wibox;
142 globalconf.timestamp = ev->time;
144 if(event_handle_mousegrabber(ev->root_x, ev->root_y, 1 << (ev->detail - 1 + 8)))
145 return;
147 /* ev->state is
148 * button status (8 bits) + modifiers status (8 bits)
149 * we don't care for button status that we get, especially on release, so
150 * drop them */
151 ev->state &= 0x00ff;
153 if((wibox = wibox_getbywin(ev->event))
154 || (wibox = wibox_getbywin(ev->child)))
156 /* If the wibox is child, then x,y are
157 * relative to root window */
158 if(wibox->window == ev->child)
160 ev->event_x -= wibox->geometry.x;
161 ev->event_y -= wibox->geometry.y;
164 /* Push the wibox */
165 luaA_object_push(globalconf.L, wibox);
166 /* Duplicate the wibox */
167 lua_pushvalue(globalconf.L, -1);
168 /* Handle the button event on it */
169 event_button_callback(ev, &wibox->buttons, -1, 1, NULL);
171 /* then try to match a widget binding */
172 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
173 wibox->geometry.width,
174 wibox->geometry.height,
175 &ev->event_x, &ev->event_y);
176 if(w)
178 /* Push widget (the wibox is already on stack) */
179 luaA_object_push_item(globalconf.L, -1, w);
180 /* Move widget before wibox */
181 lua_insert(globalconf.L, -2);
182 event_button_callback(ev, &w->buttons, -2, 2, NULL);
184 else
185 /* Remove the wibox, we did not use it */
186 lua_pop(globalconf.L, 1);
189 else if((c = client_getbywin(ev->event)))
191 luaA_object_push(globalconf.L, c);
192 event_button_callback(ev, &c->buttons, -1, 1, NULL);
193 xcb_allow_events(globalconf.connection,
194 XCB_ALLOW_REPLAY_POINTER,
195 XCB_CURRENT_TIME);
197 else if(ev->child == XCB_NONE)
198 for(screen = 0; screen < nb_screen; screen++)
199 if(xutil_screen_get(globalconf.connection, screen)->root == ev->event)
201 event_button_callback(ev, &globalconf.buttons, 0, 0, NULL);
202 return;
206 static void
207 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
209 uint16_t config_win_mask = 0;
210 uint32_t config_win_vals[7];
211 unsigned short i = 0;
213 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
215 config_win_mask |= XCB_CONFIG_WINDOW_X;
216 config_win_vals[i++] = ev->x;
218 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
220 config_win_mask |= XCB_CONFIG_WINDOW_Y;
221 config_win_vals[i++] = ev->y;
223 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
225 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
226 config_win_vals[i++] = ev->width;
228 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
230 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
231 config_win_vals[i++] = ev->height;
233 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
235 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
236 config_win_vals[i++] = ev->border_width;
238 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
240 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
241 config_win_vals[i++] = ev->sibling;
243 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
245 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
246 config_win_vals[i++] = ev->stack_mode;
249 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
252 /** The configure event handler.
253 * \param ev The event.
255 static void
256 event_handle_configurerequest(xcb_configure_request_event_t *ev)
258 client_t *c;
259 area_t geometry;
261 if((c = client_getbywin(ev->window)))
263 geometry = titlebar_geometry_remove(c->titlebar, c->border_width, c->geometry);
265 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
266 geometry.x = ev->x;
267 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
268 geometry.y = ev->y;
269 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
270 geometry.width = ev->width;
271 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
272 geometry.height = ev->height;
274 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
276 luaA_object_push(globalconf.L, c);
277 client_set_border_width(globalconf.L, -1, ev->border_width);
278 lua_pop(globalconf.L, 1);
281 /* Clients are not allowed to directly mess with stacking parameters. */
282 ev->value_mask &= ~(XCB_CONFIG_WINDOW_SIBLING |
283 XCB_CONFIG_WINDOW_STACK_MODE);
285 /** Configure request are sent with size relative to real (internal)
286 * window size, i.e. without titlebars and borders. */
287 geometry = titlebar_geometry_add(c->titlebar, c->border_width, geometry);
289 if(!client_resize(c, geometry, false))
291 /* Resize wasn't officially needed, but we don't want to break expectations. */
292 geometry = titlebar_geometry_remove(c->titlebar, c->border_width, c->geometry);
293 window_configure(c->window, geometry, c->border_width);
296 else
297 event_handle_configurerequest_configure_window(ev);
300 /** The configure notify event handler.
301 * \param ev The event.
303 static void
304 event_handle_configurenotify(xcb_configure_notify_event_t *ev)
306 int screen_nbr;
307 const xcb_screen_t *screen;
309 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection)); screen_nbr++)
310 if((screen = xutil_screen_get(globalconf.connection, screen_nbr)) != NULL
311 && ev->window == screen->root
312 && (ev->width != screen->width_in_pixels
313 || ev->height != screen->height_in_pixels))
314 /* it's not that we panic, but restart */
315 awesome_restart();
318 /** The destroy notify event handler.
319 * \param ev The event.
321 static void
322 event_handle_destroynotify(xcb_destroy_notify_event_t *ev)
324 client_t *c;
326 if((c = client_getbywin(ev->window)))
327 client_unmanage(c);
328 else
329 for(int i = 0; i < globalconf.embedded.len; i++)
330 if(globalconf.embedded.tab[i].win == ev->window)
332 xembed_window_array_take(&globalconf.embedded, i);
333 widget_invalidate_bytype(widget_systray);
337 /** Handle a motion notify over widgets.
338 * \param wibox The wibox.
339 * \param mouse_over The pointer to the registered mouse over widget.
340 * \param widget The new widget the mouse is over.
342 static void
343 event_handle_widget_motionnotify(wibox_t *wibox,
344 widget_t *widget)
346 if(widget != wibox->mouse_over)
348 if(wibox->mouse_over)
350 /* Emit mouse leave signal on old widget:
351 * - Push wibox.*/
352 luaA_object_push(globalconf.L, wibox);
353 /* - Push the widget the mouse was on */
354 luaA_object_push_item(globalconf.L, -1, wibox->mouse_over);
355 /* - Invert wibox/widget */
356 lua_insert(globalconf.L, -2);
357 /* - Emit the signal mouse::leave with the wibox as argument */
358 luaA_object_emit_signal(globalconf.L, -2, "mouse::leave", 1);
359 /* - Remove the widget */
360 lua_pop(globalconf.L, 1);
362 /* Re-push wibox */
363 luaA_object_push(globalconf.L, wibox);
364 luaA_object_unref_item(globalconf.L, -1, wibox->mouse_over);
365 /* Remove the wibox */
366 lua_pop(globalconf.L, 1);
368 if(widget)
370 /* Get a ref on this widget so that it can't be unref'd from
371 * underneath us (-> invalid pointer dereference):
372 * - Push the wibox */
373 luaA_object_push(globalconf.L, wibox);
374 /* - Push the widget */
375 luaA_object_push_item(globalconf.L, -1, widget);
376 /* - Reference the widget into the wibox */
377 luaA_object_ref_item(globalconf.L, -2, -1);
379 /* Emit mouse::enter signal on new widget:
380 * - Push the widget */
381 luaA_object_push_item(globalconf.L, -1, widget);
382 /* - Move the widget before the wibox we pushed just above */
383 lua_insert(globalconf.L, -2);
384 /* - Emit the signal with the wibox as argument */
386 luaA_object_emit_signal(globalconf.L, -2, "mouse::enter", 1);
387 /* - Remove the widget from the stack */
388 lua_pop(globalconf.L, 1);
391 /* Store that this widget was the one with the mouse over */
392 wibox->mouse_over = widget;
397 /** The motion notify event handler.
398 * \param ev The event.
400 static void
401 event_handle_motionnotify(xcb_motion_notify_event_t *ev)
403 wibox_t *wibox;
405 globalconf.timestamp = ev->time;
407 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
408 return;
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);
417 event_handle_widget_motionnotify(wibox, w);
421 /** The leave notify event handler.
422 * \param ev The event.
424 static void
425 event_handle_leavenotify(xcb_leave_notify_event_t *ev)
427 wibox_t *wibox;
428 client_t *c;
430 globalconf.timestamp = ev->time;
432 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
433 return;
435 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
437 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
439 luaA_object_push(globalconf.L, c);
440 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
442 luaA_object_push(globalconf.L, c);
443 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
444 lua_pop(globalconf.L, 1);
447 if((wibox = wibox_getbywin(ev->event)))
449 if(wibox->mouse_over)
451 /* Push the wibox */
452 luaA_object_push(globalconf.L, wibox);
453 /* Push the widget the mouse is over in this wibox */
454 luaA_object_push_item(globalconf.L, -1, wibox->mouse_over);
455 /* Move the widget before the wibox */
456 lua_insert(globalconf.L, -2);
457 /* Emit mouse::leave signal on widget the mouse was over with
458 * its wibox as argument */
459 luaA_object_emit_signal(globalconf.L, -2, "mouse::leave", 1);
460 /* Remove the widget from the stack */
461 lua_pop(globalconf.L, 1);
462 wibox_clear_mouse_over(wibox);
465 luaA_object_push(globalconf.L, wibox);
466 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
467 lua_pop(globalconf.L, 1);
471 /** The enter notify event handler.
472 * \param ev The event.
474 static void
475 event_handle_enternotify(xcb_enter_notify_event_t *ev)
477 client_t *c;
478 wibox_t *wibox;
480 globalconf.timestamp = ev->time;
482 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
483 return;
485 if((wibox = wibox_getbywin(ev->event)))
487 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
488 wibox->geometry.width,
489 wibox->geometry.height,
490 &ev->event_x, &ev->event_y);
492 event_handle_widget_motionnotify(wibox, w);
494 luaA_object_push(globalconf.L, wibox);
495 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
496 lua_pop(globalconf.L, 1);
499 if((c = client_getbytitlebarwin(ev->event))
500 || (c = client_getbywin(ev->event)))
502 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
504 luaA_object_push(globalconf.L, c);
505 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
507 luaA_object_push(globalconf.L, c);
508 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
509 lua_pop(globalconf.L, 1);
513 /** The focus in event handler.
514 * \param ev The event.
516 static void
517 event_handle_focusin(xcb_focus_in_event_t *ev)
519 client_t *c;
521 /* Events that we are interested in: */
522 switch(ev->detail)
524 /* These are events that jump between root windows.
526 case XCB_NOTIFY_DETAIL_ANCESTOR:
527 case XCB_NOTIFY_DETAIL_INFERIOR:
529 /* These are events that jump between clients.
530 * Virtual events ensure we always get an event on our top-level window.
532 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
533 case XCB_NOTIFY_DETAIL_NONLINEAR:
534 if((c = client_getbytitlebarwin(ev->event))
535 || (c = client_getbywin(ev->event)))
536 client_focus_update(c);
537 /* all other events are ignored */
538 default:
539 break;
543 /** The expose event handler.
544 * \param ev The event.
546 static void
547 event_handle_expose(xcb_expose_event_t *ev)
549 wibox_t *wibox;
551 /* If the wibox got need_update set, skip this because it will be repainted
552 * soon anyway. Without this we could be painting garbage to the screen!
554 if((wibox = wibox_getbywin(ev->window)) && !wibox->need_update)
555 wibox_refresh_pixmap_partial(wibox,
556 ev->x, ev->y,
557 ev->width, ev->height);
560 /** The key press event handler.
561 * \param ev The event.
563 static void
564 event_handle_key(xcb_key_press_event_t *ev)
566 globalconf.timestamp = ev->time;
568 if(globalconf.keygrabber != LUA_REFNIL)
570 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
571 if(keygrabber_handlekpress(globalconf.L, ev))
573 if(lua_pcall(globalconf.L, 3, 1, 0))
575 warn("error running function: %s", lua_tostring(globalconf.L, -1));
576 luaA_keygrabber_stop(globalconf.L);
578 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
579 luaA_keygrabber_stop(globalconf.L);
581 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
583 else
585 /* get keysym ignoring all modifiers */
586 xcb_keysym_t keysym = key_getkeysym(ev->detail, 0);
587 client_t *c;
588 if((c = client_getbywin(ev->event)))
590 luaA_object_push(globalconf.L, c);
591 event_key_callback(ev, &c->keys, -1, 1, &keysym);
593 else
594 event_key_callback(ev, &globalconf.keys, 0, 0, &keysym);
598 /** The map request event handler.
599 * \param ev The event.
601 static void
602 event_handle_maprequest(xcb_map_request_event_t *ev)
604 int phys_screen;
605 client_t *c;
606 xcb_get_window_attributes_cookie_t wa_c;
607 xcb_get_window_attributes_reply_t *wa_r;
608 xcb_get_geometry_cookie_t geom_c;
609 xcb_get_geometry_reply_t *geom_r;
611 wa_c = xcb_get_window_attributes_unchecked(globalconf.connection, ev->window);
613 if(!(wa_r = xcb_get_window_attributes_reply(globalconf.connection, wa_c, NULL)))
614 return;
616 if(wa_r->override_redirect)
617 goto bailout;
619 if(xembed_getbywin(&globalconf.embedded, ev->window))
621 xcb_map_window(globalconf.connection, ev->window);
622 xembed_window_activate(globalconf.connection, ev->window);
624 else if((c = client_getbywin(ev->window)))
626 /* Check that it may be visible, but not asked to be hidden */
627 if(client_maybevisible(c, c->screen) && !c->hidden)
629 luaA_object_push(globalconf.L, c);
630 client_set_minimized(globalconf.L, -1, false);
631 lua_pop(globalconf.L, 1);
632 /* it will be raised, so just update ourself */
633 client_raise(c);
636 else
638 geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
640 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
642 goto bailout;
645 phys_screen = xutil_root2screen(globalconf.connection, geom_r->root);
647 client_manage(ev->window, geom_r, phys_screen, false);
649 p_delete(&geom_r);
652 bailout:
653 p_delete(&wa_r);
656 /** The unmap notify event handler.
657 * \param ev The event.
659 static void
660 event_handle_unmapnotify(xcb_unmap_notify_event_t *ev)
662 client_t *c;
664 if((c = client_getbywin(ev->window)))
666 if(ev->event == xutil_screen_get(globalconf.connection, c->phys_screen)->root
667 && XCB_EVENT_SENT(ev))
669 client_unmanage(c);
670 xcb_unmap_window(globalconf.connection, ev->window);
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 widget_invalidate_bytype(widget_systray);
679 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, ev->window);
683 /** The randr screen change notify event handler.
684 * \param ev The event.
686 static void
687 event_handle_randr_screen_change_notify(xcb_randr_screen_change_notify_event_t *ev)
689 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
690 * (only the code relevant to RRScreenChangeNotify) as the latter
691 * doesn't provide this kind of function */
692 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
693 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->height, ev->width,
694 ev->mheight, ev->mwidth);
695 else
696 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->width, ev->height,
697 ev->mwidth, ev->mheight);
699 /* XRRUpdateConfiguration also executes the following instruction
700 * but it's not useful because SubpixelOrder is not used at all at
701 * the moment
703 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
706 awesome_restart();
709 /** The client message event handler.
710 * \param ev The event.
712 static void
713 event_handle_clientmessage(xcb_client_message_event_t *ev)
715 /* check for startup notification messages */
716 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
717 return;
719 if(ev->type == WM_CHANGE_STATE)
721 client_t *c;
722 if((c = client_getbywin(ev->window))
723 && ev->format == 32
724 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
726 luaA_object_push(globalconf.L, c);
727 client_set_minimized(globalconf.L, -1, true);
728 lua_pop(globalconf.L, 1);
731 else if(ev->type == _XEMBED)
732 xembed_process_client_message(ev);
733 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
734 systray_process_client_message(ev);
735 else
736 ewmh_process_client_message(ev);
739 /** The keymap change notify event handler.
740 * \param ev The event.
742 static void
743 event_handle_mappingnotify(xcb_mapping_notify_event_t *ev)
745 if(ev->request == XCB_MAPPING_MODIFIER
746 || ev->request == XCB_MAPPING_KEYBOARD)
748 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
749 xcb_get_modifier_mapping_unchecked(globalconf.connection);
751 /* Free and then allocate the key symbols */
752 xcb_key_symbols_free(globalconf.keysyms);
753 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
755 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
756 globalconf.keysyms, &globalconf.numlockmask,
757 &globalconf.shiftlockmask, &globalconf.capslockmask,
758 &globalconf.modeswitchmask);
760 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
762 /* regrab everything */
763 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
765 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
766 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
767 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
768 window_grabkeys(s->root, &globalconf.keys);
771 foreach(_c, globalconf.clients)
773 client_t *c = *_c;
774 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->window, XCB_BUTTON_MASK_ANY);
775 window_grabkeys(c->window, &c->keys);
780 static void
781 event_handle_reparentnotify(xcb_reparent_notify_event_t *ev)
783 client_t *c;
785 if((c = client_getbywin(ev->window)))
786 client_unmanage(c);
789 /** \brief awesome xerror function.
790 * There's no way to check accesses to destroyed windows, thus those cases are
791 * ignored (especially on UnmapNotify's).
792 * \param e The error event.
794 static void
795 xerror(xcb_generic_error_t *e)
797 /* ignore this */
798 if(e->error_code == XCB_EVENT_ERROR_BAD_WINDOW
799 || (e->error_code == XCB_EVENT_ERROR_BAD_MATCH
800 && e->major_code == XCB_SET_INPUT_FOCUS)
801 || (e->error_code == XCB_EVENT_ERROR_BAD_VALUE
802 && e->major_code == XCB_KILL_CLIENT)
803 || (e->major_code == XCB_CONFIGURE_WINDOW
804 && e->error_code == XCB_EVENT_ERROR_BAD_MATCH))
805 return;
807 warn("X error: request=%s, error=%s",
808 xcb_event_get_request_label(e->major_code),
809 xcb_event_get_error_label(e->error_code));
811 return;
814 void event_handle(xcb_generic_event_t *event)
816 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
818 if(response_type == 0)
820 /* This is an error, not a event */
821 xerror((xcb_generic_error_t *) event);
822 return;
825 switch(response_type)
827 #define EVENT(type, callback) case type: callback((void *) event); return
828 EVENT(XCB_BUTTON_PRESS, event_handle_button);
829 EVENT(XCB_BUTTON_RELEASE, event_handle_button);
830 EVENT(XCB_CONFIGURE_REQUEST, event_handle_configurerequest);
831 EVENT(XCB_CONFIGURE_NOTIFY, event_handle_configurenotify);
832 EVENT(XCB_DESTROY_NOTIFY, event_handle_destroynotify);
833 EVENT(XCB_ENTER_NOTIFY, event_handle_enternotify);
834 EVENT(XCB_CLIENT_MESSAGE, event_handle_clientmessage);
835 EVENT(XCB_EXPOSE, event_handle_expose);
836 EVENT(XCB_FOCUS_IN, event_handle_focusin);
837 EVENT(XCB_KEY_PRESS, event_handle_key);
838 EVENT(XCB_KEY_RELEASE, event_handle_key);
839 EVENT(XCB_LEAVE_NOTIFY, event_handle_leavenotify);
840 EVENT(XCB_MAPPING_NOTIFY, event_handle_mappingnotify);
841 EVENT(XCB_MAP_REQUEST, event_handle_maprequest);
842 EVENT(XCB_MOTION_NOTIFY, event_handle_motionnotify);
843 EVENT(XCB_PROPERTY_NOTIFY, property_handle_propertynotify);
844 EVENT(XCB_REPARENT_NOTIFY, event_handle_reparentnotify);
845 EVENT(XCB_UNMAP_NOTIFY, event_handle_unmapnotify);
846 #undef EVENT
849 static uint8_t randr_screen_change_notify = 0;
851 if(randr_screen_change_notify == 0)
853 /* check for randr extension */
854 const xcb_query_extension_reply_t *randr_query;
855 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
856 if(randr_query->present)
857 randr_screen_change_notify = randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY;
860 if (response_type == randr_screen_change_notify)
861 event_handle_randr_screen_change_notify((void *) event);
864 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80