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