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