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.
35 #include "menuframe.h"
41 #include "focus_cycle.h"
42 #include "moveresize.h"
45 #include "extensions.h"
46 #include "translate.h"
50 #include <X11/Xatom.h>
53 #ifdef HAVE_SYS_SELECT_H
54 # include <sys/select.h>
60 # include <unistd.h> /* for usleep() */
63 # include <X11/XKBlib.h>
67 #include <X11/ICE/ICElib.h>
84 gulong start; /* inclusive */
85 gulong end; /* inclusive */
88 static void event_process(const XEvent *e, gpointer data);
89 static void event_handle_root(XEvent *e);
90 static gboolean event_handle_menu_keyboard(XEvent *e);
91 static gboolean event_handle_menu(XEvent *e);
92 static gboolean event_handle_prompt(ObPrompt *p, XEvent *e);
93 static void event_handle_dock(ObDock *s, XEvent *e);
94 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
95 static void event_handle_client(ObClient *c, XEvent *e);
96 static void event_handle_user_input(ObClient *client, XEvent *e);
97 static gboolean is_enter_focus_event_ignored(gulong serial);
98 static void event_ignore_enter_range(gulong start, gulong end);
100 static void focus_delay_dest(gpointer data);
101 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
102 static gboolean focus_delay_func(gpointer data);
103 static void focus_delay_client_dest(ObClient *client, gpointer data);
105 Time event_curtime = CurrentTime;
106 Time event_last_user_time = CurrentTime;
107 /*! The serial of the current X event */
109 static gulong event_curserial;
110 static gboolean focus_left_screen = FALSE;
111 /*! A list of ObSerialRanges which are to be ignored for mouse enter events */
112 static GSList *ignore_serials = NULL;
115 static void ice_handler(gint fd, gpointer conn)
118 IceProcessMessages(conn, NULL, &b);
121 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
122 IcePointer *watch_data)
127 fd = IceConnectionNumber(conn);
128 ob_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
130 ob_main_loop_fd_remove(ob_main_loop, fd);
136 void event_startup(gboolean reconfig)
138 if (reconfig) return;
140 ob_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
143 IceAddConnectionWatch(ice_watch, NULL);
146 client_add_destroy_notify(focus_delay_client_dest, NULL);
149 void event_shutdown(gboolean reconfig)
151 if (reconfig) return;
154 IceRemoveConnectionWatch(ice_watch, NULL);
157 client_remove_destroy_notify(focus_delay_client_dest);
160 static Window event_get_window(XEvent *e)
167 window = RootWindow(ob_display, ob_screen);
170 window = e->xmap.window;
173 window = e->xunmap.window;
176 window = e->xdestroywindow.window;
178 case ConfigureRequest:
179 window = e->xconfigurerequest.window;
181 case ConfigureNotify:
182 window = e->xconfigure.window;
186 if (extensions_xkb && e->type == extensions_xkb_event_basep) {
187 switch (((XkbAnyEvent*)e)->xkb_type) {
189 window = ((XkbBellNotifyEvent*)e)->window;
196 if (extensions_sync &&
197 e->type == extensions_sync_event_basep + XSyncAlarmNotify)
202 window = e->xany.window;
207 static void event_set_curtime(XEvent *e)
209 Time t = CurrentTime;
211 /* grab the lasttime and hack up the state */
227 t = e->xproperty.time;
231 t = e->xcrossing.time;
235 if (extensions_sync &&
236 e->type == extensions_sync_event_basep + XSyncAlarmNotify)
238 t = ((XSyncAlarmNotifyEvent*)e)->time;
241 /* if more event types are anticipated, get their timestamp
246 /* watch that if we get an event earlier than the last specified user_time,
247 which can happen if the clock goes backwards, we erase the last
248 specified user_time */
249 if (t && event_last_user_time && event_time_after(event_last_user_time, t))
250 event_last_user_time = CurrentTime;
255 static void event_hack_mods(XEvent *e)
260 e->xbutton.state = modkeys_only_modifier_masks(e->xbutton.state);
263 e->xkey.state = modkeys_only_modifier_masks(e->xkey.state);
267 /* keep only the keyboard modifiers. xkb includes other things here.
268 (see XKBProto.pdf document: section 2.2.2) */
269 e->xkey.state &= 0xf;
271 e->xkey.state = modkeys_only_modifier_masks(e->xkey.state);
272 /* remove from the state the mask of the modifier key being
273 released, if it is a modifier key being released that is */
274 e->xkey.state &= ~modkeys_keycode_to_mask(e->xkey.keycode);
277 e->xmotion.state = modkeys_only_modifier_masks(e->xmotion.state);
278 /* compress events */
281 while (XCheckTypedWindowEvent(ob_display, e->xmotion.window,
283 e->xmotion.x = ce.xmotion.x;
284 e->xmotion.y = ce.xmotion.y;
285 e->xmotion.x_root = ce.xmotion.x_root;
286 e->xmotion.y_root = ce.xmotion.y_root;
293 static gboolean wanted_focusevent(XEvent *e, gboolean in_client_only)
295 gint mode = e->xfocus.mode;
296 gint detail = e->xfocus.detail;
297 Window win = e->xany.window;
299 if (e->type == FocusIn) {
300 /* These are ones we never want.. */
302 /* This means focus was given by a keyboard/mouse grab. */
303 if (mode == NotifyGrab)
305 /* This means focus was given back from a keyboard/mouse grab. */
306 if (mode == NotifyUngrab)
309 /* These are the ones we want.. */
311 if (win == RootWindow(ob_display, ob_screen)) {
312 /* If looking for a focus in on a client, then always return
313 FALSE for focus in's to the root window */
316 /* This means focus reverted off of a client */
317 else if (detail == NotifyPointerRoot ||
318 detail == NotifyDetailNone ||
319 detail == NotifyInferior ||
320 /* This means focus got here from another screen */
321 detail == NotifyNonlinear)
327 /* It was on a client, was it a valid one?
328 It's possible to get a FocusIn event for a client that was managed
331 if (in_client_only) {
332 ObWindow *w = g_hash_table_lookup(window_map, &e->xfocus.window);
333 if (!w || !WINDOW_IS_CLIENT(w))
337 /* This means focus reverted to parent from the client (this
338 happens often during iconify animation) */
339 if (detail == NotifyInferior)
343 /* This means focus moved from the root window to a client */
344 if (detail == NotifyVirtual)
346 /* This means focus moved from one client to another */
347 if (detail == NotifyNonlinearVirtual)
353 g_assert(e->type == FocusOut);
355 /* These are ones we never want.. */
357 /* This means focus was taken by a keyboard/mouse grab. */
358 if (mode == NotifyGrab)
360 /* This means focus was grabbed on a window and it was released. */
361 if (mode == NotifyUngrab)
364 /* Focus left the root window revertedto state */
365 if (win == RootWindow(ob_display, ob_screen))
368 /* These are the ones we want.. */
370 /* This means focus moved from a client to the root window */
371 if (detail == NotifyVirtual)
373 /* This means focus moved from one client to another */
374 if (detail == NotifyNonlinearVirtual)
382 static Bool event_look_for_focusin(Display *d, XEvent *e, XPointer arg)
384 return e->type == FocusIn && wanted_focusevent(e, FALSE);
387 static Bool event_look_for_focusin_client(Display *d, XEvent *e, XPointer arg)
389 return e->type == FocusIn && wanted_focusevent(e, TRUE);
392 static void print_focusevent(XEvent *e)
394 gint mode = e->xfocus.mode;
395 gint detail = e->xfocus.detail;
396 Window win = e->xany.window;
397 const gchar *modestr, *detailstr;
400 case NotifyNormal: modestr="NotifyNormal"; break;
401 case NotifyGrab: modestr="NotifyGrab"; break;
402 case NotifyUngrab: modestr="NotifyUngrab"; break;
403 case NotifyWhileGrabbed: modestr="NotifyWhileGrabbed"; break;
406 case NotifyAncestor: detailstr="NotifyAncestor"; break;
407 case NotifyVirtual: detailstr="NotifyVirtual"; break;
408 case NotifyInferior: detailstr="NotifyInferior"; break;
409 case NotifyNonlinear: detailstr="NotifyNonlinear"; break;
410 case NotifyNonlinearVirtual: detailstr="NotifyNonlinearVirtual"; break;
411 case NotifyPointer: detailstr="NotifyPointer"; break;
412 case NotifyPointerRoot: detailstr="NotifyPointerRoot"; break;
413 case NotifyDetailNone: detailstr="NotifyDetailNone"; break;
416 if (mode == NotifyGrab || mode == NotifyUngrab)
421 ob_debug_type(OB_DEBUG_FOCUS, "Focus%s 0x%x mode=%s detail=%s\n",
422 (e->xfocus.type == FocusIn ? "In" : "Out"),
428 static gboolean event_ignore(XEvent *e, ObClient *client)
433 if (!wanted_focusevent(e, FALSE))
438 if (!wanted_focusevent(e, FALSE))
445 static void event_process(const XEvent *ec, gpointer data)
448 ObClient *client = NULL;
450 ObDockApp *dockapp = NULL;
451 ObWindow *obwin = NULL;
453 ObEventData *ed = data;
454 ObPrompt *prompt = NULL;
456 /* make a copy we can mangle */
460 window = event_get_window(e);
461 if ((obwin = g_hash_table_lookup(window_map, &window))) {
462 switch (obwin->type) {
464 dock = WINDOW_AS_DOCK(obwin);
467 dockapp = WINDOW_AS_DOCKAPP(obwin);
470 client = WINDOW_AS_CLIENT(obwin);
471 /* events on clients can be events on prompt windows too */
472 prompt = client->prompt;
475 /* not to be used for events */
476 g_assert_not_reached();
478 case Window_Internal:
479 /* we don't do anything with events directly on these windows */
482 prompt = WINDOW_AS_PROMPT(obwin);
487 event_set_curtime(e);
488 event_curserial = e->xany.serial;
490 if (event_ignore(e, client)) {
497 /* deal with it in the kernel */
499 if (menu_frame_visible &&
500 (e->type == EnterNotify || e->type == LeaveNotify))
502 /* crossing events for menu */
503 event_handle_menu(e);
504 } else if (e->type == FocusIn) {
506 e->xfocus.detail == NotifyInferior)
508 ob_debug_type(OB_DEBUG_FOCUS,
509 "Focus went to the frame window");
511 focus_left_screen = FALSE;
513 focus_fallback(FALSE, config_focus_under_mouse, TRUE, TRUE);
515 /* We don't get a FocusOut for this case, because it's just moving
516 from our Inferior up to us. This happens when iconifying a
517 window with RevertToParent focus */
518 frame_adjust_focus(client->frame, FALSE);
519 /* focus_set_client(NULL) has already been called */
521 else if (e->xfocus.detail == NotifyPointerRoot ||
522 e->xfocus.detail == NotifyDetailNone ||
523 e->xfocus.detail == NotifyInferior ||
524 e->xfocus.detail == NotifyNonlinear)
528 ob_debug_type(OB_DEBUG_FOCUS,
529 "Focus went to root or pointer root/none\n");
531 if (e->xfocus.detail == NotifyInferior ||
532 e->xfocus.detail == NotifyNonlinear)
534 focus_left_screen = FALSE;
537 /* If another FocusIn is in the queue then don't fallback yet. This
538 fixes the fun case of:
539 window map -> send focusin
540 window unmap -> get focusout
541 window map -> send focusin
542 get first focus out -> fall back to something (new window
543 hasn't received focus yet, so something else) -> send focusin
544 which means the "something else" is the last thing to get a
545 focusin sent to it, so the new window doesn't end up with focus.
547 But if the other focus in is something like PointerRoot then we
548 still want to fall back.
550 if (XCheckIfEvent(ob_display, &ce, event_look_for_focusin_client,
553 XPutBackEvent(ob_display, &ce);
554 ob_debug_type(OB_DEBUG_FOCUS,
555 " but another FocusIn is coming\n");
557 /* Focus has been reverted.
559 FocusOut events come after UnmapNotify, so we don't need to
560 worry about focusing an invalid window
563 if (!focus_left_screen)
564 focus_fallback(FALSE, config_focus_under_mouse,
570 ob_debug_type(OB_DEBUG_FOCUS,
571 "Focus went to a window that is already gone\n");
573 /* If you send focus to a window and then it disappears, you can
574 get the FocusIn for it, after it is unmanaged.
575 Just wait for the next FocusOut/FocusIn pair, but make note that
576 the window that was focused no longer is. */
577 focus_set_client(NULL);
579 else if (client != focus_client) {
580 focus_left_screen = FALSE;
581 frame_adjust_focus(client->frame, TRUE);
582 focus_set_client(client);
583 client_calc_layer(client);
584 client_bring_helper_windows(client);
586 } else if (e->type == FocusOut) {
589 /* Look for the followup FocusIn */
590 if (!XCheckIfEvent(ob_display, &ce, event_look_for_focusin, NULL)) {
591 /* There is no FocusIn, this means focus went to a window that
592 is not being managed, or a window on another screen. */
596 xerror_set_ignore(TRUE);
597 if (XGetInputFocus(ob_display, &win, &i) != 0 &&
598 XGetGeometry(ob_display, win, &root, &i,&i,&u,&u,&u,&u) != 0 &&
599 root != RootWindow(ob_display, ob_screen))
601 ob_debug_type(OB_DEBUG_FOCUS,
602 "Focus went to another screen !\n");
603 focus_left_screen = TRUE;
606 ob_debug_type(OB_DEBUG_FOCUS,
607 "Focus went to a black hole !\n");
608 xerror_set_ignore(FALSE);
609 /* nothing is focused */
610 focus_set_client(NULL);
612 /* Focus moved, so process the FocusIn event */
613 ObEventData ed = { .ignored = FALSE };
614 event_process(&ce, &ed);
616 /* The FocusIn was ignored, this means it was on a window
617 that isn't a client. */
618 ob_debug_type(OB_DEBUG_FOCUS,
619 "Focus went to an unmanaged window 0x%x !\n",
621 focus_fallback(TRUE, config_focus_under_mouse, TRUE, TRUE);
625 if (client && client != focus_client) {
626 frame_adjust_focus(client->frame, FALSE);
627 /* focus_set_client(NULL) has already been called in this
628 section or by focus_fallback */
632 event_handle_client(client, e);
634 event_handle_dockapp(dockapp, e);
636 event_handle_dock(dock, e);
637 else if (window == RootWindow(ob_display, ob_screen))
638 event_handle_root(e);
639 else if (e->type == MapRequest)
640 client_manage(window, NULL);
641 else if (e->type == MappingNotify) {
642 /* keyboard layout changes for modifier mapping changes. reload the
643 modifier map, and rebind all the key bindings as appropriate */
644 ob_debug("Keyboard map changed. Reloading keyboard bindings.\n");
645 XRefreshKeyboardMapping(&e->xmapping);
646 ob_set_state(OB_STATE_RECONFIGURING);
647 modkeys_shutdown(TRUE);
648 modkeys_startup(TRUE);
650 ob_set_state(OB_STATE_RUNNING);
652 else if (e->type == ClientMessage) {
653 /* This is for _NET_WM_REQUEST_FRAME_EXTENTS messages. They come for
654 windows that are not managed yet. */
655 if (e->xclient.message_type == prop_atoms.net_request_frame_extents) {
656 /* Pretend to manage the client, getting information used to
657 determine its decorations */
658 ObClient *c = client_fake_manage(e->xclient.window);
661 /* set the frame extents on the window */
662 vals[0] = c->frame->size.left;
663 vals[1] = c->frame->size.right;
664 vals[2] = c->frame->size.top;
665 vals[3] = c->frame->size.bottom;
666 PROP_SETA32(e->xclient.window, net_frame_extents,
669 /* Free the pretend client */
670 client_fake_unmanage(c);
673 else if (e->type == ConfigureRequest) {
674 /* unhandled configure requests must be used to configure the
678 xwc.x = e->xconfigurerequest.x;
679 xwc.y = e->xconfigurerequest.y;
680 xwc.width = e->xconfigurerequest.width;
681 xwc.height = e->xconfigurerequest.height;
682 xwc.border_width = e->xconfigurerequest.border_width;
683 xwc.sibling = e->xconfigurerequest.above;
684 xwc.stack_mode = e->xconfigurerequest.detail;
686 /* we are not to be held responsible if someone sends us an
688 xerror_set_ignore(TRUE);
689 XConfigureWindow(ob_display, window,
690 e->xconfigurerequest.value_mask, &xwc);
691 xerror_set_ignore(FALSE);
694 else if (extensions_sync &&
695 e->type == extensions_sync_event_basep + XSyncAlarmNotify)
697 XSyncAlarmNotifyEvent *se = (XSyncAlarmNotifyEvent*)e;
698 if (se->alarm == moveresize_alarm && moveresize_in_progress)
703 if (prompt && event_handle_prompt(prompt, e))
705 else if (e->type == ButtonPress || e->type == ButtonRelease) {
706 /* If the button press was on some non-root window, or was physically
707 on the root window, then process it */
708 if (window != RootWindow(ob_display, ob_screen) ||
709 e->xbutton.subwindow == None)
711 event_handle_user_input(client, e);
713 /* Otherwise only process it if it was physically on an openbox
718 if ((w = g_hash_table_lookup(window_map, &e->xbutton.subwindow)) &&
719 WINDOW_IS_INTERNAL(w))
721 event_handle_user_input(client, e);
725 else if (e->type == KeyPress || e->type == KeyRelease ||
726 e->type == MotionNotify)
727 event_handle_user_input(client, e);
729 /* if something happens and it's not from an XEvent, then we don't know
731 event_curtime = CurrentTime;
735 static void event_handle_root(XEvent *e)
741 ob_debug("Another WM has requested to replace us. Exiting.\n");
746 if (e->xclient.format != 32) break;
748 msgtype = e->xclient.message_type;
749 if (msgtype == prop_atoms.net_current_desktop) {
750 guint d = e->xclient.data.l[0];
751 if (d < screen_num_desktops) {
752 event_curtime = e->xclient.data.l[1];
753 if (event_curtime == 0)
754 ob_debug_type(OB_DEBUG_APP_BUGS,
755 "_NET_CURRENT_DESKTOP message is missing "
757 screen_set_desktop(d, TRUE);
759 } else if (msgtype == prop_atoms.net_number_of_desktops) {
760 guint d = e->xclient.data.l[0];
761 if (d > 0 && d <= 1000)
762 screen_set_num_desktops(d);
763 } else if (msgtype == prop_atoms.net_showing_desktop) {
764 screen_show_desktop(e->xclient.data.l[0] != 0, NULL);
765 } else if (msgtype == prop_atoms.ob_control) {
766 ob_debug("OB_CONTROL: %d\n", e->xclient.data.l[0]);
767 if (e->xclient.data.l[0] == 1)
769 else if (e->xclient.data.l[0] == 2)
771 else if (e->xclient.data.l[0] == 3)
773 } else if (msgtype == prop_atoms.wm_protocols) {
774 if ((Atom)e->xclient.data.l[0] == prop_atoms.net_wm_ping)
775 ping_got_pong(e->xclient.data.l[1]);
779 if (e->xproperty.atom == prop_atoms.net_desktop_names) {
780 ob_debug("UPDATE DESKTOP NAMES\n");
781 screen_update_desktop_names();
783 else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
784 screen_update_layout();
786 case ConfigureNotify:
788 XRRUpdateConfiguration(e);
797 void event_enter_client(ObClient *client)
799 g_assert(config_focus_follow);
801 if (is_enter_focus_event_ignored(event_curserial)) {
802 ob_debug_type(OB_DEBUG_FOCUS, "Ignoring enter event with serial %lu\n"
803 "on client 0x%x", event_curserial, client->window);
807 if (client_enter_focusable(client) && client_can_focus(client)) {
808 if (config_focus_delay) {
809 ObFocusDelayData *data;
811 ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
813 data = g_new(ObFocusDelayData, 1);
814 data->client = client;
815 data->time = event_curtime;
816 data->serial = event_curserial;
818 ob_main_loop_timeout_add(ob_main_loop,
819 config_focus_delay * 1000,
821 data, focus_delay_cmp, focus_delay_dest);
823 ObFocusDelayData data;
824 data.client = client;
825 data.time = event_curtime;
826 data.serial = event_curserial;
827 focus_delay_func(&data);
832 static gboolean *context_to_button(ObFrame *f, ObFrameContext con, gboolean press)
836 case OB_FRAME_CONTEXT_MAXIMIZE:
837 return &f->max_press;
838 case OB_FRAME_CONTEXT_CLOSE:
839 return &f->close_press;
840 case OB_FRAME_CONTEXT_ICONIFY:
841 return &f->iconify_press;
842 case OB_FRAME_CONTEXT_ALLDESKTOPS:
843 return &f->desk_press;
844 case OB_FRAME_CONTEXT_SHADE:
845 return &f->shade_press;
851 case OB_FRAME_CONTEXT_MAXIMIZE:
852 return &f->max_hover;
853 case OB_FRAME_CONTEXT_CLOSE:
854 return &f->close_hover;
855 case OB_FRAME_CONTEXT_ICONIFY:
856 return &f->iconify_hover;
857 case OB_FRAME_CONTEXT_ALLDESKTOPS:
858 return &f->desk_hover;
859 case OB_FRAME_CONTEXT_SHADE:
860 return &f->shade_hover;
867 static void compress_client_message_event(XEvent *e, XEvent *ce, Window window,
870 /* compress changes into a single change */
871 while (XCheckTypedWindowEvent(ob_display, window, e->type, ce)) {
872 /* XXX: it would be nice to compress ALL messages of a
873 type, not just messages in a row without other
874 message types between. */
875 if (ce->xclient.message_type != msgtype) {
876 XPutBackEvent(ob_display, ce);
879 e->xclient = ce->xclient;
883 static void event_handle_client(ObClient *client, XEvent *e)
889 static gint px = -1, py = -1;
891 static ObFrameContext pcon = OB_FRAME_CONTEXT_NONE;
895 /* save where the press occured for the first button pressed */
897 pb = e->xbutton.button;
901 pcon = frame_context(client, e->xbutton.window, px, py);
902 pcon = mouse_button_frame_context(pcon, e->xbutton.button,
906 /* Wheel buttons don't draw because they are an instant click, so it
907 is a waste of resources to go drawing it.
908 if the user is doing an interactive thing, or has a menu open then
909 the mouse is grabbed (possibly) and if we get these events we don't
910 want to deal with them
912 if (!(e->xbutton.button == 4 || e->xbutton.button == 5) &&
915 /* use where the press occured */
916 con = frame_context(client, e->xbutton.window, px, py);
917 con = mouse_button_frame_context(con, e->xbutton.button,
920 /* button presses on CLIENT_CONTEXTs are not accompanied by a
921 release because they are Replayed to the client */
922 if ((e->type == ButtonRelease || CLIENT_CONTEXT(con, client)) &&
923 e->xbutton.button == pb)
924 pb = 0, px = py = -1, pcon = OB_FRAME_CONTEXT_NONE;
926 but = context_to_button(client->frame, con, TRUE);
928 *but = (e->type == ButtonPress);
929 frame_adjust_state(client->frame);
934 /* when there is a grab on the pointer, we won't get enter/leave
935 notifies, but we still get motion events */
936 if (grab_on_pointer()) break;
938 con = frame_context(client, e->xmotion.window,
939 e->xmotion.x, e->xmotion.y);
941 case OB_FRAME_CONTEXT_TITLEBAR:
942 case OB_FRAME_CONTEXT_TLCORNER:
943 case OB_FRAME_CONTEXT_TRCORNER:
944 /* we've left the button area inside the titlebar */
945 if (client->frame->max_hover || client->frame->desk_hover ||
946 client->frame->shade_hover || client->frame->iconify_hover ||
947 client->frame->close_hover)
949 client->frame->max_hover =
950 client->frame->desk_hover =
951 client->frame->shade_hover =
952 client->frame->iconify_hover =
953 client->frame->close_hover = FALSE;
954 frame_adjust_state(client->frame);
958 but = context_to_button(client->frame, con, FALSE);
959 if (but && !*but && !pb) {
961 frame_adjust_state(client->frame);
967 con = frame_context(client, e->xcrossing.window,
968 e->xcrossing.x, e->xcrossing.y);
970 case OB_FRAME_CONTEXT_TITLEBAR:
971 case OB_FRAME_CONTEXT_TLCORNER:
972 case OB_FRAME_CONTEXT_TRCORNER:
973 /* we've left the button area inside the titlebar */
974 client->frame->max_hover =
975 client->frame->desk_hover =
976 client->frame->shade_hover =
977 client->frame->iconify_hover =
978 client->frame->close_hover = FALSE;
979 if (e->xcrossing.mode == NotifyGrab) {
980 client->frame->max_press =
981 client->frame->desk_press =
982 client->frame->shade_press =
983 client->frame->iconify_press =
984 client->frame->close_press = FALSE;
987 case OB_FRAME_CONTEXT_FRAME:
988 /* When the mouse leaves an animating window, don't use the
989 corresponding enter events. Pretend like the animating window
990 doesn't even exist..! */
991 if (frame_iconify_animating(client->frame))
992 event_end_ignore_all_enters(event_start_ignore_all_enters());
994 ob_debug_type(OB_DEBUG_FOCUS,
995 "%sNotify mode %d detail %d on %lx\n",
996 (e->type == EnterNotify ? "Enter" : "Leave"),
998 e->xcrossing.detail, (client?client->window:0));
999 if (grab_on_keyboard())
1001 if (config_focus_follow && config_focus_delay &&
1002 /* leave inferior events can happen when the mouse goes onto
1003 the window's border and then into the window before the
1005 e->xcrossing.detail != NotifyInferior)
1007 ob_main_loop_timeout_remove_data(ob_main_loop,
1013 but = context_to_button(client->frame, con, FALSE);
1016 if (e->xcrossing.mode == NotifyGrab) {
1017 but = context_to_button(client->frame, con, TRUE);
1020 frame_adjust_state(client->frame);
1027 con = frame_context(client, e->xcrossing.window,
1028 e->xcrossing.x, e->xcrossing.y);
1030 case OB_FRAME_CONTEXT_FRAME:
1031 if (grab_on_keyboard())
1033 if (e->xcrossing.mode == NotifyGrab ||
1034 e->xcrossing.mode == NotifyUngrab ||
1035 /*ignore enters when we're already in the window */
1036 e->xcrossing.detail == NotifyInferior)
1038 ob_debug_type(OB_DEBUG_FOCUS,
1039 "%sNotify mode %d detail %d serial %lu on %lx "
1041 (e->type == EnterNotify ? "Enter" : "Leave"),
1043 e->xcrossing.detail,
1044 e->xcrossing.serial,
1045 client?client->window:0);
1048 ob_debug_type(OB_DEBUG_FOCUS,
1049 "%sNotify mode %d detail %d serial %lu on %lx, "
1050 "focusing window\n",
1051 (e->type == EnterNotify ? "Enter" : "Leave"),
1053 e->xcrossing.detail,
1054 e->xcrossing.serial,
1055 (client?client->window:0));
1056 if (config_focus_follow)
1057 event_enter_client(client);
1061 but = context_to_button(client->frame, con, FALSE);
1064 if (e->xcrossing.mode == NotifyUngrab) {
1065 but = context_to_button(client->frame, con, TRUE);
1066 *but = (con == pcon);
1068 frame_adjust_state(client->frame);
1074 case ConfigureRequest:
1076 /* dont compress these unless you're going to watch for property
1077 notifies in between (these can change what the configure would
1079 also you can't compress stacking events
1083 gboolean move = FALSE;
1084 gboolean resize = FALSE;
1086 /* get the current area */
1087 RECT_TO_DIMS(client->area, x, y, w, h);
1089 ob_debug("ConfigureRequest for \"%s\" desktop %d wmstate %d "
1091 " x %d y %d w %d h %d b %d\n",
1093 screen_desktop, client->wmstate, client->frame->visible,
1094 x, y, w, h, client->border_width);
1096 if (e->xconfigurerequest.value_mask & CWBorderWidth)
1097 if (client->border_width != e->xconfigurerequest.border_width) {
1098 client->border_width = e->xconfigurerequest.border_width;
1100 /* if the border width is changing then that is the same
1101 as requesting a resize, but we don't actually change
1102 the client's border, so it will change their root
1103 coordinates (since they include the border width) and
1104 we need to a notify then */
1108 if (e->xconfigurerequest.value_mask & CWStackMode) {
1109 ObClient *sibling = NULL;
1110 gulong ignore_start;
1113 /* get the sibling */
1114 if (e->xconfigurerequest.value_mask & CWSibling) {
1116 win = g_hash_table_lookup(window_map,
1117 &e->xconfigurerequest.above);
1118 if (win && WINDOW_IS_CLIENT(win) &&
1119 WINDOW_AS_CLIENT(win) != client)
1121 sibling = WINDOW_AS_CLIENT(win);
1124 /* an invalid sibling was specified so don't restack at
1125 all, it won't make sense no matter what we do */
1130 if (!config_focus_under_mouse)
1131 ignore_start = event_start_ignore_all_enters();
1132 stacking_restack_request(client, sibling,
1133 e->xconfigurerequest.detail);
1134 if (!config_focus_under_mouse)
1135 event_end_ignore_all_enters(ignore_start);
1138 /* a stacking change moves the window without resizing */
1142 if ((e->xconfigurerequest.value_mask & CWX) ||
1143 (e->xconfigurerequest.value_mask & CWY) ||
1144 (e->xconfigurerequest.value_mask & CWWidth) ||
1145 (e->xconfigurerequest.value_mask & CWHeight))
1147 /* don't allow clients to move shaded windows (fvwm does this)
1149 if (e->xconfigurerequest.value_mask & CWX) {
1150 if (!client->shaded)
1151 x = e->xconfigurerequest.x;
1154 if (e->xconfigurerequest.value_mask & CWY) {
1155 if (!client->shaded)
1156 y = e->xconfigurerequest.y;
1160 if (e->xconfigurerequest.value_mask & CWWidth) {
1161 w = e->xconfigurerequest.width;
1164 if (e->xconfigurerequest.value_mask & CWHeight) {
1165 h = e->xconfigurerequest.height;
1170 ob_debug("ConfigureRequest x(%d) %d y(%d) %d w(%d) %d h(%d) %d "
1171 "move %d resize %d\n",
1172 e->xconfigurerequest.value_mask & CWX, x,
1173 e->xconfigurerequest.value_mask & CWY, y,
1174 e->xconfigurerequest.value_mask & CWWidth, w,
1175 e->xconfigurerequest.value_mask & CWHeight, h,
1178 /* check for broken apps moving to their root position
1180 XXX remove this some day...that would be nice. right now all
1181 kde apps do this when they try activate themselves on another
1182 desktop. eg. open amarok window on desktop 1, switch to desktop
1183 2, click amarok tray icon. it will move by its decoration size.
1185 if (x != client->area.x &&
1186 x == (client->frame->area.x + client->frame->size.left -
1187 (gint)client->border_width) &&
1188 y != client->area.y &&
1189 y == (client->frame->area.y + client->frame->size.top -
1190 (gint)client->border_width) &&
1191 w == client->area.width &&
1192 h == client->area.height)
1194 ob_debug_type(OB_DEBUG_APP_BUGS,
1195 "Application %s is trying to move via "
1196 "ConfigureRequest to it's root window position "
1197 "but it is not using StaticGravity\n",
1203 /* they still requested a move, so don't change whether a
1204 notify is sent or not */
1210 client_try_configure(client, &x, &y, &w, &h, &lw, &lh, FALSE);
1212 /* if x was not given, then use gravity to figure out the new
1213 x. the reference point should not be moved */
1214 if ((e->xconfigurerequest.value_mask & CWWidth &&
1215 !(e->xconfigurerequest.value_mask & CWX)))
1216 client_gravity_resize_w(client, &x, client->area.width, w);
1218 if ((e->xconfigurerequest.value_mask & CWHeight &&
1219 !(e->xconfigurerequest.value_mask & CWY)))
1220 client_gravity_resize_h(client, &y, client->area.height,h);
1222 client_find_onscreen(client, &x, &y, w, h, FALSE);
1224 ob_debug("Granting ConfigureRequest x %d y %d w %d h %d\n",
1226 client_configure(client, x, y, w, h, FALSE, TRUE, TRUE);
1231 if (client->ignore_unmaps) {
1232 client->ignore_unmaps--;
1235 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
1236 "ignores left %d\n",
1237 client->window, e->xunmap.event, e->xunmap.from_configure,
1238 client->ignore_unmaps);
1239 client_unmanage(client);
1242 ob_debug("DestroyNotify for window 0x%x\n", client->window);
1243 client_unmanage(client);
1245 case ReparentNotify:
1246 /* this is when the client is first taken captive in the frame */
1247 if (e->xreparent.parent == client->frame->window) break;
1250 This event is quite rare and is usually handled in unmapHandler.
1251 However, if the window is unmapped when the reparent event occurs,
1252 the window manager never sees it because an unmap event is not sent
1253 to an already unmapped window.
1256 /* we don't want the reparent event, put it back on the stack for the
1257 X server to deal with after we unmanage the window */
1258 XPutBackEvent(ob_display, e);
1260 ob_debug("ReparentNotify for window 0x%x\n", client->window);
1261 client_unmanage(client);
1264 ob_debug("MapRequest for 0x%lx\n", client->window);
1265 if (!client->iconic) break; /* this normally doesn't happen, but if it
1266 does, we don't want it!
1267 it can happen now when the window is on
1268 another desktop, but we still don't
1270 client_activate(client, FALSE, FALSE, TRUE, TRUE, TRUE);
1273 /* validate cuz we query stuff off the client here */
1274 if (!client_validate(client)) break;
1276 if (e->xclient.format != 32) return;
1278 msgtype = e->xclient.message_type;
1279 if (msgtype == prop_atoms.wm_change_state) {
1280 compress_client_message_event(e, &ce, client->window, msgtype);
1281 client_set_wm_state(client, e->xclient.data.l[0]);
1282 } else if (msgtype == prop_atoms.net_wm_desktop) {
1283 compress_client_message_event(e, &ce, client->window, msgtype);
1284 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
1285 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
1286 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
1288 } else if (msgtype == prop_atoms.net_wm_state) {
1289 gulong ignore_start;
1291 /* can't compress these */
1292 ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
1293 (e->xclient.data.l[0] == 0 ? "Remove" :
1294 e->xclient.data.l[0] == 1 ? "Add" :
1295 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1296 e->xclient.data.l[1], e->xclient.data.l[2],
1299 /* ignore enter events caused by these like ob actions do */
1300 if (!config_focus_under_mouse)
1301 ignore_start = event_start_ignore_all_enters();
1302 client_set_state(client, e->xclient.data.l[0],
1303 e->xclient.data.l[1], e->xclient.data.l[2]);
1304 if (!config_focus_under_mouse)
1305 event_end_ignore_all_enters(ignore_start);
1306 } else if (msgtype == prop_atoms.net_close_window) {
1307 ob_debug("net_close_window for 0x%lx\n", client->window);
1308 client_close(client);
1309 } else if (msgtype == prop_atoms.net_active_window) {
1310 ob_debug("net_active_window for 0x%lx source=%s\n",
1312 (e->xclient.data.l[0] == 0 ? "unknown" :
1313 (e->xclient.data.l[0] == 1 ? "application" :
1314 (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1315 /* XXX make use of data.l[2] !? */
1316 if (e->xclient.data.l[0] == 1 || e->xclient.data.l[0] == 2) {
1317 /* we can not trust the timestamp from applications.
1318 e.g. chromium passes a very old timestamp. openbox thinks
1319 the window will get focus and calls XSetInputFocus with the
1320 (old) timestamp, which doesn't end up moving focus at all.
1321 but the window is raised, not hilited, etc, as if it was
1322 really going to get focus.
1324 so do not use this timestamp in event_curtime, as this would
1325 be used in XSetInputFocus.
1327 /*event_curtime = e->xclient.data.l[1];*/
1328 if (e->xclient.data.l[1] == 0)
1329 ob_debug_type(OB_DEBUG_APP_BUGS,
1330 "_NET_ACTIVE_WINDOW message for window %s is"
1331 " missing a timestamp\n", client->title);
1333 event_curtime = event_get_server_time();
1335 ob_debug_type(OB_DEBUG_APP_BUGS,
1336 "_NET_ACTIVE_WINDOW message for window %s is "
1337 "missing source indication\n", client->title);
1338 client_activate(client, FALSE, FALSE, TRUE, TRUE,
1339 (e->xclient.data.l[0] == 0 ||
1340 e->xclient.data.l[0] == 2));
1341 } else if (msgtype == prop_atoms.net_wm_moveresize) {
1342 ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1343 client->window, e->xclient.data.l[2]);
1344 if ((Atom)e->xclient.data.l[2] ==
1345 prop_atoms.net_wm_moveresize_size_topleft ||
1346 (Atom)e->xclient.data.l[2] ==
1347 prop_atoms.net_wm_moveresize_size_top ||
1348 (Atom)e->xclient.data.l[2] ==
1349 prop_atoms.net_wm_moveresize_size_topright ||
1350 (Atom)e->xclient.data.l[2] ==
1351 prop_atoms.net_wm_moveresize_size_right ||
1352 (Atom)e->xclient.data.l[2] ==
1353 prop_atoms.net_wm_moveresize_size_right ||
1354 (Atom)e->xclient.data.l[2] ==
1355 prop_atoms.net_wm_moveresize_size_bottomright ||
1356 (Atom)e->xclient.data.l[2] ==
1357 prop_atoms.net_wm_moveresize_size_bottom ||
1358 (Atom)e->xclient.data.l[2] ==
1359 prop_atoms.net_wm_moveresize_size_bottomleft ||
1360 (Atom)e->xclient.data.l[2] ==
1361 prop_atoms.net_wm_moveresize_size_left ||
1362 (Atom)e->xclient.data.l[2] ==
1363 prop_atoms.net_wm_moveresize_move ||
1364 (Atom)e->xclient.data.l[2] ==
1365 prop_atoms.net_wm_moveresize_size_keyboard ||
1366 (Atom)e->xclient.data.l[2] ==
1367 prop_atoms.net_wm_moveresize_move_keyboard) {
1369 moveresize_start(client, e->xclient.data.l[0],
1370 e->xclient.data.l[1], e->xclient.data.l[3],
1371 e->xclient.data.l[2]);
1373 else if ((Atom)e->xclient.data.l[2] ==
1374 prop_atoms.net_wm_moveresize_cancel)
1375 moveresize_end(TRUE);
1376 } else if (msgtype == prop_atoms.net_moveresize_window) {
1377 gint ograv, x, y, w, h;
1379 ograv = client->gravity;
1381 if (e->xclient.data.l[0] & 0xff)
1382 client->gravity = e->xclient.data.l[0] & 0xff;
1384 if (e->xclient.data.l[0] & 1 << 8)
1385 x = e->xclient.data.l[1];
1388 if (e->xclient.data.l[0] & 1 << 9)
1389 y = e->xclient.data.l[2];
1393 if (e->xclient.data.l[0] & 1 << 10) {
1394 w = e->xclient.data.l[3];
1396 /* if x was not given, then use gravity to figure out the new
1397 x. the reference point should not be moved */
1398 if (!(e->xclient.data.l[0] & 1 << 8))
1399 client_gravity_resize_w(client, &x, client->area.width, w);
1402 w = client->area.width;
1404 if (e->xclient.data.l[0] & 1 << 11) {
1405 h = e->xclient.data.l[4];
1408 if (!(e->xclient.data.l[0] & 1 << 9))
1409 client_gravity_resize_h(client, &y, client->area.height,h);
1412 h = client->area.height;
1414 ob_debug("MOVERESIZE x %d %d y %d %d (gravity %d)\n",
1415 e->xclient.data.l[0] & 1 << 8, x,
1416 e->xclient.data.l[0] & 1 << 9, y,
1419 client_find_onscreen(client, &x, &y, w, h, FALSE);
1421 client_configure(client, x, y, w, h, FALSE, TRUE, FALSE);
1423 client->gravity = ograv;
1424 } else if (msgtype == prop_atoms.net_restack_window) {
1425 if (e->xclient.data.l[0] != 2) {
1426 ob_debug_type(OB_DEBUG_APP_BUGS,
1427 "_NET_RESTACK_WINDOW sent for window %s with "
1428 "invalid source indication %ld\n",
1429 client->title, e->xclient.data.l[0]);
1431 ObClient *sibling = NULL;
1432 if (e->xclient.data.l[1]) {
1433 ObWindow *win = g_hash_table_lookup
1434 (window_map, &e->xclient.data.l[1]);
1435 if (WINDOW_IS_CLIENT(win) &&
1436 WINDOW_AS_CLIENT(win) != client)
1438 sibling = WINDOW_AS_CLIENT(win);
1440 if (sibling == NULL)
1441 ob_debug_type(OB_DEBUG_APP_BUGS,
1442 "_NET_RESTACK_WINDOW sent for window %s "
1443 "with invalid sibling 0x%x\n",
1444 client->title, e->xclient.data.l[1]);
1446 if (e->xclient.data.l[2] == Below ||
1447 e->xclient.data.l[2] == BottomIf ||
1448 e->xclient.data.l[2] == Above ||
1449 e->xclient.data.l[2] == TopIf ||
1450 e->xclient.data.l[2] == Opposite)
1452 gulong ignore_start;
1454 if (!config_focus_under_mouse)
1455 ignore_start = event_start_ignore_all_enters();
1456 /* just raise, don't activate */
1457 stacking_restack_request(client, sibling,
1458 e->xclient.data.l[2]);
1459 if (!config_focus_under_mouse)
1460 event_end_ignore_all_enters(ignore_start);
1462 /* send a synthetic ConfigureNotify, cuz this is supposed
1463 to be like a ConfigureRequest. */
1464 client_reconfigure(client, TRUE);
1466 ob_debug_type(OB_DEBUG_APP_BUGS,
1467 "_NET_RESTACK_WINDOW sent for window %s "
1468 "with invalid detail %d\n",
1469 client->title, e->xclient.data.l[2]);
1473 case PropertyNotify:
1474 /* validate cuz we query stuff off the client here */
1475 if (!client_validate(client)) break;
1477 /* compress changes to a single property into a single change */
1478 while (XCheckTypedWindowEvent(ob_display, client->window,
1482 /* XXX: it would be nice to compress ALL changes to a property,
1483 not just changes in a row without other props between. */
1485 a = ce.xproperty.atom;
1486 b = e->xproperty.atom;
1490 if ((a == prop_atoms.net_wm_name ||
1491 a == prop_atoms.wm_name ||
1492 a == prop_atoms.net_wm_icon_name ||
1493 a == prop_atoms.wm_icon_name)
1495 (b == prop_atoms.net_wm_name ||
1496 b == prop_atoms.wm_name ||
1497 b == prop_atoms.net_wm_icon_name ||
1498 b == prop_atoms.wm_icon_name)) {
1501 if (a == prop_atoms.net_wm_icon &&
1502 b == prop_atoms.net_wm_icon)
1505 XPutBackEvent(ob_display, &ce);
1509 msgtype = e->xproperty.atom;
1510 if (msgtype == XA_WM_NORMAL_HINTS) {
1511 ob_debug("Update NORMAL hints\n");
1512 client_update_normal_hints(client);
1513 /* normal hints can make a window non-resizable */
1514 client_setup_decor_and_functions(client, FALSE);
1516 /* make sure the client's sizes are within its bounds, but only
1517 reconfigure the window if it needs to. emacs will update its
1518 normal hints every time it receives a conigurenotify */
1519 client_reconfigure(client, FALSE);
1520 } else if (msgtype == prop_atoms.motif_wm_hints) {
1521 client_get_mwm_hints(client);
1522 /* This can override some mwm hints */
1523 client_get_type_and_transientness(client);
1525 /* Apply the changes to the window */
1526 client_setup_decor_and_functions(client, TRUE);
1527 } else if (msgtype == XA_WM_HINTS) {
1528 client_update_wmhints(client);
1529 } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1530 client_update_transient_for(client);
1531 client_get_type_and_transientness(client);
1532 /* type may have changed, so update the layer */
1533 client_calc_layer(client);
1534 client_setup_decor_and_functions(client, TRUE);
1535 } else if (msgtype == prop_atoms.net_wm_name ||
1536 msgtype == prop_atoms.wm_name ||
1537 msgtype == prop_atoms.net_wm_icon_name ||
1538 msgtype == prop_atoms.wm_icon_name) {
1539 client_update_title(client);
1540 } else if (msgtype == prop_atoms.wm_protocols) {
1541 client_update_protocols(client);
1542 client_setup_decor_and_functions(client, TRUE);
1544 else if (msgtype == prop_atoms.net_wm_strut ||
1545 msgtype == prop_atoms.net_wm_strut_partial) {
1546 client_update_strut(client);
1548 else if (msgtype == prop_atoms.net_wm_icon) {
1549 client_update_icons(client);
1551 else if (msgtype == prop_atoms.net_wm_icon_geometry) {
1552 client_update_icon_geometry(client);
1554 else if (msgtype == prop_atoms.net_wm_user_time) {
1556 if (client == focus_client &&
1557 PROP_GET32(client->window, net_wm_user_time, cardinal, &t) &&
1558 t && !event_time_after(t, e->xproperty.time) &&
1559 (!event_last_user_time ||
1560 event_time_after(t, event_last_user_time)))
1562 event_last_user_time = t;
1566 else if (msgtype == prop_atoms.net_wm_sync_request_counter) {
1567 client_update_sync_request_counter(client);
1571 case ColormapNotify:
1572 client_update_colormap(client, e->xcolormap.colormap);
1579 if (extensions_shape && e->type == extensions_shape_event_basep) {
1580 switch (((XShapeEvent*)e)->kind) {
1583 client->shaped = ((XShapeEvent*)e)->shaped;
1584 kind = ShapeBounding;
1587 client->shaped_input = ((XShapeEvent*)e)->shaped;
1591 frame_adjust_shape_kind(client->frame, kind);
1598 static void event_handle_dock(ObDock *s, XEvent *e)
1602 if (e->xbutton.button == 1)
1603 stacking_raise(DOCK_AS_WINDOW(s));
1604 else if (e->xbutton.button == 2)
1605 stacking_lower(DOCK_AS_WINDOW(s));
1611 /* don't hide when moving into a dock app */
1612 if (e->xcrossing.detail != NotifyInferior)
1618 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1622 dock_app_drag(app, &e->xmotion);
1625 if (app->ignore_unmaps) {
1626 app->ignore_unmaps--;
1629 dock_remove(app, TRUE);
1632 case ReparentNotify:
1633 dock_remove(app, FALSE);
1635 case ConfigureNotify:
1636 dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1641 static ObMenuFrame* find_active_menu(void)
1644 ObMenuFrame *ret = NULL;
1646 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1655 static ObMenuFrame* find_active_or_last_menu(void)
1657 ObMenuFrame *ret = NULL;
1659 ret = find_active_menu();
1660 if (!ret && menu_frame_visible)
1661 ret = menu_frame_visible->data;
1665 static gboolean event_handle_prompt(ObPrompt *p, XEvent *e)
1671 return prompt_mouse_event(p, e);
1674 return prompt_key_event(p, e);
1680 static gboolean event_handle_menu_keyboard(XEvent *ev)
1682 guint keycode, state;
1685 gboolean ret = FALSE;
1687 keycode = ev->xkey.keycode;
1688 state = ev->xkey.state;
1689 unikey = translate_unichar(keycode);
1691 frame = find_active_or_last_menu();
1693 g_assert_not_reached(); /* there is no active menu */
1695 /* Allow control while going thru the menu */
1696 else if (ev->type == KeyPress && (state & ~ControlMask) == 0) {
1697 frame->got_press = TRUE;
1699 if (ob_keycode_match(keycode, OB_KEY_ESCAPE)) {
1700 menu_frame_hide_all();
1704 else if (ob_keycode_match(keycode, OB_KEY_LEFT)) {
1705 /* Left goes to the parent menu */
1706 if (frame->parent) {
1707 /* remove focus from the child */
1708 menu_frame_select(frame, NULL, TRUE);
1709 /* and put it in the parent */
1710 menu_frame_select(frame->parent, frame->parent->selected,
1716 else if (ob_keycode_match(keycode, OB_KEY_RIGHT)) {
1717 /* Right goes to the selected submenu */
1718 if (frame->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1719 /* make sure it is visible */
1720 menu_frame_select(frame, frame->selected, TRUE);
1721 menu_frame_select_next(frame->child);
1726 else if (ob_keycode_match(keycode, OB_KEY_UP)) {
1727 menu_frame_select_previous(frame);
1731 else if (ob_keycode_match(keycode, OB_KEY_DOWN)) {
1732 menu_frame_select_next(frame);
1736 else if (ob_keycode_match(keycode, OB_KEY_HOME)) {
1737 menu_frame_select_first(frame);
1741 else if (ob_keycode_match(keycode, OB_KEY_END)) {
1742 menu_frame_select_last(frame);
1747 /* Use KeyRelease events for running things so that the key release doesn't
1748 get sent to the focused application.
1750 Allow ControlMask only, and don't bother if the menu is empty */
1751 else if (ev->type == KeyRelease && (state & ~ControlMask) == 0 &&
1752 frame->entries && frame->got_press)
1754 if (ob_keycode_match(keycode, OB_KEY_RETURN)) {
1755 /* Enter runs the active item or goes into the submenu.
1756 Control-Enter runs it without closing the menu. */
1758 menu_frame_select_next(frame->child);
1759 else if (frame->selected)
1760 menu_entry_frame_execute(frame->selected, state);
1765 /* keyboard accelerator shortcuts. (if it was a valid key) */
1766 else if (unikey != 0) {
1769 ObMenuEntryFrame *found = NULL;
1770 guint num_found = 0;
1772 /* start after the selected one */
1773 start = frame->entries;
1774 if (frame->selected) {
1775 for (it = start; frame->selected != it->data;
1776 it = g_list_next(it))
1777 g_assert(it != NULL); /* nothing was selected? */
1778 /* next with wraparound */
1779 start = g_list_next(it);
1780 if (start == NULL) start = frame->entries;
1785 ObMenuEntryFrame *e = it->data;
1786 gunichar entrykey = 0;
1788 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1789 entrykey = e->entry->data.normal.shortcut;
1790 else if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1791 entrykey = e->entry->data.submenu.submenu->shortcut;
1793 if (unikey == entrykey) {
1794 if (found == NULL) found = e;
1798 /* next with wraparound */
1799 it = g_list_next(it);
1800 if (it == NULL) it = frame->entries;
1801 } while (it != start);
1804 if (found->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1807 menu_frame_select(frame, found, TRUE);
1808 usleep(50000); /* highlight the item for a short bit so the
1809 user can see what happened */
1810 menu_entry_frame_execute(found, state);
1812 menu_frame_select(frame, found, TRUE);
1814 menu_frame_select_next(frame->child);
1825 static Bool event_look_for_menu_enter(Display *d, XEvent *ev, XPointer arg)
1827 ObMenuFrame *f = (ObMenuFrame*)arg;
1828 ObMenuEntryFrame *e;
1829 return ev->type == EnterNotify &&
1830 (e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1831 !e->ignore_enters && e->frame == f;
1834 static gboolean event_handle_menu(XEvent *ev)
1837 ObMenuEntryFrame *e;
1838 gboolean ret = TRUE;
1842 if (menu_hide_delay_reached() &&
1843 (ev->xbutton.button < 4 || ev->xbutton.button > 5))
1845 if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1846 ev->xbutton.y_root)))
1848 menu_frame_select(e->frame, e, TRUE);
1849 menu_entry_frame_execute(e, ev->xbutton.state);
1852 menu_frame_hide_all();
1856 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1857 if (e->ignore_enters)
1859 else if (!(f = find_active_menu()) ||
1861 f->parent == e->frame ||
1862 f->child == e->frame)
1863 menu_frame_select(e->frame, e, FALSE);
1867 /*ignore leaves when we're already in the window */
1868 if (ev->xcrossing.detail == NotifyInferior)
1871 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)))
1875 /* check if an EnterNotify event is coming, and if not, then select
1876 nothing in the menu */
1877 if (XCheckIfEvent(ob_display, &ce, event_look_for_menu_enter,
1878 (XPointer)e->frame))
1879 XPutBackEvent(ob_display, &ce);
1881 menu_frame_select(e->frame, NULL, FALSE);
1885 if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1886 ev->xmotion.y_root)))
1887 if (!(f = find_active_menu()) ||
1889 f->parent == e->frame ||
1890 f->child == e->frame)
1891 menu_frame_select(e->frame, e, FALSE);
1895 ret = event_handle_menu_keyboard(ev);
1901 static void event_handle_user_input(ObClient *client, XEvent *e)
1903 g_assert(e->type == ButtonPress || e->type == ButtonRelease ||
1904 e->type == MotionNotify || e->type == KeyPress ||
1905 e->type == KeyRelease);
1907 if (menu_frame_visible) {
1908 if (event_handle_menu(e))
1909 /* don't use the event if the menu used it, but if the menu
1910 didn't use it and it's a keypress that is bound, it will
1911 close the menu and be used */
1915 /* if the keyboard interactive action uses the event then dont
1916 use it for bindings. likewise is moveresize uses the event. */
1917 if (!actions_interactive_input_event(e) && !moveresize_event(e)) {
1918 if (moveresize_in_progress)
1919 /* make further actions work on the client being
1921 client = moveresize_client;
1923 if (e->type == ButtonPress ||
1924 e->type == ButtonRelease ||
1925 e->type == MotionNotify)
1927 /* the frame may not be "visible" but they can still click on it
1928 in the case where it is animating before disappearing */
1929 if (!client || !frame_iconify_animating(client->frame))
1930 mouse_event(client, e);
1932 keyboard_event((focus_cycle_target ? focus_cycle_target :
1933 (client ? client : focus_client)), e);
1937 static void focus_delay_dest(gpointer data)
1942 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1944 const ObFocusDelayData *f1 = d1;
1945 return f1->client == d2;
1948 static gboolean focus_delay_func(gpointer data)
1950 ObFocusDelayData *d = data;
1951 Time old = event_curtime;
1953 event_curtime = d->time;
1954 event_curserial = d->serial;
1955 if (client_focus(d->client) && config_focus_raise)
1956 stacking_raise(CLIENT_AS_WINDOW(d->client));
1957 event_curtime = old;
1958 return FALSE; /* no repeat */
1961 static void focus_delay_client_dest(ObClient *client, gpointer data)
1963 ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1967 void event_halt_focus_delay(void)
1969 /* ignore all enter events up till the event which caused this to occur */
1970 if (event_curserial) event_ignore_enter_range(1, event_curserial);
1971 ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1974 gulong event_start_ignore_all_enters(void)
1976 return NextRequest(ob_display);
1979 static void event_ignore_enter_range(gulong start, gulong end)
1983 g_assert(start != 0);
1986 r = g_new(ObSerialRange, 1);
1989 ignore_serials = g_slist_prepend(ignore_serials, r);
1991 ob_debug_type(OB_DEBUG_FOCUS, "ignoring enters from %lu until %lu\n",
1994 /* increment the serial so we don't ignore events we weren't meant to */
1995 PROP_ERASE(screen_support_win, motif_wm_hints);
1998 void event_end_ignore_all_enters(gulong start)
2000 /* Use (NextRequest-1) so that we ignore up to the current serial only.
2001 Inside event_ignore_enter_range, we increment the serial by one, but if
2002 we ignore that serial too, then any enter events generated by mouse
2003 movement will be ignored until we create some further network traffic.
2004 Instead ignore up to NextRequest-1, then when we increment the serial,
2005 we will be *past* the range of ignored serials */
2006 event_ignore_enter_range(start, NextRequest(ob_display)-1);
2009 static gboolean is_enter_focus_event_ignored(gulong serial)
2013 for (it = ignore_serials; it; it = next) {
2014 ObSerialRange *r = it->data;
2016 next = g_slist_next(it);
2018 if ((glong)(serial - r->end) > 0) {
2020 ignore_serials = g_slist_delete_link(ignore_serials, it);
2023 else if ((glong)(serial - r->start) >= 0)
2029 void event_cancel_all_key_grabs(void)
2031 if (actions_interactive_act_running()) {
2032 actions_interactive_cancel_act();
2033 ob_debug("KILLED interactive action\n");
2035 else if (menu_frame_visible) {
2036 menu_frame_hide_all();
2037 ob_debug("KILLED open menus\n");
2039 else if (moveresize_in_progress) {
2040 moveresize_end(TRUE);
2041 ob_debug("KILLED interactive moveresize\n");
2043 else if (grab_on_keyboard()) {
2045 ob_debug("KILLED active grab on keyboard\n");
2048 ungrab_passive_key();
2050 XSync(ob_display, FALSE);
2053 gboolean event_time_after(guint32 t1, guint32 t2)
2055 g_assert(t1 != CurrentTime);
2056 g_assert(t2 != CurrentTime);
2059 Timestamp values wrap around (after about 49.7 days). The server, given
2060 its current time is represented by timestamp T, always interprets
2061 timestamps from clients by treating half of the timestamp space as being
2062 later in time than T.
2063 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
2066 /* TIME_HALF is not half of the number space of a Time type variable.
2067 * Rather, it is half the number space of a timestamp value, which is
2068 * always 32 bits. */
2069 #define TIME_HALF (guint32)(1 << 31)
2071 if (t2 >= TIME_HALF)
2072 /* t2 is in the second half so t1 might wrap around and be smaller than
2074 return t1 >= t2 || t1 < (t2 + TIME_HALF);
2076 /* t2 is in the first half so t1 has to come after it */
2077 return t1 >= t2 && t1 < (t2 + TIME_HALF);
2080 Time event_get_server_time(void)
2082 /* Generate a timestamp */
2085 XChangeProperty(ob_display, screen_support_win,
2086 prop_atoms.wm_class, prop_atoms.string,
2087 8, PropModeAppend, NULL, 0);
2088 XWindowEvent(ob_display, screen_support_win, PropertyChangeMask, &event);
2089 return event.xproperty.time;