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"
37 #include "framerender.h"
39 #include "moveresize.h"
42 #include "extensions.h"
43 #include "translate.h"
46 #include <X11/keysym.h>
47 #include <X11/Xatom.h>
50 #ifdef HAVE_SYS_SELECT_H
51 # include <sys/select.h>
57 # include <unistd.h> /* for usleep() */
60 # include <X11/XKBlib.h>
64 #include <X11/ICE/ICElib.h>
78 static void event_process(const XEvent *e, gpointer data);
79 static void event_handle_root(XEvent *e);
80 static gboolean event_handle_menu_keyboard(XEvent *e);
81 static gboolean event_handle_menu(XEvent *e);
82 static void event_handle_dock(ObDock *s, XEvent *e);
83 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
84 static void event_handle_client(ObClient *c, XEvent *e);
85 static void event_handle_group(ObGroup *g, XEvent *e);
86 static void event_handle_user_input(ObClient *client, XEvent *e);
88 static void focus_delay_dest(gpointer data);
89 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
90 static gboolean focus_delay_func(gpointer data);
91 static void focus_delay_client_dest(ObClient *client, gpointer data);
93 static gboolean menu_hide_delay_func(gpointer data);
95 /* The time for the current event being processed */
96 Time event_curtime = CurrentTime;
98 static guint ignore_enter_focus = 0;
99 static gboolean menu_can_hide;
100 static gboolean focus_left_screen = FALSE;
103 static void ice_handler(gint fd, gpointer conn)
106 IceProcessMessages(conn, NULL, &b);
109 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
110 IcePointer *watch_data)
115 fd = IceConnectionNumber(conn);
116 ob_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
118 ob_main_loop_fd_remove(ob_main_loop, fd);
124 void event_startup(gboolean reconfig)
126 if (reconfig) return;
128 ob_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
131 IceAddConnectionWatch(ice_watch, NULL);
134 client_add_destructor(focus_delay_client_dest, NULL);
137 void event_shutdown(gboolean reconfig)
139 if (reconfig) return;
142 IceRemoveConnectionWatch(ice_watch, NULL);
145 client_remove_destructor(focus_delay_client_dest);
148 static Window event_get_window(XEvent *e)
155 window = RootWindow(ob_display, ob_screen);
158 window = e->xmap.window;
161 window = e->xunmap.window;
164 window = e->xdestroywindow.window;
166 case ConfigureRequest:
167 window = e->xconfigurerequest.window;
169 case ConfigureNotify:
170 window = e->xconfigure.window;
174 if (extensions_xkb && e->type == extensions_xkb_event_basep) {
175 switch (((XkbAnyEvent*)e)->xkb_type) {
177 window = ((XkbBellNotifyEvent*)e)->window;
184 if (extensions_sync &&
185 e->type == extensions_sync_event_basep + XSyncAlarmNotify)
190 window = e->xany.window;
195 static void event_set_curtime(XEvent *e)
197 Time t = CurrentTime;
199 /* grab the lasttime and hack up the state */
215 t = e->xproperty.time;
219 t = e->xcrossing.time;
223 if (extensions_sync &&
224 e->type == extensions_sync_event_basep + XSyncAlarmNotify)
226 t = ((XSyncAlarmNotifyEvent*)e)->time;
229 /* if more event types are anticipated, get their timestamp
237 static void event_hack_mods(XEvent *e)
240 XkbStateRec xkb_state;
246 e->xbutton.state = modkeys_only_modifier_masks(e->xbutton.state);
249 e->xkey.state = modkeys_only_modifier_masks(e->xkey.state);
252 e->xkey.state = modkeys_only_modifier_masks(e->xkey.state);
254 if (XkbGetState(ob_display, XkbUseCoreKbd, &xkb_state) == Success) {
255 e->xkey.state = xkb_state.compat_state;
259 /* remove from the state the mask of the modifier key being released,
260 if it is a modifier key being released that is */
261 e->xkey.state &= ~modkeys_keycode_to_mask(e->xkey.keycode);
264 e->xmotion.state = modkeys_only_modifier_masks(e->xmotion.state);
265 /* compress events */
268 while (XCheckTypedWindowEvent(ob_display, e->xmotion.window,
270 e->xmotion.x_root = ce.xmotion.x_root;
271 e->xmotion.y_root = ce.xmotion.y_root;
278 static gboolean wanted_focusevent(XEvent *e)
280 gint mode = e->xfocus.mode;
281 gint detail = e->xfocus.detail;
282 Window win = e->xany.window;
284 if (e->type == FocusIn) {
286 /* These are ones we never want.. */
288 /* This means focus was given by a keyboard/mouse grab. */
289 if (mode == NotifyGrab)
291 /* This means focus was given back from a keyboard/mouse grab. */
292 if (mode == NotifyUngrab)
295 /* These are the ones we want.. */
297 if (win == RootWindow(ob_display, ob_screen)) {
298 /* This means focus reverted off of a client */
299 if (detail == NotifyPointerRoot || detail == NotifyDetailNone ||
300 detail == NotifyInferior)
306 /* This means focus moved from the root window to a client */
307 if (detail == NotifyVirtual)
309 /* This means focus moved from one client to another */
310 if (detail == NotifyNonlinearVirtual)
312 /* This means focus moved to the frame window */
313 if (detail == NotifyInferior)
319 g_assert(e->type == FocusOut);
322 /* These are ones we never want.. */
324 /* This means focus was taken by a keyboard/mouse grab. */
325 if (mode == NotifyGrab)
328 /* Focus left the root window revertedto state */
329 if (win == RootWindow(ob_display, ob_screen))
332 /* These are the ones we want.. */
334 /* This means focus moved from a client to the root window */
335 if (detail == NotifyVirtual)
337 /* This means focus moved from one client to another */
338 if (detail == NotifyNonlinearVirtual)
340 /* This means focus had moved to our frame window and now moved off */
341 if (detail == NotifyNonlinear)
349 static Bool look_for_focusin(Display *d, XEvent *e, XPointer arg)
351 return e->type == FocusIn && wanted_focusevent(e);
354 static gboolean event_ignore(XEvent *e, ObClient *client)
358 if (!wanted_focusevent(e))
362 if (!wanted_focusevent(e))
369 static void event_process(const XEvent *ec, gpointer data)
372 ObGroup *group = NULL;
373 ObClient *client = NULL;
375 ObDockApp *dockapp = NULL;
376 ObWindow *obwin = NULL;
378 ObEventData *ed = data;
380 /* make a copy we can mangle */
384 window = event_get_window(e);
385 if (!(e->type == PropertyNotify &&
386 (group = g_hash_table_lookup(group_map, &window))))
387 if ((obwin = g_hash_table_lookup(window_map, &window))) {
388 switch (obwin->type) {
390 dock = WINDOW_AS_DOCK(obwin);
393 dockapp = WINDOW_AS_DOCKAPP(obwin);
396 client = WINDOW_AS_CLIENT(obwin);
399 case Window_Internal:
400 /* not to be used for events */
401 g_assert_not_reached();
406 event_set_curtime(e);
408 if (event_ignore(e, client)) {
415 /* deal with it in the kernel */
417 if (menu_frame_visible &&
418 (e->type == EnterNotify || e->type == LeaveNotify))
420 /* crossing events for menu */
421 event_handle_menu(e);
422 } else if (e->type == FocusIn) {
423 if (e->xfocus.detail == NotifyPointerRoot ||
424 e->xfocus.detail == NotifyDetailNone)
426 ob_debug_type(OB_DEBUG_FOCUS, "Focus went to pointer root/none\n");
427 /* Focus has been reverted to the root window or nothing.
428 FocusOut events come after UnmapNotify, so we don't need to
429 worry about focusing an invalid window
431 if (!focus_left_screen)
432 focus_fallback(TRUE);
433 } else if (e->xfocus.detail == NotifyInferior) {
434 ob_debug_type(OB_DEBUG_FOCUS,
435 "Focus went to root or our frame window");
436 /* Focus has been given to the root window. */
437 focus_fallback(TRUE);
438 } else if (client && client != focus_client) {
439 focus_left_screen = FALSE;
440 frame_adjust_focus(client->frame, TRUE);
441 focus_set_client(client);
442 client_calc_layer(client);
444 } else if (e->type == FocusOut) {
445 gboolean nomove = FALSE;
448 ob_debug_type(OB_DEBUG_FOCUS, "FocusOut Event\n");
450 /* Look for the followup FocusIn */
451 if (!XCheckIfEvent(ob_display, &ce, look_for_focusin, NULL)) {
452 /* There is no FocusIn, this means focus went to a window that
453 is not being managed, or a window on another screen. */
457 xerror_set_ignore(TRUE);
458 if (XGetInputFocus(ob_display, &win, &i) != 0 &&
459 XGetGeometry(ob_display, win, &root, &i,&i,&u,&u,&u,&u) != 0 &&
460 root != RootWindow(ob_display, ob_screen))
462 ob_debug_type(OB_DEBUG_FOCUS,
463 "Focus went to another screen !\n");
464 focus_left_screen = TRUE;
467 ob_debug_type(OB_DEBUG_FOCUS,
468 "Focus went to a black hole !\n");
469 xerror_set_ignore(FALSE);
470 /* nothing is focused */
471 focus_set_client(NULL);
472 } else if (ce.xany.window == e->xany.window) {
473 ob_debug_type(OB_DEBUG_FOCUS, "Focus didn't go anywhere\n");
474 /* If focus didn't actually move anywhere, there is nothing to do*/
477 /* Focus did move, so process the FocusIn event */
478 ObEventData ed = { .ignored = FALSE };
479 event_process(&ce, &ed);
481 /* The FocusIn was ignored, this means it was on a window
482 that isn't a client. */
483 ob_debug_type(OB_DEBUG_FOCUS,
484 "Focus went to an unmanaged window 0x%x !\n",
486 focus_fallback(TRUE);
490 if (client && !nomove) {
491 frame_adjust_focus(client->frame, FALSE);
492 /* focus_set_client has already been called for sure */
493 client_calc_layer(client);
496 event_handle_group(group, e);
498 event_handle_client(client, e);
500 event_handle_dockapp(dockapp, e);
502 event_handle_dock(dock, e);
503 else if (window == RootWindow(ob_display, ob_screen))
504 event_handle_root(e);
505 else if (e->type == MapRequest)
506 client_manage(window);
507 else if (e->type == ConfigureRequest) {
508 /* unhandled configure requests must be used to configure the
512 xwc.x = e->xconfigurerequest.x;
513 xwc.y = e->xconfigurerequest.y;
514 xwc.width = e->xconfigurerequest.width;
515 xwc.height = e->xconfigurerequest.height;
516 xwc.border_width = e->xconfigurerequest.border_width;
517 xwc.sibling = e->xconfigurerequest.above;
518 xwc.stack_mode = e->xconfigurerequest.detail;
520 /* we are not to be held responsible if someone sends us an
522 xerror_set_ignore(TRUE);
523 XConfigureWindow(ob_display, window,
524 e->xconfigurerequest.value_mask, &xwc);
525 xerror_set_ignore(FALSE);
528 else if (extensions_sync &&
529 e->type == extensions_sync_event_basep + XSyncAlarmNotify)
531 XSyncAlarmNotifyEvent *se = (XSyncAlarmNotifyEvent*)e;
532 if (se->alarm == moveresize_alarm && moveresize_in_progress)
537 if (e->type == ButtonPress || e->type == ButtonRelease ||
538 e->type == MotionNotify || e->type == KeyPress ||
539 e->type == KeyRelease)
541 event_handle_user_input(client, e);
544 /* if something happens and it's not from an XEvent, then we don't know
546 event_curtime = CurrentTime;
549 static void event_handle_root(XEvent *e)
555 ob_debug("Another WM has requested to replace us. Exiting.\n");
560 if (e->xclient.format != 32) break;
562 msgtype = e->xclient.message_type;
563 if (msgtype == prop_atoms.net_current_desktop) {
564 guint d = e->xclient.data.l[0];
565 if (d < screen_num_desktops) {
566 event_curtime = e->xclient.data.l[1];
567 ob_debug("SWITCH DESKTOP TIME: %d\n", event_curtime);
568 screen_set_desktop(d);
570 } else if (msgtype == prop_atoms.net_number_of_desktops) {
571 guint d = e->xclient.data.l[0];
573 screen_set_num_desktops(d);
574 } else if (msgtype == prop_atoms.net_showing_desktop) {
575 screen_show_desktop(e->xclient.data.l[0] != 0, TRUE);
576 } else if (msgtype == prop_atoms.openbox_control) {
577 if (e->xclient.data.l[0] == 1)
579 else if (e->xclient.data.l[0] == 2)
584 if (e->xproperty.atom == prop_atoms.net_desktop_names)
585 screen_update_desktop_names();
586 else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
587 screen_update_layout();
589 case ConfigureNotify:
591 XRRUpdateConfiguration(e);
600 static void event_handle_group(ObGroup *group, XEvent *e)
604 g_assert(e->type == PropertyNotify);
606 for (it = group->members; it; it = g_slist_next(it))
607 event_handle_client(it->data, e);
610 void event_enter_client(ObClient *client)
612 g_assert(config_focus_follow);
614 if (client_normal(client) && client_can_focus(client)) {
615 if (config_focus_delay) {
616 ObFocusDelayData *data;
618 ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
620 data = g_new(ObFocusDelayData, 1);
621 data->client = client;
622 data->time = event_curtime;
624 ob_main_loop_timeout_add(ob_main_loop,
627 data, focus_delay_cmp, focus_delay_dest);
629 ObFocusDelayData data;
630 data.client = client;
631 data.time = event_curtime;
632 focus_delay_func(&data);
637 static void event_handle_client(ObClient *client, XEvent *e)
647 /* Wheel buttons don't draw because they are an instant click, so it
648 is a waste of resources to go drawing it. */
649 if (!(e->xbutton.button == 4 || e->xbutton.button == 5)) {
650 con = frame_context(client, e->xbutton.window);
651 con = mouse_button_frame_context(con, e->xbutton.button);
653 case OB_FRAME_CONTEXT_MAXIMIZE:
654 client->frame->max_press = (e->type == ButtonPress);
655 framerender_frame(client->frame);
657 case OB_FRAME_CONTEXT_CLOSE:
658 client->frame->close_press = (e->type == ButtonPress);
659 framerender_frame(client->frame);
661 case OB_FRAME_CONTEXT_ICONIFY:
662 client->frame->iconify_press = (e->type == ButtonPress);
663 framerender_frame(client->frame);
665 case OB_FRAME_CONTEXT_ALLDESKTOPS:
666 client->frame->desk_press = (e->type == ButtonPress);
667 framerender_frame(client->frame);
669 case OB_FRAME_CONTEXT_SHADE:
670 client->frame->shade_press = (e->type == ButtonPress);
671 framerender_frame(client->frame);
674 /* nothing changes with clicks for any other contexts */
680 con = frame_context(client, e->xcrossing.window);
682 case OB_FRAME_CONTEXT_MAXIMIZE:
683 client->frame->max_hover = FALSE;
684 frame_adjust_state(client->frame);
686 case OB_FRAME_CONTEXT_ALLDESKTOPS:
687 client->frame->desk_hover = FALSE;
688 frame_adjust_state(client->frame);
690 case OB_FRAME_CONTEXT_SHADE:
691 client->frame->shade_hover = FALSE;
692 frame_adjust_state(client->frame);
694 case OB_FRAME_CONTEXT_ICONIFY:
695 client->frame->iconify_hover = FALSE;
696 frame_adjust_state(client->frame);
698 case OB_FRAME_CONTEXT_CLOSE:
699 client->frame->close_hover = FALSE;
700 frame_adjust_state(client->frame);
702 case OB_FRAME_CONTEXT_FRAME:
703 /* When the mouse leaves an animating window, don't use the
704 corresponding enter events. Pretend like the animating window
705 doesn't even exist..! */
706 if (frame_iconify_animating(client->frame))
707 event_ignore_queued_enters();
709 ob_debug_type(OB_DEBUG_FOCUS,
710 "%sNotify mode %d detail %d on %lx\n",
711 (e->type == EnterNotify ? "Enter" : "Leave"),
713 e->xcrossing.detail, (client?client->window:0));
714 if (keyboard_interactively_grabbed())
716 if (config_focus_follow && config_focus_delay &&
717 /* leave inferior events can happen when the mouse goes onto
718 the window's border and then into the window before the
720 e->xcrossing.detail != NotifyInferior)
722 ob_main_loop_timeout_remove_data(ob_main_loop,
733 gboolean nofocus = FALSE;
735 if (ignore_enter_focus) {
736 ignore_enter_focus--;
740 con = frame_context(client, e->xcrossing.window);
742 case OB_FRAME_CONTEXT_MAXIMIZE:
743 client->frame->max_hover = TRUE;
744 frame_adjust_state(client->frame);
746 case OB_FRAME_CONTEXT_ALLDESKTOPS:
747 client->frame->desk_hover = TRUE;
748 frame_adjust_state(client->frame);
750 case OB_FRAME_CONTEXT_SHADE:
751 client->frame->shade_hover = TRUE;
752 frame_adjust_state(client->frame);
754 case OB_FRAME_CONTEXT_ICONIFY:
755 client->frame->iconify_hover = TRUE;
756 frame_adjust_state(client->frame);
758 case OB_FRAME_CONTEXT_CLOSE:
759 client->frame->close_hover = TRUE;
760 frame_adjust_state(client->frame);
762 case OB_FRAME_CONTEXT_FRAME:
763 if (keyboard_interactively_grabbed())
765 if (e->xcrossing.mode == NotifyGrab ||
766 e->xcrossing.mode == NotifyUngrab ||
767 /*ignore enters when we're already in the window */
768 e->xcrossing.detail == NotifyInferior)
770 ob_debug_type(OB_DEBUG_FOCUS,
771 "%sNotify mode %d detail %d on %lx IGNORED\n",
772 (e->type == EnterNotify ? "Enter" : "Leave"),
774 e->xcrossing.detail, client?client->window:0);
776 ob_debug_type(OB_DEBUG_FOCUS,
777 "%sNotify mode %d detail %d on %lx, "
778 "focusing window: %d\n",
779 (e->type == EnterNotify ? "Enter" : "Leave"),
781 e->xcrossing.detail, (client?client->window:0),
783 if (!nofocus && config_focus_follow)
784 event_enter_client(client);
792 case ConfigureRequest:
794 while (XCheckTypedWindowEvent(ob_display, client->window,
795 ConfigureRequest, &ce)) {
797 /* XXX if this causes bad things.. we can compress config req's
798 with the same mask. */
799 e->xconfigurerequest.value_mask |=
800 ce.xconfigurerequest.value_mask;
801 if (ce.xconfigurerequest.value_mask & CWX)
802 e->xconfigurerequest.x = ce.xconfigurerequest.x;
803 if (ce.xconfigurerequest.value_mask & CWY)
804 e->xconfigurerequest.y = ce.xconfigurerequest.y;
805 if (ce.xconfigurerequest.value_mask & CWWidth)
806 e->xconfigurerequest.width = ce.xconfigurerequest.width;
807 if (ce.xconfigurerequest.value_mask & CWHeight)
808 e->xconfigurerequest.height = ce.xconfigurerequest.height;
809 if (ce.xconfigurerequest.value_mask & CWBorderWidth)
810 e->xconfigurerequest.border_width =
811 ce.xconfigurerequest.border_width;
812 if (ce.xconfigurerequest.value_mask & CWStackMode)
813 e->xconfigurerequest.detail = ce.xconfigurerequest.detail;
816 ob_debug("ConfigureRequest desktop %d wmstate %d vis %d\n",
817 screen_desktop, client->wmstate, client->frame->visible);
819 /* If the client is in IconicState then ignore the event.
820 This used to only ignore iconic or shaded windows, but windows on
821 other desktops are also in IconicState, so now those can't
822 send ConfigureRequests either..
823 This fixes the bug of KDE apps moving when they try to active them-
824 selves on another desktop.
825 It used to say "fvwm does this" but I'm not sure if fvwm does
826 this for windows on other desktops too. Probably, it makes sense.
828 if (client->wmstate == IconicState) return;
830 /* resize, then move, as specified in the EWMH section 7.7 */
831 if (e->xconfigurerequest.value_mask & (CWWidth | CWHeight |
836 if (e->xconfigurerequest.value_mask & CWBorderWidth)
837 client->border_width = e->xconfigurerequest.border_width;
839 x = (e->xconfigurerequest.value_mask & CWX) ?
840 e->xconfigurerequest.x : client->area.x;
841 y = (e->xconfigurerequest.value_mask & CWY) ?
842 e->xconfigurerequest.y : client->area.y;
843 w = (e->xconfigurerequest.value_mask & CWWidth) ?
844 e->xconfigurerequest.width : client->area.width;
845 h = (e->xconfigurerequest.value_mask & CWHeight) ?
846 e->xconfigurerequest.height : client->area.height;
848 ob_debug("ConfigureRequest x %d %d y %d %d\n",
849 e->xconfigurerequest.value_mask & CWX, x,
850 e->xconfigurerequest.value_mask & CWY, y);
852 client_find_onscreen(client, &x, &y, w, h, FALSE);
853 client_configure_full(client, x, y, w, h, FALSE, TRUE, TRUE);
856 if (e->xconfigurerequest.value_mask & CWStackMode) {
857 switch (e->xconfigurerequest.detail) {
860 /* Apps are so rude. And this is totally disconnected from
861 activation/focus. Bleh. */
862 /*client_lower(client);*/
868 /* Apps are so rude. And this is totally disconnected from
869 activation/focus. Bleh. */
870 /*client_raise(client);*/
876 if (client->ignore_unmaps) {
877 client->ignore_unmaps--;
880 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
882 client->window, e->xunmap.event, e->xunmap.from_configure,
883 client->ignore_unmaps);
884 client_unmanage(client);
887 ob_debug("DestroyNotify for window 0x%x\n", client->window);
888 client_unmanage(client);
891 /* this is when the client is first taken captive in the frame */
892 if (e->xreparent.parent == client->frame->plate) break;
895 This event is quite rare and is usually handled in unmapHandler.
896 However, if the window is unmapped when the reparent event occurs,
897 the window manager never sees it because an unmap event is not sent
898 to an already unmapped window.
901 /* we don't want the reparent event, put it back on the stack for the
902 X server to deal with after we unmanage the window */
903 XPutBackEvent(ob_display, e);
905 ob_debug("ReparentNotify for window 0x%x\n", client->window);
906 client_unmanage(client);
909 ob_debug("MapRequest for 0x%lx\n", client->window);
910 if (!client->iconic) break; /* this normally doesn't happen, but if it
911 does, we don't want it!
912 it can happen now when the window is on
913 another desktop, but we still don't
915 client_activate(client, FALSE, TRUE);
918 /* validate cuz we query stuff off the client here */
919 if (!client_validate(client)) break;
921 if (e->xclient.format != 32) return;
923 msgtype = e->xclient.message_type;
924 if (msgtype == prop_atoms.wm_change_state) {
925 /* compress changes into a single change */
926 while (XCheckTypedWindowEvent(ob_display, client->window,
928 /* XXX: it would be nice to compress ALL messages of a
929 type, not just messages in a row without other
930 message types between. */
931 if (ce.xclient.message_type != msgtype) {
932 XPutBackEvent(ob_display, &ce);
935 e->xclient = ce.xclient;
937 client_set_wm_state(client, e->xclient.data.l[0]);
938 } else if (msgtype == prop_atoms.net_wm_desktop) {
939 /* compress changes into a single change */
940 while (XCheckTypedWindowEvent(ob_display, client->window,
942 /* XXX: it would be nice to compress ALL messages of a
943 type, not just messages in a row without other
944 message types between. */
945 if (ce.xclient.message_type != msgtype) {
946 XPutBackEvent(ob_display, &ce);
949 e->xclient = ce.xclient;
951 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
952 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
953 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
955 } else if (msgtype == prop_atoms.net_wm_state) {
956 /* can't compress these */
957 ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
958 (e->xclient.data.l[0] == 0 ? "Remove" :
959 e->xclient.data.l[0] == 1 ? "Add" :
960 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
961 e->xclient.data.l[1], e->xclient.data.l[2],
963 client_set_state(client, e->xclient.data.l[0],
964 e->xclient.data.l[1], e->xclient.data.l[2]);
965 } else if (msgtype == prop_atoms.net_close_window) {
966 ob_debug("net_close_window for 0x%lx\n", client->window);
967 client_close(client);
968 } else if (msgtype == prop_atoms.net_active_window) {
969 ob_debug("net_active_window for 0x%lx source=%s\n",
971 (e->xclient.data.l[0] == 0 ? "unknown" :
972 (e->xclient.data.l[0] == 1 ? "application" :
973 (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
974 /* XXX make use of data.l[2] ! */
975 event_curtime = e->xclient.data.l[1];
976 client_activate(client, FALSE,
977 (e->xclient.data.l[0] == 0 ||
978 e->xclient.data.l[0] == 2));
979 } else if (msgtype == prop_atoms.net_wm_moveresize) {
980 ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
981 client->window, e->xclient.data.l[2]);
982 if ((Atom)e->xclient.data.l[2] ==
983 prop_atoms.net_wm_moveresize_size_topleft ||
984 (Atom)e->xclient.data.l[2] ==
985 prop_atoms.net_wm_moveresize_size_top ||
986 (Atom)e->xclient.data.l[2] ==
987 prop_atoms.net_wm_moveresize_size_topright ||
988 (Atom)e->xclient.data.l[2] ==
989 prop_atoms.net_wm_moveresize_size_right ||
990 (Atom)e->xclient.data.l[2] ==
991 prop_atoms.net_wm_moveresize_size_right ||
992 (Atom)e->xclient.data.l[2] ==
993 prop_atoms.net_wm_moveresize_size_bottomright ||
994 (Atom)e->xclient.data.l[2] ==
995 prop_atoms.net_wm_moveresize_size_bottom ||
996 (Atom)e->xclient.data.l[2] ==
997 prop_atoms.net_wm_moveresize_size_bottomleft ||
998 (Atom)e->xclient.data.l[2] ==
999 prop_atoms.net_wm_moveresize_size_left ||
1000 (Atom)e->xclient.data.l[2] ==
1001 prop_atoms.net_wm_moveresize_move ||
1002 (Atom)e->xclient.data.l[2] ==
1003 prop_atoms.net_wm_moveresize_size_keyboard ||
1004 (Atom)e->xclient.data.l[2] ==
1005 prop_atoms.net_wm_moveresize_move_keyboard) {
1007 moveresize_start(client, e->xclient.data.l[0],
1008 e->xclient.data.l[1], e->xclient.data.l[3],
1009 e->xclient.data.l[2]);
1011 else if ((Atom)e->xclient.data.l[2] ==
1012 prop_atoms.net_wm_moveresize_cancel)
1013 moveresize_end(TRUE);
1014 } else if (msgtype == prop_atoms.net_moveresize_window) {
1015 gint grav, x, y, w, h;
1017 if (e->xclient.data.l[0] & 0xff)
1018 grav = e->xclient.data.l[0] & 0xff;
1020 grav = client->gravity;
1022 if (e->xclient.data.l[0] & 1 << 8)
1023 x = e->xclient.data.l[1];
1026 if (e->xclient.data.l[0] & 1 << 9)
1027 y = e->xclient.data.l[2];
1030 if (e->xclient.data.l[0] & 1 << 10)
1031 w = e->xclient.data.l[3];
1033 w = client->area.width;
1034 if (e->xclient.data.l[0] & 1 << 11)
1035 h = e->xclient.data.l[4];
1037 h = client->area.height;
1039 ob_debug("MOVERESIZE x %d %d y %d %d\n",
1040 e->xclient.data.l[0] & 1 << 8, x,
1041 e->xclient.data.l[0] & 1 << 9, y);
1042 client_convert_gravity(client, grav, &x, &y, w, h);
1043 client_find_onscreen(client, &x, &y, w, h, FALSE);
1044 client_configure(client, x, y, w, h, FALSE, TRUE);
1047 case PropertyNotify:
1048 /* validate cuz we query stuff off the client here */
1049 if (!client_validate(client)) break;
1051 /* compress changes to a single property into a single change */
1052 while (XCheckTypedWindowEvent(ob_display, client->window,
1056 /* XXX: it would be nice to compress ALL changes to a property,
1057 not just changes in a row without other props between. */
1059 a = ce.xproperty.atom;
1060 b = e->xproperty.atom;
1064 if ((a == prop_atoms.net_wm_name ||
1065 a == prop_atoms.wm_name ||
1066 a == prop_atoms.net_wm_icon_name ||
1067 a == prop_atoms.wm_icon_name)
1069 (b == prop_atoms.net_wm_name ||
1070 b == prop_atoms.wm_name ||
1071 b == prop_atoms.net_wm_icon_name ||
1072 b == prop_atoms.wm_icon_name)) {
1075 if (a == prop_atoms.net_wm_icon &&
1076 b == prop_atoms.net_wm_icon)
1079 XPutBackEvent(ob_display, &ce);
1083 msgtype = e->xproperty.atom;
1084 if (msgtype == XA_WM_NORMAL_HINTS) {
1085 client_update_normal_hints(client);
1086 /* normal hints can make a window non-resizable */
1087 client_setup_decor_and_functions(client);
1088 } else if (msgtype == XA_WM_HINTS) {
1089 client_update_wmhints(client);
1090 } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1091 client_update_transient_for(client);
1092 client_get_type(client);
1093 /* type may have changed, so update the layer */
1094 client_calc_layer(client);
1095 client_setup_decor_and_functions(client);
1096 } else if (msgtype == prop_atoms.net_wm_name ||
1097 msgtype == prop_atoms.wm_name ||
1098 msgtype == prop_atoms.net_wm_icon_name ||
1099 msgtype == prop_atoms.wm_icon_name) {
1100 client_update_title(client);
1101 } else if (msgtype == prop_atoms.wm_class) {
1102 client_update_class(client);
1103 } else if (msgtype == prop_atoms.wm_protocols) {
1104 client_update_protocols(client);
1105 client_setup_decor_and_functions(client);
1107 else if (msgtype == prop_atoms.net_wm_strut) {
1108 client_update_strut(client);
1110 else if (msgtype == prop_atoms.net_wm_icon) {
1111 client_update_icons(client);
1113 else if (msgtype == prop_atoms.net_wm_icon_geometry) {
1114 client_update_icon_geometry(client);
1116 else if (msgtype == prop_atoms.net_wm_user_time) {
1117 client_update_user_time(client);
1120 else if (msgtype == prop_atoms.net_wm_sync_request_counter) {
1121 client_update_sync_request_counter(client);
1124 else if (msgtype == prop_atoms.sm_client_id) {
1125 client_update_sm_client_id(client);
1127 case ColormapNotify:
1128 client_update_colormap(client, e->xcolormap.colormap);
1133 if (extensions_shape && e->type == extensions_shape_event_basep) {
1134 client->shaped = ((XShapeEvent*)e)->shaped;
1135 frame_adjust_shape(client->frame);
1141 static void event_handle_dock(ObDock *s, XEvent *e)
1145 if (e->xbutton.button == 1)
1146 stacking_raise(DOCK_AS_WINDOW(s));
1147 else if (e->xbutton.button == 2)
1148 stacking_lower(DOCK_AS_WINDOW(s));
1159 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1163 dock_app_drag(app, &e->xmotion);
1166 if (app->ignore_unmaps) {
1167 app->ignore_unmaps--;
1170 dock_remove(app, TRUE);
1173 dock_remove(app, FALSE);
1175 case ReparentNotify:
1176 dock_remove(app, FALSE);
1178 case ConfigureNotify:
1179 dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1184 static ObMenuFrame* find_active_menu()
1187 ObMenuFrame *ret = NULL;
1189 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1198 static ObMenuFrame* find_active_or_last_menu()
1200 ObMenuFrame *ret = NULL;
1202 ret = find_active_menu();
1203 if (!ret && menu_frame_visible)
1204 ret = menu_frame_visible->data;
1208 static gboolean event_handle_menu_keyboard(XEvent *ev)
1210 guint keycode, state;
1213 gboolean ret = TRUE;
1215 keycode = ev->xkey.keycode;
1216 state = ev->xkey.state;
1217 unikey = translate_unichar(keycode);
1219 frame = find_active_or_last_menu();
1223 else if (keycode == ob_keycode(OB_KEY_ESCAPE) && state == 0) {
1224 /* Escape closes the active menu */
1225 menu_frame_hide(frame);
1228 else if (keycode == ob_keycode(OB_KEY_RETURN) && (state == 0 ||
1229 state == ControlMask))
1231 /* Enter runs the active item or goes into the submenu.
1232 Control-Enter runs it without closing the menu. */
1234 menu_frame_select_next(frame->child);
1236 menu_entry_frame_execute(frame->selected, state, ev->xkey.time);
1239 else if (keycode == ob_keycode(OB_KEY_LEFT) && ev->xkey.state == 0) {
1240 /* Left goes to the parent menu */
1241 menu_frame_select(frame, NULL, TRUE);
1244 else if (keycode == ob_keycode(OB_KEY_RIGHT) && ev->xkey.state == 0) {
1245 /* Right goes to the selected submenu */
1246 if (frame->child) menu_frame_select_next(frame->child);
1249 else if (keycode == ob_keycode(OB_KEY_UP) && state == 0) {
1250 menu_frame_select_previous(frame);
1253 else if (keycode == ob_keycode(OB_KEY_DOWN) && state == 0) {
1254 menu_frame_select_next(frame);
1257 /* keyboard accelerator shortcuts. */
1258 else if (ev->xkey.state == 0 &&
1259 /* was it a valid key? */
1261 /* don't bother if the menu is empty. */
1266 ObMenuEntryFrame *found = NULL;
1267 guint num_found = 0;
1269 /* start after the selected one */
1270 start = frame->entries;
1271 if (frame->selected) {
1272 for (it = start; frame->selected != it->data; it = g_list_next(it))
1273 g_assert(it != NULL); /* nothing was selected? */
1274 /* next with wraparound */
1275 start = g_list_next(it);
1276 if (start == NULL) start = frame->entries;
1281 ObMenuEntryFrame *e = it->data;
1282 gunichar entrykey = 0;
1284 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1285 e->entry->data.normal.enabled)
1286 entrykey = e->entry->data.normal.shortcut;
1287 else if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1288 entrykey = e->entry->data.submenu.submenu->shortcut;
1290 if (unikey == entrykey) {
1291 if (found == NULL) found = e;
1295 /* next with wraparound */
1296 it = g_list_next(it);
1297 if (it == NULL) it = frame->entries;
1298 } while (it != start);
1301 if (found->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1304 menu_frame_select(frame, found, TRUE);
1306 menu_entry_frame_execute(found, state, ev->xkey.time);
1308 menu_frame_select(frame, found, TRUE);
1310 menu_frame_select_next(frame->child);
1321 static gboolean event_handle_menu(XEvent *ev)
1324 ObMenuEntryFrame *e;
1325 gboolean ret = TRUE;
1329 if ((ev->xbutton.button < 4 || ev->xbutton.button > 5)
1332 if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1333 ev->xbutton.y_root)))
1334 menu_entry_frame_execute(e, ev->xbutton.state,
1337 menu_frame_hide_all();
1341 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1342 if (e->ignore_enters)
1345 menu_frame_select(e->frame, e, FALSE);
1349 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1350 (f = find_active_menu()) && f->selected == e &&
1351 e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1353 menu_frame_select(e->frame, NULL, FALSE);
1356 if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1357 ev->xmotion.y_root)))
1358 menu_frame_select(e->frame, e, FALSE);
1361 ret = event_handle_menu_keyboard(ev);
1367 static void event_handle_user_input(ObClient *client, XEvent *e)
1369 g_assert(e->type == ButtonPress || e->type == ButtonRelease ||
1370 e->type == MotionNotify || e->type == KeyPress ||
1371 e->type == KeyRelease);
1373 if (menu_frame_visible) {
1374 if (event_handle_menu(e))
1375 /* don't use the event if the menu used it, but if the menu
1376 didn't use it and it's a keypress that is bound, it will
1377 close the menu and be used */
1381 /* if the keyboard interactive action uses the event then dont
1382 use it for bindings. likewise is moveresize uses the event. */
1383 if (!keyboard_process_interactive_grab(e, &client) &&
1384 !(moveresize_in_progress && moveresize_event(e)))
1386 if (moveresize_in_progress)
1387 /* make further actions work on the client being
1389 client = moveresize_client;
1391 menu_can_hide = FALSE;
1392 ob_main_loop_timeout_add(ob_main_loop,
1393 config_menu_hide_delay * 1000,
1394 menu_hide_delay_func,
1395 NULL, g_direct_equal, NULL);
1397 if (e->type == ButtonPress ||
1398 e->type == ButtonRelease ||
1399 e->type == MotionNotify)
1401 /* the frame may not be "visible" but they can still click on it
1402 in the case where it is animating before disappearing */
1403 if (!client || !frame_iconify_animating(client->frame))
1404 mouse_event(client, e);
1405 } else if (e->type == KeyPress) {
1406 keyboard_event((focus_cycle_target ? focus_cycle_target :
1407 (client ? client : focus_client)), e);
1412 static gboolean menu_hide_delay_func(gpointer data)
1414 menu_can_hide = TRUE;
1415 return FALSE; /* no repeat */
1418 static void focus_delay_dest(gpointer data)
1423 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1425 const ObFocusDelayData *f1 = d1;
1426 return f1->client == d2;
1429 static gboolean focus_delay_func(gpointer data)
1431 ObFocusDelayData *d = data;
1432 Time old = event_curtime;
1434 event_curtime = d->time;
1435 if (focus_client != d->client) {
1436 if (client_focus(d->client) && config_focus_raise)
1437 client_raise(d->client);
1439 event_curtime = old;
1440 return FALSE; /* no repeat */
1443 static void focus_delay_client_dest(ObClient *client, gpointer data)
1445 ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1449 void event_halt_focus_delay()
1451 ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1454 void event_ignore_queued_enters()
1456 GSList *saved = NULL, *it;
1459 XSync(ob_display, FALSE);
1461 /* count the events */
1463 e = g_new(XEvent, 1);
1464 if (XCheckTypedEvent(ob_display, EnterNotify, e)) {
1467 win = g_hash_table_lookup(window_map, &e->xany.window);
1468 if (win && WINDOW_IS_CLIENT(win))
1469 ++ignore_enter_focus;
1471 saved = g_slist_append(saved, e);
1477 /* put the events back */
1478 for (it = saved; it; it = g_slist_next(it)) {
1479 XPutBackEvent(ob_display, it->data);
1482 g_slist_free(saved);
1485 gboolean event_time_after(Time t1, Time t2)
1487 g_assert(t1 != CurrentTime);
1488 g_assert(t2 != CurrentTime);
1491 Timestamp values wrap around (after about 49.7 days). The server, given
1492 its current time is represented by timestamp T, always interprets
1493 timestamps from clients by treating half of the timestamp space as being
1494 later in time than T.
1495 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1498 /* TIME_HALF is half of the number space of a Time type variable */
1499 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1501 if (t2 >= TIME_HALF)
1502 /* t2 is in the second half so t1 might wrap around and be smaller than
1504 return t1 >= t2 || t1 < (t2 + TIME_HALF);
1506 /* t2 is in the first half so t1 has to come after it */
1507 return t1 >= t2 && t1 < (t2 + TIME_HALF);