Add support for using relative expressions in move and resize actions
authorMikael Magnusson <mikachu@gmail.com>
Fri, 16 Apr 2010 16:04:24 +0000 (18:04 +0200)
committerDana Jansens <danakj@orodu.net>
Mon, 17 May 2010 23:31:21 +0000 (19:31 -0400)
Have MoveResizeTo use config_parse_gravity_coord instead of duplicating it locally
Allow MoveResizeTo positions and sizes and per app positions to be relative to screen size
Rename to config_parse_relative_number so it can be used for sizes too
Add relative numbers to width/height in MoveResizeTo
Add relative numbers to MoveRelative
Add relative numbers to ResizeRelative, these are for the client size, not screen size

openbox/actions/moverelative.c
openbox/actions/moveresizeto.c
openbox/actions/resizerelative.c
openbox/config.c
openbox/config.h
openbox/geom.h
openbox/place.c

index 189b4dd..fc4a615 100644 (file)
@@ -2,11 +2,14 @@
 #include "openbox/client.h"
 #include "openbox/screen.h"
 #include "openbox/frame.h"
 #include "openbox/client.h"
 #include "openbox/screen.h"
 #include "openbox/frame.h"
+#include "openbox/config.h"
 #include <stdlib.h> /* for atoi */
 
 typedef struct {
     gint x;
 #include <stdlib.h> /* for atoi */
 
 typedef struct {
     gint x;
+    gint x_denom;
     gint y;
     gint y;
+    gint y_denom;
 } Options;
 
 static gpointer setup_func(xmlNodePtr node);
 } Options;
 
 static gpointer setup_func(xmlNodePtr node);
@@ -22,13 +25,20 @@ static gpointer setup_func(xmlNodePtr node)
 {
     xmlNodePtr n;
     Options *o;
 {
     xmlNodePtr n;
     Options *o;
+    gchar *s;
 
     o = g_slice_new0(Options);
 
 
     o = g_slice_new0(Options);
 
-    if ((n = obt_xml_find_node(node, "x")))
-        o->x = obt_xml_node_int(n);
-    if ((n = obt_xml_find_node(node, "y")))
-        o->y = obt_xml_node_int(n);
+    if ((n = obt_xml_find_node(node, "x"))) {
+        s = obt_xml_node_string(n);
+        config_parse_relative_number(s, &o->x, &o->x_denom);
+        g_free(s);
+    }
+    if ((n = obt_xml_find_node(node, "y"))) {
+        s = obt_xml_node_string(n);
+        config_parse_relative_number(s, &o->y, &o->y_denom);
+        g_free(s);
+    }
 
     return o;
 }
 
     return o;
 }
@@ -48,8 +58,19 @@ static gboolean run_func(ObActionsData *data, gpointer options)
         gint x, y, lw, lh, w, h;
 
         c = data->client;
         gint x, y, lw, lh, w, h;
 
         c = data->client;
-        x = c->area.x + o->x;
-        y = c->area.y + o->y;
+        x = o->x;
+        y = o->y;
+        if (o->x_denom || o->y_denom) {
+            const Rect *carea;
+
+            carea = screen_area(c->desktop, client_monitor(c), NULL);
+            if (o->x_denom)
+                x = (x * carea->width) / o->x_denom;
+            if (o->y_denom)
+                y = (y * carea->height) / o->y_denom;
+        }
+        x = c->area.x + x;
+        y = c->area.y + y;
         w = c->area.width;
         h = c->area.height;
         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
         w = c->area.width;
         h = c->area.height;
         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
index ce50e9b..14695d4 100644 (file)
@@ -2,6 +2,7 @@
 #include "openbox/client.h"
 #include "openbox/screen.h"
 #include "openbox/frame.h"
 #include "openbox/client.h"
 #include "openbox/screen.h"
 #include "openbox/frame.h"
+#include "openbox/config.h"
 #include <stdlib.h> /* for atoi */
 
 enum {
 #include <stdlib.h> /* for atoi */
 
 enum {
@@ -12,14 +13,12 @@ enum {
 };
 
 typedef struct {
 };
 
 typedef struct {
-    gboolean xcenter;
-    gboolean ycenter;
-    gboolean xopposite;
-    gboolean yopposite;
-    gint x;
-    gint y;
+    GravityCoord x;
+    GravityCoord y;
     gint w;
     gint w;
+    gint w_denom;
     gint h;
     gint h;
+    gint h_denom;
     gint monitor;
 } Options;
 
     gint monitor;
 } Options;
 
@@ -36,53 +35,34 @@ void action_moveresizeto_startup(void)
     actions_register("MoveToCenter", setup_center_func, free_func, run_func);
 }
 
     actions_register("MoveToCenter", setup_center_func, free_func, run_func);
 }
 
