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