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 gboolean event_handle_prompt(ObPrompt *p, XEvent *e);
91 static void event_handle_dock(ObDock *s, XEvent *e);
92 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
93 static void event_handle_client(ObClient *c, XEvent *e);
94 static void event_handle_user_input(ObClient *client, XEvent *e);
95 static gboolean is_enter_focus_event_ignored(gulong serial);
96 static void event_ignore_enter_range(gulong start, gulong end);
98 static void focus_delay_dest(gpointer data);
99 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
100 static gboolean focus_delay_func(gpointer data);
101 static void focus_delay_client_dest(ObClient *client, gpointer data);
103 Time event_curtime = CurrentTime;
104 Time event_last_user_time = CurrentTime;
105 Time client_swoon = 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;
475 /* make a copy we can mangle */
479 window = event_get_window(e);
480 if (window == obt_root(ob_screen))
481 /* don't do any lookups, waste of cpu */;
482 else if ((obwin = window_find(window))) {
483 switch (obwin->type) {
484 case OB_WINDOW_CLASS_DOCK:
485 dock = WINDOW_AS_DOCK(obwin);
487 case OB_WINDOW_CLASS_CLIENT:
488 client = WINDOW_AS_CLIENT(obwin);
489 /* events on clients can be events on prompt windows too */
490 prompt = client->prompt;
492 case OB_WINDOW_CLASS_MENUFRAME:
493 menu = WINDOW_AS_MENUFRAME(obwin);
495 case OB_WINDOW_CLASS_INTERNAL:
496 /* we don't do anything with events directly on these windows */
498 case OB_WINDOW_CLASS_PROMPT:
499 prompt = WINDOW_AS_PROMPT(obwin);
504 dockapp = dock_find_dockapp(window);
506 event_set_curtime(e);
507 event_curserial = e->xany.serial;
509 if (event_ignore(e, client)) {
516 /* deal with it in the kernel */
518 if (e->type == FocusIn) {
520 e->xfocus.detail == NotifyInferior)
522 ob_debug_type(OB_DEBUG_FOCUS,
523 "Focus went to the frame window");
525 focus_left_screen = FALSE;
527 focus_fallback(FALSE, config_focus_under_mouse, TRUE, TRUE);
529 /* We don't get a FocusOut for this case, because it's just moving
530 from our Inferior up to us. This happens when iconifying a
531 window with RevertToParent focus */
532 frame_adjust_focus(client->frame, FALSE);
533 /* focus_set_client(NULL) has already been called */
535 else if (e->xfocus.detail == NotifyPointerRoot ||
536 e->xfocus.detail == NotifyDetailNone ||
537 e->xfocus.detail == NotifyInferior ||
538 e->xfocus.detail == NotifyNonlinear)
542 ob_debug_type(OB_DEBUG_FOCUS,
543 "Focus went to root or pointer root/none");
545 if (e->xfocus.detail == NotifyInferior ||
546 e->xfocus.detail == NotifyNonlinear)
548 focus_left_screen = FALSE;
551 /* If another FocusIn is in the queue then don't fallback yet. This
552 fixes the fun case of:
553 window map -> send focusin
554 window unmap -> get focusout
555 window map -> send focusin
556 get first focus out -> fall back to something (new window
557 hasn't received focus yet, so something else) -> send focusin
558 which means the "something else" is the last thing to get a
559 focusin sent to it, so the new window doesn't end up with focus.
561 But if the other focus in is something like PointerRoot then we
562 still want to fall back.
564 if (XCheckIfEvent(obt_display, &ce, event_look_for_focusin_client,
567 XPutBackEvent(obt_display, &ce);
568 ob_debug_type(OB_DEBUG_FOCUS,
569 " but another FocusIn is coming");
571 /* Focus has been reverted.
573 FocusOut events come after UnmapNotify, so we don't need to
574 worry about focusing an invalid window
577 if (!focus_left_screen)
578 focus_fallback(FALSE, config_focus_under_mouse,
584 ob_debug_type(OB_DEBUG_FOCUS,
585 "Focus went to a window that is already gone");
587 /* If you send focus to a window and then it disappears, you can
588 get the FocusIn for it, after it is unmanaged.
589 Just wait for the next FocusOut/FocusIn pair, but make note that
590 the window that was focused no longer is. */
591 focus_set_client(NULL);
593 else if (client != focus_client) {
594 focus_left_screen = FALSE;
595 if (!focus_cycle_target || !config_focus_dontstop) {
596 frame_adjust_focus(client->frame, TRUE);
597 focus_set_client(client);
598 client_calc_layer(client);
599 client_bring_helper_windows(client);
602 } else if (e->type == FocusOut) {
605 /* Look for the followup FocusIn */
606 if (!XCheckIfEvent(obt_display, &ce, event_look_for_focusin, NULL)) {
607 /* There is no FocusIn, this means focus went to a window that
608 is not being managed, or a window on another screen. */
612 obt_display_ignore_errors(TRUE);
613 if (XGetInputFocus(obt_display, &win, &i) &&
614 XGetGeometry(obt_display, win, &root, &i,&i,&u,&u,&u,&u) &&
615 root != obt_root(ob_screen))
617 ob_debug_type(OB_DEBUG_FOCUS,
618 "Focus went to another screen !");
619 focus_left_screen = TRUE;
622 ob_debug_type(OB_DEBUG_FOCUS,
623 "Focus went to a black hole !");
624 obt_display_ignore_errors(FALSE);
625 /* nothing is focused */
626 focus_set_client(NULL);
628 /* Focus moved, so process the FocusIn event */
629 ObEventData ed = { .ignored = FALSE };
630 event_process(&ce, &ed);
632 /* The FocusIn was ignored, this means it was on a window
633 that isn't a client. */
634 ob_debug_type(OB_DEBUG_FOCUS,
635 "Focus went to an unmanaged window 0x%x !",
637 focus_fallback(TRUE, config_focus_under_mouse, TRUE, TRUE);
641 if (client && client != focus_client) {
642 frame_adjust_focus(client->frame, FALSE);
643 /* focus_set_client(NULL) has already been called in this
644 section or by focus_fallback */
648 event_handle_client(client, e);
650 event_handle_dockapp(dockapp, e);
652 event_handle_dock(dock, e);
654 event_handle_menu(menu, e);
655 else if (window == obt_root(ob_screen))
656 event_handle_root(e);
657 else if (e->type == MapRequest)
658 window_manage(window);
659 else if (e->type == MappingNotify) {
660 /* keyboard layout changes for modifier mapping changes. reload the
661 modifier map, and rebind all the key bindings as appropriate */
662 ob_debug("Kepboard map changed. Reloading keyboard bindings.");
663 ob_set_state(OB_STATE_RECONFIGURING);
664 obt_keyboard_reload();
666 ob_set_state(OB_STATE_RUNNING);
668 else if (e->type == ClientMessage) {
669 /* This is for _NET_WM_REQUEST_FRAME_EXTENTS messages. They come for
670 windows that are not managed yet. */
671 if (e->xclient.message_type ==
672 OBT_PROP_ATOM(NET_REQUEST_FRAME_EXTENTS))
674 /* Pretend to manage the client, getting information used to
675 determine its decorations */
676 ObClient *c = client_fake_manage(e->xclient.window);
679 /* set the frame extents on the window */
680 vals[0] = c->frame->size.left;
681 vals[1] = c->frame->size.right;
682 vals[2] = c->frame->size.top;
683 vals[3] = c->frame->size.bottom;
684 OBT_PROP_SETA32(e->xclient.window, NET_FRAME_EXTENTS,
687 /* Free the pretend client */
688 client_fake_unmanage(c);
691 else if (e->type == ConfigureRequest) {
692 /* unhandled configure requests must be used to configure the
696 xwc.x = e->xconfigurerequest.x;
697 xwc.y = e->xconfigurerequest.y;
698 xwc.width = e->xconfigurerequest.width;
699 xwc.height = e->xconfigurerequest.height;
700 xwc.border_width = e->xconfigurerequest.border_width;
701 xwc.sibling = e->xconfigurerequest.above;
702 xwc.stack_mode = e->xconfigurerequest.detail;
704 /* we are not to be held responsible if someone sends us an
706 obt_display_ignore_errors(TRUE);
707 XConfigureWindow(obt_display, window,
708 e->xconfigurerequest.value_mask, &xwc);
709 obt_display_ignore_errors(FALSE);
712 else if (obt_display_extension_sync &&
713 e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
715 XSyncAlarmNotifyEvent *se = (XSyncAlarmNotifyEvent*)e;
716 if (se->alarm == moveresize_alarm && moveresize_in_progress)
721 if (prompt && event_handle_prompt(prompt, e))
723 else if (e->type == ButtonPress || e->type == ButtonRelease) {
724 /* If the button press was on some non-root window, or was physically
725 on the root window, then process it */
726 if (window != obt_root(ob_screen) ||
727 e->xbutton.subwindow == None)
729 event_handle_user_input(client, e);
731 /* Otherwise only process it if it was physically on an openbox
736 if ((w = window_find(e->xbutton.subwindow)) &&
737 WINDOW_IS_INTERNAL(w))
739 event_handle_user_input(client, e);
743 else if (e->type == KeyPress || e->type == KeyRelease ||
744 e->type == MotionNotify)
745 event_handle_user_input(client, e);
749 /* run all the hooks at once */
752 /* if something happens and it's not from an XEvent, then we don't know
754 event_curtime = CurrentTime;
758 static void event_handle_root(XEvent *e)
764 ob_debug("Another WM has requested to replace us. Exiting.");
769 if (e->xclient.format != 32) break;
771 msgtype = e->xclient.message_type;
772 if (msgtype == OBT_PROP_ATOM(NET_CURRENT_DESKTOP)) {
773 guint d = e->xclient.data.l[0];
774 if (d < screen_num_desktops) {
775 event_curtime = e->xclient.data.l[1];
776 if (event_curtime == 0)
777 ob_debug_type(OB_DEBUG_APP_BUGS,
778 "_NET_CURRENT_DESKTOP message is missing "
780 screen_set_desktop(d, TRUE);
782 } else if (msgtype == OBT_PROP_ATOM(NET_NUMBER_OF_DESKTOPS)) {
783 guint d = e->xclient.data.l[0];
784 if (d > 0 && d <= 1000)
785 screen_set_num_desktops(d);
786 } else if (msgtype == OBT_PROP_ATOM(NET_SHOWING_DESKTOP)) {
787 screen_show_desktop(e->xclient.data.l[0] != 0, NULL);
788 } else if (msgtype == OBT_PROP_ATOM(OB_CONTROL)) {
789 ob_debug("OB_CONTROL: %d", e->xclient.data.l[0]);
790 if (e->xclient.data.l[0] == 1)
792 else if (e->xclient.data.l[0] == 2)
794 else if (e->xclient.data.l[0] == 3)
796 } else if (msgtype == OBT_PROP_ATOM(WM_PROTOCOLS)) {
797 if ((Atom)e->xclient.data.l[0] == OBT_PROP_ATOM(NET_WM_PING))
798 ping_got_pong(e->xclient.data.l[1]);
802 if (e->xproperty.atom == OBT_PROP_ATOM(NET_DESKTOP_NAMES)) {
803 ob_debug("UPDATE DESKTOP NAMES");
804 screen_update_desktop_names();
806 else if (e->xproperty.atom == OBT_PROP_ATOM(NET_DESKTOP_LAYOUT))
807 screen_update_layout();
809 case ConfigureNotify:
811 XRRUpdateConfiguration(e);
820 void event_enter_client(ObClient *client)
822 g_assert(config_focus_follow);
824 if (is_enter_focus_event_ignored(event_curserial)) {
825 ob_debug_type(OB_DEBUG_FOCUS, "Ignoring enter event with serial %lu\n"
826 "on client 0x%x", event_curserial, client->window);
830 if (client_enter_focusable(client) && client_can_focus(client) && event_time_after(client_swoon, event_curtime - config_focus_delay /*milliseconds here, so not *1000 */)) {
831 if (config_focus_delay) {
832 ObFocusDelayData *data;
834 obt_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
836 data = g_new(ObFocusDelayData, 1);
837 data->client = client;
838 data->time = event_curtime;
839 data->serial = event_curserial;
841 obt_main_loop_timeout_add(ob_main_loop,
842 config_focus_delay * 1000,
844 data, focus_delay_cmp, focus_delay_dest);
846 ObFocusDelayData data;
847 data.client = client;
848 data.time = event_curtime;
849 data.serial = event_curserial;
850 focus_delay_func(&data);
855 static gboolean *context_to_button(ObFrame *f, ObFrameContext con, gboolean press)
859 case OB_FRAME_CONTEXT_MAXIMIZE:
860 return &f->max_press;
861 case OB_FRAME_CONTEXT_CLOSE:
862 return &f->close_press;
863 case OB_FRAME_CONTEXT_ICONIFY:
864 return &f->iconify_press;
865 case OB_FRAME_CONTEXT_ALLDESKTOPS:
866 return &f->desk_press;
867 case OB_FRAME_CONTEXT_SHADE:
868 return &f->shade_press;
874 case OB_FRAME_CONTEXT_MAXIMIZE:
875 return &f->max_hover;
876 case OB_FRAME_CONTEXT_CLOSE:
877 return &f->close_hover;
878 case OB_FRAME_CONTEXT_ICONIFY:
879 return &f->iconify_hover;
880 case OB_FRAME_CONTEXT_ALLDESKTOPS:
881 return &f->desk_hover;
882 case OB_FRAME_CONTEXT_SHADE:
883 return &f->shade_hover;
890 static void compress_client_message_event(XEvent *e, XEvent *ce, Window window,
893 /* compress changes into a single change */
894 while (XCheckTypedWindowEvent(obt_display, window, e->type, ce)) {
895 /* XXX: it would be nice to compress ALL messages of a
896 type, not just messages in a row without other
897 message types between. */
898 if (ce->xclient.message_type != msgtype) {
899 XPutBackEvent(obt_display, ce);
902 e->xclient = ce->xclient;
906 static void event_handle_client(ObClient *client, XEvent *e)
912 static gint px = -1, py = -1;
914 static ObFrameContext pcon = OB_FRAME_CONTEXT_NONE;
918 /* save where the press occured for the first button pressed */
920 pb = e->xbutton.button;
924 pcon = frame_context(client, e->xbutton.window, px, py);
925 pcon = mouse_button_frame_context(pcon, e->xbutton.button,
929 /* Wheel buttons don't draw because they are an instant click, so it
930 is a waste of resources to go drawing it.
931 if the user is doing an interactive thing, or has a menu open then
932 the mouse is grabbed (possibly) and if we get these events we don't
933 want to deal with them
935 if (!(e->xbutton.button == 4 || e->xbutton.button == 5) &&
938 /* use where the press occured */
939 con = frame_context(client, e->xbutton.window, px, py);
940 con = mouse_button_frame_context(con, e->xbutton.button,
943 /* button presses on CLIENT_CONTEXTs are not accompanied by a
944 release because they are Replayed to the client */
945 if ((e->type == ButtonRelease || CLIENT_CONTEXT(con, client)) &&
946 e->xbutton.button == pb)
947 pb = 0, px = py = -1, pcon = OB_FRAME_CONTEXT_NONE;
949 but = context_to_button(client->frame, con, TRUE);
951 *but = (e->type == ButtonPress);
952 frame_adjust_state(client->frame);
957 /* when there is a grab on the pointer, we won't get enter/leave
958 notifies, but we still get motion events */
959 if (grab_on_pointer()) break;
961 con = frame_context(client, e->xmotion.window,
962 e->xmotion.x, e->xmotion.y);
964 case OB_FRAME_CONTEXT_TITLEBAR:
965 case OB_FRAME_CONTEXT_TLCORNER:
966 case OB_FRAME_CONTEXT_TRCORNER:
967 /* we've left the button area inside the titlebar */
968 if (client->frame->max_hover || client->frame->desk_hover ||
969 client->frame->shade_hover || client->frame->iconify_hover ||
970 client->frame->close_hover)
972 client->frame->max_hover =
973 client->frame->desk_hover =
974 client->frame->shade_hover =
975 client->frame->iconify_hover =
976 client->frame->close_hover = FALSE;
977 frame_adjust_state(client->frame);
981 but = context_to_button(client->frame, con, FALSE);
982 if (but && !*but && !pb) {
984 frame_adjust_state(client->frame);
990 con = frame_context(client, e->xcrossing.window,
991 e->xcrossing.x, e->xcrossing.y);
993 case OB_FRAME_CONTEXT_TITLEBAR:
994 case OB_FRAME_CONTEXT_TLCORNER:
995 case OB_FRAME_CONTEXT_TRCORNER:
996 /* we've left the button area inside the titlebar */
997 client->frame->max_hover =
998 client->frame->desk_hover =
999 client->frame->shade_hover =
1000 client->frame->iconify_hover =
1001 client->frame->close_hover = FALSE;
1002 if (e->xcrossing.mode == NotifyGrab) {
1003 client->frame->max_press =
1004 client->frame->desk_press =
1005 client->frame->shade_press =
1006 client->frame->iconify_press =
1007 client->frame->close_press = FALSE;
1010 case OB_FRAME_CONTEXT_FRAME:
1011 /* When the mouse leaves an animating window, don't use the
1012 corresponding enter events. Pretend like the animating window
1013 doesn't even exist..! */
1014 if (frame_iconify_animating(client->frame))
1015 event_end_ignore_all_enters(event_start_ignore_all_enters());
1017 ob_debug_type(OB_DEBUG_FOCUS,
1018 "%sNotify mode %d detail %d on %lx",
1019 (e->type == EnterNotify ? "Enter" : "Leave"),
1021 e->xcrossing.detail, (client?client->window:0));
1022 if (grab_on_keyboard())
1024 if (config_focus_follow && config_focus_delay &&
1025 /* leave inferior events can happen when the mouse goes onto
1026 the window's border and then into the window before the
1028 e->xcrossing.detail != NotifyInferior)
1030 obt_main_loop_timeout_remove_data(ob_main_loop,
1033 if (client == focus_client)
1034 client_swoon = e->xcrossing.time;
1038 but = context_to_button(client->frame, con, FALSE);
1041 if (e->xcrossing.mode == NotifyGrab) {
1042 but = context_to_button(client->frame, con, TRUE);
1045 frame_adjust_state(client->frame);
1052 con = frame_context(client, e->xcrossing.window,
1053 e->xcrossing.x, e->xcrossing.y);
1055 case OB_FRAME_CONTEXT_FRAME:
1056 if (grab_on_keyboard())
1058 if (e->xcrossing.mode == NotifyGrab ||
1059 e->xcrossing.mode == NotifyUngrab ||
1060 /*ignore enters when we're already in the window */
1061 e->xcrossing.detail == NotifyInferior)
1063 ob_debug_type(OB_DEBUG_FOCUS,
1064 "%sNotify mode %d detail %d serial %lu on %lx "
1066 (e->type == EnterNotify ? "Enter" : "Leave"),
1068 e->xcrossing.detail,
1069 e->xcrossing.serial,
1070 client?client->window:0);
1073 ob_debug_type(OB_DEBUG_FOCUS,
1074 "%sNotify mode %d detail %d serial %lu on %lx, "
1076 (e->type == EnterNotify ? "Enter" : "Leave"),
1078 e->xcrossing.detail,
1079 e->xcrossing.serial,
1080 (client?client->window:0));
1081 if (config_focus_follow)
1082 event_enter_client(client);
1086 but = context_to_button(client->frame, con, FALSE);
1089 if (e->xcrossing.mode == NotifyUngrab) {
1090 but = context_to_button(client->frame, con, TRUE);
1091 *but = (con == pcon);
1093 frame_adjust_state(client->frame);
1099 case ConfigureRequest:
1104 /* dont compress these unless you're going to watch for property
1105 notifies in between (these can change what the configure would
1107 also you can't compress stacking events
1111 gboolean move = FALSE;
1112 gboolean resize = FALSE;
1114 /* get the current area */
1115 RECT_TO_DIMS(client->area, x, y, w, h);
1117 ob_debug("ConfigureRequest for \"%s\" desktop %d wmstate %d "
1120 screen_desktop, client->wmstate, client->frame->visible);
1121 ob_debug(" x %d y %d w %d h %d b %d",
1122 x, y, w, h, client->border_width);
1124 if (e->xconfigurerequest.value_mask & CWBorderWidth)
1125 if (client->border_width != e->xconfigurerequest.border_width) {
1126 client->border_width = e->xconfigurerequest.border_width;
1128 /* if the border width is changing then that is the same
1129 as requesting a resize, but we don't actually change
1130 the client's border, so it will change their root
1131 coordinates (since they include the border width) and
1132 we need to a notify then */
1136 if (e->xconfigurerequest.value_mask & CWStackMode) {
1137 ObClient *sibling = NULL;
1138 gulong ignore_start;
1141 /* get the sibling */
1142 if (e->xconfigurerequest.value_mask & CWSibling) {
1144 win = window_find(e->xconfigurerequest.above);
1145 if (win && WINDOW_IS_CLIENT(win) &&
1146 WINDOW_AS_CLIENT(win) != client)
1148 sibling = WINDOW_AS_CLIENT(win);
1151 /* an invalid sibling was specified so don't restack at
1152 all, it won't make sense no matter what we do */
1157 if (!config_focus_under_mouse)
1158 ignore_start = event_start_ignore_all_enters();
1159 stacking_restack_request(client, sibling,
1160 e->xconfigurerequest.detail);
1161 if (!config_focus_under_mouse)
1162 event_end_ignore_all_enters(ignore_start);
1165 /* a stacking change moves the window without resizing */
1169 if ((e->xconfigurerequest.value_mask & CWX) ||
1170 (e->xconfigurerequest.value_mask & CWY) ||
1171 (e->xconfigurerequest.value_mask & CWWidth) ||
1172 (e->xconfigurerequest.value_mask & CWHeight))
1174 /* don't allow clients to move shaded windows (fvwm does this)
1176 if (e->xconfigurerequest.value_mask & CWX) {
1177 if (!client->shaded)
1178 x = e->xconfigurerequest.x;
1181 if (e->xconfigurerequest.value_mask & CWY) {
1182 if (!client->shaded)
1183 y = e->xconfigurerequest.y;
1187 if (e->xconfigurerequest.value_mask & CWWidth) {
1188 w = e->xconfigurerequest.width;
1191 if (e->xconfigurerequest.value_mask & CWHeight) {
1192 h = e->xconfigurerequest.height;
1197 ob_debug("ConfigureRequest x(%d) %d y(%d) %d w(%d) %d h(%d) %d "
1198 "move %d resize %d",
1199 e->xconfigurerequest.value_mask & CWX, x,
1200 e->xconfigurerequest.value_mask & CWY, y,
1201 e->xconfigurerequest.value_mask & CWWidth, w,
1202 e->xconfigurerequest.value_mask & CWHeight, h,
1205 /* check for broken apps moving to their root position
1207 XXX remove this some day...that would be nice. right now all
1208 kde apps do this when they try activate themselves on another
1209 desktop. eg. open amarok window on desktop 1, switch to desktop
1210 2, click amarok tray icon. it will move by its decoration size.
1212 if (x != client->area.x &&
1213 x == (client->frame->area.x + client->frame->size.left -
1214 (gint)client->border_width) &&
1215 y != client->area.y &&
1216 y == (client->frame->area.y + client->frame->size.top -
1217 (gint)client->border_width) &&
1218 w == client->area.width &&
1219 h == client->area.height)
1221 ob_debug_type(OB_DEBUG_APP_BUGS,
1222 "Application %s is trying to move via "
1223 "ConfigureRequest to it's root window position "
1224 "but it is not using StaticGravity",
1230 /* they still requested a move, so don't change whether a
1231 notify is sent or not */
1237 client_try_configure(client, &x, &y, &w, &h, &lw, &lh, FALSE);
1239 /* if x was not given, then use gravity to figure out the new
1240 x. the reference point should not be moved */
1241 if ((e->xconfigurerequest.value_mask & CWWidth &&
1242 !(e->xconfigurerequest.value_mask & CWX)))
1243 client_gravity_resize_w(client, &x, client->area.width, w);
1245 if ((e->xconfigurerequest.value_mask & CWHeight &&
1246 !(e->xconfigurerequest.value_mask & CWY)))
1247 client_gravity_resize_h(client, &y, client->area.height,h);
1249 client_find_onscreen(client, &x, &y, w, h, FALSE);
1251 ob_debug("Granting ConfigureRequest x %d y %d w %d h %d",
1253 client_configure(client, x, y, w, h, FALSE, TRUE, TRUE);
1258 ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
1260 client->window, e->xunmap.event, e->xunmap.from_configure,
1261 client->ignore_unmaps);
1262 if (client->ignore_unmaps) {
1263 client->ignore_unmaps--;
1266 client_unmanage(client);
1269 ob_debug("DestroyNotify for window 0x%x", client->window);
1270 client_unmanage(client);
1272 case ReparentNotify:
1273 /* this is when the client is first taken captive in the frame */
1274 if (e->xreparent.parent == client->frame->window) break;
1277 This event is quite rare and is usually handled in unmapHandler.
1278 However, if the window is unmapped when the reparent event occurs,
1279 the window manager never sees it because an unmap event is not sent
1280 to an already unmapped window.
1283 /* we don't want the reparent event, put it back on the stack for the
1284 X server to deal with after we unmanage the window */
1285 XPutBackEvent(obt_display, e);
1287 ob_debug("ReparentNotify for window 0x%x", client->window);
1288 client_unmanage(client);
1291 ob_debug("MapRequest for 0x%lx", client->window);
1292 if (!client->iconic) break; /* this normally doesn't happen, but if it
1293 does, we don't want it!
1294 it can happen now when the window is on
1295 another desktop, but we still don't
1297 client_activate(client, FALSE, TRUE, TRUE, TRUE);
1300 /* validate cuz we query stuff off the client here */
1301 if (!client_validate(client)) break;
1303 if (e->xclient.format != 32) return;
1305 msgtype = e->xclient.message_type;
1306 if (msgtype == OBT_PROP_ATOM(WM_CHANGE_STATE)) {
1307 compress_client_message_event(e, &ce, client->window, msgtype);
1308 if (!client->locked)
1309 client_set_wm_state(client, e->xclient.data.l[0]);
1310 } else if (msgtype == OBT_PROP_ATOM(NET_WM_DESKTOP)) {
1311 compress_client_message_event(e, &ce, client->window, msgtype);
1312 if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
1313 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
1314 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
1316 } else if (msgtype == OBT_PROP_ATOM(NET_WM_STATE)) {
1317 gulong ignore_start;
1319 /* can't compress these */
1320 ob_debug("net_wm_state %s %ld %ld for 0x%lx",
1321 (e->xclient.data.l[0] == 0 ? "Remove" :
1322 e->xclient.data.l[0] == 1 ? "Add" :
1323 e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1324 e->xclient.data.l[1], e->xclient.data.l[2],
1327 if (!client->locked) {
1328 /* ignore enter events caused by these like ob actions do */
1329 if (!config_focus_under_mouse)
1330 ignore_start = event_start_ignore_all_enters();
1331 client_set_state(client, e->xclient.data.l[0],
1332 e->xclient.data.l[1], e->xclient.data.l[2]);
1333 if (!config_focus_under_mouse)
1334 event_end_ignore_all_enters(ignore_start);
1336 } else if (msgtype == OBT_PROP_ATOM(NET_CLOSE_WINDOW)) {
1337 ob_debug("net_close_window for 0x%lx", client->window);
1338 if (!client->locked)
1339 client_close(client);
1340 } else if (msgtype == OBT_PROP_ATOM(NET_ACTIVE_WINDOW)) {
1341 ob_debug("net_active_window for 0x%lx source=%s",
1343 (e->xclient.data.l[0] == 0 ? "unknown" :
1344 (e->xclient.data.l[0] == 1 ? "application" :
1345 (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1346 /* XXX make use of data.l[2] !? */
1347 if (e->xclient.data.l[0] == 1 || e->xclient.data.l[0] == 2) {
1348 /* don't use the user's timestamp for client_focus, cuz if it's
1349 an old broken timestamp (happens all the time) then focus
1350 won't move even though we're trying to move it
1351 event_curtime = e->xclient.data.l[1];*/
1352 if (e->xclient.data.l[1] == 0)
1353 ob_debug_type(OB_DEBUG_APP_BUGS,
1354 "_NET_ACTIVE_WINDOW message for window %s is"
1355 " missing a timestamp", client->title);
1357 ob_debug_type(OB_DEBUG_APP_BUGS,
1358 "_NET_ACTIVE_WINDOW message for window %s is "
1359 "missing source indication");
1360 client_activate(client, TRUE, TRUE, TRUE,
1361 (e->xclient.data.l[0] == 0 ||
1362 e->xclient.data.l[0] == 2));
1363 } else if (msgtype == OBT_PROP_ATOM(OB_FOCUS)) {
1364 client_focus(client);
1365 } else if (msgtype == OBT_PROP_ATOM(NET_WM_MOVERESIZE)) {
1366 ob_debug("net_wm_moveresize for 0x%lx direction %d",
1367 client->window, e->xclient.data.l[2]);
1368 if ((Atom)e->xclient.data.l[2] ==
1369 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOPLEFT) ||
1370 (Atom)e->xclient.data.l[2] ==
1371 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOP) ||
1372 (Atom)e->xclient.data.l[2] ==
1373 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOPRIGHT) ||
1374 (Atom)e->xclient.data.l[2] ==
1375 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_RIGHT) ||
1376 (Atom)e->xclient.data.l[2] ==
1377 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_RIGHT) ||
1378 (Atom)e->xclient.data.l[2] ==
1379 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT) ||
1380 (Atom)e->xclient.data.l[2] ==
1381 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOM) ||
1382 (Atom)e->xclient.data.l[2] ==
1383 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT) ||
1384 (Atom)e->xclient.data.l[2] ==
1385 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_LEFT) ||
1386 (Atom)e->xclient.data.l[2] ==
1387 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE) ||
1388 (Atom)e->xclient.data.l[2] ==
1389 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_KEYBOARD) ||
1390 (Atom)e->xclient.data.l[2] ==
1391 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE_KEYBOARD))
1393 moveresize_start(client, e->xclient.data.l[0],
1394 e->xclient.data.l[1], e->xclient.data.l[3],
1395 e->xclient.data.l[2]);
1397 else if ((Atom)e->xclient.data.l[2] ==
1398 OBT_PROP_ATOM(NET_WM_MOVERESIZE_CANCEL))
1399 moveresize_end(TRUE);
1400 } else if (msgtype == OBT_PROP_ATOM(NET_MOVERESIZE_WINDOW)) {
1404 gint ograv, x, y, w, h;
1406 ograv = client->gravity;
1408 if (e->xclient.data.l[0] & 0xff)
1409 client->gravity = e->xclient.data.l[0] & 0xff;
1411 if (e->xclient.data.l[0] & 1 << 8)
1412 x = e->xclient.data.l[1];
1415 if (e->xclient.data.l[0] & 1 << 9)
1416 y = e->xclient.data.l[2];
1420 if (e->xclient.data.l[0] & 1 << 10) {
1421 w = e->xclient.data.l[3];
1423 /* if x was not given, then use gravity to figure out the new
1424 x. the reference point should not be moved */
1425 if (!(e->xclient.data.l[0] & 1 << 8))
1426 client_gravity_resize_w(client, &x, client->area.width, w);
1429 w = client->area.width;
1431 if (e->xclient.data.l[0] & 1 << 11) {
1432 h = e->xclient.data.l[4];
1435 if (!(e->xclient.data.l[0] & 1 << 9))
1436 client_gravity_resize_h(client, &y, client->area.height,h);
1439 h = client->area.height;
1441 ob_debug("MOVERESIZE x %d %d y %d %d (gravity %d)",
1442 e->xclient.data.l[0] & 1 << 8, x,
1443 e->xclient.data.l[0] & 1 << 9, y,
1446 client_find_onscreen(client, &x, &y, w, h, FALSE);
1448 client_configure(client, x, y, w, h, FALSE, TRUE, FALSE);
1450 client->gravity = ograv;
1451 } else if (msgtype == OBT_PROP_ATOM(NET_RESTACK_WINDOW)) {
1452 if (e->xclient.data.l[0] != 2) {
1453 ob_debug_type(OB_DEBUG_APP_BUGS,
1454 "_NET_RESTACK_WINDOW sent for window %s with "
1455 "invalid source indication %ld",
1456 client->title, e->xclient.data.l[0]);
1458 ObClient *sibling = NULL;
1459 if (e->xclient.data.l[1]) {
1460 ObWindow *win = window_find(e->xclient.data.l[1]);
1461 if (WINDOW_IS_CLIENT(win) &&
1462 WINDOW_AS_CLIENT(win) != client)
1464 sibling = WINDOW_AS_CLIENT(win);
1466 if (sibling == NULL)
1467 ob_debug_type(OB_DEBUG_APP_BUGS,
1468 "_NET_RESTACK_WINDOW sent for window %s "
1469 "with invalid sibling 0x%x",
1470 client->title, e->xclient.data.l[1]);
1472 if (e->xclient.data.l[2] == Below ||
1473 e->xclient.data.l[2] == BottomIf ||
1474 e->xclient.data.l[2] == Above ||
1475 e->xclient.data.l[2] == TopIf ||
1476 e->xclient.data.l[2] == Opposite)
1478 gulong ignore_start;
1480 if (!config_focus_under_mouse)
1481 ignore_start = event_start_ignore_all_enters();
1482 /* just raise, don't activate */
1483 stacking_restack_request(client, sibling,
1484 e->xclient.data.l[2]);
1485 if (!config_focus_under_mouse)
1486 event_end_ignore_all_enters(ignore_start);
1488 /* send a synthetic ConfigureNotify, cuz this is supposed
1489 to be like a ConfigureRequest. */
1490 client_reconfigure(client, TRUE);
1492 ob_debug_type(OB_DEBUG_APP_BUGS,
1493 "_NET_RESTACK_WINDOW sent for window %s "
1494 "with invalid detail %d",
1495 client->title, e->xclient.data.l[2]);
1499 case PropertyNotify:
1500 /* validate cuz we query stuff off the client here */
1501 if (!client_validate(client)) break;
1503 /* compress changes to a single property into a single change */
1504 while (XCheckTypedWindowEvent(obt_display, client->window,
1508 /* XXX: it would be nice to compress ALL changes to a property,
1509 not just changes in a row without other props between. */
1511 a = ce.xproperty.atom;
1512 b = e->xproperty.atom;
1516 if ((a == OBT_PROP_ATOM(NET_WM_NAME) ||
1517 a == OBT_PROP_ATOM(WM_NAME) ||
1518 a == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1519 a == OBT_PROP_ATOM(WM_ICON_NAME))
1521 (b == OBT_PROP_ATOM(NET_WM_NAME) ||
1522 b == OBT_PROP_ATOM(WM_NAME) ||
1523 b == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1524 b == OBT_PROP_ATOM(WM_ICON_NAME))) {
1527 if (a == OBT_PROP_ATOM(NET_WM_ICON) &&
1528 b == OBT_PROP_ATOM(NET_WM_ICON))
1531 XPutBackEvent(obt_display, &ce);
1535 msgtype = e->xproperty.atom;
1536 if (msgtype == XA_WM_NORMAL_HINTS) {
1537 ob_debug("Update NORMAL hints");
1538 client_update_normal_hints(client);
1539 /* normal hints can make a window non-resizable */
1540 client_setup_decor_and_functions(client, FALSE);
1542 /* make sure the client's sizes are within its bounds, but only
1543 reconfigure the window if it needs to. emacs will update its
1544 normal hints every time it receives a conigurenotify */
1545 client_reconfigure(client, FALSE);
1546 } else if (msgtype == XA_WM_HINTS) {
1547 client_update_wmhints(client);
1548 } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1549 client_update_transient_for(client);
1550 client_get_type_and_transientness(client);
1551 /* type may have changed, so update the layer */
1552 client_calc_layer(client);
1553 client_setup_decor_and_functions(client, TRUE);
1554 } else if (msgtype == OBT_PROP_ATOM(NET_WM_NAME) ||
1555 msgtype == OBT_PROP_ATOM(WM_NAME) ||
1556 msgtype == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1557 msgtype == OBT_PROP_ATOM(WM_ICON_NAME)) {
1558 client_update_title(client);
1559 } else if (msgtype == OBT_PROP_ATOM(WM_PROTOCOLS)) {
1560 client_update_protocols(client);
1561 client_setup_decor_and_functions(client, TRUE);
1563 else if (msgtype == OBT_PROP_ATOM(NET_WM_STRUT) ||
1564 msgtype == OBT_PROP_ATOM(NET_WM_STRUT_PARTIAL)) {
1565 client_update_strut(client);
1567 else if (msgtype == OBT_PROP_ATOM(NET_WM_ICON)) {
1568 client_update_icons(client);
1570 else if (msgtype == OBT_PROP_ATOM(NET_WM_ICON_GEOMETRY)) {
1571 client_update_icon_geometry(client);
1573 else if (msgtype == OBT_PROP_ATOM(NET_WM_USER_TIME)) {
1575 if (client == focus_client &&
1576 OBT_PROP_GET32(client->window, NET_WM_USER_TIME, CARDINAL, &t)
1577 && t && !event_time_after(t, e->xproperty.time) &&
1578 (!event_last_user_time ||
1579 event_time_after(t, event_last_user_time)))
1581 event_last_user_time = t;
1585 else if (msgtype == OBT_PROP_ATOM(NET_WM_SYNC_REQUEST_COUNTER)) {
1586 client_update_sync_request_counter(client);
1590 case ColormapNotify:
1591 client_update_colormap(client, e->xcolormap.colormap);
1596 if (obt_display_extension_shape &&
1597 e->type == obt_display_extension_shape_basep)
1599 client->shaped = ((XShapeEvent*)e)->shaped;
1600 frame_adjust_shape(client->frame);
1606 static void event_handle_dock(ObDock *s, XEvent *e)
1610 switch (e->xbutton.button) {
1612 stacking_raise(DOCK_AS_WINDOW(s));
1615 stacking_lower(DOCK_AS_WINDOW(s));
1619 screen_find_desktop(screen_desktop, OB_DIRECTION_WEST,
1624 screen_find_desktop(screen_desktop, OB_DIRECTION_EAST,
1628 screen_set_desktop(screen_last_desktop, TRUE);
1636 /* don't hide when moving into a dock app */
1637 if (e->xcrossing.detail != NotifyInferior)
1643 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1647 dock_app_drag(app, &e->xmotion);
1650 if (app->ignore_unmaps) {
1651 app->ignore_unmaps--;
1654 dock_unmanage(app, TRUE);
1657 case ReparentNotify:
1658 dock_unmanage(app, FALSE);
1660 case ConfigureNotify:
1661 dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1666 static ObMenuFrame* find_active_menu(void)
1669 ObMenuFrame *ret = NULL;
1671 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1680 static ObMenuFrame* find_active_or_last_menu(void)
1682 ObMenuFrame *ret = NULL;
1684 ret = find_active_menu();
1685 if (!ret && menu_frame_visible)
1686 ret = menu_frame_visible->data;
1690 static gboolean event_handle_prompt(ObPrompt *p, XEvent *e)
1696 return prompt_mouse_event(p, e);
1699 return prompt_key_event(p, e);
1705 static gboolean event_handle_menu_input(XEvent *ev)
1707 gboolean ret = FALSE;
1709 if (ev->type == ButtonRelease || ev->type == ButtonPress) {
1710 ObMenuEntryFrame *e;
1712 if (menu_hide_delay_reached() &&
1713 (ev->xbutton.button < 4 || ev->xbutton.button > 5))
1715 if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1716 ev->xbutton.y_root)))
1718 if (ev->type == ButtonPress && e->frame->child)
1719 menu_frame_select(e->frame->child, NULL, TRUE);
1720 menu_frame_select(e->frame, e, TRUE);
1721 if (ev->type == ButtonRelease)
1722 menu_entry_frame_execute(e, ev->xbutton.state);
1724 else if (ev->type == ButtonRelease)
1725 menu_frame_hide_all();
1729 else if (ev->type == MotionNotify) {
1731 ObMenuEntryFrame *e;
1733 if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1734 ev->xmotion.y_root)))
1735 if (!(f = find_active_menu()) ||
1737 f->parent == e->frame ||
1738 f->child == e->frame)
1739 menu_frame_select(e->frame, e, FALSE);
1741 else if (ev->type == KeyPress || ev->type == KeyRelease) {
1742 guint keycode, state;
1746 keycode = ev->xkey.keycode;
1747 state = ev->xkey.state;
1748 unikey = obt_keyboard_keycode_to_unichar(keycode);
1750 frame = find_active_or_last_menu();
1752 g_assert_not_reached(); /* there is no active menu */
1754 /* Allow control while going thru the menu */
1755 else if (ev->type == KeyPress && (state & ~ControlMask) == 0) {
1756 frame->got_press = TRUE;
1758 if (keycode == ob_keycode(OB_KEY_ESCAPE)) {
1759 menu_frame_hide_all();
1763 else if (keycode == ob_keycode(OB_KEY_LEFT)) {
1764 /* Left goes to the parent menu */
1766 menu_frame_select(frame, NULL, TRUE);
1770 else if (keycode == ob_keycode(OB_KEY_RIGHT)) {
1771 /* Right goes to the selected submenu */
1772 if (frame->child) menu_frame_select_next(frame->child);
1776 else if (keycode == ob_keycode(OB_KEY_UP)) {
1777 menu_frame_select_previous(frame);
1781 else if (keycode == ob_keycode(OB_KEY_DOWN)) {
1782 menu_frame_select_next(frame);
1787 /* Use KeyRelease events for running things so that the key release
1788 doesn't get sent to the focused application.
1790 Allow ControlMask only, and don't bother if the menu is empty */
1791 else if (ev->type == KeyRelease && (state & ~ControlMask) == 0 &&
1792 frame->entries && frame->got_press)
1794 if (keycode == ob_keycode(OB_KEY_RETURN)) {
1795 /* Enter runs the active item or goes into the submenu.
1796 Control-Enter runs it without closing the menu. */
1798 menu_frame_select_next(frame->child);
1799 else if (frame->selected)
1800 menu_entry_frame_execute(frame->selected, state);
1805 /* keyboard accelerator shortcuts. (if it was a valid key) */
1806 else if (unikey != 0) {
1809 ObMenuEntryFrame *found = NULL;
1810 guint num_found = 0;
1812 /* start after the selected one */
1813 start = frame->entries;
1814 if (frame->selected) {
1815 for (it = start; frame->selected != it->data;
1816 it = g_list_next(it))
1817 g_assert(it != NULL); /* nothing was selected? */
1818 /* next with wraparound */
1819 start = g_list_next(it);
1820 if (start == NULL) start = frame->entries;
1825 ObMenuEntryFrame *e = it->data;
1826 gunichar entrykey = 0;
1828 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1829 entrykey = e->entry->data.normal.shortcut;
1830 else if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1831 entrykey = e->entry->data.submenu.submenu->shortcut;
1833 if (unikey == entrykey) {
1834 if (found == NULL) found = e;
1838 /* next with wraparound */
1839 it = g_list_next(it);
1840 if (it == NULL) it = frame->entries;
1841 } while (it != start);
1844 if (found->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1847 menu_frame_select(frame, found, TRUE);
1848 usleep(50000); /* highlight the item for a short bit so
1849 the user can see what happened */
1850 menu_entry_frame_execute(found, state);
1852 menu_frame_select(frame, found, TRUE);
1854 menu_frame_select_next(frame->child);
1866 static void event_handle_menu(ObMenuFrame *frame, XEvent *ev)
1869 ObMenuEntryFrame *e;
1873 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1874 if (e->ignore_enters)
1876 else if (!(f = find_active_menu()) ||
1878 f->parent == e->frame ||
1879 f->child == e->frame)
1880 menu_frame_select(e->frame, e, FALSE);
1884 /*ignore leaves when we're already in the window */
1885 if (ev->xcrossing.detail == NotifyInferior)
1888 if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1889 (f = find_active_menu()) && f->selected == e &&
1890 e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1892 menu_frame_select(e->frame, NULL, FALSE);
1898 static void event_handle_user_input(ObClient *client, XEvent *e)
1900 g_assert(e->type == ButtonPress || e->type == ButtonRelease ||
1901 e->type == MotionNotify || e->type == KeyPress ||
1902 e->type == KeyRelease);
1904 if (menu_frame_visible) {
1905 if (event_handle_menu_input(e))
1906 /* don't use the event if the menu used it, but if the menu
1907 didn't use it and it's a keypress that is bound, it will
1908 close the menu and be used */
1912 /* if the keyboard interactive action uses the event then dont
1913 use it for bindings. likewise is moveresize uses the event. */
1914 if (!actions_interactive_input_event(e) && !moveresize_event(e)) {
1915 if (moveresize_in_progress)
1916 /* make further actions work on the client being
1918 client = moveresize_client;
1920 if (e->type == ButtonPress ||
1921 e->type == ButtonRelease ||
1922 e->type == MotionNotify)
1924 /* the frame may not be "visible" but they can still click on it
1925 in the case where it is animating before disappearing */
1926 if (!client || !frame_iconify_animating(client->frame))
1927 mouse_event(client, e);
1929 keyboard_event(event_target_client(client), e);
1933 ObClient* event_target_client(ObClient *client)
1935 return (focus_cycle_target ? focus_cycle_target :
1936 (client ? client : focus_client));
1939 static void focus_delay_dest(gpointer data)
1944 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1946 const ObFocusDelayData *f1 = d1;
1947 return f1->client == d2;
1950 static gboolean focus_delay_func(gpointer data)
1952 ObFocusDelayData *d = data;
1953 Time old = event_curtime;
1955 event_curtime = d->time;
1956 event_curserial = d->serial;
1957 if (client_focus(d->client) && config_focus_raise)
1958 stacking_raise(CLIENT_AS_WINDOW(d->client));
1959 event_curtime = old;
1960 return FALSE; /* no repeat */
1963 static void focus_delay_client_dest(ObClient *client, gpointer data)
1965 obt_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1969 void event_halt_focus_delay(void)
1971 /* ignore all enter events up till the event which caused this to occur */
1972 if (event_curserial) event_ignore_enter_range(1, event_curserial);
1973 obt_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1976 gulong event_start_ignore_all_enters(void)
1978 return NextRequest(obt_display);
1981 static void event_ignore_enter_range(gulong start, gulong end)
1985 g_assert(start != 0);
1988 r = g_new(ObSerialRange, 1);
1991 ignore_serials = g_slist_prepend(ignore_serials, r);
1993 ob_debug_type(OB_DEBUG_FOCUS, "ignoring enters from %lu until %lu",
1996 /* increment the serial so we don't ignore events we weren't meant to */
1997 OBT_PROP_ERASE(screen_support_win, MOTIF_WM_HINTS);
2000 void event_end_ignore_all_enters(gulong start)
2002 /* Use (NextRequest-1) so that we ignore up to the current serial only.
2003 Inside event_ignore_enter_range, we increment the serial by one, but if
2004 we ignore that serial too, then any enter events generated by mouse
2005 movement will be ignored until we create some further network traffic.
2006 Instead ignore up to NextRequest-1, then when we increment the serial,
2007 we will be *past* the range of ignored serials */
2008 event_ignore_enter_range(start, NextRequest(obt_display)-1);
2011 static gboolean is_enter_focus_event_ignored(gulong serial)
2015 for (it = ignore_serials; it; it = next) {
2016 ObSerialRange *r = it->data;
2018 next = g_slist_next(it);
2020 if ((glong)(serial - r->end) > 0) {
2022 ignore_serials = g_slist_delete_link(ignore_serials, it);
2025 else if ((glong)(serial - r->start) >= 0)
2031 void event_cancel_all_key_grabs(void)
2034 if (actions_interactive_act_running()) {
2035 actions_interactive_cancel_act();
2036 ob_debug("KILLED interactive action");
2038 else if (menu_frame_visible) {
2039 menu_frame_hide_all();
2040 ob_debug("KILLED open menus");
2042 else if (moveresize_in_progress) {
2043 moveresize_end(TRUE);
2044 ob_debug("KILLED interactive moveresize");
2046 else if (grab_on_keyboard()) {
2048 ob_debug("KILLED active grab on keyboard");
2051 ungrab_passive_key();
2053 XSync(obt_display, FALSE);
2056 gboolean event_time_after(Time t1, Time t2)
2058 g_assert(t1 != CurrentTime);
2059 g_assert(t2 != CurrentTime);
2062 Timestamp values wrap around (after about 49.7 days). The server, given
2063 its current time is represented by timestamp T, always interprets
2064 timestamps from clients by treating half of the timestamp space as being
2065 later in time than T.
2066 - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
2069 /* TIME_HALF is half of the number space of a Time type variable */
2070 #define TIME_HALF (Time)((Time)1 << (sizeof(Time)*8-1))
2072 if (t2 >= TIME_HALF)
2073 /* t2 is in the second half so t1 might wrap around and be smaller than
2075 return t1 >= t2 || t1 < (t2 + TIME_HALF);
2077 /* t2 is in the first half so t1 has to come after it */
2078 return t1 >= t2 && t1 < (t2 + TIME_HALF);
2081 Time event_get_server_time(void)
2083 /* Generate a timestamp */
2086 XChangeProperty(obt_display, screen_support_win,
2087 OBT_PROP_ATOM(WM_CLASS), OBT_PROP_ATOM(STRING),
2088 8, PropModeAppend, NULL, 0);
2089 XWindowEvent(obt_display, screen_support_win, PropertyChangeMask, &event);
2090 return event.xproperty.time;