]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/event.c
leavenotify events messing up delayed focus
[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 "client.h"
26 #include "xerror.h"
27 #include "prop.h"
28 #include "config.h"
29 #include "screen.h"
30 #include "frame.h"
31 #include "menu.h"
32 #include "menuframe.h"
33 #include "keyboard.h"
34 #include "mouse.h"
35 #include "mainloop.h"
36 #include "framerender.h"
37 #include "focus.h"
38 #include "moveresize.h"
39 #include "group.h"
40 #include "stacking.h"
41 #include "extensions.h"
42
43 #include <X11/Xlib.h>
44 #include <X11/keysym.h>
45 #include <X11/Xatom.h>
46 #include <glib.h>
47
48 #ifdef HAVE_SYS_SELECT_H
49 #  include <sys/select.h>
50 #endif
51 #ifdef HAVE_SIGNAL_H
52 #  include <signal.h>
53 #endif
54 #ifdef XKB
55 #  include <X11/XKBlib.h>
56 #endif
57
58 #ifdef USE_SM
59 #include <X11/ICE/ICElib.h>
60 #endif
61
62 typedef struct
63 {
64     gboolean ignored;
65 } ObEventData;
66
67 typedef struct
68 {
69     ObClient *client;
70     Time time;
71 } ObFocusDelayData;
72
73 static void event_process(const XEvent *e, gpointer data);
74 static void event_handle_root(XEvent *e);
75 static void event_handle_menu(XEvent *e);
76 static void event_handle_dock(ObDock *s, XEvent *e);
77 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
78 static void event_handle_client(ObClient *c, XEvent *e);
79 static void event_handle_group(ObGroup *g, XEvent *e);
80
81 static void focus_delay_dest(gpointer data);
82 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
83 static gboolean focus_delay_func(gpointer data);
84 static void focus_delay_client_dest(ObClient *client, gpointer data);
85
86 static gboolean menu_hide_delay_func(gpointer data);
87
88 /* The time for the current event being processed */
89 Time event_curtime = CurrentTime;
90
91 /*! The value of the mask for the NumLock modifier */
92 guint NumLockMask;
93 /*! The value of the mask for the ScrollLock modifier */
94 guint ScrollLockMask;
95 /*! The key codes for the modifier keys */
96 static XModifierKeymap *modmap;
97 /*! Table of the constant modifier masks */
98 static const gint mask_table[] = {
99     ShiftMask, LockMask, ControlMask, Mod1Mask,
100     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
101 };
102 static gint mask_table_size;
103
104 static guint ignore_enter_focus = 0;
105
106 static gboolean menu_can_hide;
107
108 #ifdef USE_SM
109 static void ice_handler(gint fd, gpointer conn)
110 {
111     Bool b;
112     IceProcessMessages(conn, NULL, &b);
113 }
114
115 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
116                       IcePointer *watch_data)
117 {
118     static gint fd = -1;
119
120     if (opening) {
121         fd = IceConnectionNumber(conn);
122         ob_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
123     } else {
124         ob_main_loop_fd_remove(ob_main_loop, fd);
125         fd = -1;
126     }
127 }
128 #endif
129
130 void event_startup(gboolean reconfig)
131 {
132     if (reconfig) return;
133
134     mask_table_size = sizeof(mask_table) / sizeof(mask_table[0]);
135      
136     /* get lock masks that are defined by the display (not constant) */
137     modmap = XGetModifierMapping(ob_display);
138     g_assert(modmap);
139     if (modmap && modmap->max_keypermod > 0) {
140         size_t cnt;
141         const size_t size = mask_table_size * modmap->max_keypermod;
142         /* get the values of the keyboard lock modifiers
143            Note: Caps lock is not retrieved the same way as Scroll and Num
144            lock since it doesn't need to be. */
145         const KeyCode num_lock = XKeysymToKeycode(ob_display, XK_Num_Lock);
146         const KeyCode scroll_lock = XKeysymToKeycode(ob_display,
147                                                      XK_Scroll_Lock);
148
149         for (cnt = 0; cnt < size; ++cnt) {
150             if (! modmap->modifiermap[cnt]) continue;
151
152             if (num_lock == modmap->modifiermap[cnt])
153                 NumLockMask = mask_table[cnt / modmap->max_keypermod];
154             if (scroll_lock == modmap->modifiermap[cnt])
155                 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
156         }
157     }
158
159     ob_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
160
161 #ifdef USE_SM
162     IceAddConnectionWatch(ice_watch, NULL);
163 #endif
164
165     client_add_destructor(focus_delay_client_dest, NULL);
166 }
167
168 void event_shutdown(gboolean reconfig)
169 {
170     if (reconfig) return;
171
172 #ifdef USE_SM
173     IceRemoveConnectionWatch(ice_watch, NULL);
174 #endif
175
176     client_remove_destructor(focus_delay_client_dest);
177     XFreeModifiermap(modmap);
178 }
179
180 static Window event_get_window(XEvent *e)
181 {
182     Window window;
183
184     /* pick a window */
185     switch (e->type) {
186     case SelectionClear:
187         window = RootWindow(ob_display, ob_screen);
188         break;
189     case MapRequest:
190         window = e->xmap.window;
191         break;
192     case UnmapNotify:
193         window = e->xunmap.window;
194         break;
195     case DestroyNotify:
196         window = e->xdestroywindow.window;
197         break;
198     case ConfigureRequest:
199         window = e->xconfigurerequest.window;
200         break;
201     case ConfigureNotify:
202         window = e->xconfigure.window;
203         break;
204     default:
205 #ifdef XKB
206         if (extensions_xkb && e->type == extensions_xkb_event_basep) {
207             switch (((XkbAnyEvent*)e)->xkb_type) {
208             case XkbBellNotify:
209                 window = ((XkbBellNotifyEvent*)e)->window;
210             default:
211                 window = None;
212             }
213         } else
214 #endif
215             window = e->xany.window;
216     }
217     return window;
218 }
219
220 static void event_set_curtime(XEvent *e)
221 {
222     Time t = CurrentTime;
223
224     /* grab the lasttime and hack up the state */
225     switch (e->type) {
226     case ButtonPress:
227     case ButtonRelease:
228         t = e->xbutton.time;
229         break;
230     case KeyPress:
231         t = e->xkey.time;
232         break;
233     case KeyRelease:
234         t = e->xkey.time;
235         break;
236     case MotionNotify:
237         t = e->xmotion.time;
238         break;
239     case PropertyNotify:
240         t = e->xproperty.time;
241         break;
242     case EnterNotify:
243     case LeaveNotify:
244         t = e->xcrossing.time;
245         break;
246     default:
247         /* if more event types are anticipated, get their timestamp
248            explicitly */
249         break;
250     }
251
252     event_curtime = t;
253 }
254
255 #define STRIP_MODS(s) \
256         s &= ~(LockMask | NumLockMask | ScrollLockMask), \
257         /* kill off the Button1Mask etc, only want the modifiers */ \
258         s &= (ControlMask | ShiftMask | Mod1Mask | \
259               Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask) \
260
261 static void event_hack_mods(XEvent *e)
262 {
263 #ifdef XKB
264     XkbStateRec xkb_state;
265 #endif
266     KeyCode *kp;
267     gint i, k;
268
269     switch (e->type) {
270     case ButtonPress:
271     case ButtonRelease:
272         STRIP_MODS(e->xbutton.state);
273         break;
274     case KeyPress:
275         STRIP_MODS(e->xkey.state);
276         break;
277     case KeyRelease:
278         STRIP_MODS(e->xkey.state);
279         /* remove from the state the mask of the modifier being released, if
280            it is a modifier key being released (this is a little ugly..) */
281 #ifdef XKB
282         if (XkbGetState(ob_display, XkbUseCoreKbd, &xkb_state) == Success) {
283             e->xkey.state = xkb_state.compat_state;
284             break;
285         }
286 #endif
287         kp = modmap->modifiermap;
288         for (i = 0; i < mask_table_size; ++i) {
289             for (k = 0; k < modmap->max_keypermod; ++k) {
290                 if (*kp == e->xkey.keycode) { /* found the keycode */
291                     /* remove the mask for it */
292                     e->xkey.state &= ~mask_table[i];
293                     /* cause the first loop to break; */
294                     i = mask_table_size;
295                     break; /* get outta here! */
296                 }
297                 ++kp;
298             }
299         }
300         break;
301     case MotionNotify:
302         STRIP_MODS(e->xmotion.state);
303         /* compress events */
304         {
305             XEvent ce;
306             while (XCheckTypedWindowEvent(ob_display, e->xmotion.window,
307                                           e->type, &ce)) {
308                 e->xmotion.x_root = ce.xmotion.x_root;
309                 e->xmotion.y_root = ce.xmotion.y_root;
310             }
311         }
312         break;
313     }
314 }
315
316 static gboolean wanted_focusevent(XEvent *e)
317 {
318     gint mode = e->xfocus.mode;
319     gint detail = e->xfocus.detail;
320     Window win = e->xany.window;
321
322     if (e->type == FocusIn) {
323
324         /* These are ones we never want.. */
325
326         /* This means focus was given by a keyboard/mouse grab. */
327         if (mode == NotifyGrab)
328             return FALSE;
329         /* This means focus was given back from a keyboard/mouse grab. */
330         if (mode == NotifyUngrab)
331             return FALSE;
332
333         /* These are the ones we want.. */
334
335         if (win == RootWindow(ob_display, ob_screen)) {
336             /* This means focus reverted off of a client */
337             if (detail == NotifyPointerRoot || detail == NotifyDetailNone ||
338                 detail == NotifyInferior)
339                 return TRUE;
340             else
341                 return FALSE;
342         }
343
344         /* This means focus moved from the root window to a client */
345         if (detail == NotifyVirtual)
346             return TRUE;
347         /* This means focus moved from one client to another */
348         if (detail == NotifyNonlinearVirtual)
349             return TRUE;
350
351         /* Otherwise.. */
352         return FALSE;
353     } else {
354         g_assert(e->type == FocusOut);
355
356
357         /* These are ones we never want.. */
358
359         /* This means focus was taken by a keyboard/mouse grab. */
360         if (mode == NotifyGrab)
361             return FALSE;
362
363         /* Focus left the root window revertedto state */
364         if (win == RootWindow(ob_display, ob_screen))
365             return FALSE;
366
367         /* These are the ones we want.. */
368
369         /* This means focus moved from a client to the root window */
370         if (detail == NotifyVirtual)
371             return TRUE;
372         /* This means focus moved from one client to another */
373         if (detail == NotifyNonlinearVirtual)
374             return TRUE;
375         /* This means focus had moved to our frame window and now moved off */
376         if (detail == NotifyNonlinear)
377             return TRUE;
378
379         /* Otherwise.. */
380         return FALSE;
381     }
382 }
383
384 static Bool look_for_focusin(Display *d, XEvent *e, XPointer arg)
385 {
386     return e->type == FocusIn && wanted_focusevent(e);
387 }
388
389 static gboolean event_ignore(XEvent *e, ObClient *client)
390 {
391     switch(e->type) {
392     case FocusIn:
393         if (!wanted_focusevent(e))
394             return TRUE;
395         break;
396     case FocusOut:
397         if (!wanted_focusevent(e))
398             return TRUE;
399         break;
400     }
401     return FALSE;
402 }
403
404 static void event_process(const XEvent *ec, gpointer data)
405 {
406     Window window;
407     ObGroup *group = NULL;
408     ObClient *client = NULL;
409     ObDock *dock = NULL;
410     ObDockApp *dockapp = NULL;
411     ObWindow *obwin = NULL;
412     XEvent ee, *e;
413     ObEventData *ed = data;
414
415     /* make a copy we can mangle */
416     ee = *ec;
417     e = &ee;
418
419     window = event_get_window(e);
420     if (!(e->type == PropertyNotify &&
421           (group = g_hash_table_lookup(group_map, &window))))
422         if ((obwin = g_hash_table_lookup(window_map, &window))) {
423             switch (obwin->type) {
424             case Window_Dock:
425                 dock = WINDOW_AS_DOCK(obwin);
426                 break;
427             case Window_DockApp:
428                 dockapp = WINDOW_AS_DOCKAPP(obwin);
429                 break;
430             case Window_Client:
431                 client = WINDOW_AS_CLIENT(obwin);
432                 break;
433             case Window_Menu:
434             case Window_Internal:
435                 /* not to be used for events */
436                 g_assert_not_reached();
437                 break;
438             }
439         }
440
441     event_set_curtime(e);
442     event_hack_mods(e);
443     if (event_ignore(e, client)) {
444         if (ed)
445             ed->ignored = TRUE;
446         return;
447     } else if (ed)
448             ed->ignored = FALSE;
449
450     /* deal with it in the kernel */
451
452     if (menu_frame_visible &&
453         (e->type == EnterNotify || e->type == LeaveNotify))
454     {
455         /* crossing events for menu */
456         event_handle_menu(e);
457     } else if (e->type == FocusIn) {
458         if (client && client != focus_client) {
459             frame_adjust_focus(client->frame, TRUE);
460             focus_set_client(client);
461             client_calc_layer(client);
462         }
463     } else if (e->type == FocusOut) {
464         gboolean nomove = FALSE;
465         XEvent ce;
466
467         ob_debug_type(OB_DEBUG_FOCUS, "FocusOut Event\n");
468
469         /* Look for the followup FocusIn */
470         if (!XCheckIfEvent(ob_display, &ce, look_for_focusin, NULL)) {
471             /* There is no FocusIn, this means focus went to a window that
472                is not being managed, or a window on another screen. */
473             ob_debug_type(OB_DEBUG_FOCUS, "Focus went to a black hole !\n");
474             /* nothing is focused */
475             focus_set_client(NULL);
476         } else if (ce.xany.window == e->xany.window) {
477             ob_debug_type(OB_DEBUG_FOCUS, "Focus didn't go anywhere\n");
478             /* If focus didn't actually move anywhere, there is nothing to do*/
479             nomove = TRUE;
480         } else if (ce.xfocus.detail == NotifyPointerRoot ||
481                    ce.xfocus.detail == NotifyDetailNone ||
482                    ce.xfocus.detail == NotifyInferior) {
483             ob_debug_type(OB_DEBUG_FOCUS, "Focus went to root\n");
484             /* Focus has been reverted to the root window or nothing
485                FocusOut events come after UnmapNotify, so we don't need to
486                worry about focusing an invalid window
487              */
488             focus_fallback(TRUE);
489         } else {
490             /* Focus did move, so process the FocusIn event */
491             ObEventData ed = { .ignored = FALSE };
492             event_process(&ce, &ed);
493             if (ed.ignored) {
494                 /* The FocusIn was ignored, this means it was on a window
495                    that isn't a client. */
496                 ob_debug_type(OB_DEBUG_FOCUS,
497                               "Focus went to an unmanaged window 0x%x !\n",
498                               ce.xfocus.window);
499                 focus_fallback(TRUE);
500             }
501         }
502
503         if (client && !nomove) {
504             frame_adjust_focus(client->frame, FALSE);
505             /* focus_set_client has already been called for sure */
506             client_calc_layer(client);
507         }
508     } else if (group)
509         event_handle_group(group, e);
510     else if (client)
511         event_handle_client(client, e);
512     else if (dockapp)
513         event_handle_dockapp(dockapp, e);
514     else if (dock)
515         event_handle_dock(dock, e);
516     else if (window == RootWindow(ob_display, ob_screen))
517         event_handle_root(e);
518     else if (e->type == MapRequest)
519         client_manage(window);
520     else if (e->type == ConfigureRequest) {
521         /* unhandled configure requests must be used to configure the
522            window directly */
523         XWindowChanges xwc;
524
525         xwc.x = e->xconfigurerequest.x;
526         xwc.y = e->xconfigurerequest.y;
527         xwc.width = e->xconfigurerequest.width;
528         xwc.height = e->xconfigurerequest.height;
529         xwc.border_width = e->xconfigurerequest.border_width;
530         xwc.sibling = e->xconfigurerequest.above;
531         xwc.stack_mode = e->xconfigurerequest.detail;
532        
533         /* we are not to be held responsible if someone sends us an
534            invalid request! */
535         xerror_set_ignore(TRUE);
536         XConfigureWindow(ob_display, window,
537                          e->xconfigurerequest.value_mask, &xwc);
538         xerror_set_ignore(FALSE);
539     }
540
541     /* user input (action-bound) events */
542     if (e->type == ButtonPress || e->type == ButtonRelease ||
543         e->type == MotionNotify || e->type == KeyPress ||
544         e->type == KeyRelease)
545     {
546         if (menu_frame_visible)
547             event_handle_menu(e);
548         else {
549             if (!keyboard_process_interactive_grab(e, &client)) {
550                 if (moveresize_in_progress) {
551                     moveresize_event(e);
552
553                     /* make further actions work on the client being
554                        moved/resized */
555                     client = moveresize_client;
556                 }
557
558                 menu_can_hide = FALSE;
559                 ob_main_loop_timeout_add(ob_main_loop,
560                                          config_menu_hide_delay * 1000,
561                                          menu_hide_delay_func,
562                                          NULL, g_direct_equal, NULL);
563
564                 if (e->type == ButtonPress || e->type == ButtonRelease ||
565                     e->type == MotionNotify) {
566                     mouse_event(client, e);
567                 } else if (e->type == KeyPress) {
568                     keyboard_event((focus_cycle_target ? focus_cycle_target :
569                                     client), e);
570                 }
571             }
572         }
573     }
574     /* if something happens and it's not from an XEvent, then we don't know
575        the time */
576     event_curtime = CurrentTime;
577 }
578
579 static void event_handle_root(XEvent *e)
580 {
581     Atom msgtype;
582      
583     switch(e->type) {
584     case SelectionClear:
585         ob_debug("Another WM has requested to replace us. Exiting.\n");
586         ob_exit_replace();
587         break;
588
589     case ClientMessage:
590         if (e->xclient.format != 32) break;
591
592         msgtype = e->xclient.message_type;
593         if (msgtype == prop_atoms.net_current_desktop) {
594             guint d = e->xclient.data.l[0];
595             if (d < screen_num_desktops) {
596                 event_curtime = e->xclient.data.l[1];
597                 ob_debug("SWITCH DESKTOP TIME: %d\n", event_curtime);
598                 screen_set_desktop(d);
599             }
600         } else if (msgtype == prop_atoms.net_number_of_desktops) {
601             guint d = e->xclient.data.l[0];
602             if (d > 0)
603                 screen_set_num_desktops(d);
604         } else if (msgtype == prop_atoms.net_showing_desktop) {
605             screen_show_desktop(e->xclient.data.l[0] != 0);
606         } else if (msgtype == prop_atoms.ob_control) {
607             if (e->xclient.data.l[0] == 1)
608                 ob_reconfigure();
609             else if (e->xclient.data.l[0] == 2)
610                 ob_restart();
611         }
612         break;
613     case PropertyNotify:
614         if (e->xproperty.atom == prop_atoms.net_desktop_names)
615             screen_update_desktop_names();
616         else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
617             screen_update_layout();
618         break;
619     case ConfigureNotify:
620 #ifdef XRANDR
621         XRRUpdateConfiguration(e);
622 #endif
623         screen_resize();
624         break;
625     default:
626         ;
627     }
628 }
629
630 static void event_handle_group(ObGroup *group, XEvent *e)
631 {
632     GSList *it;
633
634     g_assert(e->type == PropertyNotify);
635
636     for (it = group->members; it; it = g_slist_next(it))
637         event_handle_client(it->data, e);
638 }
639
640 void event_enter_client(ObClient *client)
641 {
642     g_assert(config_focus_follow);
643
644     if (client_normal(client) && client_can_focus(client)) {
645         if (config_focus_delay) {
646             ObFocusDelayData *data;
647
648             ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
649
650             data = g_new(ObFocusDelayData, 1);
651             data->client = client;
652             data->time = event_curtime;
653
654             ob_main_loop_timeout_add(ob_main_loop,
655                                      config_focus_delay,
656                                      focus_delay_func,
657                                      data, focus_delay_cmp, focus_delay_dest);
658         } else {
659             ObFocusDelayData data;
660             data.client = client;
661             data.time = event_curtime;
662             focus_delay_func(&data);
663         }
664     }
665 }
666
667 static void event_handle_client(ObClient *client, XEvent *e)
668 {
669     XEvent ce;
670     Atom msgtype;
671     gint i=0;
672     ObFrameContext con;
673      
674     switch (e->type) {
675     case VisibilityNotify:
676         client->frame->obscured = e->xvisibility.state != VisibilityUnobscured;
677         break;
678     case ButtonPress:
679     case ButtonRelease:
680         /* Wheel buttons don't draw because they are an instant click, so it
681            is a waste of resources to go drawing it. */
682         if (!(e->xbutton.button == 4 || e->xbutton.button == 5)) {
683             con = frame_context(client, e->xbutton.window);
684             con = mouse_button_frame_context(con, e->xbutton.button);
685             switch (con) {
686             case OB_FRAME_CONTEXT_MAXIMIZE:
687                 client->frame->max_press = (e->type == ButtonPress);
688                 framerender_frame(client->frame);
689                 break;
690             case OB_FRAME_CONTEXT_CLOSE:
691                 client->frame->close_press = (e->type == ButtonPress);
692                 framerender_frame(client->frame);
693                 break;
694             case OB_FRAME_CONTEXT_ICONIFY:
695                 client->frame->iconify_press = (e->type == ButtonPress);
696                 framerender_frame(client->frame);
697                 break;
698             case OB_FRAME_CONTEXT_ALLDESKTOPS:
699                 client->frame->desk_press = (e->type == ButtonPress);
700                 framerender_frame(client->frame);
701                 break; 
702             case OB_FRAME_CONTEXT_SHADE:
703                 client->frame->shade_press = (e->type == ButtonPress);
704                 framerender_frame(client->frame);
705                 break;
706             default:
707                 /* nothing changes with clicks for any other contexts */
708                 break;
709             }
710         }
711         break;
712     case LeaveNotify:
713         con = frame_context(client, e->xcrossing.window);
714         switch (con) {
715         case OB_FRAME_CONTEXT_MAXIMIZE:
716             client->frame->max_hover = FALSE;
717             frame_adjust_state(client->frame);
718             break;
719         case OB_FRAME_CONTEXT_ALLDESKTOPS:
720             client->frame->desk_hover = FALSE;
721             frame_adjust_state(client->frame);
722             break;
723         case OB_FRAME_CONTEXT_SHADE:
724             client->frame->shade_hover = FALSE;
725             frame_adjust_state(client->frame);
726             break;
727         case OB_FRAME_CONTEXT_ICONIFY:
728             client->frame->iconify_hover = FALSE;
729             frame_adjust_state(client->frame);
730             break;
731         case OB_FRAME_CONTEXT_CLOSE:
732             client->frame->close_hover = FALSE;
733             frame_adjust_state(client->frame);
734             break;
735         case OB_FRAME_CONTEXT_FRAME:
736             ob_debug_type(OB_DEBUG_FOCUS,
737                           "%sNotify mode %d detail %d on %lx\n",
738                           (e->type == EnterNotify ? "Enter" : "Leave"),
739                           e->xcrossing.mode,
740                           e->xcrossing.detail, (client?client->window:0));
741             if (keyboard_interactively_grabbed())
742                 break;
743             if (config_focus_follow && config_focus_delay &&
744                 /* leaveinferior events can happen when the mouse goes onto the
745                    window's border and then into the window before the delay
746                    is up */
747                 e->xcrossing.detail != NotifyInferior)
748             {
749                 ob_main_loop_timeout_remove_data(ob_main_loop,
750                                                  focus_delay_func,
751                                                  client, FALSE);
752             }
753             break;
754         default:
755             break;
756         }
757         break;
758     case EnterNotify:
759     {
760         gboolean nofocus = FALSE;
761
762         if (ignore_enter_focus) {
763             ignore_enter_focus--;
764             nofocus = TRUE;
765         }
766
767         con = frame_context(client, e->xcrossing.window);
768         switch (con) {
769         case OB_FRAME_CONTEXT_MAXIMIZE:
770             client->frame->max_hover = TRUE;
771             frame_adjust_state(client->frame);
772             break;
773         case OB_FRAME_CONTEXT_ALLDESKTOPS:
774             client->frame->desk_hover = TRUE;
775             frame_adjust_state(client->frame);
776             break;
777         case OB_FRAME_CONTEXT_SHADE:
778             client->frame->shade_hover = TRUE;
779             frame_adjust_state(client->frame);
780             break;
781         case OB_FRAME_CONTEXT_ICONIFY:
782             client->frame->iconify_hover = TRUE;
783             frame_adjust_state(client->frame);
784             break;
785         case OB_FRAME_CONTEXT_CLOSE:
786             client->frame->close_hover = TRUE;
787             frame_adjust_state(client->frame);
788             break;
789         case OB_FRAME_CONTEXT_FRAME:
790             if (keyboard_interactively_grabbed())
791                 break;
792             if (e->xcrossing.mode == NotifyGrab ||
793                 e->xcrossing.mode == NotifyUngrab)
794             {
795                 ob_debug_type(OB_DEBUG_FOCUS,
796                               "%sNotify mode %d detail %d on %lx IGNORED\n",
797                               (e->type == EnterNotify ? "Enter" : "Leave"),
798                               e->xcrossing.mode,
799                               e->xcrossing.detail, client?client->window:0);
800             } else {
801                 ob_debug_type(OB_DEBUG_FOCUS,
802                               "%sNotify mode %d detail %d on %lx, "
803                               "focusing window: %d\n",
804                               (e->type == EnterNotify ? "Enter" : "Leave"),
805                               e->xcrossing.mode,
806                               e->xcrossing.detail, (client?client->window:0),
807                               !nofocus);
808                 if (!nofocus && config_focus_follow)
809                     event_enter_client(client);
810             }
811             break;
812         default:
813             break;
814         }
815         break;
816     }
817     case ConfigureRequest:
818         /* compress these */
819         while (XCheckTypedWindowEvent(ob_display, client->window,
820                                       ConfigureRequest, &ce)) {
821             ++i;
822             /* XXX if this causes bad things.. we can compress config req's
823                with the same mask. */
824             e->xconfigurerequest.value_mask |=
825                 ce.xconfigurerequest.value_mask;
826             if (ce.xconfigurerequest.value_mask & CWX)
827                 e->xconfigurerequest.x = ce.xconfigurerequest.x;
828             if (ce.xconfigurerequest.value_mask & CWY)
829                 e->xconfigurerequest.y = ce.xconfigurerequest.y;
830             if (ce.xconfigurerequest.value_mask & CWWidth)
831                 e->xconfigurerequest.width = ce.xconfigurerequest.width;
832             if (ce.xconfigurerequest.value_mask & CWHeight)
833                 e->xconfigurerequest.height = ce.xconfigurerequest.height;
834             if (ce.xconfigurerequest.value_mask & CWBorderWidth)
835                 e->xconfigurerequest.border_width =
836                     ce.xconfigurerequest.border_width;
837             if (ce.xconfigurerequest.value_mask & CWStackMode)
838                 e->xconfigurerequest.detail = ce.xconfigurerequest.detail;
839         }
840
841         /* if we are iconic (or shaded (fvwm does this)) ignore the event */
842         if (client->iconic || client->shaded) return;
843
844         /* resize, then move, as specified in the EWMH section 7.7 */
845         if (e->xconfigurerequest.value_mask & (CWWidth | CWHeight |
846                                                CWX | CWY |
847                                                CWBorderWidth)) {
848             gint x, y, w, h;
849             ObCorner corner;
850
851             if (e->xconfigurerequest.value_mask & CWBorderWidth)
852                 client->border_width = e->xconfigurerequest.border_width;
853
854             x = (e->xconfigurerequest.value_mask & CWX) ?
855                 e->xconfigurerequest.x : client->area.x;
856             y = (e->xconfigurerequest.value_mask & CWY) ?
857                 e->xconfigurerequest.y : client->area.y;
858             w = (e->xconfigurerequest.value_mask & CWWidth) ?
859                 e->xconfigurerequest.width : client->area.width;
860             h = (e->xconfigurerequest.value_mask & CWHeight) ?
861                 e->xconfigurerequest.height : client->area.height;
862
863             {
864                 gint newx = x;
865                 gint newy = y;
866                 gint fw = w +
867                      client->frame->size.left + client->frame->size.right;
868                 gint fh = h +
869                      client->frame->size.top + client->frame->size.bottom;
870                 /* make this rude for size-only changes but not for position
871                    changes.. */
872                 gboolean moving = ((e->xconfigurerequest.value_mask & CWX) ||
873                                    (e->xconfigurerequest.value_mask & CWY));
874
875                 client_find_onscreen(client, &newx, &newy, fw, fh,
876                                      !moving);
877                 if (e->xconfigurerequest.value_mask & CWX)
878                     x = newx;
879                 if (e->xconfigurerequest.value_mask & CWY)
880                     y = newy;
881             }
882
883             switch (client->gravity) {
884             case NorthEastGravity:
885             case EastGravity:
886                 corner = OB_CORNER_TOPRIGHT;
887                 break;
888             case SouthWestGravity:
889             case SouthGravity:
890                 corner = OB_CORNER_BOTTOMLEFT;
891                 break;
892             case SouthEastGravity:
893                 corner = OB_CORNER_BOTTOMRIGHT;
894                 break;
895             default:     /* NorthWest, Static, etc */
896                 corner = OB_CORNER_TOPLEFT;
897             }
898
899             client_configure_full(client, corner, x, y, w, h, FALSE, TRUE,
900                                   TRUE);
901         }
902
903         if (e->xconfigurerequest.value_mask & CWStackMode) {
904             switch (e->xconfigurerequest.detail) {
905             case Below:
906             case BottomIf:
907                 /* Apps are so rude. And this is totally disconnected from
908                    activation/focus. Bleh. */
909                 /*client_lower(client);*/
910                 break;
911
912             case Above:
913             case TopIf:
914             default:
915                 /* Apps are so rude. And this is totally disconnected from
916                    activation/focus. Bleh. */
917                 /*client_raise(client);*/
918                 break;
919             }
920         }
921         break;
922     case UnmapNotify:
923         if (client->ignore_unmaps) {
924             client->ignore_unmaps--;
925             break;
926         }
927         ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
928                  "ignores left %d\n",
929                  client->window, e->xunmap.event, e->xunmap.from_configure,
930                  client->ignore_unmaps);
931         client_unmanage(client);
932         break;
933     case DestroyNotify:
934         ob_debug("DestroyNotify for window 0x%x\n", client->window);
935         client_unmanage(client);
936         break;
937     case ReparentNotify:
938         /* this is when the client is first taken captive in the frame */
939         if (e->xreparent.parent == client->frame->plate) break;
940
941         /*
942           This event is quite rare and is usually handled in unmapHandler.
943           However, if the window is unmapped when the reparent event occurs,
944           the window manager never sees it because an unmap event is not sent
945           to an already unmapped window.
946         */
947
948         /* we don't want the reparent event, put it back on the stack for the
949            X server to deal with after we unmanage the window */
950         XPutBackEvent(ob_display, e);
951      
952         ob_debug("ReparentNotify for window 0x%x\n", client->window);
953         client_unmanage(client);
954         break;
955     case MapRequest:
956         ob_debug("MapRequest for 0x%lx\n", client->window);
957         if (!client->iconic) break; /* this normally doesn't happen, but if it
958                                        does, we don't want it!
959                                        it can happen now when the window is on
960                                        another desktop, but we still don't
961                                        want it! */
962         client_activate(client, FALSE, TRUE);
963         break;
964     case ClientMessage:
965         /* validate cuz we query stuff off the client here */
966         if (!client_validate(client)) break;
967
968         if (e->xclient.format != 32) return;
969
970         msgtype = e->xclient.message_type;
971         if (msgtype == prop_atoms.wm_change_state) {
972             /* compress changes into a single change */
973             while (XCheckTypedWindowEvent(ob_display, client->window,
974                                           e->type, &ce)) {
975                 /* XXX: it would be nice to compress ALL messages of a
976                    type, not just messages in a row without other
977                    message types between. */
978                 if (ce.xclient.message_type != msgtype) {
979                     XPutBackEvent(ob_display, &ce);
980                     break;
981                 }
982                 e->xclient = ce.xclient;
983             }
984             client_set_wm_state(client, e->xclient.data.l[0]);
985         } else if (msgtype == prop_atoms.net_wm_desktop) {
986             /* compress changes into a single change */
987             while (XCheckTypedWindowEvent(ob_display, client->window,
988                                           e->type, &ce)) {
989                 /* XXX: it would be nice to compress ALL messages of a
990                    type, not just messages in a row without other
991                    message types between. */
992                 if (ce.xclient.message_type != msgtype) {
993                     XPutBackEvent(ob_display, &ce);
994                     break;
995                 }
996                 e->xclient = ce.xclient;
997             }
998             if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
999                 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
1000                 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
1001                                    FALSE);
1002         } else if (msgtype == prop_atoms.net_wm_state) {
1003             /* can't compress these */
1004             ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
1005                      (e->xclient.data.l[0] == 0 ? "Remove" :
1006                       e->xclient.data.l[0] == 1 ? "Add" :
1007                       e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1008                      e->xclient.data.l[1], e->xclient.data.l[2],
1009                      client->window);
1010             client_set_state(client, e->xclient.data.l[0],
1011                              e->xclient.data.l[1], e->xclient.data.l[2]);
1012         } else if (msgtype == prop_atoms.net_close_window) {
1013             ob_debug("net_close_window for 0x%lx\n", client->window);
1014             client_close(client);
1015         } else if (msgtype == prop_atoms.net_active_window) {
1016             ob_debug("net_active_window for 0x%lx source=%s\n",
1017                      client->window,
1018                      (e->xclient.data.l[0] == 0 ? "unknown" :
1019                       (e->xclient.data.l[0] == 1 ? "application" :
1020                        (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1021             /* XXX make use of data.l[2] ! */
1022             event_curtime = e->xclient.data.l[1];
1023             client_activate(client, FALSE,
1024                             (e->xclient.data.l[0] == 0 ||
1025                              e->xclient.data.l[0] == 2));
1026         } else if (msgtype == prop_atoms.net_wm_moveresize) {
1027             ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1028                      client->window, e->xclient.data.l[2]);
1029             if ((Atom)e->xclient.data.l[2] ==
1030                 prop_atoms.net_wm_moveresize_size_topleft ||
1031                 (Atom)e->xclient.data.l[2] ==
1032                 prop_atoms.net_wm_moveresize_size_top ||
1033                 (Atom)e->xclient.data.l[2] ==
1034                 prop_atoms.net_wm_moveresize_size_topright ||
1035                 (Atom)e->xclient.data.l[2] ==
1036                 prop_atoms.net_wm_moveresize_size_right ||
1037                 (Atom)e->xclient.data.l[2] ==
1038                 prop_atoms.net_wm_moveresize_size_right ||
1039                 (Atom)e->xclient.data.l[2] ==
1040                 prop_atoms.net_wm_moveresize_size_bottomright ||
1041                 (Atom)e->xclient.data.l[2] ==
1042                 prop_atoms.net_wm_moveresize_size_bottom ||
1043                 (Atom)e->xclient.data.l[2] ==
1044                 prop_atoms.net_wm_moveresize_size_bottomleft ||
1045                 (Atom)e->xclient.data.l[2] ==
1046                 prop_atoms.net_wm_moveresize_size_left ||
1047                 (Atom)e->xclient.data.l[2] ==
1048                 prop_atoms.net_wm_moveresize_move ||
1049                 (Atom)e->xclient.data.l[2] ==
1050                 prop_atoms.net_wm_moveresize_size_keyboard ||
1051                 (Atom)e->xclient.data.l[2] ==
1052                 prop_atoms.net_wm_moveresize_move_keyboard) {
1053
1054                 moveresize_start(client, e->xclient.data.l[0],
1055                                  e->xclient.data.l[1], e->xclient.data.l[3],
1056                                  e->xclient.data.l[2]);
1057             }
1058             else if ((Atom)e->xclient.data.l[2] ==
1059                      prop_atoms.net_wm_moveresize_cancel)
1060                 moveresize_end(TRUE);
1061         } else if (msgtype == prop_atoms.net_moveresize_window) {
1062             gint oldg = client->gravity;
1063             gint tmpg, x, y, w, h;
1064
1065             if (e->xclient.data.l[0] & 0xff)
1066                 tmpg = e->xclient.data.l[0] & 0xff;
1067             else
1068                 tmpg = oldg;
1069
1070             if (e->xclient.data.l[0] & 1 << 8)
1071                 x = e->xclient.data.l[1];
1072             else
1073                 x = client->area.x;
1074             if (e->xclient.data.l[0] & 1 << 9)
1075                 y = e->xclient.data.l[2];
1076             else
1077                 y = client->area.y;
1078             if (e->xclient.data.l[0] & 1 << 10)
1079                 w = e->xclient.data.l[3];
1080             else
1081                 w = client->area.width;
1082             if (e->xclient.data.l[0] & 1 << 11)
1083                 h = e->xclient.data.l[4];
1084             else
1085                 h = client->area.height;
1086             client->gravity = tmpg;
1087
1088             {
1089                 gint newx = x;
1090                 gint newy = y;
1091                 gint fw = w +
1092                      client->frame->size.left + client->frame->size.right;
1093                 gint fh = h +
1094                      client->frame->size.top + client->frame->size.bottom;
1095                 client_find_onscreen(client, &newx, &newy, fw, fh,
1096                                      client_normal(client));
1097                 if (e->xclient.data.l[0] & 1 << 8)
1098                     x = newx;
1099                 if (e->xclient.data.l[0] & 1 << 9)
1100                     y = newy;
1101             }
1102
1103             client_configure(client, OB_CORNER_TOPLEFT,
1104                              x, y, w, h, FALSE, TRUE);
1105
1106             client->gravity = oldg;
1107         }
1108         break;
1109     case PropertyNotify:
1110         /* validate cuz we query stuff off the client here */
1111         if (!client_validate(client)) break;
1112   
1113         /* compress changes to a single property into a single change */
1114         while (XCheckTypedWindowEvent(ob_display, client->window,
1115                                       e->type, &ce)) {
1116             Atom a, b;
1117
1118             /* XXX: it would be nice to compress ALL changes to a property,
1119                not just changes in a row without other props between. */
1120
1121             a = ce.xproperty.atom;
1122             b = e->xproperty.atom;
1123
1124             if (a == b)
1125                 continue;
1126             if ((a == prop_atoms.net_wm_name ||
1127                  a == prop_atoms.wm_name ||
1128                  a == prop_atoms.net_wm_icon_name ||
1129                  a == prop_atoms.wm_icon_name)
1130                 &&
1131                 (b == prop_atoms.net_wm_name ||
1132                  b == prop_atoms.wm_name ||
1133                  b == prop_atoms.net_wm_icon_name ||
1134                  b == prop_atoms.wm_icon_name)) {
1135                 continue;
1136             }
1137             if (a == prop_atoms.net_wm_icon &&
1138                 b == prop_atoms.net_wm_icon)
1139                 continue;
1140
1141             XPutBackEvent(ob_display, &ce);
1142             break;
1143         }
1144
1145         msgtype = e->xproperty.atom;
1146         if (msgtype == XA_WM_NORMAL_HINTS) {
1147             client_update_normal_hints(client);
1148             /* normal hints can make a window non-resizable */
1149             client_setup_decor_and_functions(client);
1150         } else if (msgtype == XA_WM_HINTS) {
1151             client_update_wmhints(client);
1152         } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1153             client_update_transient_for(client);
1154             client_get_type(client);
1155             /* type may have changed, so update the layer */
1156             client_calc_layer(client);
1157             client_setup_decor_and_functions(client);
1158         } else if (msgtype == prop_atoms.net_wm_name ||
1159                    msgtype == prop_atoms.wm_name ||
1160                    msgtype == prop_atoms.net_wm_icon_name ||
1161                    msgtype == prop_atoms.wm_icon_name) {
1162             client_update_title(client);
1163         } else if (msgtype == prop_atoms.wm_class) {
1164             client_update_class(client);
1165         } else if (msgtype == prop_atoms.wm_protocols) {
1166             client_update_protocols(client);
1167             client_setup_decor_and_functions(client);
1168         }
1169         else if (msgtype == prop_atoms.net_wm_strut) {
1170             client_update_strut(client);
1171         }
1172         else if (msgtype == prop_atoms.net_wm_icon) {
1173             client_update_icons(client);
1174         }
1175         else if (msgtype == prop_atoms.net_wm_user_time) {
1176             client_update_user_time(client);
1177         }
1178         else if (msgtype == prop_atoms.sm_client_id) {
1179             client_update_sm_client_id(client);
1180         }
1181     default:
1182         ;
1183 #ifdef SHAPE
1184         if (extensions_shape && e->type == extensions_shape_event_basep) {
1185             client->shaped = ((XShapeEvent*)e)->shaped;
1186             frame_adjust_shape(client->frame);
1187         }
1188 #endif
1189     }
1190 }
1191
1192 static void event_handle_dock(ObDock *s, XEvent *e)
1193 {
1194     switch (e->type) {
1195     case ButtonPress:
1196         if (e->xbutton.button == 1)
1197             stacking_raise(DOCK_AS_WINDOW(s));
1198         else if (e->xbutton.button == 2)
1199             stacking_lower(DOCK_AS_WINDOW(s));
1200         break;
1201     case EnterNotify:
1202         dock_hide(FALSE);
1203         break;
1204     case LeaveNotify:
1205         dock_hide(TRUE);
1206         break;
1207     }
1208 }
1209
1210 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1211 {
1212     switch (e->type) {
1213     case MotionNotify:
1214         dock_app_drag(app, &e->xmotion);
1215         break;
1216     case UnmapNotify:
1217         if (app->ignore_unmaps) {
1218             app->ignore_unmaps--;
1219             break;
1220         }
1221         dock_remove(app, TRUE);
1222         break;
1223     case DestroyNotify:
1224         dock_remove(app, FALSE);
1225         break;
1226     case ReparentNotify:
1227         dock_remove(app, FALSE);
1228         break;
1229     case ConfigureNotify:
1230         dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1231         break;
1232     }
1233 }
1234
1235 ObMenuFrame* find_active_menu()
1236 {
1237     GList *it;
1238     ObMenuFrame *ret = NULL;
1239
1240     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1241         ret = it->data;
1242         if (ret->selected)
1243             break;
1244         ret = NULL;
1245     }
1246     return ret;
1247 }
1248
1249 ObMenuFrame* find_active_or_last_menu()
1250 {
1251     ObMenuFrame *ret = NULL;
1252
1253     ret = find_active_menu();
1254     if (!ret && menu_frame_visible)
1255         ret = menu_frame_visible->data;
1256     return ret;
1257 }
1258
1259 static void event_handle_menu(XEvent *ev)
1260 {
1261     ObMenuFrame *f;
1262     ObMenuEntryFrame *e;
1263
1264     switch (ev->type) {
1265     case ButtonRelease:
1266         if (menu_can_hide) {
1267             if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1268                                             ev->xbutton.y_root)))
1269                 menu_entry_frame_execute(e, ev->xbutton.state,
1270                                          ev->xbutton.time);
1271             else
1272                 menu_frame_hide_all();
1273         }
1274         break;
1275     case EnterNotify:
1276         if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1277             if (e->ignore_enters)
1278                 --e->ignore_enters;
1279             else
1280                 menu_frame_select(e->frame, e);
1281         }
1282         break;
1283     case LeaveNotify:
1284         if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1285             (f = find_active_menu()) && f->selected == e &&
1286             e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1287         {
1288             menu_frame_select(e->frame, NULL);
1289         }
1290     case MotionNotify:   
1291         if ((e = menu_entry_frame_under(ev->xmotion.x_root,   
1292                                         ev->xmotion.y_root)))
1293             menu_frame_select(e->frame, e);   
1294         break;
1295     case KeyPress:
1296         if (ev->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
1297             menu_frame_hide_all();
1298         else if (ev->xkey.keycode == ob_keycode(OB_KEY_RETURN)) {
1299             ObMenuFrame *f;
1300             if ((f = find_active_menu()))
1301                 menu_entry_frame_execute(f->selected, ev->xkey.state,
1302                                          ev->xkey.time);
1303         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_LEFT)) {
1304             ObMenuFrame *f;
1305             if ((f = find_active_or_last_menu()) && f->parent)
1306                 menu_frame_select(f, NULL);
1307         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_RIGHT)) {
1308             ObMenuFrame *f;
1309             if ((f = find_active_or_last_menu()) && f->child)
1310                 menu_frame_select_next(f->child);
1311         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_UP)) {
1312             ObMenuFrame *f;
1313             if ((f = find_active_or_last_menu()))
1314                 menu_frame_select_previous(f);
1315         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_DOWN)) {
1316             ObMenuFrame *f;
1317             if ((f = find_active_or_last_menu()))
1318                 menu_frame_select_next(f);
1319         }
1320         break;
1321     }
1322 }
1323
1324 static gboolean menu_hide_delay_func(gpointer data)
1325 {
1326     menu_can_hide = TRUE;
1327     return FALSE; /* no repeat */
1328 }
1329
1330 static void focus_delay_dest(gpointer data)
1331 {
1332     g_free(data);
1333 }
1334
1335 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1336 {
1337     const ObFocusDelayData *f1 = d1;
1338     return f1->client == d2;
1339 }
1340
1341 static gboolean focus_delay_func(gpointer data)
1342 {
1343     ObFocusDelayData *d = data;
1344     Time old = event_curtime;
1345
1346     event_curtime = d->time;
1347     if (focus_client != d->client) {
1348         if (client_focus(d->client) && config_focus_raise)
1349             client_raise(d->client);
1350     }
1351     event_curtime = old;
1352     return FALSE; /* no repeat */
1353 }
1354
1355 static void focus_delay_client_dest(ObClient *client, gpointer data)
1356 {
1357     ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1358                                      client, FALSE);
1359 }
1360
1361 void event_halt_focus_delay()
1362 {
1363     ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1364 }
1365
1366 void event_ignore_queued_enters()
1367 {
1368     GSList *saved = NULL, *it;
1369     XEvent *e;
1370                 
1371     XSync(ob_display, FALSE);
1372
1373     /* count the events */
1374     while (TRUE) {
1375         e = g_new(XEvent, 1);
1376         if (XCheckTypedEvent(ob_display, EnterNotify, e)) {
1377             ObWindow *win;
1378             
1379             win = g_hash_table_lookup(window_map, &e->xany.window);
1380             if (win && WINDOW_IS_CLIENT(win))
1381                 ++ignore_enter_focus;
1382             
1383             saved = g_slist_append(saved, e);
1384         } else {
1385             g_free(e);
1386             break;
1387         }
1388     }
1389     /* put the events back */
1390     for (it = saved; it; it = g_slist_next(it)) {
1391         XPutBackEvent(ob_display, it->data);
1392         g_free(it->data);
1393     }
1394     g_slist_free(saved);
1395 }
1396
1397 gboolean event_time_after(Time t1, Time t2)
1398 {
1399     g_assert(t1 != CurrentTime);
1400     g_assert(t2 != CurrentTime);
1401
1402     /*
1403       Timestamp values wrap around (after about 49.7 days). The server, given
1404       its current time is represented by timestamp T, always interprets
1405       timestamps from clients by treating half of the timestamp space as being
1406       later in time than T.
1407       - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1408     */
1409
1410     /* TIME_HALF is half of the number space of a Time type variable */
1411 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1412
1413     if (t2 >= TIME_HALF)
1414         /* t2 is in the second half so t1 might wrap around and be smaller than
1415            t2 */
1416         return t1 >= t2 || t1 < (t2 + TIME_HALF);
1417     else
1418         /* t2 is in the first half so t1 has to come after it */
1419         return t1 >= t2 && t1 < (t2 + TIME_HALF);
1420 }