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