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