]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/event.c
ignore focus events on root that we don't care about
[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        Ben 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 "menu.h"
32 #include "menuframe.h"
33 #include "keyboard.h"
34 #include "mouse.h"
35 #include "mainloop.h"
36 #include "framerender.h"
37 #include "focus.h"
38 #include "moveresize.h"
39 #include "group.h"
40 #include "stacking.h"
41 #include "extensions.h"
42
43 #include <X11/Xlib.h>
44 #include <X11/keysym.h>
45 #include <X11/Xatom.h>
46 #include <glib.h>
47
48 #ifdef HAVE_SYS_SELECT_H
49 #  include <sys/select.h>
50 #endif
51 #ifdef HAVE_SIGNAL_H
52 #  include <signal.h>
53 #endif
54 #ifdef XKB
55 #  include <X11/XKBlib.h>
56 #endif
57
58 #ifdef USE_SM
59 #include <X11/ICE/ICElib.h>
60 #endif
61
62 typedef struct
63 {
64     gboolean ignored;
65 } ObEventData;
66
67 static void event_process(const XEvent *e, gpointer data);
68 static void event_client_dest(ObClient *client, gpointer data);
69 static void event_handle_root(XEvent *e);
70 static void event_handle_menu(XEvent *e);
71 static void event_handle_dock(ObDock *s, XEvent *e);
72 static void event_handle_dockapp(ObDockApp *app, XEvent *e);
73 static void event_handle_client(ObClient *c, XEvent *e);
74 static void event_handle_group(ObGroup *g, XEvent *e);
75
76 static gboolean focus_delay_func(gpointer data);
77 static void focus_delay_client_dest(ObClient *client, gpointer data);
78
79 static gboolean menu_hide_delay_func(gpointer data);
80
81 /* The time for the current event being processed */
82 Time event_curtime = CurrentTime;
83
84 /*! The value of the mask for the NumLock modifier */
85 guint NumLockMask;
86 /*! The value of the mask for the ScrollLock modifier */
87 guint ScrollLockMask;
88 /*! The key codes for the modifier keys */
89 static XModifierKeymap *modmap;
90 /*! Table of the constant modifier masks */
91 static const gint mask_table[] = {
92     ShiftMask, LockMask, ControlMask, Mod1Mask,
93     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
94 };
95 static gint mask_table_size;
96
97 static guint ignore_enter_focus = 0;
98
99 static gboolean menu_can_hide;
100
101 #ifdef USE_SM
102 static void ice_handler(gint fd, gpointer conn)
103 {
104     Bool b;
105     IceProcessMessages(conn, NULL, &b);
106 }
107
108 static void ice_watch(IceConn conn, IcePointer data, Bool opening,
109                       IcePointer *watch_data)
110 {
111     static gint fd = -1;
112
113     if (opening) {
114         fd = IceConnectionNumber(conn);
115         ob_main_loop_fd_add(ob_main_loop, fd, ice_handler, conn, NULL);
116     } else {
117         ob_main_loop_fd_remove(ob_main_loop, fd);
118         fd = -1;
119     }
120 }
121 #endif
122
123 void event_startup(gboolean reconfig)
124 {
125     if (reconfig) return;
126
127     mask_table_size = sizeof(mask_table) / sizeof(mask_table[0]);
128      
129     /* get lock masks that are defined by the display (not constant) */
130     modmap = XGetModifierMapping(ob_display);
131     g_assert(modmap);
132     if (modmap && modmap->max_keypermod > 0) {
133         size_t cnt;
134         const size_t size = mask_table_size * modmap->max_keypermod;
135         /* get the values of the keyboard lock modifiers
136            Note: Caps lock is not retrieved the same way as Scroll and Num
137            lock since it doesn't need to be. */
138         const KeyCode num_lock = XKeysymToKeycode(ob_display, XK_Num_Lock);
139         const KeyCode scroll_lock = XKeysymToKeycode(ob_display,
140                                                      XK_Scroll_Lock);
141
142         for (cnt = 0; cnt < size; ++cnt) {
143             if (! modmap->modifiermap[cnt]) continue;
144
145             if (num_lock == modmap->modifiermap[cnt])
146                 NumLockMask = mask_table[cnt / modmap->max_keypermod];
147             if (scroll_lock == modmap->modifiermap[cnt])
148                 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
149         }
150     }
151
152     ob_main_loop_x_add(ob_main_loop, event_process, NULL, NULL);
153
154 #ifdef USE_SM
155     IceAddConnectionWatch(ice_watch, NULL);
156 #endif
157
158     client_add_destructor(focus_delay_client_dest, NULL);
159     client_add_destructor(event_client_dest, NULL);
160 }
161
162 void event_shutdown(gboolean reconfig)
163 {
164     if (reconfig) return;
165
166 #ifdef USE_SM
167     IceRemoveConnectionWatch(ice_watch, NULL);
168 #endif
169
170     client_remove_destructor(focus_delay_client_dest);
171     client_remove_destructor(event_client_dest);
172     XFreeModifiermap(modmap);
173 }
174
175 static Window event_get_window(XEvent *e)
176 {
177     Window window;
178
179     /* pick a window */
180     switch (e->type) {
181     case SelectionClear:
182         window = RootWindow(ob_display, ob_screen);
183         break;
184     case MapRequest:
185         window = e->xmap.window;
186         break;
187     case UnmapNotify:
188         window = e->xunmap.window;
189         break;
190     case DestroyNotify:
191         window = e->xdestroywindow.window;
192         break;
193     case ConfigureRequest:
194         window = e->xconfigurerequest.window;
195         break;
196     case ConfigureNotify:
197         window = e->xconfigure.window;
198         break;
199     default:
200 #ifdef XKB
201         if (extensions_xkb && e->type == extensions_xkb_event_basep) {
202             switch (((XkbAnyEvent*)e)->xkb_type) {
203             case XkbBellNotify:
204                 window = ((XkbBellNotifyEvent*)e)->window;
205             default:
206                 window = None;
207             }
208         } else
209 #endif
210             window = e->xany.window;
211     }
212     return window;
213 }
214
215 static void event_set_curtime(XEvent *e)
216 {
217     Time t = CurrentTime;
218
219     /* grab the lasttime and hack up the state */
220     switch (e->type) {
221     case ButtonPress:
222     case ButtonRelease:
223         t = e->xbutton.time;
224         break;
225     case KeyPress:
226         t = e->xkey.time;
227         break;
228     case KeyRelease:
229         t = e->xkey.time;
230         break;
231     case MotionNotify:
232         t = e->xmotion.time;
233         break;
234     case PropertyNotify:
235         t = e->xproperty.time;
236         break;
237     case EnterNotify:
238     case LeaveNotify:
239         t = e->xcrossing.time;
240         break;
241     default:
242         /* if more event types are anticipated, get their timestamp
243            explicitly */
244         break;
245     }
246
247     event_curtime = t;
248 }
249
250 #define STRIP_MODS(s) \
251         s &= ~(LockMask | NumLockMask | ScrollLockMask), \
252         /* kill off the Button1Mask etc, only want the modifiers */ \
253         s &= (ControlMask | ShiftMask | Mod1Mask | \
254               Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask) \
255
256 static void event_hack_mods(XEvent *e)
257 {
258 #ifdef XKB
259     XkbStateRec xkb_state;
260 #endif
261     KeyCode *kp;
262     gint i, k;
263
264     switch (e->type) {
265     case ButtonPress:
266     case ButtonRelease:
267         STRIP_MODS(e->xbutton.state);
268         break;
269     case KeyPress:
270         STRIP_MODS(e->xkey.state);
271         break;
272     case KeyRelease:
273         STRIP_MODS(e->xkey.state);
274         /* remove from the state the mask of the modifier being released, if
275            it is a modifier key being released (this is a little ugly..) */
276 #ifdef XKB
277         if (XkbGetState(ob_display, XkbUseCoreKbd, &xkb_state) == Success) {
278             e->xkey.state = xkb_state.compat_state;
279             break;
280         }
281 #endif
282         kp = modmap->modifiermap;
283         for (i = 0; i < mask_table_size; ++i) {
284             for (k = 0; k < modmap->max_keypermod; ++k) {
285                 if (*kp == e->xkey.keycode) { /* found the keycode */
286                     /* remove the mask for it */
287                     e->xkey.state &= ~mask_table[i];
288                     /* cause the first loop to break; */
289                     i = mask_table_size;
290                     break; /* get outta here! */
291                 }
292                 ++kp;
293             }
294         }
295         break;
296     case MotionNotify:
297         STRIP_MODS(e->xmotion.state);
298         /* compress events */
299         {
300             XEvent ce;
301             while (XCheckTypedWindowEvent(ob_display, e->xmotion.window,
302                                           e->type, &ce)) {
303                 e->xmotion.x_root = ce.xmotion.x_root;
304                 e->xmotion.y_root = ce.xmotion.y_root;
305             }
306         }
307         break;
308     }
309 }
310
311 static gboolean wanted_focusevent(XEvent *e)
312 {
313     gint mode = e->xfocus.mode;
314     gint detail = e->xfocus.detail;
315     Window win = e->xany.window;
316
317     if (e->type == FocusIn) {
318
319         /* These are ones we never want.. */
320
321         /* This means focus was given by a keyboard/mouse grab. */
322         if (mode == NotifyGrab)
323             return FALSE;
324         /* This means focus was given back from a keyboard/mouse grab. */
325         if (mode == NotifyUngrab)
326             return FALSE;
327
328         /* These are the ones we want.. */
329
330         if (win == RootWindow(ob_display, ob_screen)) {
331             /* This means focus reverted off of a client */
332             if (detail == NotifyPointerRoot || detail == NotifyDetailNone)
333                 return TRUE;
334             else
335                 return FALSE;
336         }
337
338         /* This means focus moved from the root window to a client */
339         if (detail == NotifyVirtual)
340             return TRUE;
341         /* This means focus moved from one client to another */
342         if (detail == NotifyNonlinearVirtual)
343             return TRUE;
344
345         /* This means focus reverted off of a client */
346         if (detail == NotifyInferior)
347             return TRUE;
348
349         /* Otherwise.. */
350         return FALSE;
351     } else {
352         g_assert(e->type == FocusOut);
353
354
355         /* These are ones we never want.. */
356
357         /* This means focus was taken by a keyboard/mouse grab. */
358         if (mode == NotifyGrab)
359             return FALSE;
360
361         /* Focus left the root window revertedto state */
362         if (win == RootWindow(ob_display, ob_screen))
363             return FALSE;
364
365         /* These are the ones we want.. */
366
367         /* This means focus moved from a client to the root window */
368         if (detail == NotifyVirtual)
369             return TRUE;
370         /* This means focus moved from one client to another */
371         if (detail == NotifyNonlinearVirtual)
372             return TRUE;
373
374         /* Otherwise.. */
375         return FALSE;
376     }
377 }
378
379 static Bool look_for_focusin(Display *d, XEvent *e, XPointer arg)
380 {
381     return e->type == FocusIn && wanted_focusevent(e);
382 }
383
384 static gboolean event_ignore(XEvent *e, ObClient *client)
385 {
386     switch(e->type) {
387     case EnterNotify:
388     case LeaveNotify:
389         if (e->xcrossing.detail == NotifyInferior)
390             return TRUE;
391         break;
392     case FocusIn:
393     case FocusOut:
394         /* I don't think this should ever happen with our event masks, but
395            if it does, we don't want it. */
396         if (client == NULL)
397             return TRUE;
398         if (!wanted_focusevent(e))
399             return TRUE;
400         break;
401     }
402     return FALSE;
403 }
404
405 static void event_process(const XEvent *ec, gpointer data)
406 {
407     Window window;
408     ObGroup *group = NULL;
409     ObClient *client = NULL;
410     ObDock *dock = NULL;
411     ObDockApp *dockapp = NULL;
412     ObWindow *obwin = NULL;
413     XEvent ee, *e;
414     ObEventData *ed = data;
415
416     /* make a copy we can mangle */
417     ee = *ec;
418     e = &ee;
419
420     window = event_get_window(e);
421     if (!(e->type == PropertyNotify &&
422           (group = g_hash_table_lookup(group_map, &window))))
423         if ((obwin = g_hash_table_lookup(window_map, &window))) {
424             switch (obwin->type) {
425             case Window_Dock:
426                 dock = WINDOW_AS_DOCK(obwin);
427                 break;
428             case Window_DockApp:
429                 dockapp = WINDOW_AS_DOCKAPP(obwin);
430                 break;
431             case Window_Client:
432                 client = WINDOW_AS_CLIENT(obwin);
433                 break;
434             case Window_Menu:
435             case Window_Internal:
436                 /* not to be used for events */
437                 g_assert_not_reached();
438                 break;
439             }
440         }
441
442 #if 1 /* focus debugging stuff */
443     if (e->type == FocusIn || e->type == FocusOut) {
444         gint mode = e->xfocus.mode;
445         gint detail = e->xfocus.detail;
446         Window window = e->xfocus.window;
447         if (detail == NotifyVirtual) {
448             ob_debug("FOCUS %s NOTIFY VIRTUAL window 0x%x\n",
449                      (e->type == FocusIn ? "IN" : "OUT"), window);
450         }
451
452         else if (detail == NotifyNonlinearVirtual) {
453             ob_debug("FOCUS %s NOTIFY NONLINVIRTUAL window 0x%x\n",
454                      (e->type == FocusIn ? "IN" : "OUT"), window);
455         }
456
457         else
458             ob_debug("UNKNOWN FOCUS %s (d %d, m %d) window 0x%x\n",
459                      (e->type == FocusIn ? "IN" : "OUT"),
460                      detail, mode, window);
461     }
462 #endif
463
464     event_set_curtime(e);
465     event_hack_mods(e);
466     if (event_ignore(e, client)) {
467         if (ed)
468             ed->ignored = TRUE;
469         return;
470     } else if (ed)
471             ed->ignored = FALSE;
472
473     /* deal with it in the kernel */
474     if (group)
475         event_handle_group(group, e);
476     else if (client)
477         event_handle_client(client, e);
478     else if (dockapp)
479         event_handle_dockapp(dockapp, e);
480     else if (dock)
481         event_handle_dock(dock, e);
482     else if (window == RootWindow(ob_display, ob_screen))
483         event_handle_root(e);
484     else if (e->type == MapRequest)
485         client_manage(window);
486     else if (e->type == ConfigureRequest) {
487         /* unhandled configure requests must be used to configure the
488            window directly */
489         XWindowChanges xwc;
490
491         xwc.x = e->xconfigurerequest.x;
492         xwc.y = e->xconfigurerequest.y;
493         xwc.width = e->xconfigurerequest.width;
494         xwc.height = e->xconfigurerequest.height;
495         xwc.border_width = e->xconfigurerequest.border_width;
496         xwc.sibling = e->xconfigurerequest.above;
497         xwc.stack_mode = e->xconfigurerequest.detail;
498        
499         /* we are not to be held responsible if someone sends us an
500            invalid request! */
501         xerror_set_ignore(TRUE);
502         XConfigureWindow(ob_display, window,
503                          e->xconfigurerequest.value_mask, &xwc);
504         xerror_set_ignore(FALSE);
505     }
506
507     /* user input (action-bound) events */
508     if (e->type == ButtonPress || e->type == ButtonRelease ||
509         e->type == MotionNotify || e->type == KeyPress ||
510         e->type == KeyRelease)
511     {
512         if (menu_frame_visible)
513             event_handle_menu(e);
514         else {
515             if (!keyboard_process_interactive_grab(e, &client)) {
516                 if (moveresize_in_progress) {
517                     moveresize_event(e);
518
519                     /* make further actions work on the client being
520                        moved/resized */
521                     client = moveresize_client;
522                 }
523
524                 menu_can_hide = FALSE;
525                 ob_main_loop_timeout_add(ob_main_loop,
526                                          config_menu_hide_delay * 1000,
527                                          menu_hide_delay_func,
528                                          NULL, NULL);
529
530                 if (e->type == ButtonPress || e->type == ButtonRelease ||
531                     e->type == MotionNotify)
532                     mouse_event(client, e);
533                 else if (e->type == KeyPress) {
534                     keyboard_event((focus_cycle_target ? focus_cycle_target :
535                                     (focus_hilite ? focus_hilite : client)),
536                                    e);
537                 }
538             }
539         }
540     }
541     /* if something happens and it's not from an XEvent, then we don't know
542        the time */
543     event_curtime = CurrentTime;
544 }
545
546 static void event_handle_root(XEvent *e)
547 {
548     Atom msgtype;
549      
550     switch(e->type) {
551     case SelectionClear:
552         ob_debug("Another WM has requested to replace us. Exiting.\n");
553         ob_exit_replace();
554         break;
555
556     case ClientMessage:
557         if (e->xclient.format != 32) break;
558
559         msgtype = e->xclient.message_type;
560         if (msgtype == prop_atoms.net_current_desktop) {
561             guint d = e->xclient.data.l[0];
562             event_curtime = e->xclient.data.l[1];
563             if (d < screen_num_desktops)
564                 screen_set_desktop(d);
565         } else if (msgtype == prop_atoms.net_number_of_desktops) {
566             guint d = e->xclient.data.l[0];
567             if (d > 0)
568                 screen_set_num_desktops(d);
569         } else if (msgtype == prop_atoms.net_showing_desktop) {
570             screen_show_desktop(e->xclient.data.l[0] != 0);
571         } else if (msgtype == prop_atoms.ob_control) {
572             if (e->xclient.data.l[0] == 1)
573                 ob_reconfigure();
574             else if (e->xclient.data.l[0] == 2)
575                 ob_restart();
576         }
577         break;
578     case PropertyNotify:
579         if (e->xproperty.atom == prop_atoms.net_desktop_names)
580             screen_update_desktop_names();
581         else if (e->xproperty.atom == prop_atoms.net_desktop_layout)
582             screen_update_layout();
583         break;
584     case ConfigureNotify:
585 #ifdef XRANDR
586         XRRUpdateConfiguration(e);
587 #endif
588         screen_resize();
589         break;
590     default:
591         ;
592     }
593 }
594
595 static void event_handle_group(ObGroup *group, XEvent *e)
596 {
597     GSList *it;
598
599     g_assert(e->type == PropertyNotify);
600
601     for (it = group->members; it; it = g_slist_next(it))
602         event_handle_client(it->data, e);
603 }
604
605 void event_enter_client(ObClient *client)
606 {
607     g_assert(config_focus_follow);
608
609     if (client_normal(client) && client_can_focus(client)) {
610         if (config_focus_delay) {
611             ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
612             ob_main_loop_timeout_add(ob_main_loop,
613                                      config_focus_delay,
614                                      focus_delay_func,
615                                      client, NULL);
616         } else
617             focus_delay_func(client);
618     }
619 }
620
621 static void event_handle_client(ObClient *client, XEvent *e)
622 {
623     XEvent ce;
624     Atom msgtype;
625     gint i=0;
626     ObFrameContext con;
627      
628     switch (e->type) {
629     case VisibilityNotify:
630         client->frame->obscured = e->xvisibility.state != VisibilityUnobscured;
631         break;
632     case ButtonPress:
633     case ButtonRelease:
634         /* Wheel buttons don't draw because they are an instant click, so it
635            is a waste of resources to go drawing it. */
636         if (!(e->xbutton.button == 4 || e->xbutton.button == 5)) {
637             con = frame_context(client, e->xbutton.window);
638             con = mouse_button_frame_context(con, e->xbutton.button);
639             switch (con) {
640             case OB_FRAME_CONTEXT_MAXIMIZE:
641                 client->frame->max_press = (e->type == ButtonPress);
642                 framerender_frame(client->frame);
643                 break;
644             case OB_FRAME_CONTEXT_CLOSE:
645                 client->frame->close_press = (e->type == ButtonPress);
646                 framerender_frame(client->frame);
647                 break;
648             case OB_FRAME_CONTEXT_ICONIFY:
649                 client->frame->iconify_press = (e->type == ButtonPress);
650                 framerender_frame(client->frame);
651                 break;
652             case OB_FRAME_CONTEXT_ALLDESKTOPS:
653                 client->frame->desk_press = (e->type == ButtonPress);
654                 framerender_frame(client->frame);
655                 break; 
656             case OB_FRAME_CONTEXT_SHADE:
657                 client->frame->shade_press = (e->type == ButtonPress);
658                 framerender_frame(client->frame);
659                 break;
660             default:
661                 /* nothing changes with clicks for any other contexts */
662                 break;
663             }
664         }
665         break;
666     case FocusIn:
667         if (client != focus_client) {
668             focus_set_client(client);
669             frame_adjust_focus(client->frame, TRUE);
670             client_calc_layer(client);
671         }
672         break;
673     case FocusOut:
674         /* Look for the followup FocusIn */
675         if (!XCheckIfEvent(ob_display, &ce, look_for_focusin, NULL)) {
676             /* There is no FocusIn, this means focus went to a window that
677                is not being managed, or a window on another screen. */
678             ob_debug("Focus went to a black hole !\n");
679         } else if (ce.xany.window == e->xany.window) {
680             /* If focus didn't actually move anywhere, there is nothing to do*/
681             break;
682         } else if (ce.xfocus.detail == NotifyPointerRoot ||
683                  ce.xfocus.detail == NotifyDetailNone) {
684             ob_debug("Focus went to root\n");
685             /* Focus has been reverted to the root window or nothing, so fall
686                back to something other than the window which just had it. */
687             focus_fallback(FALSE);
688         } else if (ce.xfocus.detail == NotifyInferior) {
689             ob_debug("Focus went to parent\n");
690             /* Focus has been reverted to parent, which is our frame window,
691                so fall back to something other than the window which had it. */
692             focus_fallback(FALSE);
693         } else {
694             /* Focus did move, so process the FocusIn event */
695             ObEventData ed = { .ignored = FALSE };
696             event_process(&ce, &ed);
697             if (ed.ignored) {
698                 /* The FocusIn was ignored, this means it was on a window
699                    that isn't a client. */
700                 ob_debug("Focus went to an unmanaged window 0x%x !\n",
701                          ce.xfocus.window);
702                 focus_fallback(TRUE);
703             }
704         }
705
706         /* This client is no longer focused, so show that */
707         focus_hilite = NULL;
708         frame_adjust_focus(client->frame, FALSE);
709         client_calc_layer(client);
710         break;
711     case LeaveNotify:
712         con = frame_context(client, e->xcrossing.window);
713         switch (con) {
714         case OB_FRAME_CONTEXT_MAXIMIZE:
715             client->frame->max_hover = FALSE;
716             frame_adjust_state(client->frame);
717             break;
718         case OB_FRAME_CONTEXT_ALLDESKTOPS:
719             client->frame->desk_hover = FALSE;
720             frame_adjust_state(client->frame);
721             break;
722         case OB_FRAME_CONTEXT_SHADE:
723             client->frame->shade_hover = FALSE;
724             frame_adjust_state(client->frame);
725             break;
726         case OB_FRAME_CONTEXT_ICONIFY:
727             client->frame->iconify_hover = FALSE;
728             frame_adjust_state(client->frame);
729             break;
730         case OB_FRAME_CONTEXT_CLOSE:
731             client->frame->close_hover = FALSE;
732             frame_adjust_state(client->frame);
733             break;
734         case OB_FRAME_CONTEXT_FRAME:
735             if (config_focus_follow && config_focus_delay)
736                 ob_main_loop_timeout_remove_data(ob_main_loop,
737                                                  focus_delay_func,
738                                                  client, TRUE);
739             break;
740         default:
741             break;
742         }
743         break;
744     case EnterNotify:
745     {
746         gboolean nofocus = FALSE;
747
748         if (ignore_enter_focus) {
749             ignore_enter_focus--;
750             nofocus = TRUE;
751         }
752
753         con = frame_context(client, e->xcrossing.window);
754         switch (con) {
755         case OB_FRAME_CONTEXT_MAXIMIZE:
756             client->frame->max_hover = TRUE;
757             frame_adjust_state(client->frame);
758             break;
759         case OB_FRAME_CONTEXT_ALLDESKTOPS:
760             client->frame->desk_hover = TRUE;
761             frame_adjust_state(client->frame);
762             break;
763         case OB_FRAME_CONTEXT_SHADE:
764             client->frame->shade_hover = TRUE;
765             frame_adjust_state(client->frame);
766             break;
767         case OB_FRAME_CONTEXT_ICONIFY:
768             client->frame->iconify_hover = TRUE;
769             frame_adjust_state(client->frame);
770             break;
771         case OB_FRAME_CONTEXT_CLOSE:
772             client->frame->close_hover = TRUE;
773             frame_adjust_state(client->frame);
774             break;
775         case OB_FRAME_CONTEXT_FRAME:
776             if (e->xcrossing.mode == NotifyGrab ||
777                 e->xcrossing.mode == NotifyUngrab)
778             {
779 #ifdef DEBUG_FOCUS
780                 ob_debug("%sNotify mode %d detail %d on %lx IGNORED\n",
781                          (e->type == EnterNotify ? "Enter" : "Leave"),
782                          e->xcrossing.mode,
783                          e->xcrossing.detail, client?client->window:0);
784 #endif
785             } else {
786 #ifdef DEBUG_FOCUS
787                 ob_debug("%sNotify mode %d detail %d on %lx, "
788                          "focusing window: %d\n",
789                          (e->type == EnterNotify ? "Enter" : "Leave"),
790                          e->xcrossing.mode,
791                          e->xcrossing.detail, (client?client->window:0),
792                          !nofocus);
793 #endif
794                 if (!nofocus && config_focus_follow)
795                     event_enter_client(client);
796             }
797             break;
798         default:
799             break;
800         }
801         break;
802     }
803     case ConfigureRequest:
804         /* compress these */
805         while (XCheckTypedWindowEvent(ob_display, client->window,
806                                       ConfigureRequest, &ce)) {
807             ++i;
808             /* XXX if this causes bad things.. we can compress config req's
809                with the same mask. */
810             e->xconfigurerequest.value_mask |=
811                 ce.xconfigurerequest.value_mask;
812             if (ce.xconfigurerequest.value_mask & CWX)
813                 e->xconfigurerequest.x = ce.xconfigurerequest.x;
814             if (ce.xconfigurerequest.value_mask & CWY)
815                 e->xconfigurerequest.y = ce.xconfigurerequest.y;
816             if (ce.xconfigurerequest.value_mask & CWWidth)
817                 e->xconfigurerequest.width = ce.xconfigurerequest.width;
818             if (ce.xconfigurerequest.value_mask & CWHeight)
819                 e->xconfigurerequest.height = ce.xconfigurerequest.height;
820             if (ce.xconfigurerequest.value_mask & CWBorderWidth)
821                 e->xconfigurerequest.border_width =
822                     ce.xconfigurerequest.border_width;
823             if (ce.xconfigurerequest.value_mask & CWStackMode)
824                 e->xconfigurerequest.detail = ce.xconfigurerequest.detail;
825         }
826
827         /* if we are iconic (or shaded (fvwm does this)) ignore the event */
828         if (client->iconic || client->shaded) return;
829
830         /* resize, then move, as specified in the EWMH section 7.7 */
831         if (e->xconfigurerequest.value_mask & (CWWidth | CWHeight |
832                                                CWX | CWY |
833                                                CWBorderWidth)) {
834             gint x, y, w, h;
835             ObCorner corner;
836
837             if (e->xconfigurerequest.value_mask & CWBorderWidth)
838                 client->border_width = e->xconfigurerequest.border_width;
839
840             x = (e->xconfigurerequest.value_mask & CWX) ?
841                 e->xconfigurerequest.x : client->area.x;
842             y = (e->xconfigurerequest.value_mask & CWY) ?
843                 e->xconfigurerequest.y : client->area.y;
844             w = (e->xconfigurerequest.value_mask & CWWidth) ?
845                 e->xconfigurerequest.width : client->area.width;
846             h = (e->xconfigurerequest.value_mask & CWHeight) ?
847                 e->xconfigurerequest.height : client->area.height;
848
849             {
850                 gint newx = x;
851                 gint newy = y;
852                 gint fw = w +
853                      client->frame->size.left + client->frame->size.right;
854                 gint fh = h +
855                      client->frame->size.top + client->frame->size.bottom;
856                 /* make this rude for size-only changes but not for position
857                    changes.. */
858                 gboolean moving = ((e->xconfigurerequest.value_mask & CWX) ||
859                                    (e->xconfigurerequest.value_mask & CWY));
860
861                 client_find_onscreen(client, &newx, &newy, fw, fh,
862                                      !moving);
863                 if (e->xconfigurerequest.value_mask & CWX)
864                     x = newx;
865                 if (e->xconfigurerequest.value_mask & CWY)
866                     y = newy;
867             }
868
869             switch (client->gravity) {
870             case NorthEastGravity:
871             case EastGravity:
872                 corner = OB_CORNER_TOPRIGHT;
873                 break;
874             case SouthWestGravity:
875             case SouthGravity:
876                 corner = OB_CORNER_BOTTOMLEFT;
877                 break;
878             case SouthEastGravity:
879                 corner = OB_CORNER_BOTTOMRIGHT;
880                 break;
881             default:     /* NorthWest, Static, etc */
882                 corner = OB_CORNER_TOPLEFT;
883             }
884
885             client_configure_full(client, corner, x, y, w, h, FALSE, TRUE,
886                                   TRUE);
887         }
888
889         if (e->xconfigurerequest.value_mask & CWStackMode) {
890             switch (e->xconfigurerequest.detail) {
891             case Below:
892             case BottomIf:
893                 /* Apps are so rude. And this is totally disconnected from
894                    activation/focus. Bleh. */
895                 /*client_lower(client);*/
896                 break;
897
898             case Above:
899             case TopIf:
900             default:
901                 /* Apps are so rude. And this is totally disconnected from
902                    activation/focus. Bleh. */
903                 /*client_raise(client);*/
904                 break;
905             }
906         }
907         break;
908     case UnmapNotify:
909         ob_debug("UnmapNotify for window 0x%x\n", client->window);
910         if (client->ignore_unmaps) {
911             client->ignore_unmaps--;
912             break;
913         }
914         client_unmanage(client);
915         break;
916     case DestroyNotify:
917         ob_debug("DestroyNotify for window 0x%x\n", client->window);
918         client_unmanage(client);
919         break;
920     case ReparentNotify:
921         /* this is when the client is first taken captive in the frame */
922         if (e->xreparent.parent == client->frame->plate) break;
923
924         /*
925           This event is quite rare and is usually handled in unmapHandler.
926           However, if the window is unmapped when the reparent event occurs,
927           the window manager never sees it because an unmap event is not sent
928           to an already unmapped window.
929         */
930
931         /* we don't want the reparent event, put it back on the stack for the
932            X server to deal with after we unmanage the window */
933         XPutBackEvent(ob_display, e);
934      
935         client_unmanage(client);
936         break;
937     case MapRequest:
938         ob_debug("MapRequest for 0x%lx\n", client->window);
939         if (!client->iconic) break; /* this normally doesn't happen, but if it
940                                        does, we don't want it!
941                                        it can happen now when the window is on
942                                        another desktop, but we still don't
943                                        want it! */
944         client_activate(client, FALSE, TRUE);
945         break;
946     case ClientMessage:
947         /* validate cuz we query stuff off the client here */
948         if (!client_validate(client)) break;
949
950         if (e->xclient.format != 32) return;
951
952         msgtype = e->xclient.message_type;
953         if (msgtype == prop_atoms.wm_change_state) {
954             /* compress changes into a single change */
955             while (XCheckTypedWindowEvent(ob_display, client->window,
956                                           e->type, &ce)) {
957                 /* XXX: it would be nice to compress ALL messages of a
958                    type, not just messages in a row without other
959                    message types between. */
960                 if (ce.xclient.message_type != msgtype) {
961                     XPutBackEvent(ob_display, &ce);
962                     break;
963                 }
964                 e->xclient = ce.xclient;
965             }
966             client_set_wm_state(client, e->xclient.data.l[0]);
967         } else if (msgtype == prop_atoms.net_wm_desktop) {
968             /* compress changes into a single change */
969             while (XCheckTypedWindowEvent(ob_display, client->window,
970                                           e->type, &ce)) {
971                 /* XXX: it would be nice to compress ALL messages of a
972                    type, not just messages in a row without other
973                    message types between. */
974                 if (ce.xclient.message_type != msgtype) {
975                     XPutBackEvent(ob_display, &ce);
976                     break;
977                 }
978                 e->xclient = ce.xclient;
979             }
980             if ((unsigned)e->xclient.data.l[0] < screen_num_desktops ||
981                 (unsigned)e->xclient.data.l[0] == DESKTOP_ALL)
982                 client_set_desktop(client, (unsigned)e->xclient.data.l[0],
983                                    FALSE);
984         } else if (msgtype == prop_atoms.net_wm_state) {
985             /* can't compress these */
986             ob_debug("net_wm_state %s %ld %ld for 0x%lx\n",
987                      (e->xclient.data.l[0] == 0 ? "Remove" :
988                       e->xclient.data.l[0] == 1 ? "Add" :
989                       e->xclient.data.l[0] == 2 ? "Toggle" : "INVALID"),
990                      e->xclient.data.l[1], e->xclient.data.l[2],
991                      client->window);
992             client_set_state(client, e->xclient.data.l[0],
993                              e->xclient.data.l[1], e->xclient.data.l[2]);
994         } else if (msgtype == prop_atoms.net_close_window) {
995             ob_debug("net_close_window for 0x%lx\n", client->window);
996             client_close(client);
997         } else if (msgtype == prop_atoms.net_active_window) {
998             ob_debug("net_active_window for 0x%lx source=%s\n",
999                      client->window,
1000                      (e->xclient.data.l[0] == 0 ? "unknown" :
1001                       (e->xclient.data.l[0] == 1 ? "application" :
1002                        (e->xclient.data.l[0] == 2 ? "user" : "INVALID"))));
1003             /* XXX make use of data.l[2] ! */
1004             event_curtime = e->xclient.data.l[1];
1005             client_activate(client, FALSE,
1006                             (e->xclient.data.l[0] == 0 ||
1007                              e->xclient.data.l[0] == 2));
1008         } else if (msgtype == prop_atoms.net_wm_moveresize) {
1009             ob_debug("net_wm_moveresize for 0x%lx direction %d\n",
1010                      client->window, e->xclient.data.l[2]);
1011             if ((Atom)e->xclient.data.l[2] ==
1012                 prop_atoms.net_wm_moveresize_size_topleft ||
1013                 (Atom)e->xclient.data.l[2] ==
1014                 prop_atoms.net_wm_moveresize_size_top ||
1015                 (Atom)e->xclient.data.l[2] ==
1016                 prop_atoms.net_wm_moveresize_size_topright ||
1017                 (Atom)e->xclient.data.l[2] ==
1018                 prop_atoms.net_wm_moveresize_size_right ||
1019                 (Atom)e->xclient.data.l[2] ==
1020                 prop_atoms.net_wm_moveresize_size_right ||
1021                 (Atom)e->xclient.data.l[2] ==
1022                 prop_atoms.net_wm_moveresize_size_bottomright ||
1023                 (Atom)e->xclient.data.l[2] ==
1024                 prop_atoms.net_wm_moveresize_size_bottom ||
1025                 (Atom)e->xclient.data.l[2] ==
1026                 prop_atoms.net_wm_moveresize_size_bottomleft ||
1027                 (Atom)e->xclient.data.l[2] ==
1028                 prop_atoms.net_wm_moveresize_size_left ||
1029                 (Atom)e->xclient.data.l[2] ==
1030                 prop_atoms.net_wm_moveresize_move ||
1031                 (Atom)e->xclient.data.l[2] ==
1032                 prop_atoms.net_wm_moveresize_size_keyboard ||
1033                 (Atom)e->xclient.data.l[2] ==
1034                 prop_atoms.net_wm_moveresize_move_keyboard) {
1035
1036                 moveresize_start(client, e->xclient.data.l[0],
1037                                  e->xclient.data.l[1], e->xclient.data.l[3],
1038                                  e->xclient.data.l[2]);
1039             }
1040             else if ((Atom)e->xclient.data.l[2] ==
1041                      prop_atoms.net_wm_moveresize_cancel)
1042                 moveresize_end(TRUE);
1043         } else if (msgtype == prop_atoms.net_moveresize_window) {
1044             gint oldg = client->gravity;
1045             gint tmpg, x, y, w, h;
1046
1047             if (e->xclient.data.l[0] & 0xff)
1048                 tmpg = e->xclient.data.l[0] & 0xff;
1049             else
1050                 tmpg = oldg;
1051
1052             if (e->xclient.data.l[0] & 1 << 8)
1053                 x = e->xclient.data.l[1];
1054             else
1055                 x = client->area.x;
1056             if (e->xclient.data.l[0] & 1 << 9)
1057                 y = e->xclient.data.l[2];
1058             else
1059                 y = client->area.y;
1060             if (e->xclient.data.l[0] & 1 << 10)
1061                 w = e->xclient.data.l[3];
1062             else
1063                 w = client->area.width;
1064             if (e->xclient.data.l[0] & 1 << 11)
1065                 h = e->xclient.data.l[4];
1066             else
1067                 h = client->area.height;
1068             client->gravity = tmpg;
1069
1070             {
1071                 gint newx = x;
1072                 gint newy = y;
1073                 gint fw = w +
1074                      client->frame->size.left + client->frame->size.right;
1075                 gint fh = h +
1076                      client->frame->size.top + client->frame->size.bottom;
1077                 client_find_onscreen(client, &newx, &newy, fw, fh,
1078                                      client_normal(client));
1079                 if (e->xclient.data.l[0] & 1 << 8)
1080                     x = newx;
1081                 if (e->xclient.data.l[0] & 1 << 9)
1082                     y = newy;
1083             }
1084
1085             client_configure(client, OB_CORNER_TOPLEFT,
1086                              x, y, w, h, FALSE, TRUE);
1087
1088             client->gravity = oldg;
1089         }
1090         break;
1091     case PropertyNotify:
1092         /* validate cuz we query stuff off the client here */
1093         if (!client_validate(client)) break;
1094   
1095         /* compress changes to a single property into a single change */
1096         while (XCheckTypedWindowEvent(ob_display, client->window,
1097                                       e->type, &ce)) {
1098             Atom a, b;
1099
1100             /* XXX: it would be nice to compress ALL changes to a property,
1101                not just changes in a row without other props between. */
1102
1103             a = ce.xproperty.atom;
1104             b = e->xproperty.atom;
1105
1106             if (a == b)
1107                 continue;
1108             if ((a == prop_atoms.net_wm_name ||
1109                  a == prop_atoms.wm_name ||
1110                  a == prop_atoms.net_wm_icon_name ||
1111                  a == prop_atoms.wm_icon_name)
1112                 &&
1113                 (b == prop_atoms.net_wm_name ||
1114                  b == prop_atoms.wm_name ||
1115                  b == prop_atoms.net_wm_icon_name ||
1116                  b == prop_atoms.wm_icon_name)) {
1117                 continue;
1118             }
1119             if (a == prop_atoms.net_wm_icon &&
1120                 b == prop_atoms.net_wm_icon)
1121                 continue;
1122
1123             XPutBackEvent(ob_display, &ce);
1124             break;
1125         }
1126
1127         msgtype = e->xproperty.atom;
1128         if (msgtype == XA_WM_NORMAL_HINTS) {
1129             client_update_normal_hints(client);
1130             /* normal hints can make a window non-resizable */
1131             client_setup_decor_and_functions(client);
1132         } else if (msgtype == XA_WM_HINTS) {
1133             client_update_wmhints(client);
1134         } else if (msgtype == XA_WM_TRANSIENT_FOR) {
1135             client_update_transient_for(client);
1136             client_get_type(client);
1137             /* type may have changed, so update the layer */
1138             client_calc_layer(client);
1139             client_setup_decor_and_functions(client);
1140         } else if (msgtype == prop_atoms.net_wm_name ||
1141                    msgtype == prop_atoms.wm_name ||
1142                    msgtype == prop_atoms.net_wm_icon_name ||
1143                    msgtype == prop_atoms.wm_icon_name) {
1144             client_update_title(client);
1145         } else if (msgtype == prop_atoms.wm_class) {
1146             client_update_class(client);
1147         } else if (msgtype == prop_atoms.wm_protocols) {
1148             client_update_protocols(client);
1149             client_setup_decor_and_functions(client);
1150         }
1151         else if (msgtype == prop_atoms.net_wm_strut) {
1152             client_update_strut(client);
1153         }
1154         else if (msgtype == prop_atoms.net_wm_icon) {
1155             client_update_icons(client);
1156         }
1157         else if (msgtype == prop_atoms.net_wm_user_time) {
1158             client_update_user_time(client, TRUE);
1159         }
1160         else if (msgtype == prop_atoms.sm_client_id) {
1161             client_update_sm_client_id(client);
1162         }
1163     default:
1164         ;
1165 #ifdef SHAPE
1166         if (extensions_shape && e->type == extensions_shape_event_basep) {
1167             client->shaped = ((XShapeEvent*)e)->shaped;
1168             frame_adjust_shape(client->frame);
1169         }
1170 #endif
1171     }
1172 }
1173
1174 static void event_handle_dock(ObDock *s, XEvent *e)
1175 {
1176     switch (e->type) {
1177     case ButtonPress:
1178         if (e->xbutton.button == 1)
1179             stacking_raise(DOCK_AS_WINDOW(s));
1180         else if (e->xbutton.button == 2)
1181             stacking_lower(DOCK_AS_WINDOW(s));
1182         break;
1183     case EnterNotify:
1184         dock_hide(FALSE);
1185         break;
1186     case LeaveNotify:
1187         dock_hide(TRUE);
1188         break;
1189     }
1190 }
1191
1192 static void event_handle_dockapp(ObDockApp *app, XEvent *e)
1193 {
1194     switch (e->type) {
1195     case MotionNotify:
1196         dock_app_drag(app, &e->xmotion);
1197         break;
1198     case UnmapNotify:
1199         if (app->ignore_unmaps) {
1200             app->ignore_unmaps--;
1201             break;
1202         }
1203         dock_remove(app, TRUE);
1204         break;
1205     case DestroyNotify:
1206         dock_remove(app, FALSE);
1207         break;
1208     case ReparentNotify:
1209         dock_remove(app, FALSE);
1210         break;
1211     case ConfigureNotify:
1212         dock_app_configure(app, e->xconfigure.width, e->xconfigure.height);
1213         break;
1214     }
1215 }
1216
1217 ObMenuFrame* find_active_menu()
1218 {
1219     GList *it;
1220     ObMenuFrame *ret = NULL;
1221
1222     for (it = menu_frame_visible; it; it = g_list_next(it)) {
1223         ret = it->data;
1224         if (ret->selected)
1225             break;
1226         ret = NULL;
1227     }
1228     return ret;
1229 }
1230
1231 ObMenuFrame* find_active_or_last_menu()
1232 {
1233     ObMenuFrame *ret = NULL;
1234
1235     ret = find_active_menu();
1236     if (!ret && menu_frame_visible)
1237         ret = menu_frame_visible->data;
1238     return ret;
1239 }
1240
1241 static void event_handle_menu(XEvent *ev)
1242 {
1243     ObMenuFrame *f;
1244     ObMenuEntryFrame *e;
1245
1246     switch (ev->type) {
1247     case ButtonRelease:
1248         if (menu_can_hide) {
1249             if ((e = menu_entry_frame_under(ev->xbutton.x_root,
1250                                             ev->xbutton.y_root)))
1251                 menu_entry_frame_execute(e, ev->xbutton.state,
1252                                          ev->xbutton.time);
1253             else
1254                 menu_frame_hide_all();
1255         }
1256         break;
1257     case MotionNotify:
1258         if ((f = menu_frame_under(ev->xmotion.x_root,
1259                                   ev->xmotion.y_root))) {
1260             menu_frame_move_on_screen(f);
1261             if ((e = menu_entry_frame_under(ev->xmotion.x_root,
1262                                             ev->xmotion.y_root)))
1263                 menu_frame_select(f, e);
1264         }
1265         {
1266             ObMenuFrame *a;
1267
1268             a = find_active_menu();
1269             if (a && a != f &&
1270                 a->selected->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU)
1271             {
1272                 menu_frame_select(a, NULL);
1273             }
1274         }
1275         break;
1276     case KeyPress:
1277         if (ev->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
1278             menu_frame_hide_all();
1279         else if (ev->xkey.keycode == ob_keycode(OB_KEY_RETURN)) {
1280             ObMenuFrame *f;
1281             if ((f = find_active_menu()))
1282                 menu_entry_frame_execute(f->selected, ev->xkey.state,
1283                                          ev->xkey.time);
1284         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_LEFT)) {
1285             ObMenuFrame *f;
1286             if ((f = find_active_or_last_menu()) && f->parent)
1287                 menu_frame_select(f, NULL);
1288         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_RIGHT)) {
1289             ObMenuFrame *f;
1290             if ((f = find_active_or_last_menu()) && f->child)
1291                 menu_frame_select_next(f->child);
1292         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_UP)) {
1293             ObMenuFrame *f;
1294             if ((f = find_active_or_last_menu()))
1295                 menu_frame_select_previous(f);
1296         } else if (ev->xkey.keycode == ob_keycode(OB_KEY_DOWN)) {
1297             ObMenuFrame *f;
1298             if ((f = find_active_or_last_menu()))
1299                 menu_frame_select_next(f);
1300         }
1301         break;
1302     }
1303 }
1304
1305 static gboolean menu_hide_delay_func(gpointer data)
1306 {
1307     menu_can_hide = TRUE;
1308     return FALSE; /* no repeat */
1309 }
1310
1311 static gboolean focus_delay_func(gpointer data)
1312 {
1313     ObClient *c = data;
1314
1315     if (focus_client != c) {
1316         if (client_validate(c)) {
1317             client_focus(c);
1318             if (config_focus_raise)
1319                 client_raise(c);
1320         }
1321     }
1322     return FALSE; /* no repeat */
1323 }
1324
1325 static void focus_delay_client_dest(ObClient *client, gpointer data)
1326 {
1327     ob_main_loop_timeout_remove_data(ob_main_loop, focus_delay_func,
1328                                      client, TRUE);
1329 }
1330
1331 static void event_client_dest(ObClient *client, gpointer data)
1332 {
1333     if (client == focus_hilite)
1334         focus_hilite = NULL;
1335 }
1336
1337 void event_halt_focus_delay()
1338 {
1339     ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
1340 }
1341
1342 void event_ignore_queued_enters()
1343 {
1344     GSList *saved = NULL, *it;
1345     XEvent *e;
1346                 
1347     XSync(ob_display, FALSE);
1348
1349     /* count the events */
1350     while (TRUE) {
1351         e = g_new(XEvent, 1);
1352         if (XCheckTypedEvent(ob_display, EnterNotify, e)) {
1353             ObWindow *win;
1354             
1355             win = g_hash_table_lookup(window_map, &e->xany.window);
1356             if (win && WINDOW_IS_CLIENT(win))
1357                 ++ignore_enter_focus;
1358             
1359             saved = g_slist_append(saved, e);
1360         } else {
1361             g_free(e);
1362             break;
1363         }
1364     }
1365     /* put the events back */
1366     for (it = saved; it; it = g_slist_next(it)) {
1367         XPutBackEvent(ob_display, it->data);
1368         g_free(it->data);
1369     }
1370     g_slist_free(saved);
1371 }