awful.widget: beautiful font for {tag,task}list
[awesome.git] / event.c
blob77ac2e2816c83ba93093ef68fb0e6599305bab6a
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 "keybinding.h"
37 #include "keygrabber.h"
38 #include "mousegrabber.h"
39 #include "luaa.h"
40 #include "systray.h"
41 #include "screen.h"
42 #include "layouts/floating.h"
43 #include "common/atoms.h"
45 extern awesome_t globalconf;
47 /** Handle mouse button events.
48 * \param c The client on which the event happened or NULL.
49 * \param type Event type, press or release.
50 * \param button Button number.
51 * \param state Modkeys state.
52 * \param buttons Buttons array to check for.
54 static void
55 event_handle_mouse_button(client_t *c,
56 uint8_t type,
57 xcb_button_t button,
58 uint16_t state,
59 button_array_t *buttons)
61 for(int i = 0; i < buttons->len; i++)
62 if(button == buttons->tab[i]->button
63 && XUTIL_MASK_CLEAN(state) == buttons->tab[i]->mod)
64 switch(type)
66 case XCB_BUTTON_PRESS:
67 if(buttons->tab[i]->press != LUA_REFNIL)
69 if(c)
71 luaA_client_userdata_new(globalconf.L, c);
72 luaA_dofunction(globalconf.L, buttons->tab[i]->press, 1, 0);
74 else
75 luaA_dofunction(globalconf.L, buttons->tab[i]->press, 0, 0);
77 break;
78 case XCB_BUTTON_RELEASE:
79 if(buttons->tab[i]->release != LUA_REFNIL)
81 if(c)
83 luaA_client_userdata_new(globalconf.L, c);
84 luaA_dofunction(globalconf.L, buttons->tab[i]->release, 1, 0);
86 else
87 luaA_dofunction(globalconf.L, buttons->tab[i]->release, 0, 0);
89 break;
93 /** Get a widget node from a wibox by coords.
94 * \param Container position.
95 * \param widgets The widget list.
96 * \param width The container width.
97 * \param height The container height.
98 * \param x X coordinate of the widget.
99 * \param y Y coordinate of the widget.
100 * \return A widget.
102 static widget_t *
103 widget_getbycoords(position_t position, widget_node_array_t *widgets,
104 int width, int height, int16_t *x, int16_t *y)
106 int tmp;
108 /* Need to transform coordinates like it was top/bottom */
109 switch(position)
111 case Right:
112 tmp = *y;
113 *y = width - *x;
114 *x = tmp;
115 break;
116 case Left:
117 tmp = *y;
118 *y = *x;
119 *x = height - tmp;
120 break;
121 default:
122 break;
125 for(int i = 0; i < widgets->len; i++)
127 widget_node_t *w = &widgets->tab[i];
128 if(w->widget->isvisible &&
129 *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
130 && *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
131 return w->widget;
134 return NULL;
137 /** Handle an event with mouse grabber if needed
138 * \param x The x coordinate.
139 * \param y The y coordinate.
140 * \param mask The mask buttons.
142 static void
143 event_handle_mousegrabber(int x, int y, uint16_t mask)
145 if(globalconf.mousegrabber != LUA_REFNIL)
147 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber);
148 mousegrabber_handleevent(globalconf.L, x, y, mask);
149 if(lua_pcall(globalconf.L, 1, 1, 0))
151 warn("error running function: %s", lua_tostring(globalconf.L, -1));
152 luaA_mousegrabber_stop(globalconf.L);
154 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
155 luaA_mousegrabber_stop(globalconf.L);
156 lua_pop(globalconf.L, 1); /* pop returned value */
160 /** The button press event handler.
161 * \param data The type of mouse event.
162 * \param connection The connection to the X server.
163 * \param ev The event.
165 static int
166 event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev)
168 int screen;
169 const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection));
170 client_t *c;
171 widget_t *w;
172 wibox_t *wibox;
174 /* ev->state is
175 * button status (8 bits) + modifiers status (8 bits)
176 * we don't care for button status that we get, especially on release, so
177 * drop them */
178 ev->state &= 0x00ff;
180 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
182 if((wibox = wibox_getbywin(ev->event))
183 || (wibox = wibox_getbywin(ev->child)))
185 /* If the wibox is child, then x,y are
186 * relative to root window */
187 if(wibox->sw.window == ev->child)
189 ev->event_x -= wibox->sw.geometry.x;
190 ev->event_y -= wibox->sw.geometry.y;
192 if((w = widget_getbycoords(wibox->position, &wibox->widgets,
193 wibox->sw.geometry.width,
194 wibox->sw.geometry.height,
195 &ev->event_x, &ev->event_y)))
197 button_array_t *b = &w->buttons;
199 for(int i = 0; i < b->len; i++)
200 if(ev->detail == b->tab[i]->button
201 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
202 switch(ev->response_type)
204 case XCB_BUTTON_PRESS:
205 if(b->tab[i]->press != LUA_REFNIL)
207 luaA_wibox_userdata_new(globalconf.L, wibox);
208 luaA_dofunction(globalconf.L, b->tab[i]->press, 1, 0);
210 break;
211 case XCB_BUTTON_RELEASE:
212 if(b->tab[i]->release != LUA_REFNIL)
214 luaA_wibox_userdata_new(globalconf.L, wibox);
215 luaA_dofunction(globalconf.L, b->tab[i]->release, 1, 0);
217 break;
220 /* return even if no widget match */
221 return 0;
224 if((c = client_getbywin(ev->event)))
226 event_handle_mouse_button(c, ev->response_type, ev->detail, ev->state, &c->buttons);
227 xcb_allow_events(globalconf.connection,
228 XCB_ALLOW_REPLAY_POINTER,
229 XCB_CURRENT_TIME);
231 else
232 for(screen = 0; screen < nb_screen; screen++)
233 if(xutil_screen_get(connection, screen)->root == ev->event)
235 event_handle_mouse_button(NULL, ev->response_type, ev->detail, ev->state, &globalconf.buttons);
236 return 0;
239 return 0;
242 /** The configure event handler.
243 * \param data currently unused.
244 * \param connection The connection to the X server.
245 * \param ev The event.
247 static int
248 event_handle_configurerequest(void *data __attribute__ ((unused)),
249 xcb_connection_t *connection, xcb_configure_request_event_t *ev)
251 client_t *c;
252 area_t geometry;
254 if((c = client_getbywin(ev->window)))
256 geometry = c->geometry;
258 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
259 geometry.x = ev->x;
260 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
261 geometry.y = ev->y;
262 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
263 geometry.width = ev->width;
264 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
265 geometry.height = ev->height;
267 if(geometry.x != c->geometry.x || geometry.y != c->geometry.y
268 || geometry.width != c->geometry.width || geometry.height != c->geometry.height)
270 if(client_isfloating(c) || layout_get_current(c->screen) == layout_floating)
272 client_resize(c, geometry, false);
273 if(client_hasstrut(c))
274 /* All the wiboxes (may) need to be repositioned */
275 for(int screen = 0; screen < globalconf.nscreen; screen++)
276 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
278 wibox_t *s = globalconf.screens[screen].wiboxes.tab[i];
279 wibox_position_update(s);
282 else
284 client_need_arrange(c);
285 /* If we do not resize the client, at least tell it that it
286 * has its new configuration. That fixes at least
287 * gnome-terminal */
288 window_configure(c->win, c->geometry, c->border);
291 else
292 window_configure(c->win, geometry, c->border);
294 else
296 uint16_t config_win_mask = 0;
297 uint32_t config_win_vals[7];
298 unsigned short i = 0;
300 if(ev->value_mask & XCB_CONFIG_WINDOW_X)
302 config_win_mask |= XCB_CONFIG_WINDOW_X;
303 config_win_vals[i++] = ev->x;
305 if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
307 config_win_mask |= XCB_CONFIG_WINDOW_Y;
308 config_win_vals[i++] = ev->y;
310 if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
312 config_win_mask |= XCB_CONFIG_WINDOW_WIDTH;
313 config_win_vals[i++] = ev->width;
315 if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
317 config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT;
318 config_win_vals[i++] = ev->height;
320 if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
322 config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
323 config_win_vals[i++] = ev->border_width;
325 if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
327 config_win_mask |= XCB_CONFIG_WINDOW_SIBLING;
328 config_win_vals[i++] = ev->sibling;
330 if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
332 config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE;
333 config_win_vals[i++] = ev->stack_mode;
336 xcb_configure_window(connection, ev->window, config_win_mask, config_win_vals);
339 return 0;
342 /** The configure notify event handler.
343 * \param data currently unused.
344 * \param connection The connection to the X server.
345 * \param ev The event.
347 static int
348 event_handle_configurenotify(void *data __attribute__ ((unused)),
349 xcb_connection_t *connection, xcb_configure_notify_event_t *ev)
351 int screen_nbr;
352 const xcb_screen_t *screen;
354 for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++)
355 if((screen = xutil_screen_get(connection, screen_nbr)) != NULL
356 && ev->window == screen->root
357 && (ev->width != screen->width_in_pixels
358 || ev->height != screen->height_in_pixels))
359 /* it's not that we panic, but restart */
360 awesome_restart();
362 return 0;
365 /** The destroy notify event handler.
366 * \param data currently unused.
367 * \param connection The connection to the X server.
368 * \param ev The event.
370 static int
371 event_handle_destroynotify(void *data __attribute__ ((unused)),
372 xcb_connection_t *connection __attribute__ ((unused)),
373 xcb_destroy_notify_event_t *ev)
375 client_t *c;
376 xembed_window_t *emwin;
378 if((c = client_getbywin(ev->window)))
379 client_unmanage(c);
380 else if((emwin = xembed_getbywin(globalconf.embedded, ev->event)))
382 xembed_window_list_detach(&globalconf.embedded, emwin);
383 for(int i = 0; i < globalconf.nscreen; i++)
384 widget_invalidate_bytype(i, widget_systray);
387 return 0;
390 /** Handle a motion notify over widgets.
391 * \param object The object.
392 * \param mouse_over The pointer to the registered mouse over widget.
393 * \param widget The new widget the mouse is over.
395 static void
396 event_handle_widget_motionnotify(void *object,
397 widget_t **mouse_over,
398 widget_t *widget)
400 if(widget != *mouse_over)
402 if(*mouse_over)
404 if((*mouse_over)->mouse_leave != LUA_REFNIL)
406 /* call mouse leave function on old widget */
407 luaA_wibox_userdata_new(globalconf.L, object);
408 luaA_dofunction(globalconf.L, (*mouse_over)->mouse_leave, 1, 0);
411 if(widget)
413 /* call mouse enter function on new widget and register it */
414 *mouse_over = widget;
415 if(widget->mouse_enter != LUA_REFNIL)
417 luaA_wibox_userdata_new(globalconf.L, object);
418 luaA_dofunction(globalconf.L, widget->mouse_enter, 1, 0);
424 /** The motion notify event handler.
425 * \param data currently unused.
426 * \param connection The connection to the X server.
427 * \param ev The event.
429 static int
430 event_handle_motionnotify(void *data __attribute__ ((unused)),
431 xcb_connection_t *connection,
432 xcb_motion_notify_event_t *ev)
434 wibox_t *wibox = wibox_getbywin(ev->event);
435 widget_t *w;
437 event_handle_mousegrabber(ev->root_x, ev->root_y, ev->state);
439 if(wibox
440 && (w = widget_getbycoords(wibox->position, &wibox->widgets,
441 wibox->sw.geometry.width,
442 wibox->sw.geometry.height,
443 &ev->event_x, &ev->event_y)))
445 globalconf.pointer_x = ev->root_x;
446 globalconf.pointer_y = ev->root_y;
447 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
450 return 0;
453 /** The leave notify event handler.
454 * \param data currently unused.
455 * \param connection The connection to the X server.
456 * \param ev The event.
458 static int
459 event_handle_leavenotify(void *data __attribute__ ((unused)),
460 xcb_connection_t *connection,
461 xcb_leave_notify_event_t *ev)
463 wibox_t *wibox = wibox_getbywin(ev->event);
465 if(wibox && 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;
476 return 0;
479 /** The enter notify event handler.
480 * \param data currently unused.
481 * \param connection The connection to the X server.
482 * \param ev The event.
484 static int
485 event_handle_enternotify(void *data __attribute__ ((unused)),
486 xcb_connection_t *connection,
487 xcb_enter_notify_event_t *ev)
489 client_t *c;
490 xembed_window_t *emwin;
491 wibox_t *wibox;
492 widget_t *w;
494 if(ev->mode != XCB_NOTIFY_MODE_NORMAL
495 || (ev->root_x == globalconf.pointer_x
496 && ev->root_y == globalconf.pointer_y))
497 return 0;
500 if((wibox = wibox_getbywin(ev->event))
501 && (w = widget_getbycoords(wibox->position, &wibox->widgets,
502 wibox->sw.geometry.width,
503 wibox->sw.geometry.height,
504 &ev->event_x, &ev->event_y)))
506 globalconf.pointer_x = ev->root_x;
507 globalconf.pointer_y = ev->root_y;
508 event_handle_widget_motionnotify(wibox, &wibox->mouse_over, w);
510 else if((c = client_getbytitlebarwin(ev->event))
511 || (c = client_getbywin(ev->event)))
513 window_buttons_grab(c->win, ev->root, &c->buttons);
514 /* The idea behind saving pointer_x and pointer_y is Bob Marley powered.
515 * this will allow us top drop some EnterNotify events and thus not giving
516 * focus to windows appering under the cursor without a cursor move */
517 globalconf.pointer_x = ev->root_x;
518 globalconf.pointer_y = ev->root_y;
520 if(globalconf.hooks.mouse_enter != LUA_REFNIL)
522 luaA_client_userdata_new(globalconf.L, c);
523 luaA_dofunction(globalconf.L, globalconf.hooks.mouse_enter, 1, 0);
526 else if((emwin = xembed_getbywin(globalconf.embedded, ev->event)))
527 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY,
528 xutil_screen_get(connection, emwin->phys_screen)->root,
529 XCB_BUTTON_MASK_ANY);
530 else if(ev->event == ev->root)
531 window_root_buttons_grab(ev->root);
533 return 0;
536 /** The expose event handler.
537 * \param data currently unused.
538 * \param connection The connection to the X server.
539 * \param ev The event.
541 static int
542 event_handle_expose(void *data __attribute__ ((unused)),
543 xcb_connection_t *connection __attribute__ ((unused)),
544 xcb_expose_event_t *ev)
546 wibox_t *wibox = wibox_getbywin(ev->window);
548 if(wibox)
549 simplewindow_refresh_pixmap_partial(&wibox->sw,
550 ev->x, ev->y,
551 ev->width, ev->height);
553 return 0;
556 /** The key press event handler.
557 * \param data currently unused.
558 * \param connection The connection to the X server.
559 * \param ev The event.
561 static int
562 event_handle_keypress(void *data __attribute__ ((unused)),
563 xcb_connection_t *connection __attribute__ ((unused)),
564 xcb_key_press_event_t *ev)
566 if(globalconf.keygrabber != LUA_REFNIL)
568 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber);
569 if(keygrabber_handlekpress(globalconf.L, ev))
571 if(lua_pcall(globalconf.L, 2, 1, 0))
573 warn("error running function: %s", lua_tostring(globalconf.L, -1));
574 luaA_keygrabber_stop(globalconf.L);
576 else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1))
577 luaA_keygrabber_stop(globalconf.L);
579 lua_pop(globalconf.L, 1); /* pop returned value or function if not called */
581 else
583 keybinding_t *k = keybinding_find(ev);
584 if (k && k->fct != LUA_REFNIL)
585 luaA_dofunction(globalconf.L, k->fct, 0, 0);
588 return 0;
591 /** The map request event handler.
592 * \param data currently unused.
593 * \param connection The connection to the X server.
594 * \param ev The event.
596 static int
597 event_handle_maprequest(void *data __attribute__ ((unused)),
598 xcb_connection_t *connection, xcb_map_request_event_t *ev)
600 int phys_screen, screen = 0, ret = 0;
601 client_t *c;
602 xcb_get_window_attributes_cookie_t wa_c;
603 xcb_get_window_attributes_reply_t *wa_r;
604 xcb_query_pointer_cookie_t qp_c = { 0 };
605 xcb_query_pointer_reply_t *qp_r = NULL;
606 xcb_get_geometry_cookie_t geom_c;
607 xcb_get_geometry_reply_t *geom_r;
609 wa_c = xcb_get_window_attributes_unchecked(connection, ev->window);
611 if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL)))
612 return -1;
614 if(wa_r->override_redirect)
615 goto bailout;
617 if(xembed_getbywin(globalconf.embedded, ev->window))
619 xcb_map_window(connection, ev->window);
620 xembed_window_activate(connection, ev->window);
622 else if((c = client_getbywin(ev->window)))
624 /* Check that it may be visible, but not asked to be hidden */
625 if(client_maybevisible(c, c->screen) && !c->ishidden)
627 client_setminimized(c, false);
628 /* it will be raised, so just update ourself */
629 client_raise(c);
632 else
634 geom_c = xcb_get_geometry_unchecked(connection, ev->window);
636 if(globalconf.xinerama_is_active)
637 qp_c = xcb_query_pointer_unchecked(connection,
638 xutil_screen_get(globalconf.connection,
639 globalconf.default_screen)->root);
641 if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL)))
643 if(globalconf.xinerama_is_active)
644 qp_r = xcb_query_pointer_reply(connection, qp_c, NULL);
645 ret = -1;
646 goto bailout;
650 phys_screen = xutil_root2screen(connection, geom_r->root);
652 if(globalconf.xinerama_is_active
653 && (qp_r = xcb_query_pointer_reply(connection, qp_c, NULL)))
654 screen = screen_getbycoord(screen, qp_r->root_x, qp_r->root_y);
655 else
656 screen = phys_screen;
658 client_manage(ev->window, geom_r, phys_screen, screen);
659 p_delete(&geom_r);
662 bailout:
663 p_delete(&qp_r);
664 p_delete(&wa_r);
665 return ret;
668 /** The unmap notify event handler.
669 * \param data currently unused.
670 * \param connection The connection to the X server.
671 * \param ev The event.
673 static int
674 event_handle_unmapnotify(void *data __attribute__ ((unused)),
675 xcb_connection_t *connection, xcb_unmap_notify_event_t *ev)
677 client_t *c;
678 xembed_window_t *em;
680 if((c = client_getbywin(ev->window)))
682 if(ev->event == xutil_screen_get(connection, c->phys_screen)->root
683 && XCB_EVENT_SENT(ev)
684 && window_state_get_reply(window_state_get_unchecked(c->win)) == XCB_WM_STATE_NORMAL)
685 client_unmanage(c);
687 else if((em = xembed_getbywin(globalconf.embedded, ev->window)))
689 xembed_window_list_detach(&globalconf.embedded, em);
690 for(int i = 0; i < globalconf.nscreen; i++)
691 widget_invalidate_bytype(i, widget_systray);
694 return 0;
697 /** The randr screen change notify event handler.
698 * \param data currently unused.
699 * \param connection The connection to the X server.
700 * \param ev The event.
702 static int
703 event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)),
704 xcb_connection_t *connection __attribute__ ((unused)),
705 xcb_randr_screen_change_notify_event_t *ev)
707 if(!globalconf.have_randr)
708 return -1;
710 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
711 * (only the code relevant to RRScreenChangeNotify) as the latter
712 * doesn't provide this kind of function */
713 if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270))
714 xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width,
715 ev->mheight, ev->mwidth);
716 else
717 xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height,
718 ev->mwidth, ev->mheight);
720 /* XRRUpdateConfiguration also executes the following instruction
721 * but it's not useful because SubpixelOrder is not used at all at
722 * the moment
724 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
727 awesome_restart();
729 return 0;
732 /** The client message event handler.
733 * \param data currently unused.
734 * \param connection The connection to the X server.
735 * \param ev The event.
737 static int
738 event_handle_clientmessage(void *data __attribute__ ((unused)),
739 xcb_connection_t *connection,
740 xcb_client_message_event_t *ev)
742 client_t *c;
744 if(ev->type == WM_CHANGE_STATE)
746 if((c = client_getbywin(ev->window))
747 && ev->format == 32
748 && ev->data.data32[0] == XCB_WM_STATE_ICONIC)
749 client_setminimized(c, true);
751 else if(ev->type == _XEMBED)
752 return xembed_process_client_message(ev);
753 else if(ev->type == _NET_SYSTEM_TRAY_OPCODE)
754 return systray_process_client_message(ev);
755 return ewmh_process_client_message(ev);
758 /** The keymap change notify event handler.
759 * \param data Unused data.
760 * \param connection The connection to the X server.
761 * \param ev The event.
762 * \return Status code, 0 if everything's fine.
764 static int
765 event_handle_mappingnotify(void *data,
766 xcb_connection_t *connection,
767 xcb_mapping_notify_event_t *ev)
769 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
771 if(ev->request == XCB_MAPPING_MODIFIER
772 || ev->request == XCB_MAPPING_KEYBOARD)
774 int nscreen = xcb_setup_roots_length(xcb_get_setup(connection));
775 int phys_screen = 0;
777 /* Send the request to get the NumLock, ShiftLock and CapsLock masks */
778 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
780 /* Free and then allocate the key symbols */
781 xcb_key_symbols_free(globalconf.keysyms);
782 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
784 /* Process the reply of previously sent mapping request */
785 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
786 globalconf.keysyms, &globalconf.numlockmask,
787 &globalconf.shiftlockmask, &globalconf.capslockmask);
791 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
792 /* yes XCB_BUTTON_MASK_ANY is also for grab_key even if it's look
793 * weird */
794 xcb_ungrab_key(connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
795 phys_screen++;
796 } while(phys_screen < nscreen);
798 /* regrab everything */
799 keybinding_array_t *arr = &globalconf.keys.by_sym;
800 for(int i = 0; i < arr->len; i++)
801 window_root_grabkey(arr->tab[i]);
803 arr = &globalconf.keys.by_code;
804 for(int i = 0; i < arr->len; i++)
805 window_root_grabkey(arr->tab[i]);
808 return 0;
811 static int
812 event_handle_reparentnotify(void *data,
813 xcb_connection_t *connection,
814 xcb_reparent_notify_event_t *ev)
816 client_t *c;
818 if((c = client_getbywin(ev->window)))
819 client_unmanage(c);
821 return 0;
824 void a_xcb_set_event_handlers(void)
826 const xcb_query_extension_reply_t *randr_query;
828 xcb_event_set_button_press_handler(&globalconf.evenths, event_handle_button, NULL);
829 xcb_event_set_button_release_handler(&globalconf.evenths, event_handle_button, NULL);
830 xcb_event_set_configure_request_handler(&globalconf.evenths, event_handle_configurerequest, NULL);
831 xcb_event_set_configure_notify_handler(&globalconf.evenths, event_handle_configurenotify, NULL);
832 xcb_event_set_destroy_notify_handler(&globalconf.evenths, event_handle_destroynotify, NULL);
833 xcb_event_set_enter_notify_handler(&globalconf.evenths, event_handle_enternotify, NULL);
834 xcb_event_set_leave_notify_handler(&globalconf.evenths, event_handle_leavenotify, NULL);
835 xcb_event_set_motion_notify_handler(&globalconf.evenths, event_handle_motionnotify, NULL);
836 xcb_event_set_expose_handler(&globalconf.evenths, event_handle_expose, NULL);
837 xcb_event_set_key_press_handler(&globalconf.evenths, event_handle_keypress, NULL);
838 xcb_event_set_map_request_handler(&globalconf.evenths, event_handle_maprequest, NULL);
839 xcb_event_set_unmap_notify_handler(&globalconf.evenths, event_handle_unmapnotify, NULL);
840 xcb_event_set_client_message_handler(&globalconf.evenths, event_handle_clientmessage, NULL);
841 xcb_event_set_mapping_notify_handler(&globalconf.evenths, event_handle_mappingnotify, NULL);
842 xcb_event_set_reparent_notify_handler(&globalconf.evenths, event_handle_reparentnotify, NULL);
844 /* check for randr extension */
845 randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
846 if((globalconf.have_randr = randr_query->present))
847 xcb_event_set_handler(&globalconf.evenths,
848 randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
849 (xcb_generic_event_handler_t) event_handle_randr_screen_change_notify,
850 NULL);
854 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80