allow application rules to place transient/dialog/splash windows, and allow applicati...
[mikachu/openbox.git] / openbox / place.c
index 6c81df6..36e977d 100644 (file)
 #include "frame.h"
 #include "focus.h"
 #include "config.h"
+#include "dock.h"
 #include "debug.h"
 
+extern ObDock *dock;
+
 static void add_choice(guint *choice, guint mychoice)
 {
     guint i;
@@ -40,22 +43,10 @@ static void add_choice(guint *choice, guint mychoice)
 
 static Rect *pick_pointer_head(ObClient *c)
 {
-    guint i;
-    gint px, py;
-
-    screen_pointer_pos(&px, &py);
-     
-    for (i = 0; i < screen_num_monitors; ++i) {  
-        if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
-            return screen_area_monitor(c->desktop, i);
-        }
-    }
-    g_assert_not_reached();
+    return screen_area(c->desktop, screen_monitor_pointer(), NULL);
 }
 
-/*! Pick a monitor to place a window on.
-  The returned array value should be freed with g_free. The areas within the
-  array should not be freed. */
+/*! Pick a monitor to place a window on. */
 static Rect **pick_head(ObClient *c)
 {
     Rect **area;
@@ -72,7 +63,7 @@ static Rect **pick_head(ObClient *c)
     /* try direct parent first */
     if ((p = client_direct_parent(c))) {
         add_choice(choice, client_monitor(p));
-        ob_debug("placement adding choice %d for parent\n",
+        ob_debug("placement adding choice %d for parent",
                  client_monitor(p));
     }
 
@@ -82,49 +73,58 @@ static Rect **pick_head(ObClient *c)
 
         /* try on the client's desktop */
         for (it = c->group->members; it; it = g_slist_next(it)) {
-            ObClient *itc = it->data;            
+            ObClient *itc = it->data;
             if (itc != c &&
                 (itc->desktop == c->desktop ||
                  itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
             {
                 add_choice(choice, client_monitor(it->data));
-                ob_debug("placement adding choice %d for group sibling\n",
+                ob_debug("placement adding choice %d for group sibling",
                          client_monitor(it->data));
             }
         }
 
         /* try on all desktops */
         for (it = c->group->members; it; it = g_slist_next(it)) {
-            ObClient *itc = it->data;            
+            ObClient *itc = it->data;
             if (itc != c) {
                 add_choice(choice, client_monitor(it->data));
                 ob_debug("placement adding choice %d for group sibling on "
-                         "another desktop\n", client_monitor(it->data));
+                         "another desktop", client_monitor(it->data));
             }
         }
     }
 
-    if (focus_client) {
+    /* skip this if placing by the mouse position */
+    if (focus_client && client_normal(focus_client) &&
+        config_place_monitor != OB_PLACE_MONITOR_MOUSE)
+    {
         add_choice(choice, client_monitor(focus_client));
-        ob_debug("placement adding choice %d for focused window\n",
+        ob_debug("placement adding choice %d for normal focused window",
                  client_monitor(focus_client));
     }
 
     screen_pointer_pos(&px, &py);
 
-    for (i = 0; i < screen_num_monitors; i++)
-        if (RECT_CONTAINS(*screen_physical_area_monitor(i), px, py)) {
+    for (i = 0; i < screen_num_monitors; i++) {
+        Rect *monitor = screen_physical_area_monitor(i);
+        gboolean contain = RECT_CONTAINS(*monitor, px, py);
+        g_free(monitor);
+        if (contain) {
             add_choice(choice, i);
-            ob_debug("placement adding choice %d for mouse pointer\n", i);
+            ob_debug("placement adding choice %d for mouse pointer", i);
             break;
         }
+    }
 
     /* add any leftover choices */
     for (i = 0; i < screen_num_monitors; ++i)
         add_choice(choice, i);
 
     for (i = 0; i < screen_num_monitors; ++i)
-        area[i] = screen_area_monitor(c->desktop, choice[i]);
+        area[i] = screen_area(c->desktop, choice[i], NULL);
+
+    g_free(choice);
 
     return area;
 }
@@ -136,7 +136,8 @@ static gboolean place_random(ObClient *client, gint *x, gint *y)
     guint i;
 
     areas = pick_head(client);
-    i = g_random_int_range(0, screen_num_monitors);
+    i = (config_place_monitor != OB_PLACE_MONITOR_ANY) ?
+        0 : g_random_int_range(0, screen_num_monitors);
 
     l = areas[i]->x;
     t = areas[i]->y;
@@ -148,6 +149,8 @@ static gboolean place_random(ObClient *client, gint *x, gint *y)
     if (b > t) *y = g_random_int_range(t, b + 1);
     else       *y = areas[i]->y;
 
+    for (i = 0; i < screen_num_monitors; ++i)
+        g_free(areas[i]);
     g_free(areas);
 
     return TRUE;
@@ -170,7 +173,7 @@ static GSList* area_remove(GSList *list, Rect *a)
 
         if (!RECT_INTERSECTS_RECT(*r, *a)) {
             result = g_slist_prepend(result, r);
-            r = NULL; /* dont free it */
+            /* dont free r, it's moved to the result list */
         } else {
             Rect isect, extra;
 
@@ -205,23 +208,25 @@ static GSList* area_remove(GSList *list, Rect *a)
                          r->width, RECT_BOTTOM(*r) - RECT_BOTTOM(isect));
                 result = area_add(result, &extra);
             }
-        }
 
-        g_free(r);
+            /* 'r' is not being added to the result list, so free it */
+            g_free(r);
+        }
     }
     g_slist_free(list);
     return result;
 }
 
 enum {
-    IGNORE_FULLSCREEN = 1 << 0,
-    IGNORE_MAXIMIZED  = 1 << 1,
-    IGNORE_MENUTOOL   = 1 << 2,
-    IGNORE_SHADED     = 1 << 3,
-    IGNORE_NONGROUP   = 1 << 4,
-    IGNORE_BELOW      = 1 << 5,
-    IGNORE_NONFOCUS   = 1 << 6,
-    IGNORE_END        = 1 << 7
+    IGNORE_FULLSCREEN = 1,
+    IGNORE_MAXIMIZED  = 2,
+    IGNORE_MENUTOOL   = 3,
+    /*IGNORE_SHADED     = 3,*/
+    IGNORE_NONGROUP   = 4,
+    IGNORE_BELOW      = 5,
+    /*IGNORE_NONFOCUS   = 1 << 5,*/
+    IGNORE_DOCK       = 6,
+    IGNORE_END        = 7
 };
 
 static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
