]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/event.c
ignore some focus events that shouldnt be reacted to
[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 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
376         /* Otherwise.. */
377         return FALSE;
378     }
379 }
380
381 static Bool look_for_focusin(Display *d, XEvent *e, XPointer arg)
382 {
383     return e->type == FocusIn && wanted_focusevent(e);
384 }
385
386 static gboolean event_ignore(XEvent *e, ObClient *client)
387 {
388     switch(e->type) {
389     case FocusIn:
390         if (!wanted_focusevent(e))
391             return TRUE;
392         break;
393     case FocusOut:
394         if (client == NULL)
395             return TRUE;
396         if (!wanted_focusevent(e))
397             return TRUE;
398         break;
399     }
400     return FALSE;
401 }
402
403 static void event_process(const XEvent *ec, gpointer data)
404 {
405     Window window;
406     ObGroup *group = NULL;
407     ObClient *client = NULL;
408     ObDock *dock = NULL;
409     ObDockApp *dockapp = NULL;
410     ObWindow *obwin = NULL;
411     XEvent ee, *e;
412     ObEventData *ed = data;
413
414     /* make a copy we can mangle */
415     ee = *ec;
416     e = &ee;
417
418     window = event_get_window(e);
419     if (!(e->type == PropertyNotify &&
420           (group = g_hash_table_lookup(group_map, &window))))
421         if ((obwin = g_hash_table_lookup(window_map, &window))) {
422             switch (obwin->type) {
423             case Window_Dock:
424                 dock = WINDOW_AS_DOCK(obwin);
425                 break;
426             case Window_DockApp:
427                 dockapp = WINDOW_AS_DOCKAPP(obwin);
428                 break;
429             case Window_Client:
430                 client = WINDOW_AS_CLIENT(obwin);
431                 break;
432             case Window_Menu:
433             case Window_Internal:
434                 /* not to be used for events */
435                 g_assert_not_reached();
436                 break;
437             }
438         }
439
440     event_set_curtime(e);
441     event_hack_mods(e);
442     if (event_ignore(e, client)) {
443         if (ed)
444             ed->ignored = TRUE;
445         return;
446     } else if (ed)
447             ed->ignored = FALSE;
448
449     /* deal with it in the kernel */
450
451     if (menu_frame_visible &&
452         (e->type == EnterNotify || e->type == LeaveNotify))
453     {
454         /* crossing events for menu */
455         event_handle_menu(e);
456     } else if (e->type == FocusIn) {
457         if (e->xfocus.detail == NotifyPointerRoot ||
458             e->xfocus.detail == NotifyDetailNone) {
459             ob_debug_type(OB_DEBUG_FOCUS, "Focus went to root\n");
460             /* Focus has been reverted to the root window or nothing
461                FocusOut events come after UnmapNotify, so we don't need to
462                worry about focusing an invalid window
463              */
464             focus_fallback(TRUE);
465         } else if (e->xfocus.detail == NotifyInferior) {
466             ob_debug_type(OB_DEBUG_FOCUS, "Focus went to parent\n");
467             /* Focus has been reverted to parent, which is our frame window,
468                or the root window
469                FocusOut events come after UnmapNotify, so we don't need to
470                worry about focusing an invalid window
471             */
472             focus_fallback(TRUE);
473         } else if (client && client != focus_client) {
474             frame_adjust_focus(client->frame, TRUE);
475             focus_set_client(client);
476             client_calc_layer(client);
477         }
478     } else if (e->type == FocusOut) {
479         gboolean nomove = FALSE;
480         XEvent ce;
481
482         ob_debug_type(OB_DEBUG_FOCUS, "FocusOut Event\n");
483
484         /* Look for the followup FocusIn */
485         if (!XCheckIfEvent(ob_display, &ce, look_for_focusin, NULL)) {
486             /* There is no FocusIn, this means focus went to a window that
487                is not being managed, or a window on another screen. */
488             ob_debug_type(OB_DEBUG_FOCUS, "Focus went to a black hole !\n");
489             /* nothing is focused */
490             focus_set_client(NULL);
491         } else if (ce.xany.window == e->xany.window) {
492             /* If focus didn't actually move anywhere, there is nothing to do*/
493             nomove = TRUE;
494         } else {
495             /* Focus did move, so process the FocusIn event */
496             ObEventData ed = { .ignored = FALSE };
497             event_process(&ce, &ed);
498             if (ed.ignored) {
499                 /* The FocusIn was ignored, this means it was on a window
500                    that isn't a client. */
501                 ob_debug_type(OB_DEBUG_FOCUS,
502                               "Focus went to an unmanaged window 0x%x !\n",
503                               ce.xfocus.window);
504                 focus_fallback(TRUE);
505             }
506         }
507
508         if (client && !nomove) {
509             frame_adjust_focus(client->frame, FALSE);
510             /* focus_set_client has already been called for sure */
511             client_calc_layer(client);
512         }
513     } else if (group)
514         event_handle_group(group, e);
515     else if (client)
516         event_handle_client(client, e);
517     else if (dockapp)
518         event_handle_dockapp(dockapp, e);
519     else if (dock)
520         event_handle_dock(dock, e);
521     else if (window == RootWindow(ob_display, ob_screen))
522         event_handle_root(e);
523     else if (e->type == MapRequest)
524         client_manage(window);
525     else if (e->type == ConfigureRequest) {
526         /* unhandled configure requests must be used to configure the
527            window directly */
528         XWindowChanges xwc;
529
530         xwc.x = e->xconfigurerequest.x;
531         xwc.y = e->xconfigurerequest.y;
532         xwc.width = e->xconfigurerequest.width;
533         xwc.height = e->xconfigurerequest.height;
534         xwc.border_width = e->xconfigurerequest.border_width;
535         xwc.sibling = e->xconfigurerequest.above;
536         xwc.stack_mode = e->xconfigurerequest.detail;
537        
538         /* we are not to be held responsible if someone sends us an
539            invalid request! */
540         xerror_set_ignore(TRUE);
541         XConfigureWindow(ob_display, window,
542                          e->xconfigurerequest.value_mask, &xwc);
543         xerror_set_ignore(FALSE);
544     }
545
546     /* user input (action-bound) events */
547     if (e->type == ButtonPress || e->type == ButtonRelease ||
548         e->type == MotionNotify || e->type == KeyPress ||
549         e->type == KeyRelease)
550     {
551         if (menu_frame_visible)
552             event_handle_menu(e);
553         else {
554             if (!keyboard_process_interactive_grab(e, &client)) {
555                 if (moveresize_in_progress) {
556                     moveresize_event(e);
557
558                     /* make further actions work on the client being
559                        moved/resized */
560                     client = moveresize_client;
561                 }
562
563                 menu_can_hide = FALSE;
564                 ob_main_loop_timeout_add(ob_main_loop,
565                                          config_menu_hide_delay * 1000,
566                                          menu_hide_delay_func,
567                                          NULL, g_direct_equal, NULL);
568
569                 if (e->type == ButtonPress || e->type == ButtonRelease ||
570                     e->type == MotionNotify) {
571                     mouse_event(client, e);
572                 } else if (e->type == KeyPress) {
573                     keyboard_event((focus_cycle_target ? focus_cycle_target :
574                                     client), e);
575                 }
576             }
577         }
578     }
579     /* if something happens and it's not from an XEvent, then we don't know
580        the time */
581     event_curtime = CurrentTime;
582 }
583
584 static void event_handle_root(XEvent *e)
585 {
586     Atom msgtype;
587      
588     switch(e->type) {
589     case SelectionClear:
590         ob_debug("Another WM has requested to replace us. Exiting.\n");
591         ob_exit_replace();
592         break;
593
594     case ClientMessage:
595         if (e->xclient.format != 32) break;
596
597         msgtype = e->xclient.message_type;
598         if (msgtype == prop_atoms.net_current_desktop) {
599             guint d = e->xclient.data.l[0];
600             if (d < screen_num_desktops) {
601                 event_curtime = e->xclient.data.l[1];
602                 ob_debug("SWITCH DESKTOP TIME: %d\n", event_curtime);
603                 screen_set_desktop(d);
604             }
605         } else if (msgtype == prop_atoms.net_number_of_desktops) {
606             guint d = e->xclient.data.l[0];
607             if (d > 0)
608                 screen_set_num_desktops(d);
609         } else if (msgtype == prop_atoms.net_showing_desktop) {
610             screen_show_desktop(e->xclient.data.l[0] != 0);
611         } else if (msgtype == prop_atoms.ob_control) {
612             if (e->xclient.data.l[0] == 1)
613                 ob_reconfigure();
614             else if (e->xclient.data.l[0] == 2)
615                 ob_restart();
616         }
617         break;
618     case PropertyNotify:
619         if (e->xproperty.atom == prop_atoms.net_desktop_names)
620             screen_update_desktop_names();
621         else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
622             screen_update_layout();
623         break;
624     case ConfigureNotify:
625 #ifdef XRANDR
626         XRRUpdateConfiguration(e);
627 #endif
628         screen_resize();
629         break;
630     default:
631         ;
632     }
633 }
634
635 static void event_handle_group(ObGroup *group, XEvent *e)
636 {
637     GSList *it;
638
639     g_assert(e->type == PropertyNotify);
640
641     for (it = group->members; it; it = g_slist_next(it))
642         event_handle_client(it->data, e);
643 }
644
645 void event_enter_client(ObClient *client)
646 {
647     g_assert(config_focus_follow);
648
649     if (client_normal(client) && client_can_focus(client)) {
650         if (config_focus_delay) {
651             ObFocusDelayData *data;
652
653             ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
654
655             data = g_new(ObFocusDelayData, 1);
656             data->client = client;
657             data->time = event_curtime;
658
659             ob_main_loop_timeout_add(ob_main_loop,
660                                      config_focus_delay,
661                                      focus_delay_func,
662                                      data, focus_delay_cmp, focus_delay_dest);
663         } else {
664             ObFocusDelayData data;
665             data.client = client;
666             data.time = event_curtime;
667             focus_delay_func(&data);
668         }
669     }
670 }
671
672 static void event_handle_client(ObClient *client, XEvent *e)
673 {
674     XEvent ce;
675     Atom msgtype;
676     gint i=0;
677     ObFrameContext con;
678      
679     switch (e->type) {
680     case VisibilityNotify:
681         client->frame->obscured = e->xvisibility.state != VisibilityUnobscured;
682         break;
683     case ButtonPress:
684     case ButtonRelease:
685         /* Wheel buttons don't draw because they are an instant click, so it
686            is a waste of resources to go drawing it. */
687         if (!(e->xbutton.button == 4 || e->xbutton.button == 5)) {
688             con = frame_context(client, e->xbutton.window);
689             con = mouse_button_frame_context(con, e->xbutton.button);
690             switch (con) {
691             case OB_FRAME_CONTEXT_MAXIMIZE:
692                 client->frame->max_press = (e->type == ButtonPress);
693                 framerender_frame(client->frame);
694                 break;
695             case OB_FRAME_CONTEXT_CLOSE:
696                 client->frame->close_press = (e->type == ButtonPress);
697                 framerender_frame(client->frame);
698                 break;
699             case OB_FRAME_CONTEXT_ICONIFY:
700                 client->frame->iconify_press = (e->type == ButtonPress);
701                 framerender_frame(client->frame);
702                 break;
703             case OB_FRAME_CONTEXT_ALLDESKTOPS:
704                 client->frame->desk_press = (e->type == ButtonPress);
705                 framerender_frame(client->frame);
706                 break; 
707             case OB_FRAME_CONTEXT_SHADE:
708                 client->frame->shade_press = (e->type == ButtonPress);
709                 framerender_frame(client->frame);
710                 break;
711             default:
712                 /* nothing changes with clicks for any other contexts */
713                 break;
714             }
715         }
716         break;
717     case LeaveNotify:
718         con = frame_context(client, e->xcrossing.window);
719         switch (con) {
720         case OB_FRAME_CONTEXT_MAXIMIZE:
721             client->frame->max_hover = FALSE;
722             frame_adjust_state(client->frame);
723             break;
724         case OB_FRAME_CONTEXT_ALLDESKTOPS:
725             client->frame->desk_hover = FALSE;
726             frame_adjust_state(client->frame);
727             break;
728         case OB_FRAME_CONTEXT_SHADE:
729             client->frame->shade_hover = FALSE;
730             frame_adjust_state(client->frame);
731             break;
732         case OB_FRAME_CONTEXT_ICONIFY:
733             client->frame->iconify_hover = FALSE;
734             frame_adjust_state(client->frame);
735             break;
736         case OB_FRAME_CONTEXT_CLOSE:
737             client->frame->close_hover = FALSE;
738             frame_adjust_state(client->frame);
739             break;
740         case OB_FRAME_CONTEXT_FRAME:
741             if (keyboard_interactively_grabbed())
742                 break;
743             if (config_focus_follow && config_focus_delay)
744                 ob_main_loop_timeout_remove_data(ob_main_loop,
745                                                  focus_delay_func,
746                                                  client, FALSE);
747             break;
748         default:
749             break;
750         }
751         break;
752     case EnterNotify:
753     {
754         gboolean nofocus = FALSE;
755
756         if (ignore_enter_focus) {
757             ignore_enter_focus--;
758             nofocus = TRUE;
759         }
760
761         con = frame_context(client, e->xcrossing.window);
762         switch (con) {
763         case OB_FRAME_CONTEXT_MAXIMIZE:
764             client->frame->max_hover = TRUE;
765             frame_adjust_state(client->frame);
766             break;
767         case OB_FRAME_CONTEXT_ALLDESKTOPS:
768             client->frame->desk_hover = TRUE;
769             frame_adjust_state(client->frame);
770             break;
771         case OB_FRAME_CONTEXT_SHADE:
772             client->frame->shade_hover = TRUE;
773             frame_adjust_state(client->frame);
774             break;
775         case OB_FRAME_CONTEXT_ICONIFY:
776             client->frame->iconify_hover = TRUE;
777             frame_adjust_state(client->frame);
778             break;
779         case OB_FRAME_CONTEXT_CLOSE:
780             client->frame->close_hover = TRUE;
781             frame_adjust_state(client->frame);
782             break;
783         case OB_FRAME_CONTEXT_FRAME:
784             if (keyboard_interactively_grabbed())
785                 break;
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 MotionNotify:   
1285         if ((e = menu_entry_frame_under(ev->xmotion.x_root,   
1286                                         ev->xmotion.y_root)))
1287             menu_frame_select(e->frame, e);   
1288         break;
1289     case KeyPress:
1290         if (ev->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
1291             menu_frame_hide_all();
1292         else if (ev->xkey.keycode == ob_keycode(OB_KEY_RETURN)) {
1293             ObMenuFrame *f;
1294             if ((f = find_active_menu()))
1295                 menu_entry_frame_execute(f->selected, ev->xkey.state,
1296                                          ev->xkey.time);
1297         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_LEFT)) {
1298             ObMenuFrame *f;
1299             if ((f = find_active_or_last_menu()) && f->parent)
1300                 menu_frame_select(f, NULL);
1301         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_RIGHT)) {
1302             ObMenuFrame *f;
1303             if ((f = find_active_or_last_menu()) && f->child)
1304                 menu_frame_select_next(f->child);
1305         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_UP)) {
1306             ObMenuFrame *f;
1307             if ((f = find_active_or_last_menu()))
1308                 menu_frame_select_previous(f);
1309         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_DOWN)) {
1310             ObMenuFrame *f;
1311             if ((f = find_active_or_last_menu()))
1312                 menu_frame_select_next(f);
1313         }
1314         break;
1315     }
1316 }
1317
1318 static gboolean menu_hide_delay_func(gpointer data)
1319 {
1320     menu_can_hide = TRUE;
1321     return FALSE; /* no repeat */
1322 }
1323
1324 static void focus_delay_dest(gpointer data)
1325 {
1326     g_free(data);
1327 }
1328
1329 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1330 {
1331     const ObFocusDelayData *f1 = d1;
1332     return f1->client == d2;
1333 }
1334
1335 static gboolean focus_delay_func(gpointer data)
1336 {
1337     ObFocusDelayData *d = data;
1338     Time old = event_curtime;
1339
1340     event_curtime = d->time;
1341     if (focus_client != d->client) {
1342         if (client_focus(d->client) && config_focus_raise)
1343             client_raise(d->client);
1344     }
1345     event_curtime = old;
1346     return FALSE; /* no repeat */
1347 }
1348
1349 static void focus_delay_client_dest(ObClient *client, gpointer data)
1350 {
1351     ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1352                                      client, FALSE);
1353 }
1354
1355 void event_halt_focus_delay()
1356 {
1357     ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1358 }
1359
1360 void event_ignore_queued_enters()
1361 {
1362     GSList *saved = NULL, *it;
1363     XEvent *e;
1364                 
1365     XSync(ob_display, FALSE);
1366
1367     /* count the events */
1368     while (TRUE) {
1369         e = g_new(XEvent, 1);
1370         if (XCheckTypedEvent(ob_display, EnterNotify, e)) {
1371             ObWindow *win;
1372             
1373             win = g_hash_table_lookup(window_map, &e->xany.window);
1374             if (win && WINDOW_IS_CLIENT(win))
1375                 ++ignore_enter_focus;
1376             
1377             saved = g_slist_append(saved, e);
1378         } else {
1379             g_free(e);
1380             break;
1381         }
1382     }
1383     /* put the events back */
1384     for (it = saved; it; it = g_slist_next(it)) {
1385         XPutBackEvent(ob_display, it->data);
1386         g_free(it->data);
1387     }
1388     g_slist_free(saved);
1389 }
1390
1391 gboolean event_time_after(Time t1, Time t2)
1392 {
1393     g_assert(t1 != CurrentTime);
1394     g_assert(t2 != CurrentTime);
1395
1396     /*
1397       Timestamp values wrap around (after about 49.7 days). The server, given
1398       its current time is represented by timestamp T, always interprets
1399       timestamps from clients by treating half of the timestamp space as being
1400       later in time than T.
1401       - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
1402     */
1403
1404     /* TIME_HALF is half of the number space of a Time type variable */
1405 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
1406
1407     if (t2 >= TIME_HALF)
1408         /* t2 is in the second half so t1 might wrap around and be smaller than
1409            t2 */
1410         return t1 >= t2 || t1 < (t2 + TIME_HALF);
1411     else
1412         /* t2 is in the first half so t1 has to come after it */
1413         return t1 >= t2 && t1 < (t2 + TIME_HALF);
1414 }