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.
33 #include "menuframe.h"
38 #include "focus_cycle.h"
39 #include "moveresize.h"
43 #include "obt/display.h"
45 #include "obt/keyboard.h"
48 #include <X11/Xatom.h>
51 #ifdef HAVE_SYS_SELECT_H
52 # include <sys/select.h>
58 # include <unistd.h> /* for usleep() */
61 # include <X11/XKBlib.h>
65 #include <X11/ICE/ICElib.h>
82 gulong start; /* inclusive */
83 gulong end; /* inclusive */
86 static void event_process(const XEvent *e, gpointer data);
87 static void event_handle_root(XEvent *e);
88 static gboolean event_handle_menu_input(XEvent *e);
89 static void event_handle_menu(ObMenuFrame *frame, XEvent *e);
90 static void event_handle_internal(ObInternalWindow *internal, XEvent *e);
91 static gboolean event_handle_prompt(ObPrompt *p, XEvent *e);
92 static void event_handle_dock(ObDock *s, XEvent *e);
93 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
94 static void event_handle_client(ObClient *c, XEvent *e);
95 static void event_handle_user_input(ObClient *client, XEvent *e);
96 static gboolean is_enter_focus_event_ignored(gulong serial);
97 static void event_ignore_enter_range(gulong start, gulong end);
99 static void focus_delay_dest(gpointer data);
100 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
101 static gboolean focus_delay_func(gpointer data);
102 static void focus_delay_client_dest(ObClient *client, gpointer data);
104 Time event_curtime = CurrentTime;
105 Time event_last_user_time = CurrentTime;
106 /*! The serial of the current X event */
108 static gulong event_curserial;
109 static gboolean focus_left_screen = FALSE;
110 /*! A list of ObSerialRanges which are to be ignored for mouse enter events */
111 static GSList *ignore_serials = NULL;
114 static void ice_handler(gint fd, gpointer conn)
117 IceProcessMessages(conn, NULL, &b);
120 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
121 IcePointer *watch_data)
126 fd = IceConnectionNumber(conn);
127 obt_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
129 obt_main_loop_fd_remove(ob_main_loop, fd);
135 void event_startup(gboolean reconfig)
137 if (reconfig) return;
139 obt_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
142 IceAddConnectionWatch(ice_watch, NULL);
145 client_add_destroy_notify(focus_delay_client_dest, NULL);
148 void event_shutdown(gboolean reconfig)
150 if (reconfig) return;
153 IceRemoveConnectionWatch(ice_watch, NULL);
156 client_remove_destroy_notify(focus_delay_client_dest);
159 static Window event_get_window(XEvent *e)
166 window = obt_root(ob_screen);
169 window = e->xcreatewindow.window;
172 window = e->xmaprequest.window;
175 window = e->xmap.window;
178 window = e->xunmap.window;
181 window = e->xdestroywindow.window;
183 case ConfigureRequest:
184 window = e->xconfigurerequest.window;
186 case ConfigureNotify:
187 window = e->xconfigure.window;
191 if (obt_display_extension_xkb &&
192 e->type == obt_display_extension_xkb_basep)
194 switch (((XkbAnyEvent*)e)->xkb_type) {
196 window = ((XkbBellNotifyEvent*)e)->window;
203 if (obt_display_extension_sync &&
204 e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
209 window = e->xany.window;
214 static void event_set_curtime(XEvent *e)
216 Time t = CurrentTime;
218 /* grab the lasttime and hack up the state */
234 t = e->xproperty.time;
238 t = e->xcrossing.time;
242 if (obt_display_extension_sync &&
243 e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
245 t = ((XSyncAlarmNotifyEvent*)e)->time;
248 /* if more event types are anticipated, get their timestamp
253 /* watch that if we get an event earlier than the last specified user_time,
254 which can happen if the clock goes backwards, we erase the last
255 specified user_time */
256 if (t && event_last_user_time && event_time_after(event_last_user_time, t))
257 event_last_user_time = CurrentTime;
262 static void event_hack_mods(XEvent *e)
265 XkbStateRec xkb_state;
271 e->xbutton.state = obt_keyboard_only_modmasks(e->xbutton.state);
274 e->xkey.state = obt_keyboard_only_modmasks(e->xkey.state);
278 /* If XKB is present, then the modifiers are all strange from its
279 magic. Our X core protocol stuff won't work, so we use this to
280 find what the modifier state is instead. */
281 if (XkbGetState(obt_display, XkbUseCoreKbd, &xkb_state) == Success)
283 obt_keyboard_only_modmasks(xkb_state.compat_state);
287 e->xkey.state = obt_keyboard_only_modmasks(e->xkey.state);
288 /* remove from the state the mask of the modifier key being
289 released, if it is a modifier key being released that is */
290 e->xkey.state &= ~obt_keyboard_keycode_to_modmask(e->xkey.keycode);
294 e->xmotion.state = obt_keyboard_only_modmasks(e->xmotion.state);
295 /* compress events */
298 while (XCheckTypedWindowEvent(obt_display, e->xmotion.window,
300 e->xmotion.x = ce.xmotion.x;
301 e->xmotion.y = ce.xmotion.y;
302 e->xmotion.x_root = ce.xmotion.x_root;
303 e->xmotion.y_root = ce.xmotion.y_root;
310 static gboolean wanted_focusevent(XEvent *e, gboolean in_client_only)
312 gint mode = e->xfocus.mode;
313 gint detail = e->xfocus.detail;
314 Window win = e->xany.window;
316 if (e->type == FocusIn) {
317 /* These are ones we never want.. */
319 /* This means focus was given by a keyboard/mouse grab. */
320 if (mode == NotifyGrab)
322 /* This means focus was given back from a keyboard/mouse grab. */
323 if (mode == NotifyUngrab)
326 /* These are the ones we want.. */
328 if (win == obt_root(ob_screen)) {
329 /* If looking for a focus in on a client, then always return
330 FALSE for focus in's to the root window */
333 /* This means focus reverted off of a client */
334 else if (detail == NotifyPointerRoot ||
335 detail == NotifyDetailNone ||
336 detail == NotifyInferior ||
337 /* This means focus got here from another screen */
338 detail == NotifyNonlinear)
344 /* It was on a client, was it a valid one?
345 It's possible to get a FocusIn event for a client that was managed
348 if (in_client_only) {
349 ObWindow *w = window_find(e->xfocus.window);
350 if (!w || !WINDOW_IS_CLIENT(w))
354 /* This means focus reverted to parent from the client (this
355 happens often during iconify animation) */
356 if (detail == NotifyInferior)
360 /* This means focus moved from the root window to a client */
361 if (detail == NotifyVirtual)
363 /* This means focus moved from one client to another */
364 if (detail == NotifyNonlinearVirtual)
370 g_assert(e->type == FocusOut);
372 /* These are ones we never want.. */
374 /* This means focus was taken by a keyboard/mouse grab. */
375 if (mode == NotifyGrab)
377 /* This means focus was grabbed on a window and it was released. */
378 if (mode == NotifyUngrab)
381 /* Focus left the root window revertedto state */
382 if (win == obt_root(ob_screen))
385 /* These are the ones we want.. */
387 /* This means focus moved from a client to the root window */
388 if (detail == NotifyVirtual)
390 /* This means focus moved from one client to another */
391 if (detail == NotifyNonlinearVirtual)
399 static Bool event_look_for_focusin(Display *d, XEvent *e, XPointer arg)
401 return e->type == FocusIn && wanted_focusevent(e, FALSE);
404 static Bool event_look_for_focusin_client(Display *d, XEvent *e, XPointer arg)
406 return e->type == FocusIn && wanted_focusevent(e, TRUE);
409 static void print_focusevent(XEvent *e)
411 gint mode = e->xfocus.mode;
412 gint detail = e->xfocus.detail;
413 Window win = e->xany.window;
414 const gchar *modestr, *detailstr;
417 case NotifyNormal: modestr="NotifyNormal"; break;
418 case NotifyGrab: modestr="NotifyGrab"; break;
419 case NotifyUngrab: modestr="NotifyUngrab"; break;
420 case NotifyWhileGrabbed: modestr="NotifyWhileGrabbed"; break;
423 case NotifyAncestor: detailstr="NotifyAncestor"; break;
424 case NotifyVirtual: detailstr="NotifyVirtual"; break;
425 case NotifyInferior: detailstr="NotifyInferior"; break;
426 case NotifyNonlinear: detailstr="NotifyNonlinear"; break;
427 case NotifyNonlinearVirtual: detailstr="NotifyNonlinearVirtual"; break;
428 case NotifyPointer: detailstr="NotifyPointer"; break;
429 case NotifyPointerRoot: detailstr="NotifyPointerRoot"; break;
430 case NotifyDetailNone: detailstr="NotifyDetailNone"; break;
433 if (mode == NotifyGrab || mode == NotifyUngrab)
438 ob_debug_type(OB_DEBUG_FOCUS, "Focus%s 0x%x mode=%s detail=%s",
439 (e->xfocus.type == FocusIn ? "In" : "Out"),
445 static gboolean event_ignore(XEvent *e, ObClient *client)
450 if (!wanted_focusevent(e, FALSE))
455 if (!wanted_focusevent(e, FALSE))
462 static void event_process(const XEvent *ec, gpointer data)
465 ObEventData *ed = data;
468 ObClient *client = NULL;
470 ObDockApp *dockapp = NULL;
471 ObWindow *obwin = NULL;
472 ObMenuFrame *menu = NULL;
473 ObPrompt *prompt = NULL;
474 ObInternalWindow *internal = NULL;
476 /* make a copy we can mangle */
480 window = event_get_window(e);
481 if (window == obt_root(ob_screen))
482 /* don't do any lookups, waste of cpu */;
483 else if ((obwin = window_find(window))) {
484 switch (obwin->type) {
485 case OB_WINDOW_CLASS_DOCK:
486 dock = WINDOW_AS_DOCK(obwin);
488 case OB_WINDOW_CLASS_CLIENT:
489 client = WINDOW_AS_CLIENT(obwin);
490 /* events on clients can be events on prompt windows too */
491 prompt = client->prompt;
493 case OB_WINDOW_CLASS_MENUFRAME:
494 menu = WINDOW_AS_MENUFRAME(obwin);
496 case OB_WINDOW_CLASS_INTERNAL:
497 internal = WINDOW_AS_INTERNAL(obwin);
499 case OB_WINDOW_CLASS_PROMPT:
500 prompt = WINDOW_AS_PROMPT(obwin);
505 dockapp = dock_find_dockapp(window);
507 event_set_curtime(e);
508 event_curserial = e->xany.serial;
510 if (event_ignore(e, client)) {
517 /* deal with it in the kernel */
519 if (e->type == FocusIn) {
521 e->xfocus.detail == NotifyInferior)
523 ob_debug_type(OB_DEBUG_FOCUS,
524 "Focus went to the frame window");
526 focus_left_screen = FALSE;
528 focus_fallback(FALSE, config_focus_under_mouse, TRUE, TRUE);
530 /* We don't get a FocusOut for this case, because it's just moving
531 from our Inferior up to us. This happens when iconifying a
532 window with RevertToParent focus */
533 frame_adjust_focus(client->frame, FALSE);
534 /* focus_set_client(NULL) has already been called */
536 else if (e->xfocus.detail == NotifyPointerRoot ||
537 e->xfocus.detail == NotifyDetailNone ||
538 e->xfocus.detail == NotifyInferior ||
539 e->xfocus.detail == NotifyNonlinear)
543 ob_debug_type(OB_DEBUG_FOCUS,
544 "Focus went to root or pointer root/none");
546 if (e->xfocus.detail == NotifyInferior ||
547 e->xfocus.detail == NotifyNonlinear)
549 focus_left_screen = FALSE;
552 /* If another FocusIn is in the queue then don't fallback yet. This
553 fixes the fun case of:
554 window map -> send focusin
555 window unmap -> get focusout
556 window map -> send focusin
557 get first focus out -> fall back to something (new window
558 hasn't received focus yet, so something else) -> send focusin
559 which means the "something else" is the last thing to get a
560 focusin sent to it, so the new window doesn't end up with focus.
562 But if the other focus in is something like PointerRoot then we
563 still want to fall back.
565 if (XCheckIfEvent(obt_display, &ce, event_look_for_focusin_client,
568 XPutBackEvent(obt_display, &ce);
569 ob_debug_type(OB_DEBUG_FOCUS,
570 " but another FocusIn is coming");
572 /* Focus has been reverted.
574 FocusOut events come after UnmapNotify, so we don't need to
575 worry about focusing an invalid window
578 if (!focus_left_screen)
579 focus_fallback(FALSE, config_focus_under_mouse,
585 ob_debug_type(OB_DEBUG_FOCUS,
586 "Focus went to a window that is already gone");
588 /* If you send focus to a window and then it disappears, you can
589 get the FocusIn for it, after it is unmanaged.
590 Just wait for the next FocusOut/FocusIn pair, but make note that
591 the window that was focused no longer is. */
592 focus_set_client(NULL);
594 else if (client != focus_client) {
595 focus_left_screen = FALSE;
596 if (!focus_cycle_target || !config_focus_dontstop) {
597 frame_adjust_focus(client->frame, TRUE);
598 focus_set_client(client);
599 client_calc_layer(client);
600 client_bring_helper_windows(client);
603 } else if (e->type == FocusOut) {
606 /* Look for the followup FocusIn */
607 if (!XCheckIfEvent(obt_display, &ce, event_look_for_focusin, NULL)) {
608 /* There is no FocusIn, this means focus went to a window that
609 is not being managed, or a window on another screen. */
613 obt_display_ignore_errors(TRUE);
614 if (XGetInputFocus(obt_display, &win, &i) &&
615 XGetGeometry(obt_display, win, &root, &i,&i,&u,&u,&u,&u) &&
616 root != obt_root(ob_screen))
618 ob_debug_type(OB_DEBUG_FOCUS,
619 "Focus went to another screen !");
620 focus_left_screen = TRUE;
623 ob_debug_type(OB_DEBUG_FOCUS,
624 "Focus went to a black hole !");
625 obt_display_ignore_errors(FALSE);
626 /* nothing is focused */
627 focus_set_client(NULL);
629 /* Focus moved, so process the FocusIn event */
630 ObEventData ed = { .ignored = FALSE };
631 event_process(&ce, &ed);
633 /* The FocusIn was ignored, this means it was on a window
634 that isn't a client. */
635 ob_debug_type(OB_DEBUG_FOCUS,
636 "Focus went to an unmanaged window 0x%x !",
638 focus_fallback(TRUE, config_focus_under_mouse, TRUE, TRUE);
642 if (client && client != focus_client) {
643 frame_adjust_focus(client->frame, FALSE);
644 /* focus_set_client(NULL) has already been called in this
645 section or by focus_fallback */
649 event_handle_client(client, e);
651 event_handle_dockapp(dockapp, e);
653 event_handle_dock(dock, e);
655 event_handle_menu(menu, e);
657 event_handle_internal(internal, e);
658 else if (window == obt_root(ob_screen))
659 event_handle_root(e);
660 else if (e->type == MapRequest)
661 window_manage(window);
662 else if (e->type == MappingNotify) {
663 /* keyboard layout changes for modifier mapping changes. reload the
664 modifier map, and rebind all the key bindings as appropriate */
665 ob_debug("Kepboard map changed. Reloading keyboard bindings.");
666 ob_set_state(OB_STATE_RECONFIGURING);
667 obt_keyboard_reload();
669 ob_set_state(OB_STATE_RUNNING);
671 else if (e->type == ClientMessage) {
672 /* This is for _NET_WM_REQUEST_FRAME_EXTENTS messages. They come for
673 windows that are not managed yet. */
674 if (e->xclient.message_type ==
675 OBT_PROP_ATOM(NET_REQUEST_FRAME_EXTENTS))
677 /* Pretend to manage the client, getting information used to
678 determine its decorations */
679 ObClient *c = client_fake_manage(e->xclient.window);
682 /* set the frame extents on the window */
683 vals[0] = c->frame->size.left;
684 vals[1] = c->frame->size.right;
685 vals[2] = c->frame->size.top;
686 vals[3] = c->frame->size.bottom;
687 OBT_PROP_SETA32(e->xclient.window, NET_FRAME_EXTENTS,
690 /* Free the pretend client */
691 client_fake_unmanage(c);
694 else if (e->type == ConfigureRequest) {
695 /* unhandled configure requests must be used to configure the
699 xwc.x = e->xconfigurerequest.x;
700 xwc.y = e->xconfigurerequest.y;
701 xwc.width = e->xconfigurerequest.width;
702 xwc.height = e->xconfigurerequest.height;
703 xwc.border_width = e->xconfigurerequest.border_width;
704 xwc.sibling = e->xconfigurerequest.above;
705 xwc.stack_mode = e->xconfigurerequest.detail;
707 /* we are not to be held responsible if someone sends us an
709 obt_display_ignore_errors(TRUE);
710 XConfigureWindow(obt_display, window,
711 e->xconfigurerequest.value_mask, &xwc);
712 obt_display_ignore_errors(FALSE);
715 else if (obt_display_extension_sync &&
716 e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
718 XSyncAlarmNotifyEvent *se = (XSyncAlarmNotifyEvent*)e;
719 if (se->alarm == moveresize_alarm && moveresize_in_progress)
724 if (prompt && event_handle_prompt(prompt, e))
726 else if (e->type == ButtonPress || e->type == ButtonRelease) {
727 /* If the button press was on some non-root window, or was physically
728 on the root window, then process it */
729 if (window != obt_root(ob_screen) ||
730 e->xbutton.subwindow == None)
732 event_handle_user_input(client, e);
734 /* Otherwise only process it if it was physically on an openbox
739 if ((w = window_find(e->xbutton.subwindow)) &&
740 WINDOW_IS_INTERNAL(w))
742 event_handle_user_input(client, e);
746 else if (e->type == KeyPress || e->type == KeyRelease ||
747 e->type == MotionNotify)
748 event_handle_user_input(client, e);
752 /* run all the hooks at once */
755 /* if something happens and it's not from an XEvent, then we don't know
757 event_curtime = CurrentTime;
761 static void event_handle_root(XEvent *e)
767 ob_debug("Another WM has requested to replace us. Exiting.");
772 if (e->xclient.format != 32) break;
774 msgtype = e->xclient.message_type;
775 if (msgtype == OBT_PROP_ATOM(NET_CURRENT_DESKTOP)) {
776 guint d = e->xclient.data.l[0];
777 if (d < screen_num_desktops) {
778 event_curtime = e->xclient.data.l[1];
779 if (event_curtime == 0)
780 ob_debug_type(OB_DEBUG_APP_BUGS,
781 "_NET_CURRENT_DESKTOP message is missing "
783 screen_set_desktop(d, TRUE);
785 } else if (msgtype == OBT_PROP_ATOM(NET_NUMBER_OF_DESKTOPS)) {
786 guint d = e->xclient.data.l[0];
787 if (d > 0 && d <= 1000)
788 screen_set_num_desktops(d);
789 } else if (msgtype == OBT_PROP_ATOM(NET_SHOWING_DESKTOP)) {
790 screen_show_desktop(e->xclient.data.l[0] != 0, NULL);
791 } else if (msgtype == OBT_PROP_ATOM(OB_CONTROL)) {
792 ob_debug("OB_CONTROL: %d", e->xclient.data.l[0]);
793 if (e->xclient.data.l[0] == 1)
795 else if (e->xclient.data.l[0] == 2)
797 else if (e->xclient.data.l[0] == 3)
799 } else if (msgtype == OBT_PROP_ATOM(WM_PROTOCOLS)) {
800 if ((Atom)e->xclient.data.l[0] == OBT_PROP_ATOM(NET_WM_PING))
801 ping_got_pong(e->xclient.data.l[1]);
805 if (e->xproperty.atom == OBT_PROP_ATOM(NET_DESKTOP_NAMES)) {
806 ob_debug("UPDATE DESKTOP NAMES");
807 screen_update_desktop_names();
809 else if (e->xproperty.atom == OBT_PROP_ATOM(NET_DESKTOP_LAYOUT))
810 screen_update_layout();
812 case ConfigureNotify:
814 XRRUpdateConfiguration(e);
823 void event_enter_client(ObClient *client)
825 g_assert(config_focus_follow);
827 if (is_enter_focus_event_ignored(event_curserial)) {
828 ob_debug_type(OB_DEBUG_FOCUS, "Ignoring enter event with serial %lu\n"
829 "on client 0x%x", event_curserial, client->window);
833 if (client_enter_focusable(client) && client_can_focus(client)) {
834 if (config_focus_delay) {
835 ObFocusDelayData *data;
837 obt_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
839 data = g_new(ObFocusDelayData, 1);
840 data->client = client;
841 data->time = event_curtime;
842 data->serial = event_curserial;
844 obt_main_loop_timeout_add(ob_main_loop,
845 config_focus_delay * 1000,
847 data, focus_delay_cmp, focus_delay_dest);
849 ObFocusDelayData data;
850 data.client = client;
851 data.time = event_curtime;
852 data.serial = event_curserial;
853 focus_delay_func(&data);
858 static gboolean *context_to_button(ObFrame *f, ObFrameContext con, gboolean press)
862 case OB_FRAME_CONTEXT_MAXIMIZE:
863 return &f->max_press;
864 case OB_FRAME_CONTEXT_CLOSE:
865 return &f->close_press;
866 case OB_FRAME_CONTEXT_ICONIFY:
867 return &f->iconify_press;
868 case OB_FRAME_CONTEXT_ALLDESKTOPS:
869 return &f->desk_press;
870 case OB_FRAME_CONTEXT_SHADE:
871 return &f->shade_press;
877 case OB_FRAME_CONTEXT_MAXIMIZE:
878 return &f->max_hover;
879 case OB_FRAME_CONTEXT_CLOSE:
880 return &f->close_hover;
881 case OB_FRAME_CONTEXT_ICONIFY:
882 return &f->iconify_hover;
883 case OB_FRAME_CONTEXT_ALLDESKTOPS:
884 return &f->desk_hover;
885 case OB_FRAME_CONTEXT_SHADE:
886 return &f->shade_hover;
893 static void compress_client_message_event(XEvent *e, XEvent *ce, Window window,
896 /* compress changes into a single change */
897 while (XCheckTypedWindowEvent(obt_display, window, e->type, ce)) {
898 /* XXX: it would be nice to compress ALL messages of a
899 type, not just messages in a row without other
900 message types between. */
901 if (ce->xclient.message_type != msgtype) {
902 XPutBackEvent(obt_display, ce);
905 e->xclient = ce->xclient;
909 static void event_handle_client(ObClient *client, XEvent *e)
915 static gint px = -1, py = -1;
917 static ObFrameContext pcon = OB_FRAME_CONTEXT_NONE;
921 /* save where the press occured for the first button pressed */
923 pb = e->xbutton.button;
927 pcon = frame_context(client, e->xbutton.window, px, py);
928 pcon = mouse_button_frame_context(pcon, e->xbutton.button,
932 /* Wheel buttons don't draw because they are an instant click, so it
933 is a waste of resources to go drawing it.
934 if the user is doing an interactive thing, or has a menu open then
935 the mouse is grabbed (possibly) and if we get these events we don't
936 want to deal with them
938 if (!(e->xbutton.button == 4 || e->xbutton.button == 5) &&
941 /* use where the press occured */
942 con = frame_context(client, e->xbutton.window, px, py);
943 con = mouse_button_frame_context(con, e->xbutton.button,
946 /* button presses on CLIENT_CONTEXTs are not accompanied by a
947 release because they are Replayed to the client */
948 if ((e->type == ButtonRelease || CLIENT_CONTEXT(con, client)) &&
949 e->xbutton.button == pb)
950 pb = 0, px = py = -1, pcon = OB_FRAME_CONTEXT_NONE;
952 but = context_to_button(client->frame, con, TRUE);
954 *but = (e->type == ButtonPress);
955 frame_adjust_state(client->frame);
960 /* when there is a grab on the pointer, we won't get enter/leave
961 notifies, but we still get motion events */
962 if (grab_on_pointer()) break;
964 con = frame_context(client, e->xmotion.window,
965 e->xmotion.x, e->xmotion.y);
967 case OB_FRAME_CONTEXT_TITLEBAR:
968 case OB_FRAME_CONTEXT_TLCORNER:
969 case OB_FRAME_CONTEXT_TRCORNER:
970 /* we've left the button area inside the titlebar */
971 if (client->frame->max_hover || client->frame->desk_hover ||
972 client->frame->shade_hover || client->frame->iconify_hover ||
973 client->frame->close_hover)
975 client->frame->max_hover =
976 client->frame->desk_hover =
977 client->frame->shade_hover =
978 client->frame->iconify_hover =
979 client->frame->close_hover = FALSE;
980 frame_adjust_state(client->frame);
984 but = context_to_button(client->frame, con, FALSE);
985 if (but && !*but && !pb) {
987 frame_adjust_state(client->frame);
993 con = frame_context(client, e->xcrossing.window,
994 e->xcrossing.x, e->xcrossing.y);
996 case OB_FRAME_CONTEXT_TITLEBAR:
997 case OB_FRAME_CONTEXT_TLCORNER:
998 case OB_FRAME_CONTEXT_TRCORNER:
999 /* we've left the button area inside the titlebar */
1000 client->frame->max_hover =
1001 client->frame->desk_hover =
1002 client->frame->shade_hover =
1003 client->frame->iconify_hover =
1004 client->frame->close_hover = FALSE;
1005 if (e->xcrossing.mode == NotifyGrab) {
1006 client->frame->max_press =
1007 client->frame->desk_press =
1008 client->frame->shade_press =
1009 client->frame->iconify_press =
1010 client->frame->close_press = FALSE;
1013 case OB_FRAME_CONTEXT_FRAME:
1014 /* When the mouse leaves an animating window, don't use the
1015 corresponding enter events. Pretend like the animating window
1016 doesn't even exist..! */
1017 if (frame_iconify_animating(client->frame))
1018 event_end_ignore_all_enters(event_start_ignore_all_enters());
1020 ob_debug_type(OB_DEBUG_FOCUS,
1021 "%sNotify mode %d detail %d on %lx",
1022 (e->type == EnterNotify ? "Enter" : "Leave"),
1024 e->xcrossing.detail, (client?client->window:0));
1025 if (grab_on_keyboard())
1027 if (config_focus_follow && config_focus_delay &&
1028 /* leave inferior events can happen when the mouse goes onto
1029 the window's border and then into the window before the
1031 e->xcrossing.detail != NotifyInferior)
1033 obt_main_loop_timeout_remove_data(ob_main_loop,
1039 but = context_to_button(client->frame, con, FALSE);
1042 if (e->xcrossing.mode == NotifyGrab) {
1043 but = context_to_button(client->frame, con, TRUE);
1046 frame_adjust_state(client->frame);
1053 con = frame_context(client, e->xcrossing.window,
1054 e->xcrossing.x, e->xcrossing.y);
1056 case OB_FRAME_CONTEXT_FRAME:
1057 if (grab_on_keyboard())
1059 if (e->xcrossing.mode == NotifyGrab ||
1060 e->xcrossing.mode == NotifyUngrab ||
1061 /*ignore enters when we're already in the window */
1062 e->xcrossing.detail == NotifyInferior)
1064 ob_debug_type(OB_DEBUG_FOCUS,
1065 "%sNotify mode %d detail %d serial %lu on %lx "
1067 (e->type == EnterNotify ? "Enter" : "Leave"),
1069 e->xcrossing.detail,
1070 e->xcrossing.serial,
1071 client?client->window:0);
1074 ob_debug_type(OB_DEBUG_FOCUS,
1075 "%sNotify mode %d detail %d serial %lu on %lx, "
1077 (e->type == EnterNotify ? "Enter" : "Leave"),
1079 e->xcrossing.detail,
1080 e->xcrossing.serial,
1081 (client?client->window:0));
1082 if (config_focus_follow)
1083 event_enter_client(client);
1087 but = context_to_button(client->frame, con, FALSE);
1090 if (e->xcrossing.mode == NotifyUngrab) {
1091 but = context_to_button(client->frame, con, TRUE);
1092 *but = (con == pcon);
1094 frame_adjust_state(client->frame);
1100 case ConfigureRequest:
1105 /* dont compress these unless you're going to watch for property
1106 notifies in between (these can change what the configure would
1108 also you can't compress stacking events
1112 gboolean move = FALSE;
1113 gboolean resize = FALSE;
1115 /* get the current area */
1116 RECT_TO_DIMS(client->area, x, y, w, h);
1118 ob_debug("ConfigureRequest for \"%s\" desktop %d wmstate %d "
1121 screen_desktop, client->wmstate, client->frame->visible);
1122 ob_debug(" x %d y %d w %d h %d b %d",
1123 x, y, w, h, client->border_width);
1125 if (e->xconfigurerequest.value_mask & CWBorderWidth)
1126 if (client->border_width != e->xconfigurerequest.border_width) {
1127 client->border_width = e->xconfigurerequest.border_width;
1129 /* if the border width is changing then that is the same
1130 as requesting a resize, but we don't actually change
1131 the client's border, so it will change their root
1132 coordinates (since they include the border width) and
1133 we need to a notify then */
1137 if (e->xconfigurerequest.value_mask & CWStackMode) {
1138 ObClient *sibling = NULL;
1139 gulong ignore_start;
1142 /* get the sibling */
1143 if (e->xconfigurerequest.value_mask & CWSibling) {
1145 win = window_find(e->xconfigurerequest.above);
1146 if (win && WINDOW_IS_CLIENT(win) &&
1147 WINDOW_AS_CLIENT(win) != client)
1149 sibling = WINDOW_AS_CLIENT(win);
1152 /* an invalid sibling was specified so don't restack at
1153 all, it won't make sense no matter what we do */
1158 if (!config_focus_under_mouse)
1159 ignore_start = event_start_ignore_all_enters();
1160 stacking_restack_request(client, sibling,
1161 e->xconfigurerequest.detail);
1162 if (!config_focus_under_mouse)
1163 event_end_ignore_all_enters(ignore_start);
1166 /* a stacking change moves the window without resizing */
1170 if ((e->xconfigurerequest.value_mask & CWX) ||
1171 (e->xconfigurerequest.value_mask & CWY) ||
1172 (e->xconfigurerequest.value_mask & CWWidth) ||
1173 (e->xconfigurerequest.value_mask & CWHeight))
1175 /* don't allow clients to move shaded windows (fvwm does this)
1177 if (e->xconfigurerequest.value_mask & CWX) {
1178 if (!client->shaded)
1179 x = e->xconfigurerequest.x;
1182 if (e->xconfigurerequest.value_mask & CWY) {
1183 if (!client->shaded)
1184 y = e->xconfigurerequest.y;
1188 if (e->xconfigurerequest.value_mask & CWWidth) {
1189 w = e->xconfigurerequest.width;
1192 if (e->xconfigurerequest.value_mask & CWHeight) {
1193 h = e->xconfigurerequest.height;
1198 ob_debug("ConfigureRequest x(%d) %d y(%d) %d w(%d) %d h(%d) %d "
1199 "move %d resize %d",
1200 e->xconfigurerequest.value_mask & CWX, x,
1201 e->xconfigurerequest.value_mask & CWY, y,
1202 e->xconfigurerequest.value_mask & CWWidth, w,
1203 e->xconfigurerequest.value_mask & CWHeight, h,
1206 /* check for broken apps moving to their root position
1208 XXX remove this some day...that would be nice. right now all
1209 kde apps do this when they try activate themselves on another
1210 desktop. eg. open amarok window on desktop 1, switch to desktop
1211 2, click amarok tray icon. it will move by its decoration size.
1213 if (x != client->area.x &&
1214 x == (client->frame->area.x + client->frame->size.left -
1215 (gint)client->border_width) &&
1216 y != client->area.y &&
1217 y == (client->frame->area.y + client->frame->size.top -
1218 (gint)client->border_width) &&
1219 w == client->area.width &&
1220 h == client->area.height)
1222 ob_debug_type(OB_DEBUG_APP_BUGS,
1223 "Application %s is trying to move via "
1224 "ConfigureRequest to it's root window position "
1225 "but it is not using StaticGravity",
1231 /* they still requested a move, so don't change whether a
1232 notify is sent or not */
1238 client_try_configure(client, &x, &y, &w, &h, &lw, &lh, FALSE);
1240 /* if x was not given, then use gravity to figure out the new
1241 x. the reference point should not be moved */
1242 if ((e->xconfigurerequest.value_mask & CWWidth &&
1243 !(e->xconfigurerequest.value_mask & CWX)))
1244 client_gravity_resize_w(client, &x, client->area.width, w);
1246 if ((e->xconfigurerequest.value_mask & CWHeight &&
1247 !(e->xconfigurerequest.value_mask & CWY)))
1248 client_gravity_resize_h(client, &y, client->area.height,h);
1250 client_find_onscreen(client, &x, &y, w, h, FALSE);
1252 ob_debug("Granting ConfigureRequest x %d y %d w %d h %d",
1254 client_configure(client, x, y, w, h, FALSE, TRUE, TRUE);
1259 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
1261 client->window, e->xunmap.event, e->xunmap.from_configure,
1262 client->ignore_unmaps);
1263 if (client->ignore_unmaps) {
1264 client->ignore_unmaps--;
1267 client_unmanage(client);
1270 ob_debug("DestroyNotify for window 0x%x", client->window);
1271 client_unmanage(client);
1273 case ReparentNotify:
1274 /* this is when the client is first taken captive in the frame */
1275 if (e->xreparent.parent == client->frame->window) break;
1278 This event is quite rare and is usually handled in unmapHandler.
1279 However, if the window is unmapped when the reparent event occurs,
1280 the window manager never sees it because an unmap event is not sent
1281 to an already unmapped window.
1284 /* we don't want the reparent event, put it back on the stack for the
1285 X server to deal with after we unmanage the window */
1286 XPutBackEvent(obt_display, e);
1288 ob_debug("ReparentNotify for window 0x%x", client->window);
1289 client_unmanage(client);
1292 ob_debug("MapRequest for 0x%lx", client->window);
1293 if (!client->iconic) break; /* this normally doesn't happen, but if it
1294 does, we don't want it!
1295 it can happen now when the window is on
1296 another desktop, but we still don't
1298 client_activate(client, FALSE, TRUE, TRUE, TRUE);
1301 /* validate cuz we query stuff off the client here */
1302 if (!client_validate(client)) break;
1304 if (e->xclient.format != 32) return;
1306 msgtype = e->xclient.message_type;
1307 if (msgtype == OBT_PROP_ATOM(WM_CHANGE_STATE)) {
1308 compress_client_message_event(e, &ce, client->window, msgtype);
1309 if (!client->locked)
1310 client_set_wm_state(client, e->xclient.data.l[0]);
1311 } else if (msgtype == OBT_PROP_ATOM(NET_WM_DESKTOP)) {
1312 compress_client_message_event(e, &ce, client->window, msgtype);
1313 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
1314 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
1315 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
1317 } else if (msgtype == OBT_PROP_ATOM(NET_WM_STATE)) {
1318 gulong ignore_start;
1320 /* can't compress these */
1321 ob_debug("net_wm_state %s %ld %ld for 0x%lx",
1322 (e->xclient.data.l[0] == 0 ? "Remove" :
1323 e->xclient.data.l[0] == 1 ? "Add" :
1324 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1325 e->xclient.data.l[1], e->xclient.data.l[2],
1328 if (!client->locked) {
1329 /* ignore enter events caused by these like ob actions do */
1330 if (!config_focus_under_mouse)
1331 ignore_start = event_start_ignore_all_enters();
1332 client_set_state(client, e->xclient.data.l[0],
1333 e->xclient.data.l[1], e->xclient.data.l[2]);
1334 if (!config_focus_under_mouse)
1335 event_end_ignore_all_enters(ignore_start);
1337 } else if (msgtype == OBT_PROP_ATOM(NET_CLOSE_WINDOW)) {
1338 ob_debug("net_close_window for 0x%lx", client->window);
1339 if (!client->locked)
1340 client_close(client);
1341 } else if (msgtype == OBT_PROP_ATOM(NET_ACTIVE_WINDOW)) {
1342 ob_debug("net_active_window for 0x%lx source=%s",
1344 (e->xclient.data.l[0] == 0 ? "unknown" :
1345 (e->xclient.data.l[0] == 1 ? "application" :
1346 (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1347 /* XXX make use of data.l[2] !? */
1348 if (e->xclient.data.l[0] == 1 || e->xclient.data.l[0] == 2) {
1349 /* don't use the user's timestamp for client_focus, cuz if it's
1350 an old broken timestamp (happens all the time) then focus
1351 won't move even though we're trying to move it
1352 event_curtime = e->xclient.data.l[1];*/
1353 if (e->xclient.data.l[1] == 0)
1354 ob_debug_type(OB_DEBUG_APP_BUGS,
1355 "_NET_ACTIVE_WINDOW message for window %s is"
1356 " missing a timestamp", client->title);
1358 ob_debug_type(OB_DEBUG_APP_BUGS,
1359 "_NET_ACTIVE_WINDOW message for window %s is "
1360 "missing source indication");
1361 client_activate(client, TRUE, TRUE, TRUE,
1362 (e->xclient.data.l[0] == 0 ||
1363 e->xclient.data.l[0] == 2));
1364 } else if (msgtype == OBT_PROP_ATOM(NET_WM_MOVERESIZE)) {
1365 ob_debug("net_wm_moveresize for 0x%lx direction %d",
1366 client->window, e->xclient.data.l[2]);
1367 if ((Atom)e->xclient.data.l[2] ==
1368 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOPLEFT) ||
1369 (Atom)e->xclient.data.l[2] ==
1370 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOP) ||
1371 (Atom)e->xclient.data.l[2] ==
1372 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOPRIGHT) ||
1373 (Atom)e->xclient.data.l[2] ==
1374 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_RIGHT) ||
1375 (Atom)e->xclient.data.l[2] ==
1376 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_RIGHT) ||
1377 (Atom)e->xclient.data.l[2] ==
1378 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT) ||
1379 (Atom)e->xclient.data.l[2] ==
1380 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOM) ||
1381 (Atom)e->xclient.data.l[2] ==
1382 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT) ||
1383 (Atom)e->xclient.data.l[2] ==
1384 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_LEFT) ||
1385 (Atom)e->xclient.data.l[2] ==
1386 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE) ||
1387 (Atom)e->xclient.data.l[2] ==
1388 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_KEYBOARD) ||
1389 (Atom)e->xclient.data.l[2] ==
1390 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE_KEYBOARD))
1392 moveresize_start(client, e->xclient.data.l[0],
1393 e->xclient.data.l[1], e->xclient.data.l[3],
1394 e->xclient.data.l[2]);
1396 else if ((Atom)e->xclient.data.l[2] ==
1397 OBT_PROP_ATOM(NET_WM_MOVERESIZE_CANCEL))
1398 moveresize_end(TRUE);
1399 } 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];
1434 if (!(e->xclient.data.l[0] & 1 << 9))
1435 client_gravity_resize_h(client, &y, client->area.height,h);
1438 h = client->area.height;
1440 ob_debug("MOVERESIZE x %d %d y %d %d (gravity %d)",
1441 e->xclient.data.l[0] & 1 << 8, x,
1442 e->xclient.data.l[0] & 1 << 9, y,
1445 client_find_onscreen(client, &x, &y, w, h, FALSE);
1447 client_configure(client, x, y, w, h, FALSE, TRUE, FALSE);
1449 client->gravity = ograv;
1450 } else if (msgtype == OBT_PROP_ATOM(NET_RESTACK_WINDOW)) {
1451 if (e->xclient.data.l[0] != 2) {
1452 ob_debug_type(OB_DEBUG_APP_BUGS,
1453 "_NET_RESTACK_WINDOW sent for window %s with "
1454 "invalid source indication %ld",
1455 client->title, e->xclient.data.l[0]);
1457 ObClient *sibling = NULL;
1458 if (e->xclient.data.l[1]) {
1459 ObWindow *win = window_find(e->xclient.data.l[1]);
1460 if (WINDOW_IS_CLIENT(win) &&
1461 WINDOW_AS_CLIENT(win) != client)
1463 sibling = WINDOW_AS_CLIENT(win);
1465 if (sibling == NULL)
1466 ob_debug_type(OB_DEBUG_APP_BUGS,
1467 "_NET_RESTACK_WINDOW sent for window %s "
1468 "with invalid sibling 0x%x",
1469 client->title, e->xclient.data.l[1]);
1471 if (e->xclient.data.l[2] == Below ||
1472 e->xclient.data.l[2] == BottomIf ||
1473 e->xclient.data.l[2] == Above ||
1474 e->xclient.data.l[2] == TopIf ||
1475 e->xclient.data.l[2] == Opposite)
1477 gulong ignore_start;
1479 if (!config_focus_under_mouse)
1480 ignore_start = event_start_ignore_all_enters();
1481 /* just raise, don't activate */
1482 stacking_restack_request(client, sibling,
1483 e->xclient.data.l[2]);
1484 if (!config_focus_under_mouse)
1485 event_end_ignore_all_enters(ignore_start);
1487 /* send a synthetic ConfigureNotify, cuz this is supposed
1488 to be like a ConfigureRequest. */
1489 client_reconfigure(client, TRUE);
1491 ob_debug_type(OB_DEBUG_APP_BUGS,
1492 "_NET_RESTACK_WINDOW sent for window %s "
1493 "with invalid detail %d",
1494 client->title, e->xclient.data.l[2]);
1498 case PropertyNotify:
1499 /* validate cuz we query stuff off the client here */
1500 if (!client_validate(client)) break;
1502 /* compress changes to a single property into a single change */
1503 while (XCheckTypedWindowEvent(obt_display, client->window,
1507 /* XXX: it would be nice to compress ALL changes to a property,
1508 not just changes in a row without other props between. */
1510 a = ce.xproperty.atom;
1511 b = e->xproperty.atom;
1515 if ((a == OBT_PROP_ATOM(NET_WM_NAME) ||
1516 a == OBT_PROP_ATOM(WM_NAME) ||
1517 a == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1518 a == OBT_PROP_ATOM(WM_ICON_NAME))
1520 (b == OBT_PROP_ATOM(NET_WM_NAME) ||
1521 b == OBT_PROP_ATOM(WM_NAME) ||
1522 b == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1523 b == OBT_PROP_ATOM(WM_ICON_NAME))) {
1526 if (a == OBT_PROP_ATOM(NET_WM_ICON) &&
1527 b == OBT_PROP_ATOM(NET_WM_ICON))
1530 XPutBackEvent(obt_display, &ce);
1534 msgtype = e->xproperty.atom;
1535 if (msgtype == XA_WM_NORMAL_HINTS) {
1536 ob_debug("Update NORMAL hints");
1537 client_update_normal_hints(client);
1538 /* normal hints can make a window non-resizable */
1539 client_setup_decor_and_functions(client, FALSE);
1541 /* make sure the client's sizes are within its bounds, but only
1542 reconfigure the window if it needs to. emacs will update its
1543 normal hints every time it receives a conigurenotify */
1544 client_reconfigure(client, FALSE);
1545 } else if (msgtype == XA_WM_HINTS) {
1546 client_update_wmhints(client);
1547 } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1548 client_update_transient_for(client);
1549 client_get_type_and_transientness(client);
1550 /* type may have changed, so update the layer */
1551 client_calc_layer(client);
1552 client_setup_decor_and_functions(client, TRUE);
1553 } else if (msgtype == OBT_PROP_ATOM(NET_WM_NAME) ||
1554 msgtype == OBT_PROP_ATOM(WM_NAME) ||
1555 msgtype == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1556 msgtype == OBT_PROP_ATOM(WM_ICON_NAME)) {
1557 client_update_title(client);
1558 } else if (msgtype == OBT_PROP_ATOM(WM_PROTOCOLS)) {
1559 client_update_protocols(client);
1560 client_setup_decor_and_functions(client, TRUE);
1562 else if (msgtype == OBT_PROP_ATOM(NET_WM_STRUT) ||
1563 msgtype == OBT_PROP_ATOM(NET_WM_STRUT_PARTIAL)) {
1564 client_update_strut(client);
1566 else if (msgtype == OBT_PROP_ATOM(NET_WM_ICON)) {
1567 client_update_icons(client);
1569 else if (msgtype == OBT_PROP_ATOM(NET_WM_ICON_GEOMETRY)) {
1570 client_update_icon_geometry(client);
1572 else if (msgtype == OBT_PROP_ATOM(NET_WM_USER_TIME)) {
1574 if (client == focus_client &&
1575 OBT_PROP_GET32(client->window, NET_WM_USER_TIME, CARDINAL, &t)
1576 && t && !event_time_after(t, e->xproperty.time) &&
1577 (!event_last_user_time ||
1578 event_time_after(t, event_last_user_time)))
1580 event_last_user_time = t;
1584 else if (msgtype == OBT_PROP_ATOM(NET_WM_SYNC_REQUEST_COUNTER)) {
1585 client_update_sync_request_counter(client);
1589 case ColormapNotify:
1590 client_update_colormap(client, e->xcolormap.colormap);
1595 if (obt_display_extension_shape &&
1596 e->type == obt_display_extension_shape_basep)
1598 client->shaped = ((XShapeEvent*)e)->shaped;
1599 frame_adjust_shape(client->frame);
1605 static void event_handle_dock(ObDock *s, XEvent *e)
1609 if (e->xbutton.button == 1)
1610 stacking_raise(DOCK_AS_WINDOW(s));
1611 else if (e->xbutton.button == 2)
1612 stacking_lower(DOCK_AS_WINDOW(s));
1618 /* don't hide when moving into a dock app */
1619 if (e->xcrossing.detail != NotifyInferior)
1625 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1629 dock_app_drag(app, &e->xmotion);
1632 if (app->ignore_unmaps) {
1633 app->ignore_unmaps--;
1636 dock_unmanage(app, TRUE);
1639 case ReparentNotify:
1640 dock_unmanage(app, FALSE);
1642 case ConfigureNotify:
1643 dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1648 static ObMenuFrame* find_active_menu(void)
1651 ObMenuFrame *ret = NULL;
1653 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1662 static ObMenuFrame* find_active_or_last_menu(void)
1664 ObMenuFrame *ret = NULL;
1666 ret = find_active_menu();
1667 if (!ret && menu_frame_visible)
1668 ret = menu_frame_visible->data;
1672 static gboolean event_handle_prompt(ObPrompt *p, XEvent *e)
1678 return prompt_mouse_event(p, e);
1681 return prompt_key_event(p, e);
1687 static gboolean event_handle_menu_input(XEvent *ev)
1689 gboolean ret = FALSE;
1691 if (ev->type == ButtonRelease || ev->type == ButtonPress) {
1692 ObMenuEntryFrame *e;
1694 if (menu_hide_delay_reached() &&
1695 (ev->xbutton.button < 4 || ev->xbutton.button > 5))
1697 if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1698 ev->xbutton.y_root)))
1700 if (ev->type == ButtonPress && e->frame->child)
1701 menu_frame_select(e->frame->child, NULL, TRUE);
1702 menu_frame_select(e->frame, e, TRUE);
1703 if (ev->type == ButtonRelease)
1704 menu_entry_frame_execute(e, ev->xbutton.state);
1706 else if (ev->type == ButtonRelease)
1707 menu_frame_hide_all();
1711 else if (ev->type == MotionNotify) {
1713 ObMenuEntryFrame *e;
1715 if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1716 ev->xmotion.y_root)))
1717 if (!(f = find_active_menu()) ||
1719 f->parent == e->frame ||
1720 f->child == e->frame)
1721 menu_frame_select(e->frame, e, FALSE);
1723 else if (ev->type == KeyPress || ev->type == KeyRelease) {
1724 guint keycode, state;
1728 keycode = ev->xkey.keycode;
1729 state = ev->xkey.state;
1730 unikey = obt_keyboard_keycode_to_unichar(keycode);
1732 frame = find_active_or_last_menu();
1734 g_assert_not_reached(); /* there is no active menu */
1736 /* Allow control while going thru the menu */
1737 else if (ev->type == KeyPress && (state & ~ControlMask) == 0) {
1738 frame->got_press = TRUE;
1740 if (keycode == ob_keycode(OB_KEY_ESCAPE)) {
1741 menu_frame_hide_all();
1745 else if (keycode == ob_keycode(OB_KEY_LEFT)) {
1746 /* Left goes to the parent menu */
1748 menu_frame_select(frame, NULL, TRUE);
1752 else if (keycode == ob_keycode(OB_KEY_RIGHT)) {
1753 /* Right goes to the selected submenu */
1754 if (frame->child) menu_frame_select_next(frame->child);
1758 else if (keycode == ob_keycode(OB_KEY_UP)) {
1759 menu_frame_select_previous(frame);
1763 else if (keycode == ob_keycode(OB_KEY_DOWN)) {
1764 menu_frame_select_next(frame);
1769 /* Use KeyRelease events for running things so that the key release
1770 doesn't get sent to the focused application.
1772 Allow ControlMask only, and don't bother if the menu is empty */
1773 else if (ev->type == KeyRelease && (state & ~ControlMask) == 0 &&
1774 frame->entries && frame->got_press)
1776 if (keycode == ob_keycode(OB_KEY_RETURN)) {
1777 /* Enter runs the active item or goes into the submenu.
1778 Control-Enter runs it without closing the menu. */
1780 menu_frame_select_next(frame->child);
1781 else if (frame->selected)
1782 menu_entry_frame_execute(frame->selected, state);
1787 /* keyboard accelerator shortcuts. (if it was a valid key) */
1788 else if (unikey != 0) {
1791 ObMenuEntryFrame *found = NULL;
1792 guint num_found = 0;
1794 /* start after the selected one */
1795 start = frame->entries;
1796 if (frame->selected) {
1797 for (it = start; frame->selected != it->data;
1798 it = g_list_next(it))
1799 g_assert(it != NULL); /* nothing was selected? */
1800 /* next with wraparound */
1801 start = g_list_next(it);
1802 if (start == NULL) start = frame->entries;
1807 ObMenuEntryFrame *e = it->data;
1808 gunichar entrykey = 0;
1810 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1811 entrykey = e->entry->data.normal.shortcut;
1812 else if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1813 entrykey = e->entry->data.submenu.submenu->shortcut;
1815 if (unikey == entrykey) {
1816 if (found == NULL) found = e;
1820 /* next with wraparound */
1821 it = g_list_next(it);
1822 if (it == NULL) it = frame->entries;
1823 } while (it != start);
1826 if (found->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1829 menu_frame_select(frame, found, TRUE);
1830 usleep(50000); /* highlight the item for a short bit so
1831 the user can see what happened */
1832 menu_entry_frame_execute(found, state);
1834 menu_frame_select(frame, found, TRUE);
1836 menu_frame_select_next(frame->child);
1848 static void event_handle_menu(ObMenuFrame *frame, XEvent *ev)
1851 ObMenuEntryFrame *e;
1855 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1856 if (e->ignore_enters)
1858 else if (!(f = find_active_menu()) ||
1860 f->parent == e->frame ||
1861 f->child == e->frame)
1862 menu_frame_select(e->frame, e, FALSE);
1866 /*ignore leaves when we're already in the window */
1867 if (ev->xcrossing.detail == NotifyInferior)
1870 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1871 (f = find_active_menu()) && f->selected == e &&
1872 e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1874 menu_frame_select(e->frame, NULL, FALSE);
1880 static void event_handle_internal(ObInternalWindow *internal, XEvent *ev)
1886 static void event_handle_user_input(ObClient *client, XEvent *e)
1888 g_assert(e->type == ButtonPress || e->type == ButtonRelease ||
1889 e->type == MotionNotify || e->type == KeyPress ||
1890 e->type == KeyRelease);
1892 if (menu_frame_visible) {
1893 if (event_handle_menu_input(e))
1894 /* don't use the event if the menu used it, but if the menu
1895 didn't use it and it's a keypress that is bound, it will
1896 close the menu and be used */
1900 /* if the keyboard interactive action uses the event then dont
1901 use it for bindings. likewise is moveresize uses the event. */
1902 if (!actions_interactive_input_event(e) && !moveresize_event(e)) {
1903 if (moveresize_in_progress)
1904 /* make further actions work on the client being
1906 client = moveresize_client;
1908 if (e->type == ButtonPress ||
1909 e->type == ButtonRelease ||
1910 e->type == MotionNotify)
1912 /* the frame may not be "visible" but they can still click on it
1913 in the case where it is animating before disappearing */
1914 if (!client || !frame_iconify_animating(client->frame))
1915 mouse_event(client, e);
1917 keyboard_event(event_target_client(client), e);
1921 ObClient* event_target_client(ObClient *client)
1923 return (focus_cycle_target ? focus_cycle_target :
1924 (client ? client : focus_client));
1927 static void focus_delay_dest(gpointer data)
1932 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1934 const ObFocusDelayData *f1 = d1;
1935 return f1->client == d2;
1938 static gboolean focus_delay_func(gpointer data)
1940 ObFocusDelayData *d = data;
1941 Time old = event_curtime;
1943 event_curtime = d->time;
1944 event_curserial = d->serial;
1945 if (client_focus(d->client) && config_focus_raise)
1946 stacking_raise(CLIENT_AS_WINDOW(d->client));
1947 event_curtime = old;
1948 return FALSE; /* no repeat */
1951 static void focus_delay_client_dest(ObClient *client, gpointer data)
1953 obt_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1957 void event_halt_focus_delay(void)
1959 /* ignore all enter events up till the event which caused this to occur */
1960 if (event_curserial) event_ignore_enter_range(1, event_curserial);
1961 obt_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1964 gulong event_start_ignore_all_enters(void)
1966 return NextRequest(obt_display);
1969 static void event_ignore_enter_range(gulong start, gulong end)
1973 g_assert(start != 0);
1976 r = g_new(ObSerialRange, 1);
1979 ignore_serials = g_slist_prepend(ignore_serials, r);
1981 ob_debug_type(OB_DEBUG_FOCUS, "ignoring enters from %lu until %lu",
1984 /* increment the serial so we don't ignore events we weren't meant to */
1985 OBT_PROP_ERASE(screen_support_win, MOTIF_WM_HINTS);
1988 void event_end_ignore_all_enters(gulong start)
1990 /* Use (NextRequest-1) so that we ignore up to the current serial only.
1991 Inside event_ignore_enter_range, we increment the serial by one, but if
1992 we ignore that serial too, then any enter events generated by mouse
1993 movement will be ignored until we create some further network traffic.
1994 Instead ignore up to NextRequest-1, then when we increment the serial,
1995 we will be *past* the range of ignored serials */
1996 event_ignore_enter_range(start, NextRequest(obt_display)-1);
1999 static gboolean is_enter_focus_event_ignored(gulong serial)
2003 for (it = ignore_serials; it; it = next) {
2004 ObSerialRange *r = it->data;
2006 next = g_slist_next(it);
2008 if ((glong)(serial - r->end) > 0) {
2010 ignore_serials = g_slist_delete_link(ignore_serials, it);
2013 else if ((glong)(serial - r->start) >= 0)
2019 void event_cancel_all_key_grabs(void)
2022 if (actions_interactive_act_running()) {
2023 actions_interactive_cancel_act();
2024 ob_debug("KILLED interactive action");
2026 else if (menu_frame_visible) {
2027 menu_frame_hide_all();
2028 ob_debug("KILLED open menus");
2030 else if (moveresize_in_progress) {
2031 moveresize_end(TRUE);
2032 ob_debug("KILLED interactive moveresize");
2034 else if (grab_on_keyboard()) {
2036 ob_debug("KILLED active grab on keyboard");
2039 ungrab_passive_key();
2041 XSync(obt_display, FALSE);
2044 gboolean event_time_after(Time t1, Time t2)
2046 g_assert(t1 != CurrentTime);
2047 g_assert(t2 != CurrentTime);
2050 Timestamp values wrap around (after about 49.7 days). The server, given
2051 its current time is represented by timestamp T, always interprets
2052 timestamps from clients by treating half of the timestamp space as being
2053 later in time than T.
2054 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
2057 /* TIME_HALF is half of the number space of a Time type variable */
2058 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
2060 if (t2 >= TIME_HALF)
2061 /* t2 is in the second half so t1 might wrap around and be smaller than
2063 return t1 >= t2 || t1 < (t2 + TIME_HALF);
2065 /* t2 is in the first half so t1 has to come after it */
2066 return t1 >= t2 && t1 < (t2 + TIME_HALF);
2069 Time event_get_server_time(void)
2071 /* Generate a timestamp */
2074 XChangeProperty(obt_display, screen_support_win,
2075 OBT_PROP_ATOM(WM_CLASS), OBT_PROP_ATOM(STRING),
2076 8, PropModeAppend, NULL, 0);
2077 XWindowEvent(obt_display, screen_support_win, PropertyChangeMask, &event);
2078 return event.xproperty.time;