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