@@ -231,6 +236,7 @@ static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
     gboolean ret;
     gint maxsize;
     GSList *spaces = NULL, *sit, *maxit;
+    guint i;
 
     areas = pick_head(c);
     ret = FALSE;
@@ -238,11 +244,12 @@ static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
     maxit = NULL;
 
     /* try ignoring different things to find empty space */
-    for (ignore = 0; ignore < IGNORE_END && !ret; ignore = (ignore << 1) + 1) {
-        guint i;
-
-        /* try all monitors in order of preference */
-        for (i = 0; i < screen_num_monitors && !ret; ++i) {
+    for (ignore = 0; ignore < IGNORE_END && !ret; ignore++) {
+        /* try all monitors in order of preference, but only the first one
+           if config_place_monitor is MOUSE or ACTIVE */
+        for (i = 0; (i < (config_place_monitor != OB_PLACE_MONITOR_ANY ?
+                          1 : screen_num_monitors) && !ret); ++i)
+        {
             GList *it;
 
             /* add the whole monitor */
@@ -267,37 +274,46 @@ static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
                     test->type == OB_CLIENT_TYPE_DESKTOP) continue;
 
 
-                if ((ignore & IGNORE_FULLSCREEN) &&
+                if ((ignore >= IGNORE_FULLSCREEN) &&
                     test->fullscreen) continue;
-                if ((ignore & IGNORE_MAXIMIZED) &&
+                if ((ignore >= IGNORE_MAXIMIZED) &&
                     test->max_horz && test->max_vert) continue;
-                if ((ignore & IGNORE_MENUTOOL) &&
+                if ((ignore >= IGNORE_MENUTOOL) &&
                     (test->type == OB_CLIENT_TYPE_MENU ||
                      test->type == OB_CLIENT_TYPE_TOOLBAR) &&
                     client_has_parent(c)) continue;
-                if ((ignore & IGNORE_SHADED) &&
+                /*
+                if ((ignore >= IGNORE_SHADED) &&
                     test->shaded) continue;
-                if ((ignore & IGNORE_NONGROUP) &&
-                    client_has_group_siblings(test) &&
+                */
+                if ((ignore >= IGNORE_NONGROUP) &&
+                    client_has_group_siblings(c) &&
                     test->group != c->group) continue;
-                if ((ignore & IGNORE_BELOW) &&
+                if ((ignore >= IGNORE_BELOW) &&
                     test->layer < c->layer) continue;
-                if ((ignore & IGNORE_NONFOCUS) &&
+                /*
+                if ((ignore >= IGNORE_NONFOCUS) &&
                     focus_client != test) continue;
-
+                */
                 /* don't ignore this window, so remove it from the available
                    area */
                 spaces = area_remove(spaces, &test->frame->area);
             }
 
+            if (ignore < IGNORE_DOCK) {
+                Rect a;
+                dock_get_area(&a);
+                spaces = area_remove(spaces, &a);
+            }
+
             for (sit = spaces; sit; sit = g_slist_next(sit)) {
                 Rect *r = sit->data;
 
                 if (r->width >= c->frame->area.width &&
                     r->height >= c->frame->area.height &&
-                    r->width > maxsize)
+                    r->width * r->height > maxsize)
                 {
-                    maxsize = r->width;
+                    maxsize = r->width * r->height;
                     maxit = sit;
                 }
             }
@@ -306,8 +322,12 @@ static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
                 Rect *r = maxit->data;
 
                 /* center it in the area */
-                *x = r->x + (r->width - c->frame->area.width) / 2;
-                *y = r->y + (r->height - c->frame->area.height) / 2;
+                *x = r->x;
+                *y = r->y;
+                if (config_place_center) {
+                    *x += (r->width - c->frame->area.width) / 2;
+                    *y += (r->height - c->frame->area.height) / 2;
+                }
                 ret = TRUE;
             }
 
@@ -318,6 +338,8 @@ static gboolean place_nooverlap(ObClient *c, gint *x, gint *y)
         }
     }
 
+    for (i = 0; i < screen_num_monitors; ++i)
+        g_free(areas[i]);
     g_free(areas);
     return ret;
 }
@@ -355,34 +377,44 @@ static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
 
     /* Find which head the pointer is on */
     if (settings->monitor == 0)
+        /* this can return NULL */
         screen = pick_pointer_head(client);
     else if (settings->monitor > 0 &&
              (guint)settings->monitor <= screen_num_monitors)
-        screen = screen_area_monitor(client->desktop,
-                                     (guint)settings->monitor - 1);
-    else {
-        Rect **all = NULL;
-        all = pick_head(client);
-        screen = all[0];
-        g_free(all); /* the areas themselves don't need to be freed */
+        screen = screen_area(client->desktop, (guint)settings->monitor - 1,
+                             NULL);
+
+    /* if we have't found a screen yet.. */
+    if (!screen) {
+        Rect **areas;
+        guint i;
+
+        areas = pick_head(client);
+        screen = areas[0];
+
+        /* don't free the first one, it's being set as "screen" */
+        for (i = 1; i < screen_num_monitors; ++i)
+            g_free(areas[i]);
+        g_free(areas);
     }
 
-    if (settings->center_x)
+    if (settings->position.x.center)
         *x = screen->x + screen->width / 2 - client->area.width / 2;
-    else if (settings->opposite_x)
+    else if (settings->position.x.opposite)
         *x = screen->x + screen->width - client->frame->area.width -
-            settings->position.x;
+            settings->position.x.pos;
     else
-        *x = screen->x + settings->position.x;
+        *x = screen->x + settings->position.x.pos;
 
-    if (settings->center_y)
+    if (settings->position.y.center)
         *y = screen->y + screen->height / 2 - client->area.height / 2;
-    else if (settings->opposite_y)
+    else if (settings->position.y.opposite)
         *y = screen->y + screen->height - client->frame->area.height -
-            settings->position.y;
+            settings->position.y.pos;
     else
-        *y = screen->y + settings->position.y;
+        *y = screen->y + settings->position.y.pos;
 
+    g_free(screen);
     return TRUE;
 }
 
@@ -409,7 +441,7 @@ static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
                 }
             }
             if (!first) {
-                *x = ((r + 1 - l) - client->frame->area.width) / 2 + l; 
+                *x = ((r + 1 - l) - client->frame->area.width) / 2 + l;
                 *y = ((b + 1 - t) - client->frame->area.height) / 2 + t;
                 return TRUE;
             }
@@ -420,12 +452,15 @@ static gboolean place_transient_splash(ObClient *client, gint *x, gint *y)
         client->type == OB_CLIENT_TYPE_SPLASH)
     {
         Rect **areas;
+        guint i;
 
         areas = pick_head(client);
 
         *x = (areas[0]->width - client->frame->area.width) / 2 + areas[0]->x;
         *y = (areas[0]->height - client->frame->area.height) / 2 + areas[0]->y;
 
+        for (i = 0; i < screen_num_monitors; ++i)
+            g_free(areas[i]);
         g_free(areas);
         return TRUE;
     }