-static void parse_coord(xmlNodePtr n, gint *pos,
-                        gboolean *opposite, gboolean *center)
-{
-    gchar *s = obt_xml_node_string(n);
-    if (g_ascii_strcasecmp(s, "current") != 0) {
-        if (!g_ascii_strcasecmp(s, "center"))
-            *center = TRUE;
-        else {
-            if (s[0] == '-')
-                *opposite = TRUE;
-            if (s[0] == '-' || s[0] == '+')
-                *pos = atoi(s+1);
-            else
-                *pos = atoi(s);
-        }
-    }
-    g_free(s);
-}
-
 static gpointer setup_func(xmlNodePtr node)
 {
     xmlNodePtr n;
     Options *o;
 
     o = g_slice_new0(Options);
 static gpointer setup_func(xmlNodePtr node)
 {
     xmlNodePtr n;
     Options *o;
 
     o = g_slice_new0(Options);
-    o->x = G_MININT;
-    o->y = G_MININT;
+    o->x.pos = G_MININT;
+    o->y.pos = G_MININT;
     o->w = G_MININT;
     o->h = G_MININT;
     o->monitor = CURRENT_MONITOR;
 
     if ((n = obt_xml_find_node(node, "x")))
     o->w = G_MININT;
     o->h = G_MININT;
     o->monitor = CURRENT_MONITOR;
 
     if ((n = obt_xml_find_node(node, "x")))
-        parse_coord(n, &o->x, &o->xopposite, &o->xcenter);
+        config_parse_gravity_coord(n, &o->x);
 
     if ((n = obt_xml_find_node(node, "y")))
 
     if ((n = obt_xml_find_node(node, "y")))
-        parse_coord(n, &o->y, &o->yopposite, &o->ycenter);
+        config_parse_gravity_coord(n, &o->y);
 
     if ((n = obt_xml_find_node(node, "width"))) {
         gchar *s = obt_xml_node_string(n);
         if (g_ascii_strcasecmp(s, "current") != 0)
 
     if ((n = obt_xml_find_node(node, "width"))) {
         gchar *s = obt_xml_node_string(n);
         if (g_ascii_strcasecmp(s, "current") != 0)
-            o->w = obt_xml_node_int(n);
+            config_parse_relative_number(s, &o->w, &o->w_denom);
         g_free(s);
     }
     if ((n = obt_xml_find_node(node, "height"))) {
         gchar *s = obt_xml_node_string(n);
         if (g_ascii_strcasecmp(s, "current") != 0)
         g_free(s);
     }
     if ((n = obt_xml_find_node(node, "height"))) {
         gchar *s = obt_xml_node_string(n);
         if (g_ascii_strcasecmp(s, "current") != 0)
-            o->h = obt_xml_node_int(n);
+            config_parse_relative_number(s, &o->h, &o->h_denom);
         g_free(s);
     }
 
         g_free(s);
     }
 
@@ -141,9 +121,11 @@ static gboolean run_func(ObActionsData *data, gpointer options)
 
         w = o->w;
         if (w == G_MININT) w = c->area.width;
 
         w = o->w;
         if (w == G_MININT) w = c->area.width;
+        else if (o->w_denom) w = (w * area->width) / o->w_denom;
 
         h = o->h;
         if (h == G_MININT) h = c->area.height;
 
         h = o->h;
         if (h == G_MININT) h = c->area.height;
+        else if (o->h_denom) h = (h * area->height) / o->h_denom;
 
         /* it might not be able to resize how they requested, so find out what
            it will actually be resized to */
 
         /* it might not be able to resize how they requested, so find out what
            it will actually be resized to */
@@ -155,16 +137,20 @@ static gboolean run_func(ObActionsData *data, gpointer options)
         w += c->frame->size.left + c->frame->size.right;
         h += c->frame->size.top + c->frame->size.bottom;
 
         w += c->frame->size.left + c->frame->size.right;
         h += c->frame->size.top + c->frame->size.bottom;
 
-        x = o->x;
-        if (o->xcenter) x = (area->width - w) / 2;
+        x = o->x.pos;
+        if (o->x.denom)
+            x = (x * area->width) / o->x.denom;
+        if (o->x.center) x = (area->width - w) / 2;
         else if (x == G_MININT) x = c->frame->area.x - carea->x;
         else if (x == G_MININT) x = c->frame->area.x - carea->x;
-        else if (o->xopposite) x = area->width - w - x;
+        else if (o->x.opposite) x = area->width - w - x;
         x += area->x;
 
         x += area->x;
 
-        y = o->y;
-        if (o->ycenter) y = (area->height - h) / 2;
+        y = o->y.pos;
+        if (o->y.denom)
+            y = (y * area->height) / o->y.denom;
+        if (o->y.center) y = (area->height - h) / 2;
         else if (y == G_MININT) y = c->frame->area.y - carea->y;
         else if (y == G_MININT) y = c->frame->area.y - carea->y;
-        else if (o->yopposite) y = area->height - h - y;
+        else if (o->y.opposite) y = area->height - h - y;
         y += area->y;
 
         /* get the client's size back */
         y += area->y;
 
         /* get the client's size back */
@@ -193,12 +179,12 @@ static gpointer setup_center_func(xmlNodePtr node)
     Options *o;
 
     o = g_slice_new0(Options);
     Options *o;
 
     o = g_slice_new0(Options);
-    o->x = G_MININT;
-    o->y = G_MININT;
+    o->x.pos = G_MININT;
+    o->y.pos = G_MININT;
     o->w = G_MININT;
     o->h = G_MININT;
     o->monitor = -1;
     o->w = G_MININT;
     o->h = G_MININT;
     o->monitor = -1;
-    o->xcenter = TRUE;
-    o->ycenter = TRUE;
+    o->x.center = TRUE;
+    o->y.center = TRUE;
     return o;
 }
     return o;
 }
