awful.widget.taglist: remove needless taglist_squares conditions
[awesome.git] / event.c
blob98d4274efdbee82937213803197dd7c7e2f18edb
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"
44 /** Handle mouse button events.
45 * \param c The client on which the event happened or NULL.
46 * \param type Event type, press or release.
47 * \param button Button number.
48 * \param state Modkeys state.
49 * \param buttons Buttons array to check for.
51 static void
52 event_handle_mouse_button(client_t *c,
53 uint8_t type,
54 xcb_button_t button,
55 uint16_t state,
56 button_array_t *buttons)
58 for(int i = 0; i < buttons->len; i++)
59 if(button == buttons->tab[i]->button
60 && XUTIL_MASK_CLEAN(state) == buttons->tab[i]->mod)
61 switch(type)
63 case XCB_BUTTON_PRESS:
64 if(buttons->tab[i]->press != LUA_REFNIL)
66 if(c)
68 client_push(globalconf.L, c);
69 luaA_dofunction(globalconf.L, buttons->tab[i]->press, 1, 0);
71 else
72 luaA_dofunction(globalconf.L, buttons->tab[i]->press, 0, 0);
74 break;
75 case XCB_BUTTON_RELEASE:
76 if(buttons->tab[i]->release != LUA_REFNIL)
78 if(c)
80 client_push(globalconf.L, c);
81 luaA_dofunction(globalconf.L, buttons->tab[i]->release, 1, 0);
83 else
84 luaA_dofunction(globalconf.L, buttons->tab[i]->release, 0, 0);
86 break;
90 /** Handle an event with mouse grabber if needed
91 * \param x The x coordinate.
92 * \param y The y coordinate.
93 * \param mask The mask buttons.
95 static void
96 event_handle_mousegrabber(int x, int y, uint16_t mask)
98 if(globalconf.mousegrabber != LUA_REFNIL)
100 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
101 mousegrabber_handleevent(globalconf.L, x, y, mask);
102 if(lua_pcall(globalconf.L, 1, 1, 0))
104 warn("error running function: %s", lua_tostring(globalconf.L, -1));
105 luaA_mousegrabber_stop(globalconf.L);
107 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
108 luaA_mousegrabber_stop(globalconf.L);
109 lua_pop(globalconf.L, 1); /* pop returned value */
113 /** The button press event handler.
114 * \param data The type of mouse event.
115 * \param connection The connection to the X server.
116 * \param ev The event.
118 static int
119 event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev)
121 int screen;
122 const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection));
123 client_t *c;
124 wibox_t *wibox;
126 /* ev->state is
127 * button status (8 bits) + modifiers status (8 bits)
128 * we don't care for button status that we get, especially on release, so
129 * drop them */
130 ev->state &= 0x00ff;
132 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
134 if((wibox = wibox_getbywin(ev->event))
135 || (wibox = wibox_getbywin(ev->child)))
137 /* If the wibox is child, then x,y are
138 * relative to root window */
139 if(wibox->sw.window == ev->child)
141 ev->event_x -= wibox->sw.geometry.x;
142 ev->event_y -= wibox->sw.geometry.y;
145 /* check if we match a binding on the wibox */
146 button_array_t *b = &wibox->buttons;
148 for(int i = 0; i < b->len; i++)
149 if(ev->detail == b->tab[i]->button
150 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
151 switch(ev->response_type)
153 case XCB_BUTTON_PRESS:
154 if(b->tab[i]->press != LUA_REFNIL)
156 wibox_push(globalconf.L, wibox);
157 luaA_dofunction(globalconf.L, b->tab[i]->press, 1, 0);
159 break;
160 case XCB_BUTTON_RELEASE:
161 if(b->tab[i]->release != LUA_REFNIL)
163 wibox_push(globalconf.L, wibox);
164 luaA_dofunction(globalconf.L, b->tab[i]->release, 1, 0);
166 break;
169 /* then try to match a widget binding */
170 widget_t *w = widget_getbycoords(wibox->position, &wibox->widgets,
171 wibox->sw.geometry.width,
172 wibox->sw.geometry.height,
173 &ev->event_x, &ev->event_y);
174 if(w)
176 b = &w->buttons;
178 for(int i = 0; i < b->len; i++)
179 if(ev->detail == b->tab[i]->button
180 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
181 switch(ev->response_type)
183 case XCB_BUTTON_PRESS:
184 if(b->tab[i]->press != LUA_REFNIL)
186 wibox_push(globalconf.L, wibox);
187 luaA_dofunction(globalconf.L, b->tab[i]->press, 1, 0);
189 break;
190 case XCB_BUTTON_RELEASE:
191 if(b->tab[i]->release != LUA_REFNIL)
193 wibox_push(globalconf.L, wibox);
194 luaA_dofunction(globalconf.L, b->tab[i]->release, 1, 0);
196 break;
199 /* return even if no widget match */
200 return 0;
202 else if((c = client_getbywin(ev->event)))
204 event_handle_mouse_button(c, ev->response_type, ev->detail, ev->state, &c->buttons);
205 xcb_allow_events(globalconf.connection,
206 XCB_ALLOW_REPLAY_POINTER,
207 XCB_CURRENT_TIME);
209 else if(ev->child == XCB_NONE)
211 for(screen = 0; screen < nb_screen; screen++)
212 if(xutil_screen_get(connection, screen)->root == ev->event)
214 event_handle_mouse_button(NULL, ev->response_type, ev->detail, ev->state, &globalconf.buttons);
215 return 0;
219 return 0;
222 static void
223 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
225 uint16_t config_win_mask = 0;
226 uint32_t config_win_vals[7];
227 unsigned short i = 0;
229 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
231 config_win_mask |= XCB_CONFIG_WINDOW_X;
232 config_win_vals[i++] = ev->x;
234 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
236 config_win_mask |= XCB_CONFIG_WINDOW_Y;
237 config_win_vals[i++] = ev->y;
239 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
241 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
242 config_win_vals[i++] = ev->width;
244 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
246 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
247 config_win_vals[i++] = ev->height;
249 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
251 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
252 config_win_vals[i++] = ev->border_width;
254 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
256 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
257 config_win_vals[i++] = ev->sibling;
259 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
261 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
262 config_win_vals[i++] = ev->stack_mode;
265 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
268 /** The configure event handler.
269 * \param data currently unused.
270 * \param connection The connection to the X server.
271 * \param ev The event.
273 static int
274 event_handle_configurerequest(void *data __attribute__ ((unused)),
275 xcb_connection_t *connection, xcb_configure_request_event_t *ev)
277 client_t *c;
278 area_t geometry;
280 if((c = client_getbywin(ev->window)))
282 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
284 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
285 geometry.x = ev->x;
286 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
287 geometry.y = ev->y;
288 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
289 geometry.width = ev->width;
290 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
291 geometry.height = ev->height;
293 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
294 client_setborder(c, ev->border_width);
296 /* Clients are not allowed to directly mess with stacking parameters. */
297 ev->value_mask &= ~(XCB_CONFIG_WINDOW_SIBLING |
298 XCB_CONFIG_WINDOW_STACK_MODE);
300 if(c->isbanned)
302 /* We'll be sending protocol geometry, so don't readd borders and titlebar. */
303 /* We do have to ensure the windows don't end up in the visible screen. */
304 ev->x = geometry.x = - (geometry.width + 2*c->border);
305 ev->y = geometry.y = - (geometry.height + 2*c->border);
306 window_configure(c->win, geometry, c->border);
307 event_handle_configurerequest_configure_window(ev);
309 else
311 /** Configure request are sent with size relative to real (internal)
312 * window size, i.e. without titlebars and borders. */
313 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
315 if(client_resize(c, geometry, false))
317 /* All the wiboxes (may) need to be repositioned. */
318 if(client_hasstrut(c))
319 wibox_update_positions();
320 client_need_arrange(c);
322 else
324 /* Resize wasn't officially needed, but we don't want to break expectations. */
325 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
326 window_configure(c->win, geometry, c->border);
330 else
331 event_handle_configurerequest_configure_window(ev);
333 return 0;
336 /** The configure notify event handler.
337 * \param data currently unused.
338 * \param connection The connection to the X server.
339 * \param ev The event.
341 static int
342 event_handle_configurenotify(void *data __attribute__ ((unused)),
343 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
345 int screen_nbr;
346 const xcb_screen_t *screen;
348 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
349 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
350 && 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();
356 return 0;
359 /** The destroy notify event handler.
360 * \param data currently unused.
361 * \param connection The connection to the X server.
362 * \param ev The event.
364 static int
365 event_handle_destroynotify(void *data __attribute__ ((unused)),
366 xcb_connection_t *connection __attribute__ ((unused)),
367 xcb_destroy_notify_event_t *ev)
369 client_t *c;
371 if((c = client_getbywin(ev->window)))
372 client_unmanage(c);
373 else
374 for(int i = 0; i < globalconf.embedded.len; i++)
375 if(globalconf.embedded.tab[i].win == ev->window)
377 xembed_window_array_take(&globalconf.embedded, i);
378 for(int j = 0; j < globalconf.nscreen; j++)
379 widget_invalidate_bytype(j, widget_systray);
382 return 0;
385 /** Handle a motion notify over widgets.
386 * \param object The object.
387 * \param mouse_over The pointer to the registered mouse over widget.
388 * \param widget The new widget the mouse is over.
390 static void
391 event_handle_widget_motionnotify(void *object,
392 widget_t **mouse_over,
393 widget_t *widget)
395 if(widget != *mouse_over)
397 if(*mouse_over)
399 if((*mouse_over)->mouse_leave != LUA_REFNIL)
401 /* call mouse leave function on old widget */
402 widget_push(globalconf.L, *mouse_over);
403 luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
406 if(widget)
408 /* call mouse enter function on new widget and register it */
409 *mouse_over = widget;
410 if(widget->mouse_enter != LUA_REFNIL)
412 widget_push(globalconf.L, widget);
413 luaA_dofunction(globalconf.L, widget->mouse_enter, 1, 0);
419 /** The motion notify event handler.
420 * \param data currently unused.
421 * \param connection The connection to the X server.
422 * \param ev The event.
424 static int
425 event_handle_motionnotify(void *data __attribute__ ((unused)),
426 xcb_connection_t *connection,
427 xcb_motion_notify_event_t *ev)
429 wibox_t *wibox;
431 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
433 if((wibox = wibox_getbywin(ev->event)))
435 widget_t *w = widget_getbycoords(wibox->position, &wibox->widgets,
436 wibox->sw.geometry.width,
437 wibox->sw.geometry.height,
438 &ev->event_x, &ev->event_y);
439 if(w)
440 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
443 return 0;
446 /** The leave notify event handler.
447 * \param data currently unused.
448 * \param connection The connection to the X server.
449 * \param ev The event.
451 static int
452 event_handle_leavenotify(void *data __attribute__ ((unused)),
453 xcb_connection_t *connection,
454 xcb_leave_notify_event_t *ev)
456 wibox_t *wibox;
457 client_t *c;
459 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
460 return 0;
462 if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event)))
463 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
465 client_push(globalconf.L, c);
466 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
469 if((wibox = wibox_getbywin(ev->event)))
471 if(wibox->mouse_over)
473 if(wibox->mouse_over->mouse_leave != LUA_REFNIL)
475 /* call mouse leave function on widget the mouse was over */
476 wibox_push(globalconf.L, wibox);
477 luaA_dofunction(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
479 wibox->mouse_over = NULL;
482 if(wibox->mouse_leave != LUA_REFNIL)
483 luaA_dofunction(globalconf.L, wibox->mouse_leave, 0, 0);
486 return 0;
489 /** The enter notify event handler.
490 * \param data currently unused.
491 * \param connection The connection to the X server.
492 * \param ev The event.
494 static int
495 event_handle_enternotify(void *data __attribute__ ((unused)),
496 xcb_connection_t *connection,
497 xcb_enter_notify_event_t *ev)
499 client_t *c;
500 wibox_t *wibox;
502 if(ev->mode != XCB_NOTIFY_MODE_NORMAL)
503 return 0;
505 if((wibox = wibox_getbywin(ev->event)))
507 widget_t *w = widget_getbycoords(wibox->position, &wibox->widgets,
508 wibox->sw.geometry.width,
509 wibox->sw.geometry.height,
510 &ev->event_x, &ev->event_y);
511 if(w)
512 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
514 if(wibox->mouse_enter != LUA_REFNIL)
515 luaA_dofunction(globalconf.L, wibox->mouse_enter, 0, 0);
518 if((c = client_getbytitlebarwin(ev->event))
519 || (c = client_getbywin(ev->event)))
520 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
522 client_push(globalconf.L, c);
523 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
526 return 0;
529 /** The focus in event handler.
530 * \param data currently unused.
531 * \param connection The connection to the X server.
532 * \param ev The event.
534 static int
535 event_handle_focusin(void *data __attribute__ ((unused)),
536 xcb_connection_t *connection,
537 xcb_focus_in_event_t *ev)
539 client_t *c;
541 /* Events that we are interested in: */
542 switch(ev->detail)
544 /* These are events that jump between windows of a toplevel client.
546 * NotifyVirtual event is handled in case where NotifyAncestor or
547 * NotifyInferior event is not generated on window that is managed by
548 * awesome ( client_* returns NULL )
550 * Can someone explain exactly why they are needed ?
552 case XCB_NOTIFY_DETAIL_VIRTUAL:
553 case XCB_NOTIFY_DETAIL_ANCESTOR:
554 case XCB_NOTIFY_DETAIL_INFERIOR:
556 /* These are events that jump between clients.
557 * Virtual events ensure we always get an event on our top-level window.
559 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
560 case XCB_NOTIFY_DETAIL_NONLINEAR:
561 if((c = client_getbytitlebarwin(ev->event))
562 || (c = client_getbywin(ev->event)))
563 client_focus_update(c);
564 /* all other events are ignored */
565 default:
566 return 0;
570 /** The focus out event handler.
571 * \param data currently unused.
572 * \param connection The connection to the X server.
573 * \param ev The event.
575 static int
576 event_handle_focusout(void *data __attribute__ ((unused)),
577 xcb_connection_t *connection,
578 xcb_focus_out_event_t *ev)
580 client_t *c;
582 /* Events that we are interested in: */
583 switch(ev->detail)
585 /* These are events that jump between clients.
586 * Virtual events ensure we always get an event on our top-level window.
588 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
589 case XCB_NOTIFY_DETAIL_NONLINEAR:
590 if((c = client_getbytitlebarwin(ev->event))
591 || (c = client_getbywin(ev->event)))
592 client_unfocus_update(c);
593 /* all other events are ignored */
594 default:
595 return 0;
599 /** The expose event handler.
600 * \param data currently unused.
601 * \param connection The connection to the X server.
602 * \param ev The event.
604 static int
605 event_handle_expose(void *data __attribute__ ((unused)),
606 xcb_connection_t *connection __attribute__ ((unused)),
607 xcb_expose_event_t *ev)
609 wibox_t *wibox;
611 if((wibox = wibox_getbywin(ev->window)))
612 simplewindow_refresh_pixmap_partial(&wibox->sw,
613 ev->x, ev->y,
614 ev->width, ev->height);
616 return 0;
619 /** The key press event handler.
620 * \param data currently unused.
621 * \param connection The connection to the X server.
622 * \param ev The event.
624 static int
625 event_handle_key(void *data __attribute__ ((unused)),
626 xcb_connection_t *connection __attribute__ ((unused)),
627 xcb_key_press_event_t *ev)
629 client_t *c;
631 if(globalconf.keygrabber != LUA_REFNIL)
633 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
634 if(keygrabber_handlekpress(globalconf.L, ev))
636 if(lua_pcall(globalconf.L, 3, 1, 0))
638 warn("error running function: %s", lua_tostring(globalconf.L, -1));
639 luaA_keygrabber_stop(globalconf.L);
641 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
642 luaA_keygrabber_stop(globalconf.L);
644 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
646 else if((c = client_getbywin(ev->event)))
648 keyb_t *k = key_find(&c->keys, ev);
650 if(k)
651 switch(ev->response_type)
653 case XCB_KEY_PRESS:
654 if(k->press != LUA_REFNIL)
656 client_push(globalconf.L, c);
657 luaA_dofunction(globalconf.L, k->press, 1, 0);
659 break;
660 case XCB_KEY_RELEASE:
661 if(k->release != LUA_REFNIL)
663 client_push(globalconf.L, c);
664 luaA_dofunction(globalconf.L, k->release, 1, 0);
666 break;
669 else
671 keyb_t *k = key_find(&globalconf.keys, ev);
673 if(k)
674 switch(ev->response_type)
676 case XCB_KEY_PRESS:
677 if(k->press != LUA_REFNIL)
678 luaA_dofunction(globalconf.L, k->press, 0, 0);
679 break;
680 case XCB_KEY_RELEASE:
681 if(k->release != LUA_REFNIL)
682 luaA_dofunction(globalconf.L, k->release, 0, 0);
683 break;
687 return 0;
690 /** The map request event handler.
691 * \param data currently unused.
692 * \param connection The connection to the X server.
693 * \param ev The event.
695 static int
696 event_handle_maprequest(void *data __attribute__ ((unused)),
697 xcb_connection_t *connection, xcb_map_request_event_t *ev)
699 int phys_screen, ret = 0;
700 client_t *c;
701 xcb_get_window_attributes_cookie_t wa_c;
702 xcb_get_window_attributes_reply_t *wa_r;
703 xcb_get_geometry_cookie_t geom_c;
704 xcb_get_geometry_reply_t *geom_r;
706 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
708 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
709 return -1;
711 if(wa_r->override_redirect)
712 goto bailout;
714 if(xembed_getbywin(&globalconf.embedded, ev->window))
716 xcb_map_window(connection, ev->window);
717 xembed_window_activate(connection, ev->window);
719 else if((c = client_getbywin(ev->window)))
721 /* Check that it may be visible, but not asked to be hidden */
722 if(client_maybevisible(c, c->screen) && !c->ishidden)
724 client_setminimized(c, false);
725 /* it will be raised, so just update ourself */
726 client_raise(c);
729 else
731 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
733 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
735 ret = -1;
736 goto bailout;
739 phys_screen = xutil_root2screen(connection, geom_r->root);
741 client_manage(ev->window, geom_r, phys_screen, false);
743 p_delete(&geom_r);
746 bailout:
747 p_delete(&wa_r);
748 return ret;
751 /** The unmap notify event handler.
752 * \param data currently unused.
753 * \param connection The connection to the X server.
754 * \param ev The event.
756 static int
757 event_handle_unmapnotify(void *data __attribute__ ((unused)),
758 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
760 client_t *c;
762 if((c = client_getbywin(ev->window)))
764 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
765 && XCB_EVENT_SENT(ev)
766 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
767 client_unmanage(c);
769 else
770 for(int i = 0; i < globalconf.embedded.len; i++)
771 if(globalconf.embedded.tab[i].win == ev->window)
773 xembed_window_array_take(&globalconf.embedded, i);
774 for(int j = 0; j < globalconf.nscreen; j++)
775 widget_invalidate_bytype(j, widget_systray);
778 return 0;
781 /** The randr screen change notify event handler.
782 * \param data currently unused.
783 * \param connection The connection to the X server.
784 * \param ev The event.
786 static int
787 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
788 xcb_connection_t *connection __attribute__ ((unused)),
789 xcb_randr_screen_change_notify_event_t *ev)
791 if(!globalconf.have_randr)
792 return -1;
794 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
795 * (only the code relevant to RRScreenChangeNotify) as the latter
796 * doesn't provide this kind of function */
797 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
798 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
799 ev->mheight, ev->mwidth);
800 else
801 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
802 ev->mwidth, ev->mheight);
804 /* XRRUpdateConfiguration also executes the following instruction
805 * but it's not useful because SubpixelOrder is not used at all at
806 * the moment
808 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
811 awesome_restart();
813 return 0;
816 /** The client message event handler.
817 * \param data currently unused.
818 * \param connection The connection to the X server.
819 * \param ev The event.
821 static int
822 event_handle_clientmessage(void *data __attribute__ ((unused)),
823 xcb_connection_t *connection,
824 xcb_client_message_event_t *ev)
826 client_t *c;
828 if(ev->type == WM_CHANGE_STATE)
830 if((c = client_getbywin(ev->window))
831 && ev->format == 32
832 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
833 client_setminimized(c, true);
835 else if(ev->type == _XEMBED)
836 return xembed_process_client_message(ev);
837 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
838 return systray_process_client_message(ev);
839 return ewmh_process_client_message(ev);
842 /** The keymap change notify event handler.
843 * \param data Unused data.
844 * \param connection The connection to the X server.
845 * \param ev The event.
846 * \return Status code, 0 if everything's fine.
848 static int
849 event_handle_mappingnotify(void *data,
850 xcb_connection_t *connection,
851 xcb_mapping_notify_event_t *ev)
853 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
855 if(ev->request == XCB_MAPPING_MODIFIER
856 || ev->request == XCB_MAPPING_KEYBOARD)
858 /* Send the request to get the NumLock, ShiftLock and CapsLock masks */
859 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
861 /* Free and then allocate the key symbols */
862 xcb_key_symbols_free(globalconf.keysyms);
863 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
865 /* Process the reply of previously sent mapping request */
866 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
867 globalconf.keysyms, &globalconf.numlockmask,
868 &globalconf.shiftlockmask, &globalconf.capslockmask);
870 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
872 /* regrab everything */
873 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
875 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
876 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
877 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
878 window_grabkeys(s->root, &globalconf.keys);
881 foreach(_c, globalconf.clients)
883 client_t *c = *_c;
884 xcb_ungrab_key(connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
885 window_grabkeys(c->win, &c->keys);
889 return 0;
892 static int
893 event_handle_reparentnotify(void *data,
894 xcb_connection_t *connection,
895 xcb_reparent_notify_event_t *ev)
897 client_t *c;
899 if((c = client_getbywin(ev->window)))
900 client_unmanage(c);
902 return 0;
905 void a_xcb_set_event_handlers(void)
907 const xcb_query_extension_reply_t *randr_query;
909 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
910 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
911 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
912 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
913 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
914 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
915 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
916 xcb_event_set_focus_in_handler(&globalconf.evenths, event_handle_focusin, NULL);
917 xcb_event_set_focus_out_handler(&globalconf.evenths, event_handle_focusout, NULL);
918 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
919 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
920 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
921 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
922 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
923 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
924 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
925 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
926 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
928 /* check for randr extension */
929 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
930 if((globalconf.have_randr = randr_query->present))
931 xcb_event_set_handler(&globalconf.evenths,
932 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
933 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
934 NULL);
938 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80