ewmh: add support for _NET_DESKTOP_GEOMETRY
[awesome.git] / event.c
blobdc2a7b1be8cfc3f70a22927d84b8c0a2e5527f04
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 extern awesome_t globalconf;
46 /** Handle mouse button events.
47 * \param c The client on which the event happened or NULL.
48 * \param type Event type, press or release.
49 * \param button Button number.
50 * \param state Modkeys state.
51 * \param buttons Buttons array to check for.
53 static void
54 event_handle_mouse_button(client_t *c,
55 uint8_t type,
56 xcb_button_t button,
57 uint16_t state,
58 button_array_t *buttons)
60 for(int i = 0; i < buttons->len; i++)
61 if(button == buttons->tab[i]->button
62 && XUTIL_MASK_CLEAN(state) == buttons->tab[i]->mod)
63 switch(type)
65 case XCB_BUTTON_PRESS:
66 if(buttons->tab[i]->press != LUA_REFNIL)
68 if(c)
70 luaA_client_userdata_new(globalconf.L, c);
71 luaA_dofunction(globalconf.L, buttons->tab[i]->press, 1, 0);
73 else
74 luaA_dofunction(globalconf.L, buttons->tab[i]->press, 0, 0);
76 break;
77 case XCB_BUTTON_RELEASE:
78 if(buttons->tab[i]->release != LUA_REFNIL)
80 if(c)
82 luaA_client_userdata_new(globalconf.L, c);
83 luaA_dofunction(globalconf.L, buttons->tab[i]->release, 1, 0);
85 else
86 luaA_dofunction(globalconf.L, buttons->tab[i]->release, 0, 0);
88 break;
92 /** Handle an event with mouse grabber if needed
93 * \param x The x coordinate.
94 * \param y The y coordinate.
95 * \param mask The mask buttons.
97 static void
98 event_handle_mousegrabber(int x, int y, uint16_t mask)
100 if(globalconf.mousegrabber != LUA_REFNIL)
102 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
103 mousegrabber_handleevent(globalconf.L, x, y, mask);
104 if(lua_pcall(globalconf.L, 1, 1, 0))
106 warn("error running function: %s", lua_tostring(globalconf.L, -1));
107 luaA_mousegrabber_stop(globalconf.L);
109 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
110 luaA_mousegrabber_stop(globalconf.L);
111 lua_pop(globalconf.L, 1); /* pop returned value */
115 /** The button press event handler.
116 * \param data The type of mouse event.
117 * \param connection The connection to the X server.
118 * \param ev The event.
120 static int
121 event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev)
123 int screen;
124 const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection));
125 client_t *c;
126 widget_t *w;
127 wibox_t *wibox;
129 /* ev->state is
130 * button status (8 bits) + modifiers status (8 bits)
131 * we don't care for button status that we get, especially on release, so
132 * drop them */
133 ev->state &= 0x00ff;
135 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
137 if((wibox = wibox_getbywin(ev->event))
138 || (wibox = wibox_getbywin(ev->child)))
140 /* If the wibox is child, then x,y are
141 * relative to root window */
142 if(wibox->sw.window == ev->child)
144 ev->event_x -= wibox->sw.geometry.x;
145 ev->event_y -= wibox->sw.geometry.y;
148 /* check if we match a binding on the wibox */
149 button_array_t *b = &wibox->buttons;
151 for(int i = 0; i < b->len; i++)
152 if(ev->detail == b->tab[i]->button
153 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
154 switch(ev->response_type)
156 case XCB_BUTTON_PRESS:
157 if(b->tab[i]->press != LUA_REFNIL)
159 luaA_wibox_userdata_new(globalconf.L, wibox);
160 luaA_dofunction(globalconf.L, b->tab[i]->press, 1, 0);
162 break;
163 case XCB_BUTTON_RELEASE:
164 if(b->tab[i]->release != LUA_REFNIL)
166 luaA_wibox_userdata_new(globalconf.L, wibox);
167 luaA_dofunction(globalconf.L, b->tab[i]->release, 1, 0);
169 break;
172 /* then try to match a widget binding */
173 if((w = widget_getbycoords(wibox->position, &wibox->widgets,
174 wibox->sw.geometry.width,
175 wibox->sw.geometry.height,
176 &ev->event_x, &ev->event_y)))
178 b = &w->buttons;
180 for(int i = 0; i < b->len; i++)
181 if(ev->detail == b->tab[i]->button
182 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
183 switch(ev->response_type)
185 case XCB_BUTTON_PRESS:
186 if(b->tab[i]->press != LUA_REFNIL)
188 luaA_wibox_userdata_new(globalconf.L, wibox);
189 luaA_dofunction(globalconf.L, b->tab[i]->press, 1, 0);
191 break;
192 case XCB_BUTTON_RELEASE:
193 if(b->tab[i]->release != LUA_REFNIL)
195 luaA_wibox_userdata_new(globalconf.L, wibox);
196 luaA_dofunction(globalconf.L, b->tab[i]->release, 1, 0);
198 break;
201 /* return even if no widget match */
202 return 0;
205 if((c = client_getbywin(ev->event)))
207 event_handle_mouse_button(c, ev->response_type, ev->detail, ev->state, &c->buttons);
208 xcb_allow_events(globalconf.connection,
209 XCB_ALLOW_REPLAY_POINTER,
210 XCB_CURRENT_TIME);
212 else
213 for(screen = 0; screen < nb_screen; screen++)
214 if(xutil_screen_get(connection, screen)->root == ev->event)
216 event_handle_mouse_button(NULL, ev->response_type, ev->detail, ev->state, &globalconf.buttons);
217 return 0;
220 return 0;
223 static void
224 event_handle_configurerequest_configure_window(xcb_configure_request_event_t *ev)
226 uint16_t config_win_mask = 0;
227 uint32_t config_win_vals[7];
228 unsigned short i = 0;
230 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
232 config_win_mask |= XCB_CONFIG_WINDOW_X;
233 config_win_vals[i++] = ev->x;
235 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
237 config_win_mask |= XCB_CONFIG_WINDOW_Y;
238 config_win_vals[i++] = ev->y;
240 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
242 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
243 config_win_vals[i++] = ev->width;
245 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
247 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
248 config_win_vals[i++] = ev->height;
250 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
252 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
253 config_win_vals[i++] = ev->border_width;
255 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
257 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
258 config_win_vals[i++] = ev->sibling;
260 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
262 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
263 config_win_vals[i++] = ev->stack_mode;
266 xcb_configure_window(globalconf.connection, ev->window, config_win_mask, config_win_vals);
269 /** The configure event handler.
270 * \param data currently unused.
271 * \param connection The connection to the X server.
272 * \param ev The event.
274 static int
275 event_handle_configurerequest(void *data __attribute__ ((unused)),
276 xcb_connection_t *connection, xcb_configure_request_event_t *ev)
278 client_t *c;
279 area_t geometry;
281 if((c = client_getbywin(ev->window)))
283 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
285 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
286 geometry.x = ev->x;
287 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
288 geometry.y = ev->y;
289 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
290 geometry.width = ev->width;
291 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
292 geometry.height = ev->height;
294 if(c->isbanned)
296 /* We'll be sending protocol geometry, so don't readd borders and titlebar. */
297 /* We do have to ensure the windows don't end up in the visible screen. */
298 geometry.x = - (geometry.width + 2*c->border);
299 geometry.y = - (geometry.height + 2*c->border);
300 window_configure(c->win, geometry, c->border);
301 event_handle_configurerequest_configure_window(ev);
303 else
305 /** Configure request are sent with size relative to real (internal)
306 * window size, i.e. without titlebars and borders. */
307 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
309 if(client_resize(c, geometry, false))
311 /* All the wiboxes (may) need to be repositioned. */
312 if(client_hasstrut(c))
313 wibox_update_positions();
314 client_need_arrange(c);
316 else
318 /* Resize wasn't officially needed, but we don't want to break expectations. */
319 geometry = titlebar_geometry_remove(c->titlebar, c->border, c->geometry);
320 window_configure(c->win, geometry, c->border);
324 else
325 event_handle_configurerequest_configure_window(ev);
327 return 0;
330 /** The configure notify event handler.
331 * \param data currently unused.
332 * \param connection The connection to the X server.
333 * \param ev The event.
335 static int
336 event_handle_configurenotify(void *data __attribute__ ((unused)),
337 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
339 int screen_nbr;
340 const xcb_screen_t *screen;
342 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
343 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
344 && ev->window == screen->root
345 && (ev->width != screen->width_in_pixels
346 || ev->height != screen->height_in_pixels))
347 /* it's not that we panic, but restart */
348 awesome_restart();
350 return 0;
353 /** The destroy notify event handler.
354 * \param data currently unused.
355 * \param connection The connection to the X server.
356 * \param ev The event.
358 static int
359 event_handle_destroynotify(void *data __attribute__ ((unused)),
360 xcb_connection_t *connection __attribute__ ((unused)),
361 xcb_destroy_notify_event_t *ev)
363 client_t *c;
365 if((c = client_getbywin(ev->window)))
366 client_unmanage(c);
367 else
368 for(int i = 0; i < globalconf.embedded.len; i++)
369 if(globalconf.embedded.tab[i].win == ev->window)
371 xembed_window_array_take(&globalconf.embedded, i);
372 for(int j = 0; j < globalconf.nscreen; j++)
373 widget_invalidate_bytype(j, widget_systray);
376 return 0;
379 /** Handle a motion notify over widgets.
380 * \param object The object.
381 * \param mouse_over The pointer to the registered mouse over widget.
382 * \param widget The new widget the mouse is over.
384 static void
385 event_handle_widget_motionnotify(void *object,
386 widget_t **mouse_over,
387 widget_t *widget)
389 if(widget != *mouse_over)
391 if(*mouse_over)
393 if((*mouse_over)->mouse_leave != LUA_REFNIL)
395 /* call mouse leave function on old widget */
396 luaA_wibox_userdata_new(globalconf.L, object);
397 luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
400 if(widget)
402 /* call mouse enter function on new widget and register it */
403 *mouse_over = widget;
404 if(widget->mouse_enter != LUA_REFNIL)
406 luaA_wibox_userdata_new(globalconf.L, object);
407 luaA_dofunction(globalconf.L, widget->mouse_enter, 1, 0);
413 /** The motion notify event handler.
414 * \param data currently unused.
415 * \param connection The connection to the X server.
416 * \param ev The event.
418 static int
419 event_handle_motionnotify(void *data __attribute__ ((unused)),
420 xcb_connection_t *connection,
421 xcb_motion_notify_event_t *ev)
423 wibox_t *wibox = wibox_getbywin(ev->event);
424 widget_t *w;
426 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
428 if(wibox
429 && (w = widget_getbycoords(wibox->position, &wibox->widgets,
430 wibox->sw.geometry.width,
431 wibox->sw.geometry.height,
432 &ev->event_x, &ev->event_y)))
434 globalconf.pointer_x = ev->root_x;
435 globalconf.pointer_y = ev->root_y;
436 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
439 return 0;
442 /** The leave notify event handler.
443 * \param data currently unused.
444 * \param connection The connection to the X server.
445 * \param ev The event.
447 static int
448 event_handle_leavenotify(void *data __attribute__ ((unused)),
449 xcb_connection_t *connection,
450 xcb_leave_notify_event_t *ev)
452 wibox_t *wibox;
453 client_t *c = client_getbywin(ev->event);
455 if(c)
457 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
459 luaA_client_userdata_new(globalconf.L, c);
460 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
463 else if((wibox = wibox_getbywin(ev->event)))
465 if(wibox->mouse_over)
467 if(wibox->mouse_over->mouse_leave != LUA_REFNIL)
469 /* call mouse leave function on widget the mouse was over */
470 luaA_wibox_userdata_new(globalconf.L, wibox);
471 luaA_dofunction(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
473 wibox->mouse_over = NULL;
477 return 0;
480 /** The enter notify event handler.
481 * \param data currently unused.
482 * \param connection The connection to the X server.
483 * \param ev The event.
485 static int
486 event_handle_enternotify(void *data __attribute__ ((unused)),
487 xcb_connection_t *connection,
488 xcb_enter_notify_event_t *ev)
490 client_t *c;
491 xembed_window_t *emwin;
492 wibox_t *wibox;
493 widget_t *w;
495 if(ev->mode != XCB_NOTIFY_MODE_NORMAL
496 || (ev->root_x == globalconf.pointer_x
497 && ev->root_y == globalconf.pointer_y))
498 return 0;
501 if((wibox = wibox_getbywin(ev->event))
502 && (w = widget_getbycoords(wibox->position, &wibox->widgets,
503 wibox->sw.geometry.width,
504 wibox->sw.geometry.height,
505 &ev->event_x, &ev->event_y)))
507 globalconf.pointer_x = ev->root_x;
508 globalconf.pointer_y = ev->root_y;
509 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
511 else if((c = client_getbytitlebarwin(ev->event))
512 || (c = client_getbywin(ev->event)))
514 window_buttons_grab(c->win, ev->root, &c->buttons);
515 /* The idea behind saving pointer_x and pointer_y is Bob Marley powered.
516 * this will allow us top drop some EnterNotify events and thus not giving
517 * focus to windows appering under the cursor without a cursor move */
518 globalconf.pointer_x = ev->root_x;
519 globalconf.pointer_y = ev->root_y;
521 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
523 luaA_client_userdata_new(globalconf.L, c);
524 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
527 else if((emwin = xembed_getbywin(&globalconf.embedded, ev->event)))
528 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY,
529 xutil_screen_get(connection, emwin->phys_screen)->root,
530 XCB_BUTTON_MASK_ANY);
531 else if(ev->event == ev->root)
532 window_root_buttons_grab(ev->root);
534 return 0;
537 /** The expose event handler.
538 * \param data currently unused.
539 * \param connection The connection to the X server.
540 * \param ev The event.
542 static int
543 event_handle_expose(void *data __attribute__ ((unused)),
544 xcb_connection_t *connection __attribute__ ((unused)),
545 xcb_expose_event_t *ev)
547 wibox_t *wibox = wibox_getbywin(ev->window);
549 if(wibox)
550 simplewindow_refresh_pixmap_partial(&wibox->sw,
551 ev->x, ev->y,
552 ev->width, ev->height);
554 return 0;
557 /** The key press event handler.
558 * \param data currently unused.
559 * \param connection The connection to the X server.
560 * \param ev The event.
562 static int
563 event_handle_key(void *data __attribute__ ((unused)),
564 xcb_connection_t *connection __attribute__ ((unused)),
565 xcb_key_press_event_t *ev)
567 client_t *c;
569 if(globalconf.keygrabber != LUA_REFNIL)
571 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
572 if(keygrabber_handlekpress(globalconf.L, ev))
574 if(lua_pcall(globalconf.L, 3, 1, 0))
576 warn("error running function: %s", lua_tostring(globalconf.L, -1));
577 luaA_keygrabber_stop(globalconf.L);
579 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
580 luaA_keygrabber_stop(globalconf.L);
582 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
584 else if((c = client_getbywin(ev->event)))
586 keyb_t *k = key_find(&c->keys, ev);
588 if(k)
589 switch(ev->response_type)
591 case XCB_KEY_PRESS:
592 if(k->press != LUA_REFNIL)
594 luaA_client_userdata_new(globalconf.L, c);
595 luaA_dofunction(globalconf.L, k->press, 1, 0);
597 break;
598 case XCB_KEY_RELEASE:
599 if(k->release != LUA_REFNIL)
601 luaA_client_userdata_new(globalconf.L, c);
602 luaA_dofunction(globalconf.L, k->release, 1, 0);
604 break;
607 else
609 keyb_t *k = key_find(&globalconf.keys, ev);
611 if(k)
612 switch(ev->response_type)
614 case XCB_KEY_PRESS:
615 if(k->press != LUA_REFNIL)
616 luaA_dofunction(globalconf.L, k->press, 0, 0);
617 break;
618 case XCB_KEY_RELEASE:
619 if(k->release != LUA_REFNIL)
620 luaA_dofunction(globalconf.L, k->release, 0, 0);
621 break;
625 return 0;
628 /** The map request event handler.
629 * \param data currently unused.
630 * \param connection The connection to the X server.
631 * \param ev The event.
633 static int
634 event_handle_maprequest(void *data __attribute__ ((unused)),
635 xcb_connection_t *connection, xcb_map_request_event_t *ev)
637 int phys_screen, ret = 0;
638 client_t *c;
639 xcb_get_window_attributes_cookie_t wa_c;
640 xcb_get_window_attributes_reply_t *wa_r;
641 xcb_get_geometry_cookie_t geom_c;
642 xcb_get_geometry_reply_t *geom_r;
644 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
646 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
647 return -1;
649 if(wa_r->override_redirect)
650 goto bailout;
652 if(xembed_getbywin(&globalconf.embedded, ev->window))
654 xcb_map_window(connection, ev->window);
655 xembed_window_activate(connection, ev->window);
657 else if((c = client_getbywin(ev->window)))
659 /* Check that it may be visible, but not asked to be hidden */
660 if(client_maybevisible(c, c->screen) && !c->ishidden)
662 client_setminimized(c, false);
663 /* it will be raised, so just update ourself */
664 client_raise(c);
667 else
669 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
671 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
673 ret = -1;
674 goto bailout;
677 phys_screen = xutil_root2screen(connection, geom_r->root);
679 client_manage(ev->window, geom_r, phys_screen, false);
681 p_delete(&geom_r);
684 bailout:
685 p_delete(&wa_r);
686 return ret;
689 /** The unmap notify event handler.
690 * \param data currently unused.
691 * \param connection The connection to the X server.
692 * \param ev The event.
694 static int
695 event_handle_unmapnotify(void *data __attribute__ ((unused)),
696 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
698 client_t *c;
700 if((c = client_getbywin(ev->window)))
702 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
703 && XCB_EVENT_SENT(ev)
704 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
705 client_unmanage(c);
707 else
708 for(int i = 0; i < globalconf.embedded.len; i++)
709 if(globalconf.embedded.tab[i].win == ev->window)
711 xembed_window_array_take(&globalconf.embedded, i);
712 for(int j = 0; j < globalconf.nscreen; j++)
713 widget_invalidate_bytype(j, widget_systray);
716 return 0;
719 /** The randr screen change notify event handler.
720 * \param data currently unused.
721 * \param connection The connection to the X server.
722 * \param ev The event.
724 static int
725 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
726 xcb_connection_t *connection __attribute__ ((unused)),
727 xcb_randr_screen_change_notify_event_t *ev)
729 if(!globalconf.have_randr)
730 return -1;
732 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
733 * (only the code relevant to RRScreenChangeNotify) as the latter
734 * doesn't provide this kind of function */
735 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
736 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
737 ev->mheight, ev->mwidth);
738 else
739 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
740 ev->mwidth, ev->mheight);
742 /* XRRUpdateConfiguration also executes the following instruction
743 * but it's not useful because SubpixelOrder is not used at all at
744 * the moment
746 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
749 awesome_restart();
751 return 0;
754 /** The client message event handler.
755 * \param data currently unused.
756 * \param connection The connection to the X server.
757 * \param ev The event.
759 static int
760 event_handle_clientmessage(void *data __attribute__ ((unused)),
761 xcb_connection_t *connection,
762 xcb_client_message_event_t *ev)
764 client_t *c;
766 if(ev->type == WM_CHANGE_STATE)
768 if((c = client_getbywin(ev->window))
769 && ev->format == 32
770 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
771 client_setminimized(c, true);
773 else if(ev->type == _XEMBED)
774 return xembed_process_client_message(ev);
775 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
776 return systray_process_client_message(ev);
777 return ewmh_process_client_message(ev);
780 /** The keymap change notify event handler.
781 * \param data Unused data.
782 * \param connection The connection to the X server.
783 * \param ev The event.
784 * \return Status code, 0 if everything's fine.
786 static int
787 event_handle_mappingnotify(void *data,
788 xcb_connection_t *connection,
789 xcb_mapping_notify_event_t *ev)
791 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
793 if(ev->request == XCB_MAPPING_MODIFIER
794 || ev->request == XCB_MAPPING_KEYBOARD)
796 /* Send the request to get the NumLock, ShiftLock and CapsLock masks */
797 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
799 /* Free and then allocate the key symbols */
800 xcb_key_symbols_free(globalconf.keysyms);
801 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
803 /* Process the reply of previously sent mapping request */
804 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
805 globalconf.keysyms, &globalconf.numlockmask,
806 &globalconf.shiftlockmask, &globalconf.capslockmask);
808 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
810 /* regrab everything */
811 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
813 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
814 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look weird */
815 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
816 window_grabkeys(s->root, &globalconf.keys);
819 for(client_t *c = globalconf.clients; c; c = c->next)
821 xcb_ungrab_key(connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
822 window_grabkeys(c->win, &c->keys);
826 return 0;
829 static int
830 event_handle_reparentnotify(void *data,
831 xcb_connection_t *connection,
832 xcb_reparent_notify_event_t *ev)
834 client_t *c;
836 if((c = client_getbywin(ev->window)))
837 client_unmanage(c);
839 return 0;
842 void a_xcb_set_event_handlers(void)
844 const xcb_query_extension_reply_t *randr_query;
846 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
847 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
848 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
849 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
850 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
851 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
852 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
853 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
854 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
855 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
856 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
857 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
858 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
859 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
860 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
861 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
863 /* check for randr extension */
864 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
865 if((globalconf.have_randr = randr_query->present))
866 xcb_event_set_handler(&globalconf.evenths,
867 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
868 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
869 NULL);
873 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80