client: Ignore transient_for causing loops (FS#1124)
[awesome.git] / event.c
blob9bd02651ca54f46a8f937a00afadae5917cb798b
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 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 /** 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 /** The button press event handler.
160 * \param ev The event.
162 static void
163 event_handle_button(xcb_button_press_event_t *ev)
165 client_t *c;
166 drawin_t *drawin;
168 globalconf.timestamp = ev->time;
170 if(event_handle_mousegrabber(ev->root_x, ev->root_y, 1 << (ev->detail - 1 + 8)))
171 return;
173 /* ev->state is
174 * button status (8 bits) + modifiers status (8 bits)
175 * we don't care for button status that we get, especially on release, so
176 * drop them */
177 ev->state &= 0x00ff;
179 if((drawin = drawin_getbywin(ev->event))
180 || (drawin = drawin_getbywin(ev->child)))
182 /* If the drawin is child, then x,y are
183 * relative to root window */
184 if(drawin->window == ev->child)
186 ev->event_x -= drawin->geometry.x + drawin->border_width;
187 ev->event_y -= drawin->geometry.y + drawin->border_width;
190 /* Push the drawable */
191 luaA_object_push(globalconf.L, drawin);
192 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
193 /* and handle the button raw button event */
194 event_emit_button(ev);
195 lua_pop(globalconf.L, 1);
196 /* check if any button object matches */
197 event_button_callback(ev, &drawin->buttons, -1, 1, NULL);
199 else if((c = client_getbyframewin(ev->event)))
201 luaA_object_push(globalconf.L, c);
202 /* And handle the button raw button event */
203 event_emit_button(ev);
204 /* then check if a titlebar was "hit" */
205 int x = ev->event_x, y = ev->event_y;
206 drawable_t *d = client_get_drawable_offset(c, &x, &y);
207 if (d)
209 /* Copy the event so that we can fake x/y */
210 xcb_button_press_event_t event = *ev;
211 event.event_x = x;
212 event.event_y = y;
213 luaA_object_push_item(globalconf.L, -1, d);
214 event_emit_button(&event);
215 lua_pop(globalconf.L, 1);
217 /* then check if any button objects match */
218 event_button_callback(ev, &c->buttons, -1, 1, NULL);
219 xcb_allow_events(globalconf.connection,
220 XCB_ALLOW_REPLAY_POINTER,
221 XCB_CURRENT_TIME);
223 else if(ev->child == XCB_NONE)
224 if(globalconf.screen->root == ev->event)
226 event_button_callback(ev, &globalconf.buttons, 0, 0, NULL);
227 return;
231 static void
232 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
234 uint16_t config_win_mask = 0;
235 uint32_t config_win_vals[7];
236 unsigned short i = 0;
238 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
240 config_win_mask |= XCB_CONFIG_WINDOW_X;
241 config_win_vals[i++] = ev->x;
243 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
245 config_win_mask |= XCB_CONFIG_WINDOW_Y;
246 config_win_vals[i++] = ev->y;
248 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
250 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
251 config_win_vals[i++] = ev->width;
253 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
255 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
256 config_win_vals[i++] = ev->height;
258 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
260 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
261 config_win_vals[i++] = ev->border_width;
263 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
265 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
266 config_win_vals[i++] = ev->sibling;
268 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
270 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
271 config_win_vals[i++] = ev->stack_mode;
274 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
277 /** The configure event handler.
278 * \param ev The event.
280 static void
281 event_handle_configurerequest(xcb_configure_request_event_t *ev)
283 client_t *c;
285 if((c = client_getbywin(ev->window)))
287 area_t geometry = c->geometry;
288 int16_t diff_w = 0, diff_h = 0, diff_border = 0;
290 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
291 geometry.x = ev->x;
292 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
293 geometry.y = ev->y;
294 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
296 uint16_t old_w = geometry.width;
297 geometry.width = ev->width;
298 /* The ConfigureRequest specifies the size of the client window, we want the frame */
299 geometry.width += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
300 geometry.width += c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
301 diff_w = geometry.width - old_w;
303 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
305 uint16_t old_h = geometry.height;
306 geometry.height = ev->height;
307 /* The ConfigureRequest specifies the size of the client window, we want the frame */
308 geometry.height += c->titlebar[CLIENT_TITLEBAR_TOP].size;
309 geometry.height += c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
310 diff_h = geometry.height - old_h;
312 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
314 diff_border = ev->border_width - c->border_width;
315 diff_h += diff_border;
316 diff_w += diff_border;
318 luaA_object_push(globalconf.L, c);
319 window_set_border_width(globalconf.L, -1, ev->border_width);
320 lua_pop(globalconf.L, 1);
323 /* If the client resizes without moving itself, apply window gravity */
324 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY)
326 int16_t diff_x = 0, diff_y = 0;
327 xwindow_translate_for_gravity(c->size_hints.win_gravity, diff_border, diff_border, diff_w, diff_h, &diff_x, &diff_y);
328 if(!(ev->value_mask & XCB_CONFIG_WINDOW_X))
329 geometry.x += diff_x;
330 if(!(ev->value_mask & XCB_CONFIG_WINDOW_Y))
331 geometry.y += diff_y;
334 if(!client_resize(c, geometry, false))
335 /* ICCCM 4.1.5 / 4.2.3, if nothing was changed, send an event saying so */
336 client_send_configure(c);
338 else
339 event_handle_configurerequest_configure_window(ev);
342 /** The configure notify event handler.
343 * \param ev The event.
345 static void
346 event_handle_configurenotify(xcb_configure_notify_event_t *ev)
348 const xcb_screen_t *screen = globalconf.screen;
350 if(ev->window == screen->root
351 && (ev->width != screen->width_in_pixels
352 || ev->height != screen->height_in_pixels))
353 /* it's not that we panic, but restart */
354 awesome_restart();
357 /** The destroy notify event handler.
358 * \param ev The event.
360 static void
361 event_handle_destroynotify(xcb_destroy_notify_event_t *ev)
363 client_t *c;
365 if((c = client_getbywin(ev->window)))
366 client_unmanage(c, false);
367 else
368 for(int i = 0; i < globalconf.embedded.len; i++)
369 if(globalconf.embedded.tab[i].win == ev->window)
371 xembed_window_array_take(&globalconf.embedded, i);
372 luaA_systray_invalidate();
376 /** The motion notify event handler.
377 * \param ev The event.
379 static void
380 event_handle_motionnotify(xcb_motion_notify_event_t *ev)
382 drawin_t *w;
383 client_t *c;
385 globalconf.timestamp = ev->time;
387 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
388 return;
390 if((c = client_getbyframewin(ev->event)))
392 luaA_object_push(globalconf.L, c);
393 lua_pushnumber(globalconf.L, ev->event_x);
394 lua_pushnumber(globalconf.L, ev->event_y);
395 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
397 /* now check if a titlebar was "hit" */
398 int x = ev->event_x, y = ev->event_y;
399 drawable_t *d = client_get_drawable_offset(c, &x, &y);
400 if (d)
402 luaA_object_push_item(globalconf.L, -1, d);
403 lua_pushnumber(globalconf.L, x);
404 lua_pushnumber(globalconf.L, y);
405 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
406 lua_pop(globalconf.L, 1);
408 lua_pop(globalconf.L, 1);
411 if((w = drawin_getbywin(ev->event)))
413 luaA_object_push(globalconf.L, w);
414 luaA_object_push_item(globalconf.L, -1, w->drawable);
415 lua_pushnumber(globalconf.L, ev->event_x);
416 lua_pushnumber(globalconf.L, ev->event_y);
417 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
418 lua_pop(globalconf.L, 2);
422 /** The leave notify event handler.
423 * \param ev The event.
425 static void
426 event_handle_leavenotify(xcb_leave_notify_event_t *ev)
428 drawin_t *drawin;
429 client_t *c;
431 globalconf.timestamp = ev->time;
433 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
434 return;
436 if((c = client_getbyframewin(ev->event)))
438 luaA_object_push(globalconf.L, c);
439 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
440 drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
441 if (d)
443 luaA_object_push_item(globalconf.L, -1, d);
444 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
445 lua_pop(globalconf.L, 1);
447 lua_pop(globalconf.L, 1);
450 if((drawin = drawin_getbywin(ev->event)))
452 luaA_object_push(globalconf.L, drawin);
453 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
454 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
455 lua_pop(globalconf.L, 2);
459 /** The enter notify event handler.
460 * \param ev The event.
462 static void
463 event_handle_enternotify(xcb_enter_notify_event_t *ev)
465 client_t *c;
466 drawin_t *drawin;
468 globalconf.timestamp = ev->time;
470 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
471 return;
473 if((drawin = drawin_getbywin(ev->event)))
475 luaA_object_push(globalconf.L, drawin);
476 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
477 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
478 lua_pop(globalconf.L, 2);
481 if((c = client_getbyframewin(ev->event)))
483 luaA_object_push(globalconf.L, c);
484 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
485 drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
486 if (d)
488 luaA_object_push_item(globalconf.L, -1, d);
489 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
490 lua_pop(globalconf.L, 1);
492 lua_pop(globalconf.L, 1);
496 /** The focus in event handler.
497 * \param ev The event.
499 static void
500 event_handle_focusin(xcb_focus_in_event_t *ev)
502 if (ev->mode == XCB_NOTIFY_MODE_GRAB
503 || ev->mode == XCB_NOTIFY_MODE_UNGRAB)
504 /* Ignore focus changes due to keyboard grabs */
505 return;
507 /* Events that we are interested in: */
508 switch(ev->detail)
510 /* These are events that jump between root windows.
512 case XCB_NOTIFY_DETAIL_ANCESTOR:
513 case XCB_NOTIFY_DETAIL_INFERIOR:
515 /* These are events that jump between clients.
516 * Virtual events ensure we always get an event on our top-level window.
518 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
519 case XCB_NOTIFY_DETAIL_NONLINEAR:
521 client_t *c;
523 if((c = client_getbywin(ev->event))) {
524 /* If there is still a pending focus change, do it now. */
525 client_focus_refresh();
526 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 drawin_t *drawin;
542 client_t *client;
544 if((drawin = drawin_getbywin(ev->window)))
545 drawin_refresh_pixmap_partial(drawin,
546 ev->x, ev->y,
547 ev->width, ev->height);
548 if ((client = client_getbyframewin(ev->window)))
549 client_refresh(client);
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 globalconf.timestamp = ev->time;
560 if(globalconf.keygrabber != LUA_REFNIL)
562 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
563 if(keygrabber_handlekpress(globalconf.L, ev))
565 if(lua_pcall(globalconf.L, 3, 1, 0))
567 warn("error running function: %s", lua_tostring(globalconf.L, -1));
568 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 = keyresolv_get_keysym(ev->detail, 0);
577 client_t *c;
578 if((c = client_getbyframewin(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 client_t *c;
595 xcb_get_window_attributes_cookie_t wa_c;
596 xcb_get_window_attributes_reply_t *wa_r;
597 xcb_get_geometry_cookie_t geom_c;
598 xcb_get_geometry_reply_t *geom_r;
600 wa_c = xcb_get_window_attributes_unchecked(globalconf.connection, ev->window);
602 if(!(wa_r = xcb_get_window_attributes_reply(globalconf.connection, wa_c, NULL)))
603 return;
605 if(wa_r->override_redirect)
606 goto bailout;
608 if(xembed_getbywin(&globalconf.embedded, ev->window))
610 xcb_map_window(globalconf.connection, ev->window);
611 xembed_window_activate(globalconf.connection, ev->window);
613 else if((c = client_getbywin(ev->window)))
615 /* Check that it may be visible, but not asked to be hidden */
616 if(client_maybevisible(c) && !c->hidden)
618 luaA_object_push(globalconf.L, c);
619 client_set_minimized(globalconf.L, -1, false);
620 lua_pop(globalconf.L, 1);
621 /* it will be raised, so just update ourself */
622 client_raise(c);
625 else
627 geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
629 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
631 goto bailout;
634 client_manage(ev->window, geom_r, false);
636 p_delete(&geom_r);
639 bailout:
640 p_delete(&wa_r);
643 /** The unmap notify event handler.
644 * \param ev The event.
646 static void
647 event_handle_unmapnotify(xcb_unmap_notify_event_t *ev)
649 client_t *c;
651 if((c = client_getbywin(ev->window)))
652 client_unmanage(c, true);
653 else
654 for(int i = 0; i < globalconf.embedded.len; i++)
655 if(globalconf.embedded.tab[i].win == ev->window)
657 xembed_window_array_take(&globalconf.embedded, i);
658 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, ev->window);
659 luaA_systray_invalidate();
663 /** The randr screen change notify event handler.
664 * \param ev The event.
666 static void
667 event_handle_randr_screen_change_notify(xcb_randr_screen_change_notify_event_t *ev)
669 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
670 * (only the code relevant to RRScreenChangeNotify) as the latter
671 * doesn't provide this kind of function */
672 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
673 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->height, ev->width,
674 ev->mheight, ev->mwidth);
675 else
676 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->width, ev->height,
677 ev->mwidth, ev->mheight);
679 /* XRRUpdateConfiguration also executes the following instruction
680 * but it's not useful because SubpixelOrder is not used at all at
681 * the moment
683 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
686 awesome_restart();
689 /** The client message event handler.
690 * \param ev The event.
692 static void
693 event_handle_clientmessage(xcb_client_message_event_t *ev)
695 /* check for startup notification messages */
696 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
697 return;
699 if(ev->type == WM_CHANGE_STATE)
701 client_t *c;
702 if((c = client_getbywin(ev->window))
703 && ev->format == 32
704 && ev->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC)
706 luaA_object_push(globalconf.L, c);
707 client_set_minimized(globalconf.L, -1, true);
708 lua_pop(globalconf.L, 1);
711 else if(ev->type == _XEMBED)
712 xembed_process_client_message(ev);
713 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
714 systray_process_client_message(ev);
715 else
716 ewmh_process_client_message(ev);
719 /** The keymap change notify event handler.
720 * \param ev The event.
722 static void
723 event_handle_mappingnotify(xcb_mapping_notify_event_t *ev)
725 if(ev->request == XCB_MAPPING_MODIFIER
726 || ev->request == XCB_MAPPING_KEYBOARD)
728 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
729 xcb_get_modifier_mapping_unchecked(globalconf.connection);
731 /* Free and then allocate the key symbols */
732 xcb_key_symbols_free(globalconf.keysyms);
733 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
735 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
736 globalconf.keysyms, &globalconf.numlockmask,
737 &globalconf.shiftlockmask, &globalconf.capslockmask,
738 &globalconf.modeswitchmask);
740 /* regrab everything */
741 xcb_screen_t *s = globalconf.screen;
742 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
743 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
744 xwindow_grabkeys(s->root, &globalconf.keys);
746 foreach(_c, globalconf.clients)
748 client_t *c = *_c;
749 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->frame_window, XCB_BUTTON_MASK_ANY);
750 xwindow_grabkeys(c->frame_window, &c->keys);
755 static void
756 event_handle_reparentnotify(xcb_reparent_notify_event_t *ev)
758 client_t *c;
760 if((c = client_getbywin(ev->window)) && c->frame_window != ev->parent)
762 /* Ignore reparents to the root window, they *might* be caused by
763 * ourselves if a client quickly unmaps and maps itself again. */
764 if (ev->parent != globalconf.screen->root)
765 client_unmanage(c, true);
769 /** \brief awesome xerror function.
770 * There's no way to check accesses to destroyed windows, thus those cases are
771 * ignored (especially on UnmapNotify's).
772 * \param e The error event.
774 static void
775 xerror(xcb_generic_error_t *e)
777 /* ignore this */
778 if(e->error_code == XCB_WINDOW
779 || (e->error_code == XCB_MATCH
780 && e->major_code == XCB_SET_INPUT_FOCUS)
781 || (e->error_code == XCB_VALUE
782 && e->major_code == XCB_KILL_CLIENT)
783 || (e->error_code == XCB_MATCH
784 && e->major_code == XCB_CONFIGURE_WINDOW))
785 return;
787 warn("X error: request=%s (major %d, minor %d), error=%s (%d)",
788 xcb_event_get_request_label(e->major_code),
789 e->major_code, e->minor_code,
790 xcb_event_get_error_label(e->error_code),
791 e->error_code);
793 return;
796 void event_handle(xcb_generic_event_t *event)
798 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
800 if(response_type == 0)
802 /* This is an error, not a event */
803 xerror((xcb_generic_error_t *) event);
804 return;
807 switch(response_type)
809 #define EVENT(type, callback) case type: callback((void *) event); return
810 EVENT(XCB_BUTTON_PRESS, event_handle_button);
811 EVENT(XCB_BUTTON_RELEASE, event_handle_button);
812 EVENT(XCB_CONFIGURE_REQUEST, event_handle_configurerequest);
813 EVENT(XCB_CONFIGURE_NOTIFY, event_handle_configurenotify);
814 EVENT(XCB_DESTROY_NOTIFY, event_handle_destroynotify);
815 EVENT(XCB_ENTER_NOTIFY, event_handle_enternotify);
816 EVENT(XCB_CLIENT_MESSAGE, event_handle_clientmessage);
817 EVENT(XCB_EXPOSE, event_handle_expose);
818 EVENT(XCB_FOCUS_IN, event_handle_focusin);
819 EVENT(XCB_KEY_PRESS, event_handle_key);
820 EVENT(XCB_KEY_RELEASE, event_handle_key);
821 EVENT(XCB_LEAVE_NOTIFY, event_handle_leavenotify);
822 EVENT(XCB_MAPPING_NOTIFY, event_handle_mappingnotify);
823 EVENT(XCB_MAP_REQUEST, event_handle_maprequest);
824 EVENT(XCB_MOTION_NOTIFY, event_handle_motionnotify);
825 EVENT(XCB_PROPERTY_NOTIFY, property_handle_propertynotify);
826 EVENT(XCB_REPARENT_NOTIFY, event_handle_reparentnotify);
827 EVENT(XCB_UNMAP_NOTIFY, event_handle_unmapnotify);
828 #undef EVENT
831 static uint8_t randr_screen_change_notify = 0;
833 if(randr_screen_change_notify == 0)
835 /* check for randr extension */
836 const xcb_query_extension_reply_t *randr_query;
837 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
838 if(randr_query->present)
839 randr_screen_change_notify = randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY;
842 if (response_type == randr_screen_change_notify)
843 event_handle_randr_screen_change_notify((void *) event);
846 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80