image: set alpha when creating ARGB32 image
[awesome.git] / event.c
blobd38a92e6e07393d91c04fa4381cdba895d8fd0a0
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 /** The configure event handler.
224 * \param data currently unused.
225 * \param connection The connection to the X server.
226 * \param ev The event.
228 static int
229 event_handle_configurerequest(void *data __attribute__ ((unused)),
230 xcb_connection_t *connection, xcb_configure_request_event_t *ev)
232 client_t *c;
233 area_t geometry;
235 if((c = client_getbywin(ev->window)))
237 geometry = c->geometry;
239 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
240 geometry.x = ev->x;
241 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
242 geometry.y = ev->y;
243 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
244 geometry.width = ev->width;
245 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
246 geometry.height = ev->height;
248 if(geometry.x != c->geometry.x || geometry.y != c->geometry.y
249 || geometry.width != c->geometry.width || geometry.height != c->geometry.height)
251 client_resize(c, geometry, false);
252 /* All the wiboxes (may) need to be repositioned. */
253 if(client_hasstrut(c))
254 wibox_update_positions();
255 client_need_arrange(c);
257 else
258 window_configure(c->win, geometry, c->border);
260 else
262 uint16_t config_win_mask = 0;
263 uint32_t config_win_vals[7];
264 unsigned short i = 0;
266 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
268 config_win_mask |= XCB_CONFIG_WINDOW_X;
269 config_win_vals[i++] = ev->x;
271 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
273 config_win_mask |= XCB_CONFIG_WINDOW_Y;
274 config_win_vals[i++] = ev->y;
276 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
278 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
279 config_win_vals[i++] = ev->width;
281 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
283 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
284 config_win_vals[i++] = ev->height;
286 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
288 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
289 config_win_vals[i++] = ev->border_width;
291 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
293 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
294 config_win_vals[i++] = ev->sibling;
296 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
298 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
299 config_win_vals[i++] = ev->stack_mode;
302 xcb_configure_window(connection, ev->window, config_win_mask, config_win_vals);
305 return 0;
308 /** The configure notify event handler.
309 * \param data currently unused.
310 * \param connection The connection to the X server.
311 * \param ev The event.
313 static int
314 event_handle_configurenotify(void *data __attribute__ ((unused)),
315 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
317 int screen_nbr;
318 const xcb_screen_t *screen;
320 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
321 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
322 && ev->window == screen->root
323 && (ev->width != screen->width_in_pixels
324 || ev->height != screen->height_in_pixels))
325 /* it's not that we panic, but restart */
326 awesome_restart();
328 return 0;
331 /** The destroy 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_destroynotify(void *data __attribute__ ((unused)),
338 xcb_connection_t *connection __attribute__ ((unused)),
339 xcb_destroy_notify_event_t *ev)
341 client_t *c;
343 if((c = client_getbywin(ev->window)))
344 client_unmanage(c);
345 else
346 for(int i = 0; i < globalconf.embedded.len; i++)
347 if(globalconf.embedded.tab[i].win == ev->window)
349 xembed_window_array_take(&globalconf.embedded, i);
350 for(int j = 0; j < globalconf.nscreen; j++)
351 widget_invalidate_bytype(j, widget_systray);
354 return 0;
357 /** Handle a motion notify over widgets.
358 * \param object The object.
359 * \param mouse_over The pointer to the registered mouse over widget.
360 * \param widget The new widget the mouse is over.
362 static void
363 event_handle_widget_motionnotify(void *object,
364 widget_t **mouse_over,
365 widget_t *widget)
367 if(widget != *mouse_over)
369 if(*mouse_over)
371 if((*mouse_over)->mouse_leave != LUA_REFNIL)
373 /* call mouse leave function on old widget */
374 luaA_wibox_userdata_new(globalconf.L, object);
375 luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
378 if(widget)
380 /* call mouse enter function on new widget and register it */
381 *mouse_over = widget;
382 if(widget->mouse_enter != LUA_REFNIL)
384 luaA_wibox_userdata_new(globalconf.L, object);
385 luaA_dofunction(globalconf.L, widget->mouse_enter, 1, 0);
391 /** The motion notify event handler.
392 * \param data currently unused.
393 * \param connection The connection to the X server.
394 * \param ev The event.
396 static int
397 event_handle_motionnotify(void *data __attribute__ ((unused)),
398 xcb_connection_t *connection,
399 xcb_motion_notify_event_t *ev)
401 wibox_t *wibox = wibox_getbywin(ev->event);
402 widget_t *w;
404 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
406 if(wibox
407 && (w = widget_getbycoords(wibox->position, &wibox->widgets,
408 wibox->sw.geometry.width,
409 wibox->sw.geometry.height,
410 &ev->event_x, &ev->event_y)))
412 globalconf.pointer_x = ev->root_x;
413 globalconf.pointer_y = ev->root_y;
414 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
417 return 0;
420 /** The leave notify event handler.
421 * \param data currently unused.
422 * \param connection The connection to the X server.
423 * \param ev The event.
425 static int
426 event_handle_leavenotify(void *data __attribute__ ((unused)),
427 xcb_connection_t *connection,
428 xcb_leave_notify_event_t *ev)
430 wibox_t *wibox;
431 client_t *c = client_getbywin(ev->event);
433 if(c)
435 if(globalconf.hooks.mouse_leave != LUA_REFNIL)
437 luaA_client_userdata_new(globalconf.L, c);
438 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_leave, 1, 0);
441 else if((wibox = wibox_getbywin(ev->event)))
443 if(wibox->mouse_over)
445 if(wibox->mouse_over->mouse_leave != LUA_REFNIL)
447 /* call mouse leave function on widget the mouse was over */
448 luaA_wibox_userdata_new(globalconf.L, wibox);
449 luaA_dofunction(globalconf.L, wibox->mouse_over->mouse_leave, 1, 0);
451 wibox->mouse_over = NULL;
455 return 0;
458 /** The enter notify event handler.
459 * \param data currently unused.
460 * \param connection The connection to the X server.
461 * \param ev The event.
463 static int
464 event_handle_enternotify(void *data __attribute__ ((unused)),
465 xcb_connection_t *connection,
466 xcb_enter_notify_event_t *ev)
468 client_t *c;
469 xembed_window_t *emwin;
470 wibox_t *wibox;
471 widget_t *w;
473 if(ev->mode != XCB_NOTIFY_MODE_NORMAL
474 || (ev->root_x == globalconf.pointer_x
475 && ev->root_y == globalconf.pointer_y))
476 return 0;
479 if((wibox = wibox_getbywin(ev->event))
480 && (w = widget_getbycoords(wibox->position, &wibox->widgets,
481 wibox->sw.geometry.width,
482 wibox->sw.geometry.height,
483 &ev->event_x, &ev->event_y)))
485 globalconf.pointer_x = ev->root_x;
486 globalconf.pointer_y = ev->root_y;
487 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
489 else if((c = client_getbytitlebarwin(ev->event))
490 || (c = client_getbywin(ev->event)))
492 window_buttons_grab(c->win, ev->root, &c->buttons);
493 /* The idea behind saving pointer_x and pointer_y is Bob Marley powered.
494 * this will allow us top drop some EnterNotify events and thus not giving
495 * focus to windows appering under the cursor without a cursor move */
496 globalconf.pointer_x = ev->root_x;
497 globalconf.pointer_y = ev->root_y;
499 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
501 luaA_client_userdata_new(globalconf.L, c);
502 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
505 else if((emwin = xembed_getbywin(&globalconf.embedded, ev->event)))
506 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY,
507 xutil_screen_get(connection, emwin->phys_screen)->root,
508 XCB_BUTTON_MASK_ANY);
509 else if(ev->event == ev->root)
510 window_root_buttons_grab(ev->root);
512 return 0;
515 /** The expose event handler.
516 * \param data currently unused.
517 * \param connection The connection to the X server.
518 * \param ev The event.
520 static int
521 event_handle_expose(void *data __attribute__ ((unused)),
522 xcb_connection_t *connection __attribute__ ((unused)),
523 xcb_expose_event_t *ev)
525 wibox_t *wibox = wibox_getbywin(ev->window);
527 if(wibox)
528 simplewindow_refresh_pixmap_partial(&wibox->sw,
529 ev->x, ev->y,
530 ev->width, ev->height);
532 return 0;
535 /** The key press event handler.
536 * \param data currently unused.
537 * \param connection The connection to the X server.
538 * \param ev The event.
540 static int
541 event_handle_key(void *data __attribute__ ((unused)),
542 xcb_connection_t *connection __attribute__ ((unused)),
543 xcb_key_press_event_t *ev)
545 if(globalconf.keygrabber != LUA_REFNIL)
547 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
548 if(keygrabber_handlekpress(globalconf.L, ev))
550 if(lua_pcall(globalconf.L, 3, 1, 0))
552 warn("error running function: %s", lua_tostring(globalconf.L, -1));
553 luaA_keygrabber_stop(globalconf.L);
555 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
556 luaA_keygrabber_stop(globalconf.L);
558 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
560 else
562 keyb_t *k = key_find(ev);
564 if(k)
565 switch(ev->response_type)
567 case XCB_KEY_PRESS:
568 if(k->press != LUA_REFNIL)
569 luaA_dofunction(globalconf.L, k->press, 0, 0);
570 break;
571 case XCB_KEY_RELEASE:
572 if(k->release != LUA_REFNIL)
573 luaA_dofunction(globalconf.L, k->release, 0, 0);
574 break;
578 return 0;
581 /** The map request event handler.
582 * \param data currently unused.
583 * \param connection The connection to the X server.
584 * \param ev The event.
586 static int
587 event_handle_maprequest(void *data __attribute__ ((unused)),
588 xcb_connection_t *connection, xcb_map_request_event_t *ev)
590 int phys_screen, screen = 0, ret = 0;
591 client_t *c;
592 xcb_get_window_attributes_cookie_t wa_c;
593 xcb_get_window_attributes_reply_t *wa_r;
594 xcb_query_pointer_cookie_t qp_c = { 0 };
595 xcb_query_pointer_reply_t *qp_r = NULL;
596 xcb_get_geometry_cookie_t geom_c;
597 xcb_get_geometry_reply_t *geom_r;
599 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
601 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
602 return -1;
604 if(wa_r->override_redirect)
605 goto bailout;
607 if(xembed_getbywin(&globalconf.embedded, ev->window))
609 xcb_map_window(connection, ev->window);
610 xembed_window_activate(connection, ev->window);
612 else if((c = client_getbywin(ev->window)))
614 /* Check that it may be visible, but not asked to be hidden */
615 if(client_maybevisible(c, c->screen) && !c->ishidden)
617 client_setminimized(c, false);
618 /* it will be raised, so just update ourself */
619 client_raise(c);
622 else
624 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
626 if(globalconf.xinerama_is_active)
627 qp_c = xcb_query_pointer_unchecked(connection,
628 xutil_screen_get(globalconf.connection,
629 globalconf.default_screen)->root);
631 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
633 if(globalconf.xinerama_is_active)
634 qp_r = xcb_query_pointer_reply(connection, qp_c, NULL);
635 ret = -1;
636 goto bailout;
640 phys_screen = xutil_root2screen(connection, geom_r->root);
642 if(globalconf.xinerama_is_active
643 && (qp_r = xcb_query_pointer_reply(connection, qp_c, NULL)))
644 screen = screen_getbycoord(screen, qp_r->root_x, qp_r->root_y);
645 else
646 screen = phys_screen;
648 client_manage(ev->window, geom_r, phys_screen, screen);
649 p_delete(&geom_r);
652 bailout:
653 p_delete(&qp_r);
654 p_delete(&wa_r);
655 return ret;
658 /** The unmap notify event handler.
659 * \param data currently unused.
660 * \param connection The connection to the X server.
661 * \param ev The event.
663 static int
664 event_handle_unmapnotify(void *data __attribute__ ((unused)),
665 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
667 client_t *c;
669 if((c = client_getbywin(ev->window)))
671 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
672 && XCB_EVENT_SENT(ev)
673 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
674 client_unmanage(c);
676 else
677 for(int i = 0; i < globalconf.embedded.len; i++)
678 if(globalconf.embedded.tab[i].win == ev->window)
680 xembed_window_array_take(&globalconf.embedded, i);
681 for(int j = 0; j < globalconf.nscreen; j++)
682 widget_invalidate_bytype(j, widget_systray);
685 return 0;
688 /** The randr screen change notify event handler.
689 * \param data currently unused.
690 * \param connection The connection to the X server.
691 * \param ev The event.
693 static int
694 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
695 xcb_connection_t *connection __attribute__ ((unused)),
696 xcb_randr_screen_change_notify_event_t *ev)
698 if(!globalconf.have_randr)
699 return -1;
701 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
702 * (only the code relevant to RRScreenChangeNotify) as the latter
703 * doesn't provide this kind of function */
704 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
705 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
706 ev->mheight, ev->mwidth);
707 else
708 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
709 ev->mwidth, ev->mheight);
711 /* XRRUpdateConfiguration also executes the following instruction
712 * but it's not useful because SubpixelOrder is not used at all at
713 * the moment
715 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
718 awesome_restart();
720 return 0;
723 /** The client message event handler.
724 * \param data currently unused.
725 * \param connection The connection to the X server.
726 * \param ev The event.
728 static int
729 event_handle_clientmessage(void *data __attribute__ ((unused)),
730 xcb_connection_t *connection,
731 xcb_client_message_event_t *ev)
733 client_t *c;
735 if(ev->type == WM_CHANGE_STATE)
737 if((c = client_getbywin(ev->window))
738 && ev->format == 32
739 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
740 client_setminimized(c, true);
742 else if(ev->type == _XEMBED)
743 return xembed_process_client_message(ev);
744 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
745 return systray_process_client_message(ev);
746 return ewmh_process_client_message(ev);
749 /** The keymap change notify event handler.
750 * \param data Unused data.
751 * \param connection The connection to the X server.
752 * \param ev The event.
753 * \return Status code, 0 if everything's fine.
755 static int
756 event_handle_mappingnotify(void *data,
757 xcb_connection_t *connection,
758 xcb_mapping_notify_event_t *ev)
760 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
762 if(ev->request == XCB_MAPPING_MODIFIER
763 || ev->request == XCB_MAPPING_KEYBOARD)
765 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
766 int phys_screen = 0;
768 /* Send the request to get the NumLock, ShiftLock and CapsLock masks */
769 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
771 /* Free and then allocate the key symbols */
772 xcb_key_symbols_free(globalconf.keysyms);
773 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
775 /* Process the reply of previously sent mapping request */
776 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
777 globalconf.keysyms, &globalconf.numlockmask,
778 &globalconf.shiftlockmask, &globalconf.capslockmask);
782 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
783 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look
784 * weird */
785 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
786 phys_screen++;
787 } while(phys_screen < nscreen);
789 /* regrab everything */
790 key_array_t *arr = &globalconf.keys.by_sym;
791 for(int i = 0; i < arr->len; i++)
792 window_root_grabkey(arr->tab[i]);
794 arr = &globalconf.keys.by_code;
795 for(int i = 0; i < arr->len; i++)
796 window_root_grabkey(arr->tab[i]);
799 return 0;
802 static int
803 event_handle_reparentnotify(void *data,
804 xcb_connection_t *connection,
805 xcb_reparent_notify_event_t *ev)
807 client_t *c;
809 if((c = client_getbywin(ev->window)))
810 client_unmanage(c);
812 return 0;
815 void a_xcb_set_event_handlers(void)
817 const xcb_query_extension_reply_t *randr_query;
819 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
820 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
821 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
822 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
823 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
824 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
825 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
826 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
827 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
828 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_key, NULL);
829 xcb_event_set_key_release_handler(&globalconf.evenths, event_handle_key, NULL);
830 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
831 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
832 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
833 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
834 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
836 /* check for randr extension */
837 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
838 if((globalconf.have_randr = randr_query->present))
839 xcb_event_set_handler(&globalconf.evenths,
840 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
841 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
842 NULL);
846 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80