Improve fatal signal handling
[awesome.git] / event.c
blob1881a9e2ad52274f086902e0a90e5aa27b54a6e3
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/shape.h>
25 #include <xcb/xcb_atom.h>
26 #include <xcb/xcb_icccm.h>
27 #include <xcb/xcb_event.h>
29 #include "awesome.h"
30 #include "event.h"
31 #include "property.h"
32 #include "objects/tag.h"
33 #include "objects/drawin.h"
34 #include "xwindow.h"
35 #include "ewmh.h"
36 #include "objects/client.h"
37 #include "keyresolv.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 /** 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);
200 else if((c = client_getbyframewin(ev->event)))
202 luaA_object_push(globalconf.L, c);
203 /* And handle the button raw button event */
204 event_emit_button(ev);
205 /* then check if a titlebar was "hit" */
206 int x = ev->event_x, y = ev->event_y;
207 drawable_t *d = client_get_drawable_offset(c, &x, &y);
208 if (d)
210 /* Copy the event so that we can fake x/y */
211 xcb_button_press_event_t event = *ev;
212 event.event_x = x;
213 event.event_y = y;
214 luaA_object_push_item(globalconf.L, -1, d);
215 event_emit_button(&event);
216 lua_pop(globalconf.L, 1);
218 /* then check if any button objects match */
219 event_button_callback(ev, &c->buttons, -1, 1, NULL);
220 xcb_allow_events(globalconf.connection,
221 XCB_ALLOW_REPLAY_POINTER,
222 XCB_CURRENT_TIME);
224 else if(ev->child == XCB_NONE)
225 if(globalconf.screen->root == ev->event)
227 event_button_callback(ev, &globalconf.buttons, 0, 0, NULL);
228 return;
232 static void
233 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
235 uint16_t config_win_mask = 0;
236 uint32_t config_win_vals[7];
237 unsigned short i = 0;
239 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
241 config_win_mask |= XCB_CONFIG_WINDOW_X;
242 config_win_vals[i++] = ev->x;
244 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
246 config_win_mask |= XCB_CONFIG_WINDOW_Y;
247 config_win_vals[i++] = ev->y;
249 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
251 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
252 config_win_vals[i++] = ev->width;
254 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
256 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
257 config_win_vals[i++] = ev->height;
259 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
261 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
262 config_win_vals[i++] = ev->border_width;
264 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
266 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
267 config_win_vals[i++] = ev->sibling;
269 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
271 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
272 config_win_vals[i++] = ev->stack_mode;
275 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
278 /** The configure event handler.
279 * \param ev The event.
281 static void
282 event_handle_configurerequest(xcb_configure_request_event_t *ev)
284 client_t *c;
286 if((c = client_getbywin(ev->window)))
288 area_t geometry = c->geometry;
289 int16_t diff_w = 0, diff_h = 0, diff_border = 0;
291 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
292 geometry.x = ev->x;
293 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
294 geometry.y = ev->y;
295 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
297 uint16_t old_w = geometry.width;
298 geometry.width = ev->width;
299 /* The ConfigureRequest specifies the size of the client window, we want the frame */
300 geometry.width += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
301 geometry.width += c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
302 diff_w = geometry.width - old_w;
304 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
306 uint16_t old_h = geometry.height;
307 geometry.height = ev->height;
308 /* The ConfigureRequest specifies the size of the client window, we want the frame */
309 geometry.height += c->titlebar[CLIENT_TITLEBAR_TOP].size;
310 geometry.height += c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
311 diff_h = geometry.height - old_h;
313 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
315 diff_border = ev->border_width - c->border_width;
316 diff_h += diff_border;
317 diff_w += diff_border;
319 luaA_object_push(globalconf.L, c);
320 window_set_border_width(globalconf.L, -1, ev->border_width);
321 lua_pop(globalconf.L, 1);
324 /* If the client resizes without moving itself, apply window gravity */
325 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY)
327 int16_t diff_x = 0, diff_y = 0;
328 xwindow_translate_for_gravity(c->size_hints.win_gravity, diff_border, diff_border, diff_w, diff_h, &diff_x, &diff_y);
329 if(!(ev->value_mask & XCB_CONFIG_WINDOW_X))
330 geometry.x += diff_x;
331 if(!(ev->value_mask & XCB_CONFIG_WINDOW_Y))
332 geometry.y += diff_y;
335 if(!client_resize(c, geometry, false))
336 /* ICCCM 4.1.5 / 4.2.3, if nothing was changed, send an event saying so */
337 client_send_configure(c);
339 else
340 event_handle_configurerequest_configure_window(ev);
343 /** The configure notify event handler.
344 * \param ev The event.
346 static void
347 event_handle_configurenotify(xcb_configure_notify_event_t *ev)
349 const xcb_screen_t *screen = globalconf.screen;
351 if(ev->window == screen->root
352 && (ev->width != screen->width_in_pixels
353 || ev->height != screen->height_in_pixels))
354 /* it's not that we panic, but restart */
355 awesome_restart();
358 /** The destroy notify event handler.
359 * \param ev The event.
361 static void
362 event_handle_destroynotify(xcb_destroy_notify_event_t *ev)
364 client_t *c;
366 if((c = client_getbywin(ev->window)))
367 client_unmanage(c, false);
368 else
369 for(int i = 0; i < globalconf.embedded.len; i++)
370 if(globalconf.embedded.tab[i].win == ev->window)
372 xembed_window_array_take(&globalconf.embedded, i);
373 luaA_systray_invalidate();
377 /** The motion notify event handler.
378 * \param ev The event.
380 static void
381 event_handle_motionnotify(xcb_motion_notify_event_t *ev)
383 drawin_t *w;
384 client_t *c;
386 globalconf.timestamp = ev->time;
388 if(event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state))
389 return;
391 if((c = client_getbyframewin(ev->event)))
393 luaA_object_push(globalconf.L, c);
394 lua_pushnumber(globalconf.L, ev->event_x);
395 lua_pushnumber(globalconf.L, ev->event_y);
396 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
398 /* now check if a titlebar was "hit" */
399 int x = ev->event_x, y = ev->event_y;
400 drawable_t *d = client_get_drawable_offset(c, &x, &y);
401 if (d)
403 luaA_object_push_item(globalconf.L, -1, d);
404 lua_pushnumber(globalconf.L, x);
405 lua_pushnumber(globalconf.L, y);
406 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
407 lua_pop(globalconf.L, 1);
409 lua_pop(globalconf.L, 1);
412 if((w = drawin_getbywin(ev->event)))
414 luaA_object_push(globalconf.L, w);
415 luaA_object_push_item(globalconf.L, -1, w->drawable);
416 lua_pushnumber(globalconf.L, ev->event_x);
417 lua_pushnumber(globalconf.L, ev->event_y);
418 luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2);
419 lua_pop(globalconf.L, 2);
423 /** The leave notify event handler.
424 * \param ev The event.
426 static void
427 event_handle_leavenotify(xcb_leave_notify_event_t *ev)
429 drawin_t *drawin;
430 client_t *c;
432 globalconf.timestamp = ev->time;
434 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
435 return;
437 if((c = client_getbyframewin(ev->event)))
439 luaA_object_push(globalconf.L, c);
440 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
441 drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
442 if (d)
444 luaA_object_push_item(globalconf.L, -1, d);
445 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
446 lua_pop(globalconf.L, 1);
448 lua_pop(globalconf.L, 1);
451 if((drawin = drawin_getbywin(ev->event)))
453 luaA_object_push(globalconf.L, drawin);
454 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
455 luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0);
456 lua_pop(globalconf.L, 2);
460 /** The enter notify event handler.
461 * \param ev The event.
463 static void
464 event_handle_enternotify(xcb_enter_notify_event_t *ev)
466 client_t *c;
467 drawin_t *drawin;
469 globalconf.timestamp = ev->time;
471 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
472 return;
474 if((drawin = drawin_getbywin(ev->event)))
476 luaA_object_push(globalconf.L, drawin);
477 luaA_object_push_item(globalconf.L, -1, drawin->drawable);
478 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
479 lua_pop(globalconf.L, 2);
482 if((c = client_getbyframewin(ev->event)))
484 luaA_object_push(globalconf.L, c);
485 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
486 drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y);
487 if (d)
489 luaA_object_push_item(globalconf.L, -1, d);
490 luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0);
491 lua_pop(globalconf.L, 1);
493 lua_pop(globalconf.L, 1);
497 /** The focus in event handler.
498 * \param ev The event.
500 static void
501 event_handle_focusin(xcb_focus_in_event_t *ev)
503 if (ev->mode == XCB_NOTIFY_MODE_GRAB
504 || ev->mode == XCB_NOTIFY_MODE_UNGRAB)
505 /* Ignore focus changes due to keyboard grabs */
506 return;
508 /* Events that we are interested in: */
509 switch(ev->detail)
511 /* These are events that jump between root windows.
513 case XCB_NOTIFY_DETAIL_ANCESTOR:
514 case XCB_NOTIFY_DETAIL_INFERIOR:
516 /* These are events that jump between clients.
517 * Virtual events ensure we always get an event on our top-level window.
519 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
520 case XCB_NOTIFY_DETAIL_NONLINEAR:
522 client_t *c;
524 if((c = client_getbywin(ev->event))) {
525 /* If there is still a pending focus change, do it now. */
526 client_focus_refresh();
527 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 shape notify event handler.
691 * \param ev The event.
693 static void
694 event_handle_shape_notify(xcb_shape_notify_event_t *ev)
696 client_t *c = client_getbywin(ev->affected_window);
697 if (c)
699 luaA_object_push(globalconf.L, c);
700 if (ev->shape_kind == XCB_SHAPE_SK_BOUNDING)
701 luaA_object_emit_signal(globalconf.L, -1, "property::shape_client_bounding", 0);
702 if (ev->shape_kind == XCB_SHAPE_SK_CLIP)
703 luaA_object_emit_signal(globalconf.L, -1, "property::shape_client_clip", 0);
704 lua_pop(globalconf.L, 1);
708 /** The client message event handler.
709 * \param ev The event.
711 static void
712 event_handle_clientmessage(xcb_client_message_event_t *ev)
714 /* check for startup notification messages */
715 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
716 return;
718 if(ev->type == WM_CHANGE_STATE)
720 client_t *c;
721 if((c = client_getbywin(ev->window))
722 && ev->format == 32
723 && ev->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC)
725 luaA_object_push(globalconf.L, c);
726 client_set_minimized(globalconf.L, -1, true);
727 lua_pop(globalconf.L, 1);
730 else if(ev->type == _XEMBED)
731 xembed_process_client_message(ev);
732 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
733 systray_process_client_message(ev);
734 else
735 ewmh_process_client_message(ev);
738 /** The keymap change notify event handler.
739 * \param ev The event.
741 static void
742 event_handle_mappingnotify(xcb_mapping_notify_event_t *ev)
744 if(ev->request == XCB_MAPPING_MODIFIER
745 || ev->request == XCB_MAPPING_KEYBOARD)
747 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
748 xcb_get_modifier_mapping_unchecked(globalconf.connection);
750 /* Free and then allocate the key symbols */
751 xcb_key_symbols_free(globalconf.keysyms);
752 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
754 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
755 globalconf.keysyms, &globalconf.numlockmask,
756 &globalconf.shiftlockmask, &globalconf.capslockmask,
757 &globalconf.modeswitchmask);
759 /* regrab everything */
760 xcb_screen_t *s = globalconf.screen;
761 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
762 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
763 xwindow_grabkeys(s->root, &globalconf.keys);
765 foreach(_c, globalconf.clients)
767 client_t *c = *_c;
768 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->frame_window, XCB_BUTTON_MASK_ANY);
769 xwindow_grabkeys(c->frame_window, &c->keys);
774 static void
775 event_handle_reparentnotify(xcb_reparent_notify_event_t *ev)
777 client_t *c;
779 if((c = client_getbywin(ev->window)) && c->frame_window != ev->parent)
781 /* Ignore reparents to the root window, they *might* be caused by
782 * ourselves if a client quickly unmaps and maps itself again. */
783 if (ev->parent != globalconf.screen->root)
784 client_unmanage(c, true);
788 /** \brief awesome xerror function.
789 * There's no way to check accesses to destroyed windows, thus those cases are
790 * ignored (especially on UnmapNotify's).
791 * \param e The error event.
793 static void
794 xerror(xcb_generic_error_t *e)
796 /* ignore this */
797 if(e->error_code == XCB_WINDOW
798 || (e->error_code == XCB_MATCH
799 && e->major_code == XCB_SET_INPUT_FOCUS)
800 || (e->error_code == XCB_VALUE
801 && e->major_code == XCB_KILL_CLIENT)
802 || (e->error_code == XCB_MATCH
803 && e->major_code == XCB_CONFIGURE_WINDOW))
804 return;
806 warn("X error: request=%s (major %d, minor %d), error=%s (%d)",
807 xcb_event_get_request_label(e->major_code),
808 e->major_code, e->minor_code,
809 xcb_event_get_error_label(e->error_code),
810 e->error_code);
812 return;
815 void event_handle(xcb_generic_event_t *event)
817 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
819 if(response_type == 0)
821 /* This is an error, not a event */
822 xerror((xcb_generic_error_t *) event);
823 return;
826 switch(response_type)
828 #define EVENT(type, callback) case type: callback((void *) event); return
829 EVENT(XCB_BUTTON_PRESS, event_handle_button);
830 EVENT(XCB_BUTTON_RELEASE, event_handle_button);
831 EVENT(XCB_CONFIGURE_REQUEST, event_handle_configurerequest);
832 EVENT(XCB_CONFIGURE_NOTIFY, event_handle_configurenotify);
833 EVENT(XCB_DESTROY_NOTIFY, event_handle_destroynotify);
834 EVENT(XCB_ENTER_NOTIFY, event_handle_enternotify);
835 EVENT(XCB_CLIENT_MESSAGE, event_handle_clientmessage);
836 EVENT(XCB_EXPOSE, event_handle_expose);
837 EVENT(XCB_FOCUS_IN, event_handle_focusin);
838 EVENT(XCB_KEY_PRESS, event_handle_key);
839 EVENT(XCB_KEY_RELEASE, event_handle_key);
840 EVENT(XCB_LEAVE_NOTIFY, event_handle_leavenotify);
841 EVENT(XCB_MAPPING_NOTIFY, event_handle_mappingnotify);
842 EVENT(XCB_MAP_REQUEST, event_handle_maprequest);
843 EVENT(XCB_MOTION_NOTIFY, event_handle_motionnotify);
844 EVENT(XCB_PROPERTY_NOTIFY, property_handle_propertynotify);
845 EVENT(XCB_REPARENT_NOTIFY, event_handle_reparentnotify);
846 EVENT(XCB_UNMAP_NOTIFY, event_handle_unmapnotify);
847 #undef EVENT
850 static uint8_t randr_screen_change_notify = 0;
851 static uint8_t shape_notify = 0;
853 if(randr_screen_change_notify == 0)
855 /* check for randr extension */
856 const xcb_query_extension_reply_t *randr_query;
857 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
858 if(randr_query->present)
859 randr_screen_change_notify = randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY;
861 if(shape_notify == 0)
863 /* check for shape extension */
864 const xcb_query_extension_reply_t *shape_query;
865 shape_query = xcb_get_extension_data(globalconf.connection, &xcb_shape_id);
866 if(shape_query->present)
867 shape_notify = shape_query->first_event + XCB_SHAPE_NOTIFY;
870 if (response_type == randr_screen_change_notify)
871 event_handle_randr_screen_change_notify((void *) event);
872 if (response_type == shape_notify)
873 event_handle_shape_notify((void *) event);
876 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80