]> icculus.org git repositories - dana/openbox.git/blob - openbox/event.c
continue commit 2b80e4e8ef56bb4fba614139601e750344418e5b - remove debug code, and...
[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     gulong serial;
78 } ObFocusDelayData;
79
80 typedef struct
81 {
82     gulong start; /* inclusive */
83     gulong end;   /* inclusive */
84 } ObSerialRange;
85
86 static void event_process(const XEvent *e, gpointer data);
87 static void event_handle_root(XEvent *e);
88 static gboolean event_handle_menu_keyboard(XEvent *e);
89 static gboolean event_handle_menu(XEvent *e);
90 static void event_handle_dock(ObDock *s, XEvent *e);
91 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
92 static void event_handle_client(ObClient *c, XEvent *e);
93 static void event_handle_user_input(ObClient *client, XEvent *e);
94 static gboolean is_enter_focus_event_ignored(XEvent *e);
95 static void event_ignore_enter_range(gulong start, gulong end);
96
97 static void focus_delay_dest(gpointer data);
98 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
99 static gboolean focus_delay_func(gpointer data);
100 static void focus_delay_client_dest(ObClient *client, gpointer data);
101
102 Time event_curtime = CurrentTime;
103 Time event_last_user_time = CurrentTime;
104 /*! The serial of the current X event */
105 gulong event_curserial;
106
107 static gboolean focus_left_screen = FALSE;
108 /*! A list of ObSerialRanges which are to be ignored for mouse enter events */
109 static GSList *ignore_serials = NULL;
110
111 #ifdef USE_SM
112 static void ice_handler(gint fd, gpointer conn)
113 {
114     Bool b;
115     IceProcessMessages(conn, NULL, &b);
116 }
117
118 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
119                       IcePointer *watch_data)
120 {
121     static gint fd = -1;
122
123     if (opening) {
124         fd = IceConnectionNumber(conn);
125         ob_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
126     } else {
127         ob_main_loop_fd_remove(ob_main_loop, fd);
128         fd = -1;
129     }
130 }
131 #endif
132
133 void event_startup(gboolean reconfig)
134 {
135     if (reconfig) return;
136
137     ob_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
138
139 #ifdef USE_SM
140     IceAddConnectionWatch(ice_watch, NULL);
141 #endif
142
143     client_add_destroy_notify(focus_delay_client_dest, NULL);
144 }
145
146 void event_shutdown(gboolean reconfig)
147 {
148     if (reconfig) return;
149
150 #ifdef USE_SM
151     IceRemoveConnectionWatch(ice_watch, NULL);
152 #endif
153
154     client_remove_destroy_notify(focus_delay_client_dest);
155 }
156
157 static Window event_get_window(XEvent *e)
158 {
159     Window window;
160
161     /* pick a window */
162     switch (e->type) {
163     case SelectionClear:
164         window = RootWindow(ob_display, ob_screen);
165         break;
166     case MapRequest:
167         window = e->xmap.window;
168         break;
169     case UnmapNotify:
170         window = e->xunmap.window;
171         break;
172     case DestroyNotify:
173         window = e->xdestroywindow.window;
174         break;
175     case ConfigureRequest:
176         window = e->xconfigurerequest.window;
177         break;
178     case ConfigureNotify:
179         window = e->xconfigure.window;
180         break;
181     default:
182 #ifdef XKB
183         if (extensions_xkb && e->type == extensions_xkb_event_basep) {
184             switch (((XkbAnyEvent*)e)->xkb_type) {
185             case XkbBellNotify:
186                 window = ((XkbBellNotifyEvent*)e)->window;
187             default:
188                 window = None;
189             }
190         } else
191 #endif
192 #ifdef SYNC
193         if (extensions_sync &&
194             e->type == extensions_sync_event_basep + XSyncAlarmNotify)
195         {
196             window = None;
197         } else
198 #endif
199             window = e->xany.window;
200     }
201     return window;
202 }
203
204 static void event_set_curtime(XEvent *e)
205 {
206     Time t = CurrentTime;
207
208     /* grab the lasttime and hack up the state */
209     switch (e->type) {
210     case ButtonPress:
211     case ButtonRelease:
212         t = e->xbutton.time;
213         break;
214     case KeyPress:
215         t = e->xkey.time;
216         break;
217     case KeyRelease:
218         t = e->xkey.time;
219         break;
220     case MotionNotify:
221         t = e->xmotion.time;
222         break;
223     case PropertyNotify:
224         t = e->xproperty.time;
225         break;
226     case EnterNotify:
227     case LeaveNotify:
228         t = e->xcrossing.time;
229         break;
230     default:
231 #ifdef SYNC
232         if (extensions_sync &&
233             e->type == extensions_sync_event_basep + XSyncAlarmNotify)
234         {
235             t = ((XSyncAlarmNotifyEvent*)e)->time;
236         }
237 #endif
238         /* if more event types are anticipated, get their timestamp
239            explicitly */
240         break;
241     }
242
243     /* watch that if we get an event earlier than the last specified user_time,
244        which can happen if the clock goes backwards, we erase the last
245        specified user_time */
246     if (t && event_last_user_time && event_time_after(event_last_user_time, t))
247         event_last_user_time = CurrentTime;
248
249     event_curtime = t;
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             ObWindow *w;
709
710             if ((w = g_hash_table_lookup(window_map, &e->xbutton.subwindow)) &&
711                 WINDOW_IS_INTERNAL(w))
712             {
713                 event_handle_user_input(client, e);
714             }
715         }
716     }
717     else if (e->type == KeyPress || e->type == KeyRelease ||
718              e->type == MotionNotify)
719         event_handle_user_input(client, e);
720
721     /* if something happens and it's not from an XEvent, then we don't know
722        the time */
723     event_curtime = CurrentTime;
724     event_curserial = 0;
725 }
726
727 static void event_handle_root(XEvent *e)
728 {
729     Atom msgtype;
730
731     switch(e->type) {
732     case SelectionClear:
733         ob_debug("Another WM has requested to replace us. Exiting.\n");
734         ob_exit_replace();
735         break;
736
737     case ClientMessage:
738         if (e->xclient.format != 32) break;
739
740         msgtype = e->xclient.message_type;
741         if (msgtype == prop_atoms.net_current_desktop) {
742             guint d = e->xclient.data.l[0];
743             if (d < screen_num_desktops) {
744                 event_curtime = e->xclient.data.l[1];
745                 if (event_curtime == 0)
746                     ob_debug_type(OB_DEBUG_APP_BUGS,
747                                   "_NET_CURRENT_DESKTOP message is missing "
748                                   "a timestamp\n");
749                 screen_set_desktop(d, TRUE);
750             }
751         } else if (msgtype == prop_atoms.net_number_of_desktops) {
752             guint d = e->xclient.data.l[0];
753             if (d > 0 && d <= 1000)
754                 screen_set_num_desktops(d);
755         } else if (msgtype == prop_atoms.net_showing_desktop) {
756             screen_show_desktop(e->xclient.data.l[0] != 0, NULL);
757         } else if (msgtype == prop_atoms.ob_control) {
758             ob_debug("OB_CONTROL: %d\n", e->xclient.data.l[0]);
759             if (e->xclient.data.l[0] == 1)
760                 ob_reconfigure();
761             else if (e->xclient.data.l[0] == 2)
762                 ob_restart();
763             else if (e->xclient.data.l[0] == 3)
764                 ob_exit(0);
765         }
766         break;
767     case PropertyNotify:
768         if (e->xproperty.atom == prop_atoms.net_desktop_names) {
769             ob_debug("UPDATE DESKTOP NAMES\n");
770             screen_update_desktop_names();
771         }
772         else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
773             screen_update_layout();
774         break;
775     case ConfigureNotify:
776 #ifdef XRANDR
777         XRRUpdateConfiguration(e);
778 #endif
779         screen_resize();
780         break;
781     default:
782         ;
783     }
784 }
785
786 void event_enter_client(ObClient *client)
787 {
788     g_assert(config_focus_follow);
789
790     if (client_enter_focusable(client) && client_can_focus(client)) {
791         if (config_focus_delay) {
792             ObFocusDelayData *data;
793
794             ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
795
796             data = g_new(ObFocusDelayData, 1);
797             data->client = client;
798             data->time = event_curtime;
799             data->serial = event_curserial;
800
801             ob_main_loop_timeout_add(ob_main_loop,
802                                      config_focus_delay * 1000,
803                                      focus_delay_func,
804                                      data, focus_delay_cmp, focus_delay_dest);
805         } else {
806             ObFocusDelayData data;
807             data.client = client;
808             data.time = event_curtime;
809             data.serial = event_curserial;
810             focus_delay_func(&data);
811         }
812     }
813 }
814
815 static void event_handle_client(ObClient *client, XEvent *e)
816 {
817     XEvent ce;
818     Atom msgtype;
819     ObFrameContext con;
820     static gint px = -1, py = -1;
821     static guint pb = 0;
822
823     switch (e->type) {
824     case ButtonPress:
825         /* save where the press occured for the first button pressed */
826         if (!pb) {
827             pb = e->xbutton.button;
828             px = e->xbutton.x;
829             py = e->xbutton.y;
830         }
831     case ButtonRelease:
832         /* Wheel buttons don't draw because they are an instant click, so it
833            is a waste of resources to go drawing it.
834            if the user is doing an intereactive thing, or has a menu open then
835            the mouse is grabbed (possibly) and if we get these events we don't
836            want to deal with them
837         */
838         if (!(e->xbutton.button == 4 || e->xbutton.button == 5) &&
839             !grab_on_keyboard())
840         {
841             /* use where the press occured */
842             con = frame_context(client, e->xbutton.window, px, py);
843             con = mouse_button_frame_context(con, e->xbutton.button,
844                                              e->xbutton.state);
845
846             if (e->type == ButtonRelease && e->xbutton.button == pb)
847                 pb = 0, px = py = -1;
848
849             switch (con) {
850             case OB_FRAME_CONTEXT_MAXIMIZE:
851                 client->frame->max_press = (e->type == ButtonPress);
852                 frame_adjust_state(client->frame);
853                 break;
854             case OB_FRAME_CONTEXT_CLOSE:
855                 client->frame->close_press = (e->type == ButtonPress);
856                 frame_adjust_state(client->frame);
857                 break;
858             case OB_FRAME_CONTEXT_ICONIFY:
859                 client->frame->iconify_press = (e->type == ButtonPress);
860                 frame_adjust_state(client->frame);
861                 break;
862             case OB_FRAME_CONTEXT_ALLDESKTOPS:
863                 client->frame->desk_press = (e->type == ButtonPress);
864                 frame_adjust_state(client->frame);
865                 break;
866             case OB_FRAME_CONTEXT_SHADE:
867                 client->frame->shade_press = (e->type == ButtonPress);
868                 frame_adjust_state(client->frame);
869                 break;
870             default:
871                 /* nothing changes with clicks for any other contexts */
872                 break;
873             }
874         }
875         break;
876     case MotionNotify:
877         /* when there is a grab on the pointer, we won't get enter/leave
878            notifies, but we still get motion events */
879         if (grab_on_pointer()) break;
880
881         con = frame_context(client, e->xmotion.window,
882                             e->xmotion.x, e->xmotion.y);
883         switch (con) {
884         case OB_FRAME_CONTEXT_TITLEBAR:
885         case OB_FRAME_CONTEXT_TLCORNER:
886         case OB_FRAME_CONTEXT_TRCORNER:
887             /* we've left the button area inside the titlebar */
888             if (client->frame->max_hover || client->frame->desk_hover ||
889                 client->frame->shade_hover || client->frame->iconify_hover ||
890                 client->frame->close_hover)
891             {
892                 client->frame->max_hover = FALSE;
893                 client->frame->desk_hover = FALSE;
894                 client->frame->shade_hover = FALSE;
895                 client->frame->iconify_hover = FALSE;
896                 client->frame->close_hover = FALSE;
897                 frame_adjust_state(client->frame);
898             }
899             break;
900         case OB_FRAME_CONTEXT_MAXIMIZE:
901             if (!client->frame->max_hover) {
902                 client->frame->max_hover = TRUE;
903                 frame_adjust_state(client->frame);
904             }
905             break;
906         case OB_FRAME_CONTEXT_ALLDESKTOPS:
907             if (!client->frame->desk_hover) {
908                 client->frame->desk_hover = TRUE;
909                 frame_adjust_state(client->frame);
910             }
911             break;
912         case OB_FRAME_CONTEXT_SHADE:
913             if (!client->frame->shade_hover) {
914                 client->frame->shade_hover = TRUE;
915                 frame_adjust_state(client->frame);
916             }
917             break;
918         case OB_FRAME_CONTEXT_ICONIFY:
919             if (!client->frame->iconify_hover) {
920                 client->frame->iconify_hover = TRUE;
921                 frame_adjust_state(client->frame);
922             }
923             break;
924         case OB_FRAME_CONTEXT_CLOSE:
925             if (!client->frame->close_hover) {
926                 client->frame->close_hover = TRUE;
927                 frame_adjust_state(client->frame);
928             }
929             break;
930         default:
931             break;
932         }
933         break;
934     case LeaveNotify:
935         con = frame_context(client, e->xcrossing.window,
936                             e->xcrossing.x, e->xcrossing.y);
937         switch (con) {
938         case OB_FRAME_CONTEXT_TITLEBAR:
939         case OB_FRAME_CONTEXT_TLCORNER:
940         case OB_FRAME_CONTEXT_TRCORNER:
941             /* we've left the button area inside the titlebar */
942             if (client->frame->max_hover || client->frame->desk_hover ||
943                 client->frame->shade_hover || client->frame->iconify_hover ||
944                 client->frame->close_hover)
945             {
946                 client->frame->max_hover = FALSE;
947                 client->frame->desk_hover = FALSE;
948                 client->frame->shade_hover = FALSE;
949                 client->frame->iconify_hover = FALSE;
950                 client->frame->close_hover = FALSE;
951                 frame_adjust_state(client->frame);
952             }
953             break;
954         case OB_FRAME_CONTEXT_MAXIMIZE:
955             client->frame->max_hover = FALSE;
956             frame_adjust_state(client->frame);
957             break;
958         case OB_FRAME_CONTEXT_ALLDESKTOPS:
959             client->frame->desk_hover = FALSE;
960             frame_adjust_state(client->frame);
961             break;
962         case OB_FRAME_CONTEXT_SHADE:
963             client->frame->shade_hover = FALSE;
964             frame_adjust_state(client->frame);
965             break;
966         case OB_FRAME_CONTEXT_ICONIFY:
967             client->frame->iconify_hover = FALSE;
968             frame_adjust_state(client->frame);
969             break;
970         case OB_FRAME_CONTEXT_CLOSE:
971             client->frame->close_hover = FALSE;
972             frame_adjust_state(client->frame);
973             break;
974         case OB_FRAME_CONTEXT_FRAME:
975             /* When the mouse leaves an animating window, don't use the
976                corresponding enter events. Pretend like the animating window
977                doesn't even exist..! */
978             if (frame_iconify_animating(client->frame))
979                 event_end_ignore_all_enters(event_start_ignore_all_enters());
980
981             ob_debug_type(OB_DEBUG_FOCUS,
982                           "%sNotify mode %d detail %d on %lx\n",
983                           (e->type == EnterNotify ? "Enter" : "Leave"),
984                           e->xcrossing.mode,
985                           e->xcrossing.detail, (client?client->window:0));
986             if (grab_on_keyboard())
987                 break;
988             if (config_focus_follow && config_focus_delay &&
989                 /* leave inferior events can happen when the mouse goes onto
990                    the window's border and then into the window before the
991                    delay is up */
992                 e->xcrossing.detail != NotifyInferior)
993             {
994                 ob_main_loop_timeout_remove_data(ob_main_loop,
995                                                  focus_delay_func,
996                                                  client, FALSE);
997             }
998             break;
999         default:
1000             break;
1001         }
1002         break;
1003     case EnterNotify:
1004     {
1005         con = frame_context(client, e->xcrossing.window,
1006                             e->xcrossing.x, e->xcrossing.y);
1007         switch (con) {
1008         case OB_FRAME_CONTEXT_MAXIMIZE:
1009             client->frame->max_hover = TRUE;
1010             frame_adjust_state(client->frame);
1011             break;
1012         case OB_FRAME_CONTEXT_ALLDESKTOPS:
1013             client->frame->desk_hover = TRUE;
1014             frame_adjust_state(client->frame);
1015             break;
1016         case OB_FRAME_CONTEXT_SHADE:
1017             client->frame->shade_hover = TRUE;
1018             frame_adjust_state(client->frame);
1019             break;
1020         case OB_FRAME_CONTEXT_ICONIFY:
1021             client->frame->iconify_hover = TRUE;
1022             frame_adjust_state(client->frame);
1023             break;
1024         case OB_FRAME_CONTEXT_CLOSE:
1025             client->frame->close_hover = TRUE;
1026             frame_adjust_state(client->frame);
1027             break;
1028         case OB_FRAME_CONTEXT_FRAME:
1029             if (grab_on_keyboard())
1030                 break;
1031             if (e->xcrossing.mode == NotifyGrab ||
1032                 e->xcrossing.mode == NotifyUngrab ||
1033                 /*ignore enters when we're already in the window */
1034                 e->xcrossing.detail == NotifyInferior ||
1035                 is_enter_focus_event_ignored(e))
1036             {
1037                 ob_debug_type(OB_DEBUG_FOCUS,
1038                               "%sNotify mode %d detail %d serial %lu on %lx "
1039                               "IGNORED\n",
1040                               (e->type == EnterNotify ? "Enter" : "Leave"),
1041                               e->xcrossing.mode,
1042                               e->xcrossing.detail,
1043                               e->xcrossing.serial,
1044                               client?client->window:0);
1045             }
1046             else {
1047                 ob_debug_type(OB_DEBUG_FOCUS,
1048                               "%sNotify mode %d detail %d serial %lu on %lx, "
1049                               "focusing window\n",
1050                               (e->type == EnterNotify ? "Enter" : "Leave"),
1051                               e->xcrossing.mode,
1052                               e->xcrossing.detail,
1053                               e->xcrossing.serial,
1054                               (client?client->window:0));
1055                 if (config_focus_follow)
1056                     event_enter_client(client);
1057             }
1058             break;
1059         default:
1060             break;
1061         }
1062         break;
1063     }
1064     case ConfigureRequest:
1065     {
1066         /* dont compress these unless you're going to watch for property
1067            notifies in between (these can change what the configure would
1068            do to the window).
1069            also you can't compress stacking events
1070         */
1071
1072         gint x, y, w, h;
1073         gboolean move = FALSE;
1074         gboolean resize = FALSE;
1075
1076         /* get the current area */
1077         RECT_TO_DIMS(client->area, x, y, w, h);
1078
1079         ob_debug("ConfigureRequest for \"%s\" desktop %d wmstate %d "
1080                  "visibile %d\n"
1081                  "                     x %d y %d w %d h %d b %d\n",
1082                  client->title,
1083                  screen_desktop, client->wmstate, client->frame->visible,
1084                  x, y, w, h, client->border_width);
1085
1086         if (e->xconfigurerequest.value_mask & CWBorderWidth)
1087             if (client->border_width != e->xconfigurerequest.border_width) {
1088                 client->border_width = e->xconfigurerequest.border_width;
1089
1090                 /* if the border width is changing then that is the same
1091                    as requesting a resize, but we don't actually change
1092                    the client's border, so it will change their root
1093                    coordiantes (since they include the border width) and
1094                    we need to a notify then */
1095                 move = TRUE;
1096             }
1097
1098
1099         if (e->xconfigurerequest.value_mask & CWStackMode) {
1100             ObClient *sibling = NULL;
1101             gulong ignore_start;
1102             gboolean ok = TRUE;
1103
1104             /* get the sibling */
1105             if (e->xconfigurerequest.value_mask & CWSibling) {
1106                 ObWindow *win;
1107                 win = g_hash_table_lookup(window_map,
1108                                           &e->xconfigurerequest.above);
1109                 if (win && WINDOW_IS_CLIENT(win) &&
1110                     WINDOW_AS_CLIENT(win) != client)
1111                 {
1112                     sibling = WINDOW_AS_CLIENT(win);
1113                 }
1114                 else
1115                     /* an invalid sibling was specified so don't restack at
1116                        all, it won't make sense no matter what we do */
1117                     ok = FALSE;
1118             }
1119
1120             if (ok) {
1121                 if (!config_focus_under_mouse)
1122                     ignore_start = event_start_ignore_all_enters();
1123                 stacking_restack_request(client, sibling,
1124                                          e->xconfigurerequest.detail);
1125                 if (!config_focus_under_mouse)
1126                     event_end_ignore_all_enters(ignore_start);
1127             }
1128
1129             /* a stacking change moves the window without resizing */
1130             move = TRUE;
1131         }
1132
1133         if ((e->xconfigurerequest.value_mask & CWX) ||
1134             (e->xconfigurerequest.value_mask & CWY) ||
1135             (e->xconfigurerequest.value_mask & CWWidth) ||
1136             (e->xconfigurerequest.value_mask & CWHeight))
1137         {
1138             if (e->xconfigurerequest.value_mask & CWX) {
1139                 /* don't allow clients to move shaded windows (fvwm does this)
1140                  */
1141                 if (!client->shaded)
1142                     x = e->xconfigurerequest.x;
1143                 move = TRUE;
1144             }
1145             if (e->xconfigurerequest.value_mask & CWY) {
1146                 /* don't allow clients to move shaded windows (fvwm does this)
1147                  */
1148                 if (!client->shaded)
1149                     y = e->xconfigurerequest.y;
1150                 move = TRUE;
1151             }
1152
1153             if (e->xconfigurerequest.value_mask & CWWidth) {
1154                 w = e->xconfigurerequest.width;
1155                 resize = TRUE;
1156             }
1157             if (e->xconfigurerequest.value_mask & CWHeight) {
1158                 h = e->xconfigurerequest.height;
1159                 resize = TRUE;
1160             }
1161         }
1162
1163         ob_debug("ConfigureRequest x(%d) %d y(%d) %d w(%d) %d h(%d) %d "
1164                  "move %d resize %d\n",
1165                  e->xconfigurerequest.value_mask & CWX, x,
1166                  e->xconfigurerequest.value_mask & CWY, y,
1167                  e->xconfigurerequest.value_mask & CWWidth, w,
1168                  e->xconfigurerequest.value_mask & CWHeight, h,
1169                  move, resize);
1170
1171         /* check for broken apps moving to their root position
1172
1173            XXX remove this some day...that would be nice. right now all
1174            kde apps do this when they try activate themselves on another
1175            desktop. eg. open amarok window on desktop 1, switch to desktop
1176            2, click amarok tray icon. it will move by its decoration size.
1177         */
1178         if (x != client->area.x &&
1179             x == (client->frame->area.x + client->frame->size.left -
1180                   (gint)client->border_width) &&
1181             y != client->area.y &&
1182             y == (client->frame->area.y + client->frame->size.top -
1183                   (gint)client->border_width) &&
1184             w == client->area.width &&
1185             h == client->area.height)
1186         {
1187             ob_debug_type(OB_DEBUG_APP_BUGS,
1188                           "Application %s is trying to move via "
1189                           "ConfigureRequest to it's root window position "
1190                           "but it is not using StaticGravity\n",
1191                           client->title);
1192             /* don't move it */
1193             x = client->area.x;
1194             y = client->area.y;
1195
1196             /* they still requested a move, so don't change whether a
1197                notify is sent or not */
1198         }
1199
1200         {
1201             gint lw,lh;
1202
1203             client_try_configure(client, &x, &y, &w, &h, &lw, &lh, FALSE);
1204
1205             /* if x was not given, then use gravity to figure out the new
1206                x.  the reference point should not be moved */
1207             if ((e->xconfigurerequest.value_mask & CWWidth &&
1208                  !(e->xconfigurerequest.value_mask & CWX)))
1209                 client_gravity_resize_w(client, &x, client->area.width, w);
1210             /* if y was not given, then use gravity to figure out the new
1211                y.  the reference point should not be moved */
1212             if ((e->xconfigurerequest.value_mask & CWHeight &&
1213                  !(e->xconfigurerequest.value_mask & CWY)))
1214                 client_gravity_resize_h(client, &y, client->area.height,h);
1215
1216             client_find_onscreen(client, &x, &y, w, h, FALSE);
1217
1218             ob_debug("Granting ConfigureRequest x %d y %d w %d h %d\n",
1219                      x, y, w, h);
1220             client_configure(client, x, y, w, h, FALSE, TRUE, TRUE);
1221         }
1222         break;
1223     }
1224     case UnmapNotify:
1225         if (client->ignore_unmaps) {
1226             client->ignore_unmaps--;
1227             break;
1228         }
1229         ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
1230                  "ignores left %d\n",
1231                  client->window, e->xunmap.event, e->xunmap.from_configure,
1232                  client->ignore_unmaps);
1233         client_unmanage(client);
1234         break;
1235     case DestroyNotify:
1236         ob_debug("DestroyNotify for window 0x%x\n", client->window);
1237         client_unmanage(client);
1238         break;
1239     case ReparentNotify:
1240         /* this is when the client is first taken captive in the frame */
1241         if (e->xreparent.parent == client->frame->window) break;
1242
1243         /*
1244           This event is quite rare and is usually handled in unmapHandler.
1245           However, if the window is unmapped when the reparent event occurs,
1246           the window manager never sees it because an unmap event is not sent
1247           to an already unmapped window.
1248         */
1249
1250         /* we don't want the reparent event, put it back on the stack for the
1251            X server to deal with after we unmanage the window */
1252         XPutBackEvent(ob_display, e);
1253
1254         ob_debug("ReparentNotify for window 0x%x\n", client->window);
1255         client_unmanage(client);
1256         break;
1257     case MapRequest:
1258         ob_debug("MapRequest for 0x%lx\n", client->window);
1259         if (!client->iconic) break; /* this normally doesn't happen, but if it
1260                                        does, we don't want it!
1261                                        it can happen now when the window is on
1262                                        another desktop, but we still don't
1263                                        want it! */
1264         client_activate(client, FALSE, TRUE, TRUE, TRUE);
1265         break;
1266     case ClientMessage:
1267         /* validate cuz we query stuff off the client here */
1268         if (!client_validate(client)) break;
1269
1270         if (e->xclient.format != 32) return;
1271
1272         msgtype = e->xclient.message_type;
1273         if (msgtype == prop_atoms.wm_change_state) {
1274             /* compress changes into a single change */
1275             while (XCheckTypedWindowEvent(ob_display, client->window,
1276                                           e->type, &ce)) {
1277                 /* XXX: it would be nice to compress ALL messages of a
1278                    type, not just messages in a row without other
1279                    message types between. */
1280                 if (ce.xclient.message_type != msgtype) {
1281                     XPutBackEvent(ob_display, &ce);
1282                     break;
1283                 }
1284                 e->xclient = ce.xclient;
1285             }
1286             client_set_wm_state(client, e->xclient.data.l[0]);
1287         } else if (msgtype == prop_atoms.net_wm_desktop) {
1288             /* compress changes into a single change */
1289             while (XCheckTypedWindowEvent(ob_display, client->window,
1290                                           e->type, &ce)) {
1291                 /* XXX: it would be nice to compress ALL messages of a
1292                    type, not just messages in a row without other
1293                    message types between. */
1294                 if (ce.xclient.message_type != msgtype) {
1295                     XPutBackEvent(ob_display, &ce);
1296                     break;
1297                 }
1298                 e->xclient = ce.xclient;
1299             }
1300             if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
1301                 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
1302                 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
1303                                    FALSE, FALSE);
1304         } else if (msgtype == prop_atoms.net_wm_state) {
1305             gulong ignore_start;
1306
1307             /* can't compress these */
1308             ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
1309                      (e->xclient.data.l[0] == 0 ? "Remove" :
1310                       e->xclient.data.l[0] == 1 ? "Add" :
1311                       e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1312                      e->xclient.data.l[1], e->xclient.data.l[2],
1313                      client->window);
1314
1315             /* ignore enter events caused by these like ob actions do */
1316             if (!config_focus_under_mouse)
1317                 ignore_start = event_start_ignore_all_enters();
1318             client_set_state(client, e->xclient.data.l[0],
1319                              e->xclient.data.l[1], e->xclient.data.l[2]);
1320             if (!config_focus_under_mouse)
1321                 event_end_ignore_all_enters(ignore_start);
1322         } else if (msgtype == prop_atoms.net_close_window) {
1323             ob_debug("net_close_window for 0x%lx\n", client->window);
1324             client_close(client);
1325         } else if (msgtype == prop_atoms.net_active_window) {
1326             ob_debug("net_active_window for 0x%lx source=%s\n",
1327                      client->window,
1328                      (e->xclient.data.l[0] == 0 ? "unknown" :
1329                       (e->xclient.data.l[0] == 1 ? "application" :
1330                        (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1331             /* XXX make use of data.l[2] !? */
1332             if (e->xclient.data.l[0] == 1 || e->xclient.data.l[0] == 2) {
1333                 /* don't use the user's timestamp for client_focus, cuz if it's
1334                    an old broken timestamp (happens all the time) then focus
1335                    won't move even though we're trying to move it
1336                   event_curtime = e->xclient.data.l[1];*/
1337                 if (e->xclient.data.l[1] == 0)
1338                     ob_debug_type(OB_DEBUG_APP_BUGS,
1339                                   "_NET_ACTIVE_WINDOW message for window %s is"
1340                                   " missing a timestamp\n", client->title);
1341             } else
1342                 ob_debug_type(OB_DEBUG_APP_BUGS,
1343                               "_NET_ACTIVE_WINDOW message for window %s is "
1344                               "missing source indication\n");
1345             client_activate(client, FALSE, TRUE, TRUE,
1346                             (e->xclient.data.l[0] == 0 ||
1347                              e->xclient.data.l[0] == 2));
1348         } else if (msgtype == prop_atoms.net_wm_moveresize) {
1349             ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1350                      client->window, e->xclient.data.l[2]);
1351             if ((Atom)e->xclient.data.l[2] ==
1352                 prop_atoms.net_wm_moveresize_size_topleft ||
1353                 (Atom)e->xclient.data.l[2] ==
1354                 prop_atoms.net_wm_moveresize_size_top ||
1355                 (Atom)e->xclient.data.l[2] ==
1356                 prop_atoms.net_wm_moveresize_size_topright ||
1357                 (Atom)e->xclient.data.l[2] ==
1358                 prop_atoms.net_wm_moveresize_size_right ||
1359                 (Atom)e->xclient.data.l[2] ==
1360                 prop_atoms.net_wm_moveresize_size_right ||
1361                 (Atom)e->xclient.data.l[2] ==
1362                 prop_atoms.net_wm_moveresize_size_bottomright ||
1363                 (Atom)e->xclient.data.l[2] ==
1364                 prop_atoms.net_wm_moveresize_size_bottom ||
1365                 (Atom)e->xclient.data.l[2] ==
1366                 prop_atoms.net_wm_moveresize_size_bottomleft ||
1367                 (Atom)e->xclient.data.l[2] ==
1368                 prop_atoms.net_wm_moveresize_size_left ||
1369                 (Atom)e->xclient.data.l[2] ==
1370                 prop_atoms.net_wm_moveresize_move ||
1371                 (Atom)e->xclient.data.l[2] ==
1372                 prop_atoms.net_wm_moveresize_size_keyboard ||
1373                 (Atom)e->xclient.data.l[2] ==
1374                 prop_atoms.net_wm_moveresize_move_keyboard) {
1375
1376                 moveresize_start(client, e->xclient.data.l[0],
1377                                  e->xclient.data.l[1], e->xclient.data.l[3],
1378                                  e->xclient.data.l[2]);
1379             }
1380             else if ((Atom)e->xclient.data.l[2] ==
1381                      prop_atoms.net_wm_moveresize_cancel)
1382                 moveresize_end(TRUE);
1383         } else if (msgtype == prop_atoms.net_moveresize_window) {
1384             gint ograv, x, y, w, h;
1385
1386             ograv = client->gravity;
1387
1388             if (e->xclient.data.l[0] & 0xff)
1389                 client->gravity = e->xclient.data.l[0] & 0xff;
1390
1391             if (e->xclient.data.l[0] & 1 << 8)
1392                 x = e->xclient.data.l[1];
1393             else
1394                 x = client->area.x;
1395             if (e->xclient.data.l[0] & 1 << 9)
1396                 y = e->xclient.data.l[2];
1397             else
1398                 y = client->area.y;
1399
1400             if (e->xclient.data.l[0] & 1 << 10) {
1401                 w = e->xclient.data.l[3];
1402
1403                 /* if x was not given, then use gravity to figure out the new
1404                    x.  the reference point should not be moved */
1405                 if (!(e->xclient.data.l[0] & 1 << 8))
1406                     client_gravity_resize_w(client, &x, client->area.width, w);
1407             }
1408             else
1409                 w = client->area.width;
1410
1411             if (e->xclient.data.l[0] & 1 << 11) {
1412                 h = e->xclient.data.l[4];
1413
1414                 /* if y was not given, then use gravity to figure out the new
1415                    y.  the reference point should not be moved */
1416                 if (!(e->xclient.data.l[0] & 1 << 9))
1417                     client_gravity_resize_h(client, &y, client->area.height,h);
1418             }
1419             else
1420                 h = client->area.height;
1421
1422             ob_debug("MOVERESIZE x %d %d y %d %d (gravity %d)\n",
1423                      e->xclient.data.l[0] & 1 << 8, x,
1424                      e->xclient.data.l[0] & 1 << 9, y,
1425                      client->gravity);
1426
1427             client_find_onscreen(client, &x, &y, w, h, FALSE);
1428
1429             client_configure(client, x, y, w, h, FALSE, TRUE, FALSE);
1430
1431             client->gravity = ograv;
1432         } else if (msgtype == prop_atoms.net_restack_window) {
1433             if (e->xclient.data.l[0] != 2) {
1434                 ob_debug_type(OB_DEBUG_APP_BUGS,
1435                               "_NET_RESTACK_WINDOW sent for window %s with "
1436                               "invalid source indication %ld\n",
1437                               client->title, e->xclient.data.l[0]);
1438             } else {
1439                 ObClient *sibling = NULL;
1440                 if (e->xclient.data.l[1]) {
1441                     ObWindow *win = g_hash_table_lookup
1442                         (window_map, &e->xclient.data.l[1]);
1443                     if (WINDOW_IS_CLIENT(win) &&
1444                         WINDOW_AS_CLIENT(win) != client)
1445                     {
1446                         sibling = WINDOW_AS_CLIENT(win);
1447                     }
1448                     if (sibling == NULL)
1449                         ob_debug_type(OB_DEBUG_APP_BUGS,
1450                                       "_NET_RESTACK_WINDOW sent for window %s "
1451                                       "with invalid sibling 0x%x\n",
1452                                  client->title, e->xclient.data.l[1]);
1453                 }
1454                 if (e->xclient.data.l[2] == Below ||
1455                     e->xclient.data.l[2] == BottomIf ||
1456                     e->xclient.data.l[2] == Above ||
1457                     e->xclient.data.l[2] == TopIf ||
1458                     e->xclient.data.l[2] == Opposite)
1459                 {
1460                     gulong ignore_start;
1461
1462                     if (!config_focus_under_mouse)
1463                         ignore_start = event_start_ignore_all_enters();
1464                     /* just raise, don't activate */
1465                     stacking_restack_request(client, sibling,
1466                                              e->xclient.data.l[2]);
1467                     if (!config_focus_under_mouse)
1468                         event_end_ignore_all_enters(ignore_start);
1469
1470                     /* send a synthetic ConfigureNotify, cuz this is supposed
1471                        to be like a ConfigureRequest. */
1472                     client_reconfigure(client, TRUE);
1473                 } else
1474                     ob_debug_type(OB_DEBUG_APP_BUGS,
1475                                   "_NET_RESTACK_WINDOW sent for window %s "
1476                                   "with invalid detail %d\n",
1477                                   client->title, e->xclient.data.l[2]);
1478             }
1479         }
1480         break;
1481     case PropertyNotify:
1482         /* validate cuz we query stuff off the client here */
1483         if (!client_validate(client)) break;
1484
1485         /* compress changes to a single property into a single change */
1486         while (XCheckTypedWindowEvent(ob_display, client->window,
1487                                       e->type, &ce)) {
1488             Atom a, b;
1489
1490             /* XXX: it would be nice to compress ALL changes to a property,
1491                not just changes in a row without other props between. */
1492
1493             a = ce.xproperty.atom;
1494             b = e->xproperty.atom;
1495
1496             if (a == b)
1497                 continue;
1498             if ((a == prop_atoms.net_wm_name ||
1499                  a == prop_atoms.wm_name ||
1500                  a == prop_atoms.net_wm_icon_name ||
1501                  a == prop_atoms.wm_icon_name)
1502                 &&
1503                 (b == prop_atoms.net_wm_name ||
1504                  b == prop_atoms.wm_name ||
1505                  b == prop_atoms.net_wm_icon_name ||
1506                  b == prop_atoms.wm_icon_name)) {
1507                 continue;
1508             }
1509             if (a == prop_atoms.net_wm_icon &&
1510                 b == prop_atoms.net_wm_icon)
1511                 continue;
1512
1513             XPutBackEvent(ob_display, &ce);
1514             break;
1515         }
1516
1517         msgtype = e->xproperty.atom;
1518         if (msgtype == XA_WM_NORMAL_HINTS) {
1519             ob_debug("Update NORMAL hints\n");
1520             client_update_normal_hints(client);
1521             /* normal hints can make a window non-resizable */
1522             client_setup_decor_and_functions(client, FALSE);
1523
1524             /* make sure the client's sizes are within its bounds, but only
1525                reconfigure the window if it needs to. emacs will update its
1526                normal hints every time it receives a conigurenotify */
1527             client_reconfigure(client, FALSE);
1528         } else if (msgtype == XA_WM_HINTS) {
1529             client_update_wmhints(client);
1530         } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1531             client_update_transient_for(client);
1532             client_get_type_and_transientness(client);
1533             /* type may have changed, so update the layer */
1534             client_calc_layer(client);
1535             client_setup_decor_and_functions(client, TRUE);
1536         } else if (msgtype == prop_atoms.net_wm_name ||
1537                    msgtype == prop_atoms.wm_name ||
1538                    msgtype == prop_atoms.net_wm_icon_name ||
1539                    msgtype == prop_atoms.wm_icon_name) {
1540             client_update_title(client);
1541         } else if (msgtype == prop_atoms.wm_protocols) {
1542             client_update_protocols(client);
1543             client_setup_decor_and_functions(client, TRUE);
1544         }
1545         else if (msgtype == prop_atoms.net_wm_strut) {
1546             client_update_strut(client);
1547         }
1548         else if (msgtype == prop_atoms.net_wm_strut_partial) {
1549             client_update_strut(client);
1550         }
1551         else if (msgtype == prop_atoms.net_wm_icon) {
1552             client_update_icons(client);
1553         }
1554         else if (msgtype == prop_atoms.net_wm_icon_geometry) {
1555             client_update_icon_geometry(client);
1556         }
1557         else if (msgtype == prop_atoms.net_wm_user_time) {
1558             guint32 t;
1559             if (client == focus_client &&
1560                 PROP_GET32(client->window, net_wm_user_time, cardinal, &t) &&
1561                 t && !event_time_after(t, e->xproperty.time) &&
1562                 (!event_last_user_time ||
1563                  event_time_after(t, event_last_user_time)))
1564             {
1565                 event_last_user_time = t;
1566             }
1567         }
1568 #ifdef SYNC
1569         else if (msgtype == prop_atoms.net_wm_sync_request_counter) {
1570             client_update_sync_request_counter(client);
1571         }
1572 #endif
1573         break;
1574     case ColormapNotify:
1575         client_update_colormap(client, e->xcolormap.colormap);
1576         break;
1577     default:
1578         ;
1579 #ifdef SHAPE
1580         if (extensions_shape && e->type == extensions_shape_event_basep) {
1581             client->shaped = ((XShapeEvent*)e)->shaped;
1582             frame_adjust_shape(client->frame);
1583         }
1584 #endif
1585     }
1586 }
1587
1588 static void event_handle_dock(ObDock *s, XEvent *e)
1589 {
1590     switch (e->type) {
1591     case ButtonPress:
1592         if (e->xbutton.button == 1)
1593             stacking_raise(DOCK_AS_WINDOW(s));
1594         else if (e->xbutton.button == 2)
1595             stacking_lower(DOCK_AS_WINDOW(s));
1596         break;
1597     case EnterNotify:
1598         dock_hide(FALSE);
1599         break;
1600     case LeaveNotify:
1601         /* don't hide when moving into a dock app */
1602         if (e->xcrossing.detail != NotifyInferior)
1603             dock_hide(TRUE);
1604         break;
1605     }
1606 }
1607
1608 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1609 {
1610     switch (e->type) {
1611     case MotionNotify:
1612         dock_app_drag(app, &e->xmotion);
1613         break;
1614     case UnmapNotify:
1615         if (app->ignore_unmaps) {
1616             app->ignore_unmaps--;
1617             break;
1618         }
1619         dock_remove(app, TRUE);
1620         break;
1621     case DestroyNotify:
1622         dock_remove(app, FALSE);
1623         break;
1624     case ReparentNotify:
1625         dock_remove(app, FALSE);
1626         break;
1627     case ConfigureNotify:
1628         dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1629         break;
1630     }
1631 }
1632
1633 static ObMenuFrame* find_active_menu(void)
1634 {
1635     GList *it;
1636     ObMenuFrame *ret = NULL;
1637
1638     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1639         ret = it->data;
1640         if (ret->selected)
1641             break;
1642         ret = NULL;
1643     }
1644     return ret;
1645 }
1646
1647 static ObMenuFrame* find_active_or_last_menu(void)
1648 {
1649     ObMenuFrame *ret = NULL;
1650
1651     ret = find_active_menu();
1652     if (!ret && menu_frame_visible)
1653         ret = menu_frame_visible->data;
1654     return ret;
1655 }
1656
1657 static gboolean event_handle_menu_keyboard(XEvent *ev)
1658 {
1659     guint keycode, state;
1660     gunichar unikey;
1661     ObMenuFrame *frame;
1662     gboolean ret = FALSE;
1663
1664     keycode = ev->xkey.keycode;
1665     state = ev->xkey.state;
1666     unikey = translate_unichar(keycode);
1667
1668     frame = find_active_or_last_menu();
1669     if (frame == NULL)
1670         g_assert_not_reached(); /* there is no active menu */
1671
1672     /* Allow control while going thru the menu */
1673     else if (ev->type == KeyPress && (state & ~ControlMask) == 0) {
1674         if (keycode == ob_keycode(OB_KEY_ESCAPE)) {
1675             menu_frame_hide_all();
1676             ret = TRUE;
1677         }
1678
1679         else if (keycode == ob_keycode(OB_KEY_LEFT)) {
1680             /* Left goes to the parent menu */
1681             menu_frame_select(frame, NULL, TRUE);
1682             ret = TRUE;
1683         }
1684
1685         else if (keycode == ob_keycode(OB_KEY_RIGHT)) {
1686             /* Right goes to the selected submenu */
1687             if (frame->child) menu_frame_select_next(frame->child);
1688             ret = TRUE;
1689         }
1690
1691         else if (keycode == ob_keycode(OB_KEY_UP)) {
1692             menu_frame_select_previous(frame);
1693             ret = TRUE;
1694         }
1695
1696         else if (keycode == ob_keycode(OB_KEY_DOWN)) {
1697             menu_frame_select_next(frame);
1698             ret = TRUE;
1699         }
1700     }
1701
1702     /* Use KeyRelease events for running things so that the key release doesn't
1703        get sent to the focused application.
1704
1705        Allow ControlMask only, and don't bother if the menu is empty */
1706     else if (ev->type == KeyRelease && (state & ~ControlMask) == 0 &&
1707              frame->entries)
1708     {
1709         if (keycode == ob_keycode(OB_KEY_RETURN)) {
1710             /* Enter runs the active item or goes into the submenu.
1711                Control-Enter runs it without closing the menu. */
1712             if (frame->child)
1713                 menu_frame_select_next(frame->child);
1714             else if (frame->selected)
1715                 menu_entry_frame_execute(frame->selected, state);
1716
1717             ret = TRUE;
1718         }
1719
1720         /* keyboard accelerator shortcuts. (if it was a valid key) */
1721         else if (unikey != 0) {
1722             GList *start;
1723             GList *it;
1724             ObMenuEntryFrame *found = NULL;
1725             guint num_found = 0;
1726
1727             /* start after the selected one */
1728             start = frame->entries;
1729             if (frame->selected) {
1730                 for (it = start; frame->selected != it->data;
1731                      it = g_list_next(it))
1732                     g_assert(it != NULL); /* nothing was selected? */
1733                 /* next with wraparound */
1734                 start = g_list_next(it);
1735                 if (start == NULL) start = frame->entries;
1736             }
1737
1738             it = start;
1739             do {
1740                 ObMenuEntryFrame *e = it->data;
1741                 gunichar entrykey = 0;
1742
1743                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1744                     entrykey = e->entry->data.normal.shortcut;
1745                 else if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1746                     entrykey = e->entry->data.submenu.submenu->shortcut;
1747
1748                 if (unikey == entrykey) {
1749                     if (found == NULL) found = e;
1750                     ++num_found;
1751                 }
1752
1753                 /* next with wraparound */
1754                 it = g_list_next(it);
1755                 if (it == NULL) it = frame->entries;
1756             } while (it != start);
1757
1758             if (found) {
1759                 if (found->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1760                     num_found == 1)
1761                 {
1762                     menu_frame_select(frame, found, TRUE);
1763                     usleep(50000); /* highlight the item for a short bit so the
1764                                       user can see what happened */
1765                     menu_entry_frame_execute(found, state);
1766                 } else {
1767                     menu_frame_select(frame, found, TRUE);
1768                     if (num_found == 1)
1769                         menu_frame_select_next(frame->child);
1770                 }
1771
1772                 ret = TRUE;
1773             }
1774         }
1775     }
1776
1777     return ret;
1778 }
1779
1780 static gboolean event_handle_menu(XEvent *ev)
1781 {
1782     ObMenuFrame *f;
1783     ObMenuEntryFrame *e;
1784     gboolean ret = TRUE;
1785
1786     switch (ev->type) {
1787     case ButtonRelease:
1788         if (menu_hide_delay_reached() &&
1789             (ev->xbutton.button < 4 || ev->xbutton.button > 5))
1790         {
1791             if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1792                                             ev->xbutton.y_root)))
1793             {
1794                 menu_frame_select(e->frame, e, TRUE);
1795                 menu_entry_frame_execute(e, ev->xbutton.state);
1796             }
1797             else
1798                 menu_frame_hide_all();
1799         }
1800         break;
1801     case EnterNotify:
1802         if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1803             if (e->ignore_enters)
1804                 --e->ignore_enters;
1805             else if (!(f = find_active_menu()) ||
1806                      f == e->frame ||
1807                      f->parent == e->frame ||
1808                      f->child == e->frame)
1809                 menu_frame_select(e->frame, e, FALSE);
1810         }
1811         break;
1812     case LeaveNotify:
1813         /*ignore leaves when we're already in the window */
1814         if (ev->xcrossing.detail == NotifyInferior)
1815             break;
1816
1817         if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1818             (f = find_active_menu()) && f->selected == e &&
1819             e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1820         {
1821             menu_frame_select(e->frame, NULL, FALSE);
1822         }
1823         break;
1824     case MotionNotify:
1825         if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1826                                         ev->xmotion.y_root)))
1827             if (!(f = find_active_menu()) ||
1828                 f == e->frame ||
1829                 f->parent == e->frame ||
1830                 f->child == e->frame)
1831                 menu_frame_select(e->frame, e, FALSE);
1832         break;
1833     case KeyPress:
1834     case KeyRelease:
1835         ret = event_handle_menu_keyboard(ev);
1836         break;
1837     }
1838     return ret;
1839 }
1840
1841 static void event_handle_user_input(ObClient *client, XEvent *e)
1842 {
1843     g_assert(e->type == ButtonPress || e->type == ButtonRelease ||
1844              e->type == MotionNotify || e->type == KeyPress ||
1845              e->type == KeyRelease);
1846
1847     if (menu_frame_visible) {
1848         if (event_handle_menu(e))
1849             /* don't use the event if the menu used it, but if the menu
1850                didn't use it and it's a keypress that is bound, it will
1851                close the menu and be used */
1852             return;
1853     }
1854
1855     /* if the keyboard interactive action uses the event then dont
1856        use it for bindings. likewise is moveresize uses the event. */
1857     if (!actions_interactive_input_event(e) && !moveresize_event(e)) {
1858         if (moveresize_in_progress)
1859             /* make further actions work on the client being
1860                moved/resized */
1861             client = moveresize_client;
1862
1863         if (e->type == ButtonPress ||
1864             e->type == ButtonRelease ||
1865             e->type == MotionNotify)
1866         {
1867             /* the frame may not be "visible" but they can still click on it
1868                in the case where it is animating before disappearing */
1869             if (!client || !frame_iconify_animating(client->frame))
1870                 mouse_event(client, e);
1871         } else
1872             keyboard_event((focus_cycle_target ? focus_cycle_target :
1873                             (client ? client : focus_client)), e);
1874     }
1875 }
1876
1877 static void focus_delay_dest(gpointer data)
1878 {
1879     g_free(data);
1880 }
1881
1882 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1883 {
1884     const ObFocusDelayData *f1 = d1;
1885     return f1->client == d2;
1886 }
1887
1888 static gboolean focus_delay_func(gpointer data)
1889 {
1890     ObFocusDelayData *d = data;
1891     Time old = event_curtime;
1892
1893     /* don't move focus and kill the menu or the move/resize */
1894     if (menu_frame_visible || moveresize_in_progress) return FALSE;
1895
1896     event_curtime = d->time;
1897     event_curserial = d->serial;
1898     if (client_focus(d->client) && config_focus_raise)
1899         stacking_raise(CLIENT_AS_WINDOW(d->client));
1900     event_curtime = old;
1901     return FALSE; /* no repeat */
1902 }
1903
1904 static void focus_delay_client_dest(ObClient *client, gpointer data)
1905 {
1906     ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1907                                      client, FALSE);
1908 }
1909
1910 void event_halt_focus_delay()
1911 {
1912     /* ignore all enter events up till the event which caused this to occur */
1913     if (event_curserial) event_ignore_enter_range(1, event_curserial);
1914     ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1915 }
1916
1917 gulong event_start_ignore_all_enters(void)
1918 {
1919     XSync(ob_display, FALSE);
1920     return LastKnownRequestProcessed(ob_display);
1921 }
1922
1923 static void event_ignore_enter_range(gulong start, gulong end)
1924 {
1925     ObSerialRange *r;
1926
1927     g_assert(start != 0);
1928     g_assert(end != 0);
1929
1930     r = g_new(ObSerialRange, 1);
1931     r->start = start;
1932     r->end = end;
1933     ignore_serials = g_slist_prepend(ignore_serials, r);
1934
1935     ob_debug_type(OB_DEBUG_FOCUS, "ignoring enters from %lu until %lu\n",
1936                   r->start, r->end);
1937
1938     /* increment the serial so we don't ignore events we weren't meant to */
1939     XSync(ob_display, FALSE);
1940 }
1941
1942 void event_end_ignore_all_enters(gulong start)
1943 {
1944     XSync(ob_display, FALSE);
1945     event_ignore_enter_range(start, LastKnownRequestProcessed(ob_display));
1946 }
1947
1948 static gboolean is_enter_focus_event_ignored(XEvent *e)
1949 {
1950     GSList *it, *next;
1951
1952     g_assert(e->type == EnterNotify &&
1953              !(e->xcrossing.mode == NotifyGrab ||
1954                e->xcrossing.mode == NotifyUngrab ||
1955                e->xcrossing.detail == NotifyInferior));
1956
1957     for (it = ignore_serials; it; it = next) {
1958         ObSerialRange *r = it->data;
1959
1960         next = g_slist_next(it);
1961
1962         if ((glong)(e->xany.serial - r->end) > 0) {
1963             /* past the end */
1964             ignore_serials = g_slist_delete_link(ignore_serials, it);
1965             g_free(r);
1966         }
1967         else if ((glong)(e->xany.serial - r->start) >= 0)
1968             return TRUE;
1969     }
1970     return FALSE;
1971 }
1972
1973 void event_cancel_all_key_grabs(void)
1974 {
1975     if (actions_interactive_act_running()) {
1976         actions_interactive_cancel_act();
1977         ob_debug("KILLED interactive action\n");
1978     }
1979     else if (menu_frame_visible) {
1980         menu_frame_hide_all();
1981         ob_debug("KILLED open menus\n");
1982     }
1983     else if (moveresize_in_progress) {
1984         moveresize_end(TRUE);
1985         ob_debug("KILLED interactive moveresize\n");
1986     }
1987     else if (grab_on_keyboard()) {
1988         ungrab_keyboard();
1989         ob_debug("KILLED active grab on keyboard\n");
1990     }
1991     else
1992         ungrab_passive_key();
1993 }
1994
1995 gboolean event_time_after(Time t1, Time t2)
1996 {
1997     g_assert(t1 != CurrentTime);
1998     g_assert(t2 != CurrentTime);
1999
2000     /*
2001       Timestamp values wrap around (after about 49.7 days). The server, given
2002       its current time is represented by timestamp T, always interprets
2003       timestamps from clients by treating half of the timestamp space as being
2004       later in time than T.
2005       - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
2006     */
2007
2008     /* TIME_HALF is half of the number space of a Time type variable */
2009 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
2010
2011     if (t2 >= TIME_HALF)
2012         /* t2 is in the second half so t1 might wrap around and be smaller than
2013            t2 */
2014         return t1 >= t2 || t1 < (t2 + TIME_HALF);
2015     else
2016         /* t2 is in the first half so t1 has to come after it */
2017         return t1 >= t2 && t1 < (t2 + TIME_HALF);
2018 }
2019
2020 Time event_get_server_time(void)
2021 {
2022     /* Generate a timestamp */
2023     XEvent event;
2024
2025     XChangeProperty(ob_display, screen_support_win,
2026                     prop_atoms.wm_class, prop_atoms.string,
2027                     8, PropModeAppend, NULL, 0);
2028     XWindowEvent(ob_display, screen_support_win, PropertyChangeMask, &event);
2029     return event.xproperty.time;
2030 }