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