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