index 1d42df2..d5910ce 100644 (file)
@@ -2,13 +2,18 @@
 #include "openbox/client.h"
 #include "openbox/screen.h"
 #include "openbox/frame.h"
 #include "openbox/client.h"
 #include "openbox/screen.h"
 #include "openbox/frame.h"
+#include "openbox/config.h"
 #include <stdlib.h> /* for atoi */
 
 typedef struct {
     gint left;
 #include <stdlib.h> /* for atoi */
 
 typedef struct {
     gint left;
+    gint left_denom;
     gint right;
     gint right;
+    gint right_denom;
     gint top;
     gint top;
+    gint top_denom;
     gint bottom;
     gint bottom;
+    gint bottom_denom;
 } Options;
 
 static gpointer setup_func(xmlNodePtr node);
 } Options;
 
 static gpointer setup_func(xmlNodePtr node);
@@ -20,6 +25,15 @@ void action_resizerelative_startup(void)
     actions_register("ResizeRelative", setup_func, free_func, run_func);
 }
 
     actions_register("ResizeRelative", setup_func, free_func, run_func);
 }
 
+static void xml_node_relative(xmlNodePtr n, gint *num, gint *denom)
+{
+    gchar *s;
+
+    s = obt_xml_node_string(n);
+    config_parse_relative_number(s, num, denom);
+    g_free(s);
+}
+
 static gpointer setup_func(xmlNodePtr node)
 {
     xmlNodePtr n;
 static gpointer setup_func(xmlNodePtr node)
 {
     xmlNodePtr n;
@@ -28,15 +42,15 @@ static gpointer setup_func(xmlNodePtr node)
     o = g_slice_new0(Options);
 
     if ((n = obt_xml_find_node(node, "left")))
     o = g_slice_new0(Options);
 
     if ((n = obt_xml_find_node(node, "left")))
-        o->left = obt_xml_node_int(n);
+        xml_node_relative(n, &o->left, &o->left_denom);
     if ((n = obt_xml_find_node(node, "right")))
     if ((n = obt_xml_find_node(node, "right")))
-        o->right = obt_xml_node_int(n);
+        xml_node_relative(n, &o->right, &o->right_denom);
     if ((n = obt_xml_find_node(node, "top")) ||
         (n = obt_xml_find_node(node, "up")))
     if ((n = obt_xml_find_node(node, "top")) ||
         (n = obt_xml_find_node(node, "up")))
-        o->top = obt_xml_node_int(n);
+        xml_node_relative(n, &o->top, &o->top_denom);
     if ((n = obt_xml_find_node(node, "bottom")) ||
         (n = obt_xml_find_node(node, "down")))
     if ((n = obt_xml_find_node(node, "bottom")) ||
         (n = obt_xml_find_node(node, "down")))
-        o->bottom = obt_xml_node_int(n);
+        xml_node_relative(n, &o->bottom, &o->bottom_denom);
 
     return o;
 }
 
     return o;
 }
