Menubar utils fix the wm_name matching once more.
[awesome.git] / event.c
blobaf08cfd9cf9e30d5cada4905b3d37f119695c301
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 "property.h"
31 #include "objects/tag.h"
32 #include "objects/drawin.h"
33 #include "xwindow.h"
34 #include "ewmh.h"
35 #include "objects/client.h"
36 #include "keyresolv.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 luaA_mouse_pushstatus(globalconf.L, x, y);
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 /** Emit a button signal.
131 * The top of the lua stack has to be the object on which to emit the event.
132 * \param ev The event to handle.
134 static void
135 event_emit_button(xcb_button_press_event_t *ev)
137 const char *name;
138 switch(XCB_EVENT_RESPONSE_TYPE(ev))
140 case XCB_BUTTON_PRESS:
141 name = "button::press";
142 break;
143 case XCB_BUTTON_RELEASE:
144 name = "button::release";
145 break;
146 default:
147 fatal("Invalid event type");
150 /* Push the event's info */
151 lua_pushnumber(globalconf.L, ev->event_x);
152 lua_pushnumber(globalconf.L, ev->event_y);
153 lua_pushnumber(globalconf.L, ev->detail);
154 luaA_pushmodifiers(globalconf.L, ev->state);
155 /* And emit the signal */
156 luaA_object_emit_signal(globalconf.L, -5, name, 4);
159 /** Update the global button state
160 * \param response_type XCB_BUTTON_PRESS or XCB_BUTTON_RELEASE
161 * \param button Button being changed
163 static void
164 event_update_button_state(uint8_t response_type, uint8_t button)
166 /* There is no button 0! */
167 const unsigned int max_button = sizeof(globalconf.buttons_pressed) * 8;
168 uint32_t mask = 1 << (button - 1);
170 if (button > max_button) {
171 warn("Button %d pressed, but we only support %d buttons", button, max_button);
172 return;
175 switch(response_type & XCB_EVENT_RESPONSE_TYPE_MASK)
177 case XCB_BUTTON_PRESS:
178 /* Set the (button-1)-st bit */
179 globalconf.buttons_pressed |= mask;
180 break;
181 case XCB_BUTTON_RELEASE:
182 /* Clear the (button-1)-st bit */
183 globalconf.buttons_pressed &= ~mask;
184 break;
185 default:
186 fatal("Invalid event type");
190 /** The button press event handler.
191 * \param ev The event.
193 static void
194 event_handle_button(xcb_button_press_event_t *ev)
196 client_t *c;
197 drawin_t *drawin;
199 globalconf.timestamp = ev->time;
201 event_update_button_state(ev->response_type, ev->detail);
203 if(event_handle_mousegrabber(ev->root_x, ev->root_y, 1 << (ev->detail - 1 + 8)))
204 return;
206 /* ev->state is
207 * button status (8 bits) + modifiers status (8 bits)
208 * we don't care for button status that we get, especially on release, so
209 * drop them */
210 ev->state &= 0x00ff;
212 if((drawin = drawin_getbywin(ev->event))
213 || (drawin = drawin_getbywin(ev->child)))
215 /* If the drawin is child, then x,y are
216 * relative to root window */
217 if(drawin->window == ev->child)
219 ev->event_x -= drawin->geometry.x + drawin->border_width;
220 ev->event_y -= drawin->geometry.y + drawin->border_width;
223 /* Push the drawable */
224 luaA_object_push(globalconf.L, drawin);
225 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
226 /* and handle the button raw button event */
227 event_emit_button(ev);
228 lua_pop(globalconf.L, 1);
229 /* check if any button object matches */
230 event_button_callback(ev, &drawin->buttons, -1, 1, NULL);
232 else if((c = client_getbyframewin(ev->event)))
234 luaA_object_push(globalconf.L, c);
235 /* And handle the button raw button event */
236 event_emit_button(ev);
237 /* then check if a titlebar was "hit" */
238 int x = ev->event_x, y = ev->event_y;
239 drawable_t *d = client_get_drawable_offset(c, &x, &y);
240 if (d)
242 /* Copy the event so that we can fake x/y */
243 xcb_button_press_event_t event = *ev;
244 event.event_x = x;
245 event.event_y = y;
246 luaA_object_push_item(globalconf.L, -1, d);
247 event_emit_button(&event);
248 lua_pop(globalconf.L, 1);
250 /* then check if any button objects match */
251 event_button_callback(ev, &c->buttons, -1, 1, NULL);
252 xcb_allow_events(globalconf.connection,
253 XCB_ALLOW_REPLAY_POINTER,
254 XCB_CURRENT_TIME);
256 else if(ev->child == XCB_NONE)
257 if(globalconf.screen->root == ev->event)
259 event_button_callback(ev, &globalconf.buttons, 0, 0, NULL);
260 return;
264 static void
265 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
267 uint16_t config_win_mask = 0;
268 uint32_t config_win_vals[7];
269 unsigned short i = 0;
271 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
273 config_win_mask |= XCB_CONFIG_WINDOW_X;
274 config_win_vals[i++] = ev->x;
276 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
278 config_win_mask |= XCB_CONFIG_WINDOW_Y;
279 config_win_vals[i++] = ev->y;
281 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
283 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
284 config_win_vals[i++] = ev->width;
286 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
288 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
289 config_win_vals[i++] = ev->height;
291 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
293 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
294 config_win_vals[i++] = ev->border_width;
296 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
298 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
299 config_win_vals[i++] = ev->sibling;
301 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
303 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
304 config_win_vals[i++] = ev->stack_mode;
307 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
310 /** The configure event handler.
311 * \param ev The event.
313 static void
314 event_handle_configurerequest(xcb_configure_request_event_t *ev)
316 client_t *c;
318 if((c = client_getbywin(ev->window)))
320 area_t geometry = c->geometry;
322 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
323 geometry.x = ev->x;
324 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
325 geometry.y = ev->y;
326 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
327 geometry.width = ev->width;
328 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
329 geometry.height = ev->height;
331 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
333 luaA_object_push(globalconf.L, c);
334 window_set_border_width(globalconf.L, -1, ev->border_width);
335 lua_pop(globalconf.L, 1);
338 if(!client_resize(c, geometry))
339 /* ICCCM 4.1.5 / 4.2.3, if nothing was changed, send an event saying so */
340 client_send_configure(c);
342 else
343 event_handle_configurerequest_configure_window(ev);
346 /** The configure notify event handler.
347 * \param ev The event.
349 static void
350 event_handle_configurenotify(xcb_configure_notify_event_t *ev)
352 const xcb_screen_t *screen = globalconf.screen;
354 if(ev->window == screen->root
355 && (ev->width != screen->width_in_pixels
356 || ev->height != screen->height_in_pixels))
357 /* it's not that we panic, but restart */
358 awesome_restart();
361 /** The destroy notify event handler.
362 * \param ev The event.
364 static void
365 event_handle_destroynotify(xcb_destroy_notify_event_t *ev)
367 client_t *c;
369 if((c = client_getbywin(ev->window)))
370 client_unmanage(c, false);
371 else
372 for(int i = 0; i < globalconf.embedded.len; i++)
373 if(globalconf.embedded.tab[i].win == ev->window)
375 xembed_window_array_take(&globalconf.embedded, i);
376 luaA_systray_invalidate();
380 /** The motion notify event handler.
381 * \param ev The event.
383 static void
384 event_handle_motionnotify(xcb_motion_notify_event_t *ev)
386 drawin_t *w;
387 client_t *c;
389 globalconf.timestamp = ev->time;
391 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
392 return;
394 if((c = client_getbyframewin(ev->event)))
396 luaA_object_push(globalconf.L, c);
397 lua_pushnumber(globalconf.L, ev->event_x);
398 lua_pushnumber(globalconf.L, ev->event_y);
399 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
401 /* now check if a titlebar was "hit" */
402 int x = ev->event_x, y = ev->event_y;
403 drawable_t *d = client_get_drawable_offset(c, &x, &y);
404 if (d)
406 luaA_object_push_item(globalconf.L, -1, d);
407 lua_pushnumber(globalconf.L, x);
408 lua_pushnumber(globalconf.L, y);
409 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
410 lua_pop(globalconf.L, 1);
412 lua_pop(globalconf.L, 1);
415 if((w = drawin_getbywin(ev->event)))
417 luaA_object_push(globalconf.L, w);
418 luaA_object_push_item(globalconf.L, -1, w->drawable);
419 lua_pushnumber(globalconf.L, ev->event_x);
420 lua_pushnumber(globalconf.L, ev->event_y);
421 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
422 lua_pop(globalconf.L, 2);
426 /** The leave notify event handler.
427 * \param ev The event.
429 static void
430 event_handle_leavenotify(xcb_leave_notify_event_t *ev)
432 drawin_t *drawin;
433 client_t *c;
435 globalconf.timestamp = ev->time;
437 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
438 return;
440 if((c = client_getbyframewin(ev->event)))
442 luaA_object_push(globalconf.L, c);
443 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
444 drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
445 if (d)
447 luaA_object_push_item(globalconf.L, -1, d);
448 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
449 lua_pop(globalconf.L, 1);
451 lua_pop(globalconf.L, 1);
454 if((drawin = drawin_getbywin(ev->event)))
456 luaA_object_push(globalconf.L, drawin);
457 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
458 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
459 lua_pop(globalconf.L, 2);
463 /** The enter notify event handler.
464 * \param ev The event.
466 static void
467 event_handle_enternotify(xcb_enter_notify_event_t *ev)
469 client_t *c;
470 drawin_t *drawin;
472 globalconf.timestamp = ev->time;
474 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
475 return;
477 if((drawin = drawin_getbywin(ev->event)))
479 luaA_object_push(globalconf.L, drawin);
480 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
481 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
482 lua_pop(globalconf.L, 2);
485 if((c = client_getbyframewin(ev->event)))
487 luaA_object_push(globalconf.L, c);
488 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
489 drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
490 if (d)
492 luaA_object_push_item(globalconf.L, -1, d);
493 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
494 lua_pop(globalconf.L, 1);
496 lua_pop(globalconf.L, 1);
500 /** The focus in event handler.
501 * \param ev The event.
503 static void
504 event_handle_focusin(xcb_focus_in_event_t *ev)
506 if (ev->mode == XCB_NOTIFY_MODE_GRAB
507 || ev->mode == XCB_NOTIFY_MODE_UNGRAB)
508 /* Ignore focus changes due to keyboard grabs */
509 return;
511 /* Events that we are interested in: */
512 switch(ev->detail)
514 /* These are events that jump between root windows.
516 case XCB_NOTIFY_DETAIL_ANCESTOR:
517 case XCB_NOTIFY_DETAIL_INFERIOR:
519 /* These are events that jump between clients.
520 * Virtual events ensure we always get an event on our top-level window.
522 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
523 case XCB_NOTIFY_DETAIL_NONLINEAR:
525 client_t *c;
527 if((c = client_getbywin(ev->event)))
528 client_focus_update(c);
530 /* all other events are ignored */
531 default:
532 break;
536 /** The expose event handler.
537 * \param ev The event.
539 static void
540 event_handle_expose(xcb_expose_event_t *ev)
542 drawin_t *drawin;
543 client_t *client;
545 if((drawin = drawin_getbywin(ev->window)))
546 drawin_refresh_pixmap_partial(drawin,
547 ev->x, ev->y,
548 ev->width, ev->height);
549 if ((client = client_getbyframewin(ev->window)))
550 client_refresh(client);
553 /** The key press event handler.
554 * \param ev The event.
556 static void
557 event_handle_key(xcb_key_press_event_t *ev)
559 globalconf.timestamp = ev->time;
561 if(globalconf.keygrabber != LUA_REFNIL)
563 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
564 if(keygrabber_handlekpress(globalconf.L, ev))
566 if(lua_pcall(globalconf.L, 3, 1, 0))
568 warn("error running function: %s", lua_tostring(globalconf.L, -1));
569 luaA_keygrabber_stop(globalconf.L);
572 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
574 else
576 /* get keysym ignoring all modifiers */
577 xcb_keysym_t keysym = keyresolv_get_keysym(ev->detail, 0);
578 client_t *c;
579 if((c = client_getbyframewin(ev->event)))
581 luaA_object_push(globalconf.L, c);
582 event_key_callback(ev, &c->keys, -1, 1, &keysym);
584 else
585 event_key_callback(ev, &globalconf.keys, 0, 0, &keysym);
589 /** The map request event handler.
590 * \param ev The event.
592 static void
593 event_handle_maprequest(xcb_map_request_event_t *ev)
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->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 client_manage(ev->window, geom_r, false);
637 p_delete(&geom_r);
640 bailout:
641 p_delete(&wa_r);
644 /** The unmap notify event handler.
645 * \param ev The event.
647 static void
648 event_handle_unmapnotify(xcb_unmap_notify_event_t *ev)
650 client_t *c;
652 if((c = client_getbywin(ev->window)))
653 client_unmanage(c, true);
654 else
655 for(int i = 0; i < globalconf.embedded.len; i++)
656 if(globalconf.embedded.tab[i].win == ev->window)
658 xembed_window_array_take(&globalconf.embedded, i);
659 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, ev->window);
660 luaA_systray_invalidate();
664 /** The randr screen change notify event handler.
665 * \param ev The event.
667 static void
668 event_handle_randr_screen_change_notify(xcb_randr_screen_change_notify_event_t *ev)
670 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
671 * (only the code relevant to RRScreenChangeNotify) as the latter
672 * doesn't provide this kind of function */
673 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
674 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->height, ev->width,
675 ev->mheight, ev->mwidth);
676 else
677 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->width, ev->height,
678 ev->mwidth, ev->mheight);
680 /* XRRUpdateConfiguration also executes the following instruction
681 * but it's not useful because SubpixelOrder is not used at all at
682 * the moment
684 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
687 awesome_restart();
690 /** The client message event handler.
691 * \param ev The event.
693 static void
694 event_handle_clientmessage(xcb_client_message_event_t *ev)
696 /* check for startup notification messages */
697 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
698 return;
700 if(ev->type == WM_CHANGE_STATE)
702 client_t *c;
703 if((c = client_getbywin(ev->window))
704 && ev->format == 32
705 && ev->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC)
707 luaA_object_push(globalconf.L, c);
708 client_set_minimized(globalconf.L, -1, true);
709 lua_pop(globalconf.L, 1);
712 else if(ev->type == _XEMBED)
713 xembed_process_client_message(ev);
714 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
715 systray_process_client_message(ev);
716 else
717 ewmh_process_client_message(ev);
720 /** The keymap change notify event handler.
721 * \param ev The event.
723 static void
724 event_handle_mappingnotify(xcb_mapping_notify_event_t *ev)
726 if(ev->request == XCB_MAPPING_MODIFIER
727 || ev->request == XCB_MAPPING_KEYBOARD)
729 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
730 xcb_get_modifier_mapping_unchecked(globalconf.connection);
732 /* Free and then allocate the key symbols */
733 xcb_key_symbols_free(globalconf.keysyms);
734 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
736 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
737 globalconf.keysyms, &globalconf.numlockmask,
738 &globalconf.shiftlockmask, &globalconf.capslockmask,
739 &globalconf.modeswitchmask);
741 /* regrab everything */
742 xcb_screen_t *s = globalconf.screen;
743 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
744 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
745 xwindow_grabkeys(s->root, &globalconf.keys);
747 foreach(_c, globalconf.clients)
749 client_t *c = *_c;
750 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->frame_window, XCB_BUTTON_MASK_ANY);
751 xwindow_grabkeys(c->frame_window, &c->keys);
756 static void
757 event_handle_reparentnotify(xcb_reparent_notify_event_t *ev)
759 client_t *c;
761 if((c = client_getbywin(ev->window)) && c->frame_window != ev->parent)
763 /* Ignore reparents to the root window, they *might* be caused by
764 * ourselves if a client quickly unmaps and maps itself again. */
765 if (ev->parent != globalconf.screen->root)
766 client_unmanage(c, true);
770 /** \brief awesome xerror function.
771 * There's no way to check accesses to destroyed windows, thus those cases are
772 * ignored (especially on UnmapNotify's).
773 * \param e The error event.
775 static void
776 xerror(xcb_generic_error_t *e)
778 /* ignore this */
779 if(e->error_code == XCB_WINDOW
780 || (e->error_code == XCB_MATCH
781 && e->major_code == XCB_SET_INPUT_FOCUS)
782 || (e->error_code == XCB_VALUE
783 && e->major_code == XCB_KILL_CLIENT)
784 || (e->error_code == XCB_MATCH
785 && e->major_code == XCB_CONFIGURE_WINDOW))
786 return;
788 warn("X error: request=%s (major %d, minor %d), error=%s (%d)",
789 xcb_event_get_request_label(e->major_code),
790 e->major_code, e->minor_code,
791 xcb_event_get_error_label(e->error_code),
792 e->error_code);
794 return;
797 void event_handle(xcb_generic_event_t *event)
799 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
801 if(response_type == 0)
803 /* This is an error, not a event */
804 xerror((xcb_generic_error_t *) event);
805 return;
808 switch(response_type)
810 #define EVENT(type, callback) case type: callback((void *) event); return
811 EVENT(XCB_BUTTON_PRESS, event_handle_button);
812 EVENT(XCB_BUTTON_RELEASE, event_handle_button);
813 EVENT(XCB_CONFIGURE_REQUEST, event_handle_configurerequest);
814 EVENT(XCB_CONFIGURE_NOTIFY, event_handle_configurenotify);
815 EVENT(XCB_DESTROY_NOTIFY, event_handle_destroynotify);
816 EVENT(XCB_ENTER_NOTIFY, event_handle_enternotify);
817 EVENT(XCB_CLIENT_MESSAGE, event_handle_clientmessage);
818 EVENT(XCB_EXPOSE, event_handle_expose);
819 EVENT(XCB_FOCUS_IN, event_handle_focusin);
820 EVENT(XCB_KEY_PRESS, event_handle_key);
821 EVENT(XCB_KEY_RELEASE, event_handle_key);
822 EVENT(XCB_LEAVE_NOTIFY, event_handle_leavenotify);
823 EVENT(XCB_MAPPING_NOTIFY, event_handle_mappingnotify);
824 EVENT(XCB_MAP_REQUEST, event_handle_maprequest);
825 EVENT(XCB_MOTION_NOTIFY, event_handle_motionnotify);
826 EVENT(XCB_PROPERTY_NOTIFY, property_handle_propertynotify);
827 EVENT(XCB_REPARENT_NOTIFY, event_handle_reparentnotify);
828 EVENT(XCB_UNMAP_NOTIFY, event_handle_unmapnotify);
829 #undef EVENT
832 static uint8_t randr_screen_change_notify = 0;
834 if(randr_screen_change_notify == 0)
836 /* check for randr extension */
837 const xcb_query_extension_reply_t *randr_query;
838 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
839 if(randr_query->present)
840 randr_screen_change_notify = randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY;
843 if (response_type == randr_screen_change_notify)
844 event_handle_randr_screen_change_notify((void *) event);
847 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80