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