]> Dogcows Code - chaz/openbox/blobdiff - openbox/event.c
super correct enter event ignoring that will only ignore what it has to, yay?
[chaz/openbox] / openbox / event.c
index c529f15648e12dcb790b3866fcafe621a17ba272..383b260136ca6e80dfb1c94de7b21515ca2f4283 100644 (file)
@@ -28,6 +28,7 @@
 #include "config.h"
 #include "screen.h"
 #include "frame.h"
+#include "grab.h"
 #include "menu.h"
 #include "menuframe.h"
 #include "keyboard.h"
@@ -84,6 +85,7 @@ static void event_handle_dockapp(ObDockApp *app, XEvent *e);
 static void event_handle_client(ObClient *c, XEvent *e);
 static void event_handle_user_time_window_clients(GSList *l, XEvent *e);
 static void event_handle_user_input(ObClient *client, XEvent *e);
+static gboolean is_enter_focus_event_ignored(XEvent *e);
 
 static void focus_delay_dest(gpointer data);
 static gboolean focus_delay_cmp(gconstpointer d1, gconstpointer d2);
@@ -95,7 +97,11 @@ static gboolean menu_hide_delay_func(gpointer data);
 /* The time for the current event being processed */
 Time event_curtime = CurrentTime;
 
+#define NUM_IGNORE_SERIALS 20
+
 static guint ignore_enter_focus = 0;
+/*! This is a 0 terminated list of ignored serials */
+static gulong ignore_enter_serials[NUM_IGNORE_SERIALS+1] = {0};
 static gboolean menu_can_hide;
 static gboolean focus_left_screen = FALSE;
 
@@ -131,7 +137,7 @@ void event_startup(gboolean reconfig)
     IceAddConnectionWatch(ice_watch, NULL);
 #endif
 
-    client_add_destructor(focus_delay_client_dest, NULL);
+    client_add_destroy_notify(focus_delay_client_dest, NULL);
 }
 
 void event_shutdown(gboolean reconfig)
@@ -142,7 +148,7 @@ void event_shutdown(gboolean reconfig)
     IceRemoveConnectionWatch(ice_watch, NULL);
 #endif
 
-    client_remove_destructor(focus_delay_client_dest);
+    client_remove_destroy_notify(focus_delay_client_dest);
 }
 
 static Window event_get_window(XEvent *e)
@@ -293,24 +299,36 @@ static gboolean wanted_focusevent(XEvent *e, gboolean in_client_only)
 
         /* These are the ones we want.. */
 
-        if (win == RootWindow(ob_display, ob_screen) && !in_client_only) {
+        if (win == RootWindow(ob_display, ob_screen)) {
+            /* If looking for a focus in on a client, then always return
+               FALSE for focus in's to the root window */
+            if (in_client_only)
+                return FALSE;
             /* This means focus reverted off of a client */
-            if (detail == NotifyPointerRoot || detail == NotifyDetailNone ||
-                detail == NotifyInferior)
+            else if (detail == NotifyPointerRoot ||
+                     detail == NotifyDetailNone ||
+                     detail == NotifyInferior)
                 return TRUE;
             else
                 return FALSE;
         }
 
+        /* It was on a client, was it a valid one?
+           It's possible to get a FocusIn event for a client that was managed
+           but has disappeared.
+        */
+        if (in_client_only) {
+            ObWindow *w = g_hash_table_lookup(window_map, &e->xfocus.window);
+            if (!w || !WINDOW_IS_CLIENT(w))
+                return FALSE;
+        }
+
         /* This means focus moved from the root window to a client */
         if (detail == NotifyVirtual)
             return TRUE;
         /* This means focus moved from one client to another */
         if (detail == NotifyNonlinearVirtual)
             return TRUE;
-        /* This means focus moved to the frame window */
-        if (detail == NotifyInferior && !in_client_only)
-            return TRUE;
 
         /* Otherwise.. */
         return FALSE;
@@ -335,21 +353,18 @@ static gboolean wanted_focusevent(XEvent *e, gboolean in_client_only)
         /* This means focus moved from one client to another */
         if (detail == NotifyNonlinearVirtual)
             return TRUE;
-        /* This means focus had moved to our frame window and now moved off */
-        if (detail == NotifyNonlinear)
-            return TRUE;
 
         /* Otherwise.. */
         return FALSE;
     }
 }
 
-static Bool look_for_focusin(Display *d, XEvent *e, XPointer arg)
+static Bool event_look_for_focusin(Display *d, XEvent *e, XPointer arg)
 {
     return e->type == FocusIn && wanted_focusevent(e, FALSE);
 }
 