@@ -54,17 +68,27 @@ static gboolean run_func(ObActionsData *data, gpointer options)
     if (data->client) {
         ObClient *c = data->client;
         gint x, y, ow, xoff, nw, oh, yoff, nh, lw, lh;
     if (data->client) {
         ObClient *c = data->client;
         gint x, y, ow, xoff, nw, oh, yoff, nh, lw, lh;
+        gint left = o->left, right = o->right, top = o->top, bottom = o->bottom;
+
+        if (o->left_denom)
+            left = (left * c->area.width / c->size_inc.width) / o->left_denom;
+        if (o->right_denom)
+            right = (right * c->area.width / c->size_inc.width) / o->right_denom;
+        if (o->top_denom)
+            top = (top * c->area.height / c->size_inc.height) / o->top_denom;
+        if (o->bottom_denom)
+            bottom = (bottom * c->area.height / c->size_inc.height) / o->bottom_denom;
 
         x = c->area.x;
         y = c->area.y;
         ow = c->area.width;
 
         x = c->area.x;
         y = c->area.y;
         ow = c->area.width;
-        xoff = -o->left * c->size_inc.width;
-        nw = ow + o->right * c->size_inc.width
-            + o->left * c->size_inc.width;
+        xoff = -left * c->size_inc.width;
+        nw = ow + right * c->size_inc.width
+            + left * c->size_inc.width;
         oh = c->area.height;
         oh = c->area.height;
-        yoff = -o->top * c->size_inc.height;
-        nh = oh + o->bottom * c->size_inc.height
-            + o->top * c->size_inc.height;
+        yoff = -top * c->size_inc.height;
+        nh = oh + bottom * c->size_inc.height
+            + top * c->size_inc.height;
 
         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
         xoff = xoff == 0 ? 0 :
 
         client_try_configure(c, &x, &y, &nw, &nh, &lw, &lh, TRUE);
         xoff = xoff == 0 ? 0 :
index 573ce4c..00309d8 100644 (file)
@@ -154,18 +154,29 @@ void config_app_settings_copy_non_defaults(const ObAppSettings *src,
     }
 }
 
     }
 }
 
-static void config_parse_gravity_coord(xmlNodePtr node, GravityCoord *c)
+void config_parse_relative_number(gchar *s, gint *num, gint *denom)
+{
+    *num = strtol(s, &s, 10);
+
+    if (*s == '%') {
+        *denom = 100;
+    } else if (*s == '/') {
+        *denom = atoi(s+1);
+    }
+}
+
+void config_parse_gravity_coord(xmlNodePtr node, GravityCoord *c)
 {
     gchar *s = obt_xml_node_string(node);
     if (!g_ascii_strcasecmp(s, "center"))
         c->center = TRUE;
     else {
 {
     gchar *s = obt_xml_node_string(node);
     if (!g_ascii_strcasecmp(s, "center"))
         c->center = TRUE;
     else {
+        gchar *ps = s;
         if (s[0] == '-')
             c->opposite = TRUE;
         if (s[0] == '-' || s[0] == '+')
         if (s[0] == '-')
             c->opposite = TRUE;
         if (s[0] == '-' || s[0] == '+')
-            c->pos = atoi(s+1);
-        else
-            c->pos = atoi(s);
+            ps++;
+        config_parse_relative_number(ps, &c->pos, &c->denom);
     }
     g_free(s);
 }
     }
     g_free(s);
 }
index 90b6581..890b002 100644 (file)
@@ -215,5 +215,10 @@ ObAppSettings* config_create_app_settings(void);
   src. */
 void config_app_settings_copy_non_defaults(const ObAppSettings *src,
                                            ObAppSettings *dest);
   src. */
 void config_app_settings_copy_non_defaults(const ObAppSettings *src,
                                            ObAppSettings *dest);
+/*! Parses an x geometry style position, with some extensions like ratios
+  and percentages */
+void config_parse_gravity_coord(xmlNodePtr node, GravityCoord *c);
+/*! Parses a rational number or percentage into num and denom */
+void config_parse_relative_number(gchar *s, gint *num, gint *denom);
 
 #endif
 
 #endif
index 6a1725e..003b008 100644 (file)
@@ -24,6 +24,7 @@
 
 typedef struct _GravityCoord {
     gint pos;
 
 typedef struct _GravityCoord {
     gint pos;
+    gint denom;
     gboolean center;
     gboolean opposite;
 } GravityCoord;
     gboolean center;
     gboolean opposite;
 } GravityCoord;
index c39d506..d9919d0 100644 (file)
@@ -406,6 +406,8 @@ static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
             settings->position.x.pos;
     else
         *x = screen->x + settings->position.x.pos;
             settings->position.x.pos;
     else
         *x = screen->x + settings->position.x.pos;
+    if (settings->position.x.denom)
+        *x = (*x * screen->width) / settings->position.x.denom;
 
     if (settings->position.y.center)
         *y = screen->y + screen->height / 2 - client->area.height / 2;
 
     if (settings->position.y.center)
         *y = screen->y + screen->height / 2 - client->area.height / 2;
@@ -414,6 +416,8 @@ static gboolean place_per_app_setting(ObClient *client, gint *x, gint *y,
             settings->position.y.pos;
     else
         *y = screen->y + settings->position.y.pos;
             settings->position.y.pos;
     else
         *y = screen->y + settings->position.y.pos;
+    if (settings->position.y.denom)
+        *y = (*y * screen->height) / settings->position.y.denom;
 
     g_slice_free(Rect, screen);
     return TRUE;
 
     g_slice_free(Rect, screen);
     return TRUE;