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