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