Merge branch 'fix-ldoc-set-spacing' of git://github.com/actionless/awesome
[awesome.git] / event.c
blob50511e0b097bbc0baf0e3d70140852ee26bb498a
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 "event.h"
23 #include "awesome.h"
24 #include "property.h"
25 #include "objects/tag.h"
26 #include "objects/drawin.h"
27 #include "xwindow.h"
28 #include "ewmh.h"
29 #include "objects/client.h"
30 #include "keyresolv.h"
31 #include "keygrabber.h"
32 #include "mousegrabber.h"
33 #include "luaa.h"
34 #include "systray.h"
35 #include "objects/screen.h"
36 #include "common/atoms.h"
37 #include "common/xutil.h"
39 #include <xcb/xcb.h>
40 #include <xcb/randr.h>
41 #include <xcb/shape.h>
42 #include <xcb/xcb_atom.h>
43 #include <xcb/xcb_icccm.h>
44 #include <xcb/xcb_event.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 /** Emit a button signal.
132 * The top of the lua stack has to be the object on which to emit the event.
133 * \param ev The event to handle.
135 static void
136 event_emit_button(xcb_button_press_event_t *ev)
138 const char *name;
139 switch(XCB_EVENT_RESPONSE_TYPE(ev))
141 case XCB_BUTTON_PRESS:
142 name = "button::press";
143 break;
144 case XCB_BUTTON_RELEASE:
145 name = "button::release";
146 break;
147 default:
148 fatal("Invalid event type");
151 /* Push the event's info */
152 lua_pushnumber(globalconf.L, ev->event_x);
153 lua_pushnumber(globalconf.L, ev->event_y);
154 lua_pushnumber(globalconf.L, ev->detail);
155 luaA_pushmodifiers(globalconf.L, ev->state);
156 /* And emit the signal */
157 luaA_object_emit_signal(globalconf.L, -5, name, 4);
160 /** The button press event handler.
161 * \param ev The event.
163 static void
164 event_handle_button(xcb_button_press_event_t *ev)
166 client_t *c;
167 drawin_t *drawin;
169 globalconf.timestamp = ev->time;
171 if(event_handle_mousegrabber(ev->root_x, ev->root_y, 1 << (ev->detail - 1 + 8)))
172 return;
174 /* ev->state is
175 * button status (8 bits) + modifiers status (8 bits)
176 * we don't care for button status that we get, especially on release, so
177 * drop them */
178 ev->state &= 0x00ff;
180 if((drawin = drawin_getbywin(ev->event))
181 || (drawin = drawin_getbywin(ev->child)))
183 /* If the drawin is child, then x,y are
184 * relative to root window */
185 if(drawin->window == ev->child)
187 ev->event_x -= drawin->geometry.x + drawin->border_width;
188 ev->event_y -= drawin->geometry.y + drawin->border_width;
191 /* Push the drawable */
192 luaA_object_push(globalconf.L, drawin);
193 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
194 /* and handle the button raw button event */
195 event_emit_button(ev);
196 lua_pop(globalconf.L, 1);
197 /* check if any button object matches */
198 event_button_callback(ev, &drawin->buttons, -1, 1, NULL);
199 /* Either we are receiving this due to ButtonPress/Release on the root
200 * window or because we grabbed the button on the window. In the later
201 * case we have to call AllowEvents.
202 * Use AsyncPointer instead of ReplayPointer so that the event is
203 * "eaten" instead of being handled again on the root window.
205 if(ev->child == XCB_NONE)
206 xcb_allow_events(globalconf.connection,
207 XCB_ALLOW_ASYNC_POINTER,
208 XCB_CURRENT_TIME);
210 else if((c = client_getbyframewin(ev->event)))
212 luaA_object_push(globalconf.L, c);
213 /* And handle the button raw button event */
214 event_emit_button(ev);
215 /* then check if a titlebar was "hit" */
216 int x = ev->event_x, y = ev->event_y;
217 drawable_t *d = client_get_drawable_offset(c, &x, &y);
218 if (d)
220 /* Copy the event so that we can fake x/y */
221 xcb_button_press_event_t event = *ev;
222 event.event_x = x;
223 event.event_y = y;
224 luaA_object_push_item(globalconf.L, -1, d);
225 event_emit_button(&event);
226 lua_pop(globalconf.L, 1);
228 /* then check if any button objects match */
229 event_button_callback(ev, &c->buttons, -1, 1, NULL);
230 xcb_allow_events(globalconf.connection,
231 XCB_ALLOW_REPLAY_POINTER,
232 XCB_CURRENT_TIME);
234 else if(ev->child == XCB_NONE)
235 if(globalconf.screen->root == ev->event)
237 event_button_callback(ev, &globalconf.buttons, 0, 0, NULL);
238 return;
242 static void
243 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
245 uint16_t config_win_mask = 0;
246 uint32_t config_win_vals[7];
247 unsigned short i = 0;
249 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
251 config_win_mask |= XCB_CONFIG_WINDOW_X;
252 config_win_vals[i++] = ev->x;
254 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
256 config_win_mask |= XCB_CONFIG_WINDOW_Y;
257 config_win_vals[i++] = ev->y;
259 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
261 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
262 config_win_vals[i++] = ev->width;
264 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
266 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
267 config_win_vals[i++] = ev->height;
269 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
271 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
272 config_win_vals[i++] = ev->border_width;
274 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
276 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
277 config_win_vals[i++] = ev->sibling;
279 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
281 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
282 config_win_vals[i++] = ev->stack_mode;
285 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
288 /** The configure event handler.
289 * \param ev The event.
291 static void
292 event_handle_configurerequest(xcb_configure_request_event_t *ev)
294 client_t *c;
296 if((c = client_getbywin(ev->window)))
298 area_t geometry = c->geometry;
299 int16_t diff_w = 0, diff_h = 0, diff_border = 0;
301 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
302 geometry.x = ev->x;
303 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
304 geometry.y = ev->y;
305 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
307 uint16_t old_w = geometry.width;
308 geometry.width = ev->width;
309 /* The ConfigureRequest specifies the size of the client window, we want the frame */
310 geometry.width += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
311 geometry.width += c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
312 diff_w = geometry.width - old_w;
314 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
316 uint16_t old_h = geometry.height;
317 geometry.height = ev->height;
318 /* The ConfigureRequest specifies the size of the client window, we want the frame */
319 geometry.height += c->titlebar[CLIENT_TITLEBAR_TOP].size;
320 geometry.height += c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
321 diff_h = geometry.height - old_h;
323 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
325 diff_border = ev->border_width - c->border_width;
326 diff_h += diff_border;
327 diff_w += diff_border;
329 luaA_object_push(globalconf.L, c);
330 window_set_border_width(globalconf.L, -1, ev->border_width);
331 lua_pop(globalconf.L, 1);
334 /* If the client resizes without moving itself, apply window gravity */
335 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY)
337 int16_t diff_x = 0, diff_y = 0;
338 xwindow_translate_for_gravity(c->size_hints.win_gravity, diff_border, diff_border, diff_w, diff_h, &diff_x, &diff_y);
339 if(!(ev->value_mask & XCB_CONFIG_WINDOW_X))
340 geometry.x += diff_x;
341 if(!(ev->value_mask & XCB_CONFIG_WINDOW_Y))
342 geometry.y += diff_y;
345 if(!client_resize(c, geometry, false))
346 /* ICCCM 4.1.5 / 4.2.3, if nothing was changed, send an event saying so */
347 client_send_configure(c);
349 else
350 event_handle_configurerequest_configure_window(ev);
353 /** The configure notify event handler.
354 * \param ev The event.
356 static void
357 event_handle_configurenotify(xcb_configure_notify_event_t *ev)
359 const xcb_screen_t *screen = globalconf.screen;
361 if(ev->window == screen->root
362 && (ev->width != screen->width_in_pixels
363 || ev->height != screen->height_in_pixels))
364 /* it's not that we panic, but restart */
365 awesome_restart();
368 /** The destroy notify event handler.
369 * \param ev The event.
371 static void
372 event_handle_destroynotify(xcb_destroy_notify_event_t *ev)
374 client_t *c;
376 if((c = client_getbywin(ev->window)))
377 client_unmanage(c, false);
378 else
379 for(int i = 0; i < globalconf.embedded.len; i++)
380 if(globalconf.embedded.tab[i].win == ev->window)
382 xembed_window_array_take(&globalconf.embedded, i);
383 luaA_systray_invalidate();
387 /** The motion notify event handler.
388 * \param ev The event.
390 static void
391 event_handle_motionnotify(xcb_motion_notify_event_t *ev)
393 drawin_t *w;
394 client_t *c;
396 globalconf.timestamp = ev->time;
398 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
399 return;
401 if((c = client_getbyframewin(ev->event)))
403 luaA_object_push(globalconf.L, c);
404 lua_pushnumber(globalconf.L, ev->event_x);
405 lua_pushnumber(globalconf.L, ev->event_y);
406 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
408 /* now check if a titlebar was "hit" */
409 int x = ev->event_x, y = ev->event_y;
410 drawable_t *d = client_get_drawable_offset(c, &x, &y);
411 if (d)
413 luaA_object_push_item(globalconf.L, -1, d);
414 lua_pushnumber(globalconf.L, x);
415 lua_pushnumber(globalconf.L, y);
416 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
417 lua_pop(globalconf.L, 1);
419 lua_pop(globalconf.L, 1);
422 if((w = drawin_getbywin(ev->event)))
424 luaA_object_push(globalconf.L, w);
425 luaA_object_push_item(globalconf.L, -1, w->drawable);
426 lua_pushnumber(globalconf.L, ev->event_x);
427 lua_pushnumber(globalconf.L, ev->event_y);
428 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
429 lua_pop(globalconf.L, 2);
433 /** The leave notify event handler.
434 * \param ev The event.
436 static void
437 event_handle_leavenotify(xcb_leave_notify_event_t *ev)
439 drawin_t *drawin;
440 client_t *c;
442 globalconf.timestamp = ev->time;
444 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
445 return;
447 if((c = client_getbyframewin(ev->event)))
449 luaA_object_push(globalconf.L, c);
450 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
451 drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
452 if (d)
454 luaA_object_push_item(globalconf.L, -1, d);
455 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
456 lua_pop(globalconf.L, 1);
458 lua_pop(globalconf.L, 1);
461 if((drawin = drawin_getbywin(ev->event)))
463 luaA_object_push(globalconf.L, drawin);
464 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
465 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
466 lua_pop(globalconf.L, 2);
470 /** The enter notify event handler.
471 * \param ev The event.
473 static void
474 event_handle_enternotify(xcb_enter_notify_event_t *ev)
476 client_t *c;
477 drawin_t *drawin;
479 globalconf.timestamp = ev->time;
481 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
482 return;
484 if((drawin = drawin_getbywin(ev->event)))
486 luaA_object_push(globalconf.L, drawin);
487 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
488 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
489 lua_pop(globalconf.L, 2);
492 if((c = client_getbyframewin(ev->event)))
494 luaA_object_push(globalconf.L, c);
495 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
496 drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
497 if (d)
499 luaA_object_push_item(globalconf.L, -1, d);
500 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
501 lua_pop(globalconf.L, 1);
503 lua_pop(globalconf.L, 1);
507 /** The focus in event handler.
508 * \param ev The event.
510 static void
511 event_handle_focusin(xcb_focus_in_event_t *ev)
513 if (ev->mode == XCB_NOTIFY_MODE_GRAB
514 || ev->mode == XCB_NOTIFY_MODE_UNGRAB)
515 /* Ignore focus changes due to keyboard grabs */
516 return;
518 /* Events that we are interested in: */
519 switch(ev->detail)
521 /* These are events that jump between root windows.
523 case XCB_NOTIFY_DETAIL_ANCESTOR:
524 case XCB_NOTIFY_DETAIL_INFERIOR:
526 /* These are events that jump between clients.
527 * Virtual events ensure we always get an event on our top-level window.
529 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
530 case XCB_NOTIFY_DETAIL_NONLINEAR:
532 client_t *c;
534 if((c = client_getbywin(ev->event))) {
535 /* If there is still a pending focus change, do it now. */
536 client_focus_refresh();
537 client_focus_update(c);
540 /* all other events are ignored */
541 default:
542 break;
546 /** The expose event handler.
547 * \param ev The event.
549 static void
550 event_handle_expose(xcb_expose_event_t *ev)
552 drawin_t *drawin;
553 client_t *client;
555 if((drawin = drawin_getbywin(ev->window)))
556 drawin_refresh_pixmap_partial(drawin,
557 ev->x, ev->y,
558 ev->width, ev->height);
559 if ((client = client_getbyframewin(ev->window)))
560 client_refresh_partial(client, ev->x, ev->y, ev->width, ev->height);
563 /** The key press event handler.
564 * \param ev The event.
566 static void
567 event_handle_key(xcb_key_press_event_t *ev)
569 globalconf.timestamp = ev->time;
571 if(globalconf.keygrabber != LUA_REFNIL)
573 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
574 if(keygrabber_handlekpress(globalconf.L, ev))
576 if(lua_pcall(globalconf.L, 3, 1, 0))
578 warn("error running function: %s", lua_tostring(globalconf.L, -1));
579 luaA_keygrabber_stop(globalconf.L);
582 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
584 else
586 /* get keysym ignoring all modifiers */
587 xcb_keysym_t keysym = keyresolv_get_keysym(ev->detail, 0);
588 client_t *c;
589 if((c = client_getbyframewin(ev->event)))
591 luaA_object_push(globalconf.L, c);
592 event_key_callback(ev, &c->keys, -1, 1, &keysym);
594 else
595 event_key_callback(ev, &globalconf.keys, 0, 0, &keysym);
599 /** The map request event handler.
600 * \param ev The event.
602 static void
603 event_handle_maprequest(xcb_map_request_event_t *ev)
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->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 client_manage(ev->window, geom_r, wa_r);
647 p_delete(&geom_r);
650 bailout:
651 p_delete(&wa_r);
654 /** The unmap notify event handler.
655 * \param ev The event.
657 static void
658 event_handle_unmapnotify(xcb_unmap_notify_event_t *ev)
660 client_t *c;
662 if((c = client_getbywin(ev->window)))
663 client_unmanage(c, true);
664 else
665 for(int i = 0; i < globalconf.embedded.len; i++)
666 if(globalconf.embedded.tab[i].win == ev->window)
668 xembed_window_array_take(&globalconf.embedded, i);
669 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, ev->window);
670 luaA_systray_invalidate();
674 /** The randr screen change notify event handler.
675 * \param ev The event.
677 static void
678 event_handle_randr_screen_change_notify(xcb_randr_screen_change_notify_event_t *ev)
680 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
681 * (only the code relevant to RRScreenChangeNotify) as the latter
682 * doesn't provide this kind of function */
683 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
684 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->height, ev->width,
685 ev->mheight, ev->mwidth);
686 else
687 xcb_randr_set_screen_size(globalconf.connection, ev->root, ev->width, ev->height,
688 ev->mwidth, ev->mheight);
690 /* XRRUpdateConfiguration also executes the following instruction
691 * but it's not useful because SubpixelOrder is not used at all at
692 * the moment
694 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
697 awesome_restart();
700 /** The shape notify event handler.
701 * \param ev The event.
703 static void
704 event_handle_shape_notify(xcb_shape_notify_event_t *ev)
706 client_t *c = client_getbywin(ev->affected_window);
707 if (c)
709 luaA_object_push(globalconf.L, c);
710 if (ev->shape_kind == XCB_SHAPE_SK_BOUNDING)
711 luaA_object_emit_signal(globalconf.L, -1, "property::shape_client_bounding", 0);
712 if (ev->shape_kind == XCB_SHAPE_SK_CLIP)
713 luaA_object_emit_signal(globalconf.L, -1, "property::shape_client_clip", 0);
714 lua_pop(globalconf.L, 1);
718 /** The client message event handler.
719 * \param ev The event.
721 static void
722 event_handle_clientmessage(xcb_client_message_event_t *ev)
724 /* check for startup notification messages */
725 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
726 return;
728 if(ev->type == WM_CHANGE_STATE)
730 client_t *c;
731 if((c = client_getbywin(ev->window))
732 && ev->format == 32
733 && ev->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC)
735 luaA_object_push(globalconf.L, c);
736 client_set_minimized(globalconf.L, -1, true);
737 lua_pop(globalconf.L, 1);
740 else if(ev->type == _XEMBED)
741 xembed_process_client_message(ev);
742 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
743 systray_process_client_message(ev);
744 else
745 ewmh_process_client_message(ev);
748 /** The keymap change notify event handler.
749 * \param ev The event.
751 static void
752 event_handle_mappingnotify(xcb_mapping_notify_event_t *ev)
754 if(ev->request == XCB_MAPPING_MODIFIER
755 || ev->request == XCB_MAPPING_KEYBOARD)
757 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
758 xcb_get_modifier_mapping_unchecked(globalconf.connection);
760 /* Free and then allocate the key symbols */
761 xcb_key_symbols_free(globalconf.keysyms);
762 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
764 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
765 globalconf.keysyms, &globalconf.numlockmask,
766 &globalconf.shiftlockmask, &globalconf.capslockmask,
767 &globalconf.modeswitchmask);
769 /* regrab everything */
770 xcb_screen_t *s = globalconf.screen;
771 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
772 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
773 xwindow_grabkeys(s->root, &globalconf.keys);
775 foreach(_c, globalconf.clients)
777 client_t *c = *_c;
778 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->frame_window, XCB_BUTTON_MASK_ANY);
779 xwindow_grabkeys(c->frame_window, &c->keys);
784 static void
785 event_handle_reparentnotify(xcb_reparent_notify_event_t *ev)
787 client_t *c;
789 if((c = client_getbywin(ev->window)) && c->frame_window != ev->parent)
791 /* Ignore reparents to the root window, they *might* be caused by
792 * ourselves if a client quickly unmaps and maps itself again. */
793 if (ev->parent != globalconf.screen->root)
794 client_unmanage(c, true);
798 /** \brief awesome xerror function.
799 * There's no way to check accesses to destroyed windows, thus those cases are
800 * ignored (especially on UnmapNotify's).
801 * \param e The error event.
803 static void
804 xerror(xcb_generic_error_t *e)
806 /* ignore this */
807 if(e->error_code == XCB_WINDOW
808 || (e->error_code == XCB_MATCH
809 && e->major_code == XCB_SET_INPUT_FOCUS)
810 || (e->error_code == XCB_VALUE
811 && e->major_code == XCB_KILL_CLIENT)
812 || (e->error_code == XCB_MATCH
813 && e->major_code == XCB_CONFIGURE_WINDOW))
814 return;
816 warn("X error: request=%s (major %d, minor %d), error=%s (%d)",
817 xcb_event_get_request_label(e->major_code),
818 e->major_code, e->minor_code,
819 xcb_event_get_error_label(e->error_code),
820 e->error_code);
822 return;
825 void event_handle(xcb_generic_event_t *event)
827 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
829 if(response_type == 0)
831 /* This is an error, not a event */
832 xerror((xcb_generic_error_t *) event);
833 return;
836 switch(response_type)
838 #define EVENT(type, callback) case type: callback((void *) event); return
839 EVENT(XCB_BUTTON_PRESS, event_handle_button);
840 EVENT(XCB_BUTTON_RELEASE, event_handle_button);
841 EVENT(XCB_CONFIGURE_REQUEST, event_handle_configurerequest);
842 EVENT(XCB_CONFIGURE_NOTIFY, event_handle_configurenotify);
843 EVENT(XCB_DESTROY_NOTIFY, event_handle_destroynotify);
844 EVENT(XCB_ENTER_NOTIFY, event_handle_enternotify);
845 EVENT(XCB_CLIENT_MESSAGE, event_handle_clientmessage);
846 EVENT(XCB_EXPOSE, event_handle_expose);
847 EVENT(XCB_FOCUS_IN, event_handle_focusin);
848 EVENT(XCB_KEY_PRESS, event_handle_key);
849 EVENT(XCB_KEY_RELEASE, event_handle_key);
850 EVENT(XCB_LEAVE_NOTIFY, event_handle_leavenotify);
851 EVENT(XCB_MAPPING_NOTIFY, event_handle_mappingnotify);
852 EVENT(XCB_MAP_REQUEST, event_handle_maprequest);
853 EVENT(XCB_MOTION_NOTIFY, event_handle_motionnotify);
854 EVENT(XCB_PROPERTY_NOTIFY, property_handle_propertynotify);
855 EVENT(XCB_REPARENT_NOTIFY, event_handle_reparentnotify);
856 EVENT(XCB_UNMAP_NOTIFY, event_handle_unmapnotify);
857 #undef EVENT
860 static uint8_t randr_screen_change_notify = 0;
861 static uint8_t shape_notify = 0;
863 if(randr_screen_change_notify == 0)
865 /* check for randr extension */
866 const xcb_query_extension_reply_t *randr_query;
867 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
868 if(randr_query->present)
869 randr_screen_change_notify = randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY;
871 if(shape_notify == 0)
873 /* check for shape extension */
874 const xcb_query_extension_reply_t *shape_query;
875 shape_query = xcb_get_extension_data(globalconf.connection, &xcb_shape_id);
876 if(shape_query->present)
877 shape_notify = shape_query->first_event + XCB_SHAPE_NOTIFY;
880 if (response_type == randr_screen_change_notify)
881 event_handle_randr_screen_change_notify((void *) event);
882 if (response_type == shape_notify)
883 event_handle_shape_notify((void *) event);
886 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80