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