-static Bool look_for_focusin_client(Display *d, XEvent *e, XPointer arg)
+static Bool event_look_for_focusin_client(Display *d, XEvent *e, XPointer arg)
 {
     return e->type == FocusIn && wanted_focusevent(e, TRUE);
 }
@@ -378,6 +393,9 @@ static void print_focusevent(XEvent *e)
     case NotifyDetailNone:  detailstr="NotifyDetailNone";  break;
     }
 
+    if (mode == NotifyGrab || mode == NotifyUngrab)
+        return;
+
     g_assert(modestr);
     g_assert(detailstr);
     ob_debug_type(OB_DEBUG_FOCUS, "Focus%s 0x%x mode=%s detail=%s\n",
@@ -464,9 +482,8 @@ static void event_process(const XEvent *ec, gpointer data)
             e->xfocus.detail == NotifyInferior)
         {
             XEvent ce;
-            ob_debug_type(OB_DEBUG_FOCUS,
-                          "Focus went to pointer root/none or to our frame "
-                          "window\n");
+
+            ob_debug_type(OB_DEBUG_FOCUS, "Focus went to pointer root/none\n");
 
             /* If another FocusIn is in the queue then don't fallback yet. This
                fixes the fun case of:
@@ -481,26 +498,33 @@ static void event_process(const XEvent *ec, gpointer data)
                But if the other focus in is something like PointerRoot then we
                still want to fall back.
             */
-            if (XCheckIfEvent(ob_display, &ce, look_for_focusin_client, NULL)){
+            if (XCheckIfEvent(ob_display, &ce, event_look_for_focusin_client,
+                              NULL))
+            {
                 XPutBackEvent(ob_display, &ce);
                 ob_debug_type(OB_DEBUG_FOCUS,
                               "  but another FocusIn is coming\n");
             } else {
-                /* Focus has been reverted to the root window, nothing, or to
-                   our frame window.
+                /* Focus has been reverted to the root window or nothing.
 
                    FocusOut events come after UnmapNotify, so we don't need to
                    worry about focusing an invalid window
                 */
 
-                /* In this case we know focus is in our screen */
-                if (e->xfocus.detail == NotifyInferior)
-                    focus_left_screen = FALSE;
-
                 if (!focus_left_screen)
                     focus_fallback(TRUE);
             }
-        } else if (client && client != focus_client) {
+        }
+        else if (!client)
+        {
+            ob_debug_type(OB_DEBUG_FOCUS,
+                          "Focus went to a window that is already gone\n");
+
+            /* If you send focus to a window and then it disappears, you can
+               get the FocusIn for it, after it is unmanaged.
+               Just wait for the next FocusOut/FocusIn pair. */
+        }
+        else if (client != focus_client) {
             focus_left_screen = FALSE;
             frame_adjust_focus(client->frame, TRUE);
             focus_set_client(client);
@@ -512,7 +536,7 @@ static void event_process(const XEvent *ec, gpointer data)
         XEvent ce;
 
         /* Look for the followup FocusIn */
-        if (!XCheckIfEvent(ob_display, &ce, look_for_focusin, NULL)) {
+        if (!XCheckIfEvent(ob_display, &ce, event_look_for_focusin, NULL)) {
             /* There is no FocusIn, this means focus went to a window that
                is not being managed, or a window on another screen. */
             Window win, root;
@@ -553,7 +577,8 @@ static void event_process(const XEvent *ec, gpointer data)
 
         if (client && !nomove) {
             frame_adjust_focus(client->frame, FALSE);
-            /* focus_set_client has already been called for sure */
+            if (client == focus_client)
+                focus_set_client(NULL);
             client_calc_layer(client);
         }
     } else if (timewinclients)
@@ -661,7 +686,7 @@ static void event_handle_root(XEvent *e)
                 screen_set_num_desktops(d);
         } else if (msgtype == prop_atoms.net_showing_desktop) {
             screen_show_desktop(e->xclient.data.l[0] != 0, NULL);
-        } else if (msgtype == prop_atoms.openbox_control) {
+        } else if (msgtype == prop_atoms.ob_control) {
             if (e->xclient.data.l[0] == 1)
                 ob_reconfigure();
             else if (e->xclient.data.l[0] == 2)
@@ -863,7 +888,7 @@ static void event_handle_client(ObClient *client, XEvent *e)
                corresponding enter events. Pretend like the animating window
                doesn't even exist..! */
             if (frame_iconify_animating(client->frame))
-                event_ignore_queued_enters();
+                event_ignore_enters_leaving_window(client);
 
             ob_debug_type(OB_DEBUG_FOCUS,
                           "%sNotify mode %d detail %d on %lx\n",
@@ -891,10 +916,8 @@ static void event_handle_client(ObClient *client, XEvent *e)
     {
         gboolean nofocus = FALSE;
 
-        if (ignore_enter_focus) {
-            ignore_enter_focus--;
+        if (is_enter_focus_event_ignored(e))
             nofocus = TRUE;
-        }
 
         con = frame_context(client, e->xcrossing.window,
                             e->xcrossing.x, e->xcrossing.y);
@@ -950,70 +973,33 @@ static void event_handle_client(ObClient *client, XEvent *e)
         break;
     }
     case ConfigureRequest:
+    {
         /* dont compress these unless you're going to watch for property
            notifies in between (these can change what the configure would
            do to the window).
            also you can't compress stacking events
         */
 
-        ob_debug("ConfigureRequest desktop %d wmstate %d vis %d\n",
-                 screen_desktop, client->wmstate, client->frame->visible);
+        gint x, y, w, h;
 
-        /* don't allow clients to move shaded windows (fvwm does this) */
-        if (client->shaded) {
-            e->xconfigurerequest.value_mask &= ~CWX;
-            e->xconfigurerequest.value_mask &= ~CWY;
-        }
+        /* if nothing is changed, then a configurenotify is needed */
+        gboolean config = TRUE;
 
-        /* resize, then move, as specified in the EWMH section 7.7 */
-        if (e->xconfigurerequest.value_mask & (CWWidth | CWHeight |
-                                               CWX | CWY |
-                                               CWBorderWidth)) {
-            gint x, y, w, h;
+        x = client->area.x;
+        y = client->area.y;
+        w = client->area.width;
+        h = client->area.height;
 
-            if (e->xconfigurerequest.value_mask & CWBorderWidth)
-                client->border_width = e->xconfigurerequest.border_width;
+        ob_debug("ConfigureRequest desktop %d wmstate %d visibile %d\n",
+                 screen_desktop, client->wmstate, client->frame->visible);
 
-            x = (e->xconfigurerequest.value_mask & CWX) ?
-                e->xconfigurerequest.x : client->area.x;
-            y = (e->xconfigurerequest.value_mask & CWY) ?
-                e->xconfigurerequest.y : client->area.y;
-            w = (e->xconfigurerequest.value_mask & CWWidth) ?
-                e->xconfigurerequest.width : client->area.width;
-            h = (e->xconfigurerequest.value_mask & CWHeight) ?
-                e->xconfigurerequest.height : client->area.height;
-
-            ob_debug("ConfigureRequest x %d %d y %d %d\n",
-                     e->xconfigurerequest.value_mask & CWX, x,
-                     e->xconfigurerequest.value_mask & CWY, y);
-
-            /* check for broken apps moving to their root position
-
-               XXX remove this some day...that would be nice. right now all
-               kde apps do this when they try activate themselves on another
-               desktop. eg. open amarok window on desktop 1, switch to desktop
-               2, click amarok tray icon. it will move by its decoration size.
-            */
-            if (x != client->area.x &&
-                x == (client->frame->area.x + client->frame->size.left -
-                      (gint)client->border_width) &&
-                y != client->area.y &&
-                y == (client->frame->area.y + client->frame->size.top -
-                      (gint)client->border_width))
-            {
-                ob_debug_type(OB_DEBUG_APP_BUGS,
-                              "Application %s is trying to move via "
-                              "ConfigureRequest to it's root window position "
-                              "but it is not using StaticGravity\n",
-                              client->title);
-                /* don't move it */
-                x = client->area.x;
-                y = client->area.y;
+        if (e->xconfigurerequest.value_mask & CWBorderWidth)
+            if (client->border_width != e->xconfigurerequest.border_width) {
+                client->border_width = e->xconfigurerequest.border_width;
+                /* if only the border width is changing, then it's not needed*/
+                config = FALSE;
             }
 
-            client_find_onscreen(client, &x, &y, w, h, FALSE);
-            client_configure_full(client, x, y, w, h, FALSE, TRUE, TRUE);
-        }
 
         if (e->xconfigurerequest.value_mask & CWStackMode) {
             ObClient *sibling = NULL;
@@ -1030,8 +1016,81 @@ static void event_handle_client(ObClient *client, XEvent *e)
             /* activate it rather than just focus it */
             stacking_restack_request(client, sibling,
                                      e->xconfigurerequest.detail, TRUE);
+
+            /* if a stacking change is requested then it is needed */
+            config = TRUE;
+        }
+
+        /* don't allow clients to move shaded windows (fvwm does this) */
+        if (client->shaded && (e->xconfigurerequest.value_mask & CWX ||
+                               e->xconfigurerequest.value_mask & CWY))
+        {
+            e->xconfigurerequest.value_mask &= ~CWX;
+            e->xconfigurerequest.value_mask &= ~CWY;
+
+            /* if the client tried to move and we aren't letting it then a
+               synthetic event is needed */
+            config = TRUE;
+        }
+
+        if (e->xconfigurerequest.value_mask & CWX ||
+            e->xconfigurerequest.value_mask & CWY ||
+            e->xconfigurerequest.value_mask & CWWidth ||
+            e->xconfigurerequest.value_mask & CWHeight)
+        {
+            if (e->xconfigurerequest.value_mask & CWX)
+                x = e->xconfigurerequest.x;
+            if (e->xconfigurerequest.value_mask & CWY)
+                y = e->xconfigurerequest.y;
+            if (e->xconfigurerequest.value_mask & CWWidth)
+                w = e->xconfigurerequest.width;
+            if (e->xconfigurerequest.value_mask & CWHeight)
+                h = e->xconfigurerequest.height;
+
+            /* if a new position or size is requested, then a configure is
+               needed */
+            config = TRUE;
+        }
+
+        ob_debug("ConfigureRequest x(%d) %d y(%d) %d w(%d) %d h(%d) %d\n",
+                 e->xconfigurerequest.value_mask & CWX, x,
+                 e->xconfigurerequest.value_mask & CWY, y,
+                 e->xconfigurerequest.value_mask & CWWidth, w,
+                 e->xconfigurerequest.value_mask & CWHeight, h);
+
+        /* check for broken apps moving to their root position
+
+           XXX remove this some day...that would be nice. right now all
+           kde apps do this when they try activate themselves on another
+           desktop. eg. open amarok window on desktop 1, switch to desktop
+           2, click amarok tray icon. it will move by its decoration size.
+        */
+        if (x != client->area.x &&
+            x == (client->frame->area.x + client->frame->size.left -
+                  (gint)client->border_width) &&
+            y != client->area.y &&
+            y == (client->frame->area.y + client->frame->size.top -
+                  (gint)client->border_width))
+        {
+            ob_debug_type(OB_DEBUG_APP_BUGS,
+                          "Application %s is trying to move via "
+                          "ConfigureRequest to it's root window position "
+                          "but it is not using StaticGravity\n",
+                          client->title);
+            /* don't move it */
+            x = client->area.x;
+            y = client->area.y;
+        }
+
+        if (config) {
+            client_find_onscreen(client, &x, &y, w, h, FALSE);
+            client_configure(client, x, y, w, h, FALSE, TRUE);
+
+            /* ignore enter events caused by these like ob actions do */
+            event_ignore_enters_leaving_window(client);
         }
         break;
+    }
     case UnmapNotify:
         if (client->ignore_unmaps) {
             client->ignore_unmaps--;
@@ -1122,6 +1181,9 @@ static void event_handle_client(ObClient *client, XEvent *e)
                      client->window);
             client_set_state(client, e->xclient.data.l[0],
                              e->xclient.data.l[1], e->xclient.data.l[2]);
+
+            /* ignore enter events caused by these like ob actions do */
+            event_ignore_enters_leaving_window(client);
         } else if (msgtype == prop_atoms.net_close_window) {
             ob_debug("net_close_window for 0x%lx\n", client->window);
             client_close(client);
@@ -1205,7 +1267,11 @@ static void event_handle_client(ObClient *client, XEvent *e)
                      e->xclient.data.l[0] & 1 << 9, y);
             client_convert_gravity(client, grav, &x, &y, w, h);
             client_find_onscreen(client, &x, &y, w, h, FALSE);
+
             client_configure(client, x, y, w, h, FALSE, TRUE);
+
+            /* ignore enter events caused by these like ob actions do */
+            event_ignore_enters_leaving_window(client);
         } else if (msgtype == prop_atoms.net_restack_window) {
             if (e->xclient.data.l[0] != 2) {
                 ob_debug_type(OB_DEBUG_APP_BUGS,
@@ -1237,6 +1303,9 @@ static void event_handle_client(ObClient *client, XEvent *e)
                     /* just raise, don't activate */
                     stacking_restack_request(client, sibling,
                                              e->xclient.data.l[2], FALSE);
+                    /* send a synthetic ConfigureNotify, cuz this is supposed
+                       to be like a ConfigureRequest. */
+                    client_reconfigure(client);
                 } else
                     ob_debug_type(OB_DEBUG_APP_BUGS,
                                   "_NET_RESTACK_WINDOW sent for window %s "
@@ -1551,6 +1620,7 @@ static gboolean event_handle_menu(XEvent *ev)
         {
             menu_frame_select(e->frame, NULL, FALSE);
         }
+        break;
     case MotionNotify:   
         if ((e = menu_entry_frame_under(ev->xmotion.x_root,   
                                         ev->xmotion.y_root)))
@@ -1650,35 +1720,90 @@ void event_halt_focus_delay()
     ob_main_loop_timeout_remove(ob_main_loop, focus_delay_func);
 }
 
-void event_ignore_queued_enters()
+struct ObLookForEnters
 {
-    GSList *saved = NULL, *it;
-    XEvent *e;
-                
-    XSync(ob_display, FALSE);
+    ObClient *c;
+    gulong looking_for_enter;
+};
 
-    /* count the events */
-    while (TRUE) {
-        e = g_new(XEvent, 1);
-        if (XCheckTypedEvent(ob_display, EnterNotify, e)) {
-            ObWindow *win;
-            
+static Bool event_look_for_enters(Display *d, XEvent *e, XPointer arg)
+{
+    struct ObLookForEnters *lfe = (struct ObLookForEnters*)arg;
+
+    if (lfe->c != NULL && e->type == LeaveNotify) {
+        if (g_hash_table_lookup(window_map, &e->xany.window) == lfe->c)
+            /* found an event leaving this window */
+            lfe->looking_for_enter = e->xany.serial;
+    } else if (e->type == EnterNotify &&
+               (lfe->c == NULL || e->xany.serial == lfe->looking_for_enter))
+    {
+        ObWindow *win;
+        gint i;
+        gboolean ignored = FALSE;
+
+        /* make sure the serial isn't already being ignored */
+        for (i = 0; ignore_enter_serials[i] != 0 && !ignored; ++i) {
+            if (ignore_enter_serials[i] == e->xany.serial)
+                ignored = TRUE;
+        }
+
+        if (!ignored) {
+            /* found an enter for that leave, ignore it if it's going to
+               another window */
             win = g_hash_table_lookup(window_map, &e->xany.window);
             if (win && WINDOW_IS_CLIENT(win))
                 ++ignore_enter_focus;
-            
-            saved = g_slist_append(saved, e);
-        } else {
-            g_free(e);
-            break;
+        }
+
+        /* add it to the ignored list if there is room */
+        if (i < NUM_IGNORE_SERIALS) {
+            ignore_enter_serials[i] = e->xany.serial;
+            ignore_enter_serials[i+1] = 0;
         }
     }
-    /* put the events back */
-    for (it = saved; it; it = g_slist_next(it)) {
-        XPutBackEvent(ob_display, it->data);
-        g_free(it->data);
+    return False; /* don't disrupt the queue order, just count them */
+}
+
+void event_ignore_all_queued_enters()
+{
+    event_ignore_enters_leaving_window(NULL);
+}
+
+void event_ignore_enters_leaving_window(ObClient *c)
+{
+    struct ObLookForEnters lfe;
+    XEvent e;
+
+    XSync(ob_display, FALSE);
+
+    /* count the events without disrupting them */
+    ignore_enter_focus = 0;
+    lfe.c = c;
+    lfe.looking_for_enter = 0;
+    XCheckIfEvent(ob_display, &e, event_look_for_enters, (XPointer)&lfe);
+
+}
+
+static gboolean is_enter_focus_event_ignored(XEvent *e)
+{
+    g_assert(e->type == EnterNotify);
+
+    if (ignore_enter_focus) {
+        gint i;
+
+        --ignore_enter_focus;
+
+        /* remove the serial */
+        for (i = 0; ignore_enter_serials[i] != 0; ++i) {
+            if (ignore_enter_serials[i] == e->xany.serial) {
+                for (; ignore_enter_serials[i+1] != 0; ++i)
+                    ignore_enter_serials[i] = ignore_enter_serials[i+1];
+                ignore_enter_serials[i] = 0;
+            }
+        }
+        return TRUE;
     }
-    g_slist_free(saved);
+    return FALSE;
 }
 
 gboolean event_time_after(Time t1, Time t2)
This page took 0.034965 seconds and 4 git commands to generate.