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