@@ -438,22 +473,26 @@ gboolean place_client(ObClient *client, gint *x, gint *y,
                       ObAppSettings *settings)
 {
     gboolean ret;
-
-    if (client->positioned)
+    gboolean userplaced = FALSE;
+
+    /* per-app settings override program specified position
+     * but not user specified, unless pos_force is enabled */
+    if (((client->positioned & USPosition) &&
+         !(settings && settings->pos_given && settings->pos_force)) ||
+        ((client->positioned & PPosition) &&
+         !(settings && settings->pos_given)))
         return FALSE;
 
     /* try a number of methods */
-    ret = place_transient_splash(client, x, y) ||
-        place_per_app_setting(client, x, y, settings) ||
+    ret = (userplaced = place_per_app_setting(client, x, y, settings)) ||
+        place_transient_splash(client, x, y) ||
         (config_place_policy == OB_PLACE_POLICY_MOUSE &&
          place_under_mouse(client, x, y)) ||
         place_nooverlap(client, x, y) ||
-        place_under_mouse(client, x, y) ||
         place_random(client, x, y);
     g_assert(ret);
 
     /* get where the client should be */
-    frame_frame_gravity(client->frame, x, y,
-                        client->area.width, client->area.height);
-    return ret;
+    frame_frame_gravity(client->frame, x, y);
+    return !userplaced;
 }