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