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