Merge branch 'backport' into work
[mikachu/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     NEXT_MONITOR = -3,
11     PREV_MONITOR = -4
12 };
13
14 typedef struct {
15     gboolean xcenter;
16     gboolean ycenter;
17     gboolean xopposite;
18     gboolean yopposite;
19     gint x;
20     gint y;
21     gint w;
22     gint h;
23     gint monitor;
24 } Options;
25
26 static gpointer setup_func(xmlNodePtr node);
27 static gboolean run_func(ObActionsData *data, gpointer options);
28 /* 3.4-compatibility */
29 static gpointer setup_center_func(xmlNodePtr node);
30
31 void action_moveresizeto_startup(void)
32 {
33     actions_register("MoveResizeTo", setup_func, g_free, run_func);
34     /* 3.4-compatibility */
35     actions_register("MoveToCenter", setup_center_func, g_free, run_func);
36 }
37
38 static void parse_coord(xmlNodePtr n, gint *pos,
39                         gboolean *opposite, gboolean *center)
40 {
41     gchar *s = obt_xml_node_string(n);
42     if (g_ascii_strcasecmp(s, "current") != 0) {
43         if (!g_ascii_strcasecmp(s, "center"))
44             *center = TRUE;
45         else {
46             if (s[0] == '-')
47                 *opposite = TRUE;
48             if (s[0] == '-' || s[0] == '+')
49                 *pos = atoi(s+1);
50             else
51                 *pos = atoi(s);
52         }
53     }
54     g_free(s);
55 }
56
57 static gpointer setup_func(xmlNodePtr node)
58 {
59     xmlNodePtr n;
60     Options *o;
61
62     o = g_new0(Options, 1);
63     o->x = G_MININT;
64     o->y = G_MININT;
65     o->w = G_MININT;
66     o->h = G_MININT;
67     o->monitor = CURRENT_MONITOR;
68
69     if ((n = obt_xml_find_node(node, "x")))
70         parse_coord(n, &o->x, &o->xopposite, &o->xcenter);
71
72     if ((n = obt_xml_find_node(node, "y")))
73         parse_coord(n, &o->y, &o->yopposite, &o->ycenter);
74
75     if ((n = obt_xml_find_node(node, "width"))) {
76         gchar *s = obt_xml_node_string(n);
77         if (g_ascii_strcasecmp(s, "current") != 0)
78             o->w = obt_xml_node_int(n);
79         g_free(s);
80     }
81     if ((n = obt_xml_find_node(node, "height"))) {
82         gchar *s = obt_xml_node_string(n);
83         if (g_ascii_strcasecmp(s, "current") != 0)
84             o->h = obt_xml_node_int(n);
85         g_free(s);
86     }
87
88     if ((n = obt_xml_find_node(node, "monitor"))) {
89         gchar *s = obt_xml_node_string(n);
90         if (g_ascii_strcasecmp(s, "current") != 0) {
91             if (!g_ascii_strcasecmp(s, "all"))
92                 o->monitor = ALL_MONITORS;
93             else if(!g_ascii_strcasecmp(s, "next"))
94                 o->monitor = NEXT_MONITOR;
95             else if(!g_ascii_strcasecmp(s, "prev"))
96                 o->monitor = PREV_MONITOR;
97             else
98                 o->monitor = obt_xml_node_int(n) - 1;
99         }
100         g_free(s);
101     }
102
103     return o;
104 }
105
106 /* Always return FALSE because its not interactive */
107 static gboolean run_func(ObActionsData *data, gpointer options)
108 {
109     Options *o = options;
110
111     if (data->client) {
112         Rect *area, *carea;
113         ObClient *c;
114         guint mon, cmon;
115         gint x, y, lw, lh, w, h;
116
117         c = data->client;
118         mon = o->monitor;
119         cmon = client_monitor(c);
120         switch (mon) {
121         case CURRENT_MONITOR:
122             mon = cmon; break;
123         case ALL_MONITORS:
124             mon = SCREEN_AREA_ALL_MONITORS; break;
125         case NEXT_MONITOR:
126             mon = (cmon + 1 > screen_num_monitors - 1) ? 0 : (cmon + 1); break;
127         case PREV_MONITOR:
128             mon = (cmon == 0) ? (screen_num_monitors - 1) : (cmon - 1); break;
129         default:
130             g_assert_not_reached();
131         }
132
133         area = screen_area(c->desktop, mon, NULL);
134         carea = screen_area(c->desktop, cmon, NULL);
135
136         w = o->w;
137         if (w == G_MININT) w = c->area.width;
138
139         h = o->h;
140         if (h == G_MININT) h = c->area.height;
141
142         /* it might not be able to resize how they requested, so find out what
143            it will actually be resized to */
144         x = c->area.x;
145         y = c->area.y;
146         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
147
148         /* get the frame's size */
149         w += c->frame->size.left + c->frame->size.right;
150         h += c->frame->size.top + c->frame->size.bottom;
151
152         x = o->x;
153         if (o->xcenter) x = (area->width - w) / 2;
154         else if (x == G_MININT) x = c->frame->area.x - carea->x;
155         else if (o->xopposite) x = area->width - w - x;
156         x += area->x;
157
158         y = o->y;
159         if (o->ycenter) y = (area->height - h) / 2;
160         else if (y == G_MININT) y = c->frame->area.y - carea->y;
161         else if (o->yopposite) y = area->height - h - y;
162         y += area->y;
163
164         /* get the client's size back */
165         w -= c->frame->size.left + c->frame->size.right;
166         h -= c->frame->size.top + c->frame->size.bottom;
167
168         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
169         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
170         /* force it on screen if its moving to another monitor */
171         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
172
173         actions_client_move(data, TRUE);
174         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
175         actions_client_move(data, FALSE);
176
177         g_free(area);
178         g_free(carea);
179     }
180
181     return FALSE;
182 }
183
184 /* 3.4-compatibility */
185 static gpointer setup_center_func(xmlNodePtr node)
186 {
187     Options *o;
188
189     o = g_new0(Options, 1);
190     o->x = G_MININT;
191     o->y = G_MININT;
192     o->w = G_MININT;
193     o->h = G_MININT;
194     o->monitor = -1;
195     o->xcenter = TRUE;
196     o->ycenter = TRUE;
197     return o;
198 }