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 "focus_cycle.h"
37 #include "moveresize.h"
41 #include "obt/display.h"
43 #include "obt/keyboard.h"
46 #include <X11/Xatom.h>
49 #ifdef HAVE_SYS_SELECT_H
50 # include <sys/select.h>
56 # include <unistd.h> /* for usleep() */
59 # include <X11/XKBlib.h>
63 #include <X11/ICE/ICElib.h>
80 gulong start; /* inclusive */
81 gulong end; /* inclusive */
84 static void event_process(const XEvent *e, gpointer data);
85 static void event_handle_root(XEvent *e);
86 static gboolean event_handle_menu_input(XEvent *e);
87 static void event_handle_menu(ObMenuFrame *frame, XEvent *e);
88 static void event_handle_dock(ObDock *s, XEvent *e);
89 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
90 static void event_handle_client(ObClient *c, XEvent *e);
91 static void event_handle_user_input(ObClient *client, XEvent *e);
92 static gboolean is_enter_focus_event_ignored(XEvent *e);
93 static void event_ignore_enter_range(gulong start, gulong end);
95 static void focus_delay_dest(gpointer data);
96 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
97 static gboolean focus_delay_func(gpointer data);
98 static void focus_delay_client_dest(ObClient *client, gpointer data);
100 Time event_curtime = CurrentTime;
101 Time event_last_user_time = CurrentTime;
102 /*! The serial of the current X event */
104 static gulong event_curserial;
105 static gboolean focus_left_screen = FALSE;
106 /*! A list of ObSerialRanges which are to be ignored for mouse enter events */
107 static GSList *ignore_serials = NULL;
110 static void ice_handler(gint fd, gpointer conn)
113 IceProcessMessages(conn, NULL, &b);
116 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
117 IcePointer *watch_data)
122 fd = IceConnectionNumber(conn);
123 obt_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
125 obt_main_loop_fd_remove(ob_main_loop, fd);
131 void event_startup(gboolean reconfig)
133 if (reconfig) return;
135 obt_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
138 IceAddConnectionWatch(ice_watch, NULL);
141 client_add_destroy_notify(focus_delay_client_dest, NULL);
144 void event_shutdown(gboolean reconfig)
146 if (reconfig) return;
149 IceRemoveConnectionWatch(ice_watch, NULL);
152 client_remove_destroy_notify(focus_delay_client_dest);
155 static Window event_get_window(XEvent *e)
162 window = obt_root(ob_screen);
165 window = e->xcreatewindow.window;
168 window = e->xmaprequest.window;
171 window = e->xmap.window;
174 window = e->xunmap.window;
177 window = e->xdestroywindow.window;
179 case ConfigureRequest:
180 window = e->xconfigurerequest.window;
182 case ConfigureNotify:
183 window = e->xconfigure.window;
187 if (obt_display_extension_xkb &&
188 e->type == obt_display_extension_xkb_basep)
190 switch (((XkbAnyEvent*)e)->xkb_type) {
192 window = ((XkbBellNotifyEvent*)e)->window;
199 if (obt_display_extension_sync &&
200 e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
205 window = e->xany.window;
210 static void event_set_curtime(XEvent *e)
212 Time t = CurrentTime;
214 /* grab the lasttime and hack up the state */
230 t = e->xproperty.time;
234 t = e->xcrossing.time;
238 if (obt_display_extension_sync &&
239 e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
241 t = ((XSyncAlarmNotifyEvent*)e)->time;
244 /* if more event types are anticipated, get their timestamp
249 /* watch that if we get an event earlier than the last specified user_time,
250 which can happen if the clock goes backwards, we erase the last
251 specified user_time */
252 if (t && event_last_user_time && event_time_after(event_last_user_time, t))
253 event_last_user_time = CurrentTime;
258 static void event_hack_mods(XEvent *e)
261 XkbStateRec xkb_state;
267 e->xbutton.state = obt_keyboard_only_modmasks(e->xbutton.state);
270 e->xkey.state = obt_keyboard_only_modmasks(e->xkey.state);
274 /* If XKB is present, then the modifiers are all strange from its
275 magic. Our X core protocol stuff won't work, so we use this to
276 find what the modifier state is instead. */
277 if (XkbGetState(obt_display, XkbUseCoreKbd, &xkb_state) == Success)
279 obt_keyboard_only_modmasks(xkb_state.compat_state);
283 e->xkey.state = obt_keyboard_only_modmasks(e->xkey.state);
284 /* remove from the state the mask of the modifier key being
285 released, if it is a modifier key being released that is */
286 e->xkey.state &= ~obt_keyboard_keycode_to_modmask(e->xkey.keycode);
290 e->xmotion.state = obt_keyboard_only_modmasks(e->xmotion.state);
291 /* compress events */
294 while (XCheckTypedWindowEvent(obt_display, e->xmotion.window,
296 e->xmotion.x = ce.xmotion.x;
297 e->xmotion.y = ce.xmotion.y;
298 e->xmotion.x_root = ce.xmotion.x_root;
299 e->xmotion.y_root = ce.xmotion.y_root;
306 static gboolean wanted_focusevent(XEvent *e, gboolean in_client_only)
308 gint mode = e->xfocus.mode;
309 gint detail = e->xfocus.detail;
310 Window win = e->xany.window;
312 if (e->type == FocusIn) {
313 /* These are ones we never want.. */
315 /* This means focus was given by a keyboard/mouse grab. */
316 if (mode == NotifyGrab)
318 /* This means focus was given back from a keyboard/mouse grab. */
319 if (mode == NotifyUngrab)
322 /* These are the ones we want.. */
324 if (win == obt_root(ob_screen)) {
325 /* If looking for a focus in on a client, then always return
326 FALSE for focus in's to the root window */
329 /* This means focus reverted off of a client */
330 else if (detail == NotifyPointerRoot ||
331 detail == NotifyDetailNone ||
332 detail == NotifyInferior ||
333 /* This means focus got here from another screen */
334 detail == NotifyNonlinear)
340 /* It was on a client, was it a valid one?
341 It's possible to get a FocusIn event for a client that was managed
344 if (in_client_only) {
345 ObWindow *w = window_find(e->xfocus.window);
346 if (!w || !WINDOW_IS_CLIENT(w))
350 /* This means focus reverted to parent from the client (this
351 happens often during iconify animation) */
352 if (detail == NotifyInferior)
356 /* This means focus moved from the root window to a client */
357 if (detail == NotifyVirtual)
359 /* This means focus moved from one client to another */
360 if (detail == NotifyNonlinearVirtual)
366 g_assert(e->type == FocusOut);
368 /* These are ones we never want.. */
370 /* This means focus was taken by a keyboard/mouse grab. */
371 if (mode == NotifyGrab)
373 /* This means focus was grabbed on a window and it was released. */
374 if (mode == NotifyUngrab)
377 /* Focus left the root window revertedto state */
378 if (win == obt_root(ob_screen))
381 /* These are the ones we want.. */
383 /* This means focus moved from a client to the root window */
384 if (detail == NotifyVirtual)
386 /* This means focus moved from one client to another */
387 if (detail == NotifyNonlinearVirtual)
395 static Bool event_look_for_focusin(Display *d, XEvent *e, XPointer arg)
397 return e->type == FocusIn && wanted_focusevent(e, FALSE);
400 static Bool event_look_for_focusin_client(Display *d, XEvent *e, XPointer arg)
402 return e->type == FocusIn && wanted_focusevent(e, TRUE);
405 static void print_focusevent(XEvent *e)
407 gint mode = e->xfocus.mode;
408 gint detail = e->xfocus.detail;
409 Window win = e->xany.window;
410 const gchar *modestr, *detailstr;
413 case NotifyNormal: modestr="NotifyNormal"; break;
414 case NotifyGrab: modestr="NotifyGrab"; break;
415 case NotifyUngrab: modestr="NotifyUngrab"; break;
416 case NotifyWhileGrabbed: modestr="NotifyWhileGrabbed"; break;
419 case NotifyAncestor: detailstr="NotifyAncestor"; break;
420 case NotifyVirtual: detailstr="NotifyVirtual"; break;
421 case NotifyInferior: detailstr="NotifyInferior"; break;
422 case NotifyNonlinear: detailstr="NotifyNonlinear"; break;
423 case NotifyNonlinearVirtual: detailstr="NotifyNonlinearVirtual"; break;
424 case NotifyPointer: detailstr="NotifyPointer"; break;
425 case NotifyPointerRoot: detailstr="NotifyPointerRoot"; break;
426 case NotifyDetailNone: detailstr="NotifyDetailNone"; break;
429 if (mode == NotifyGrab || mode == NotifyUngrab)
434 ob_debug_type(OB_DEBUG_FOCUS, "Focus%s 0x%x mode=%s detail=%s",
435 (e->xfocus.type == FocusIn ? "In" : "Out"),
441 static gboolean event_ignore(XEvent *e, ObClient *client)
446 if (!wanted_focusevent(e, FALSE))
451 if (!wanted_focusevent(e, FALSE))
458 static void event_process(const XEvent *ec, gpointer data)
461 ObEventData *ed = data;
464 ObClient *client = NULL;
466 ObDockApp *dockapp = NULL;
467 ObWindow *obwin = NULL;
468 ObMenuFrame *menu = NULL;
470 /* make a copy we can mangle */
474 window = event_get_window(e);
475 if (window == obt_root(ob_screen))
476 /* don't do any lookups, waste of cpu */;
477 else if ((obwin = window_find(window))) {
478 switch (obwin->type) {
479 case OB_WINDOW_CLASS_DOCK:
480 dock = WINDOW_AS_DOCK(obwin);
482 case OB_WINDOW_CLASS_CLIENT:
483 client = WINDOW_AS_CLIENT(obwin);
485 case OB_WINDOW_CLASS_MENUFRAME:
486 menu = WINDOW_AS_MENUFRAME(obwin);
488 case OB_WINDOW_CLASS_INTERNAL:
489 /* we don't do anything with events directly on these windows */
494 dockapp = dock_find_dockapp(window);
496 event_set_curtime(e);
497 event_curserial = e->xany.serial;
499 if (event_ignore(e, client)) {
506 /* deal with it in the kernel */
508 if (e->type == FocusIn) {
510 e->xfocus.detail == NotifyInferior)
512 ob_debug_type(OB_DEBUG_FOCUS,
513 "Focus went to the frame window");
515 focus_left_screen = FALSE;
517 focus_fallback(FALSE, config_focus_under_mouse, TRUE, TRUE);
519 /* We don't get a FocusOut for this case, because it's just moving
520 from our Inferior up to us. This happens when iconifying a
521 window with RevertToParent focus */
522 frame_adjust_focus(client->frame, FALSE);
523 /* focus_set_client(NULL) has already been called */
524 client_calc_layer(client);
526 else if (e->xfocus.detail == NotifyPointerRoot ||
527 e->xfocus.detail == NotifyDetailNone ||
528 e->xfocus.detail == NotifyInferior ||
529 e->xfocus.detail == NotifyNonlinear)
533 ob_debug_type(OB_DEBUG_FOCUS,
534 "Focus went to root or pointer root/none");
536 if (e->xfocus.detail == NotifyInferior ||
537 e->xfocus.detail == NotifyNonlinear)
539 focus_left_screen = FALSE;
542 /* If another FocusIn is in the queue then don't fallback yet. This
543 fixes the fun case of:
544 window map -> send focusin
545 window unmap -> get focusout
546 window map -> send focusin
547 get first focus out -> fall back to something (new window
548 hasn't received focus yet, so something else) -> send focusin
549 which means the "something else" is the last thing to get a
550 focusin sent to it, so the new window doesn't end up with focus.
552 But if the other focus in is something like PointerRoot then we
553 still want to fall back.
555 if (XCheckIfEvent(obt_display, &ce, event_look_for_focusin_client,
558 XPutBackEvent(obt_display, &ce);
559 ob_debug_type(OB_DEBUG_FOCUS,
560 " but another FocusIn is coming");
562 /* Focus has been reverted.
564 FocusOut events come after UnmapNotify, so we don't need to
565 worry about focusing an invalid window
568 if (!focus_left_screen)
569 focus_fallback(FALSE, config_focus_under_mouse,
575 ob_debug_type(OB_DEBUG_FOCUS,
576 "Focus went to a window that is already gone");
578 /* If you send focus to a window and then it disappears, you can
579 get the FocusIn for it, after it is unmanaged.
580 Just wait for the next FocusOut/FocusIn pair, but make note that
581 the window that was focused no longer is. */
582 focus_set_client(NULL);
584 else if (client != focus_client) {
585 focus_left_screen = FALSE;
586 frame_adjust_focus(client->frame, TRUE);
587 focus_set_client(client);
588 client_calc_layer(client);
589 client_bring_helper_windows(client);
591 } else if (e->type == FocusOut) {
594 /* Look for the followup FocusIn */
595 if (!XCheckIfEvent(obt_display, &ce, event_look_for_focusin, NULL)) {
596 /* There is no FocusIn, this means focus went to a window that
597 is not being managed, or a window on another screen. */
601 obt_display_ignore_errors(TRUE);
602 if (XGetInputFocus(obt_display, &win, &i) &&
603 XGetGeometry(obt_display, win, &root, &i,&i,&u,&u,&u,&u) &&
604 root != obt_root(ob_screen))
606 ob_debug_type(OB_DEBUG_FOCUS,
607 "Focus went to another screen !");
608 focus_left_screen = TRUE;
611 ob_debug_type(OB_DEBUG_FOCUS,
612 "Focus went to a black hole !");
613 obt_display_ignore_errors(FALSE);
614 /* nothing is focused */
615 focus_set_client(NULL);
617 /* Focus moved, so process the FocusIn event */
618 ObEventData ed = { .ignored = FALSE };
619 event_process(&ce, &ed);
621 /* The FocusIn was ignored, this means it was on a window
622 that isn't a client. */
623 ob_debug_type(OB_DEBUG_FOCUS,
624 "Focus went to an unmanaged window 0x%x !",
626 focus_fallback(TRUE, config_focus_under_mouse, TRUE, TRUE);
630 if (client && client != focus_client) {
631 frame_adjust_focus(client->frame, FALSE);
632 /* focus_set_client(NULL) has already been called in this
633 section or by focus_fallback */
634 client_calc_layer(client);
638 event_handle_client(client, e);
640 event_handle_dockapp(dockapp, e);
642 event_handle_dock(dock, e);
644 event_handle_menu(menu, e);
645 else if (window == obt_root(ob_screen))
646 event_handle_root(e);
647 else if (e->type == MapRequest)
648 window_manage(window);
649 else if (e->type == MappingNotify) {
650 /* keyboard layout changes for modifier mapping changes. reload the
651 modifier map, and rebind all the key bindings as appropriate */
652 ob_debug("Kepboard map changed. Reloading keyboard bindings.");
653 obt_keyboard_reload();
656 else if (e->type == ClientMessage) {
657 /* This is for _NET_WM_REQUEST_FRAME_EXTENTS messages. They come for
658 windows that are not managed yet. */
659 if (e->xclient.message_type ==
660 OBT_PROP_ATOM(NET_REQUEST_FRAME_EXTENTS))
662 /* Pretend to manage the client, getting information used to
663 determine its decorations */
664 ObClient *c = client_fake_manage(e->xclient.window);
667 /* set the frame extents on the window */
668 vals[0] = c->frame->size.left;
669 vals[1] = c->frame->size.right;
670 vals[2] = c->frame->size.top;
671 vals[3] = c->frame->size.bottom;
672 OBT_PROP_SETA32(e->xclient.window, NET_FRAME_EXTENTS,
675 /* Free the pretend client */
676 client_fake_unmanage(c);
679 else if (e->type == ConfigureRequest) {
680 /* unhandled configure requests must be used to configure the
684 xwc.x = e->xconfigurerequest.x;
685 xwc.y = e->xconfigurerequest.y;
686 xwc.width = e->xconfigurerequest.width;
687 xwc.height = e->xconfigurerequest.height;
688 xwc.border_width = e->xconfigurerequest.border_width;
689 xwc.sibling = e->xconfigurerequest.above;
690 xwc.stack_mode = e->xconfigurerequest.detail;
692 /* we are not to be held responsible if someone sends us an
694 obt_display_ignore_errors(TRUE);
695 XConfigureWindow(obt_display, window,
696 e->xconfigurerequest.value_mask, &xwc);
697 obt_display_ignore_errors(FALSE);
700 else if (obt_display_extension_sync &&
701 e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
703 XSyncAlarmNotifyEvent *se = (XSyncAlarmNotifyEvent*)e;
704 if (se->alarm == moveresize_alarm && moveresize_in_progress)
709 if (e->type == ButtonPress || e->type == ButtonRelease) {
710 /* If the button press was on some non-root window, or was physically
711 on the root window, the process it */
712 if (window != obt_root(ob_screen) ||
713 e->xbutton.subwindow == None)
715 event_handle_user_input(client, e);
717 /* Otherwise only process it if it was physically on an openbox
722 if ((w = window_find(e->xbutton.subwindow)) &&
723 WINDOW_IS_INTERNAL(w))
725 event_handle_user_input(client, e);
729 else if (e->type == KeyPress || e->type == KeyRelease ||
730 e->type == MotionNotify)
731 event_handle_user_input(client, e);
733 /* if something happens and it's not from an XEvent, then we don't know
735 event_curtime = CurrentTime;
739 static void event_handle_root(XEvent *e)
745 ob_debug("Another WM has requested to replace us. Exiting.");
750 if (e->xclient.format != 32) break;
752 msgtype = e->xclient.message_type;
753 if (msgtype == OBT_PROP_ATOM(NET_CURRENT_DESKTOP)) {
754 guint d = e->xclient.data.l[0];
755 if (d < screen_num_desktops) {
756 event_curtime = e->xclient.data.l[1];
757 if (event_curtime == 0)
758 ob_debug_type(OB_DEBUG_APP_BUGS,
759 "_NET_CURRENT_DESKTOP message is missing "
761 screen_set_desktop(d, TRUE);
763 } else if (msgtype == OBT_PROP_ATOM(NET_NUMBER_OF_DESKTOPS)) {
764 guint d = e->xclient.data.l[0];
765 if (d > 0 && d <= 1000)
766 screen_set_num_desktops(d);
767 } else if (msgtype == OBT_PROP_ATOM(NET_SHOWING_DESKTOP)) {
768 screen_show_desktop(e->xclient.data.l[0] != 0, NULL);
769 } else if (msgtype == OBT_PROP_ATOM(OB_CONTROL)) {
770 ob_debug("OB_CONTROL: %d", e->xclient.data.l[0]);
771 if (e->xclient.data.l[0] == 1)
773 else if (e->xclient.data.l[0] == 2)
775 else if (e->xclient.data.l[0] == 3)
777 } else if (msgtype == OBT_PROP_ATOM(WM_PROTOCOLS)) {
778 if ((Atom)e->xclient.data.l[0] == OBT_PROP_ATOM(NET_WM_PING))
779 ping_got_pong(e->xclient.data.l[1]);
783 if (e->xproperty.atom == OBT_PROP_ATOM(NET_DESKTOP_NAMES)) {
784 ob_debug("UPDATE DESKTOP NAMES");
785 screen_update_desktop_names();
787 else if (e->xproperty.atom == OBT_PROP_ATOM(NET_DESKTOP_LAYOUT))
788 screen_update_layout();
790 case ConfigureNotify:
792 XRRUpdateConfiguration(e);
801 void event_enter_client(ObClient *client)
803 g_assert(config_focus_follow);
805 if (client_enter_focusable(client) && client_can_focus(client)) {
806 if (config_focus_delay) {
807 ObFocusDelayData *data;
809 obt_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
811 data = g_new(ObFocusDelayData, 1);
812 data->client = client;
813 data->time = event_curtime;
814 data->serial = event_curserial;
816 obt_main_loop_timeout_add(ob_main_loop,
817 config_focus_delay * 1000,
819 data, focus_delay_cmp, focus_delay_dest);
821 ObFocusDelayData data;
822 data.client = client;
823 data.time = event_curtime;
824 data.serial = event_curserial;
825 focus_delay_func(&data);
830 static void event_handle_client(ObClient *client, XEvent *e)
835 static gint px = -1, py = -1;
840 /* save where the press occured for the first button pressed */
842 pb = e->xbutton.button;
847 /* Wheel buttons don't draw because they are an instant click, so it
848 is a waste of resources to go drawing it.
849 if the user is doing an intereactive thing, or has a menu open then
850 the mouse is grabbed (possibly) and if we get these events we don't
851 want to deal with them
853 if (!(e->xbutton.button == 4 || e->xbutton.button == 5) &&
856 /* use where the press occured */
857 con = frame_context(client, e->xbutton.window, px, py);
858 con = mouse_button_frame_context(con, e->xbutton.button,
861 if (e->type == ButtonRelease && e->xbutton.button == pb)
862 pb = 0, px = py = -1;
865 case OB_FRAME_CONTEXT_MAXIMIZE:
866 client->frame->max_press = (e->type == ButtonPress);
867 frame_adjust_state(client->frame);
869 case OB_FRAME_CONTEXT_CLOSE:
870 client->frame->close_press = (e->type == ButtonPress);
871 frame_adjust_state(client->frame);
873 case OB_FRAME_CONTEXT_ICONIFY:
874 client->frame->iconify_press = (e->type == ButtonPress);
875 frame_adjust_state(client->frame);
877 case OB_FRAME_CONTEXT_ALLDESKTOPS:
878 client->frame->desk_press = (e->type == ButtonPress);
879 frame_adjust_state(client->frame);
881 case OB_FRAME_CONTEXT_SHADE:
882 client->frame->shade_press = (e->type == ButtonPress);
883 frame_adjust_state(client->frame);
886 /* nothing changes with clicks for any other contexts */
892 /* when there is a grab on the pointer, we won't get enter/leave
893 notifies, but we still get motion events */
894 if (grab_on_pointer()) break;
896 con = frame_context(client, e->xmotion.window,
897 e->xmotion.x, e->xmotion.y);
899 case OB_FRAME_CONTEXT_TITLEBAR:
900 case OB_FRAME_CONTEXT_TLCORNER:
901 case OB_FRAME_CONTEXT_TRCORNER:
902 /* we've left the button area inside the titlebar */
903 if (client->frame->max_hover || client->frame->desk_hover ||
904 client->frame->shade_hover || client->frame->iconify_hover ||
905 client->frame->close_hover)
907 client->frame->max_hover = FALSE;
908 client->frame->desk_hover = FALSE;
909 client->frame->shade_hover = FALSE;
910 client->frame->iconify_hover = FALSE;
911 client->frame->close_hover = FALSE;
912 frame_adjust_state(client->frame);
915 case OB_FRAME_CONTEXT_MAXIMIZE:
916 if (!client->frame->max_hover) {
917 client->frame->max_hover = TRUE;
918 frame_adjust_state(client->frame);
921 case OB_FRAME_CONTEXT_ALLDESKTOPS:
922 if (!client->frame->desk_hover) {
923 client->frame->desk_hover = TRUE;
924 frame_adjust_state(client->frame);
927 case OB_FRAME_CONTEXT_SHADE:
928 if (!client->frame->shade_hover) {
929 client->frame->shade_hover = TRUE;
930 frame_adjust_state(client->frame);
933 case OB_FRAME_CONTEXT_ICONIFY:
934 if (!client->frame->iconify_hover) {
935 client->frame->iconify_hover = TRUE;
936 frame_adjust_state(client->frame);
939 case OB_FRAME_CONTEXT_CLOSE:
940 if (!client->frame->close_hover) {
941 client->frame->close_hover = TRUE;
942 frame_adjust_state(client->frame);
950 con = frame_context(client, e->xcrossing.window,
951 e->xcrossing.x, e->xcrossing.y);
953 case OB_FRAME_CONTEXT_TITLEBAR:
954 case OB_FRAME_CONTEXT_TLCORNER:
955 case OB_FRAME_CONTEXT_TRCORNER:
956 /* we've left the button area inside the titlebar */
957 if (client->frame->max_hover || client->frame->desk_hover ||
958 client->frame->shade_hover || client->frame->iconify_hover ||
959 client->frame->close_hover)
961 client->frame->max_hover = FALSE;
962 client->frame->desk_hover = FALSE;
963 client->frame->shade_hover = FALSE;
964 client->frame->iconify_hover = FALSE;
965 client->frame->close_hover = FALSE;
966 frame_adjust_state(client->frame);
969 case OB_FRAME_CONTEXT_MAXIMIZE:
970 client->frame->max_hover = FALSE;
971 frame_adjust_state(client->frame);
973 case OB_FRAME_CONTEXT_ALLDESKTOPS:
974 client->frame->desk_hover = FALSE;
975 frame_adjust_state(client->frame);
977 case OB_FRAME_CONTEXT_SHADE:
978 client->frame->shade_hover = FALSE;
979 frame_adjust_state(client->frame);
981 case OB_FRAME_CONTEXT_ICONIFY:
982 client->frame->iconify_hover = FALSE;
983 frame_adjust_state(client->frame);
985 case OB_FRAME_CONTEXT_CLOSE:
986 client->frame->close_hover = FALSE;
987 frame_adjust_state(client->frame);
989 case OB_FRAME_CONTEXT_FRAME:
990 /* When the mouse leaves an animating window, don't use the
991 corresponding enter events. Pretend like the animating window
992 doesn't even exist..! */
993 if (frame_iconify_animating(client->frame))
994 event_end_ignore_all_enters(event_start_ignore_all_enters());
996 ob_debug_type(OB_DEBUG_FOCUS,
997 "%sNotify mode %d detail %d on %lx",
998 (e->type == EnterNotify ? "Enter" : "Leave"),
1000 e->xcrossing.detail, (client?client->window:0));
1001 if (grab_on_keyboard())
1003 if (config_focus_follow && config_focus_delay &&
1004 /* leave inferior events can happen when the mouse goes onto
1005 the window's border and then into the window before the
1007 e->xcrossing.detail != NotifyInferior)
1009 obt_main_loop_timeout_remove_data(ob_main_loop,
1020 con = frame_context(client, e->xcrossing.window,
1021 e->xcrossing.x, e->xcrossing.y);
1023 case OB_FRAME_CONTEXT_MAXIMIZE:
1024 client->frame->max_hover = TRUE;
1025 frame_adjust_state(client->frame);
1027 case OB_FRAME_CONTEXT_ALLDESKTOPS:
1028 client->frame->desk_hover = TRUE;
1029 frame_adjust_state(client->frame);
1031 case OB_FRAME_CONTEXT_SHADE:
1032 client->frame->shade_hover = TRUE;
1033 frame_adjust_state(client->frame);
1035 case OB_FRAME_CONTEXT_ICONIFY:
1036 client->frame->iconify_hover = TRUE;
1037 frame_adjust_state(client->frame);
1039 case OB_FRAME_CONTEXT_CLOSE:
1040 client->frame->close_hover = TRUE;
1041 frame_adjust_state(client->frame);
1043 case OB_FRAME_CONTEXT_FRAME:
1044 if (grab_on_keyboard())
1046 if (e->xcrossing.mode == NotifyGrab ||
1047 e->xcrossing.mode == NotifyUngrab ||
1048 /*ignore enters when we're already in the window */
1049 e->xcrossing.detail == NotifyInferior ||
1050 is_enter_focus_event_ignored(e))
1052 ob_debug_type(OB_DEBUG_FOCUS,
1053 "%sNotify mode %d detail %d serial %lu on %lx "
1055 (e->type == EnterNotify ? "Enter" : "Leave"),
1057 e->xcrossing.detail,
1058 e->xcrossing.serial,
1059 client?client->window:0);
1062 ob_debug_type(OB_DEBUG_FOCUS,
1063 "%sNotify mode %d detail %d serial %lu on %lx, "
1065 (e->type == EnterNotify ? "Enter" : "Leave"),
1067 e->xcrossing.detail,
1068 e->xcrossing.serial,
1069 (client?client->window:0));
1070 if (config_focus_follow)
1071 event_enter_client(client);
1079 case ConfigureRequest:
1081 /* dont compress these unless you're going to watch for property
1082 notifies in between (these can change what the configure would
1084 also you can't compress stacking events
1088 gboolean move = FALSE;
1089 gboolean resize = FALSE;
1091 /* get the current area */
1092 RECT_TO_DIMS(client->area, x, y, w, h);
1094 ob_debug("ConfigureRequest for \"%s\" desktop %d wmstate %d "
1097 screen_desktop, client->wmstate, client->frame->visible);
1098 ob_debug(" x %d y %d w %d h %d b %d",
1099 x, y, w, h, client->border_width);
1101 if (e->xconfigurerequest.value_mask & CWBorderWidth)
1102 if (client->border_width != e->xconfigurerequest.border_width) {
1103 client->border_width = e->xconfigurerequest.border_width;
1105 /* if the border width is changing then that is the same
1106 as requesting a resize, but we don't actually change
1107 the client's border, so it will change their root
1108 coordiantes (since they include the border width) and
1109 we need to a notify then */
1114 if (e->xconfigurerequest.value_mask & CWStackMode) {
1115 ObClient *sibling = NULL;
1116 gulong ignore_start;
1119 /* get the sibling */
1120 if (e->xconfigurerequest.value_mask & CWSibling) {
1122 win = window_find(e->xconfigurerequest.above);
1123 if (win && WINDOW_IS_CLIENT(win) &&
1124 WINDOW_AS_CLIENT(win) != client)
1126 sibling = WINDOW_AS_CLIENT(win);
1129 /* an invalid sibling was specified so don't restack at
1130 all, it won't make sense no matter what we do */
1135 if (!config_focus_under_mouse)
1136 ignore_start = event_start_ignore_all_enters();
1137 stacking_restack_request(client, sibling,
1138 e->xconfigurerequest.detail);
1139 if (!config_focus_under_mouse)
1140 event_end_ignore_all_enters(ignore_start);
1143 /* a stacking change moves the window without resizing */
1147 if ((e->xconfigurerequest.value_mask & CWX) ||
1148 (e->xconfigurerequest.value_mask & CWY) ||
1149 (e->xconfigurerequest.value_mask & CWWidth) ||
1150 (e->xconfigurerequest.value_mask & CWHeight))
1152 if (e->xconfigurerequest.value_mask & CWX) {
1153 /* don't allow clients to move shaded windows (fvwm does this)
1155 if (!client->shaded)
1156 x = e->xconfigurerequest.x;
1159 if (e->xconfigurerequest.value_mask & CWY) {
1160 /* don't allow clients to move shaded windows (fvwm does this)
1162 if (!client->shaded)
1163 y = e->xconfigurerequest.y;
1167 if (e->xconfigurerequest.value_mask & CWWidth) {
1168 w = e->xconfigurerequest.width;
1171 if (e->xconfigurerequest.value_mask & CWHeight) {
1172 h = e->xconfigurerequest.height;
1177 ob_debug("ConfigureRequest x(%d) %d y(%d) %d w(%d) %d h(%d) %d "
1178 "move %d resize %d",
1179 e->xconfigurerequest.value_mask & CWX, x,
1180 e->xconfigurerequest.value_mask & CWY, y,
1181 e->xconfigurerequest.value_mask & CWWidth, w,
1182 e->xconfigurerequest.value_mask & CWHeight, h,
1185 /* check for broken apps moving to their root position
1187 XXX remove this some day...that would be nice. right now all
1188 kde apps do this when they try activate themselves on another
1189 desktop. eg. open amarok window on desktop 1, switch to desktop
1190 2, click amarok tray icon. it will move by its decoration size.
1192 if (x != client->area.x &&
1193 x == (client->frame->area.x + client->frame->size.left -
1194 (gint)client->border_width) &&
1195 y != client->area.y &&
1196 y == (client->frame->area.y + client->frame->size.top -
1197 (gint)client->border_width) &&
1198 w == client->area.width &&
1199 h == client->area.height)
1201 ob_debug_type(OB_DEBUG_APP_BUGS,
1202 "Application %s is trying to move via "
1203 "ConfigureRequest to it's root window position "
1204 "but it is not using StaticGravity",
1210 /* they still requested a move, so don't change whether a
1211 notify is sent or not */
1217 client_try_configure(client, &x, &y, &w, &h, &lw, &lh, FALSE);
1219 /* if x was not given, then use gravity to figure out the new
1220 x. the reference point should not be moved */
1221 if ((e->xconfigurerequest.value_mask & CWWidth &&
1222 !(e->xconfigurerequest.value_mask & CWX)))
1223 client_gravity_resize_w(client, &x, client->area.width, w);
1224 /* if y was not given, then use gravity to figure out the new
1225 y. the reference point should not be moved */
1226 if ((e->xconfigurerequest.value_mask & CWHeight &&
1227 !(e->xconfigurerequest.value_mask & CWY)))
1228 client_gravity_resize_h(client, &y, client->area.height,h);
1230 client_find_onscreen(client, &x, &y, w, h, FALSE);
1232 ob_debug("Granting ConfigureRequest x %d y %d w %d h %d",
1234 client_configure(client, x, y, w, h, FALSE, TRUE, TRUE);
1239 /* Don't care about the frame window unmapping */
1240 if (e->xunmap.window != client->window) break;
1243 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
1245 client->window, e->xunmap.event, e->xunmap.from_configure,
1246 client->ignore_unmaps);
1248 if (client->ignore_unmaps) {
1249 client->ignore_unmaps--;
1252 client_unmanage(client);
1255 ob_debug("DestroyNotify for window 0x%x", client->window);
1256 client_unmanage(client);
1258 case ReparentNotify:
1259 /* this is when the client is first taken captive in the frame */
1260 if (e->xreparent.parent == client->frame->window) break;
1263 This event is quite rare and is usually handled in unmapHandler.
1264 However, if the window is unmapped when the reparent event occurs,
1265 the window manager never sees it because an unmap event is not sent
1266 to an already unmapped window.
1269 /* we don't want the reparent event, put it back on the stack for the
1270 X server to deal with after we unmanage the window */
1271 XPutBackEvent(obt_display, e);
1273 ob_debug("ReparentNotify for window 0x%x", client->window);
1274 client_unmanage(client);
1277 ob_debug("MapRequest for 0x%lx", client->window);
1278 if (!client->iconic) break; /* this normally doesn't happen, but if it
1279 does, we don't want it!
1280 it can happen now when the window is on
1281 another desktop, but we still don't
1283 client_activate(client, FALSE, TRUE, TRUE, TRUE);
1286 /* validate cuz we query stuff off the client here */
1287 if (!client_validate(client)) break;
1289 if (e->xclient.format != 32) return;
1291 msgtype = e->xclient.message_type;
1292 if (msgtype == OBT_PROP_ATOM(WM_CHANGE_STATE)) {
1293 /* compress changes into a single change */
1294 while (XCheckTypedWindowEvent(obt_display, client->window,
1296 /* XXX: it would be nice to compress ALL messages of a
1297 type, not just messages in a row without other
1298 message types between. */
1299 if (ce.xclient.message_type != msgtype) {
1300 XPutBackEvent(obt_display, &ce);
1303 e->xclient = ce.xclient;
1305 client_set_wm_state(client, e->xclient.data.l[0]);
1306 } else if (msgtype == OBT_PROP_ATOM(NET_WM_DESKTOP)) {
1307 /* compress changes into a single change */
1308 while (XCheckTypedWindowEvent(obt_display, client->window,
1310 /* XXX: it would be nice to compress ALL messages of a
1311 type, not just messages in a row without other
1312 message types between. */
1313 if (ce.xclient.message_type != msgtype) {
1314 XPutBackEvent(obt_display, &ce);
1317 e->xclient = ce.xclient;
1319 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
1320 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
1321 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
1323 } else if (msgtype == OBT_PROP_ATOM(NET_WM_STATE)) {
1324 gulong ignore_start;
1326 /* can't compress these */
1327 ob_debug("net_wm_state %s %ld %ld for 0x%lx",
1328 (e->xclient.data.l[0] == 0 ? "Remove" :
1329 e->xclient.data.l[0] == 1 ? "Add" :
1330 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1331 e->xclient.data.l[1], e->xclient.data.l[2],
1334 /* ignore enter events caused by these like ob actions do */
1335 if (!config_focus_under_mouse)
1336 ignore_start = event_start_ignore_all_enters();
1337 client_set_state(client, e->xclient.data.l[0],
1338 e->xclient.data.l[1], e->xclient.data.l[2]);
1339 if (!config_focus_under_mouse)
1340 event_end_ignore_all_enters(ignore_start);
1341 } else if (msgtype == OBT_PROP_ATOM(NET_CLOSE_WINDOW)) {
1342 ob_debug("net_close_window for 0x%lx", client->window);
1343 client_close(client);
1344 } else if (msgtype == OBT_PROP_ATOM(NET_ACTIVE_WINDOW)) {
1345 ob_debug("net_active_window for 0x%lx source=%s",
1347 (e->xclient.data.l[0] == 0 ? "unknown" :
1348 (e->xclient.data.l[0] == 1 ? "application" :
1349 (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1350 /* XXX make use of data.l[2] !? */
1351 if (e->xclient.data.l[0] == 1 || e->xclient.data.l[0] == 2) {
1352 /* don't use the user's timestamp for client_focus, cuz if it's
1353 an old broken timestamp (happens all the time) then focus
1354 won't move even though we're trying to move it
1355 event_curtime = e->xclient.data.l[1];*/
1356 if (e->xclient.data.l[1] == 0)
1357 ob_debug_type(OB_DEBUG_APP_BUGS,
1358 "_NET_ACTIVE_WINDOW message for window %s is"
1359 " missing a timestamp", client->title);
1361 ob_debug_type(OB_DEBUG_APP_BUGS,
1362 "_NET_ACTIVE_WINDOW message for window %s is "
1363 "missing source indication");
1364 client_activate(client, FALSE, TRUE, TRUE,
1365 (e->xclient.data.l[0] == 0 ||
1366 e->xclient.data.l[0] == 2));
1367 } else if (msgtype == OBT_PROP_ATOM(NET_WM_MOVERESIZE)) {
1368 ob_debug("net_wm_moveresize for 0x%lx direction %d",
1369 client->window, e->xclient.data.l[2]);
1370 if ((Atom)e->xclient.data.l[2] ==
1371 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOPLEFT) ||
1372 (Atom)e->xclient.data.l[2] ==
1373 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOP) ||
1374 (Atom)e->xclient.data.l[2] ==
1375 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOPRIGHT) ||
1376 (Atom)e->xclient.data.l[2] ==
1377 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_RIGHT) ||
1378 (Atom)e->xclient.data.l[2] ==
1379 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_RIGHT) ||
1380 (Atom)e->xclient.data.l[2] ==
1381 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT) ||
1382 (Atom)e->xclient.data.l[2] ==
1383 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOM) ||
1384 (Atom)e->xclient.data.l[2] ==
1385 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT) ||
1386 (Atom)e->xclient.data.l[2] ==
1387 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_LEFT) ||
1388 (Atom)e->xclient.data.l[2] ==
1389 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE) ||
1390 (Atom)e->xclient.data.l[2] ==
1391 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_KEYBOARD) ||
1392 (Atom)e->xclient.data.l[2] ==
1393 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE_KEYBOARD))
1395 moveresize_start(client, e->xclient.data.l[0],
1396 e->xclient.data.l[1], e->xclient.data.l[3],
1397 e->xclient.data.l[2]);
1399 else if ((Atom)e->xclient.data.l[2] ==
1400 OBT_PROP_ATOM(NET_WM_MOVERESIZE_CANCEL))
1401 moveresize_end(TRUE);
1402 } else if (msgtype == OBT_PROP_ATOM(NET_MOVERESIZE_WINDOW)) {
1403 gint ograv, x, y, w, h;
1405 ograv = client->gravity;
1407 if (e->xclient.data.l[0] & 0xff)
1408 client->gravity = e->xclient.data.l[0] & 0xff;
1410 if (e->xclient.data.l[0] & 1 << 8)
1411 x = e->xclient.data.l[1];
1414 if (e->xclient.data.l[0] & 1 << 9)
1415 y = e->xclient.data.l[2];
1419 if (e->xclient.data.l[0] & 1 << 10) {
1420 w = e->xclient.data.l[3];
1422 /* if x was not given, then use gravity to figure out the new
1423 x. the reference point should not be moved */
1424 if (!(e->xclient.data.l[0] & 1 << 8))
1425 client_gravity_resize_w(client, &x, client->area.width, w);
1428 w = client->area.width;
1430 if (e->xclient.data.l[0] & 1 << 11) {
1431 h = e->xclient.data.l[4];
1433 /* if y was not given, then use gravity to figure out the new
1434 y. the reference point should not be moved */
1435 if (!(e->xclient.data.l[0] & 1 << 9))
1436 client_gravity_resize_h(client, &y, client->area.height,h);
1439 h = client->area.height;
1441 ob_debug("MOVERESIZE x %d %d y %d %d (gravity %d)",
1442 e->xclient.data.l[0] & 1 << 8, x,
1443 e->xclient.data.l[0] & 1 << 9, y,
1446 client_find_onscreen(client, &x, &y, w, h, FALSE);
1448 client_configure(client, x, y, w, h, FALSE, TRUE, FALSE);
1450 client->gravity = ograv;
1451 } else if (msgtype == OBT_PROP_ATOM(NET_RESTACK_WINDOW)) {
1452 if (e->xclient.data.l[0] != 2) {
1453 ob_debug_type(OB_DEBUG_APP_BUGS,
1454 "_NET_RESTACK_WINDOW sent for window %s with "
1455 "invalid source indication %ld",
1456 client->title, e->xclient.data.l[0]);
1458 ObClient *sibling = NULL;
1459 if (e->xclient.data.l[1]) {
1460 ObWindow *win = window_find(e->xclient.data.l[1]);
1461 if (WINDOW_IS_CLIENT(win) &&
1462 WINDOW_AS_CLIENT(win) != client)
1464 sibling = WINDOW_AS_CLIENT(win);
1466 if (sibling == NULL)
1467 ob_debug_type(OB_DEBUG_APP_BUGS,
1468 "_NET_RESTACK_WINDOW sent for window %s "
1469 "with invalid sibling 0x%x",
1470 client->title, e->xclient.data.l[1]);
1472 if (e->xclient.data.l[2] == Below ||
1473 e->xclient.data.l[2] == BottomIf ||
1474 e->xclient.data.l[2] == Above ||
1475 e->xclient.data.l[2] == TopIf ||
1476 e->xclient.data.l[2] == Opposite)
1478 gulong ignore_start;
1480 if (!config_focus_under_mouse)
1481 ignore_start = event_start_ignore_all_enters();
1482 /* just raise, don't activate */
1483 stacking_restack_request(client, sibling,
1484 e->xclient.data.l[2]);
1485 if (!config_focus_under_mouse)
1486 event_end_ignore_all_enters(ignore_start);
1488 /* send a synthetic ConfigureNotify, cuz this is supposed
1489 to be like a ConfigureRequest. */
1490 client_reconfigure(client, TRUE);
1492 ob_debug_type(OB_DEBUG_APP_BUGS,
1493 "_NET_RESTACK_WINDOW sent for window %s "
1494 "with invalid detail %d",
1495 client->title, e->xclient.data.l[2]);
1499 case PropertyNotify:
1500 /* validate cuz we query stuff off the client here */
1501 if (!client_validate(client)) break;
1503 /* compress changes to a single property into a single change */
1504 while (XCheckTypedWindowEvent(obt_display, client->window,
1508 /* XXX: it would be nice to compress ALL changes to a property,
1509 not just changes in a row without other props between. */
1511 a = ce.xproperty.atom;
1512 b = e->xproperty.atom;
1516 if ((a == OBT_PROP_ATOM(NET_WM_NAME) ||
1517 a == OBT_PROP_ATOM(WM_NAME) ||
1518 a == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1519 a == OBT_PROP_ATOM(WM_ICON_NAME))
1521 (b == OBT_PROP_ATOM(NET_WM_NAME) ||
1522 b == OBT_PROP_ATOM(WM_NAME) ||
1523 b == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1524 b == OBT_PROP_ATOM(WM_ICON_NAME))) {
1527 if (a == OBT_PROP_ATOM(NET_WM_ICON) &&
1528 b == OBT_PROP_ATOM(NET_WM_ICON))
1531 XPutBackEvent(obt_display, &ce);
1535 msgtype = e->xproperty.atom;
1536 if (msgtype == XA_WM_NORMAL_HINTS) {
1537 ob_debug("Update NORMAL hints");
1538 client_update_normal_hints(client);
1539 /* normal hints can make a window non-resizable */
1540 client_setup_decor_and_functions(client, FALSE);
1542 /* make sure the client's sizes are within its bounds, but only
1543 reconfigure the window if it needs to. emacs will update its
1544 normal hints every time it receives a conigurenotify */
1545 client_reconfigure(client, FALSE);
1546 } else if (msgtype == XA_WM_HINTS) {
1547 client_update_wmhints(client);
1548 } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1549 client_update_transient_for(client);
1550 client_get_type_and_transientness(client);
1551 /* type may have changed, so update the layer */
1552 client_calc_layer(client);
1553 client_setup_decor_and_functions(client, TRUE);
1554 } else if (msgtype == OBT_PROP_ATOM(NET_WM_NAME) ||
1555 msgtype == OBT_PROP_ATOM(WM_NAME) ||
1556 msgtype == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1557 msgtype == OBT_PROP_ATOM(WM_ICON_NAME)) {
1558 client_update_title(client);
1559 } else if (msgtype == OBT_PROP_ATOM(WM_PROTOCOLS)) {
1560 client_update_protocols(client);
1561 client_setup_decor_and_functions(client, TRUE);
1563 else if (msgtype == OBT_PROP_ATOM(NET_WM_STRUT)) {
1564 client_update_strut(client);
1566 else if (msgtype == OBT_PROP_ATOM(NET_WM_STRUT_PARTIAL)) {
1567 client_update_strut(client);
1569 else if (msgtype == OBT_PROP_ATOM(NET_WM_ICON)) {
1570 client_update_icons(client);
1572 else if (msgtype == OBT_PROP_ATOM(NET_WM_ICON_GEOMETRY)) {
1573 client_update_icon_geometry(client);
1575 else if (msgtype == OBT_PROP_ATOM(NET_WM_USER_TIME)) {
1577 if (client == focus_client &&
1578 OBT_PROP_GET32(client->window, NET_WM_USER_TIME, CARDINAL, &t)
1579 && t && !event_time_after(t, e->xproperty.time) &&
1580 (!event_last_user_time ||
1581 event_time_after(t, event_last_user_time)))
1583 event_last_user_time = t;
1587 else if (msgtype == OBT_PROP_ATOM(NET_WM_SYNC_REQUEST_COUNTER)) {
1588 client_update_sync_request_counter(client);
1592 case ColormapNotify:
1593 client_update_colormap(client, e->xcolormap.colormap);
1598 if (obt_display_extension_shape &&
1599 e->type == obt_display_extension_shape_basep)
1601 client->shaped = ((XShapeEvent*)e)->shaped;
1602 frame_adjust_shape(client->frame);
1608 static void event_handle_dock(ObDock *s, XEvent *e)
1612 if (e->xbutton.button == 1)
1613 stacking_raise(DOCK_AS_WINDOW(s));
1614 else if (e->xbutton.button == 2)
1615 stacking_lower(DOCK_AS_WINDOW(s));
1621 /* don't hide when moving into a dock app */
1622 if (e->xcrossing.detail != NotifyInferior)
1628 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1632 dock_app_drag(app, &e->xmotion);
1635 if (app->ignore_unmaps) {
1636 app->ignore_unmaps--;
1639 dock_unmanage(app, TRUE);
1642 dock_unmanage(app, FALSE);
1644 case ReparentNotify:
1645 dock_unmanage(app, FALSE);
1647 case ConfigureNotify:
1648 dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1653 static ObMenuFrame* find_active_menu(void)
1656 ObMenuFrame *ret = NULL;
1658 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1667 static ObMenuFrame* find_active_or_last_menu(void)
1669 ObMenuFrame *ret = NULL;
1671 ret = find_active_menu();
1672 if (!ret && menu_frame_visible)
1673 ret = menu_frame_visible->data;
1677 static gboolean event_handle_menu_input(XEvent *ev)
1679 gboolean ret = FALSE;
1681 if (ev->type == ButtonRelease) {
1682 ObMenuEntryFrame *e;
1684 if (menu_hide_delay_reached() &&
1685 (ev->xbutton.button < 4 || ev->xbutton.button > 5))
1687 if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1688 ev->xbutton.y_root)))
1690 menu_frame_select(e->frame, e, TRUE);
1691 menu_entry_frame_execute(e, ev->xbutton.state);
1694 menu_frame_hide_all();
1698 else if (ev->type == MotionNotify) {
1700 ObMenuEntryFrame *e;
1702 if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1703 ev->xmotion.y_root)))
1704 if (!(f = find_active_menu()) ||
1706 f->parent == e->frame ||
1707 f->child == e->frame)
1708 menu_frame_select(e->frame, e, FALSE);
1710 else if (ev->type == KeyPress || ev->type == KeyRelease) {
1711 guint keycode, state;
1715 keycode = ev->xkey.keycode;
1716 state = ev->xkey.state;
1717 unikey = obt_keyboard_keycode_to_unichar(keycode);
1719 frame = find_active_or_last_menu();
1721 g_assert_not_reached(); /* there is no active menu */
1723 /* Allow control while going thru the menu */
1724 else if (ev->type == KeyPress && (state & ~ControlMask) == 0) {
1725 frame->got_press = TRUE;
1727 if (keycode == ob_keycode(OB_KEY_ESCAPE)) {
1728 menu_frame_hide_all();
1732 else if (keycode == ob_keycode(OB_KEY_LEFT)) {
1733 /* Left goes to the parent menu */
1734 menu_frame_select(frame, NULL, TRUE);
1738 else if (keycode == ob_keycode(OB_KEY_RIGHT)) {
1739 /* Right goes to the selected submenu */
1740 if (frame->child) menu_frame_select_next(frame->child);
1744 else if (keycode == ob_keycode(OB_KEY_UP)) {
1745 menu_frame_select_previous(frame);
1749 else if (keycode == ob_keycode(OB_KEY_DOWN)) {
1750 menu_frame_select_next(frame);
1755 /* Use KeyRelease events for running things so that the key release
1756 doesn't get sent to the focused application.
1758 Allow ControlMask only, and don't bother if the menu is empty */
1759 else if (ev->type == KeyRelease && (state & ~ControlMask) == 0 &&
1760 frame->entries && frame->got_press)
1762 if (keycode == ob_keycode(OB_KEY_RETURN)) {
1763 /* Enter runs the active item or goes into the submenu.
1764 Control-Enter runs it without closing the menu. */
1766 menu_frame_select_next(frame->child);
1767 else if (frame->selected)
1768 menu_entry_frame_execute(frame->selected, state);
1773 /* keyboard accelerator shortcuts. (if it was a valid key) */
1774 else if (unikey != 0) {
1777 ObMenuEntryFrame *found = NULL;
1778 guint num_found = 0;
1780 /* start after the selected one */
1781 start = frame->entries;
1782 if (frame->selected) {
1783 for (it = start; frame->selected != it->data;
1784 it = g_list_next(it))
1785 g_assert(it != NULL); /* nothing was selected? */
1786 /* next with wraparound */
1787 start = g_list_next(it);
1788 if (start == NULL) start = frame->entries;
1793 ObMenuEntryFrame *e = it->data;
1794 gunichar entrykey = 0;
1796 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1797 entrykey = e->entry->data.normal.shortcut;
1798 else if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1799 entrykey = e->entry->data.submenu.submenu->shortcut;
1801 if (unikey == entrykey) {
1802 if (found == NULL) found = e;
1806 /* next with wraparound */
1807 it = g_list_next(it);
1808 if (it == NULL) it = frame->entries;
1809 } while (it != start);
1812 if (found->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1815 menu_frame_select(frame, found, TRUE);
1816 usleep(50000); /* highlight the item for a short bit so
1817 the user can see what happened */
1818 menu_entry_frame_execute(found, state);
1820 menu_frame_select(frame, found, TRUE);
1822 menu_frame_select_next(frame->child);
1834 static void event_handle_menu(ObMenuFrame *frame, XEvent *ev)
1837 ObMenuEntryFrame *e;
1841 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1842 if (e->ignore_enters)
1844 else if (!(f = find_active_menu()) ||
1846 f->parent == e->frame ||
1847 f->child == e->frame)
1848 menu_frame_select(e->frame, e, FALSE);
1852 /*ignore leaves when we're already in the window */
1853 if (ev->xcrossing.detail == NotifyInferior)
1856 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1857 (f = find_active_menu()) && f->selected == e &&
1858 e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1860 menu_frame_select(e->frame, NULL, FALSE);
1866 static void event_handle_user_input(ObClient *client, XEvent *e)
1868 g_assert(e->type == ButtonPress || e->type == ButtonRelease ||
1869 e->type == MotionNotify || e->type == KeyPress ||
1870 e->type == KeyRelease);
1872 if (menu_frame_visible) {
1873 if (event_handle_menu_input(e))
1874 /* don't use the event if the menu used it, but if the menu
1875 didn't use it and it's a keypress that is bound, it will
1876 close the menu and be used */
1880 /* if the keyboard interactive action uses the event then dont
1881 use it for bindings. likewise is moveresize uses the event. */
1882 if (!actions_interactive_input_event(e) && !moveresize_event(e)) {
1883 if (moveresize_in_progress)
1884 /* make further actions work on the client being
1886 client = moveresize_client;
1888 if (e->type == ButtonPress ||
1889 e->type == ButtonRelease ||
1890 e->type == MotionNotify)
1892 /* the frame may not be "visible" but they can still click on it
1893 in the case where it is animating before disappearing */
1894 if (!client || !frame_iconify_animating(client->frame))
1895 mouse_event(client, e);
1897 keyboard_event((focus_cycle_target ? focus_cycle_target :
1898 (client ? client : focus_client)), e);
1902 static void focus_delay_dest(gpointer data)
1907 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1909 const ObFocusDelayData *f1 = d1;
1910 return f1->client == d2;
1913 static gboolean focus_delay_func(gpointer data)
1915 ObFocusDelayData *d = data;
1916 Time old = event_curtime;
1918 /* don't move focus and kill the menu or the move/resize */
1919 if (menu_frame_visible || moveresize_in_progress) return FALSE;
1921 event_curtime = d->time;
1922 event_curserial = d->serial;
1923 if (client_focus(d->client) && config_focus_raise)
1924 stacking_raise(CLIENT_AS_WINDOW(d->client));
1925 event_curtime = old;
1926 return FALSE; /* no repeat */
1929 static void focus_delay_client_dest(ObClient *client, gpointer data)
1931 obt_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1935 void event_halt_focus_delay(void)
1937 /* ignore all enter events up till the event which caused this to occur */
1938 if (event_curserial) event_ignore_enter_range(1, event_curserial);
1939 obt_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1942 gulong event_start_ignore_all_enters(void)
1944 return NextRequest(obt_display);
1947 static void event_ignore_enter_range(gulong start, gulong end)
1951 g_assert(start != 0);
1954 r = g_new(ObSerialRange, 1);
1957 ignore_serials = g_slist_prepend(ignore_serials, r);
1959 ob_debug_type(OB_DEBUG_FOCUS, "ignoring enters from %lu until %lu",
1962 /* increment the serial so we don't ignore events we weren't meant to */
1963 OBT_PROP_ERASE(screen_support_win, MOTIF_WM_HINTS);
1966 void event_end_ignore_all_enters(gulong start)
1968 /* Use (NextRequest-1) so that we ignore up to the current serial only.
1969 Inside event_ignore_enter_range, we increment the serial by one, but if
1970 we ignore that serial too, then any enter events generated by mouse
1971 movement will be ignored until we create some further network traffic.
1972 Instead ignore up to NextRequest-1, then when we increment the serial,
1973 we will be *past* the range of ignored serials */
1974 event_ignore_enter_range(start, NextRequest(obt_display)-1);
1977 static gboolean is_enter_focus_event_ignored(XEvent *e)
1981 g_assert(e->type == EnterNotify &&
1982 !(e->xcrossing.mode == NotifyGrab ||
1983 e->xcrossing.mode == NotifyUngrab ||
1984 e->xcrossing.detail == NotifyInferior));
1986 for (it = ignore_serials; it; it = next) {
1987 ObSerialRange *r = it->data;
1989 next = g_slist_next(it);
1991 if ((glong)(e->xany.serial - r->end) > 0) {
1993 ignore_serials = g_slist_delete_link(ignore_serials, it);
1996 else if ((glong)(e->xany.serial - r->start) >= 0)
2002 void event_cancel_all_key_grabs(void)
2004 if (actions_interactive_act_running()) {
2005 actions_interactive_cancel_act();
2006 ob_debug("KILLED interactive action");
2008 else if (menu_frame_visible) {
2009 menu_frame_hide_all();
2010 ob_debug("KILLED open menus");
2012 else if (moveresize_in_progress) {
2013 moveresize_end(TRUE);
2014 ob_debug("KILLED interactive moveresize");
2016 else if (grab_on_keyboard()) {
2018 ob_debug("KILLED active grab on keyboard");
2021 ungrab_passive_key();
2023 XSync(obt_display, FALSE);
2026 gboolean event_time_after(Time t1, Time t2)
2028 g_assert(t1 != CurrentTime);
2029 g_assert(t2 != CurrentTime);
2032 Timestamp values wrap around (after about 49.7 days). The server, given
2033 its current time is represented by timestamp T, always interprets
2034 timestamps from clients by treating half of the timestamp space as being
2035 later in time than T.
2036 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
2039 /* TIME_HALF is half of the number space of a Time type variable */
2040 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
2042 if (t2 >= TIME_HALF)
2043 /* t2 is in the second half so t1 might wrap around and be smaller than
2045 return t1 >= t2 || t1 < (t2 + TIME_HALF);
2047 /* t2 is in the first half so t1 has to come after it */
2048 return t1 >= t2 && t1 < (t2 + TIME_HALF);
2051 Time event_get_server_time(void)
2053 /* Generate a timestamp */
2056 XChangeProperty(obt_display, screen_support_win,
2057 OBT_PROP_ATOM(WM_CLASS), OBT_PROP_ATOM(STRING),
2058 8, PropModeAppend, NULL, 0);
2059 XWindowEvent(obt_display, screen_support_win, PropertyChangeMask, &event);
2060 return event.xproperty.time;