since the internal windows are in window_map now, it's possible we'll get them back...
[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             /* not to be used for events */
479             g_assert_not_reached();
480             break;
481         case Window_Internal:
482             /* we don't do anything with events directly on these windows */
483             break;
484         }
485     }
486
487     event_set_curtime(e);
488     event_curserial = e->xany.serial;
489     event_hack_mods(e);
490     if (event_ignore(e, client)) {
491         if (ed)
492             ed->ignored = TRUE;
493         return;
494     } else if (ed)
495             ed->ignored = FALSE;
496
497     /* deal with it in the kernel */
498
499     if (menu_frame_visible &&
500         (e->type == EnterNotify || e->type == LeaveNotify))
501     {
502         /* crossing events for menu */
503         event_handle_menu(e);
504     } else if (e->type == FocusIn) {
505         if (client &&
506             e->xfocus.detail == NotifyInferior)
507         {
508             ob_debug_type(OB_DEBUG_FOCUS,
509                           "Focus went to the frame window");
510
511             focus_left_screen = FALSE;
512
513             focus_fallback(FALSE, config_focus_under_mouse, TRUE, TRUE);
514
515             /* We don't get a FocusOut for this case, because it's just moving
516                from our Inferior up to us. This happens when iconifying a
517                window with RevertToParent focus */
518             frame_adjust_focus(client->frame, FALSE);
519             /* focus_set_client(NULL) has already been called */
520             client_calc_layer(client);
521         }
522         else if (e->xfocus.detail == NotifyPointerRoot ||
523                  e->xfocus.detail == NotifyDetailNone ||
524                  e->xfocus.detail == NotifyInferior ||
525                  e->xfocus.detail == NotifyNonlinear)
526         {
527             XEvent ce;
528
529             ob_debug_type(OB_DEBUG_FOCUS,
530                           "Focus went to root or pointer root/none\n");
531
532             if (e->xfocus.detail == NotifyInferior ||
533                 e->xfocus.detail == NotifyNonlinear)
534             {
535                 focus_left_screen = FALSE;
536             }
537
538             /* If another FocusIn is in the queue then don't fallback yet. This
539                fixes the fun case of:
540                window map -> send focusin
541                window unmap -> get focusout
542                window map -> send focusin
543                get first focus out -> fall back to something (new window
544                  hasn't received focus yet, so something else) -> send focusin
545                which means the "something else" is the last thing to get a
546                focusin sent to it, so the new window doesn't end up with focus.
547
548                But if the other focus in is something like PointerRoot then we
549                still want to fall back.
550             */
551             if (XCheckIfEvent(ob_display, &ce, event_look_for_focusin_client,
552                               NULL))
553             {
554                 XPutBackEvent(ob_display, &ce);
555                 ob_debug_type(OB_DEBUG_FOCUS,
556                               "  but another FocusIn is coming\n");
557             } else {
558                 /* Focus has been reverted.
559
560                    FocusOut events come after UnmapNotify, so we don't need to
561                    worry about focusing an invalid window
562                 */
563
564                 if (!focus_left_screen)
565                     focus_fallback(FALSE, config_focus_under_mouse,
566                                    TRUE, TRUE);
567             }
568         }
569         else if (!client)
570         {
571             ob_debug_type(OB_DEBUG_FOCUS,
572                           "Focus went to a window that is already gone\n");
573
574             /* If you send focus to a window and then it disappears, you can
575                get the FocusIn for it, after it is unmanaged.
576                Just wait for the next FocusOut/FocusIn pair, but make note that
577                the window that was focused no longer is. */
578             focus_set_client(NULL);
579         }
580         else if (client != focus_client) {
581             focus_left_screen = FALSE;
582             frame_adjust_focus(client->frame, TRUE);
583             focus_set_client(client);
584             client_calc_layer(client);
585             client_bring_helper_windows(client);
586         }
587     } else if (e->type == FocusOut) {
588         XEvent ce;
589
590         /* Look for the followup FocusIn */
591         if (!XCheckIfEvent(ob_display, &ce, event_look_for_focusin, NULL)) {
592             /* There is no FocusIn, this means focus went to a window that
593                is not being managed, or a window on another screen. */
594             Window win, root;
595             gint i;
596             guint u;
597             xerror_set_ignore(TRUE);
598             if (XGetInputFocus(ob_display, &win, &i) != 0 &&
599                 XGetGeometry(ob_display, win, &root, &i,&i,&u,&u,&u,&u) != 0 &&
600                 root != RootWindow(ob_display, ob_screen))
601             {
602                 ob_debug_type(OB_DEBUG_FOCUS,
603                               "Focus went to another screen !\n");
604                 focus_left_screen = TRUE;
605             }
606             else
607                 ob_debug_type(OB_DEBUG_FOCUS,
608                               "Focus went to a black hole !\n");
609             xerror_set_ignore(FALSE);
610             /* nothing is focused */
611             focus_set_client(NULL);
612         } else {
613             /* Focus moved, so process the FocusIn event */
614             ObEventData ed = { .ignored = FALSE };
615             event_process(&ce, &ed);
616             if (ed.ignored) {
617                 /* The FocusIn was ignored, this means it was on a window
618                    that isn't a client. */
619                 ob_debug_type(OB_DEBUG_FOCUS,
620                               "Focus went to an unmanaged window 0x%x !\n",
621                               ce.xfocus.window);
622                 focus_fallback(TRUE, config_focus_under_mouse, TRUE, TRUE);
623             }
624         }
625
626         if (client && client != focus_client) {
627             frame_adjust_focus(client->frame, FALSE);
628             /* focus_set_client(NULL) has already been called in this
629                section or by focus_fallback */
630             client_calc_layer(client);
631         }
632     }
633     else if (client)
634         event_handle_client(client, e);
635     else if (dockapp)
636         event_handle_dockapp(dockapp, e);
637     else if (dock)
638         event_handle_dock(dock, e);
639     else if (window == RootWindow(ob_display, ob_screen))
640         event_handle_root(e);
641     else if (e->type == MapRequest)
642         client_manage(window);
643     else if (e->type == MappingNotify) {
644         /* keyboard layout changes for modifier mapping changes. reload the
645            modifier map, and rebind all the key bindings as appropriate */
646         ob_debug("Kepboard map changed. Reloading keyboard bindings.\n");
647         modkeys_shutdown(TRUE);
648         modkeys_startup(TRUE);
649         keyboard_rebind();
650     }
651     else if (e->type == ClientMessage) {
652         /* This is for _NET_WM_REQUEST_FRAME_EXTENTS messages. They come for
653            windows that are not managed yet. */
654         if (e->xclient.message_type == prop_atoms.net_request_frame_extents) {
655             /* Pretend to manage the client, getting information used to
656                determine its decorations */
657             ObClient *c = client_fake_manage(e->xclient.window);
658             gulong vals[4];
659
660             /* set the frame extents on the window */
661             vals[0] = c->frame->size.left;
662             vals[1] = c->frame->size.right;
663             vals[2] = c->frame->size.top;
664             vals[3] = c->frame->size.bottom;
665             PROP_SETA32(e->xclient.window, net_frame_extents,
666                         cardinal, vals, 4);
667
668             /* Free the pretend client */
669             client_fake_unmanage(c);
670         }
671     }
672     else if (e->type == ConfigureRequest) {
673         /* unhandled configure requests must be used to configure the
674            window directly */
675         XWindowChanges xwc;
676
677         xwc.x = e->xconfigurerequest.x;
678         xwc.y = e->xconfigurerequest.y;
679         xwc.width = e->xconfigurerequest.width;
680         xwc.height = e->xconfigurerequest.height;
681         xwc.border_width = e->xconfigurerequest.border_width;
682         xwc.sibling = e->xconfigurerequest.above;
683         xwc.stack_mode = e->xconfigurerequest.detail;
684
685         /* we are not to be held responsible if someone sends us an
686            invalid request! */
687         xerror_set_ignore(TRUE);
688         XConfigureWindow(ob_display, window,
689                          e->xconfigurerequest.value_mask, &xwc);
690         xerror_set_ignore(FALSE);
691     }
692 #ifdef SYNC
693     else if (extensions_sync &&
694         e->type == extensions_sync_event_basep + XSyncAlarmNotify)
695     {
696         XSyncAlarmNotifyEvent *se = (XSyncAlarmNotifyEvent*)e;
697         if (se->alarm == moveresize_alarm && moveresize_in_progress)
698             moveresize_event(e);
699     }
700 #endif
701
702     if (e->type == ButtonPress || e->type == ButtonRelease) {
703         /* If the button press was on some non-root window, or was physically
704            on the root window, the process it */
705         if (window != RootWindow(ob_display, ob_screen) ||
706             e->xbutton.subwindow == None)
707         {
708             event_handle_user_input(client, e);
709         }
710         /* Otherwise only process it if it was physically on an openbox
711            internal window */
712         else {
713             ObWindow *w;
714
715             if ((w = g_hash_table_lookup(window_map, &e->xbutton.subwindow)) &&
716                 WINDOW_IS_INTERNAL(w))
717             {
718                 event_handle_user_input(client, e);
719             }
720         }
721     }
722     else if (e->type == KeyPress || e->type == KeyRelease ||
723              e->type == MotionNotify)
724         event_handle_user_input(client, e);
725
726     /* if something happens and it's not from an XEvent, then we don't know
727        the time */
728     event_curtime = CurrentTime;
729     event_curserial = 0;
730 }
731
732 static void event_handle_root(XEvent *e)
733 {
734     Atom msgtype;
735
736     switch(e->type) {
737     case SelectionClear:
738         ob_debug("Another WM has requested to replace us. Exiting.\n");
739         ob_exit_replace();
740         break;
741
742     case ClientMessage:
743         if (e->xclient.format != 32) break;
744
745         msgtype = e->xclient.message_type;
746         if (msgtype == prop_atoms.net_current_desktop) {
747             guint d = e->xclient.data.l[0];
748             if (d < screen_num_desktops) {
749                 event_curtime = e->xclient.data.l[1];
750                 if (event_curtime == 0)
751                     ob_debug_type(OB_DEBUG_APP_BUGS,
752                                   "_NET_CURRENT_DESKTOP message is missing "
753                                   "a timestamp\n");
754                 screen_set_desktop(d, TRUE);
755             }
756         } else if (msgtype == prop_atoms.net_number_of_desktops) {
757             guint d = e->xclient.data.l[0];
758             if (d > 0 && d <= 1000)
759                 screen_set_num_desktops(d);
760         } else if (msgtype == prop_atoms.net_showing_desktop) {
761             screen_show_desktop(e->xclient.data.l[0] != 0, NULL);
762         } else if (msgtype == prop_atoms.ob_control) {
763             ob_debug("OB_CONTROL: %d\n", e->xclient.data.l[0]);
764             if (e->xclient.data.l[0] == 1)
765                 ob_reconfigure();
766             else if (e->xclient.data.l[0] == 2)
767                 ob_restart();
768             else if (e->xclient.data.l[0] == 3)
769                 ob_exit(0);
770         }
771         break;
772     case PropertyNotify:
773         if (e->xproperty.atom == prop_atoms.net_desktop_names) {
774             ob_debug("UPDATE DESKTOP NAMES\n");
775             screen_update_desktop_names();
776         }
777         else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
778             screen_update_layout();
779         break;
780     case ConfigureNotify:
781 #ifdef XRANDR
782         XRRUpdateConfiguration(e);
783 #endif
784         screen_resize();
785         break;
786     default:
787         ;
788     }
789 }
790
791 void event_enter_client(ObClient *client)
792 {
793     g_assert(config_focus_follow);
794
795     if (client_enter_focusable(client) && client_can_focus(client)) {
796         if (config_focus_delay) {
797             ObFocusDelayData *data;
798
799             ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
800
801             data = g_new(ObFocusDelayData, 1);
802             data->client = client;
803             data->time = event_curtime;
804             data->serial = event_curserial;
805
806             ob_main_loop_timeout_add(ob_main_loop,
807                                      config_focus_delay * 1000,
808                                      focus_delay_func,
809                                      data, focus_delay_cmp, focus_delay_dest);
810         } else {
811             ObFocusDelayData data;
812             data.client = client;
813             data.time = event_curtime;
814             data.serial = event_curserial;
815             focus_delay_func(&data);
816         }
817     }
818 }
819
820 static void event_handle_client(ObClient *client, XEvent *e)
821 {
822     XEvent ce;
823     Atom msgtype;
824     ObFrameContext con;
825     static gint px = -1, py = -1;
826     static guint pb = 0;
827
828     switch (e->type) {
829     case ButtonPress:
830         /* save where the press occured for the first button pressed */
831         if (!pb) {
832             pb = e->xbutton.button;
833             px = e->xbutton.x;
834             py = e->xbutton.y;
835         }
836     case ButtonRelease:
837         /* Wheel buttons don't draw because they are an instant click, so it
838            is a waste of resources to go drawing it.
839            if the user is doing an intereactive thing, or has a menu open then
840            the mouse is grabbed (possibly) and if we get these events we don't
841            want to deal with them
842         */
843         if (!(e->xbutton.button == 4 || e->xbutton.button == 5) &&
844             !grab_on_keyboard())
845         {
846             /* use where the press occured */
847             con = frame_context(client, e->xbutton.window, px, py);
848             con = mouse_button_frame_context(con, e->xbutton.button,
849                                              e->xbutton.state);
850
851             if (e->type == ButtonRelease && e->xbutton.button == pb)
852                 pb = 0, px = py = -1;
853
854             switch (con) {
855             case OB_FRAME_CONTEXT_MAXIMIZE:
856                 client->frame->max_press = (e->type == ButtonPress);
857                 frame_adjust_state(client->frame);
858                 break;
859             case OB_FRAME_CONTEXT_CLOSE:
860                 client->frame->close_press = (e->type == ButtonPress);
861                 frame_adjust_state(client->frame);
862                 break;
863             case OB_FRAME_CONTEXT_ICONIFY:
864                 client->frame->iconify_press = (e->type == ButtonPress);
865                 frame_adjust_state(client->frame);
866                 break;
867             case OB_FRAME_CONTEXT_ALLDESKTOPS:
868                 client->frame->desk_press = (e->type == ButtonPress);
869                 frame_adjust_state(client->frame);
870                 break;
871             case OB_FRAME_CONTEXT_SHADE:
872                 client->frame->shade_press = (e->type == ButtonPress);
873                 frame_adjust_state(client->frame);
874                 break;
875             default:
876                 /* nothing changes with clicks for any other contexts */
877                 break;
878             }
879         }
880         break;
881     case MotionNotify:
882         /* when there is a grab on the pointer, we won't get enter/leave
883            notifies, but we still get motion events */
884         if (grab_on_pointer()) break;
885
886         con = frame_context(client, e->xmotion.window,
887                             e->xmotion.x, e->xmotion.y);
888         switch (con) {
889         case OB_FRAME_CONTEXT_TITLEBAR:
890         case OB_FRAME_CONTEXT_TLCORNER:
891         case OB_FRAME_CONTEXT_TRCORNER:
892             /* we've left the button area inside the titlebar */
893             if (client->frame->max_hover || client->frame->desk_hover ||
894                 client->frame->shade_hover || client->frame->iconify_hover ||
895                 client->frame->close_hover)
896             {
897                 client->frame->max_hover = FALSE;
898                 client->frame->desk_hover = FALSE;
899                 client->frame->shade_hover = FALSE;
900                 client->frame->iconify_hover = FALSE;
901                 client->frame->close_hover = FALSE;
902                 frame_adjust_state(client->frame);
903             }
904             break;
905         case OB_FRAME_CONTEXT_MAXIMIZE:
906             if (!client->frame->max_hover) {
907                 client->frame->max_hover = TRUE;
908                 frame_adjust_state(client->frame);
909             }
910             break;
911         case OB_FRAME_CONTEXT_ALLDESKTOPS:
912             if (!client->frame->desk_hover) {
913                 client->frame->desk_hover = TRUE;
914                 frame_adjust_state(client->frame);
915             }
916             break;
917         case OB_FRAME_CONTEXT_SHADE:
918             if (!client->frame->shade_hover) {
919                 client->frame->shade_hover = TRUE;
920                 frame_adjust_state(client->frame);
921             }
922             break;
923         case OB_FRAME_CONTEXT_ICONIFY:
924             if (!client->frame->iconify_hover) {
925                 client->frame->iconify_hover = TRUE;
926                 frame_adjust_state(client->frame);
927             }
928             break;
929         case OB_FRAME_CONTEXT_CLOSE:
930             if (!client->frame->close_hover) {
931                 client->frame->close_hover = TRUE;
932                 frame_adjust_state(client->frame);
933             }
934             break;
935         default:
936             break;
937         }
938         break;
939     case LeaveNotify:
940         con = frame_context(client, e->xcrossing.window,
941                             e->xcrossing.x, e->xcrossing.y);
942         switch (con) {
943         case OB_FRAME_CONTEXT_TITLEBAR:
944         case OB_FRAME_CONTEXT_TLCORNER:
945         case OB_FRAME_CONTEXT_TRCORNER:
946             /* we've left the button area inside the titlebar */
947             if (client->frame->max_hover || client->frame->desk_hover ||
948                 client->frame->shade_hover || client->frame->iconify_hover ||
949                 client->frame->close_hover)
950             {
951                 client->frame->max_hover = FALSE;
952                 client->frame->desk_hover = FALSE;
953                 client->frame->shade_hover = FALSE;
954                 client->frame->iconify_hover = FALSE;
955                 client->frame->close_hover = FALSE;
956                 frame_adjust_state(client->frame);
957             }
958             break;
959         case OB_FRAME_CONTEXT_MAXIMIZE:
960             client->frame->max_hover = FALSE;
961             frame_adjust_state(client->frame);
962             break;
963         case OB_FRAME_CONTEXT_ALLDESKTOPS:
964             client->frame->desk_hover = FALSE;
965             frame_adjust_state(client->frame);
966             break;
967         case OB_FRAME_CONTEXT_SHADE:
968             client->frame->shade_hover = FALSE;
969             frame_adjust_state(client->frame);
970             break;
971         case OB_FRAME_CONTEXT_ICONIFY:
972             client->frame->iconify_hover = FALSE;
973             frame_adjust_state(client->frame);
974             break;
975         case OB_FRAME_CONTEXT_CLOSE:
976             client->frame->close_hover = FALSE;
977             frame_adjust_state(client->frame);
978             break;
979         case OB_FRAME_CONTEXT_FRAME:
980             /* When the mouse leaves an animating window, don't use the
981                corresponding enter events. Pretend like the animating window
982                doesn't even exist..! */
983             if (frame_iconify_animating(client->frame))
984                 event_end_ignore_all_enters(event_start_ignore_all_enters());
985
986             ob_debug_type(OB_DEBUG_FOCUS,
987                           "%sNotify mode %d detail %d on %lx\n",
988                           (e->type == EnterNotify ? "Enter" : "Leave"),
989                           e->xcrossing.mode,
990                           e->xcrossing.detail, (client?client->window:0));
991             if (grab_on_keyboard())
992                 break;
993             if (config_focus_follow && config_focus_delay &&
994                 /* leave inferior events can happen when the mouse goes onto
995                    the window's border and then into the window before the
996                    delay is up */
997                 e->xcrossing.detail != NotifyInferior)
998             {
999                 ob_main_loop_timeout_remove_data(ob_main_loop,
1000                                                  focus_delay_func,
1001                                                  client, FALSE);
1002             }
1003             break;
1004         default:
1005             break;
1006         }
1007         break;
1008     case EnterNotify:
1009     {
1010         con = frame_context(client, e->xcrossing.window,
1011                             e->xcrossing.x, e->xcrossing.y);
1012         switch (con) {
1013         case OB_FRAME_CONTEXT_MAXIMIZE:
1014             client->frame->max_hover = TRUE;
1015             frame_adjust_state(client->frame);
1016             break;
1017         case OB_FRAME_CONTEXT_ALLDESKTOPS:
1018             client->frame->desk_hover = TRUE;
1019             frame_adjust_state(client->frame);
1020             break;
1021         case OB_FRAME_CONTEXT_SHADE:
1022             client->frame->shade_hover = TRUE;
1023             frame_adjust_state(client->frame);
1024             break;
1025         case OB_FRAME_CONTEXT_ICONIFY:
1026             client->frame->iconify_hover = TRUE;
1027             frame_adjust_state(client->frame);
1028             break;
1029         case OB_FRAME_CONTEXT_CLOSE:
1030             client->frame->close_hover = TRUE;
1031             frame_adjust_state(client->frame);
1032             break;
1033         case OB_FRAME_CONTEXT_FRAME:
1034             if (grab_on_keyboard())
1035                 break;
1036             if (e->xcrossing.mode == NotifyGrab ||
1037                 e->xcrossing.mode == NotifyUngrab ||
1038                 /*ignore enters when we're already in the window */
1039                 e->xcrossing.detail == NotifyInferior ||
1040                 is_enter_focus_event_ignored(e))
1041             {
1042                 ob_debug_type(OB_DEBUG_FOCUS,
1043                               "%sNotify mode %d detail %d serial %lu on %lx "
1044                               "IGNORED\n",
1045                               (e->type == EnterNotify ? "Enter" : "Leave"),
1046                               e->xcrossing.mode,
1047                               e->xcrossing.detail,
1048                               e->xcrossing.serial,
1049                               client?client->window:0);
1050             }
1051             else {
1052                 ob_debug_type(OB_DEBUG_FOCUS,
1053                               "%sNotify mode %d detail %d serial %lu on %lx, "
1054                               "focusing window\n",
1055                               (e->type == EnterNotify ? "Enter" : "Leave"),
1056                               e->xcrossing.mode,
1057                               e->xcrossing.detail,
1058                               e->xcrossing.serial,
1059                               (client?client->window:0));
1060                 if (config_focus_follow)
1061                     event_enter_client(client);
1062             }
1063             break;
1064         default:
1065             break;
1066         }
1067         break;
1068     }
1069     case ConfigureRequest:
1070     {
1071         /* dont compress these unless you're going to watch for property
1072            notifies in between (these can change what the configure would
1073            do to the window).
1074            also you can't compress stacking events
1075         */
1076
1077         gint x, y, w, h;
1078         gboolean move = FALSE;
1079         gboolean resize = FALSE;
1080
1081         /* get the current area */
1082         RECT_TO_DIMS(client->area, x, y, w, h);
1083
1084         ob_debug("ConfigureRequest for \"%s\" desktop %d wmstate %d "
1085                  "visibile %d\n"
1086                  "                     x %d y %d w %d h %d b %d\n",
1087                  client->title,
1088                  screen_desktop, client->wmstate, client->frame->visible,
1089                  x, y, w, h, client->border_width);
1090
1091         if (e->xconfigurerequest.value_mask & CWBorderWidth)
1092             if (client->border_width != e->xconfigurerequest.border_width) {
1093                 client->border_width = e->xconfigurerequest.border_width;
1094
1095                 /* if the border width is changing then that is the same
1096                    as requesting a resize, but we don't actually change
1097                    the client's border, so it will change their root
1098                    coordiantes (since they include the border width) and
1099                    we need to a notify then */
1100                 move = TRUE;
1101             }
1102
1103
1104         if (e->xconfigurerequest.value_mask & CWStackMode) {
1105             ObClient *sibling = NULL;
1106             gulong ignore_start;
1107             gboolean ok = TRUE;
1108
1109             /* get the sibling */
1110             if (e->xconfigurerequest.value_mask & CWSibling) {
1111                 ObWindow *win;
1112                 win = g_hash_table_lookup(window_map,
1113                                           &e->xconfigurerequest.above);
1114                 if (win && WINDOW_IS_CLIENT(win) &&
1115                     WINDOW_AS_CLIENT(win) != client)
1116                 {
1117                     sibling = WINDOW_AS_CLIENT(win);
1118                 }
1119                 else
1120                     /* an invalid sibling was specified so don't restack at
1121                        all, it won't make sense no matter what we do */
1122                     ok = FALSE;
1123             }
1124
1125             if (ok) {
1126                 if (!config_focus_under_mouse)
1127                     ignore_start = event_start_ignore_all_enters();
1128                 stacking_restack_request(client, sibling,
1129                                          e->xconfigurerequest.detail);
1130                 if (!config_focus_under_mouse)
1131                     event_end_ignore_all_enters(ignore_start);
1132             }
1133
1134             /* a stacking change moves the window without resizing */
1135             move = TRUE;
1136         }
1137
1138         if ((e->xconfigurerequest.value_mask & CWX) ||
1139             (e->xconfigurerequest.value_mask & CWY) ||
1140             (e->xconfigurerequest.value_mask & CWWidth) ||
1141             (e->xconfigurerequest.value_mask & CWHeight))
1142         {
1143             if (e->xconfigurerequest.value_mask & CWX) {
1144                 /* don't allow clients to move shaded windows (fvwm does this)
1145                  */
1146                 if (!client->shaded)
1147                     x = e->xconfigurerequest.x;
1148                 move = TRUE;
1149             }
1150             if (e->xconfigurerequest.value_mask & CWY) {
1151                 /* don't allow clients to move shaded windows (fvwm does this)
1152                  */
1153                 if (!client->shaded)
1154                     y = e->xconfigurerequest.y;
1155                 move = TRUE;
1156             }
1157
1158             if (e->xconfigurerequest.value_mask & CWWidth) {
1159                 w = e->xconfigurerequest.width;
1160                 resize = TRUE;
1161             }
1162             if (e->xconfigurerequest.value_mask & CWHeight) {
1163                 h = e->xconfigurerequest.height;
1164                 resize = TRUE;
1165             }
1166         }
1167
1168         ob_debug("ConfigureRequest x(%d) %d y(%d) %d w(%d) %d h(%d) %d "
1169                  "move %d resize %d\n",
1170                  e->xconfigurerequest.value_mask & CWX, x,
1171                  e->xconfigurerequest.value_mask & CWY, y,
1172                  e->xconfigurerequest.value_mask & CWWidth, w,
1173                  e->xconfigurerequest.value_mask & CWHeight, h,
1174                  move, resize);
1175
1176         /* check for broken apps moving to their root position
1177
1178            XXX remove this some day...that would be nice. right now all
1179            kde apps do this when they try activate themselves on another
1180            desktop. eg. open amarok window on desktop 1, switch to desktop
1181            2, click amarok tray icon. it will move by its decoration size.
1182         */
1183         if (x != client->area.x &&
1184             x == (client->frame->area.x + client->frame->size.left -
1185                   (gint)client->border_width) &&
1186             y != client->area.y &&
1187             y == (client->frame->area.y + client->frame->size.top -
1188                   (gint)client->border_width) &&
1189             w == client->area.width &&
1190             h == client->area.height)
1191         {
1192             ob_debug_type(OB_DEBUG_APP_BUGS,
1193                           "Application %s is trying to move via "
1194                           "ConfigureRequest to it's root window position "
1195                           "but it is not using StaticGravity\n",
1196                           client->title);
1197             /* don't move it */
1198             x = client->area.x;
1199             y = client->area.y;
1200
1201             /* they still requested a move, so don't change whether a
1202                notify is sent or not */
1203         }
1204
1205         {
1206             gint lw,lh;
1207
1208             client_try_configure(client, &x, &y, &w, &h, &lw, &lh, FALSE);
1209
1210             /* if x was not given, then use gravity to figure out the new
1211                x.  the reference point should not be moved */
1212             if ((e->xconfigurerequest.value_mask & CWWidth &&
1213                  !(e->xconfigurerequest.value_mask & CWX)))
1214                 client_gravity_resize_w(client, &x, client->area.width, w);
1215             /* if y was not given, then use gravity to figure out the new
1216                y.  the reference point should not be moved */
1217             if ((e->xconfigurerequest.value_mask & CWHeight &&
1218                  !(e->xconfigurerequest.value_mask & CWY)))
1219                 client_gravity_resize_h(client, &y, client->area.height,h);
1220
1221             client_find_onscreen(client, &x, &y, w, h, FALSE);
1222
1223             ob_debug("Granting ConfigureRequest x %d y %d w %d h %d\n",
1224                      x, y, w, h);
1225             client_configure(client, x, y, w, h, FALSE, TRUE, TRUE);
1226         }
1227         break;
1228     }
1229     case UnmapNotify:
1230         if (client->ignore_unmaps) {
1231             client->ignore_unmaps--;
1232             break;
1233         }
1234         ob_debug("UnmapNotify for window 0x%x eventwin 0x%x sendevent %d "
1235                  "ignores left %d\n",
1236                  client->window, e->xunmap.event, e->xunmap.from_configure,
1237                  client->ignore_unmaps);
1238         client_unmanage(client);
1239         break;
1240     case DestroyNotify:
1241         ob_debug("DestroyNotify for window 0x%x\n", client->window);
1242         client_unmanage(client);
1243         break;
1244     case ReparentNotify:
1245         /* this is when the client is first taken captive in the frame */
1246         if (e->xreparent.parent == client->frame->window) break;
1247
1248         /*
1249           This event is quite rare and is usually handled in unmapHandler.
1250           However, if the window is unmapped when the reparent event occurs,
1251           the window manager never sees it because an unmap event is not sent
1252           to an already unmapped window.
1253         */
1254
1255         /* we don't want the reparent event, put it back on the stack for the
1256            X server to deal with after we unmanage the window */
1257         XPutBackEvent(ob_display, e);
1258
1259         ob_debug("ReparentNotify for window 0x%x\n", client->window);
1260         client_unmanage(client);
1261         break;
1262     case MapRequest:
1263         ob_debug("MapRequest for 0x%lx\n", client->window);
1264         if (!client->iconic) break; /* this normally doesn't happen, but if it
1265                                        does, we don't want it!
1266                                        it can happen now when the window is on
1267                                        another desktop, but we still don't
1268                                        want it! */
1269         client_activate(client, FALSE, TRUE, TRUE, TRUE);
1270         break;
1271     case ClientMessage:
1272         /* validate cuz we query stuff off the client here */
1273         if (!client_validate(client)) break;
1274
1275         if (e->xclient.format != 32) return;
1276
1277         msgtype = e->xclient.message_type;
1278         if (msgtype == prop_atoms.wm_change_state) {
1279             /* compress changes into a single change */
1280             while (XCheckTypedWindowEvent(ob_display, client->window,
1281                                           e->type, &ce)) {
1282                 /* XXX: it would be nice to compress ALL messages of a
1283                    type, not just messages in a row without other
1284                    message types between. */
1285                 if (ce.xclient.message_type != msgtype) {
1286                     XPutBackEvent(ob_display, &ce);
1287                     break;
1288                 }
1289                 e->xclient = ce.xclient;
1290             }
1291             client_set_wm_state(client, e->xclient.data.l[0]);
1292         } else if (msgtype == prop_atoms.net_wm_desktop) {
1293             /* compress changes into a single change */
1294             while (XCheckTypedWindowEvent(ob_display, client->window,
1295                                           e->type, &ce)) {
1296                 /* XXX: it would be nice to compress ALL messages of a
1297                    type, not just messages in a row without other
1298                    message types between. */
1299                 if (ce.xclient.message_type != msgtype) {
1300                     XPutBackEvent(ob_display, &ce);
1301                     break;
1302                 }
1303                 e->xclient = ce.xclient;
1304             }
1305             if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
1306                 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
1307                 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
1308                                    FALSE, FALSE);
1309         } else if (msgtype == prop_atoms.net_wm_state) {
1310             gulong ignore_start;
1311
1312             /* can't compress these */
1313             ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
1314                      (e->xclient.data.l[0] == 0 ? "Remove" :
1315                       e->xclient.data.l[0] == 1 ? "Add" :
1316                       e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
1317                      e->xclient.data.l[1], e->xclient.data.l[2],
1318                      client->window);
1319
1320             /* ignore enter events caused by these like ob actions do */
1321             if (!config_focus_under_mouse)
1322                 ignore_start = event_start_ignore_all_enters();
1323             client_set_state(client, e->xclient.data.l[0],
1324                              e->xclient.data.l[1], e->xclient.data.l[2]);
1325             if (!config_focus_under_mouse)
1326                 event_end_ignore_all_enters(ignore_start);
1327         } else if (msgtype == prop_atoms.net_close_window) {
1328             ob_debug("net_close_window for 0x%lx\n", client->window);
1329             client_close(client);
1330         } else if (msgtype == prop_atoms.net_active_window) {
1331             ob_debug("net_active_window for 0x%lx source=%s\n",
1332                      client->window,
1333                      (e->xclient.data.l[0] == 0 ? "unknown" :
1334                       (e->xclient.data.l[0] == 1 ? "application" :
1335                        (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1336             /* XXX make use of data.l[2] !? */
1337             if (e->xclient.data.l[0] == 1 || e->xclient.data.l[0] == 2) {
1338                 /* don't use the user's timestamp for client_focus, cuz if it's
1339                    an old broken timestamp (happens all the time) then focus
1340                    won't move even though we're trying to move it
1341                   event_curtime = e->xclient.data.l[1];*/
1342                 if (e->xclient.data.l[1] == 0)
1343                     ob_debug_type(OB_DEBUG_APP_BUGS,
1344                                   "_NET_ACTIVE_WINDOW message for window %s is"
1345                                   " missing a timestamp\n", client->title);
1346             } else
1347                 ob_debug_type(OB_DEBUG_APP_BUGS,
1348                               "_NET_ACTIVE_WINDOW message for window %s is "
1349                               "missing source indication\n");
1350             client_activate(client, FALSE, TRUE, TRUE,
1351                             (e->xclient.data.l[0] == 0 ||
1352                              e->xclient.data.l[0] == 2));
1353         } else if (msgtype == prop_atoms.net_wm_moveresize) {
1354             ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1355                      client->window, e->xclient.data.l[2]);
1356             if ((Atom)e->xclient.data.l[2] ==
1357                 prop_atoms.net_wm_moveresize_size_topleft ||
1358                 (Atom)e->xclient.data.l[2] ==
1359                 prop_atoms.net_wm_moveresize_size_top ||
1360                 (Atom)e->xclient.data.l[2] ==
1361                 prop_atoms.net_wm_moveresize_size_topright ||
1362                 (Atom)e->xclient.data.l[2] ==
1363                 prop_atoms.net_wm_moveresize_size_right ||
1364                 (Atom)e->xclient.data.l[2] ==
1365                 prop_atoms.net_wm_moveresize_size_right ||
1366                 (Atom)e->xclient.data.l[2] ==
1367                 prop_atoms.net_wm_moveresize_size_bottomright ||
1368                 (Atom)e->xclient.data.l[2] ==
1369                 prop_atoms.net_wm_moveresize_size_bottom ||
1370                 (Atom)e->xclient.data.l[2] ==
1371                 prop_atoms.net_wm_moveresize_size_bottomleft ||
1372                 (Atom)e->xclient.data.l[2] ==
1373                 prop_atoms.net_wm_moveresize_size_left ||
1374                 (Atom)e->xclient.data.l[2] ==
1375                 prop_atoms.net_wm_moveresize_move ||
1376                 (Atom)e->xclient.data.l[2] ==
1377                 prop_atoms.net_wm_moveresize_size_keyboard ||
1378                 (Atom)e->xclient.data.l[2] ==
1379                 prop_atoms.net_wm_moveresize_move_keyboard) {
1380
1381                 moveresize_start(client, e->xclient.data.l[0],
1382                                  e->xclient.data.l[1], e->xclient.data.l[3],
1383                                  e->xclient.data.l[2]);
1384             }
1385             else if ((Atom)e->xclient.data.l[2] ==
1386                      prop_atoms.net_wm_moveresize_cancel)
1387                 moveresize_end(TRUE);
1388         } else if (msgtype == prop_atoms.net_moveresize_window) {
1389             gint ograv, x, y, w, h;
1390
1391             ograv = client->gravity;
1392
1393             if (e->xclient.data.l[0] & 0xff)
1394                 client->gravity = e->xclient.data.l[0] & 0xff;
1395
1396             if (e->xclient.data.l[0] & 1 << 8)
1397                 x = e->xclient.data.l[1];
1398             else
1399                 x = client->area.x;
1400             if (e->xclient.data.l[0] & 1 << 9)
1401                 y = e->xclient.data.l[2];
1402             else
1403                 y = client->area.y;
1404
1405             if (e->xclient.data.l[0] & 1 << 10) {
1406                 w = e->xclient.data.l[3];
1407
1408                 /* if x was not given, then use gravity to figure out the new
1409                    x.  the reference point should not be moved */
1410                 if (!(e->xclient.data.l[0] & 1 << 8))
1411                     client_gravity_resize_w(client, &x, client->area.width, w);
1412             }
1413             else
1414                 w = client->area.width;
1415
1416             if (e->xclient.data.l[0] & 1 << 11) {
1417                 h = e->xclient.data.l[4];
1418
1419                 /* if y was not given, then use gravity to figure out the new
1420                    y.  the reference point should not be moved */
1421                 if (!(e->xclient.data.l[0] & 1 << 9))
1422                     client_gravity_resize_h(client, &y, client->area.height,h);
1423             }
1424             else
1425                 h = client->area.height;
1426
1427             ob_debug("MOVERESIZE x %d %d y %d %d (gravity %d)\n",
1428                      e->xclient.data.l[0] & 1 << 8, x,
1429                      e->xclient.data.l[0] & 1 << 9, y,
1430                      client->gravity);
1431
1432             client_find_onscreen(client, &x, &y, w, h, FALSE);
1433
1434             client_configure(client, x, y, w, h, FALSE, TRUE, FALSE);
1435
1436             client->gravity = ograv;
1437         } else if (msgtype == prop_atoms.net_restack_window) {
1438             if (e->xclient.data.l[0] != 2) {
1439                 ob_debug_type(OB_DEBUG_APP_BUGS,
1440                               "_NET_RESTACK_WINDOW sent for window %s with "
1441                               "invalid source indication %ld\n",
1442                               client->title, e->xclient.data.l[0]);
1443             } else {
1444                 ObClient *sibling = NULL;
1445                 if (e->xclient.data.l[1]) {
1446                     ObWindow *win = g_hash_table_lookup
1447                         (window_map, &e->xclient.data.l[1]);
1448                     if (WINDOW_IS_CLIENT(win) &&
1449                         WINDOW_AS_CLIENT(win) != client)
1450                     {
1451                         sibling = WINDOW_AS_CLIENT(win);
1452                     }
1453                     if (sibling == NULL)
1454                         ob_debug_type(OB_DEBUG_APP_BUGS,
1455                                       "_NET_RESTACK_WINDOW sent for window %s "
1456                                       "with invalid sibling 0x%x\n",
1457                                  client->title, e->xclient.data.l[1]);
1458                 }
1459                 if (e->xclient.data.l[2] == Below ||
1460                     e->xclient.data.l[2] == BottomIf ||
1461                     e->xclient.data.l[2] == Above ||
1462                     e->xclient.data.l[2] == TopIf ||
1463                     e->xclient.data.l[2] == Opposite)
1464                 {
1465                     gulong ignore_start;
1466
1467                     if (!config_focus_under_mouse)
1468                         ignore_start = event_start_ignore_all_enters();
1469                     /* just raise, don't activate */
1470                     stacking_restack_request(client, sibling,
1471                                              e->xclient.data.l[2]);
1472                     if (!config_focus_under_mouse)
1473                         event_end_ignore_all_enters(ignore_start);
1474
1475                     /* send a synthetic ConfigureNotify, cuz this is supposed
1476                        to be like a ConfigureRequest. */
1477                     client_reconfigure(client, TRUE);
1478                 } else
1479                     ob_debug_type(OB_DEBUG_APP_BUGS,
1480                                   "_NET_RESTACK_WINDOW sent for window %s "
1481                                   "with invalid detail %d\n",
1482                                   client->title, e->xclient.data.l[2]);
1483             }
1484         }
1485         break;
1486     case PropertyNotify:
1487         /* validate cuz we query stuff off the client here */
1488         if (!client_validate(client)) break;
1489
1490         /* compress changes to a single property into a single change */
1491         while (XCheckTypedWindowEvent(ob_display, client->window,
1492                                       e->type, &ce)) {
1493             Atom a, b;
1494
1495             /* XXX: it would be nice to compress ALL changes to a property,
1496                not just changes in a row without other props between. */
1497
1498             a = ce.xproperty.atom;
1499             b = e->xproperty.atom;
1500
1501             if (a == b)
1502                 continue;
1503             if ((a == prop_atoms.net_wm_name ||
1504                  a == prop_atoms.wm_name ||
1505                  a == prop_atoms.net_wm_icon_name ||
1506                  a == prop_atoms.wm_icon_name)
1507                 &&
1508                 (b == prop_atoms.net_wm_name ||
1509                  b == prop_atoms.wm_name ||
1510                  b == prop_atoms.net_wm_icon_name ||
1511                  b == prop_atoms.wm_icon_name)) {
1512                 continue;
1513             }
1514             if (a == prop_atoms.net_wm_icon &&
1515                 b == prop_atoms.net_wm_icon)
1516                 continue;
1517
1518             XPutBackEvent(ob_display, &ce);
1519             break;
1520         }
1521
1522         msgtype = e->xproperty.atom;
1523         if (msgtype == XA_WM_NORMAL_HINTS) {
1524             ob_debug("Update NORMAL hints\n");
1525             client_update_normal_hints(client);
1526             /* normal hints can make a window non-resizable */
1527             client_setup_decor_and_functions(client, FALSE);
1528
1529             /* make sure the client's sizes are within its bounds, but only
1530                reconfigure the window if it needs to. emacs will update its
1531                normal hints every time it receives a conigurenotify */
1532             client_reconfigure(client, FALSE);
1533         } else if (msgtype == XA_WM_HINTS) {
1534             client_update_wmhints(client);
1535         } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1536             client_update_transient_for(client);
1537             client_get_type_and_transientness(client);
1538             /* type may have changed, so update the layer */
1539             client_calc_layer(client);
1540             client_setup_decor_and_functions(client, TRUE);
1541         } else if (msgtype == prop_atoms.net_wm_name ||
1542                    msgtype == prop_atoms.wm_name ||
1543                    msgtype == prop_atoms.net_wm_icon_name ||
1544                    msgtype == prop_atoms.wm_icon_name) {
1545             client_update_title(client);
1546         } else if (msgtype == prop_atoms.wm_protocols) {
1547             client_update_protocols(client);
1548             client_setup_decor_and_functions(client, TRUE);
1549         }
1550         else if (msgtype == prop_atoms.net_wm_strut) {
1551             client_update_strut(client);
1552         }
1553         else if (msgtype == prop_atoms.net_wm_strut_partial) {
1554             client_update_strut(client);
1555         }
1556         else if (msgtype == prop_atoms.net_wm_icon) {
1557             client_update_icons(client);
1558         }
1559         else if (msgtype == prop_atoms.net_wm_icon_geometry) {
1560             client_update_icon_geometry(client);
1561         }
1562         else if (msgtype == prop_atoms.net_wm_user_time) {
1563             guint32 t;
1564             if (client == focus_client &&
1565                 PROP_GET32(client->window, net_wm_user_time, cardinal, &t) &&
1566                 t && !event_time_after(t, e->xproperty.time) &&
1567                 (!event_last_user_time ||
1568                  event_time_after(t, event_last_user_time)))
1569             {
1570                 event_last_user_time = t;
1571             }
1572         }
1573 #ifdef SYNC
1574         else if (msgtype == prop_atoms.net_wm_sync_request_counter) {
1575             client_update_sync_request_counter(client);
1576         }
1577 #endif
1578         break;
1579     case ColormapNotify:
1580         client_update_colormap(client, e->xcolormap.colormap);
1581         break;
1582     default:
1583         ;
1584 #ifdef SHAPE
1585         if (extensions_shape && e->type == extensions_shape_event_basep) {
1586             client->shaped = ((XShapeEvent*)e)->shaped;
1587             frame_adjust_shape(client->frame);
1588         }
1589 #endif
1590     }
1591 }
1592
1593 static void event_handle_dock(ObDock *s, XEvent *e)
1594 {
1595     switch (e->type) {
1596     case ButtonPress:
1597         if (e->xbutton.button == 1)
1598             stacking_raise(DOCK_AS_WINDOW(s));
1599         else if (e->xbutton.button == 2)
1600             stacking_lower(DOCK_AS_WINDOW(s));
1601         break;
1602     case EnterNotify:
1603         dock_hide(FALSE);
1604         break;
1605     case LeaveNotify:
1606         /* don't hide when moving into a dock app */
1607         if (e->xcrossing.detail != NotifyInferior)
1608             dock_hide(TRUE);
1609         break;
1610     }
1611 }
1612
1613 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1614 {
1615     switch (e->type) {
1616     case MotionNotify:
1617         dock_app_drag(app, &e->xmotion);
1618         break;
1619     case UnmapNotify:
1620         if (app->ignore_unmaps) {
1621             app->ignore_unmaps--;
1622             break;
1623         }
1624         dock_remove(app, TRUE);
1625         break;
1626     case DestroyNotify:
1627         dock_remove(app, FALSE);
1628         break;
1629     case ReparentNotify:
1630         dock_remove(app, FALSE);
1631         break;
1632     case ConfigureNotify:
1633         dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1634         break;
1635     }
1636 }
1637
1638 static ObMenuFrame* find_active_menu(void)
1639 {
1640     GList *it;
1641     ObMenuFrame *ret = NULL;
1642
1643     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1644         ret = it->data;
1645         if (ret->selected)
1646             break;
1647         ret = NULL;
1648     }
1649     return ret;
1650 }
1651
1652 static ObMenuFrame* find_active_or_last_menu(void)
1653 {
1654     ObMenuFrame *ret = NULL;
1655
1656     ret = find_active_menu();
1657     if (!ret && menu_frame_visible)
1658         ret = menu_frame_visible->data;
1659     return ret;
1660 }
1661
1662 static gboolean event_handle_menu_keyboard(XEvent *ev)
1663 {
1664     guint keycode, state;
1665     gunichar unikey;
1666     ObMenuFrame *frame;
1667     gboolean ret = FALSE;
1668
1669     keycode = ev->xkey.keycode;
1670     state = ev->xkey.state;
1671     unikey = translate_unichar(keycode);
1672
1673     frame = find_active_or_last_menu();
1674     if (frame == NULL)
1675         g_assert_not_reached(); /* there is no active menu */
1676
1677     /* Allow control while going thru the menu */
1678     else if (ev->type == KeyPress && (state & ~ControlMask) == 0) {
1679         frame->got_press = TRUE;
1680
1681         if (keycode == ob_keycode(OB_KEY_ESCAPE)) {
1682             menu_frame_hide_all();
1683             ret = TRUE;
1684         }
1685
1686         else if (keycode == ob_keycode(OB_KEY_LEFT)) {
1687             /* Left goes to the parent menu */
1688             menu_frame_select(frame, NULL, TRUE);
1689             ret = TRUE;
1690         }
1691
1692         else if (keycode == ob_keycode(OB_KEY_RIGHT)) {
1693             /* Right goes to the selected submenu */
1694             if (frame->child) menu_frame_select_next(frame->child);
1695             ret = TRUE;
1696         }
1697
1698         else if (keycode == ob_keycode(OB_KEY_UP)) {
1699             menu_frame_select_previous(frame);
1700             ret = TRUE;
1701         }
1702
1703         else if (keycode == ob_keycode(OB_KEY_DOWN)) {
1704             menu_frame_select_next(frame);
1705             ret = TRUE;
1706         }
1707     }
1708
1709     /* Use KeyRelease events for running things so that the key release doesn't
1710        get sent to the focused application.
1711
1712        Allow ControlMask only, and don't bother if the menu is empty */
1713     else if (ev->type == KeyRelease && (state & ~ControlMask) == 0 &&
1714              frame->entries && frame->got_press)
1715     {
1716         if (keycode == ob_keycode(OB_KEY_RETURN)) {
1717             /* Enter runs the active item or goes into the submenu.
1718                Control-Enter runs it without closing the menu. */
1719             if (frame->child)
1720                 menu_frame_select_next(frame->child);
1721             else if (frame->selected)
1722                 menu_entry_frame_execute(frame->selected, state);
1723
1724             ret = TRUE;
1725         }
1726
1727         /* keyboard accelerator shortcuts. (if it was a valid key) */
1728         else if (unikey != 0) {
1729             GList *start;
1730             GList *it;
1731             ObMenuEntryFrame *found = NULL;
1732             guint num_found = 0;
1733
1734             /* start after the selected one */
1735             start = frame->entries;
1736             if (frame->selected) {
1737                 for (it = start; frame->selected != it->data;
1738                      it = g_list_next(it))
1739                     g_assert(it != NULL); /* nothing was selected? */
1740                 /* next with wraparound */
1741                 start = g_list_next(it);
1742                 if (start == NULL) start = frame->entries;
1743             }
1744
1745             it = start;
1746             do {
1747                 ObMenuEntryFrame *e = it->data;
1748                 gunichar entrykey = 0;
1749
1750                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1751                     entrykey = e->entry->data.normal.shortcut;
1752                 else if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1753                     entrykey = e->entry->data.submenu.submenu->shortcut;
1754
1755                 if (unikey == entrykey) {
1756                     if (found == NULL) found = e;
1757                     ++num_found;
1758                 }
1759
1760                 /* next with wraparound */
1761                 it = g_list_next(it);
1762                 if (it == NULL) it = frame->entries;
1763             } while (it != start);
1764
1765             if (found) {
1766                 if (found->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1767                     num_found == 1)
1768                 {
1769                     menu_frame_select(frame, found, TRUE);
1770                     usleep(50000); /* highlight the item for a short bit so the
1771                                       user can see what happened */
1772                     menu_entry_frame_execute(found, state);
1773                 } else {
1774                     menu_frame_select(frame, found, TRUE);
1775                     if (num_found == 1)
1776                         menu_frame_select_next(frame->child);
1777                 }
1778
1779                 ret = TRUE;
1780             }
1781         }
1782     }
1783
1784     return ret;
1785 }
1786
1787 static gboolean event_handle_menu(XEvent *ev)
1788 {
1789     ObMenuFrame *f;
1790     ObMenuEntryFrame *e;
1791     gboolean ret = TRUE;
1792
1793     switch (ev->type) {
1794     case ButtonRelease:
1795         if (menu_hide_delay_reached() &&
1796             (ev->xbutton.button < 4 || ev->xbutton.button > 5))
1797         {
1798             if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1799                                             ev->xbutton.y_root)))
1800             {
1801                 menu_frame_select(e->frame, e, TRUE);
1802                 menu_entry_frame_execute(e, ev->xbutton.state);
1803             }
1804             else
1805                 menu_frame_hide_all();
1806         }
1807         break;
1808     case EnterNotify:
1809         if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window))) {
1810             if (e->ignore_enters)
1811                 --e->ignore_enters;
1812             else if (!(f = find_active_menu()) ||
1813                      f == e->frame ||
1814                      f->parent == e->frame ||
1815                      f->child == e->frame)
1816                 menu_frame_select(e->frame, e, FALSE);
1817         }
1818         break;
1819     case LeaveNotify:
1820         /*ignore leaves when we're already in the window */
1821         if (ev->xcrossing.detail == NotifyInferior)
1822             break;
1823
1824         if ((e = g_hash_table_lookup(menu_frame_map, &ev->xcrossing.window)) &&
1825             (f = find_active_menu()) && f->selected == e &&
1826             e->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1827         {
1828             menu_frame_select(e->frame, NULL, FALSE);
1829         }
1830         break;
1831     case MotionNotify:
1832         if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1833                                         ev->xmotion.y_root)))
1834             if (!(f = find_active_menu()) ||
1835                 f == e->frame ||
1836                 f->parent == e->frame ||
1837                 f->child == e->frame)
1838                 menu_frame_select(e->frame, e, FALSE);
1839         break;
1840     case KeyPress:
1841     case KeyRelease:
1842         ret = event_handle_menu_keyboard(ev);
1843         break;
1844     }
1845     return ret;
1846 }
1847
1848 static void event_handle_user_input(ObClient *client, XEvent *e)
1849 {
1850     g_assert(e->type == ButtonPress || e->type == ButtonRelease ||
1851              e->type == MotionNotify || e->type == KeyPress ||
1852              e->type == KeyRelease);
1853
1854     if (menu_frame_visible) {
1855         if (event_handle_menu(e))
1856             /* don't use the event if the menu used it, but if the menu
1857                didn't use it and it's a keypress that is bound, it will
1858                close the menu and be used */
1859             return;
1860     }
1861
1862     /* if the keyboard interactive action uses the event then dont
1863        use it for bindings. likewise is moveresize uses the event. */
1864     if (!actions_interactive_input_event(e) && !moveresize_event(e)) {
1865         if (moveresize_in_progress)
1866             /* make further actions work on the client being
1867                moved/resized */
1868             client = moveresize_client;
1869
1870         if (e->type == ButtonPress ||
1871             e->type == ButtonRelease ||
1872             e->type == MotionNotify)
1873         {
1874             /* the frame may not be "visible" but they can still click on it
1875                in the case where it is animating before disappearing */
1876             if (!client || !frame_iconify_animating(client->frame))
1877                 mouse_event(client, e);
1878         } else
1879             keyboard_event((focus_cycle_target ? focus_cycle_target :
1880                             (client ? client : focus_client)), e);
1881     }
1882 }
1883
1884 static void focus_delay_dest(gpointer data)
1885 {
1886     g_free(data);
1887 }
1888
1889 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2)
1890 {
1891     const ObFocusDelayData *f1 = d1;
1892     return f1->client == d2;
1893 }
1894
1895 static gboolean focus_delay_func(gpointer data)
1896 {
1897     ObFocusDelayData *d = data;
1898     Time old = event_curtime;
1899
1900     /* don't move focus and kill the menu or the move/resize */
1901     if (menu_frame_visible || moveresize_in_progress) return FALSE;
1902
1903     event_curtime = d->time;
1904     event_curserial = d->serial;
1905     if (client_focus(d->client) && config_focus_raise)
1906         stacking_raise(CLIENT_AS_WINDOW(d->client));
1907     event_curtime = old;
1908     return FALSE; /* no repeat */
1909 }
1910
1911 static void focus_delay_client_dest(ObClient *client, gpointer data)
1912 {
1913     ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1914                                      client, FALSE);
1915 }
1916
1917 void event_halt_focus_delay()
1918 {
1919     /* ignore all enter events up till the event which caused this to occur */
1920     if (event_curserial) event_ignore_enter_range(1, event_curserial);
1921     ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1922 }
1923
1924 gulong event_start_ignore_all_enters(void)
1925 {
1926     XSync(ob_display, FALSE);
1927     return LastKnownRequestProcessed(ob_display);
1928 }
1929
1930 static void event_ignore_enter_range(gulong start, gulong end)
1931 {
1932     ObSerialRange *r;
1933
1934     g_assert(start != 0);
1935     g_assert(end != 0);
1936
1937     r = g_new(ObSerialRange, 1);
1938     r->start = start;
1939     r->end = end;
1940     ignore_serials = g_slist_prepend(ignore_serials, r);
1941
1942     ob_debug_type(OB_DEBUG_FOCUS, "ignoring enters from %lu until %lu\n",
1943                   r->start, r->end);
1944
1945     /* increment the serial so we don't ignore events we weren't meant to */
1946     XSync(ob_display, FALSE);
1947 }
1948
1949 void event_end_ignore_all_enters(gulong start)
1950 {
1951     XSync(ob_display, FALSE);
1952     event_ignore_enter_range(start, LastKnownRequestProcessed(ob_display));
1953 }
1954
1955 static gboolean is_enter_focus_event_ignored(XEvent *e)
1956 {
1957     GSList *it, *next;
1958
1959     g_assert(e->type == EnterNotify &&
1960              !(e->xcrossing.mode == NotifyGrab ||
1961                e->xcrossing.mode == NotifyUngrab ||
1962                e->xcrossing.detail == NotifyInferior));
1963
1964     for (it = ignore_serials; it; it = next) {
1965         ObSerialRange *r = it->data;
1966
1967         next = g_slist_next(it);
1968
1969         if ((glong)(e->xany.serial - r->end) > 0) {
1970             /* past the end */
1971             ignore_serials = g_slist_delete_link(ignore_serials, it);
1972             g_free(r);
1973         }
1974         else if ((glong)(e->xany.serial - r->start) >= 0)
1975             return TRUE;
1976     }
1977     return FALSE;
1978 }
1979
1980 void event_cancel_all_key_grabs(void)
1981 {
1982     if (actions_interactive_act_running()) {
1983         actions_interactive_cancel_act();
1984         ob_debug("KILLED interactive action\n");
1985     }
1986     else if (menu_frame_visible) {
1987         menu_frame_hide_all();
1988         ob_debug("KILLED open menus\n");
1989     }
1990     else if (moveresize_in_progress) {
1991         moveresize_end(TRUE);
1992         ob_debug("KILLED interactive moveresize\n");
1993     }
1994     else if (grab_on_keyboard()) {
1995         ungrab_keyboard();
1996         ob_debug("KILLED active grab on keyboard\n");
1997     }
1998     else
1999         ungrab_passive_key();
2000 }
2001
2002 gboolean event_time_after(Time t1, Time t2)
2003 {
2004     g_assert(t1 != CurrentTime);
2005     g_assert(t2 != CurrentTime);
2006
2007     /*
2008       Timestamp values wrap around (after about 49.7 days). The server, given
2009       its current time is represented by timestamp T, always interprets
2010       timestamps from clients by treating half of the timestamp space as being
2011       later in time than T.
2012       - http://tronche.com/gui/x/xlib/input/pointer-grabbing.html
2013     */
2014
2015     /* TIME_HALF is half of the number space of a Time type variable */
2016 #define TIME_HALF (Time)(1 << (sizeof(Time)*8-1))
2017
2018     if (t2 >= TIME_HALF)
2019         /* t2 is in the second half so t1 might wrap around and be smaller than
2020            t2 */
2021         return t1 >= t2 || t1 < (t2 + TIME_HALF);
2022     else
2023         /* t2 is in the first half so t1 has to come after it */
2024         return t1 >= t2 && t1 < (t2 + TIME_HALF);
2025 }
2026
2027 Time event_get_server_time(void)
2028 {
2029     /* Generate a timestamp */
2030     XEvent event;
2031
2032     XChangeProperty(ob_display, screen_support_win,
2033                     prop_atoms.wm_class, prop_atoms.string,
2034                     8, PropModeAppend, NULL, 0);
2035     XWindowEvent(ob_display, screen_support_win, PropertyChangeMask, &event);
2036     return event.xproperty.time;
2037 }