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-2007 Dana 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)
375 /* This means focus had moved to our frame window and now moved off */
376 if (detail == NotifyNonlinear)
384 static Bool look_for_focusin(Display *d, XEvent *e, XPointer arg)
386 return e->type == FocusIn && wanted_focusevent(e);
389 static gboolean event_ignore(XEvent *e, ObClient *client)
393 if (!wanted_focusevent(e))
397 if (!wanted_focusevent(e))
404 static void event_process(const XEvent *ec, gpointer data)
407 ObGroup *group = NULL;
408 ObClient *client = NULL;
410 ObDockApp *dockapp = NULL;
411 ObWindow *obwin = NULL;
413 ObEventData *ed = data;
415 /* make a copy we can mangle */
419 window = event_get_window(e);
420 if (!(e->type == PropertyNotify &&
421 (group = g_hash_table_lookup(group_map, &window))))
422 if ((obwin = g_hash_table_lookup(window_map, &window))) {
423 switch (obwin->type) {
425 dock = WINDOW_AS_DOCK(obwin);
428 dockapp = WINDOW_AS_DOCKAPP(obwin);
431 client = WINDOW_AS_CLIENT(obwin);
434 case Window_Internal:
435 /* not to be used for events */
436 g_assert_not_reached();
441 event_set_curtime(e);
443 if (event_ignore(e, client)) {
450 /* deal with it in the kernel */
452 if (menu_frame_visible &&
453 (e->type == EnterNotify || e->type == LeaveNotify))
455 /* crossing events for menu */
456 event_handle_menu(e);
457 } else if (e->type == FocusIn) {
458 if (client && client != focus_client) {
459 frame_adjust_focus(client->frame, TRUE);
460 focus_set_client(client);
461 client_calc_layer(client);
463 } else if (e->type == FocusOut) {
464 gboolean nomove = FALSE;
467 ob_debug_type(OB_DEBUG_FOCUS, "FocusOut Event\n");
469 /* Look for the followup FocusIn */
470 if (!XCheckIfEvent(ob_display, &ce, look_for_focusin, NULL)) {
471 /* There is no FocusIn, this means focus went to a window that
472 is not being managed, or a window on another screen. */
473 ob_debug_type(OB_DEBUG_FOCUS, "Focus went to a black hole !\n");
474 /* nothing is focused */
475 focus_set_client(NULL);
476 } else if (ce.xany.window == e->xany.window) {
477 ob_debug_type(OB_DEBUG_FOCUS, "Focus didn't go anywhere\n");
478 /* If focus didn't actually move anywhere, there is nothing to do*/
480 } else if (ce.xfocus.detail == NotifyPointerRoot ||
481 ce.xfocus.detail == NotifyDetailNone ||
482 ce.xfocus.detail == NotifyInferior) {
483 ob_debug_type(OB_DEBUG_FOCUS, "Focus went to root\n");
484 /* Focus has been reverted to the root window or nothing
485 FocusOut events come after UnmapNotify, so we don't need to
486 worry about focusing an invalid window
488 focus_fallback(TRUE);
490 /* Focus did move, so process the FocusIn event */
491 ObEventData ed = { .ignored = FALSE };
492 event_process(&ce, &ed);
494 /* The FocusIn was ignored, this means it was on a window
495 that isn't a client. */
496 ob_debug_type(OB_DEBUG_FOCUS,
497 "Focus went to an unmanaged window 0x%x !\n",
499 focus_fallback(TRUE);
503 if (client && !nomove) {
504 frame_adjust_focus(client->frame, FALSE);
505 /* focus_set_client has already been called for sure */
506 client_calc_layer(client);
509 event_handle_group(group, e);
511 event_handle_client(client, e);
513 event_handle_dockapp(dockapp, e);
515 event_handle_dock(dock, e);
516 else if (window == RootWindow(ob_display, ob_screen))
517 event_handle_root(e);
518 else if (e->type == MapRequest)
519 client_manage(window);
520 else if (e->type == ConfigureRequest) {
521 /* unhandled configure requests must be used to configure the
525 xwc.x = e->xconfigurerequest.x;
526 xwc.y = e->xconfigurerequest.y;
527 xwc.width = e->xconfigurerequest.width;
528 xwc.height = e->xconfigurerequest.height;
529 xwc.border_width = e->xconfigurerequest.border_width;
530 xwc.sibling = e->xconfigurerequest.above;
531 xwc.stack_mode = e->xconfigurerequest.detail;
533 /* we are not to be held responsible if someone sends us an
535 xerror_set_ignore(TRUE);
536 XConfigureWindow(ob_display, window,
537 e->xconfigurerequest.value_mask, &xwc);
538 xerror_set_ignore(FALSE);
541 /* user input (action-bound) events */
542 if (e->type == ButtonPress || e->type == ButtonRelease ||
543 e->type == MotionNotify || e->type == KeyPress ||
544 e->type == KeyRelease)
546 if (menu_frame_visible)
547 event_handle_menu(e);
549 if (!keyboard_process_interactive_grab(e, &client)) {
550 if (moveresize_in_progress) {
553 /* make further actions work on the client being
555 client = moveresize_client;
558 menu_can_hide = FALSE;
559 ob_main_loop_timeout_add(ob_main_loop,
560 config_menu_hide_delay * 1000,
561 menu_hide_delay_func,
562 NULL, g_direct_equal, NULL);
564 if (e->type == ButtonPress || e->type == ButtonRelease ||
565 e->type == MotionNotify) {
566 mouse_event(client, e);
567 } else if (e->type == KeyPress) {
568 keyboard_event((focus_cycle_target ? focus_cycle_target :
574 /* if something happens and it's not from an XEvent, then we don't know
576 event_curtime = CurrentTime;
579 static void event_handle_root(XEvent *e)
585 ob_debug("Another WM has requested to replace us. Exiting.\n");
590 if (e->xclient.format != 32) break;
592 msgtype = e->xclient.message_type;
593 if (msgtype == prop_atoms.net_current_desktop) {
594 guint d = e->xclient.data.l[0];
595 if (d < screen_num_desktops) {
596 event_curtime = e->xclient.data.l[1];
597 ob_debug("SWITCH DESKTOP TIME: %d\n", event_curtime);
598 screen_set_desktop(d);
600 } else if (msgtype == prop_atoms.net_number_of_desktops) {
601 guint d = e->xclient.data.l[0];
603 screen_set_num_desktops(d);
604 } else if (msgtype == prop_atoms.net_showing_desktop) {
605 screen_show_desktop(e->xclient.data.l[0] != 0);
606 } else if (msgtype == prop_atoms.ob_control) {
607 if (e->xclient.data.l[0] == 1)
609 else if (e->xclient.data.l[0] == 2)
614 if (e->xproperty.atom == prop_atoms.net_desktop_names)
615 screen_update_desktop_names();
616 else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
617 screen_update_layout();
619 case ConfigureNotify:
621 XRRUpdateConfiguration(e);
630 static void event_handle_group(ObGroup *group, XEvent *e)
634 g_assert(e->type == PropertyNotify);
636 for (it = group->members; it; it = g_slist_next(it))
637 event_handle_client(it->data, e);
640 void event_enter_client(ObClient *client)
642 g_assert(config_focus_follow);
644 if (client_normal(client) && client_can_focus(client)) {
645 if (config_focus_delay) {
646 ObFocusDelayData *data;
648 ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
650 data = g_new(ObFocusDelayData, 1);
651 data->client = client;
652 data->time = event_curtime;
654 ob_main_loop_timeout_add(ob_main_loop,
657 data, focus_delay_cmp, focus_delay_dest);
659 ObFocusDelayData data;
660 data.client = client;
661 data.time = event_curtime;
662 focus_delay_func(&data);
667 static void event_handle_client(ObClient *client, XEvent *e)
677 /* Wheel buttons don't draw because they are an instant click, so it
678 is a waste of resources to go drawing it. */
679 if (!(e->xbutton.button == 4 || e->xbutton.button == 5)) {
680 con = frame_context(client, e->xbutton.window);
681 con = mouse_button_frame_context(con, e->xbutton.button);
683 case OB_FRAME_CONTEXT_MAXIMIZE:
684 client->frame->max_press = (e->type == ButtonPress);
685 framerender_frame(client->frame);
687 case OB_FRAME_CONTEXT_CLOSE:
688 client->frame->close_press = (e->type == ButtonPress);
689 framerender_frame(client->frame);
691 case OB_FRAME_CONTEXT_ICONIFY:
692 client->frame->iconify_press = (e->type == ButtonPress);
693 framerender_frame(client->frame);
695 case OB_FRAME_CONTEXT_ALLDESKTOPS:
696 client->frame->desk_press = (e->type == ButtonPress);
697 framerender_frame(client->frame);
699 case OB_FRAME_CONTEXT_SHADE:
700 client->frame->shade_press = (e->type == ButtonPress);
701 framerender_frame(client->frame);
704 /* nothing changes with clicks for any other contexts */
710 con = frame_context(client, e->xcrossing.window);
712 case OB_FRAME_CONTEXT_MAXIMIZE:
713 client->frame->max_hover = FALSE;
714 frame_adjust_state(client->frame);
716 case OB_FRAME_CONTEXT_ALLDESKTOPS:
717 client->frame->desk_hover = FALSE;
718 frame_adjust_state(client->frame);
720 case OB_FRAME_CONTEXT_SHADE:
721 client->frame->shade_hover = FALSE;
722 frame_adjust_state(client->frame);
724 case OB_FRAME_CONTEXT_ICONIFY:
725 client->frame->iconify_hover = FALSE;
726 frame_adjust_state(client->frame);
728 case OB_FRAME_CONTEXT_CLOSE:
729 client->frame->close_hover = FALSE;
730 frame_adjust_state(client->frame);
732 case OB_FRAME_CONTEXT_FRAME:
733 ob_debug_type(OB_DEBUG_FOCUS,
734 "%sNotify mode %d detail %d on %lx\n",
735 (e->type == EnterNotify ? "Enter" : "Leave"),
737 e->xcrossing.detail, (client?client->window:0));
738 if (keyboard_interactively_grabbed())
740 if (config_focus_follow && config_focus_delay &&
741 /* leaveinferior events can happen when the mouse goes onto the
742 window's border and then into the window before the delay
744 e->xcrossing.detail != NotifyInferior)
746 ob_main_loop_timeout_remove_data(ob_main_loop,
757 gboolean nofocus = FALSE;
759 if (ignore_enter_focus) {
760 ignore_enter_focus--;
764 con = frame_context(client, e->xcrossing.window);
766 case OB_FRAME_CONTEXT_MAXIMIZE:
767 client->frame->max_hover = TRUE;
768 frame_adjust_state(client->frame);
770 case OB_FRAME_CONTEXT_ALLDESKTOPS:
771 client->frame->desk_hover = TRUE;
772 frame_adjust_state(client->frame);
774 case OB_FRAME_CONTEXT_SHADE:
775 client->frame->shade_hover = TRUE;
776 frame_adjust_state(client->frame);
778 case OB_FRAME_CONTEXT_ICONIFY:
779 client->frame->iconify_hover = TRUE;
780 frame_adjust_state(client->frame);
782 case OB_FRAME_CONTEXT_CLOSE:
783 client->frame->close_hover = TRUE;
784 frame_adjust_state(client->frame);
786 case OB_FRAME_CONTEXT_FRAME:
787 if (keyboard_interactively_grabbed())
789 if (e->xcrossing.mode == NotifyGrab ||
790 e->xcrossing.mode == NotifyUngrab)
792 ob_debug_type(OB_DEBUG_FOCUS,
793 "%sNotify mode %d detail %d on %lx IGNORED\n",
794 (e->type == EnterNotify ? "Enter" : "Leave"),
796 e->xcrossing.detail, client?client->window:0);
798 ob_debug_type(OB_DEBUG_FOCUS,
799 "%sNotify mode %d detail %d on %lx, "
800 "focusing window: %d\n",
801 (e->type == EnterNotify ? "Enter" : "Leave"),
803 e->xcrossing.detail, (client?client->window:0),
805 if (!nofocus && config_focus_follow)
806 event_enter_client(client);
814 case ConfigureRequest:
816 while (XCheckTypedWindowEvent(ob_display, client->window,
817 ConfigureRequest, &ce)) {
819 /* XXX if this causes bad things.. we can compress config req's
820 with the same mask. */
821 e->xconfigurerequest.value_mask |=
822 ce.xconfigurerequest.value_mask;
823 if (ce.xconfigurerequest.value_mask & CWX)
824 e->xconfigurerequest.x = ce.xconfigurerequest.x;
825 if (ce.xconfigurerequest.value_mask & CWY)
826 e->xconfigurerequest.y = ce.xconfigurerequest.y;
827 if (ce.xconfigurerequest.value_mask & CWWidth)
828 e->xconfigurerequest.width = ce.xconfigurerequest.width;
829 if (ce.xconfigurerequest.value_mask & CWHeight)
830 e->xconfigurerequest.height = ce.xconfigurerequest.height;
831 if (ce.xconfigurerequest.value_mask & CWBorderWidth)
832 e->xconfigurerequest.border_width =
833 ce.xconfigurerequest.border_width;
834 if (ce.xconfigurerequest.value_mask & CWStackMode)
835 e->xconfigurerequest.detail = ce.xconfigurerequest.detail;
838 /* if we are iconic (or shaded (fvwm does this)) ignore the event */
839 if (client->iconic || client->shaded) return;
841 /* resize, then move, as specified in the EWMH section 7.7 */
842 if (e->xconfigurerequest.value_mask & (CWWidth | CWHeight |
848 if (e->xconfigurerequest.value_mask & CWBorderWidth)
849 client->border_width = e->xconfigurerequest.border_width;
851 x = (e->xconfigurerequest.value_mask & CWX) ?
852 e->xconfigurerequest.x : client->area.x;
853 y = (e->xconfigurerequest.value_mask & CWY) ?
854 e->xconfigurerequest.y : client->area.y;
855 w = (e->xconfigurerequest.value_mask & CWWidth) ?
856 e->xconfigurerequest.width : client->area.width;
857 h = (e->xconfigurerequest.value_mask & CWHeight) ?
858 e->xconfigurerequest.height : client->area.height;
864 client->frame->size.left + client->frame->size.right;
866 client->frame->size.top + client->frame->size.bottom;
867 /* make this rude for size-only changes but not for position
869 gboolean moving = ((e->xconfigurerequest.value_mask & CWX) ||
870 (e->xconfigurerequest.value_mask & CWY));
872 client_find_onscreen(client, &newx, &newy, fw, fh,
874 if (e->xconfigurerequest.value_mask & CWX)
876 if (e->xconfigurerequest.value_mask & CWY)
880 switch (client->gravity) {
881 case NorthEastGravity:
883 corner = OB_CORNER_TOPRIGHT;
885 case SouthWestGravity:
887 corner = OB_CORNER_BOTTOMLEFT;
889 case SouthEastGravity:
890 corner = OB_CORNER_BOTTOMRIGHT;
892 default: /* NorthWest, Static, etc */
893 corner = OB_CORNER_TOPLEFT;
896 client_configure_full(client, corner, x, y, w, h, FALSE, TRUE,
900 if (e->xconfigurerequest.value_mask & CWStackMode) {
901 switch (e->xconfigurerequest.detail) {
904 /* Apps are so rude. And this is totally disconnected from
905 activation/focus. Bleh. */
906 /*client_lower(client);*/
912 /* Apps are so rude. And this is totally disconnected from
913 activation/focus. Bleh. */
914 /*client_raise(client);*/
920 if (client->ignore_unmaps) {
921 client->ignore_unmaps--;
924 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
926 client->window, e->xunmap.event, e->xunmap.from_configure,
927 client->ignore_unmaps);
928 client_unmanage(client);
931 ob_debug("DestroyNotify for window 0x%x\n", client->window);
932 client_unmanage(client);
935 /* this is when the client is first taken captive in the frame */
936 if (e->xreparent.parent == client->frame->plate) break;
939 This event is quite rare and is usually handled in unmapHandler.
940 However, if the window is unmapped when the reparent event occurs,
941 the window manager never sees it because an unmap event is not sent
942 to an already unmapped window.
945 /* we don't want the reparent event, put it back on the stack for the
946 X server to deal with after we unmanage the window */
947 XPutBackEvent(ob_display, e);
949 ob_debug("ReparentNotify for window 0x%x\n", client->window);
950 client_unmanage(client);
953 ob_debug("MapRequest for 0x%lx\n", client->window);
954 if (!client->iconic) break; /* this normally doesn't happen, but if it
955 does, we don't want it!
956 it can happen now when the window is on
957 another desktop, but we still don't
959 client_activate(client, FALSE, TRUE);
962 /* validate cuz we query stuff off the client here */
963 if (!client_validate(client)) break;
965 if (e->xclient.format != 32) return;
967 msgtype = e->xclient.message_type;
968 if (msgtype == prop_atoms.wm_change_state) {
969 /* compress changes into a single change */
970 while (XCheckTypedWindowEvent(ob_display, client->window,
972 /* XXX: it would be nice to compress ALL messages of a
973 type, not just messages in a row without other
974 message types between. */
975 if (ce.xclient.message_type != msgtype) {
976 XPutBackEvent(ob_display, &ce);
979 e->xclient = ce.xclient;
981 client_set_wm_state(client, e->xclient.data.l[0]);
982 } else if (msgtype == prop_atoms.net_wm_desktop) {
983 /* compress changes into a single change */
984 while (XCheckTypedWindowEvent(ob_display, client->window,
986 /* XXX: it would be nice to compress ALL messages of a
987 type, not just messages in a row without other
988 message types between. */
989 if (ce.xclient.message_type != msgtype) {
990 XPutBackEvent(ob_display, &ce);
993 e->xclient = ce.xclient;
995 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
996 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
997 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
999 } else if (msgtype == prop_atoms.net_wm_state) {
1000 /* can't compress these */
1001 ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
1002 (e->xclient.data.l[0] == 0 ? "Remove" :
1003 e->xclient.data.l[0] == 1 ? "Add" :
1004 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1005 e->xclient.data.l[1], e->xclient.data.l[2],
1007 client_set_state(client, e->xclient.data.l[0],
1008 e->xclient.data.l[1], e->xclient.data.l[2]);
1009 } else if (msgtype == prop_atoms.net_close_window) {
1010 ob_debug("net_close_window for 0x%lx\n", client->window);
1011 client_close(client);
1012 } else if (msgtype == prop_atoms.net_active_window) {
1013 ob_debug("net_active_window for 0x%lx source=%s\n",
1015 (e->xclient.data.l[0] == 0 ? "unknown" :
1016 (e->xclient.data.l[0] == 1 ? "application" :
1017 (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1018 /* XXX make use of data.l[2] ! */
1019 event_curtime = e->xclient.data.l[1];
1020 client_activate(client, FALSE,
1021 (e->xclient.data.l[0] == 0 ||
1022 e->xclient.data.l[0] == 2));
1023 } else if (msgtype == prop_atoms.net_wm_moveresize) {
1024 ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1025 client->window, e->xclient.data.l[2]);
1026 if ((Atom)e->xclient.data.l[2] ==
1027 prop_atoms.net_wm_moveresize_size_topleft ||
1028 (Atom)e->xclient.data.l[2] ==
1029 prop_atoms.net_wm_moveresize_size_top ||
1030 (Atom)e->xclient.data.l[2] ==
1031 prop_atoms.net_wm_moveresize_size_topright ||
1032 (Atom)e->xclient.data.l[2] ==
1033 prop_atoms.net_wm_moveresize_size_right ||
1034 (Atom)e->xclient.data.l[2] ==
1035 prop_atoms.net_wm_moveresize_size_right ||
1036 (Atom)e->xclient.data.l[2] ==
1037 prop_atoms.net_wm_moveresize_size_bottomright ||
1038 (Atom)e->xclient.data.l[2] ==
1039 prop_atoms.net_wm_moveresize_size_bottom ||
1040 (Atom)e->xclient.data.l[2] ==
1041 prop_atoms.net_wm_moveresize_size_bottomleft ||
1042 (Atom)e->xclient.data.l[2] ==
1043 prop_atoms.net_wm_moveresize_size_left ||
1044 (Atom)e->xclient.data.l[2] ==
1045 prop_atoms.net_wm_moveresize_move ||
1046 (Atom)e->xclient.data.l[2] ==
1047 prop_atoms.net_wm_moveresize_size_keyboard ||
1048 (Atom)e->xclient.data.l[2] ==
1049 prop_atoms.net_wm_moveresize_move_keyboard) {
1051 moveresize_start(client, e->xclient.data.l[0],
1052 e->xclient.data.l[1], e->xclient.data.l[3],
1053 e->xclient.data.l[2]);
1055 else if ((Atom)e->xclient.data.l[2] ==
1056 prop_atoms.net_wm_moveresize_cancel)
1057 moveresize_end(TRUE);
1058 } else if (msgtype == prop_atoms.net_moveresize_window) {
1059 gint oldg = client->gravity;
1060 gint tmpg, x, y, w, h;
1062 if (e->xclient.data.l[0] & 0xff)
1063 tmpg = e->xclient.data.l[0] & 0xff;
1067 if (e->xclient.data.l[0] & 1 << 8)
1068 x = e->xclient.data.l[1];
1071 if (e->xclient.data.l[0] & 1 << 9)
1072 y = e->xclient.data.l[2];
1075 if (e->xclient.data.l[0] & 1 << 10)
1076 w = e->xclient.data.l[3];
1078 w = client->area.width;
1079 if (e->xclient.data.l[0] & 1 << 11)
1080 h = e->xclient.data.l[4];
1082 h = client->area.height;
1083 client->gravity = tmpg;
1089 client->frame->size.left + client->frame->size.right;
1091 client->frame->size.top + client->frame->size.bottom;
1092 client_find_onscreen(client, &newx, &newy, fw, fh,
1093 client_normal(client));
1094 if (e->xclient.data.l[0] & 1 << 8)
1096 if (e->xclient.data.l[0] & 1 << 9)
1100 client_configure(client, OB_CORNER_TOPLEFT,
1101 x, y, w, h, FALSE, TRUE);
1103 client->gravity = oldg;
1106 case PropertyNotify:
1107 /* validate cuz we query stuff off the client here */
1108 if (!client_validate(client)) break;
1110 /* compress changes to a single property into a single change */
1111 while (XCheckTypedWindowEvent(ob_display, client->window,
1115 /* XXX: it would be nice to compress ALL changes to a property,
1116 not just changes in a row without other props between. */
1118 a = ce.xproperty.atom;
1119 b = e->xproperty.atom;
1123 if ((a == prop_atoms.net_wm_name ||
1124 a == prop_atoms.wm_name ||
1125 a == prop_atoms.net_wm_icon_name ||
1126 a == prop_atoms.wm_icon_name)
1128 (b == prop_atoms.net_wm_name ||
1129 b == prop_atoms.wm_name ||
1130 b == prop_atoms.net_wm_icon_name ||
1131 b == prop_atoms.wm_icon_name)) {
1134 if (a == prop_atoms.net_wm_icon &&
1135 b == prop_atoms.net_wm_icon)
1138 XPutBackEvent(ob_display, &ce);
1142 msgtype = e->xproperty.atom;
1143 if (msgtype == XA_WM_NORMAL_HINTS) {
1144 client_update_normal_hints(client);
1145 /* normal hints can make a window non-resizable */
1146 client_setup_decor_and_functions(client);
1147 } else if (msgtype == XA_WM_HINTS) {
1148 client_update_wmhints(client);
1149 } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1150 client_update_transient_for(client);
1151 client_get_type(client);
1152 /* type may have changed, so update the layer */
1153 client_calc_layer(client);
1154 client_setup_decor_and_functions(client);
1155 } else if (msgtype == prop_atoms.net_wm_name ||
1156 msgtype == prop_atoms.wm_name ||
1157 msgtype == prop_atoms.net_wm_icon_name ||
1158 msgtype == prop_atoms.wm_icon_name) {
1159 client_update_title(client);
1160 } else if (msgtype == prop_atoms.wm_class) {
1161 client_update_class(client);
1162 } else if (msgtype == prop_atoms.wm_protocols) {
1163 client_update_protocols(client);
1164 client_setup_decor_and_functions(client);
1166 else if (msgtype == prop_atoms.net_wm_strut) {
1167 client_update_strut(client);
1169 else if (msgtype == prop_atoms.net_wm_icon) {
1170 client_update_icons(client);
1172 else if (msgtype == prop_atoms.net_wm_user_time) {
1173 client_update_user_time(client);
1175 else if (msgtype == prop_atoms.sm_client_id) {
1176 client_update_sm_client_id(client);
1181 if (extensions_shape && e->type == extensions_shape_event_basep) {
1182 client->shaped = ((XShapeEvent*)e)->shaped;
1183 frame_adjust_shape(client->frame);
1189 static void event_handle_dock(ObDock *s, XEvent *e)
1193 if (e->xbutton.button == 1)
1194 stacking_raise(DOCK_AS_WINDOW(s));
1195 else if (e->xbutton.button == 2)
1196 stacking_lower(DOCK_AS_WINDOW(s));
1207 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1211 dock_app_drag(app, &e->xmotion);
1214 if (app->ignore_unmaps) {
1215 app->ignore_unmaps--;
1218 dock_remove(app, TRUE);
1221 dock_remove(app, FALSE);
1223 case ReparentNotify:
1224 dock_remove(app, FALSE);
1226 case ConfigureNotify:
1227 dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1232 ObMenuFrame* find_active_menu()
1235 ObMenuFrame *ret = NULL;
1237 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1246 ObMenuFrame* find_active_or_last_menu()
1248 ObMenuFrame *ret = NULL;
1250 ret = find_active_menu();
1251 if (!ret && menu_frame_visible)
1252 ret = menu_frame_visible->data;
1256 static void event_handle_menu(XEvent *ev)
1259 ObMenuEntryFrame *e;
1263 if (menu_can_hide) {
1264 if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1265 ev->xbutton.y_root)))
1266 menu_entry_frame_execute(e, ev->xbutton.state,
1269 menu_frame_hide_all();
1273 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1274 if (e->ignore_enters)
1277 menu_frame_select(e->frame, e);
1281 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1282 (f = find_active_menu()) && f->selected == e &&
1283 e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1285 menu_frame_select(e->frame, NULL);
1288 if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1289 ev->xmotion.y_root)))
1290 menu_frame_select(e->frame, e);
1293 if (ev->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
1294 menu_frame_hide_all();
1295 else if (ev->xkey.keycode == ob_keycode(OB_KEY_RETURN)) {
1297 if ((f = find_active_menu()))
1298 menu_entry_frame_execute(f->selected, ev->xkey.state,
1300 } else if (ev->xkey.keycode == ob_keycode(OB_KEY_LEFT)) {
1302 if ((f = find_active_or_last_menu()) && f->parent)
1303 menu_frame_select(f, NULL);
1304 } else if (ev->xkey.keycode == ob_keycode(OB_KEY_RIGHT)) {
1306 if ((f = find_active_or_last_menu()) && f->child)
1307 menu_frame_select_next(f->child);
1308 } else if (ev->xkey.keycode == ob_keycode(OB_KEY_UP)) {
1310 if ((f = find_active_or_last_menu()))
1311 menu_frame_select_previous(f);
1312 } else if (ev->xkey.keycode == ob_keycode(OB_KEY_DOWN)) {
1314 if ((f = find_active_or_last_menu()))
1315 menu_frame_select_next(f);
1321 static gboolean menu_hide_delay_func(gpointer data)
1323 menu_can_hide = TRUE;
1324 return FALSE; /* no repeat */
1327 static void focus_delay_dest(gpointer data)
1332 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1334 const ObFocusDelayData *f1 = d1;
1335 return f1->client == d2;
1338 static gboolean focus_delay_func(gpointer data)
1340 ObFocusDelayData *d = data;
1341 Time old = event_curtime;
1343 event_curtime = d->time;
1344 if (focus_client != d->client) {
1345 if (client_focus(d->client) && config_focus_raise)
1346 client_raise(d->client);
1348 event_curtime = old;
1349 return FALSE; /* no repeat */
1352 static void focus_delay_client_dest(ObClient *client, gpointer data)
1354 ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1358 void event_halt_focus_delay()
1360 ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1363 void event_ignore_queued_enters()
1365 GSList *saved = NULL, *it;
1368 XSync(ob_display, FALSE);
1370 /* count the events */
1372 e = g_new(XEvent, 1);
1373 if (XCheckTypedEvent(ob_display, EnterNotify, e)) {
1376 win = g_hash_table_lookup(window_map, &e->xany.window);
1377 if (win && WINDOW_IS_CLIENT(win))
1378 ++ignore_enter_focus;
1380 saved = g_slist_append(saved, e);
1386 /* put the events back */
1387 for (it = saved; it; it = g_slist_next(it)) {
1388 XPutBackEvent(ob_display, it->data);
1391 g_slist_free(saved);
1394 gboolean event_time_after(Time t1, Time t2)
1396 g_assert(t1 != CurrentTime);
1397 g_assert(t2 != CurrentTime);
1400 Timestamp values wrap around (after about 49.7 days). The server, given
1401 its current time is represented by timestamp T, always interprets
1402 timestamps from clients by treating half of the timestamp space as being
1403 later in time than T.
1404 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1407 /* TIME_HALF is half of the number space of a Time type variable */
1408 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1410 if (t2 >= TIME_HALF)
1411 /* t2 is in the second half so t1 might wrap around and be smaller than
1413 return t1 >= t2 || t1 < (t2 + TIME_HALF);
1415 /* t2 is in the first half so t1 has to come after it */
1416 return t1 >= t2 && t1 < (t2 + TIME_HALF);