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