make sure clients stay on screen when the root window changes size, and maximized...
[dana/openbox.git] / openbox / client.c
index 605a191..8acbf84 100644 (file)
@@ -42,6 +42,7 @@
 #include "obrender/render.h"
 #include "gettext.h"
 #include "obt/display.h"
+#include "obt/xqueue.h"
 #include "obt/prop.h"
 
 #ifdef HAVE_UNISTD_H
@@ -2487,10 +2488,6 @@ gboolean client_is_oldfullscreen(const ObClient *self,
 static ObStackingLayer calc_layer(ObClient *self)
 {
     ObStackingLayer l;
-    const Rect *monitor, *allmonitors;
-
-    monitor = screen_physical_area_monitor(client_monitor(self));
-    allmonitors = screen_physical_area_all_monitors();
 
     if (self->type == OB_CLIENT_TYPE_DESKTOP)
         l = OB_STACKING_LAYER_DESKTOP;
@@ -3128,11 +3125,16 @@ void client_configure(ObClient *self, gint x, gint y, gint w, gint h,
     /* if it moved between monitors, then this can affect the stacking
        layer of this window or others - for fullscreen windows.
        also if it changed to/from oldschool fullscreen then its layer may
-       change */
-    if (screen_find_monitor(&self->frame->area) !=
-        screen_find_monitor(&oldframe) ||
-        (final && (client_is_oldfullscreen(self, &oldclient) !=
-                   client_is_oldfullscreen(self, &self->area))))
+       change
+
+       watch out tho, don't try change stacking stuff if the window is no
+       longer being managed !
+    */
+    if (self->managed &&
+        (screen_find_monitor(&self->frame->area) !=
+         screen_find_monitor(&oldframe) ||
+         (final && (client_is_oldfullscreen(self, &oldclient) !=
+                    client_is_oldfullscreen(self, &self->area)))))
     {
         client_calc_layer(self);
     }
@@ -3195,6 +3197,11 @@ void client_fullscreen(ObClient *self, gboolean fs)
     ob_debug("Window %s going fullscreen (%d)",
              self->title, self->fullscreen);
 
+    if (fs) {
+        /* make sure the window is on some monitor */
+        client_find_onscreen(self, &x, &y, w, h, FALSE);
+    }
+
     client_setup_decor_and_functions(self, FALSE);
     client_move_resize(self, x, y, w, h);
 
@@ -3337,6 +3344,11 @@ void client_maximize(ObClient *self, gboolean max, gint dir)
     if (dir == 0 || dir == 2) /* vert */
         self->max_vert = max;
 
+    if (max) {
+        /* make sure the window is on some monitor */
+        client_find_onscreen(self, &x, &y, w, h, FALSE);
+    }
+
     client_change_state(self); /* change the state hints on the client */
 
     client_setup_decor_and_functions(self, FALSE);
@@ -3615,36 +3627,31 @@ ObClient *client_search_modal_child(ObClient *self)
     return NULL;
 }
 
-static gboolean client_validate_unmap(ObClient *self, int n)
-{
-    XEvent e;
-    gboolean ret = TRUE;
+struct ObClientFindDestroyUnmap {
+    Window window;
+    gint ignore_unmaps;
+};
 
-    if (XCheckTypedWindowEvent(obt_display, self->window, UnmapNotify, &e)) {
-        if (n < self->ignore_unmaps) // ignore this one, but look for more
-            ret = client_validate_unmap(self, n+1);
-        else
-            ret = FALSE; // the window is going to become unmanaged
-
-        /* put them back on the event stack so they end up in the same order */
-        XPutBackEvent(obt_display, &e);
-    }
-
-    return ret;
+static gboolean find_destroy_unmap(XEvent *e, gpointer data)
+{
+    struct ObClientFindDestroyUnmap *find = data;
+    if (e->type == DestroyNotify)
+        return e->xdestroywindow.window == find->window;
+    if (e->type == UnmapNotify && e->xunmap.window == find->window)
+        /* ignore the first $find->ignore_unmaps$ many unmap events */
+        return --find->ignore_unmaps < 0;
+    return FALSE;
 }
 
 gboolean client_validate(ObClient *self)
 {
-    XEvent e;
+    struct ObClientFindDestroyUnmap find;
 
     XSync(obt_display, FALSE); /* get all events on the server */
 
-    if (XCheckTypedWindowEvent(obt_display, self->window, DestroyNotify, &e)) {
-        XPutBackEvent(obt_display, &e);
-        return FALSE;
-    }
-
-    if (!client_validate_unmap(self, 0))
+    find.window = self->window;
+    find.ignore_unmaps = self->ignore_unmaps;
+    if (xqueue_exists_local(find_destroy_unmap, &find))
         return FALSE;
 
     return TRUE;
@@ -3836,6 +3843,8 @@ gboolean client_can_focus(ObClient *self)
 
 gboolean client_focus(ObClient *self)
 {
+    if (!client_validate(self)) return FALSE;
+
     /* we might not focus this window, so if we have modal children which would
        be focused instead, bring them to this desktop */
     client_bring_modal_windows(self);