prompt: Only show error messages
[awesome.git] / event.c
blobf9d78c2a35a4bab0edfe40d2f7725ffccef6014b
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 if(event_handle_mousegrabber(ev->root_x, ev->root_y, 1 << (ev->detail - 1 + 8)))
143 return;
145 /* ev->state is
146 * button status (8 bits) + modifiers status (8 bits)
147 * we don't care for button status that we get, especially on release, so
148 * drop them */
149 ev->state &= 0x00ff;
151 if((wibox = wibox_getbywin(ev->event))
152 || (wibox = wibox_getbywin(ev->child)))
154 /* If the wibox is child, then x,y are
155 * relative to root window */
156 if(wibox->window == ev->child)
158 ev->event_x -= wibox->geometry.x;
159 ev->event_y -= wibox->geometry.y;
162 /* Push the wibox */
163 luaA_object_push(globalconf.L, wibox);
164 /* Duplicate the wibox */
165 lua_pushvalue(globalconf.L, -1);
166 /* Handle the button event on it */
167 event_button_callback(ev, &wibox->buttons, -1, 1, NULL);
169 /* then try to match a widget binding */
170 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
171 wibox->geometry.width,
172 wibox->geometry.height,
173 &ev->event_x, &ev->event_y);
174 if(w)
176 /* Push widget (the wibox is already on stack) */
177 luaA_object_push_item(globalconf.L, -1, w);
178 /* Move widget before wibox */
179 lua_insert(globalconf.L, -2);
180 event_button_callback(ev, &w->buttons, -2, 2, NULL);
182 else
183 /* Remove the wibox, we did not use it */
184 lua_pop(globalconf.L, 1);
187 else if((c = client_getbywin(ev->event)))
189 luaA_object_push(globalconf.L, c);
190 event_button_callback(ev, &c->buttons, -1, 1, NULL);
191 xcb_allow_events(globalconf.connection,
192 XCB_ALLOW_REPLAY_POINTER,
193 XCB_CURRENT_TIME);
195 else if(ev->child == XCB_NONE)
196 for(screen = 0; screen < nb_screen; screen++)
197 if(xutil_screen_get(globalconf.connection, screen)->root == ev->event)
199 event_button_callback(ev, &globalconf.buttons, 0, 0, NULL);
200 return;
204 static void
205 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
207 uint16_t config_win_mask = 0;
208 uint32_t config_win_vals[7];
209 unsigned short i = 0;
211 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
213 config_win_mask |= XCB_CONFIG_WINDOW_X;
214 config_win_vals[i++] = ev->x;
216 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
218 config_win_mask |= XCB_CONFIG_WINDOW_Y;
219 config_win_vals[i++] = ev->y;
221 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
223 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
224 config_win_vals[i++] = ev->width;
226 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
228 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
229 config_win_vals[i++] = ev->height;
231 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
233 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
234 config_win_vals[i++] = ev->border_width;
236 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
238 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
239 config_win_vals[i++] = ev->sibling;
241 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
243 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
244 config_win_vals[i++] = ev->stack_mode;
247 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
250 /** The configure event handler.
251 * \param ev The event.
253 static void
254 event_handle_configurerequest(xcb_configure_request_event_t *ev)
256 client_t *c;
257 area_t geometry;
259 if((c = client_getbywin(ev->window)))
261 geometry = titlebar_geometry_remove(c->titlebar, c->border_width, c->geometry);
263 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
264 geometry.x = ev->x;
265 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
266 geometry.y = ev->y;
267 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
268 geometry.width = ev->width;
269 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
270 geometry.height = ev->height;
272 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
274 luaA_object_push(globalconf.L, c);
275 client_set_border_width(globalconf.L, -1, ev->border_width);
276 lua_pop(globalconf.L, 1);
279 /* Clients are not allowed to directly mess with stacking parameters. */
280 ev->value_mask &= ~(XCB_CONFIG_WINDOW_SIBLING |
281 XCB_CONFIG_WINDOW_STACK_MODE);
283 /** Configure request are sent with size relative to real (internal)
284 * window size, i.e. without titlebars and borders. */
285 geometry = titlebar_geometry_add(c->titlebar, c->border_width, geometry);
287 if(!client_resize(c, geometry, false))
289 /* Resize wasn't officially needed, but we don't want to break expectations. */
290 geometry = titlebar_geometry_remove(c->titlebar, c->border_width, c->geometry);
291 window_configure(c->window, geometry, c->border_width);
294 else
295 event_handle_configurerequest_configure_window(ev);
298 /** The configure notify event handler.
299 * \param ev The event.
301 static void
302 event_handle_configurenotify(xcb_configure_notify_event_t *ev)
304 int screen_nbr;
305 const xcb_screen_t *screen;
307 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection)); screen_nbr++)
308 if((screen = xutil_screen_get(globalconf.connection, screen_nbr)) != NULL
309 && ev->window == screen->root
310 && (ev->width != screen->width_in_pixels
311 || ev->height != screen->height_in_pixels))
312 /* it's not that we panic, but restart */
313 awesome_restart();
316 /** The destroy notify event handler.
317 * \param ev The event.
319 static void
320 event_handle_destroynotify(xcb_destroy_notify_event_t *ev)
322 client_t *c;
324 if((c = client_getbywin(ev->window)))
325 client_unmanage(c);
326 else
327 for(int i = 0; i < globalconf.embedded.len; i++)
328 if(globalconf.embedded.tab[i].win == ev->window)
330 xembed_window_array_take(&globalconf.embedded, i);
331 widget_invalidate_bytype(widget_systray);
335 /** Handle a motion notify over widgets.
336 * \param wibox The wibox.
337 * \param mouse_over The pointer to the registered mouse over widget.
338 * \param widget The new widget the mouse is over.
340 static void
341 event_handle_widget_motionnotify(wibox_t *wibox,
342 widget_t *widget)
344 if(widget != wibox->mouse_over)
346 if(wibox->mouse_over)
348 /* Emit mouse leave signal on old widget:
349 * - Push wibox.*/
350 luaA_object_push(globalconf.L, wibox);
351 /* - Push the widget the mouse was on */
352 luaA_object_push_item(globalconf.L, -1, wibox->mouse_over);
353 /* - Invert wibox/widget */
354 lua_insert(globalconf.L, -2);
355 /* - Emit the signal mouse::leave with the wibox as argument */
356 luaA_object_emit_signal(globalconf.L, -2, "mouse::leave", 1);
357 /* - Remove the widget */
358 lua_pop(globalconf.L, 1);
360 /* Re-push wibox */
361 luaA_object_push(globalconf.L, wibox);
362 luaA_object_unref_item(globalconf.L, -1, wibox->mouse_over);
363 /* Remove the wibox */
364 lua_pop(globalconf.L, 1);
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 * - Push the wibox */
371 luaA_object_push(globalconf.L, wibox);
372 /* - Push the widget */
373 luaA_object_push_item(globalconf.L, -1, widget);
374 /* - Reference the widget into the wibox */
375 luaA_object_ref_item(globalconf.L, -2, -1);
377 /* Emit mouse::enter signal on new widget:
378 * - Push the widget */
379 luaA_object_push_item(globalconf.L, -1, widget);
380 /* - Move the widget before the wibox we pushed just above */
381 lua_insert(globalconf.L, -2);
382 /* - Emit the signal with the wibox as argument */
384 luaA_object_emit_signal(globalconf.L, -2, "mouse::enter", 1);
385 /* - Remove the widget from the stack */
386 lua_pop(globalconf.L, 1);
389 /* Store that this widget was the one with the mouse over */
390 wibox->mouse_over = widget;
395 /** The motion notify event handler.
396 * \param ev The event.
398 static void
399 event_handle_motionnotify(xcb_motion_notify_event_t *ev)
401 wibox_t *wibox;
403 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
404 return;
406 if((wibox = wibox_getbywin(ev->event)))
408 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
409 wibox->geometry.width,
410 wibox->geometry.height,
411 &ev->event_x, &ev->event_y);
413 event_handle_widget_motionnotify(wibox, w);
417 /** The leave notify event handler.
418 * \param ev The event.
420 static void
421 event_handle_leavenotify(xcb_leave_notify_event_t *ev)
423 wibox_t *wibox;
424 client_t *c;
426 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
427 return;
429 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
431 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
433 luaA_object_push(globalconf.L, c);
434 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
436 luaA_object_push(globalconf.L, c);
437 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
438 lua_pop(globalconf.L, 1);
441 if((wibox = wibox_getbywin(ev->event)))
443 if(wibox->mouse_over)
445 /* Push the wibox */
446 luaA_object_push(globalconf.L, wibox);
447 /* Push the widget the mouse is over in this wibox */
448 luaA_object_push_item(globalconf.L, -1, wibox->mouse_over);
449 /* Move the widget before the wibox */
450 lua_insert(globalconf.L, -2);
451 /* Emit mouse::leave signal on widget the mouse was over with
452 * its wibox as argument */
453 luaA_object_emit_signal(globalconf.L, -2, "mouse::leave", 1);
454 /* Remove the widget from the stack */
455 lua_pop(globalconf.L, 1);
456 wibox_clear_mouse_over(wibox);
459 luaA_object_push(globalconf.L, wibox);
460 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
461 lua_pop(globalconf.L, 1);
465 /** The enter notify event handler.
466 * \param ev The event.
468 static void
469 event_handle_enternotify(xcb_enter_notify_event_t *ev)
471 client_t *c;
472 wibox_t *wibox;
474 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
475 return;
477 if((wibox = wibox_getbywin(ev->event)))
479 widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets,
480 wibox->geometry.width,
481 wibox->geometry.height,
482 &ev->event_x, &ev->event_y);
484 event_handle_widget_motionnotify(wibox, w);
486 luaA_object_push(globalconf.L, wibox);
487 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
488 lua_pop(globalconf.L, 1);
491 if((c = client_getbytitlebarwin(ev->event))
492 || (c = client_getbywin(ev->event)))
494 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
496 luaA_object_push(globalconf.L, c);
497 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
499 luaA_object_push(globalconf.L, c);
500 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
501 lua_pop(globalconf.L, 1);
505 /** The focus in event handler.
506 * \param ev The event.
508 static void
509 event_handle_focusin(xcb_focus_in_event_t *ev)
511 client_t *c;
513 /* Events that we are interested in: */
514 switch(ev->detail)
516 /* These are events that jump between root windows.
518 case XCB_NOTIFY_DETAIL_ANCESTOR:
519 case XCB_NOTIFY_DETAIL_INFERIOR:
521 /* These are events that jump between clients.
522 * Virtual events ensure we always get an event on our top-level window.
524 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
525 case XCB_NOTIFY_DETAIL_NONLINEAR:
526 if((c = client_getbytitlebarwin(ev->event))
527 || (c = client_getbywin(ev->event)))
528 client_focus_update(c);
529 /* all other events are ignored */
530 default:
531 break;
535 /** The expose event handler.
536 * \param ev The event.
538 static void
539 event_handle_expose(xcb_expose_event_t *ev)
541 wibox_t *wibox;
543 /* If the wibox got need_update set, skip this because it will be repainted
544 * soon anyway. Without this we could be painting garbage to the screen!
546 if((wibox = wibox_getbywin(ev->window)) && !wibox->need_update)
547 wibox_refresh_pixmap_partial(wibox,
548 ev->x, ev->y,
549 ev->width, ev->height);
552 /** The key press event handler.
553 * \param ev The event.
555 static void
556 event_handle_key(xcb_key_press_event_t *ev)
558 if(globalconf.keygrabber != LUA_REFNIL)
560 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
561 if(keygrabber_handlekpress(globalconf.L, ev))
563 if(lua_pcall(globalconf.L, 3, 1, 0))
565 warn("error running function: %s", lua_tostring(globalconf.L, -1));
566 luaA_keygrabber_stop(globalconf.L);
568 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
569 luaA_keygrabber_stop(globalconf.L);
571 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
573 else
575 /* get keysym ignoring all modifiers */
576 xcb_keysym_t keysym = key_getkeysym(ev->detail, 0);
577 client_t *c;
578 if((c = client_getbywin(ev->event)))
580 luaA_object_push(globalconf.L, c);
581 event_key_callback(ev, &c->keys, -1, 1, &keysym);
583 else
584 event_key_callback(ev, &globalconf.keys, 0, 0, &keysym);
588 /** The map request event handler.
589 * \param ev The event.
591 static void
592 event_handle_maprequest(xcb_map_request_event_t *ev)
594 int phys_screen;
595 client_t *c;
596 xcb_get_window_attributes_cookie_t wa_c;
597 xcb_get_window_attributes_reply_t *wa_r;
598 xcb_get_geometry_cookie_t geom_c;
599 xcb_get_geometry_reply_t *geom_r;
601 wa_c = xcb_get_window_attributes_unchecked(globalconf.connection, ev->window);
603 if(!(wa_r = xcb_get_window_attributes_reply(globalconf.connection, wa_c, NULL)))
604 return;
606 if(wa_r->override_redirect)
607 goto bailout;
609 if(xembed_getbywin(&globalconf.embedded, ev->window))
611 xcb_map_window(globalconf.connection, ev->window);
612 xembed_window_activate(globalconf.connection, ev->window);
614 else if((c = client_getbywin(ev->window)))
616 /* Check that it may be visible, but not asked to be hidden */
617 if(client_maybevisible(c, c->screen) && !c->hidden)
619 luaA_object_push(globalconf.L, c);
620 client_set_minimized(globalconf.L, -1, false);
621 lua_pop(globalconf.L, 1);
622 /* it will be raised, so just update ourself */
623 client_raise(c);
626 else
628 geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
630 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
632 goto bailout;
635 phys_screen = xutil_root2screen(globalconf.connection, geom_r->root);
637 client_manage(ev->window, geom_r, phys_screen, false);
639 p_delete(&geom_r);
642 bailout:
643 p_delete(&wa_r);
646 /** The unmap notify event handler.
647 * \param ev The event.
649 static void
650 event_handle_unmapnotify(xcb_unmap_notify_event_t *ev)
652 client_t *c;
654 if((c = client_getbywin(ev->window)))
656 if(ev->event == xutil_screen_get(globalconf.connection, c->phys_screen)->root
657 && XCB_EVENT_SENT(ev))
659 client_unmanage(c);
660 xcb_unmap_window(globalconf.connection, ev->window);
663 else
664 for(int i = 0; i < globalconf.embedded.len; i++)
665 if(globalconf.embedded.tab[i].win == ev->window)
667 xembed_window_array_take(&globalconf.embedded, i);
668 widget_invalidate_bytype(widget_systray);
669 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, ev->window);
673 /** The randr screen change notify event handler.
674 * \param ev The event.
676 static void
677 event_handle_randr_screen_change_notify(xcb_randr_screen_change_notify_event_t *ev)
679 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
680 * (only the code relevant to RRScreenChangeNotify) as the latter
681 * doesn't provide this kind of function */
682 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
683 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->height, ev->width,
684 ev->mheight, ev->mwidth);
685 else
686 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->width, ev->height,
687 ev->mwidth, ev->mheight);
689 /* XRRUpdateConfiguration also executes the following instruction
690 * but it's not useful because SubpixelOrder is not used at all at
691 * the moment
693 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
696 awesome_restart();
699 /** The client message event handler.
700 * \param ev The event.
702 static void
703 event_handle_clientmessage(xcb_client_message_event_t *ev)
705 /* check for startup notification messages */
706 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
707 return;
709 if(ev->type == WM_CHANGE_STATE)
711 client_t *c;
712 if((c = client_getbywin(ev->window))
713 && ev->format == 32
714 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
716 luaA_object_push(globalconf.L, c);
717 client_set_minimized(globalconf.L, -1, true);
718 lua_pop(globalconf.L, 1);
721 else if(ev->type == _XEMBED)
722 xembed_process_client_message(ev);
723 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
724 systray_process_client_message(ev);
725 else
726 ewmh_process_client_message(ev);
729 /** The keymap change notify event handler.
730 * \param ev The event.
732 static void
733 event_handle_mappingnotify(xcb_mapping_notify_event_t *ev)
735 if(ev->request == XCB_MAPPING_MODIFIER
736 || ev->request == XCB_MAPPING_KEYBOARD)
738 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
739 xcb_get_modifier_mapping_unchecked(globalconf.connection);
741 /* Free and then allocate the key symbols */
742 xcb_key_symbols_free(globalconf.keysyms);
743 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
745 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
746 globalconf.keysyms, &globalconf.numlockmask,
747 &globalconf.shiftlockmask, &globalconf.capslockmask,
748 &globalconf.modeswitchmask);
750 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
752 /* regrab everything */
753 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
755 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
756 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
757 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
758 window_grabkeys(s->root, &globalconf.keys);
761 foreach(_c, globalconf.clients)
763 client_t *c = *_c;
764 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->window, XCB_BUTTON_MASK_ANY);
765 window_grabkeys(c->window, &c->keys);
770 static void
771 event_handle_reparentnotify(xcb_reparent_notify_event_t *ev)
773 client_t *c;
775 if((c = client_getbywin(ev->window)))
776 client_unmanage(c);
779 /** \brief awesome xerror function.
780 * There's no way to check accesses to destroyed windows, thus those cases are
781 * ignored (especially on UnmapNotify's).
782 * \param e The error event.
784 static void
785 xerror(xcb_generic_error_t *e)
787 /* ignore this */
788 if(e->error_code == XCB_EVENT_ERROR_BAD_WINDOW
789 || (e->error_code == XCB_EVENT_ERROR_BAD_MATCH
790 && e->major_code == XCB_SET_INPUT_FOCUS)
791 || (e->error_code == XCB_EVENT_ERROR_BAD_VALUE
792 && e->major_code == XCB_KILL_CLIENT)
793 || (e->major_code == XCB_CONFIGURE_WINDOW
794 && e->error_code == XCB_EVENT_ERROR_BAD_MATCH))
795 return;
797 warn("X error: request=%s, error=%s",
798 xcb_event_get_request_label(e->major_code),
799 xcb_event_get_error_label(e->error_code));
801 return;
804 void event_handle(xcb_generic_event_t *event)
806 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
808 if(response_type == 0)
810 /* This is an error, not a event */
811 xerror((xcb_generic_error_t *) event);
812 return;
815 switch(response_type)
817 #define EVENT(type, callback) case type: callback((void *) event); return
818 EVENT(XCB_BUTTON_PRESS, event_handle_button);
819 EVENT(XCB_BUTTON_RELEASE, event_handle_button);
820 EVENT(XCB_CONFIGURE_REQUEST, event_handle_configurerequest);
821 EVENT(XCB_CONFIGURE_NOTIFY, event_handle_configurenotify);
822 EVENT(XCB_DESTROY_NOTIFY, event_handle_destroynotify);
823 EVENT(XCB_ENTER_NOTIFY, event_handle_enternotify);
824 EVENT(XCB_CLIENT_MESSAGE, event_handle_clientmessage);
825 EVENT(XCB_EXPOSE, event_handle_expose);
826 EVENT(XCB_FOCUS_IN, event_handle_focusin);
827 EVENT(XCB_KEY_PRESS, event_handle_key);
828 EVENT(XCB_KEY_RELEASE, event_handle_key);
829 EVENT(XCB_LEAVE_NOTIFY, event_handle_leavenotify);
830 EVENT(XCB_MAPPING_NOTIFY, event_handle_mappingnotify);
831 EVENT(XCB_MAP_REQUEST, event_handle_maprequest);
832 EVENT(XCB_MOTION_NOTIFY, event_handle_motionnotify);
833 EVENT(XCB_PROPERTY_NOTIFY, property_handle_propertynotify);
834 EVENT(XCB_REPARENT_NOTIFY, event_handle_reparentnotify);
835 EVENT(XCB_UNMAP_NOTIFY, event_handle_unmapnotify);
836 #undef EVENT
839 static uint8_t randr_screen_change_notify = 0;
841 if(randr_screen_change_notify == 0)
843 /* check for randr extension */
844 const xcb_query_extension_reply_t *randr_query;
845 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
846 if(randr_query->present)
847 randr_screen_change_notify = randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY;
850 if (response_type == randr_screen_change_notify)
851 event_handle_randr_screen_change_notify((void *) event);
854 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80