Merge branch 'backport' into work
[dana/openbox.git] / openbox / actions / moveresizeto.c
1 #include "openbox/actions.h"
2 #include "openbox/client.h"
3 #include "openbox/screen.h"
4 #include "openbox/frame.h"
5 #include <stdlib.h> /* for atoi */
6
7 enum {
8     CURRENT_MONITOR = -1,
9     ALL_MONITORS = -2
10 };
11
12 typedef struct {
13     gboolean xcenter;
14     gboolean ycenter;
15     gboolean xopposite;
16     gboolean yopposite;
17     gint x;
18     gint y;
19     gint w;
20     gint h;
21     gint monitor;
22 } Options;
23
24 static gpointer setup_func(xmlNodePtr node);
25 static gboolean run_func(ObActionsData *data, gpointer options);
26
27 void action_moveresizeto_startup(void)
28 {
29     actions_register("MoveResizeTo", setup_func, g_free, run_func, NULL, NULL);
30 }
31
32 static void parse_coord(xmlNodePtr n, gint *pos,
33                         gboolean *opposite, gboolean *center)
34 {
35     gchar *s = obt_parse_node_string(n);
36     if (g_ascii_strcasecmp(s, "current") != 0) {
37         if (!g_ascii_strcasecmp(s, "center"))
38             *center = TRUE;
39         else {
40             if (s[0] == '-')
41                 *opposite = TRUE;
42             if (s[0] == '-' || s[0] == '+')
43                 *pos = atoi(s+1);
44             else
45                 *pos = atoi(s);
46         }
47     }
48     g_free(s);
49 }
50
51 static gpointer setup_func(xmlNodePtr node)
52 {
53     xmlNodePtr n;
54     Options *o;
55
56     o = g_new0(Options, 1);
57     o->x = G_MININT;
58     o->y = G_MININT;
59     o->w = G_MININT;
60     o->h = G_MININT;
61     o->monitor = CURRENT_MONITOR;
62
63     if ((n = obt_parse_find_node(node, "x")))
64         parse_coord(n, &o->x, &o->xopposite, &o->xcenter);
65
66     if ((n = obt_parse_find_node(node, "y")))
67         parse_coord(n, &o->y, &o->yopposite, &o->ycenter);
68
69     if ((n = obt_parse_find_node(node, "width"))) {
70         gchar *s = obt_parse_node_string(n);
71         if (g_ascii_strcasecmp(s, "current") != 0)
72             o->w = obt_parse_node_int(n);
73         g_free(s);
74     }
75     if ((n = obt_parse_find_node(node, "height"))) {
76         gchar *s = obt_parse_node_string(n);
77         if (g_ascii_strcasecmp(s, "current") != 0)
78             o->h = obt_parse_node_int(n);
79         g_free(s);
80     }
81
82     if ((n = obt_parse_find_node(node, "monitor"))) {
83         gchar *s = obt_parse_node_string(n);
84         if (g_ascii_strcasecmp(s, "current") != 0) {
85             if (!g_ascii_strcasecmp(s, "all"))
86                 o->monitor = ALL_MONITORS;
87             else
88                 o->monitor = obt_parse_node_int(n) - 1;
89         }
90         g_free(s);
91     }
92
93     return o;
94 }
95
96 /* Always return FALSE because its not interactive */
97 static gboolean run_func(ObActionsData *data, gpointer options)
98 {
99     Options *o = options;
100
101     if (data->client) {
102         Rect *area, *carea;
103         ObClient *c;
104         gint mon, cmon;
105         gint x, y, lw, lh, w, h;
106
107         c = data->client;
108         mon = o->monitor;
109         cmon = client_monitor(c);
110         if (mon == CURRENT_MONITOR) mon = cmon;
111         else if (mon == ALL_MONITORS) mon = SCREEN_AREA_ALL_MONITORS;
112         area = screen_area(c->desktop, mon, NULL);
113         carea = screen_area(c->desktop, cmon, NULL);
114
115         w = o->w;
116         if (w == G_MININT) w = c->area.width;
117
118         h = o->h;
119         if (h == G_MININT) h = c->area.height;
120
121         /* it might not be able to resize how they requested, so find out what
122            it will actually be resized to */
123         x = c->area.x;
124         y = c->area.y;
125         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
126
127         /* get the frame's size */
128         w += c->frame->size.left + c->frame->size.right;
129         h += c->frame->size.top + c->frame->size.bottom;
130
131         x = o->x;
132         if (o->xcenter) x = (area->width - w) / 2;
133         else if (x == G_MININT) x = c->frame->area.x - carea->x;
134         else if (o->xopposite) x = area->width - w - x;
135         x += area->x;
136
137         y = o->y;
138         if (o->ycenter) y = (area->height - h) / 2;
139         else if (y == G_MININT) y = c->frame->area.y - carea->y;
140         else if (o->yopposite) y = area->height - h - y;
141         y += area->y;
142
143         /* get the client's size back */
144         w -= c->frame->size.left + c->frame->size.right;
145         h -= c->frame->size.top + c->frame->size.bottom;
146
147         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
148         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
149         /* force it on screen if its moving to another monitor */
150         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
151
152         actions_client_move(data, TRUE);
153         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
154         actions_client_move(data, FALSE);
155
156         g_free(area);
157         g_free(carea);
158     }
159
160     return FALSE;
161 }