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