Merge branch 'backport' into work
[mikachu/openbox.git] / openbox / event.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    event.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
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.
11
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.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "event.h"
21 #include "debug.h"
22 #include "window.h"
23 #include "openbox.h"
24 #include "dock.h"
25 #include "actions.h"
26 #include "client.h"
27 #include "config.h"
28 #include "screen.h"
29 #include "frame.h"
30 #include "grab.h"
31 #include "menu.h"
32 #include "menuframe.h"
33 #include "keyboard.h"
34 #include "mouse.h"
35 #include "focus.h"
36 #include "focus_cycle.h"
37 #include "moveresize.h"
38 #include "group.h"
39 #include "stacking.h"
40 #include "ping.h"
41 #include "obt/display.h"
42 #include "obt/prop.h"
43 #include "obt/keyboard.h"
44
45 #include <X11/Xlib.h>
46 #include <X11/Xatom.h>
47 #include <glib.h>
48
49 #ifdef HAVE_SYS_SELECT_H
50 #  include <sys/select.h>
51 #endif
52 #ifdef HAVE_SIGNAL_H
53 #  include <signal.h>
54 #endif
55 #ifdef HAVE_UNISTD_H
56 #  include <unistd.h> /* for usleep() */
57 #endif
58 #ifdef XKB
59 #  include <X11/XKBlib.h>
60 #endif
61
62 #ifdef USE_SM
63 #include <X11/ICE/ICElib.h>
64 #endif
65
66 typedef struct
67 {
68     gboolean ignored;
69 } ObEventData;
70
71 typedef struct
72 {
73     ObClient *client;
74     Time time;
75     gulong serial;
76 } ObFocusDelayData;
77
78 typedef struct
79 {
80     gulong start; /* inclusive */
81     gulong end;   /* inclusive */
82 } ObSerialRange;
83
84 static void event_process(const XEvent *e, gpointer data);
85 static void event_handle_root(XEvent *e);
86 static gboolean event_handle_menu_input(XEvent *e);
87 static void event_handle_menu(ObMenuFrame *frame, XEvent *e);
88 static void event_handle_dock(ObDock *s, XEvent *e);
89 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
90 static void event_handle_client(ObClient *c, XEvent *e);
91 static void event_handle_user_input(ObClient *client, XEvent *e);
92 static gboolean is_enter_focus_event_ignored(XEvent *e);
93 static void event_ignore_enter_range(gulong start, gulong end);
94
95 static void focus_delay_dest(gpointer data);
96 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
97 static gboolean focus_delay_func(gpointer data);
98 static void focus_delay_client_dest(ObClient *client, gpointer data);
99
100 Time event_curtime = CurrentTime;
101 Time event_last_user_time = CurrentTime;
102 /*! The serial of the current X event */
103
104 static gulong event_curserial;
105 static gboolean focus_left_screen = FALSE;
106 /*! A list of ObSerialRanges which are to be ignored for mouse enter events */
107 static GSList *ignore_serials = NULL;
108
109 #ifdef USE_SM
110 static void ice_handler(gint fd, gpointer conn)
111 {
112     Bool b;
113     IceProcessMessages(conn, NULL, &b);
114 }
115
116 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
117                       IcePointer *watch_data)
118 {
119     static gint fd = -1;
120
121     if (opening) {
122         fd = IceConnectionNumber(conn);
123         obt_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
124     } else {
125         obt_main_loop_fd_remove(ob_main_loop, fd);
126         fd = -1;
127     }
128 }
129 #endif
130
131 void event_startup(gboolean reconfig)
132 {
133     if (reconfig) return;
134
135     obt_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
136
137 #ifdef USE_SM
138     IceAddConnectionWatch(ice_watch, NULL);
139 #endif
140
141     client_add_destroy_notify(focus_delay_client_dest, NULL);
142 }
143
144 void event_shutdown(gboolean reconfig)
145 {
146     if (reconfig) return;
147
148 #ifdef USE_SM
149     IceRemoveConnectionWatch(ice_watch, NULL);
150 #endif
151
152     client_remove_destroy_notify(focus_delay_client_dest);
153 }
154
155 static Window event_get_window(XEvent *e)
156 {
157     Window window;
158
159     /* pick a window */
160     switch (e->type) {
161     case SelectionClear:
162         window = obt_root(ob_screen);
163         break;
164     case CreateNotify:
165         window = e->xcreatewindow.window;
166         break;
167     case MapRequest:
168         window = e->xmaprequest.window;
169         break;
170     case MapNotify:
171         window = e->xmap.window;
172         break;
173     case UnmapNotify:
174         window = e->xunmap.window;
175         break;
176     case DestroyNotify:
177         window = e->xdestroywindow.window;
178         break;
179     case ConfigureRequest:
180         window = e->xconfigurerequest.window;
181         break;
182     case ConfigureNotify:
183         window = e->xconfigure.window;
184         break;
185     default:
186 #ifdef XKB
187         if (obt_display_extension_xkb &&
188             e->type == obt_display_extension_xkb_basep)
189         {
190             switch (((XkbAnyEvent*)e)->xkb_type) {
191             case XkbBellNotify:
192                 window = ((XkbBellNotifyEvent*)e)->window;
193             default:
194                 window = None;
195             }
196         } else
197 #endif
198 #ifdef SYNC
199         if (obt_display_extension_sync &&
200             e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
201         {
202             window = None;
203         } else
204 #endif
205             window = e->xany.window;
206     }
207     return window;
208 }
209
210 static void event_set_curtime(XEvent *e)
211 {
212     Time t = CurrentTime;
213
214     /* grab the lasttime and hack up the state */
215     switch (e->type) {
216     case ButtonPress:
217     case ButtonRelease:
218         t = e->xbutton.time;
219         break;
220     case KeyPress:
221         t = e->xkey.time;
222         break;
223     case KeyRelease:
224         t = e->xkey.time;
225         break;
226     case MotionNotify:
227         t = e->xmotion.time;
228         break;
229     case PropertyNotify:
230         t = e->xproperty.time;
231         break;
232     case EnterNotify:
233     case LeaveNotify:
234         t = e->xcrossing.time;
235         break;
236     default:
237 #ifdef SYNC
238         if (obt_display_extension_sync &&
239             e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
240         {
241             t = ((XSyncAlarmNotifyEvent*)e)->time;
242         }
243 #endif
244         /* if more event types are anticipated, get their timestamp
245            explicitly */
246         break;
247     }
248
249     /* watch that if we get an event earlier than the last specified user_time,
250        which can happen if the clock goes backwards, we erase the last
251        specified user_time */
252     if (t && event_last_user_time && event_time_after(event_last_user_time, t))
253         event_last_user_time = CurrentTime;
254
255     event_curtime = t;
256 }
257
258 static void event_hack_mods(XEvent *e)
259 {
260 #ifdef XKB
261     XkbStateRec xkb_state;
262 #endif
263
264     switch (e->type) {
265     case ButtonPress:
266     case ButtonRelease:
267         e->xbutton.state = obt_keyboard_only_modmasks(e->xbutton.state);
268         break;
269     case KeyPress:
270         e->xkey.state = obt_keyboard_only_modmasks(e->xkey.state);
271         break;
272     case KeyRelease:
273 #ifdef XKB
274         /* If XKB is present, then the modifiers are all strange from its
275            magic.  Our X core protocol stuff won't work, so we use this to
276            find what the modifier state is instead. */
277         if (XkbGetState(obt_display, XkbUseCoreKbd, &xkb_state) == Success)
278             e->xkey.state =
279                 obt_keyboard_only_modmasks(xkb_state.compat_state);
280         else
281 #endif
282         {
283             e->xkey.state = obt_keyboard_only_modmasks(e->xkey.state);
284             /* remove from the state the mask of the modifier key being
285                released, if it is a modifier key being released that is */
286             e->xkey.state &= ~obt_keyboard_keycode_to_modmask(e->xkey.keycode);
287         }
288         break;
289     case MotionNotify:
290         e->xmotion.state = obt_keyboard_only_modmasks(e->xmotion.state);
291         /* compress events */
292         {
293             XEvent ce;
294             while (XCheckTypedWindowEvent(obt_display, e->xmotion.window,
295                                           e->type, &ce)) {
296                 e->xmotion.x = ce.xmotion.x;
297                 e->xmotion.y = ce.xmotion.y;
298                 e->xmotion.x_root = ce.xmotion.x_root;
299                 e->xmotion.y_root = ce.xmotion.y_root;
300             }
301         }
302         break;
303     }
304 }
305
306 static gboolean wanted_focusevent(XEvent *e, gboolean in_client_only)
307 {
308     gint mode = e->xfocus.mode;
309     gint detail = e->xfocus.detail;
310     Window win = e->xany.window;
311
312     if (e->type == FocusIn) {
313         /* These are ones we never want.. */
314
315         /* This means focus was given by a keyboard/mouse grab. */
316         if (mode == NotifyGrab)
317             return FALSE;
318         /* This means focus was given back from a keyboard/mouse grab. */
319         if (mode == NotifyUngrab)
320             return FALSE;
321
322         /* These are the ones we want.. */
323
324         if (win == obt_root(ob_screen)) {
325             /* If looking for a focus in on a client, then always return
326                FALSE for focus in's to the root window */
327             if (in_client_only)
328                 return FALSE;
329             /* This means focus reverted off of a client */
330             else if (detail == NotifyPointerRoot ||
331                      detail == NotifyDetailNone ||
332                      detail == NotifyInferior ||
333                      /* This means focus got here from another screen */
334                      detail == NotifyNonlinear)
335                 return TRUE;
336             else
337                 return FALSE;
338         }
339
340         /* It was on a client, was it a valid one?
341            It's possible to get a FocusIn event for a client that was managed
342            but has disappeared.
343         */
344         if (in_client_only) {
345             ObWindow *w = window_find(e->xfocus.window);
346             if (!w || !WINDOW_IS_CLIENT(w))
347                 return FALSE;
348         }
349         else {
350             /* This means focus reverted to parent from the client (this
351                happens often during iconify animation) */
352             if (detail == NotifyInferior)
353                 return TRUE;
354         }
355
356         /* This means focus moved from the root window to a client */
357         if (detail == NotifyVirtual)
358             return TRUE;
359         /* This means focus moved from one client to another */
360         if (detail == NotifyNonlinearVirtual)
361             return TRUE;
362
363         /* Otherwise.. */
364         return FALSE;
365     } else {
366         g_assert(e->type == FocusOut);
367
368         /* These are ones we never want.. */
369
370         /* This means focus was taken by a keyboard/mouse grab. */
371         if (mode == NotifyGrab)
372             return FALSE;
373         /* This means focus was grabbed on a window and it was released. */
374         if (mode == NotifyUngrab)
375             return FALSE;
376
377         /* Focus left the root window revertedto state */
378         if (win == obt_root(ob_screen))
379             return FALSE;
380
381         /* These are the ones we want.. */
382
383         /* This means focus moved from a client to the root window */
384         if (detail == NotifyVirtual)
385             return TRUE;
386         /* This means focus moved from one client to another */
387         if (detail == NotifyNonlinearVirtual)
388             return TRUE;
389
390         /* Otherwise.. */
391         return FALSE;
392     }
393 }
394
395 static Bool event_look_for_focusin(Display *d, XEvent *e, XPointer arg)
396 {
397     return e->type == FocusIn && wanted_focusevent(e, FALSE);
398 }
399
400 static Bool event_look_for_focusin_client(Display *d, XEvent *e, XPointer arg)
401 {
402     return e->type == FocusIn && wanted_focusevent(e, TRUE);
403 }
404
405 static void print_focusevent(XEvent *e)
406 {
407     gint mode = e->xfocus.mode;
408     gint detail = e->xfocus.detail;
409     Window win = e->xany.window;
410     const gchar *modestr, *detailstr;
411
412     switch (mode) {
413     case NotifyNormal:       modestr="NotifyNormal";       break;
414     case NotifyGrab:         modestr="NotifyGrab";         break;
415     case NotifyUngrab:       modestr="NotifyUngrab";       break;
416     case NotifyWhileGrabbed: modestr="NotifyWhileGrabbed"; break;
417     }
418     switch (detail) {
419     case NotifyAncestor:    detailstr="NotifyAncestor";    break;
420     case NotifyVirtual:     detailstr="NotifyVirtual";     break;
421     case NotifyInferior:    detailstr="NotifyInferior";    break;
422     case NotifyNonlinear:   detailstr="NotifyNonlinear";   break;
423     case NotifyNonlinearVirtual: detailstr="NotifyNonlinearVirtual"; break;
424     case NotifyPointer:     detailstr="NotifyPointer";     break;
425     case NotifyPointerRoot: detailstr="NotifyPointerRoot"; break;
426     case NotifyDetailNone:  detailstr="NotifyDetailNone";  break;
427     }
428
429     if (mode == NotifyGrab || mode == NotifyUngrab)
430         return;
431
432     g_assert(modestr);
433     g_assert(detailstr);
434     ob_debug_type(OB_DEBUG_FOCUS, "Focus%s 0x%x mode=%s detail=%s",
435                   (e->xfocus.type == FocusIn ? "In" : "Out"),
436                   win,
437                   modestr, detailstr);
438
439 }
440
441 static gboolean event_ignore(XEvent *e, ObClient *client)
442 {
443     switch(e->type) {
444     case FocusIn:
445         print_focusevent(e);
446         if (!wanted_focusevent(e, FALSE))
447             return TRUE;
448         break;
449     case FocusOut:
450         print_focusevent(e);
451         if (!wanted_focusevent(e, FALSE))
452             return TRUE;
453         break;
454     }
455     return FALSE;
456 }
457
458 static void event_process(const XEvent *ec, gpointer data)
459 {
460     XEvent ee, *e;
461     ObEventData *ed = data;
462
463     Window window;
464     ObClient *client = NULL;
465     ObDock *dock = NULL;
466     ObDockApp *dockapp = NULL;
467     ObWindow *obwin = NULL;
468     ObMenuFrame *menu = NULL;
469
470     /* make a copy we can mangle */
471     ee = *ec;
472     e = &ee;
473
474     window = event_get_window(e);
475     if (window == obt_root(ob_screen))
476         /* don't do any lookups, waste of cpu */;
477     else if ((obwin = window_find(window))) {
478         switch (obwin->type) {
479         case OB_WINDOW_CLASS_DOCK:
480             dock = WINDOW_AS_DOCK(obwin);
481             break;
482         case OB_WINDOW_CLASS_CLIENT:
483             client = WINDOW_AS_CLIENT(obwin);
484             break;
485         case OB_WINDOW_CLASS_MENUFRAME:
486             menu = WINDOW_AS_MENUFRAME(obwin);
487             break;
488         case OB_WINDOW_CLASS_INTERNAL:
489             /* we don't do anything with events directly on these windows */
490             break;
491         }
492     }
493     else
494         dockapp = dock_find_dockapp(window);
495
496     event_set_curtime(e);
497     event_curserial = e->xany.serial;
498     event_hack_mods(e);
499     if (event_ignore(e, client)) {
500         if (ed)
501             ed->ignored = TRUE;
502         return;
503     } else if (ed)
504             ed->ignored = FALSE;
505
506     /* deal with it in the kernel */
507
508     if (e->type == FocusIn) {
509         if (client &&
510             e->xfocus.detail == NotifyInferior)
511         {
512             ob_debug_type(OB_DEBUG_FOCUS,
513                           "Focus went to the frame window");
514
515             focus_left_screen = FALSE;
516
517             focus_fallback(FALSE, config_focus_under_mouse, TRUE, TRUE);
518
519             /* We don't get a FocusOut for this case, because it's just moving
520                from our Inferior up to us. This happens when iconifying a
521                window with RevertToParent focus */
522             frame_adjust_focus(client->frame, FALSE);
523             /* focus_set_client(NULL) has already been called */
524         }
525         else if (e->xfocus.detail == NotifyPointerRoot ||
526                  e->xfocus.detail == NotifyDetailNone ||
527                  e->xfocus.detail == NotifyInferior ||
528                  e->xfocus.detail == NotifyNonlinear)
529         {
530             XEvent ce;
531
532             ob_debug_type(OB_DEBUG_FOCUS,
533                           "Focus went to root or pointer root/none");
534
535             if (e->xfocus.detail == NotifyInferior ||
536                 e->xfocus.detail == NotifyNonlinear)
537             {
538                 focus_left_screen = FALSE;
539             }
540
541             /* If another FocusIn is in the queue then don't fallback yet. This
542                fixes the fun case of:
543                window map -> send focusin
544                window unmap -> get focusout
545                window map -> send focusin
546                get first focus out -> fall back to something (new window
547                  hasn't received focus yet, so something else) -> send focusin
548                which means the "something else" is the last thing to get a
549                focusin sent to it, so the new window doesn't end up with focus.
550
551                But if the other focus in is something like PointerRoot then we
552                still want to fall back.
553             */
554             if (XCheckIfEvent(obt_display, &ce, event_look_for_focusin_client,
555                               NULL))
556             {
557                 XPutBackEvent(obt_display, &ce);
558                 ob_debug_type(OB_DEBUG_FOCUS,
559                               "  but another FocusIn is coming");
560             } else {
561                 /* Focus has been reverted.
562
563                    FocusOut events come after UnmapNotify, so we don't need to
564                    worry about focusing an invalid window
565                 */
566
567                 if (!focus_left_screen)
568                     focus_fallback(FALSE, config_focus_under_mouse,
569                                    TRUE, TRUE);
570             }
571         }
572         else if (!client)
573         {
574             ob_debug_type(OB_DEBUG_FOCUS,
575                           "Focus went to a window that is already gone");
576
577             /* If you send focus to a window and then it disappears, you can
578                get the FocusIn for it, after it is unmanaged.
579                Just wait for the next FocusOut/FocusIn pair, but make note that
580                the window that was focused no longer is. */
581             focus_set_client(NULL);
582         }
583         else if (client != focus_client) {
584             focus_left_screen = FALSE;
585             frame_adjust_focus(client->frame, TRUE);
586             focus_set_client(client);
587             client_calc_layer(client);
588             client_bring_helper_windows(client);
589         }
590     } else if (e->type == FocusOut) {
591         XEvent ce;
592
593         /* Look for the followup FocusIn */
594         if (!XCheckIfEvent(obt_display, &ce, event_look_for_focusin, NULL)) {
595             /* There is no FocusIn, this means focus went to a window that
596                is not being managed, or a window on another screen. */
597             Window win, root;
598             gint i;
599             guint u;
600             obt_display_ignore_errors(TRUE);
601             if (XGetInputFocus(obt_display, &win, &i) &&
602                 XGetGeometry(obt_display, win, &root, &i,&i,&u,&u,&u,&u) &&
603                 root != obt_root(ob_screen))
604             {
605                 ob_debug_type(OB_DEBUG_FOCUS,
606                               "Focus went to another screen !");
607                 focus_left_screen = TRUE;
608             }
609             else
610                 ob_debug_type(OB_DEBUG_FOCUS,
611                               "Focus went to a black hole !");
612             obt_display_ignore_errors(FALSE);
613             /* nothing is focused */
614             focus_set_client(NULL);
615         } else {
616             /* Focus moved, so process the FocusIn event */
617             ObEventData ed = { .ignored = FALSE };
618             event_process(&ce, &ed);
619             if (ed.ignored) {
620                 /* The FocusIn was ignored, this means it was on a window
621                    that isn't a client. */
622                 ob_debug_type(OB_DEBUG_FOCUS,
623                               "Focus went to an unmanaged window 0x%x !",
624                               ce.xfocus.window);
625                 focus_fallback(TRUE, config_focus_under_mouse, TRUE, TRUE);
626             }
627         }
628
629         if (client && client != focus_client) {
630             frame_adjust_focus(client->frame, FALSE);
631             /* focus_set_client(NULL) has already been called in this
632                section or by focus_fallback */
633         }
634     }
635     else if (client)
636         event_handle_client(client, e);
637     else if (dockapp)
638         event_handle_dockapp(dockapp, e);
639     else if (dock)
640         event_handle_dock(dock, e);
641     else if (menu)
642         event_handle_menu(menu, e);
643     else if (window == obt_root(ob_screen))
644         event_handle_root(e);
645     else if (e->type == MapRequest)
646         window_manage(window);
647     else if (e->type == MappingNotify) {
648         /* keyboard layout changes for modifier mapping changes. reload the
649            modifier map, and rebind all the key bindings as appropriate */
650         ob_debug("Kepboard map changed. Reloading keyboard bindings.");
651         obt_keyboard_reload();
652         keyboard_rebind();
653     }
654     else if (e->type == ClientMessage) {
655         /* This is for _NET_WM_REQUEST_FRAME_EXTENTS messages. They come for
656            windows that are not managed yet. */
657         if (e->xclient.message_type ==
658             OBT_PROP_ATOM(NET_REQUEST_FRAME_EXTENTS))
659         {
660             /* Pretend to manage the client, getting information used to
661                determine its decorations */
662             ObClient *c = client_fake_manage(e->xclient.window);
663             gulong vals[4];
664
665             /* set the frame extents on the window */
666             vals[0] = c->frame->size.left;
667             vals[1] = c->frame->size.right;
668             vals[2] = c->frame->size.top;
669             vals[3] = c->frame->size.bottom;
670             OBT_PROP_SETA32(e->xclient.window, NET_FRAME_EXTENTS,
671                             CARDINAL, vals, 4);
672
673             /* Free the pretend client */
674             client_fake_unmanage(c);
675         }
676     }
677     else if (e->type == ConfigureRequest) {
678         /* unhandled configure requests must be used to configure the
679            window directly */
680         XWindowChanges xwc;
681
682         xwc.x = e->xconfigurerequest.x;
683         xwc.y = e->xconfigurerequest.y;
684         xwc.width = e->xconfigurerequest.width;
685         xwc.height = e->xconfigurerequest.height;
686         xwc.border_width = e->xconfigurerequest.border_width;
687         xwc.sibling = e->xconfigurerequest.above;
688         xwc.stack_mode = e->xconfigurerequest.detail;
689
690         /* we are not to be held responsible if someone sends us an
691            invalid request! */
692         obt_display_ignore_errors(TRUE);
693         XConfigureWindow(obt_display, window,
694                          e->xconfigurerequest.value_mask, &xwc);
695         obt_display_ignore_errors(FALSE);
696     }
697 #ifdef SYNC
698     else if (obt_display_extension_sync &&
699              e->type == obt_display_extension_sync_basep + XSyncAlarmNotify)
700     {
701         XSyncAlarmNotifyEvent *se = (XSyncAlarmNotifyEvent*)e;
702         if (se->alarm == moveresize_alarm && moveresize_in_progress)
703             moveresize_event(e);
704     }
705 #endif
706
707     if (e->type == ButtonPress || e->type == ButtonRelease) {
708         /* If the button press was on some non-root window, or was physically
709            on the root window, then process it */
710         if (window != obt_root(ob_screen) ||
711             e->xbutton.subwindow == None)
712         {
713             event_handle_user_input(client, e);
714         }
715         /* Otherwise only process it if it was physically on an openbox
716            internal window */
717         else {
718             ObWindow *w;
719
720             if ((w = window_find(e->xbutton.subwindow)) &&
721                 WINDOW_IS_INTERNAL(w))
722             {
723                 event_handle_user_input(client, e);
724             }
725         }
726     }
727     else if (e->type == KeyPress || e->type == KeyRelease ||
728              e->type == MotionNotify)
729         event_handle_user_input(client, e);
730
731     /* if something happens and it's not from an XEvent, then we don't know
732        the time */
733     event_curtime = CurrentTime;
734     event_curserial = 0;
735 }
736
737 static void event_handle_root(XEvent *e)
738 {
739     Atom msgtype;
740
741     switch(e->type) {
742     case SelectionClear:
743         ob_debug("Another WM has requested to replace us. Exiting.");
744         ob_exit_replace();
745         break;
746
747     case ClientMessage:
748         if (e->xclient.format != 32) break;
749
750         msgtype = e->xclient.message_type;
751         if (msgtype == OBT_PROP_ATOM(NET_CURRENT_DESKTOP)) {
752             guint d = e->xclient.data.l[0];
753             if (d < screen_num_desktops) {
754                 event_curtime = e->xclient.data.l[1];
755                 if (event_curtime == 0)
756                     ob_debug_type(OB_DEBUG_APP_BUGS,
757                                   "_NET_CURRENT_DESKTOP message is missing "
758                                   "a timestamp");
759                 screen_set_desktop(d, TRUE);
760             }
761         } else if (msgtype == OBT_PROP_ATOM(NET_NUMBER_OF_DESKTOPS)) {
762             guint d = e->xclient.data.l[0];
763             if (d > 0 && d <= 1000)
764                 screen_set_num_desktops(d);
765         } else if (msgtype == OBT_PROP_ATOM(NET_SHOWING_DESKTOP)) {
766             screen_show_desktop(e->xclient.data.l[0] != 0, NULL);
767         } else if (msgtype == OBT_PROP_ATOM(OB_CONTROL)) {
768             ob_debug("OB_CONTROL: %d", e->xclient.data.l[0]);
769             if (e->xclient.data.l[0] == 1)
770                 ob_reconfigure();
771             else if (e->xclient.data.l[0] == 2)
772                 ob_restart();
773             else if (e->xclient.data.l[0] == 3)
774                 ob_exit(0);
775         } else if (msgtype == OBT_PROP_ATOM(WM_PROTOCOLS)) {
776             if ((Atom)e->xclient.data.l[0] == OBT_PROP_ATOM(NET_WM_PING))
777                 ping_got_pong(e->xclient.data.l[1]);
778         }
779         break;
780     case PropertyNotify:
781         if (e->xproperty.atom == OBT_PROP_ATOM(NET_DESKTOP_NAMES)) {
782             ob_debug("UPDATE DESKTOP NAMES");
783             screen_update_desktop_names();
784         }
785         else if (e->xproperty.atom == OBT_PROP_ATOM(NET_DESKTOP_LAYOUT))
786             screen_update_layout();
787         break;
788     case ConfigureNotify:
789 #ifdef XRANDR
790         XRRUpdateConfiguration(e);
791 #endif
792         screen_resize();
793         break;
794     default:
795         ;
796     }
797 }
798
799 void event_enter_client(ObClient *client)
800 {
801     g_assert(config_focus_follow);
802
803     if (client_enter_focusable(client) && client_can_focus(client)) {
804         if (config_focus_delay) {
805             ObFocusDelayData *data;
806
807             obt_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
808
809             data = g_new(ObFocusDelayData, 1);
810             data->client = client;
811             data->time = event_curtime;
812             data->serial = event_curserial;
813
814             obt_main_loop_timeout_add(ob_main_loop,
815                                       config_focus_delay * 1000,
816                                       focus_delay_func,
817                                       data, focus_delay_cmp, focus_delay_dest);
818         } else {
819             ObFocusDelayData data;
820             data.client = client;
821             data.time = event_curtime;
822             data.serial = event_curserial;
823             focus_delay_func(&data);
824         }
825     }
826 }
827
828 static void event_handle_client(ObClient *client, XEvent *e)
829 {
830     XEvent ce;
831     Atom msgtype;
832     ObFrameContext con;
833     static gint px = -1, py = -1;
834     static guint pb = 0;
835
836     switch (e->type) {
837     case ButtonPress:
838         /* save where the press occured for the first button pressed */
839         if (!pb) {
840             pb = e->xbutton.button;
841             px = e->xbutton.x;
842             py = e->xbutton.y;
843         }
844     case ButtonRelease:
845         /* Wheel buttons don't draw because they are an instant click, so it
846            is a waste of resources to go drawing it.
847            if the user is doing an intereactive thing, or has a menu open then
848            the mouse is grabbed (possibly) and if we get these events we don't
849            want to deal with them
850         */
851         if (!(e->xbutton.button == 4 || e->xbutton.button == 5) &&
852             !grab_on_keyboard())
853         {
854             /* use where the press occured */
855             con = frame_context(client, e->xbutton.window, px, py);
856             con = mouse_button_frame_context(con, e->xbutton.button,
857                                              e->xbutton.state);
858
859             if (e->type == ButtonRelease && e->xbutton.button == pb)
860                 pb = 0, px = py = -1;
861
862             switch (con) {
863             case OB_FRAME_CONTEXT_MAXIMIZE:
864                 client->frame->max_press = (e->type == ButtonPress);
865                 frame_adjust_state(client->frame);
866                 break;
867             case OB_FRAME_CONTEXT_CLOSE:
868                 client->frame->close_press = (e->type == ButtonPress);
869                 frame_adjust_state(client->frame);
870                 break;
871             case OB_FRAME_CONTEXT_ICONIFY:
872                 client->frame->iconify_press = (e->type == ButtonPress);
873                 frame_adjust_state(client->frame);
874                 break;
875             case OB_FRAME_CONTEXT_ALLDESKTOPS:
876                 client->frame->desk_press = (e->type == ButtonPress);
877                 frame_adjust_state(client->frame);
878                 break;
879             case OB_FRAME_CONTEXT_SHADE:
880                 client->frame->shade_press = (e->type == ButtonPress);
881                 frame_adjust_state(client->frame);
882                 break;
883             default:
884                 /* nothing changes with clicks for any other contexts */
885                 break;
886             }
887         }
888         break;
889     case MotionNotify:
890         /* when there is a grab on the pointer, we won't get enter/leave
891            notifies, but we still get motion events */
892         if (grab_on_pointer()) break;
893
894         con = frame_context(client, e->xmotion.window,
895                             e->xmotion.x, e->xmotion.y);
896         switch (con) {
897         case OB_FRAME_CONTEXT_TITLEBAR:
898         case OB_FRAME_CONTEXT_TLCORNER:
899         case OB_FRAME_CONTEXT_TRCORNER:
900             /* we've left the button area inside the titlebar */
901             if (client->frame->max_hover || client->frame->desk_hover ||
902                 client->frame->shade_hover || client->frame->iconify_hover ||
903                 client->frame->close_hover)
904             {
905                 client->frame->max_hover = FALSE;
906                 client->frame->desk_hover = FALSE;
907                 client->frame->shade_hover = FALSE;
908                 client->frame->iconify_hover = FALSE;
909                 client->frame->close_hover = FALSE;
910                 frame_adjust_state(client->frame);
911             }
912             break;
913         case OB_FRAME_CONTEXT_MAXIMIZE:
914             if (!client->frame->max_hover) {
915                 client->frame->max_hover = TRUE;
916                 frame_adjust_state(client->frame);
917             }
918             break;
919         case OB_FRAME_CONTEXT_ALLDESKTOPS:
920             if (!client->frame->desk_hover) {
921                 client->frame->desk_hover = TRUE;
922                 frame_adjust_state(client->frame);
923             }
924             break;
925         case OB_FRAME_CONTEXT_SHADE:
926             if (!client->frame->shade_hover) {
927                 client->frame->shade_hover = TRUE;
928                 frame_adjust_state(client->frame);
929             }
930             break;
931         case OB_FRAME_CONTEXT_ICONIFY:
932             if (!client->frame->iconify_hover) {
933                 client->frame->iconify_hover = TRUE;
934                 frame_adjust_state(client->frame);
935             }
936             break;
937         case OB_FRAME_CONTEXT_CLOSE:
938             if (!client->frame->close_hover) {
939                 client->frame->close_hover = TRUE;
940                 frame_adjust_state(client->frame);
941             }
942             break;
943         default:
944             break;
945         }
946         break;
947     case LeaveNotify:
948         con = frame_context(client, e->xcrossing.window,
949                             e->xcrossing.x, e->xcrossing.y);
950         switch (con) {
951         case OB_FRAME_CONTEXT_TITLEBAR:
952         case OB_FRAME_CONTEXT_TLCORNER:
953         case OB_FRAME_CONTEXT_TRCORNER:
954             /* we've left the button area inside the titlebar */
955             if (client->frame->max_hover || client->frame->desk_hover ||
956                 client->frame->shade_hover || client->frame->iconify_hover ||
957                 client->frame->close_hover)
958             {
959                 client->frame->max_hover = FALSE;
960                 client->frame->desk_hover = FALSE;
961                 client->frame->shade_hover = FALSE;
962                 client->frame->iconify_hover = FALSE;
963                 client->frame->close_hover = FALSE;
964                 frame_adjust_state(client->frame);
965             }
966             break;
967         case OB_FRAME_CONTEXT_MAXIMIZE:
968             client->frame->max_hover = FALSE;
969             frame_adjust_state(client->frame);
970             break;
971         case OB_FRAME_CONTEXT_ALLDESKTOPS:
972             client->frame->desk_hover = FALSE;
973             frame_adjust_state(client->frame);
974             break;
975         case OB_FRAME_CONTEXT_SHADE:
976             client->frame->shade_hover = FALSE;
977             frame_adjust_state(client->frame);
978             break;
979         case OB_FRAME_CONTEXT_ICONIFY:
980             client->frame->iconify_hover = FALSE;
981             frame_adjust_state(client->frame);
982             break;
983         case OB_FRAME_CONTEXT_CLOSE:
984             client->frame->close_hover = FALSE;
985             frame_adjust_state(client->frame);
986             break;
987         case OB_FRAME_CONTEXT_FRAME:
988             /* When the mouse leaves an animating window, don't use the
989                corresponding enter events. Pretend like the animating window
990                doesn't even exist..! */
991             if (frame_iconify_animating(client->frame))
992                 event_end_ignore_all_enters(event_start_ignore_all_enters());
993
994             ob_debug_type(OB_DEBUG_FOCUS,
995                           "%sNotify mode %d detail %d on %lx",
996                           (e->type == EnterNotify ? "Enter" : "Leave"),
997                           e->xcrossing.mode,
998                           e->xcrossing.detail, (client?client->window:0));
999             if (grab_on_keyboard())
1000                 break;
1001             if (config_focus_follow && config_focus_delay &&
1002                 /* leave inferior events can happen when the mouse goes onto
1003                    the window's border and then into the window before the
1004                    delay is up */
1005                 e->xcrossing.detail != NotifyInferior)
1006             {
1007                 obt_main_loop_timeout_remove_data(ob_main_loop,
1008                                                   focus_delay_func,
1009                                                   client, FALSE);
1010             }
1011             break;
1012         default:
1013             break;
1014         }
1015         break;
1016     case EnterNotify:
1017     {
1018         con = frame_context(client, e->xcrossing.window,
1019                             e->xcrossing.x, e->xcrossing.y);
1020         switch (con) {
1021         case OB_FRAME_CONTEXT_MAXIMIZE:
1022             client->frame->max_hover = TRUE;
1023             frame_adjust_state(client->frame);
1024             break;
1025         case OB_FRAME_CONTEXT_ALLDESKTOPS:
1026             client->frame->desk_hover = TRUE;
1027             frame_adjust_state(client->frame);
1028             break;
1029         case OB_FRAME_CONTEXT_SHADE:
1030             client->frame->shade_hover = TRUE;
1031             frame_adjust_state(client->frame);
1032             break;
1033         case OB_FRAME_CONTEXT_ICONIFY:
1034             client->frame->iconify_hover = TRUE;
1035             frame_adjust_state(client->frame);
1036             break;
1037         case OB_FRAME_CONTEXT_CLOSE:
1038             client->frame->close_hover = TRUE;
1039             frame_adjust_state(client->frame);
1040             break;
1041         case OB_FRAME_CONTEXT_FRAME:
1042             if (grab_on_keyboard())
1043                 break;
1044             if (e->xcrossing.mode == NotifyGrab ||
1045                 e->xcrossing.mode == NotifyUngrab ||
1046                 /*ignore enters when we're already in the window */
1047                 e->xcrossing.detail == NotifyInferior ||
1048                 is_enter_focus_event_ignored(e))
1049             {
1050                 ob_debug_type(OB_DEBUG_FOCUS,
1051                               "%sNotify mode %d detail %d serial %lu on %lx "
1052                               "IGNORED",
1053                               (e->type == EnterNotify ? "Enter" : "Leave"),
1054                               e->xcrossing.mode,
1055                               e->xcrossing.detail,
1056                               e->xcrossing.serial,
1057                               client?client->window:0);
1058             }
1059             else {
1060                 ob_debug_type(OB_DEBUG_FOCUS,
1061                               "%sNotify mode %d detail %d serial %lu on %lx, "
1062                               "focusing window",
1063                               (e->type == EnterNotify ? "Enter" : "Leave"),
1064                               e->xcrossing.mode,
1065                               e->xcrossing.detail,
1066                               e->xcrossing.serial,
1067                               (client?client->window:0));
1068                 if (config_focus_follow)
1069                     event_enter_client(client);
1070             }
1071             break;
1072         default:
1073             break;
1074         }
1075         break;
1076     }
1077     case ConfigureRequest:
1078     {
1079         /* dont compress these unless you're going to watch for property
1080            notifies in between (these can change what the configure would
1081            do to the window).
1082            also you can't compress stacking events
1083         */
1084
1085         gint x, y, w, h;
1086         gboolean move = FALSE;
1087         gboolean resize = FALSE;
1088
1089         /* get the current area */
1090         RECT_TO_DIMS(client->area, x, y, w, h);
1091
1092         ob_debug("ConfigureRequest for \"%s\" desktop %d wmstate %d "
1093                  "visibile %d",
1094                  client->title,
1095                  screen_desktop, client->wmstate, client->frame->visible);
1096         ob_debug("                     x %d y %d w %d h %d b %d",
1097                  x, y, w, h, client->border_width);
1098
1099         if (e->xconfigurerequest.value_mask & CWBorderWidth)
1100             if (client->border_width != e->xconfigurerequest.border_width) {
1101                 client->border_width = e->xconfigurerequest.border_width;
1102
1103                 /* if the border width is changing then that is the same
1104                    as requesting a resize, but we don't actually change
1105                    the client's border, so it will change their root
1106                    coordiantes (since they include the border width) and
1107                    we need to a notify then */
1108                 move = TRUE;
1109             }
1110
1111
1112         if (e->xconfigurerequest.value_mask & CWStackMode) {
1113             ObClient *sibling = NULL;
1114             gulong ignore_start;
1115             gboolean ok = TRUE;
1116
1117             /* get the sibling */
1118             if (e->xconfigurerequest.value_mask & CWSibling) {
1119                 ObWindow *win;
1120                 win = window_find(e->xconfigurerequest.above);
1121                 if (win && WINDOW_IS_CLIENT(win) &&
1122                     WINDOW_AS_CLIENT(win) != client)
1123                 {
1124                     sibling = WINDOW_AS_CLIENT(win);
1125                 }
1126                 else
1127                     /* an invalid sibling was specified so don't restack at
1128                        all, it won't make sense no matter what we do */
1129                     ok = FALSE;
1130             }
1131
1132             if (ok) {
1133                 if (!config_focus_under_mouse)
1134                     ignore_start = event_start_ignore_all_enters();
1135                 stacking_restack_request(client, sibling,
1136                                          e->xconfigurerequest.detail);
1137                 if (!config_focus_under_mouse)
1138                     event_end_ignore_all_enters(ignore_start);
1139             }
1140
1141             /* a stacking change moves the window without resizing */
1142             move = TRUE;
1143         }
1144
1145         if ((e->xconfigurerequest.value_mask & CWX) ||
1146             (e->xconfigurerequest.value_mask & CWY) ||
1147             (e->xconfigurerequest.value_mask & CWWidth) ||
1148             (e->xconfigurerequest.value_mask & CWHeight))
1149         {
1150             if (e->xconfigurerequest.value_mask & CWX) {
1151                 /* don't allow clients to move shaded windows (fvwm does this)
1152                  */
1153                 if (!client->shaded)
1154                     x = e->xconfigurerequest.x;
1155                 move = TRUE;
1156             }
1157             if (e->xconfigurerequest.value_mask & CWY) {
1158                 /* don't allow clients to move shaded windows (fvwm does this)
1159                  */
1160                 if (!client->shaded)
1161                     y = e->xconfigurerequest.y;
1162                 move = TRUE;
1163             }
1164
1165             if (e->xconfigurerequest.value_mask & CWWidth) {
1166                 w = e->xconfigurerequest.width;
1167                 resize = TRUE;
1168             }
1169             if (e->xconfigurerequest.value_mask & CWHeight) {
1170                 h = e->xconfigurerequest.height;
1171                 resize = TRUE;
1172             }
1173         }
1174
1175         ob_debug("ConfigureRequest x(%d) %d y(%d) %d w(%d) %d h(%d) %d "
1176                  "move %d resize %d",
1177                  e->xconfigurerequest.value_mask & CWX, x,
1178                  e->xconfigurerequest.value_mask & CWY, y,
1179                  e->xconfigurerequest.value_mask & CWWidth, w,
1180                  e->xconfigurerequest.value_mask & CWHeight, h,
1181                  move, resize);
1182
1183         /* check for broken apps moving to their root position
1184
1185            XXX remove this some day...that would be nice. right now all
1186            kde apps do this when they try activate themselves on another
1187            desktop. eg. open amarok window on desktop 1, switch to desktop
1188            2, click amarok tray icon. it will move by its decoration size.
1189         */
1190         if (x != client->area.x &&
1191             x == (client->frame->area.x + client->frame->size.left -
1192                   (gint)client->border_width) &&
1193             y != client->area.y &&
1194             y == (client->frame->area.y + client->frame->size.top -
1195                   (gint)client->border_width) &&
1196             w == client->area.width &&
1197             h == client->area.height)
1198         {
1199             ob_debug_type(OB_DEBUG_APP_BUGS,
1200                           "Application %s is trying to move via "
1201                           "ConfigureRequest to it's root window position "
1202                           "but it is not using StaticGravity",
1203                           client->title);
1204             /* don't move it */
1205             x = client->area.x;
1206             y = client->area.y;
1207
1208             /* they still requested a move, so don't change whether a
1209                notify is sent or not */
1210         }
1211
1212         {
1213             gint lw,lh;
1214
1215             client_try_configure(client, &x, &y, &w, &h, &lw, &lh, FALSE);
1216
1217             /* if x was not given, then use gravity to figure out the new
1218                x.  the reference point should not be moved */
1219             if ((e->xconfigurerequest.value_mask & CWWidth &&
1220                  !(e->xconfigurerequest.value_mask & CWX)))
1221                 client_gravity_resize_w(client, &x, client->area.width, w);
1222             /* if y was not given, then use gravity to figure out the new
1223                y.  the reference point should not be moved */
1224             if ((e->xconfigurerequest.value_mask & CWHeight &&
1225                  !(e->xconfigurerequest.value_mask & CWY)))
1226                 client_gravity_resize_h(client, &y, client->area.height,h);
1227
1228             client_find_onscreen(client, &x, &y, w, h, FALSE);
1229
1230             ob_debug("Granting ConfigureRequest x %d y %d w %d h %d",
1231                      x, y, w, h);
1232             client_configure(client, x, y, w, h, FALSE, TRUE, TRUE);
1233         }
1234         break;
1235     }
1236     case UnmapNotify:
1237         ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
1238                  "ignores left %d",
1239                  client->window, e->xunmap.event, e->xunmap.from_configure,
1240                  client->ignore_unmaps);
1241         if (client->ignore_unmaps) {
1242             client->ignore_unmaps--;
1243             break;
1244         }
1245         client_unmanage(client);
1246         break;
1247     case DestroyNotify:
1248         ob_debug("DestroyNotify for window 0x%x", client->window);
1249         client_unmanage(client);
1250         break;
1251     case ReparentNotify:
1252         /* this is when the client is first taken captive in the frame */
1253         if (e->xreparent.parent == client->frame->window) break;
1254
1255         /*
1256           This event is quite rare and is usually handled in unmapHandler.
1257           However, if the window is unmapped when the reparent event occurs,
1258           the window manager never sees it because an unmap event is not sent
1259           to an already unmapped window.
1260         */
1261
1262         /* we don't want the reparent event, put it back on the stack for the
1263            X server to deal with after we unmanage the window */
1264         XPutBackEvent(obt_display, e);
1265
1266         ob_debug("ReparentNotify for window 0x%x", client->window);
1267         client_unmanage(client);
1268         break;
1269     case MapRequest:
1270         ob_debug("MapRequest for 0x%lx", client->window);
1271         if (!client->iconic) break; /* this normally doesn't happen, but if it
1272                                        does, we don't want it!
1273                                        it can happen now when the window is on
1274                                        another desktop, but we still don't
1275                                        want it! */
1276         client_activate(client, FALSE, TRUE, TRUE, TRUE);
1277         break;
1278     case ClientMessage:
1279         /* validate cuz we query stuff off the client here */
1280         if (!client_validate(client)) break;
1281
1282         if (e->xclient.format != 32) return;
1283
1284         msgtype = e->xclient.message_type;
1285         if (msgtype == OBT_PROP_ATOM(WM_CHANGE_STATE)) {
1286             /* compress changes into a single change */
1287             while (XCheckTypedWindowEvent(obt_display, client->window,
1288                                           e->type, &ce)) {
1289                 /* XXX: it would be nice to compress ALL messages of a
1290                    type, not just messages in a row without other
1291                    message types between. */
1292                 if (ce.xclient.message_type != msgtype) {
1293                     XPutBackEvent(obt_display, &ce);
1294                     break;
1295                 }
1296                 e->xclient = ce.xclient;
1297             }
1298             client_set_wm_state(client, e->xclient.data.l[0]);
1299         } else if (msgtype == OBT_PROP_ATOM(NET_WM_DESKTOP)) {
1300             /* compress changes into a single change */
1301             while (XCheckTypedWindowEvent(obt_display, client->window,
1302                                           e->type, &ce)) {
1303                 /* XXX: it would be nice to compress ALL messages of a
1304                    type, not just messages in a row without other
1305                    message types between. */
1306                 if (ce.xclient.message_type != msgtype) {
1307                     XPutBackEvent(obt_display, &ce);
1308                     break;
1309                 }
1310                 e->xclient = ce.xclient;
1311             }
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],
1315                                    FALSE, FALSE);
1316         } else if (msgtype == OBT_PROP_ATOM(NET_WM_STATE)) {
1317             gulong ignore_start;
1318
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],
1325                      client->window);
1326
1327             /* ignore enter events caused by these like ob actions do */
1328             if (!config_focus_under_mouse)
1329                 ignore_start = event_start_ignore_all_enters();
1330             client_set_state(client, e->xclient.data.l[0],
1331                              e->xclient.data.l[1], e->xclient.data.l[2]);
1332             if (!config_focus_under_mouse)
1333                 event_end_ignore_all_enters(ignore_start);
1334         } else if (msgtype == OBT_PROP_ATOM(NET_CLOSE_WINDOW)) {
1335             ob_debug("net_close_window for 0x%lx", client->window);
1336             client_close(client);
1337         } else if (msgtype == OBT_PROP_ATOM(NET_ACTIVE_WINDOW)) {
1338             ob_debug("net_active_window for 0x%lx source=%s",
1339                      client->window,
1340                      (e->xclient.data.l[0] == 0 ? "unknown" :
1341                       (e->xclient.data.l[0] == 1 ? "application" :
1342                        (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1343             /* XXX make use of data.l[2] !? */
1344             if (e->xclient.data.l[0] == 1 || e->xclient.data.l[0] == 2) {
1345                 /* don't use the user's timestamp for client_focus, cuz if it's
1346                    an old broken timestamp (happens all the time) then focus
1347                    won't move even though we're trying to move it
1348                   event_curtime = e->xclient.data.l[1];*/
1349                 if (e->xclient.data.l[1] == 0)
1350                     ob_debug_type(OB_DEBUG_APP_BUGS,
1351                                   "_NET_ACTIVE_WINDOW message for window %s is"
1352                                   " missing a timestamp", client->title);
1353             } else
1354                 ob_debug_type(OB_DEBUG_APP_BUGS,
1355                               "_NET_ACTIVE_WINDOW message for window %s is "
1356                               "missing source indication");
1357             client_activate(client, FALSE, TRUE, TRUE,
1358                             (e->xclient.data.l[0] == 0 ||
1359                              e->xclient.data.l[0] == 2));
1360         } else if (msgtype == OBT_PROP_ATOM(NET_WM_MOVERESIZE)) {
1361             ob_debug("net_wm_moveresize for 0x%lx direction %d",
1362                      client->window, e->xclient.data.l[2]);
1363             if ((Atom)e->xclient.data.l[2] ==
1364                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOPLEFT) ||
1365                 (Atom)e->xclient.data.l[2] ==
1366                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOP) ||
1367                 (Atom)e->xclient.data.l[2] ==
1368                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_TOPRIGHT) ||
1369                 (Atom)e->xclient.data.l[2] ==
1370                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_RIGHT) ||
1371                 (Atom)e->xclient.data.l[2] ==
1372                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_RIGHT) ||
1373                 (Atom)e->xclient.data.l[2] ==
1374                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT) ||
1375                 (Atom)e->xclient.data.l[2] ==
1376                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOM) ||
1377                 (Atom)e->xclient.data.l[2] ==
1378                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT) ||
1379                 (Atom)e->xclient.data.l[2] ==
1380                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_LEFT) ||
1381                 (Atom)e->xclient.data.l[2] ==
1382                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE) ||
1383                 (Atom)e->xclient.data.l[2] ==
1384                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_KEYBOARD) ||
1385                 (Atom)e->xclient.data.l[2] ==
1386                 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE_KEYBOARD))
1387             {
1388                 moveresize_start(client, e->xclient.data.l[0],
1389                                  e->xclient.data.l[1], e->xclient.data.l[3],
1390                                  e->xclient.data.l[2]);
1391             }
1392             else if ((Atom)e->xclient.data.l[2] ==
1393                      OBT_PROP_ATOM(NET_WM_MOVERESIZE_CANCEL))
1394                 moveresize_end(TRUE);
1395         } else if (msgtype == OBT_PROP_ATOM(NET_MOVERESIZE_WINDOW)) {
1396             gint ograv, x, y, w, h;
1397
1398             ograv = client->gravity;
1399
1400             if (e->xclient.data.l[0] & 0xff)
1401                 client->gravity = e->xclient.data.l[0] & 0xff;
1402
1403             if (e->xclient.data.l[0] & 1 << 8)
1404                 x = e->xclient.data.l[1];
1405             else
1406                 x = client->area.x;
1407             if (e->xclient.data.l[0] & 1 << 9)
1408                 y = e->xclient.data.l[2];
1409             else
1410                 y = client->area.y;
1411
1412             if (e->xclient.data.l[0] & 1 << 10) {
1413                 w = e->xclient.data.l[3];
1414
1415                 /* if x was not given, then use gravity to figure out the new
1416                    x.  the reference point should not be moved */
1417                 if (!(e->xclient.data.l[0] & 1 << 8))
1418                     client_gravity_resize_w(client, &x, client->area.width, w);
1419             }
1420             else
1421                 w = client->area.width;
1422
1423             if (e->xclient.data.l[0] & 1 << 11) {
1424                 h = e->xclient.data.l[4];
1425
1426                 /* if y was not given, then use gravity to figure out the new
1427                    y.  the reference point should not be moved */
1428                 if (!(e->xclient.data.l[0] & 1 << 9))
1429                     client_gravity_resize_h(client, &y, client->area.height,h);
1430             }
1431             else
1432                 h = client->area.height;
1433
1434             ob_debug("MOVERESIZE x %d %d y %d %d (gravity %d)",
1435                      e->xclient.data.l[0] & 1 << 8, x,
1436                      e->xclient.data.l[0] & 1 << 9, y,
1437                      client->gravity);
1438
1439             client_find_onscreen(client, &x, &y, w, h, FALSE);
1440
1441             client_configure(client, x, y, w, h, FALSE, TRUE, FALSE);
1442
1443             client->gravity = ograv;
1444         } else if (msgtype == OBT_PROP_ATOM(NET_RESTACK_WINDOW)) {
1445             if (e->xclient.data.l[0] != 2) {
1446                 ob_debug_type(OB_DEBUG_APP_BUGS,
1447                               "_NET_RESTACK_WINDOW sent for window %s with "
1448                               "invalid source indication %ld",
1449                               client->title, e->xclient.data.l[0]);
1450             } else {
1451                 ObClient *sibling = NULL;
1452                 if (e->xclient.data.l[1]) {
1453                     ObWindow *win = window_find(e->xclient.data.l[1]);
1454                     if (WINDOW_IS_CLIENT(win) &&
1455                         WINDOW_AS_CLIENT(win) != client)
1456                     {
1457                         sibling = WINDOW_AS_CLIENT(win);
1458                     }
1459                     if (sibling == NULL)
1460                         ob_debug_type(OB_DEBUG_APP_BUGS,
1461                                       "_NET_RESTACK_WINDOW sent for window %s "
1462                                       "with invalid sibling 0x%x",
1463                                  client->title, e->xclient.data.l[1]);
1464                 }
1465                 if (e->xclient.data.l[2] == Below ||
1466                     e->xclient.data.l[2] == BottomIf ||
1467                     e->xclient.data.l[2] == Above ||
1468                     e->xclient.data.l[2] == TopIf ||
1469                     e->xclient.data.l[2] == Opposite)
1470                 {
1471                     gulong ignore_start;
1472
1473                     if (!config_focus_under_mouse)
1474                         ignore_start = event_start_ignore_all_enters();
1475                     /* just raise, don't activate */
1476                     stacking_restack_request(client, sibling,
1477                                              e->xclient.data.l[2]);
1478                     if (!config_focus_under_mouse)
1479                         event_end_ignore_all_enters(ignore_start);
1480
1481                     /* send a synthetic ConfigureNotify, cuz this is supposed
1482                        to be like a ConfigureRequest. */
1483                     client_reconfigure(client, TRUE);
1484                 } else
1485                     ob_debug_type(OB_DEBUG_APP_BUGS,
1486                                   "_NET_RESTACK_WINDOW sent for window %s "
1487                                   "with invalid detail %d",
1488                                   client->title, e->xclient.data.l[2]);
1489             }
1490         }
1491         break;
1492     case PropertyNotify:
1493         /* validate cuz we query stuff off the client here */
1494         if (!client_validate(client)) break;
1495
1496         /* compress changes to a single property into a single change */
1497         while (XCheckTypedWindowEvent(obt_display, client->window,
1498                                       e->type, &ce)) {
1499             Atom a, b;
1500
1501             /* XXX: it would be nice to compress ALL changes to a property,
1502                not just changes in a row without other props between. */
1503
1504             a = ce.xproperty.atom;
1505             b = e->xproperty.atom;
1506
1507             if (a == b)
1508                 continue;
1509             if ((a == OBT_PROP_ATOM(NET_WM_NAME) ||
1510                  a == OBT_PROP_ATOM(WM_NAME) ||
1511                  a == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1512                  a == OBT_PROP_ATOM(WM_ICON_NAME))
1513                 &&
1514                 (b == OBT_PROP_ATOM(NET_WM_NAME) ||
1515                  b == OBT_PROP_ATOM(WM_NAME) ||
1516                  b == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1517                  b == OBT_PROP_ATOM(WM_ICON_NAME))) {
1518                 continue;
1519             }
1520             if (a == OBT_PROP_ATOM(NET_WM_ICON) &&
1521                 b == OBT_PROP_ATOM(NET_WM_ICON))
1522                 continue;
1523
1524             XPutBackEvent(obt_display, &ce);
1525             break;
1526         }
1527
1528         msgtype = e->xproperty.atom;
1529         if (msgtype == XA_WM_NORMAL_HINTS) {
1530             ob_debug("Update NORMAL hints");
1531             client_update_normal_hints(client);
1532             /* normal hints can make a window non-resizable */
1533             client_setup_decor_and_functions(client, FALSE);
1534
1535             /* make sure the client's sizes are within its bounds, but only
1536                reconfigure the window if it needs to. emacs will update its
1537                normal hints every time it receives a conigurenotify */
1538             client_reconfigure(client, FALSE);
1539         } else if (msgtype == XA_WM_HINTS) {
1540             client_update_wmhints(client);
1541         } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1542             client_update_transient_for(client);
1543             client_get_type_and_transientness(client);
1544             /* type may have changed, so update the layer */
1545             client_calc_layer(client);
1546             client_setup_decor_and_functions(client, TRUE);
1547         } else if (msgtype == OBT_PROP_ATOM(NET_WM_NAME) ||
1548                    msgtype == OBT_PROP_ATOM(WM_NAME) ||
1549                    msgtype == OBT_PROP_ATOM(NET_WM_ICON_NAME) ||
1550                    msgtype == OBT_PROP_ATOM(WM_ICON_NAME)) {
1551             client_update_title(client);
1552         } else if (msgtype == OBT_PROP_ATOM(WM_PROTOCOLS)) {
1553             client_update_protocols(client);
1554             client_setup_decor_and_functions(client, TRUE);
1555         }
1556         else if (msgtype == OBT_PROP_ATOM(NET_WM_STRUT)) {
1557             client_update_strut(client);
1558         }
1559         else if (msgtype == OBT_PROP_ATOM(NET_WM_STRUT_PARTIAL)) {
1560             client_update_strut(client);
1561         }
1562         else if (msgtype == OBT_PROP_ATOM(NET_WM_ICON)) {
1563             client_update_icons(client);
1564         }
1565         else if (msgtype == OBT_PROP_ATOM(NET_WM_ICON_GEOMETRY)) {
1566             client_update_icon_geometry(client);
1567         }
1568         else if (msgtype == OBT_PROP_ATOM(NET_WM_USER_TIME)) {
1569             guint32 t;
1570             if (client == focus_client &&
1571                 OBT_PROP_GET32(client->window, NET_WM_USER_TIME, CARDINAL, &t)
1572                 && t && !event_time_after(t, e->xproperty.time) &&
1573                 (!event_last_user_time ||
1574                  event_time_after(t, event_last_user_time)))
1575             {
1576                 event_last_user_time = t;
1577             }
1578         }
1579 #ifdef SYNC
1580         else if (msgtype == OBT_PROP_ATOM(NET_WM_SYNC_REQUEST_COUNTER)) {
1581             client_update_sync_request_counter(client);
1582         }
1583 #endif
1584         break;
1585     case ColormapNotify:
1586         client_update_colormap(client, e->xcolormap.colormap);
1587         break;
1588     default:
1589         ;
1590 #ifdef SHAPE
1591         if (obt_display_extension_shape &&
1592             e->type == obt_display_extension_shape_basep)
1593         {
1594             client->shaped = ((XShapeEvent*)e)->shaped;
1595             frame_adjust_shape(client->frame);
1596         }
1597 #endif
1598     }
1599 }
1600
1601 static void event_handle_dock(ObDock *s, XEvent *e)
1602 {
1603     switch (e->type) {
1604     case ButtonPress:
1605         if (e->xbutton.button == 1)
1606             stacking_raise(DOCK_AS_WINDOW(s));
1607         else if (e->xbutton.button == 2)
1608             stacking_lower(DOCK_AS_WINDOW(s));
1609         break;
1610     case EnterNotify:
1611         dock_hide(FALSE);
1612         break;
1613     case LeaveNotify:
1614         /* don't hide when moving into a dock app */
1615         if (e->xcrossing.detail != NotifyInferior)
1616             dock_hide(TRUE);
1617         break;
1618     }
1619 }
1620
1621 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1622 {
1623     switch (e->type) {
1624     case MotionNotify:
1625         dock_app_drag(app, &e->xmotion);
1626         break;
1627     case UnmapNotify:
1628         if (app->ignore_unmaps) {
1629             app->ignore_unmaps--;
1630             break;
1631         }
1632         dock_unmanage(app, TRUE);
1633         break;
1634     case DestroyNotify:
1635         dock_unmanage(app, FALSE);
1636         break;
1637     case ReparentNotify:
1638         dock_unmanage(app, FALSE);
1639         break;
1640     case ConfigureNotify:
1641         dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1642         break;
1643     }
1644 }
1645
1646 static ObMenuFrame* find_active_menu(void)
1647 {
1648     GList *it;
1649     ObMenuFrame *ret = NULL;
1650
1651     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1652         ret = it->data;
1653         if (ret->selected)
1654             break;
1655         ret = NULL;
1656     }
1657     return ret;
1658 }
1659
1660 static ObMenuFrame* find_active_or_last_menu(void)
1661 {
1662     ObMenuFrame *ret = NULL;
1663
1664     ret = find_active_menu();
1665     if (!ret && menu_frame_visible)
1666         ret = menu_frame_visible->data;
1667     return ret;
1668 }
1669
1670 static gboolean event_handle_menu_input(XEvent *ev)
1671 {
1672     gboolean ret = FALSE;
1673
1674     if (ev->type == ButtonRelease) {
1675         ObMenuEntryFrame *e;
1676
1677         if (menu_hide_delay_reached() &&
1678             (ev->xbutton.button < 4 || ev->xbutton.button > 5))
1679         {
1680             if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1681                                             ev->xbutton.y_root)))
1682             {
1683                 menu_frame_select(e->frame, e, TRUE);
1684                 menu_entry_frame_execute(e, ev->xbutton.state);
1685             }
1686             else
1687                 menu_frame_hide_all();
1688         }
1689         ret = TRUE;
1690     }
1691     else if (ev->type == MotionNotify) {
1692         ObMenuFrame *f;
1693         ObMenuEntryFrame *e;
1694
1695         if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1696                                         ev->xmotion.y_root)))
1697             if (!(f = find_active_menu()) ||
1698                 f == e->frame ||
1699                 f->parent == e->frame ||
1700                 f->child == e->frame)
1701                 menu_frame_select(e->frame, e, FALSE);
1702     }
1703     else if (ev->type == KeyPress || ev->type == KeyRelease) {
1704         guint keycode, state;
1705         gunichar unikey;
1706         ObMenuFrame *frame;
1707
1708         keycode = ev->xkey.keycode;
1709         state = ev->xkey.state;
1710         unikey = obt_keyboard_keycode_to_unichar(keycode);
1711
1712         frame = find_active_or_last_menu();
1713         if (frame == NULL)
1714             g_assert_not_reached(); /* there is no active menu */
1715
1716         /* Allow control while going thru the menu */
1717         else if (ev->type == KeyPress && (state & ~ControlMask) == 0) {
1718             frame->got_press = TRUE;
1719
1720             if (keycode == ob_keycode(OB_KEY_ESCAPE)) {
1721                 menu_frame_hide_all();
1722                 ret = TRUE;
1723             }
1724
1725             else if (keycode == ob_keycode(OB_KEY_LEFT)) {
1726                 /* Left goes to the parent menu */
1727                 menu_frame_select(frame, NULL, TRUE);
1728                 ret = TRUE;
1729             }
1730
1731             else if (keycode == ob_keycode(OB_KEY_RIGHT)) {
1732                 /* Right goes to the selected submenu */
1733                 if (frame->child) menu_frame_select_next(frame->child);
1734                 ret = TRUE;
1735             }
1736
1737             else if (keycode == ob_keycode(OB_KEY_UP)) {
1738                 menu_frame_select_previous(frame);
1739                 ret = TRUE;
1740             }
1741
1742             else if (keycode == ob_keycode(OB_KEY_DOWN)) {
1743                 menu_frame_select_next(frame);
1744                 ret = TRUE;
1745             }
1746         }
1747
1748         /* Use KeyRelease events for running things so that the key release
1749            doesn't get sent to the focused application.
1750
1751            Allow ControlMask only, and don't bother if the menu is empty */
1752         else if (ev->type == KeyRelease && (state & ~ControlMask) == 0 &&
1753                  frame->entries && frame->got_press)
1754         {
1755             if (keycode == ob_keycode(OB_KEY_RETURN)) {
1756                 /* Enter runs the active item or goes into the submenu.
1757                    Control-Enter runs it without closing the menu. */
1758                 if (frame->child)
1759                     menu_frame_select_next(frame->child);
1760                 else if (frame->selected)
1761                     menu_entry_frame_execute(frame->selected, state);
1762
1763                 ret = TRUE;
1764             }
1765
1766             /* keyboard accelerator shortcuts. (if it was a valid key) */
1767             else if (unikey != 0) {
1768                 GList *start;
1769                 GList *it;
1770                 ObMenuEntryFrame *found = NULL;
1771                 guint num_found = 0;
1772
1773                 /* start after the selected one */
1774                 start = frame->entries;
1775                 if (frame->selected) {
1776                     for (it = start; frame->selected != it->data;
1777                          it = g_list_next(it))
1778                         g_assert(it != NULL); /* nothing was selected? */
1779                     /* next with wraparound */
1780                     start = g_list_next(it);
1781                     if (start == NULL) start = frame->entries;
1782                 }
1783
1784                 it = start;
1785                 do {
1786                     ObMenuEntryFrame *e = it->data;
1787                     gunichar entrykey = 0;
1788
1789                     if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1790                         entrykey = e->entry->data.normal.shortcut;
1791                     else if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1792                         entrykey = e->entry->data.submenu.submenu->shortcut;
1793
1794                     if (unikey == entrykey) {
1795                         if (found == NULL) found = e;
1796                         ++num_found;
1797                     }
1798
1799                     /* next with wraparound */
1800                     it = g_list_next(it);
1801                     if (it == NULL) it = frame->entries;
1802                 } while (it != start);
1803
1804                 if (found) {
1805                     if (found->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1806                         num_found == 1)
1807                     {
1808                         menu_frame_select(frame, found, TRUE);
1809                         usleep(50000); /* highlight the item for a short bit so
1810                                           the user can see what happened */
1811                         menu_entry_frame_execute(found, state);
1812                     } else {
1813                         menu_frame_select(frame, found, TRUE);
1814                         if (num_found == 1)
1815                             menu_frame_select_next(frame->child);
1816                     }
1817
1818                     ret = TRUE;
1819                 }
1820             }
1821         }
1822     }
1823
1824     return ret;
1825 }
1826
1827 static void event_handle_menu(ObMenuFrame *frame, XEvent *ev)
1828 {
1829     ObMenuFrame *f;
1830     ObMenuEntryFrame *e;
1831
1832     switch (ev->type) {
1833     case EnterNotify:
1834         if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1835             if (e->ignore_enters)
1836                 --e->ignore_enters;
1837             else if (!(f = find_active_menu()) ||
1838                      f == e->frame ||
1839                      f->parent == e->frame ||
1840                      f->child == e->frame)
1841                 menu_frame_select(e->frame, e, FALSE);
1842         }
1843         break;
1844     case LeaveNotify:
1845         /*ignore leaves when we're already in the window */
1846         if (ev->xcrossing.detail == NotifyInferior)
1847             break;
1848
1849         if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1850             (f = find_active_menu()) && f->selected == e &&
1851             e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1852         {
1853             menu_frame_select(e->frame, NULL, FALSE);
1854         }
1855         break;
1856     }
1857 }
1858
1859 static void event_handle_user_input(ObClient *client, XEvent *e)
1860 {
1861     g_assert(e->type == ButtonPress || e->type == ButtonRelease ||
1862              e->type == MotionNotify || e->type == KeyPress ||
1863              e->type == KeyRelease);
1864
1865     if (menu_frame_visible) {
1866         if (event_handle_menu_input(e))
1867             /* don't use the event if the menu used it, but if the menu
1868                didn't use it and it's a keypress that is bound, it will
1869                close the menu and be used */
1870             return;
1871     }
1872
1873     /* if the keyboard interactive action uses the event then dont
1874        use it for bindings. likewise is moveresize uses the event. */
1875     if (!actions_interactive_input_event(e) && !moveresize_event(e)) {
1876         if (moveresize_in_progress)
1877             /* make further actions work on the client being
1878                moved/resized */
1879             client = moveresize_client;
1880
1881         if (e->type == ButtonPress ||
1882             e->type == ButtonRelease ||
1883             e->type == MotionNotify)
1884         {
1885             /* the frame may not be "visible" but they can still click on it
1886                in the case where it is animating before disappearing */
1887             if (!client || !frame_iconify_animating(client->frame))
1888                 mouse_event(client, e);
1889         } else
1890             keyboard_event((focus_cycle_target ? focus_cycle_target :
1891                             (client ? client : focus_client)), e);
1892     }
1893 }
1894
1895 static void focus_delay_dest(gpointer data)
1896 {
1897     g_free(data);
1898 }
1899
1900 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1901 {
1902     const ObFocusDelayData *f1 = d1;
1903     return f1->client == d2;
1904 }
1905
1906 static gboolean focus_delay_func(gpointer data)
1907 {
1908     ObFocusDelayData *d = data;
1909     Time old = event_curtime;
1910
1911     /* don't move focus and kill the menu or the move/resize */
1912     if (menu_frame_visible || moveresize_in_progress) return FALSE;
1913
1914     event_curtime = d->time;
1915     event_curserial = d->serial;
1916     if (client_focus(d->client) && config_focus_raise)
1917         stacking_raise(CLIENT_AS_WINDOW(d->client));
1918     event_curtime = old;
1919     return FALSE; /* no repeat */
1920 }
1921
1922 static void focus_delay_client_dest(ObClient *client, gpointer data)
1923 {
1924     obt_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1925                                       client, FALSE);
1926 }
1927
1928 void event_halt_focus_delay(void)
1929 {
1930     /* ignore all enter events up till the event which caused this to occur */
1931     if (event_curserial) event_ignore_enter_range(1, event_curserial);
1932     obt_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1933 }
1934
1935 gulong event_start_ignore_all_enters(void)
1936 {
1937     return NextRequest(obt_display);
1938 }
1939
1940 static void event_ignore_enter_range(gulong start, gulong end)
1941 {
1942     ObSerialRange *r;
1943
1944     g_assert(start != 0);
1945     g_assert(end != 0);
1946
1947     r = g_new(ObSerialRange, 1);
1948     r->start = start;
1949     r->end = end;
1950     ignore_serials = g_slist_prepend(ignore_serials, r);
1951
1952     ob_debug_type(OB_DEBUG_FOCUS, "ignoring enters from %lu until %lu",
1953                   r->start, r->end);
1954
1955     /* increment the serial so we don't ignore events we weren't meant to */
1956     OBT_PROP_ERASE(screen_support_win, MOTIF_WM_HINTS);
1957 }
1958
1959 void event_end_ignore_all_enters(gulong start)
1960 {
1961     /* Use (NextRequest-1) so that we ignore up to the current serial only.
1962        Inside event_ignore_enter_range, we increment the serial by one, but if
1963        we ignore that serial too, then any enter events generated by mouse
1964        movement will be ignored until we create some further network traffic.
1965        Instead ignore up to NextRequest-1, then when we increment the serial,
1966        we will be *past* the range of ignored serials */
1967     event_ignore_enter_range(start, NextRequest(obt_display)-1);
1968 }
1969
1970 static gboolean is_enter_focus_event_ignored(XEvent *e)
1971 {
1972     GSList *it, *next;
1973
1974     g_assert(e->type == EnterNotify &&
1975              !(e->xcrossing.mode == NotifyGrab ||
1976                e->xcrossing.mode == NotifyUngrab ||
1977                e->xcrossing.detail == NotifyInferior));
1978
1979     for (it = ignore_serials; it; it = next) {
1980         ObSerialRange *r = it->data;
1981
1982         next = g_slist_next(it);
1983
1984         if ((glong)(e->xany.serial - r->end) > 0) {
1985             /* past the end */
1986             ignore_serials = g_slist_delete_link(ignore_serials, it);
1987             g_free(r);
1988         }
1989         else if ((glong)(e->xany.serial - r->start) >= 0)
1990             return TRUE;
1991     }
1992     return FALSE;
1993 }
1994
1995 void event_cancel_all_key_grabs(void)
1996 {
1997     if (actions_interactive_act_running()) {
1998         actions_interactive_cancel_act();
1999         ob_debug("KILLED interactive action");
2000     }
2001     else if (menu_frame_visible) {
2002         menu_frame_hide_all();
2003         ob_debug("KILLED open menus");
2004     }
2005     else if (moveresize_in_progress) {
2006         moveresize_end(TRUE);
2007         ob_debug("KILLED interactive moveresize");
2008     }
2009     else if (grab_on_keyboard()) {
2010         ungrab_keyboard();
2011         ob_debug("KILLED active grab on keyboard");
2012     }
2013     else
2014         ungrab_passive_key();
2015
2016     XSync(obt_display, FALSE);
2017 }
2018
2019 gboolean event_time_after(Time t1, Time t2)
2020 {
2021     g_assert(t1 != CurrentTime);
2022     g_assert(t2 != CurrentTime);
2023
2024     /*
2025       Timestamp values wrap around (after about 49.7 days). The server, given
2026       its current time is represented by timestamp T, always interprets
2027       timestamps from clients by treating half of the timestamp space as being
2028       later in time than T.
2029       - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
2030     */
2031
2032     /* TIME_HALF is half of the number space of a Time type variable */
2033 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
2034
2035     if (t2 >= TIME_HALF)
2036         /* t2 is in the second half so t1 might wrap around and be smaller than
2037            t2 */
2038         return t1 >= t2 || t1 < (t2 + TIME_HALF);
2039     else
2040         /* t2 is in the first half so t1 has to come after it */
2041         return t1 >= t2 && t1 < (t2 + TIME_HALF);
2042 }
2043
2044 Time event_get_server_time(void)
2045 {
2046     /* Generate a timestamp */
2047     XEvent event;
2048
2049     XChangeProperty(obt_display, screen_support_win,
2050                     OBT_PROP_ATOM(WM_CLASS), OBT_PROP_ATOM(STRING),
2051                     8, PropModeAppend, NULL, 0);
2052     XWindowEvent(obt_display, screen_support_win, PropertyChangeMask, &event);
2053     return event.xproperty.time;
2054 }