1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 event.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
32 #include "menuframe.h"
36 #include "framerender.h"
38 #include "moveresize.h"
41 #include "extensions.h"
44 #include <X11/keysym.h>
45 #include <X11/Xatom.h>
48 #ifdef HAVE_SYS_SELECT_H
49 # include <sys/select.h>
55 # include <X11/XKBlib.h>
59 #include <X11/ICE/ICElib.h>
73 static void event_process(const XEvent *e, gpointer data);
74 static void event_handle_root(XEvent *e);
75 static void event_handle_menu(XEvent *e);
76 static void event_handle_dock(ObDock *s, XEvent *e);
77 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
78 static void event_handle_client(ObClient *c, XEvent *e);
79 static void event_handle_group(ObGroup *g, XEvent *e);
81 static void focus_delay_dest(gpointer data);
82 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
83 static gboolean focus_delay_func(gpointer data);
84 static void focus_delay_client_dest(ObClient *client, gpointer data);
86 static gboolean menu_hide_delay_func(gpointer data);
88 /* The time for the current event being processed */
89 Time event_curtime = CurrentTime;
91 /*! The value of the mask for the NumLock modifier */
93 /*! The value of the mask for the ScrollLock modifier */
95 /*! The key codes for the modifier keys */
96 static XModifierKeymap *modmap;
97 /*! Table of the constant modifier masks */
98 static const gint mask_table[] = {
99 ShiftMask, LockMask, ControlMask, Mod1Mask,
100 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
102 static gint mask_table_size;
104 static guint ignore_enter_focus = 0;
106 static gboolean menu_can_hide;
109 static void ice_handler(gint fd, gpointer conn)
112 IceProcessMessages(conn, NULL, &b);
115 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
116 IcePointer *watch_data)
121 fd = IceConnectionNumber(conn);
122 ob_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
124 ob_main_loop_fd_remove(ob_main_loop, fd);
130 void event_startup(gboolean reconfig)
132 if (reconfig) return;
134 mask_table_size = sizeof(mask_table) / sizeof(mask_table[0]);
136 /* get lock masks that are defined by the display (not constant) */
137 modmap = XGetModifierMapping(ob_display);
139 if (modmap && modmap->max_keypermod > 0) {
141 const size_t size = mask_table_size * modmap->max_keypermod;
142 /* get the values of the keyboard lock modifiers
143 Note: Caps lock is not retrieved the same way as Scroll and Num
144 lock since it doesn't need to be. */
145 const KeyCode num_lock = XKeysymToKeycode(ob_display, XK_Num_Lock);
146 const KeyCode scroll_lock = XKeysymToKeycode(ob_display,
149 for (cnt = 0; cnt < size; ++cnt) {
150 if (! modmap->modifiermap[cnt]) continue;
152 if (num_lock == modmap->modifiermap[cnt])
153 NumLockMask = mask_table[cnt / modmap->max_keypermod];
154 if (scroll_lock == modmap->modifiermap[cnt])
155 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
159 ob_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
162 IceAddConnectionWatch(ice_watch, NULL);
165 client_add_destructor(focus_delay_client_dest, NULL);
168 void event_shutdown(gboolean reconfig)
170 if (reconfig) return;
173 IceRemoveConnectionWatch(ice_watch, NULL);
176 client_remove_destructor(focus_delay_client_dest);
177 XFreeModifiermap(modmap);
180 static Window event_get_window(XEvent *e)
187 window = RootWindow(ob_display, ob_screen);
190 window = e->xmap.window;
193 window = e->xunmap.window;
196 window = e->xdestroywindow.window;
198 case ConfigureRequest:
199 window = e->xconfigurerequest.window;
201 case ConfigureNotify:
202 window = e->xconfigure.window;
206 if (extensions_xkb && e->type == extensions_xkb_event_basep) {
207 switch (((XkbAnyEvent*)e)->xkb_type) {
209 window = ((XkbBellNotifyEvent*)e)->window;
215 window = e->xany.window;
220 static void event_set_curtime(XEvent *e)
222 Time t = CurrentTime;
224 /* grab the lasttime and hack up the state */
240 t = e->xproperty.time;
244 t = e->xcrossing.time;
247 /* if more event types are anticipated, get their timestamp
255 #define STRIP_MODS(s) \
256 s &= ~(LockMask | NumLockMask | ScrollLockMask), \
257 /* kill off the Button1Mask etc, only want the modifiers */ \
258 s &= (ControlMask | ShiftMask | Mod1Mask | \
259 Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask) \
261 static void event_hack_mods(XEvent *e)
264 XkbStateRec xkb_state;
272 STRIP_MODS(e->xbutton.state);
275 STRIP_MODS(e->xkey.state);
278 STRIP_MODS(e->xkey.state);
279 /* remove from the state the mask of the modifier being released, if
280 it is a modifier key being released (this is a little ugly..) */
282 if (XkbGetState(ob_display, XkbUseCoreKbd, &xkb_state) == Success) {
283 e->xkey.state = xkb_state.compat_state;
287 kp = modmap->modifiermap;
288 for (i = 0; i < mask_table_size; ++i) {
289 for (k = 0; k < modmap->max_keypermod; ++k) {
290 if (*kp == e->xkey.keycode) { /* found the keycode */
291 /* remove the mask for it */
292 e->xkey.state &= ~mask_table[i];
293 /* cause the first loop to break; */
295 break; /* get outta here! */
302 STRIP_MODS(e->xmotion.state);
303 /* compress events */
306 while (XCheckTypedWindowEvent(ob_display, e->xmotion.window,
308 e->xmotion.x_root = ce.xmotion.x_root;
309 e->xmotion.y_root = ce.xmotion.y_root;
316 static gboolean wanted_focusevent(XEvent *e)
318 gint mode = e->xfocus.mode;
319 gint detail = e->xfocus.detail;
320 Window win = e->xany.window;
322 if (e->type == FocusIn) {
324 /* These are ones we never want.. */
326 /* This means focus was given by a keyboard/mouse grab. */
327 if (mode == NotifyGrab)
329 /* This means focus was given back from a keyboard/mouse grab. */
330 if (mode == NotifyUngrab)
333 /* These are the ones we want.. */
335 if (win == RootWindow(ob_display, ob_screen)) {
336 /* This means focus reverted off of a client */
337 if (detail == NotifyPointerRoot || detail == NotifyDetailNone ||
338 detail == NotifyInferior)
344 /* This means focus moved from the root window to a client */
345 if (detail == NotifyVirtual)
347 /* This means focus moved from one client to another */
348 if (detail == NotifyNonlinearVirtual)
354 g_assert(e->type == FocusOut);
357 /* These are ones we never want.. */
359 /* This means focus was taken by a keyboard/mouse grab. */
360 if (mode == NotifyGrab)
363 /* Focus left the root window revertedto state */
364 if (win == RootWindow(ob_display, ob_screen))
367 /* These are the ones we want.. */
369 /* This means focus moved from a client to the root window */
370 if (detail == NotifyVirtual)
372 /* This means focus moved from one client to another */
373 if (detail == NotifyNonlinearVirtual)
381 static Bool look_for_focusin(Display *d, XEvent *e, XPointer arg)
383 return e->type == FocusIn && wanted_focusevent(e);
386 static gboolean event_ignore(XEvent *e, ObClient *client)
390 if (!wanted_focusevent(e))
396 if (!wanted_focusevent(e))
403 static void event_process(const XEvent *ec, gpointer data)
406 ObGroup *group = NULL;
407 ObClient *client = NULL;
409 ObDockApp *dockapp = NULL;
410 ObWindow *obwin = NULL;
412 ObEventData *ed = data;
414 /* make a copy we can mangle */
418 window = event_get_window(e);
419 if (!(e->type == PropertyNotify &&
420 (group = g_hash_table_lookup(group_map, &window))))
421 if ((obwin = g_hash_table_lookup(window_map, &window))) {
422 switch (obwin->type) {
424 dock = WINDOW_AS_DOCK(obwin);
427 dockapp = WINDOW_AS_DOCKAPP(obwin);
430 client = WINDOW_AS_CLIENT(obwin);
433 case Window_Internal:
434 /* not to be used for events */
435 g_assert_not_reached();
440 event_set_curtime(e);
442 if (event_ignore(e, client)) {
449 /* deal with it in the kernel */
451 if (menu_frame_visible &&
452 (e->type == EnterNotify || e->type == LeaveNotify))
454 /* crossing events for menu */
455 event_handle_menu(e);
456 } else if (e->type == FocusIn) {
457 if (e->xfocus.detail == NotifyPointerRoot ||
458 e->xfocus.detail == NotifyDetailNone) {
459 ob_debug_type(OB_DEBUG_FOCUS, "Focus went to root\n");
460 /* Focus has been reverted to the root window or nothing
461 FocusOut events come after UnmapNotify, so we don't need to
462 worry about focusing an invalid window
464 focus_fallback(TRUE);
465 } else if (e->xfocus.detail == NotifyInferior) {
466 ob_debug_type(OB_DEBUG_FOCUS, "Focus went to parent\n");
467 /* Focus has been reverted to parent, which is our frame window,
469 FocusOut events come after UnmapNotify, so we don't need to
470 worry about focusing an invalid window
472 focus_fallback(TRUE);
473 } else if (client && client != focus_client) {
474 frame_adjust_focus(client->frame, TRUE);
475 focus_set_client(client);
476 client_calc_layer(client);
478 } else if (e->type == FocusOut) {
479 gboolean nomove = FALSE;
482 ob_debug_type(OB_DEBUG_FOCUS, "FocusOut Event\n");
484 /* Look for the followup FocusIn */
485 if (!XCheckIfEvent(ob_display, &ce, look_for_focusin, NULL)) {
486 /* There is no FocusIn, this means focus went to a window that
487 is not being managed, or a window on another screen. */
488 ob_debug_type(OB_DEBUG_FOCUS, "Focus went to a black hole !\n");
489 /* nothing is focused */
490 focus_set_client(NULL);
491 } else if (ce.xany.window == e->xany.window) {
492 /* If focus didn't actually move anywhere, there is nothing to do*/
495 /* Focus did move, so process the FocusIn event */
496 ObEventData ed = { .ignored = FALSE };
497 event_process(&ce, &ed);
499 /* The FocusIn was ignored, this means it was on a window
500 that isn't a client. */
501 ob_debug_type(OB_DEBUG_FOCUS,
502 "Focus went to an unmanaged window 0x%x !\n",
504 focus_fallback(TRUE);
508 if (client && !nomove) {
509 frame_adjust_focus(client->frame, FALSE);
510 /* focus_set_client has already been called for sure */
511 client_calc_layer(client);
514 event_handle_group(group, e);
516 event_handle_client(client, e);
518 event_handle_dockapp(dockapp, e);
520 event_handle_dock(dock, e);
521 else if (window == RootWindow(ob_display, ob_screen))
522 event_handle_root(e);
523 else if (e->type == MapRequest)
524 client_manage(window);
525 else if (e->type == ConfigureRequest) {
526 /* unhandled configure requests must be used to configure the
530 xwc.x = e->xconfigurerequest.x;
531 xwc.y = e->xconfigurerequest.y;
532 xwc.width = e->xconfigurerequest.width;
533 xwc.height = e->xconfigurerequest.height;
534 xwc.border_width = e->xconfigurerequest.border_width;
535 xwc.sibling = e->xconfigurerequest.above;
536 xwc.stack_mode = e->xconfigurerequest.detail;
538 /* we are not to be held responsible if someone sends us an
540 xerror_set_ignore(TRUE);
541 XConfigureWindow(ob_display, window,
542 e->xconfigurerequest.value_mask, &xwc);
543 xerror_set_ignore(FALSE);
546 /* user input (action-bound) events */
547 if (e->type == ButtonPress || e->type == ButtonRelease ||
548 e->type == MotionNotify || e->type == KeyPress ||
549 e->type == KeyRelease)
551 if (menu_frame_visible)
552 event_handle_menu(e);
554 if (!keyboard_process_interactive_grab(e, &client)) {
555 if (moveresize_in_progress) {
558 /* make further actions work on the client being
560 client = moveresize_client;
563 menu_can_hide = FALSE;
564 ob_main_loop_timeout_add(ob_main_loop,
565 config_menu_hide_delay * 1000,
566 menu_hide_delay_func,
567 NULL, g_direct_equal, NULL);
569 if (e->type == ButtonPress || e->type == ButtonRelease ||
570 e->type == MotionNotify) {
571 mouse_event(client, e);
572 } else if (e->type == KeyPress) {
573 keyboard_event((focus_cycle_target ? focus_cycle_target :
579 /* if something happens and it's not from an XEvent, then we don't know
581 event_curtime = CurrentTime;
584 static void event_handle_root(XEvent *e)
590 ob_debug("Another WM has requested to replace us. Exiting.\n");
595 if (e->xclient.format != 32) break;
597 msgtype = e->xclient.message_type;
598 if (msgtype == prop_atoms.net_current_desktop) {
599 guint d = e->xclient.data.l[0];
600 if (d < screen_num_desktops) {
601 event_curtime = e->xclient.data.l[1];
602 ob_debug("SWITCH DESKTOP TIME: %d\n", event_curtime);
603 screen_set_desktop(d);
605 } else if (msgtype == prop_atoms.net_number_of_desktops) {
606 guint d = e->xclient.data.l[0];
608 screen_set_num_desktops(d);
609 } else if (msgtype == prop_atoms.net_showing_desktop) {
610 screen_show_desktop(e->xclient.data.l[0] != 0);
611 } else if (msgtype == prop_atoms.ob_control) {
612 if (e->xclient.data.l[0] == 1)
614 else if (e->xclient.data.l[0] == 2)
619 if (e->xproperty.atom == prop_atoms.net_desktop_names)
620 screen_update_desktop_names();
621 else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
622 screen_update_layout();
624 case ConfigureNotify:
626 XRRUpdateConfiguration(e);
635 static void event_handle_group(ObGroup *group, XEvent *e)
639 g_assert(e->type == PropertyNotify);
641 for (it = group->members; it; it = g_slist_next(it))
642 event_handle_client(it->data, e);
645 void event_enter_client(ObClient *client)
647 g_assert(config_focus_follow);
649 if (client_normal(client) && client_can_focus(client)) {
650 if (config_focus_delay) {
651 ObFocusDelayData *data;
653 ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
655 data = g_new(ObFocusDelayData, 1);
656 data->client = client;
657 data->time = event_curtime;
659 ob_main_loop_timeout_add(ob_main_loop,
662 data, focus_delay_cmp, focus_delay_dest);
664 ObFocusDelayData data;
665 data.client = client;
666 data.time = event_curtime;
667 focus_delay_func(&data);
672 static void event_handle_client(ObClient *client, XEvent *e)
680 case VisibilityNotify:
681 client->frame->obscured = e->xvisibility.state != VisibilityUnobscured;
685 /* Wheel buttons don't draw because they are an instant click, so it
686 is a waste of resources to go drawing it. */
687 if (!(e->xbutton.button == 4 || e->xbutton.button == 5)) {
688 con = frame_context(client, e->xbutton.window);
689 con = mouse_button_frame_context(con, e->xbutton.button);
691 case OB_FRAME_CONTEXT_MAXIMIZE:
692 client->frame->max_press = (e->type == ButtonPress);
693 framerender_frame(client->frame);
695 case OB_FRAME_CONTEXT_CLOSE:
696 client->frame->close_press = (e->type == ButtonPress);
697 framerender_frame(client->frame);
699 case OB_FRAME_CONTEXT_ICONIFY:
700 client->frame->iconify_press = (e->type == ButtonPress);
701 framerender_frame(client->frame);
703 case OB_FRAME_CONTEXT_ALLDESKTOPS:
704 client->frame->desk_press = (e->type == ButtonPress);
705 framerender_frame(client->frame);
707 case OB_FRAME_CONTEXT_SHADE:
708 client->frame->shade_press = (e->type == ButtonPress);
709 framerender_frame(client->frame);
712 /* nothing changes with clicks for any other contexts */
718 con = frame_context(client, e->xcrossing.window);
720 case OB_FRAME_CONTEXT_MAXIMIZE:
721 client->frame->max_hover = FALSE;
722 frame_adjust_state(client->frame);
724 case OB_FRAME_CONTEXT_ALLDESKTOPS:
725 client->frame->desk_hover = FALSE;
726 frame_adjust_state(client->frame);
728 case OB_FRAME_CONTEXT_SHADE:
729 client->frame->shade_hover = FALSE;
730 frame_adjust_state(client->frame);
732 case OB_FRAME_CONTEXT_ICONIFY:
733 client->frame->iconify_hover = FALSE;
734 frame_adjust_state(client->frame);
736 case OB_FRAME_CONTEXT_CLOSE:
737 client->frame->close_hover = FALSE;
738 frame_adjust_state(client->frame);
740 case OB_FRAME_CONTEXT_FRAME:
741 if (keyboard_interactively_grabbed())
743 if (config_focus_follow && config_focus_delay)
744 ob_main_loop_timeout_remove_data(ob_main_loop,
754 gboolean nofocus = FALSE;
756 if (ignore_enter_focus) {
757 ignore_enter_focus--;
761 con = frame_context(client, e->xcrossing.window);
763 case OB_FRAME_CONTEXT_MAXIMIZE:
764 client->frame->max_hover = TRUE;
765 frame_adjust_state(client->frame);
767 case OB_FRAME_CONTEXT_ALLDESKTOPS:
768 client->frame->desk_hover = TRUE;
769 frame_adjust_state(client->frame);
771 case OB_FRAME_CONTEXT_SHADE:
772 client->frame->shade_hover = TRUE;
773 frame_adjust_state(client->frame);
775 case OB_FRAME_CONTEXT_ICONIFY:
776 client->frame->iconify_hover = TRUE;
777 frame_adjust_state(client->frame);
779 case OB_FRAME_CONTEXT_CLOSE:
780 client->frame->close_hover = TRUE;
781 frame_adjust_state(client->frame);
783 case OB_FRAME_CONTEXT_FRAME:
784 if (keyboard_interactively_grabbed())
786 if (e->xcrossing.mode == NotifyGrab ||
787 e->xcrossing.mode == NotifyUngrab)
789 ob_debug_type(OB_DEBUG_FOCUS,
790 "%sNotify mode %d detail %d on %lx IGNORED\n",
791 (e->type == EnterNotify ? "Enter" : "Leave"),
793 e->xcrossing.detail, client?client->window:0);
795 ob_debug_type(OB_DEBUG_FOCUS,
796 "%sNotify mode %d detail %d on %lx, "
797 "focusing window: %d\n",
798 (e->type == EnterNotify ? "Enter" : "Leave"),
800 e->xcrossing.detail, (client?client->window:0),
802 if (!nofocus && config_focus_follow)
803 event_enter_client(client);
811 case ConfigureRequest:
813 while (XCheckTypedWindowEvent(ob_display, client->window,
814 ConfigureRequest, &ce)) {
816 /* XXX if this causes bad things.. we can compress config req's
817 with the same mask. */
818 e->xconfigurerequest.value_mask |=
819 ce.xconfigurerequest.value_mask;
820 if (ce.xconfigurerequest.value_mask & CWX)
821 e->xconfigurerequest.x = ce.xconfigurerequest.x;
822 if (ce.xconfigurerequest.value_mask & CWY)
823 e->xconfigurerequest.y = ce.xconfigurerequest.y;
824 if (ce.xconfigurerequest.value_mask & CWWidth)
825 e->xconfigurerequest.width = ce.xconfigurerequest.width;
826 if (ce.xconfigurerequest.value_mask & CWHeight)
827 e->xconfigurerequest.height = ce.xconfigurerequest.height;
828 if (ce.xconfigurerequest.value_mask & CWBorderWidth)
829 e->xconfigurerequest.border_width =
830 ce.xconfigurerequest.border_width;
831 if (ce.xconfigurerequest.value_mask & CWStackMode)
832 e->xconfigurerequest.detail = ce.xconfigurerequest.detail;
835 /* if we are iconic (or shaded (fvwm does this)) ignore the event */
836 if (client->iconic || client->shaded) return;
838 /* resize, then move, as specified in the EWMH section 7.7 */
839 if (e->xconfigurerequest.value_mask & (CWWidth | CWHeight |
845 if (e->xconfigurerequest.value_mask & CWBorderWidth)
846 client->border_width = e->xconfigurerequest.border_width;
848 x = (e->xconfigurerequest.value_mask & CWX) ?
849 e->xconfigurerequest.x : client->area.x;
850 y = (e->xconfigurerequest.value_mask & CWY) ?
851 e->xconfigurerequest.y : client->area.y;
852 w = (e->xconfigurerequest.value_mask & CWWidth) ?
853 e->xconfigurerequest.width : client->area.width;
854 h = (e->xconfigurerequest.value_mask & CWHeight) ?
855 e->xconfigurerequest.height : client->area.height;
861 client->frame->size.left + client->frame->size.right;
863 client->frame->size.top + client->frame->size.bottom;
864 /* make this rude for size-only changes but not for position
866 gboolean moving = ((e->xconfigurerequest.value_mask & CWX) ||
867 (e->xconfigurerequest.value_mask & CWY));
869 client_find_onscreen(client, &newx, &newy, fw, fh,
871 if (e->xconfigurerequest.value_mask & CWX)
873 if (e->xconfigurerequest.value_mask & CWY)
877 switch (client->gravity) {
878 case NorthEastGravity:
880 corner = OB_CORNER_TOPRIGHT;
882 case SouthWestGravity:
884 corner = OB_CORNER_BOTTOMLEFT;
886 case SouthEastGravity:
887 corner = OB_CORNER_BOTTOMRIGHT;
889 default: /* NorthWest, Static, etc */
890 corner = OB_CORNER_TOPLEFT;
893 client_configure_full(client, corner, x, y, w, h, FALSE, TRUE,
897 if (e->xconfigurerequest.value_mask & CWStackMode) {
898 switch (e->xconfigurerequest.detail) {
901 /* Apps are so rude. And this is totally disconnected from
902 activation/focus. Bleh. */
903 /*client_lower(client);*/
909 /* Apps are so rude. And this is totally disconnected from
910 activation/focus. Bleh. */
911 /*client_raise(client);*/
917 if (client->ignore_unmaps) {
918 client->ignore_unmaps--;
921 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
923 client->window, e->xunmap.event, e->xunmap.from_configure,
924 client->ignore_unmaps);
925 client_unmanage(client);
928 ob_debug("DestroyNotify for window 0x%x\n", client->window);
929 client_unmanage(client);
932 /* this is when the client is first taken captive in the frame */
933 if (e->xreparent.parent == client->frame->plate) break;
936 This event is quite rare and is usually handled in unmapHandler.
937 However, if the window is unmapped when the reparent event occurs,
938 the window manager never sees it because an unmap event is not sent
939 to an already unmapped window.
942 /* we don't want the reparent event, put it back on the stack for the
943 X server to deal with after we unmanage the window */
944 XPutBackEvent(ob_display, e);
946 ob_debug("ReparentNotify for window 0x%x\n", client->window);
947 client_unmanage(client);
950 ob_debug("MapRequest for 0x%lx\n", client->window);
951 if (!client->iconic) break; /* this normally doesn't happen, but if it
952 does, we don't want it!
953 it can happen now when the window is on
954 another desktop, but we still don't
956 client_activate(client, FALSE, TRUE);
959 /* validate cuz we query stuff off the client here */
960 if (!client_validate(client)) break;
962 if (e->xclient.format != 32) return;
964 msgtype = e->xclient.message_type;
965 if (msgtype == prop_atoms.wm_change_state) {
966 /* compress changes into a single change */
967 while (XCheckTypedWindowEvent(ob_display, client->window,
969 /* XXX: it would be nice to compress ALL messages of a
970 type, not just messages in a row without other
971 message types between. */
972 if (ce.xclient.message_type != msgtype) {
973 XPutBackEvent(ob_display, &ce);
976 e->xclient = ce.xclient;
978 client_set_wm_state(client, e->xclient.data.l[0]);
979 } else if (msgtype == prop_atoms.net_wm_desktop) {
980 /* compress changes into a single change */
981 while (XCheckTypedWindowEvent(ob_display, client->window,
983 /* XXX: it would be nice to compress ALL messages of a
984 type, not just messages in a row without other
985 message types between. */
986 if (ce.xclient.message_type != msgtype) {
987 XPutBackEvent(ob_display, &ce);
990 e->xclient = ce.xclient;
992 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
993 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
994 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
996 } else if (msgtype == prop_atoms.net_wm_state) {
997 /* can't compress these */
998 ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
999 (e->xclient.data.l[0] == 0 ? "Remove" :
1000 e->xclient.data.l[0] == 1 ? "Add" :
1001 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1002 e->xclient.data.l[1], e->xclient.data.l[2],
1004 client_set_state(client, e->xclient.data.l[0],
1005 e->xclient.data.l[1], e->xclient.data.l[2]);
1006 } else if (msgtype == prop_atoms.net_close_window) {
1007 ob_debug("net_close_window for 0x%lx\n", client->window);
1008 client_close(client);
1009 } else if (msgtype == prop_atoms.net_active_window) {
1010 ob_debug("net_active_window for 0x%lx source=%s\n",
1012 (e->xclient.data.l[0] == 0 ? "unknown" :
1013 (e->xclient.data.l[0] == 1 ? "application" :
1014 (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1015 /* XXX make use of data.l[2] ! */
1016 event_curtime = e->xclient.data.l[1];
1017 client_activate(client, FALSE,
1018 (e->xclient.data.l[0] == 0 ||
1019 e->xclient.data.l[0] == 2));
1020 } else if (msgtype == prop_atoms.net_wm_moveresize) {
1021 ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1022 client->window, e->xclient.data.l[2]);
1023 if ((Atom)e->xclient.data.l[2] ==
1024 prop_atoms.net_wm_moveresize_size_topleft ||
1025 (Atom)e->xclient.data.l[2] ==
1026 prop_atoms.net_wm_moveresize_size_top ||
1027 (Atom)e->xclient.data.l[2] ==
1028 prop_atoms.net_wm_moveresize_size_topright ||
1029 (Atom)e->xclient.data.l[2] ==
1030 prop_atoms.net_wm_moveresize_size_right ||
1031 (Atom)e->xclient.data.l[2] ==
1032 prop_atoms.net_wm_moveresize_size_right ||
1033 (Atom)e->xclient.data.l[2] ==
1034 prop_atoms.net_wm_moveresize_size_bottomright ||
1035 (Atom)e->xclient.data.l[2] ==
1036 prop_atoms.net_wm_moveresize_size_bottom ||
1037 (Atom)e->xclient.data.l[2] ==
1038 prop_atoms.net_wm_moveresize_size_bottomleft ||
1039 (Atom)e->xclient.data.l[2] ==
1040 prop_atoms.net_wm_moveresize_size_left ||
1041 (Atom)e->xclient.data.l[2] ==
1042 prop_atoms.net_wm_moveresize_move ||
1043 (Atom)e->xclient.data.l[2] ==
1044 prop_atoms.net_wm_moveresize_size_keyboard ||
1045 (Atom)e->xclient.data.l[2] ==
1046 prop_atoms.net_wm_moveresize_move_keyboard) {
1048 moveresize_start(client, e->xclient.data.l[0],
1049 e->xclient.data.l[1], e->xclient.data.l[3],
1050 e->xclient.data.l[2]);
1052 else if ((Atom)e->xclient.data.l[2] ==
1053 prop_atoms.net_wm_moveresize_cancel)
1054 moveresize_end(TRUE);
1055 } else if (msgtype == prop_atoms.net_moveresize_window) {
1056 gint oldg = client->gravity;
1057 gint tmpg, x, y, w, h;
1059 if (e->xclient.data.l[0] & 0xff)
1060 tmpg = e->xclient.data.l[0] & 0xff;
1064 if (e->xclient.data.l[0] & 1 << 8)
1065 x = e->xclient.data.l[1];
1068 if (e->xclient.data.l[0] & 1 << 9)
1069 y = e->xclient.data.l[2];
1072 if (e->xclient.data.l[0] & 1 << 10)
1073 w = e->xclient.data.l[3];
1075 w = client->area.width;
1076 if (e->xclient.data.l[0] & 1 << 11)
1077 h = e->xclient.data.l[4];
1079 h = client->area.height;
1080 client->gravity = tmpg;
1086 client->frame->size.left + client->frame->size.right;
1088 client->frame->size.top + client->frame->size.bottom;
1089 client_find_onscreen(client, &newx, &newy, fw, fh,
1090 client_normal(client));
1091 if (e->xclient.data.l[0] & 1 << 8)
1093 if (e->xclient.data.l[0] & 1 << 9)
1097 client_configure(client, OB_CORNER_TOPLEFT,
1098 x, y, w, h, FALSE, TRUE);
1100 client->gravity = oldg;
1103 case PropertyNotify:
1104 /* validate cuz we query stuff off the client here */
1105 if (!client_validate(client)) break;
1107 /* compress changes to a single property into a single change */
1108 while (XCheckTypedWindowEvent(ob_display, client->window,
1112 /* XXX: it would be nice to compress ALL changes to a property,
1113 not just changes in a row without other props between. */
1115 a = ce.xproperty.atom;
1116 b = e->xproperty.atom;
1120 if ((a == prop_atoms.net_wm_name ||
1121 a == prop_atoms.wm_name ||
1122 a == prop_atoms.net_wm_icon_name ||
1123 a == prop_atoms.wm_icon_name)
1125 (b == prop_atoms.net_wm_name ||
1126 b == prop_atoms.wm_name ||
1127 b == prop_atoms.net_wm_icon_name ||
1128 b == prop_atoms.wm_icon_name)) {
1131 if (a == prop_atoms.net_wm_icon &&
1132 b == prop_atoms.net_wm_icon)
1135 XPutBackEvent(ob_display, &ce);
1139 msgtype = e->xproperty.atom;
1140 if (msgtype == XA_WM_NORMAL_HINTS) {
1141 client_update_normal_hints(client);
1142 /* normal hints can make a window non-resizable */
1143 client_setup_decor_and_functions(client);
1144 } else if (msgtype == XA_WM_HINTS) {
1145 client_update_wmhints(client);
1146 } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1147 client_update_transient_for(client);
1148 client_get_type(client);
1149 /* type may have changed, so update the layer */
1150 client_calc_layer(client);
1151 client_setup_decor_and_functions(client);
1152 } else if (msgtype == prop_atoms.net_wm_name ||
1153 msgtype == prop_atoms.wm_name ||
1154 msgtype == prop_atoms.net_wm_icon_name ||
1155 msgtype == prop_atoms.wm_icon_name) {
1156 client_update_title(client);
1157 } else if (msgtype == prop_atoms.wm_class) {
1158 client_update_class(client);
1159 } else if (msgtype == prop_atoms.wm_protocols) {
1160 client_update_protocols(client);
1161 client_setup_decor_and_functions(client);
1163 else if (msgtype == prop_atoms.net_wm_strut) {
1164 client_update_strut(client);
1166 else if (msgtype == prop_atoms.net_wm_icon) {
1167 client_update_icons(client);
1169 else if (msgtype == prop_atoms.net_wm_user_time) {
1170 client_update_user_time(client);
1172 else if (msgtype == prop_atoms.sm_client_id) {
1173 client_update_sm_client_id(client);
1178 if (extensions_shape && e->type == extensions_shape_event_basep) {
1179 client->shaped = ((XShapeEvent*)e)->shaped;
1180 frame_adjust_shape(client->frame);
1186 static void event_handle_dock(ObDock *s, XEvent *e)
1190 if (e->xbutton.button == 1)
1191 stacking_raise(DOCK_AS_WINDOW(s));
1192 else if (e->xbutton.button == 2)
1193 stacking_lower(DOCK_AS_WINDOW(s));
1204 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1208 dock_app_drag(app, &e->xmotion);
1211 if (app->ignore_unmaps) {
1212 app->ignore_unmaps--;
1215 dock_remove(app, TRUE);
1218 dock_remove(app, FALSE);
1220 case ReparentNotify:
1221 dock_remove(app, FALSE);
1223 case ConfigureNotify:
1224 dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1229 ObMenuFrame* find_active_menu()
1232 ObMenuFrame *ret = NULL;
1234 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1243 ObMenuFrame* find_active_or_last_menu()
1245 ObMenuFrame *ret = NULL;
1247 ret = find_active_menu();
1248 if (!ret && menu_frame_visible)
1249 ret = menu_frame_visible->data;
1253 static void event_handle_menu(XEvent *ev)
1256 ObMenuEntryFrame *e;
1260 if (menu_can_hide) {
1261 if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1262 ev->xbutton.y_root)))
1263 menu_entry_frame_execute(e, ev->xbutton.state,
1266 menu_frame_hide_all();
1270 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1271 if (e->ignore_enters)
1274 menu_frame_select(e->frame, e);
1278 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1279 (f = find_active_menu()) && f->selected == e &&
1280 e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1282 menu_frame_select(e->frame, NULL);
1285 if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1286 ev->xmotion.y_root)))
1287 menu_frame_select(e->frame, e);
1290 if (ev->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
1291 menu_frame_hide_all();
1292 else if (ev->xkey.keycode == ob_keycode(OB_KEY_RETURN)) {
1294 if ((f = find_active_menu()))
1295 menu_entry_frame_execute(f->selected, ev->xkey.state,
1297 } else if (ev->xkey.keycode == ob_keycode(OB_KEY_LEFT)) {
1299 if ((f = find_active_or_last_menu()) && f->parent)
1300 menu_frame_select(f, NULL);
1301 } else if (ev->xkey.keycode == ob_keycode(OB_KEY_RIGHT)) {
1303 if ((f = find_active_or_last_menu()) && f->child)
1304 menu_frame_select_next(f->child);
1305 } else if (ev->xkey.keycode == ob_keycode(OB_KEY_UP)) {
1307 if ((f = find_active_or_last_menu()))
1308 menu_frame_select_previous(f);
1309 } else if (ev->xkey.keycode == ob_keycode(OB_KEY_DOWN)) {
1311 if ((f = find_active_or_last_menu()))
1312 menu_frame_select_next(f);
1318 static gboolean menu_hide_delay_func(gpointer data)
1320 menu_can_hide = TRUE;
1321 return FALSE; /* no repeat */
1324 static void focus_delay_dest(gpointer data)
1329 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1331 const ObFocusDelayData *f1 = d1;
1332 return f1->client == d2;
1335 static gboolean focus_delay_func(gpointer data)
1337 ObFocusDelayData *d = data;
1338 Time old = event_curtime;
1340 event_curtime = d->time;
1341 if (focus_client != d->client) {
1342 if (client_focus(d->client) && config_focus_raise)
1343 client_raise(d->client);
1345 event_curtime = old;
1346 return FALSE; /* no repeat */
1349 static void focus_delay_client_dest(ObClient *client, gpointer data)
1351 ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1355 void event_halt_focus_delay()
1357 ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1360 void event_ignore_queued_enters()
1362 GSList *saved = NULL, *it;
1365 XSync(ob_display, FALSE);
1367 /* count the events */
1369 e = g_new(XEvent, 1);
1370 if (XCheckTypedEvent(ob_display, EnterNotify, e)) {
1373 win = g_hash_table_lookup(window_map, &e->xany.window);
1374 if (win && WINDOW_IS_CLIENT(win))
1375 ++ignore_enter_focus;
1377 saved = g_slist_append(saved, e);
1383 /* put the events back */
1384 for (it = saved; it; it = g_slist_next(it)) {
1385 XPutBackEvent(ob_display, it->data);
1388 g_slist_free(saved);
1391 gboolean event_time_after(Time t1, Time t2)
1393 g_assert(t1 != CurrentTime);
1394 g_assert(t2 != CurrentTime);
1397 Timestamp values wrap around (after about 49.7 days). The server, given
1398 its current time is represented by timestamp T, always interprets
1399 timestamps from clients by treating half of the timestamp space as being
1400 later in time than T.
1401 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1404 /* TIME_HALF is half of the number space of a Time type variable */
1405 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1407 if (t2 >= TIME_HALF)
1408 /* t2 is in the second half so t1 might wrap around and be smaller than
1410 return t1 >= t2 || t1 < (t2 + TIME_HALF);
1412 /* t2 is in the first half so t1 has to come after it */
1413 return t1 >= t2 && t1 < (t2 + TIME_HALF);