luaa: use buffer rather than asprintf()
[awesome.git] / event.c
blob41dbc8cda8532bc3ae228d17d5de3c67aa9e78eb
1 /*
2 * event.c - event handlers
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <xcb/xcb.h>
23 #include <xcb/randr.h>
24 #include <xcb/xcb_atom.h>
25 #include <xcb/xcb_icccm.h>
26 #include <xcb/xcb_event.h>
28 #include "awesome.h"
29 #include "event.h"
30 #include "tag.h"
31 #include "window.h"
32 #include "ewmh.h"
33 #include "client.h"
34 #include "widget.h"
35 #include "titlebar.h"
36 #include "key.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 EVENT_BUTTON_MATCH(b, state, xbutton) \
46 ((!(b)->button || (xbutton) == (b)->button) \
47 && ((b)->mod == XCB_BUTTON_MASK_ANY || (b)->mod == (state)))
49 /** Handle mouse button events.
50 * \param c The client on which the event happened or NULL.
51 * \param type Event type, press or release.
52 * \param button Button number.
53 * \param state Modkeys state.
54 * \param buttons Buttons array to check for.
56 static void
57 event_handle_mouse_button(client_t *c,
58 uint8_t type,
59 xcb_button_t button,
60 uint16_t state,
61 button_array_t *buttons)
63 foreach(b, *buttons)
64 if(EVENT_BUTTON_MATCH(*b, state, button))
65 switch(type)
67 case XCB_BUTTON_PRESS:
68 if((*b)->press != LUA_REFNIL)
70 if(c)
72 client_push(globalconf.L, c);
73 luaA_dofunction(globalconf.L, (*b)->press, 1, 0);
75 else
76 luaA_dofunction(globalconf.L, (*b)->press, 0, 0);
78 break;
79 case XCB_BUTTON_RELEASE:
80 if((*b)->release != LUA_REFNIL)
82 if(c)
84 client_push(globalconf.L, c);
85 luaA_dofunction(globalconf.L, (*b)->release, 1, 0);
87 else
88 luaA_dofunction(globalconf.L, (*b)->release, 0, 0);
90 break;
94 /** Handle an event with mouse grabber if needed
95 * \param x The x coordinate.
96 * \param y The y coordinate.
97 * \param mask The mask buttons.
99 static void
100 event_handle_mousegrabber(int x, int y, uint16_t mask)
102 if(globalconf.mousegrabber != LUA_REFNIL)
104 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
105 mousegrabber_handleevent(globalconf.L, x, y, mask);
106 if(lua_pcall(globalconf.L, 1, 1, 0))
108 warn("error running function: %s", lua_tostring(globalconf.L, -1));
109 luaA_mousegrabber_stop(globalconf.L);
111 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
112 luaA_mousegrabber_stop(globalconf.L);
113 lua_pop(globalconf.L, 1); /* pop returned value */
117 /** The button press event handler.
118 * \param data The type of mouse event.
119 * \param connection The connection to the X server.
120 * \param ev The event.
122 static int
123 event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev)
125 int screen;
126 const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection));
127 client_t *c;
128 wibox_t *wibox;
130 /* ev->state is
131 * button status (8 bits) + modifiers status (8 bits)
132 * we don't care for button status that we get, especially on release, so
133 * drop them */
134 ev->state &= 0x00ff;
136 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
138 if((wibox = wibox_getbywin(ev->event))
139 || (wibox = wibox_getbywin(ev->child)))
141 /* If the wibox is child, then x,y are
142 * relative to root window */
143 if(wibox->sw.window == ev->child)
145 ev->event_x -= wibox->sw.geometry.x;
146 ev->event_y -= wibox->sw.geometry.y;
149 /* check if we match a binding on the wibox */
150 foreach(b, wibox->buttons)
151 if(EVENT_BUTTON_MATCH(*b, ev->state, ev->detail))
152 switch(ev->response_type)
154 case XCB_BUTTON_PRESS:
155 if((*b)->press != LUA_REFNIL)
157 wibox_push(globalconf.L, wibox);
158 luaA_dofunction(globalconf.L, (*b)->press, 1, 0);
160 break;
161 case XCB_BUTTON_RELEASE:
162 if((*b)->release != LUA_REFNIL)
164 wibox_push(globalconf.L, wibox);
165 luaA_dofunction(globalconf.L, (*b)->release, 1, 0);
167 break;
170 /* then try to match a widget binding */
171 widget_t *w = widget_getbycoords(wibox->position, &wibox->widgets,
172 wibox->sw.geometry.width,
173 wibox->sw.geometry.height,
174 &ev->event_x, &ev->event_y);
175 if(w)
176 foreach(b, w->buttons)
177 if(EVENT_BUTTON_MATCH(*b, ev->state, ev->detail))
178 switch(ev->response_type)
180 case XCB_BUTTON_PRESS:
181 if((*b)->press != LUA_REFNIL)
183 wibox_push(globalconf.L, wibox);
184 luaA_dofunction(globalconf.L, (*b)->press, 1, 0);
186 break;
187 case XCB_BUTTON_RELEASE:
188 if((*b)->release != LUA_REFNIL)
190 wibox_push(globalconf.L, wibox);
191 luaA_dofunction(globalconf.L, (*b)->release, 1, 0);
193 break;
196 /* return even if no widget match */
197 return 0;
199 else if((c = client_getbywin(ev->event)))
201 event_handle_mouse_button(c, ev->response_type, ev->detail, ev->state, &c->buttons);
202 xcb_allow_events(globalconf.connection,
203 XCB_ALLOW_REPLAY_POINTER,
204 XCB_CURRENT_TIME);
206 else if(ev->child == XCB_NONE)
207 for(screen = 0; screen < nb_screen; screen++)
208 if(xutil_screen_get(connection, screen)->root == ev->event)
210 event_handle_mouse_button(NULL, ev->response_type, ev->detail, ev->state, &globalconf.buttons);
211 return 0;
214 return 0;
217 static void
218 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
220 uint16_t config_win_mask = 0;
221 uint32_t config_win_vals[7];
222 unsigned short i = 0;
224 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
226 config_win_mask |= XCB_CONFIG_WINDOW_X;
227 config_win_vals[i++] = ev->x;
229 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
231 config_win_mask |= XCB_CONFIG_WINDOW_Y;
232 config_win_vals[i++] = ev->y;
234 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
236 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
237 config_win_vals[i++] = ev->width;
239 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
241 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
242 config_win_vals[i++] = ev->height;
244 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
246 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
247 config_win_vals[i++] = ev->border_width;
249 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
251 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
252 config_win_vals[i++] = ev->sibling;
254 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
256 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
257 config_win_vals[i++] = ev->stack_mode;
260 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
263 /** The configure event handler.
264 * \param data currently unused.
265 * \param connection The connection to the X server.
266 * \param ev The event.
268 static int
269 event_handle_configurerequest(void *data __attribute__ ((unused)),
270 xcb_connection_t *connection, xcb_configure_request_event_t *ev)
272 client_t *c;
273 area_t geometry;
275 if((c = client_getbywin(ev->window)))
277 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
279 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
280 geometry.x = ev->x;
281 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
282 geometry.y = ev->y;
283 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
284 geometry.width = ev->width;
285 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
286 geometry.height = ev->height;
288 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
289 client_setborder(c, ev->border_width);
291 /* Clients are not allowed to directly mess with stacking parameters. */
292 ev->value_mask &= ~(XCB_CONFIG_WINDOW_SIBLING |
293 XCB_CONFIG_WINDOW_STACK_MODE);
295 if(c->isbanned)
297 /* We'll be sending protocol geometry, so don't readd borders and titlebar. */
298 /* We do have to ensure the windows don't end up in the visible screen. */
299 ev->x = geometry.x = - (geometry.width + 2*c->border);
300 ev->y = geometry.y = - (geometry.height + 2*c->border);
301 window_configure(c->win, geometry, c->border);
302 event_handle_configurerequest_configure_window(ev);
304 else
306 /** Configure request are sent with size relative to real (internal)
307 * window size, i.e. without titlebars and borders. */
308 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
310 if(client_resize(c, geometry, false))
312 /* All the wiboxes (may) need to be repositioned. */
313 if(client_hasstrut(c))
314 wibox_update_positions();
315 client_need_arrange(c);
317 else
319 /* Resize wasn't officially needed, but we don't want to break expectations. */
320 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
321 window_configure(c->win, geometry, c->border);
325 else
326 event_handle_configurerequest_configure_window(ev);
328 return 0;
331 /** The configure notify event handler.
332 * \param data currently unused.
333 * \param connection The connection to the X server.
334 * \param ev The event.
336 static int
337 event_handle_configurenotify(void *data __attribute__ ((unused)),
338 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
340 int screen_nbr;
341 const xcb_screen_t *screen;
343 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
344 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
345 && ev->window == screen->root
346 && (ev->width != screen->width_in_pixels
347 || ev->height != screen->height_in_pixels))
348 /* it's not that we panic, but restart */
349 awesome_restart();
351 return 0;
354 /** The destroy notify event handler.
355 * \param data currently unused.
356 * \param connection The connection to the X server.
357 * \param ev The event.
359 static int
360 event_handle_destroynotify(void *data __attribute__ ((unused)),
361 xcb_connection_t *connection __attribute__ ((unused)),
362 xcb_destroy_notify_event_t *ev)
364 client_t *c;
366 if((c = client_getbywin(ev->window)))
367 client_unmanage(c);
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 foreach(screen, globalconf.screens)
374 widget_invalidate_bytype(screen, widget_systray);
377 return 0;
380 /** Handle a motion notify over widgets.
381 * \param object The object.
382 * \param mouse_over The pointer to the registered mouse over widget.
383 * \param widget The new widget the mouse is over.
385 static void
386 event_handle_widget_motionnotify(void *object,
387 widget_t **mouse_over,
388 widget_t *widget)
390 if(widget != *mouse_over)
392 if(*mouse_over)
394 if((*mouse_over)->mouse_leave != LUA_REFNIL)
396 /* call mouse leave function on old widget */
397 widget_push(globalconf.L, *mouse_over);
398 luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
401 if(widget)
403 /* call mouse enter function on new widget and register it */
404 *mouse_over = widget;
405 if(widget->mouse_enter != LUA_REFNIL)
407 widget_push(globalconf.L, widget);
408 luaA_dofunction(globalconf.L, widget->mouse_enter, 1, 0);
414 /** The motion notify event handler.
415 * \param data currently unused.
416 * \param connection The connection to the X server.
417 * \param ev The event.
419 static int
420 event_handle_motionnotify(void *data __attribute__ ((unused)),
421 xcb_connection_t *connection,
422 xcb_motion_notify_event_t *ev)
424 wibox_t *wibox;
426 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
428 if((wibox = wibox_getbywin(ev->event)))
430 widget_t *w = widget_getbycoords(wibox->position, &wibox->widgets,
431 wibox->sw.geometry.width,
432 wibox->sw.geometry.height,
433 &ev->event_x, &ev->event_y);
434 if(w)
435 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
438 return 0;
441 /** The leave notify event handler.
442 * \param data currently unused.
443 * \param connection The connection to the X server.
444 * \param ev The event.
446 static int
447 event_handle_leavenotify(void *data __attribute__ ((unused)),
448 xcb_connection_t *connection,
449 xcb_leave_notify_event_t *ev)
451 wibox_t *wibox;
452 client_t *c;
454 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
455 return 0;
457 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
458 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
460 client_push(globalconf.L, c);
461 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
464 if((wibox = wibox_getbywin(ev->event)))
466 if(wibox->mouse_over)
468 if(wibox->mouse_over->mouse_leave != LUA_REFNIL)
470 /* call mouse leave function on widget the mouse was over */
471 wibox_push(globalconf.L, wibox);
472 luaA_dofunction(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
474 wibox->mouse_over = NULL;
477 if(wibox->mouse_leave != LUA_REFNIL)
478 luaA_dofunction(globalconf.L, wibox->mouse_leave, 0, 0);
481 return 0;
484 /** The enter notify event handler.
485 * \param data currently unused.
486 * \param connection The connection to the X server.
487 * \param ev The event.
489 static int
490 event_handle_enternotify(void *data __attribute__ ((unused)),
491 xcb_connection_t *connection,
492 xcb_enter_notify_event_t *ev)
494 client_t *c;
495 wibox_t *wibox;
497 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
498 return 0;
500 if((wibox = wibox_getbywin(ev->event)))
502 widget_t *w = widget_getbycoords(wibox->position, &wibox->widgets,
503 wibox->sw.geometry.width,
504 wibox->sw.geometry.height,
505 &ev->event_x, &ev->event_y);
506 if(w)
507 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
509 if(wibox->mouse_enter != LUA_REFNIL)
510 luaA_dofunction(globalconf.L, wibox->mouse_enter, 0, 0);
513 if((c = client_getbytitlebarwin(ev->event))
514 || (c = client_getbywin(ev->event)))
515 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
517 client_push(globalconf.L, c);
518 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
521 return 0;
524 /** The focus in event handler.
525 * \param data currently unused.
526 * \param connection The connection to the X server.
527 * \param ev The event.
529 static int
530 event_handle_focusin(void *data __attribute__ ((unused)),
531 xcb_connection_t *connection,
532 xcb_focus_in_event_t *ev)
534 client_t *c;
536 /* Events that we are interested in: */
537 switch(ev->detail)
539 /* These are events that jump between windows of a toplevel client.
541 * NotifyVirtual event is handled in case where NotifyAncestor or
542 * NotifyInferior event is not generated on window that is managed by
543 * awesome ( client_* returns NULL )
545 * Can someone explain exactly why they are needed ?
547 case XCB_NOTIFY_DETAIL_VIRTUAL:
548 case XCB_NOTIFY_DETAIL_ANCESTOR:
549 case XCB_NOTIFY_DETAIL_INFERIOR:
551 /* These are events that jump between clients.
552 * Virtual events ensure we always get an event on our top-level window.
554 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
555 case XCB_NOTIFY_DETAIL_NONLINEAR:
556 if((c = client_getbytitlebarwin(ev->event))
557 || (c = client_getbywin(ev->event)))
558 client_focus_update(c);
559 /* all other events are ignored */
560 default:
561 return 0;
565 /** The focus out event handler.
566 * \param data currently unused.
567 * \param connection The connection to the X server.
568 * \param ev The event.
570 static int
571 event_handle_focusout(void *data __attribute__ ((unused)),
572 xcb_connection_t *connection,
573 xcb_focus_out_event_t *ev)
575 client_t *c;
577 /* Events that we are interested in: */
578 switch(ev->detail)
580 /* These are events that jump between clients.
581 * Virtual events ensure we always get an event on our top-level window.
583 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
584 case XCB_NOTIFY_DETAIL_NONLINEAR:
585 if((c = client_getbytitlebarwin(ev->event))
586 || (c = client_getbywin(ev->event)))
587 client_unfocus_update(c);
588 /* all other events are ignored */
589 default:
590 return 0;
594 /** The expose event handler.
595 * \param data currently unused.
596 * \param connection The connection to the X server.
597 * \param ev The event.
599 static int
600 event_handle_expose(void *data __attribute__ ((unused)),
601 xcb_connection_t *connection __attribute__ ((unused)),
602 xcb_expose_event_t *ev)
604 wibox_t *wibox;
606 if((wibox = wibox_getbywin(ev->window)))
607 simplewindow_refresh_pixmap_partial(&wibox->sw,
608 ev->x, ev->y,
609 ev->width, ev->height);
611 return 0;
614 /** The key press event handler.
615 * \param data currently unused.
616 * \param connection The connection to the X server.
617 * \param ev The event.
619 static int
620 event_handle_key(void *data __attribute__ ((unused)),
621 xcb_connection_t *connection __attribute__ ((unused)),
622 xcb_key_press_event_t *ev)
624 client_t *c;
626 if(globalconf.keygrabber != LUA_REFNIL)
628 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
629 if(keygrabber_handlekpress(globalconf.L, ev))
631 if(lua_pcall(globalconf.L, 3, 1, 0))
633 warn("error running function: %s", lua_tostring(globalconf.L, -1));
634 luaA_keygrabber_stop(globalconf.L);
636 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
637 luaA_keygrabber_stop(globalconf.L);
639 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
641 else if((c = client_getbywin(ev->event)))
643 keyb_t *k = key_find(&c->keys, ev);
645 if(k)
646 switch(ev->response_type)
648 case XCB_KEY_PRESS:
649 if(k->press != LUA_REFNIL)
651 client_push(globalconf.L, c);
652 luaA_dofunction(globalconf.L, k->press, 1, 0);
654 break;
655 case XCB_KEY_RELEASE:
656 if(k->release != LUA_REFNIL)
658 client_push(globalconf.L, c);
659 luaA_dofunction(globalconf.L, k->release, 1, 0);
661 break;
664 else
666 keyb_t *k = key_find(&globalconf.keys, ev);
668 if(k)
669 switch(ev->response_type)
671 case XCB_KEY_PRESS:
672 if(k->press != LUA_REFNIL)
673 luaA_dofunction(globalconf.L, k->press, 0, 0);
674 break;
675 case XCB_KEY_RELEASE:
676 if(k->release != LUA_REFNIL)
677 luaA_dofunction(globalconf.L, k->release, 0, 0);
678 break;
682 return 0;
685 /** The map request event handler.
686 * \param data currently unused.
687 * \param connection The connection to the X server.
688 * \param ev The event.
690 static int
691 event_handle_maprequest(void *data __attribute__ ((unused)),
692 xcb_connection_t *connection, xcb_map_request_event_t *ev)
694 int phys_screen, ret = 0;
695 client_t *c;
696 xcb_get_window_attributes_cookie_t wa_c;
697 xcb_get_window_attributes_reply_t *wa_r;
698 xcb_get_geometry_cookie_t geom_c;
699 xcb_get_geometry_reply_t *geom_r;
701 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
703 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
704 return -1;
706 if(wa_r->override_redirect)
707 goto bailout;
709 if(xembed_getbywin(&globalconf.embedded, ev->window))
711 xcb_map_window(connection, ev->window);
712 xembed_window_activate(connection, ev->window);
714 else if((c = client_getbywin(ev->window)))
716 /* Check that it may be visible, but not asked to be hidden */
717 if(client_maybevisible(c, c->screen) && !c->ishidden)
719 client_setminimized(c, false);
720 /* it will be raised, so just update ourself */
721 client_raise(c);
724 else
726 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
728 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
730 ret = -1;
731 goto bailout;
734 phys_screen = xutil_root2screen(connection, geom_r->root);
736 client_manage(ev->window, geom_r, phys_screen, false);
738 p_delete(&geom_r);
741 bailout:
742 p_delete(&wa_r);
743 return ret;
746 /** The unmap notify event handler.
747 * \param data currently unused.
748 * \param connection The connection to the X server.
749 * \param ev The event.
751 static int
752 event_handle_unmapnotify(void *data __attribute__ ((unused)),
753 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
755 client_t *c;
757 if((c = client_getbywin(ev->window)))
759 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
760 && XCB_EVENT_SENT(ev)
761 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
762 client_unmanage(c);
764 else
765 for(int i = 0; i < globalconf.embedded.len; i++)
766 if(globalconf.embedded.tab[i].win == ev->window)
768 xembed_window_array_take(&globalconf.embedded, i);
769 foreach(screen, globalconf.screens)
770 widget_invalidate_bytype(screen, widget_systray);
773 return 0;
776 /** The randr screen change notify event handler.
777 * \param data currently unused.
778 * \param connection The connection to the X server.
779 * \param ev The event.
781 static int
782 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
783 xcb_connection_t *connection __attribute__ ((unused)),
784 xcb_randr_screen_change_notify_event_t *ev)
786 if(!globalconf.have_randr)
787 return -1;
789 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
790 * (only the code relevant to RRScreenChangeNotify) as the latter
791 * doesn't provide this kind of function */
792 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
793 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
794 ev->mheight, ev->mwidth);
795 else
796 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
797 ev->mwidth, ev->mheight);
799 /* XRRUpdateConfiguration also executes the following instruction
800 * but it's not useful because SubpixelOrder is not used at all at
801 * the moment
803 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
806 awesome_restart();
808 return 0;
811 /** The client message event handler.
812 * \param data currently unused.
813 * \param connection The connection to the X server.
814 * \param ev The event.
816 static int
817 event_handle_clientmessage(void *data __attribute__ ((unused)),
818 xcb_connection_t *connection,
819 xcb_client_message_event_t *ev)
821 /* check for startup notification messages */
822 if(sn_xcb_display_process_event(globalconf.sndisplay, (xcb_generic_event_t *) ev))
823 return 0;
825 if(ev->type == WM_CHANGE_STATE)
827 client_t *c;
828 if((c = client_getbywin(ev->window))
829 && ev->format == 32
830 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
831 client_setminimized(c, true);
833 else if(ev->type == _XEMBED)
834 return xembed_process_client_message(ev);
835 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
836 return systray_process_client_message(ev);
837 return ewmh_process_client_message(ev);
840 /** The keymap change notify event handler.
841 * \param data Unused data.
842 * \param connection The connection to the X server.
843 * \param ev The event.
844 * \return Status code, 0 if everything's fine.
846 static int
847 event_handle_mappingnotify(void *data,
848 xcb_connection_t *connection,
849 xcb_mapping_notify_event_t *ev)
851 if(ev->request == XCB_MAPPING_MODIFIER
852 || ev->request == XCB_MAPPING_KEYBOARD)
854 /* Free and then allocate the key symbols */
855 xcb_key_symbols_free(globalconf.keysyms);
856 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
858 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
860 /* regrab everything */
861 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
863 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
864 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
865 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
866 window_grabkeys(s->root, &globalconf.keys);
869 foreach(_c, globalconf.clients)
871 client_t *c = *_c;
872 xcb_ungrab_key(connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
873 window_grabkeys(c->win, &c->keys);
877 return 0;
880 static int
881 event_handle_reparentnotify(void *data,
882 xcb_connection_t *connection,
883 xcb_reparent_notify_event_t *ev)
885 client_t *c;
887 if((c = client_getbywin(ev->window)))
888 client_unmanage(c);
890 return 0;
893 void a_xcb_set_event_handlers(void)
895 const xcb_query_extension_reply_t *randr_query;
897 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
898 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
899 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
900 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
901 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
902 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
903 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
904 xcb_event_set_focus_in_handler(&globalconf.evenths, event_handle_focusin, NULL);
905 xcb_event_set_focus_out_handler(&globalconf.evenths, event_handle_focusout, NULL);
906 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
907 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
908 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
909 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
910 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
911 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
912 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
913 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
914 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
916 /* check for randr extension */
917 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
918 if((globalconf.have_randr = randr_query->present))
919 xcb_event_set_handler(&globalconf.evenths,
920 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
921 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
922 NULL);
926 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80