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.
23 #include <xcb/randr.h>
24 #include <xcb/xcb_atom.h>
25 #include <xcb/xcb_icccm.h>
26 #include <xcb/xcb_event.h>
36 #include "keybinding.h"
37 #include "keygrabber.h"
41 #include "layouts/floating.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.
54 event_handle_mouse_button(client_t
*c
,
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
)
66 luaA_client_userdata_new(globalconf
.L
, c
);
67 luaA_dofunction(globalconf
.L
,
68 type
== XCB_BUTTON_PRESS
?
69 buttons
->tab
[i
]->press
: buttons
->tab
[i
]->release
,
73 luaA_dofunction(globalconf
.L
,
74 type
== XCB_BUTTON_PRESS
?
75 buttons
->tab
[i
]->press
: buttons
->tab
[i
]->release
,
80 /** Get a widget node from a wibox by coords.
81 * \param Container position.
82 * \param widgets The widget list.
83 * \param width The container width.
84 * \param height The container height.
85 * \param x X coordinate of the widget.
86 * \param y Y coordinate of the widget.
87 * \return A widget node.
89 static widget_node_t
*
90 widget_getbycoords(position_t position
, widget_node_t
*widgets
, int width
, int height
, int16_t *x
, int16_t *y
)
95 /* Need to transform coordinates like it was top/bottom */
112 for(w
= widgets
; w
; w
= w
->next
)
113 if(*x
>= w
->area
.x
&& *x
< w
->area
.x
+ w
->area
.width
114 && *y
>= w
->area
.y
&& *y
< w
->area
.y
+ w
->area
.height
)
120 /** The button press event handler.
121 * \param data The type of mouse event.
122 * \param connection The connection to the X server.
123 * \param ev The event.
126 event_handle_button(void *data
, xcb_connection_t
*connection
, xcb_button_press_event_t
*ev
)
129 const int nb_screen
= xcb_setup_roots_length(xcb_get_setup(connection
));
135 * button status (8 bits) + modifiers status (8 bits)
136 * we don't care for button status that we get, especially on release, so
140 if((wibox
= wibox_getbywin(ev
->event
))
141 || (wibox
= wibox_getbywin(ev
->child
)))
143 /* If the wibox is child, then x,y are
144 * relative to root window */
145 if(wibox
->sw
.window
== ev
->child
)
147 ev
->event_x
-= wibox
->sw
.geometry
.x
;
148 ev
->event_y
-= wibox
->sw
.geometry
.y
;
150 if((w
= widget_getbycoords(wibox
->position
, wibox
->widgets
,
151 wibox
->sw
.geometry
.width
,
152 wibox
->sw
.geometry
.height
,
153 &ev
->event_x
, &ev
->event_y
)))
154 w
->widget
->button(w
, ev
, wibox
->screen
, wibox
);
155 /* return even if no widget match */
159 if((c
= client_getbywin(ev
->event
)))
161 event_handle_mouse_button(c
, ev
->response_type
, ev
->detail
, ev
->state
, &c
->buttons
);
162 xcb_allow_events(globalconf
.connection
,
163 XCB_ALLOW_REPLAY_POINTER
,
167 for(screen
= 0; screen
< nb_screen
; screen
++)
168 if(xutil_screen_get(connection
, screen
)->root
== ev
->event
)
170 event_handle_mouse_button(NULL
, ev
->response_type
, ev
->detail
, ev
->state
, &globalconf
.buttons
);
177 /** The configure event handler.
178 * \param data currently unused.
179 * \param connection The connection to the X server.
180 * \param ev The event.
183 event_handle_configurerequest(void *data
__attribute__ ((unused
)),
184 xcb_connection_t
*connection
, xcb_configure_request_event_t
*ev
)
189 if((c
= client_getbywin(ev
->window
)))
191 geometry
= c
->geometry
;
193 if(ev
->value_mask
& XCB_CONFIG_WINDOW_X
)
195 if(ev
->value_mask
& XCB_CONFIG_WINDOW_Y
)
197 if(ev
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
198 geometry
.width
= ev
->width
;
199 if(ev
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
200 geometry
.height
= ev
->height
;
202 if(geometry
.x
!= c
->geometry
.x
|| geometry
.y
!= c
->geometry
.y
203 || geometry
.width
!= c
->geometry
.width
|| geometry
.height
!= c
->geometry
.height
)
205 if(client_isfloating(c
) || layout_get_current(c
->screen
) == layout_floating
)
207 client_resize(c
, geometry
, false);
208 if(client_hasstrut(c
))
209 /* All the wiboxes (may) need to be repositioned */
210 for(int screen
= 0; screen
< globalconf
.nscreen
; screen
++)
211 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
213 wibox_t
*s
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
214 wibox_position_update(s
);
219 client_need_arrange(c
);
220 /* If we do not resize the client, at least tell it that it
221 * has its new configuration. That fixes at least
223 window_configure(c
->win
, c
->geometry
, c
->border
);
227 window_configure(c
->win
, geometry
, c
->border
);
231 uint16_t config_win_mask
= 0;
232 uint32_t config_win_vals
[7];
233 unsigned short i
= 0;
235 if(ev
->value_mask
& XCB_CONFIG_WINDOW_X
)
237 config_win_mask
|= XCB_CONFIG_WINDOW_X
;
238 config_win_vals
[i
++] = ev
->x
;
240 if(ev
->value_mask
& XCB_CONFIG_WINDOW_Y
)
242 config_win_mask
|= XCB_CONFIG_WINDOW_Y
;
243 config_win_vals
[i
++] = ev
->y
;
245 if(ev
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
247 config_win_mask
|= XCB_CONFIG_WINDOW_WIDTH
;
248 config_win_vals
[i
++] = ev
->width
;
250 if(ev
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
252 config_win_mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
253 config_win_vals
[i
++] = ev
->height
;
255 if(ev
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
257 config_win_mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
258 config_win_vals
[i
++] = ev
->border_width
;
260 if(ev
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
262 config_win_mask
|= XCB_CONFIG_WINDOW_SIBLING
;
263 config_win_vals
[i
++] = ev
->sibling
;
265 if(ev
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
267 config_win_mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
268 config_win_vals
[i
++] = ev
->stack_mode
;
271 xcb_configure_window(connection
, ev
->window
, config_win_mask
, config_win_vals
);
277 /** The configure notify event handler.
278 * \param data currently unused.
279 * \param connection The connection to the X server.
280 * \param ev The event.
283 event_handle_configurenotify(void *data
__attribute__ ((unused
)),
284 xcb_connection_t
*connection
, xcb_configure_notify_event_t
*ev
)
287 const xcb_screen_t
*screen
;
289 for(screen_nbr
= 0; screen_nbr
< xcb_setup_roots_length(xcb_get_setup (connection
)); screen_nbr
++)
290 if((screen
= xutil_screen_get(connection
, screen_nbr
)) != NULL
291 && ev
->window
== screen
->root
292 && (ev
->width
!= screen
->width_in_pixels
293 || ev
->height
!= screen
->height_in_pixels
))
294 /* it's not that we panic, but restart */
300 /** The destroy notify event handler.
301 * \param data currently unused.
302 * \param connection The connection to the X server.
303 * \param ev The event.
306 event_handle_destroynotify(void *data
__attribute__ ((unused
)),
307 xcb_connection_t
*connection
__attribute__ ((unused
)),
308 xcb_destroy_notify_event_t
*ev
)
311 xembed_window_t
*emwin
;
314 if((c
= client_getbywin(ev
->window
)))
316 else if((emwin
= xembed_getbywin(globalconf
.embedded
, ev
->event
)))
318 xembed_window_list_detach(&globalconf
.embedded
, emwin
);
319 for(i
= 0; i
< globalconf
.nscreen
; i
++)
320 widget_invalidate_cache(i
, WIDGET_CACHE_EMBEDDED
);
326 /** Handle a motion notify over widgets.
327 * \param object The object.
328 * \param mouse_over The pointer to the registered mouse over widget.
329 * \param w The new widget the mouse is over.
332 event_handle_widget_motionnotify(void *object
,
333 widget_node_t
**mouse_over
,
340 /* call mouse leave function on old widget */
341 luaA_wibox_userdata_new(globalconf
.L
, object
);
342 luaA_dofunction(globalconf
.L
, (*mouse_over
)->widget
->mouse_leave
, 1, 0);
346 /* call mouse enter function on new widget and register it */
348 luaA_wibox_userdata_new(globalconf
.L
, object
);
349 luaA_dofunction(globalconf
.L
, w
->widget
->mouse_enter
, 1, 0);
354 /** The motion notify event handler.
355 * \param data currently unused.
356 * \param connection The connection to the X server.
357 * \param ev The event.
360 event_handle_motionnotify(void *data
__attribute__ ((unused
)),
361 xcb_connection_t
*connection
,
362 xcb_motion_notify_event_t
*ev
)
364 wibox_t
*wibox
= wibox_getbywin(ev
->event
);
369 w
= widget_getbycoords(wibox
->position
, wibox
->widgets
,
370 wibox
->sw
.geometry
.width
,
371 wibox
->sw
.geometry
.height
,
372 &ev
->event_x
, &ev
->event_y
);
373 event_handle_widget_motionnotify(wibox
, &wibox
->mouse_over
, w
);
379 /** The leave notify event handler.
380 * \param data currently unused.
381 * \param connection The connection to the X server.
382 * \param ev The event.
385 event_handle_leavenotify(void *data
__attribute__ ((unused
)),
386 xcb_connection_t
*connection
,
387 xcb_leave_notify_event_t
*ev
)
389 wibox_t
*wibox
= wibox_getbywin(ev
->event
);
391 if(wibox
&& wibox
->mouse_over
)
393 /* call mouse leave function on widget the mouse was over */
394 luaA_wibox_userdata_new(globalconf
.L
, wibox
);
395 luaA_dofunction(globalconf
.L
, wibox
->mouse_over
->widget
->mouse_leave
, 1, 0);
401 /** The enter notify event handler.
402 * \param data currently unused.
403 * \param connection The connection to the X server.
404 * \param ev The event.
407 event_handle_enternotify(void *data
__attribute__ ((unused
)),
408 xcb_connection_t
*connection
,
409 xcb_enter_notify_event_t
*ev
)
412 xembed_window_t
*emwin
;
414 if(ev
->mode
!= XCB_NOTIFY_MODE_NORMAL
415 || (ev
->root_x
== globalconf
.pointer_x
416 && ev
->root_y
== globalconf
.pointer_y
))
419 if((c
= client_getbytitlebarwin(ev
->event
))
420 || (c
= client_getbywin(ev
->event
)))
422 window_buttons_grab(c
->win
, ev
->root
, &c
->buttons
);
423 /* The idea behind saving pointer_x and pointer_y is Bob Marley powered.
424 * this will allow us top drop some EnterNotify events and thus not giving
425 * focus to windows appering under the cursor without a cursor move */
426 globalconf
.pointer_x
= ev
->root_x
;
427 globalconf
.pointer_y
= ev
->root_y
;
429 luaA_client_userdata_new(globalconf
.L
, c
);
430 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.mouse_enter
, 1, 0);
432 else if((emwin
= xembed_getbywin(globalconf
.embedded
, ev
->event
)))
433 xcb_ungrab_button(globalconf
.connection
, XCB_BUTTON_INDEX_ANY
,
434 xutil_screen_get(connection
, emwin
->phys_screen
)->root
,
435 XCB_BUTTON_MASK_ANY
);
436 else if(ev
->event
== ev
->root
)
437 window_root_buttons_grab(ev
->root
);
442 /** The expose event handler.
443 * \param data currently unused.
444 * \param connection The connection to the X server.
445 * \param ev The event.
448 event_handle_expose(void *data
__attribute__ ((unused
)),
449 xcb_connection_t
*connection
__attribute__ ((unused
)),
450 xcb_expose_event_t
*ev
)
452 wibox_t
*wibox
= wibox_getbywin(ev
->window
);
455 simplewindow_refresh_pixmap_partial(&wibox
->sw
,
457 ev
->width
, ev
->height
);
462 /** The key press event handler.
463 * \param data currently unused.
464 * \param connection The connection to the X server.
465 * \param ev The event.
468 event_handle_keypress(void *data
__attribute__ ((unused
)),
469 xcb_connection_t
*connection
__attribute__ ((unused
)),
470 xcb_key_press_event_t
*ev
)
472 if(globalconf
.keygrabber
!= LUA_REFNIL
)
474 lua_rawgeti(globalconf
.L
, LUA_REGISTRYINDEX
, globalconf
.keygrabber
);
475 if(keygrabber_handlekpress(globalconf
.L
, ev
))
477 if(lua_pcall(globalconf
.L
, 2, 1, 0))
479 warn("error running function: %s", lua_tostring(globalconf
.L
, -1));
482 else if(!lua_isboolean(globalconf
.L
, -1) || !lua_toboolean(globalconf
.L
, -1))
485 lua_pop(globalconf
.L
, 1); /* pop returned value or function if not called */
489 keybinding_t
*k
= keybinding_find(ev
);
490 if (k
&& k
->fct
!= LUA_REFNIL
)
491 luaA_dofunction(globalconf
.L
, k
->fct
, 0, 0);
497 /** The map request event handler.
498 * \param data currently unused.
499 * \param connection The connection to the X server.
500 * \param ev The event.
503 event_handle_maprequest(void *data
__attribute__ ((unused
)),
504 xcb_connection_t
*connection
, xcb_map_request_event_t
*ev
)
506 int phys_screen
, screen
= 0, ret
= 0;
508 xcb_get_window_attributes_cookie_t wa_c
;
509 xcb_get_window_attributes_reply_t
*wa_r
;
510 xcb_query_pointer_cookie_t qp_c
= { 0 };
511 xcb_query_pointer_reply_t
*qp_r
= NULL
;
512 xcb_get_geometry_cookie_t geom_c
;
513 xcb_get_geometry_reply_t
*geom_r
;
515 wa_c
= xcb_get_window_attributes_unchecked(connection
, ev
->window
);
517 if(!(wa_r
= xcb_get_window_attributes_reply(connection
, wa_c
, NULL
)))
520 if(wa_r
->override_redirect
)
523 if(xembed_getbywin(globalconf
.embedded
, ev
->window
))
525 xcb_map_window(connection
, ev
->window
);
526 xembed_window_activate(connection
, ev
->window
);
528 else if((c
= client_getbywin(ev
->window
)))
530 /* Check that it may be visible, but not asked to be hidden */
531 if(client_maybevisible(c
, c
->screen
) && !c
->ishidden
)
533 client_setminimized(c
, false);
534 /* it will be raised, so just update ourself */
540 geom_c
= xcb_get_geometry_unchecked(connection
, ev
->window
);
542 if(globalconf
.xinerama_is_active
)
543 qp_c
= xcb_query_pointer_unchecked(connection
,
544 xutil_screen_get(globalconf
.connection
,
545 globalconf
.default_screen
)->root
);
547 if(!(geom_r
= xcb_get_geometry_reply(connection
, geom_c
, NULL
)))
549 if(globalconf
.xinerama_is_active
)
550 qp_r
= xcb_query_pointer_reply(connection
, qp_c
, NULL
);
556 phys_screen
= xutil_root2screen(connection
, geom_r
->root
);
558 if(globalconf
.xinerama_is_active
559 && (qp_r
= xcb_query_pointer_reply(connection
, qp_c
, NULL
)))
560 screen
= screen_getbycoord(screen
, qp_r
->root_x
, qp_r
->root_y
);
562 screen
= phys_screen
;
564 client_manage(ev
->window
, geom_r
, phys_screen
, screen
);
574 /** The unmap notify event handler.
575 * \param data currently unused.
576 * \param connection The connection to the X server.
577 * \param ev The event.
580 event_handle_unmapnotify(void *data
__attribute__ ((unused
)),
581 xcb_connection_t
*connection
, xcb_unmap_notify_event_t
*ev
)
587 if((c
= client_getbywin(ev
->window
)))
589 if(ev
->event
== xutil_screen_get(connection
, c
->phys_screen
)->root
590 && XCB_EVENT_SENT(ev
)
591 && window_state_get_reply(window_state_get_unchecked(c
->win
)) == XCB_WM_STATE_NORMAL
)
594 else if((em
= xembed_getbywin(globalconf
.embedded
, ev
->window
)))
596 xembed_window_list_detach(&globalconf
.embedded
, em
);
597 for(i
= 0; i
< globalconf
.nscreen
; i
++)
598 widget_invalidate_cache(i
, WIDGET_CACHE_EMBEDDED
);
604 /** The randr screen change notify event handler.
605 * \param data currently unused.
606 * \param connection The connection to the X server.
607 * \param ev The event.
610 event_handle_randr_screen_change_notify(void *data
__attribute__ ((unused
)),
611 xcb_connection_t
*connection
__attribute__ ((unused
)),
612 xcb_randr_screen_change_notify_event_t
*ev
)
614 if(!globalconf
.have_randr
)
617 /* Code of XRRUpdateConfiguration Xlib function ported to XCB
618 * (only the code relevant to RRScreenChangeNotify) as the latter
619 * doesn't provide this kind of function */
620 if(ev
->rotation
& (XCB_RANDR_ROTATION_ROTATE_90
| XCB_RANDR_ROTATION_ROTATE_270
))
621 xcb_randr_set_screen_size(connection
, ev
->root
, ev
->height
, ev
->width
,
622 ev
->mheight
, ev
->mwidth
);
624 xcb_randr_set_screen_size(connection
, ev
->root
, ev
->width
, ev
->height
,
625 ev
->mwidth
, ev
->mheight
);
627 /* XRRUpdateConfiguration also executes the following instruction
628 * but it's not useful because SubpixelOrder is not used at all at
631 * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order);
639 /** The client message event handler.
640 * \param data currently unused.
641 * \param connection The connection to the X server.
642 * \param ev The event.
645 event_handle_clientmessage(void *data
__attribute__ ((unused
)),
646 xcb_connection_t
*connection
,
647 xcb_client_message_event_t
*ev
)
651 if(ev
->type
== WM_CHANGE_STATE
)
653 if((c
= client_getbywin(ev
->window
))
655 && ev
->data
.data32
[0] == XCB_WM_STATE_ICONIC
)
656 client_setminimized(c
, true);
658 else if(ev
->type
== _XEMBED
)
659 return xembed_process_client_message(ev
);
660 else if(ev
->type
== _NET_SYSTEM_TRAY_OPCODE
)
661 return systray_process_client_message(ev
);
662 return ewmh_process_client_message(ev
);
665 /** The keymap change notify event handler.
666 * \param data Unused data.
667 * \param connection The connection to the X server.
668 * \param ev The event.
669 * \return Status code, 0 if everything's fine.
672 event_handle_mappingnotify(void *data
,
673 xcb_connection_t
*connection
,
674 xcb_mapping_notify_event_t
*ev
)
676 xcb_get_modifier_mapping_cookie_t xmapping_cookie
;
678 if(ev
->request
== XCB_MAPPING_MODIFIER
679 || ev
->request
== XCB_MAPPING_KEYBOARD
)
681 /* Send the request to get the NumLock, ShiftLock and CapsLock masks */
682 xmapping_cookie
= xcb_get_modifier_mapping_unchecked(globalconf
.connection
);
684 /* Free and then allocate the key symbols */
685 xcb_key_symbols_free(globalconf
.keysyms
);
686 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
688 /* Process the reply of previously sent mapping request */
689 xutil_lock_mask_get(globalconf
.connection
, xmapping_cookie
,
690 globalconf
.keysyms
, &globalconf
.numlockmask
,
691 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
);
698 event_handle_reparentnotify(void *data
,
699 xcb_connection_t
*connection
,
700 xcb_reparent_notify_event_t
*ev
)
704 if((c
= client_getbywin(ev
->window
)))
710 void a_xcb_set_event_handlers(void)
712 const xcb_query_extension_reply_t
*randr_query
;
714 xcb_event_set_button_press_handler(&globalconf
.evenths
, event_handle_button
, NULL
);
715 xcb_event_set_button_release_handler(&globalconf
.evenths
, event_handle_button
, NULL
);
716 xcb_event_set_configure_request_handler(&globalconf
.evenths
, event_handle_configurerequest
, NULL
);
717 xcb_event_set_configure_notify_handler(&globalconf
.evenths
, event_handle_configurenotify
, NULL
);
718 xcb_event_set_destroy_notify_handler(&globalconf
.evenths
, event_handle_destroynotify
, NULL
);
719 xcb_event_set_enter_notify_handler(&globalconf
.evenths
, event_handle_enternotify
, NULL
);
720 xcb_event_set_leave_notify_handler(&globalconf
.evenths
, event_handle_leavenotify
, NULL
);
721 xcb_event_set_motion_notify_handler(&globalconf
.evenths
, event_handle_motionnotify
, NULL
);
722 xcb_event_set_expose_handler(&globalconf
.evenths
, event_handle_expose
, NULL
);
723 xcb_event_set_key_press_handler(&globalconf
.evenths
, event_handle_keypress
, NULL
);
724 xcb_event_set_map_request_handler(&globalconf
.evenths
, event_handle_maprequest
, NULL
);
725 xcb_event_set_unmap_notify_handler(&globalconf
.evenths
, event_handle_unmapnotify
, NULL
);
726 xcb_event_set_client_message_handler(&globalconf
.evenths
, event_handle_clientmessage
, NULL
);
727 xcb_event_set_mapping_notify_handler(&globalconf
.evenths
, event_handle_mappingnotify
, NULL
);
728 xcb_event_set_reparent_notify_handler(&globalconf
.evenths
, event_handle_reparentnotify
, NULL
);
730 /* check for randr extension */
731 randr_query
= xcb_get_extension_data(globalconf
.connection
, &xcb_randr_id
);
732 if((globalconf
.have_randr
= randr_query
->present
))
733 xcb_event_set_handler(&globalconf
.evenths
,
734 randr_query
->first_event
+ XCB_RANDR_SCREEN_CHANGE_NOTIFY
,
735 (xcb_generic_event_handler_t
) event_handle_randr_screen_change_notify
,
740 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80