]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/actions/moveresizeto.c
Merge branch 'backport' into 3.4-working
[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(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         guint 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         switch (mon) {
152         case CURRENT_MONITOR:
153             mon = cmon; break;
154         case ALL_MONITORS:
155             mon = SCREEN_AREA_ALL_MONITORS; break;
156         case NEXT_MONITOR:
157             mon = (cmon + 1 > screen_num_monitors - 1) ? 0 : (cmon + 1); break;
158         case PREV_MONITOR:
159             mon = (cmon == 0) ? (screen_num_monitors - 1) : (cmon - 1); break;
160         default:
161             g_assert_not_reached();
162         }
163
164         area = screen_area(c->desktop, mon, NULL);
165         carea = screen_area(c->desktop, cmon, NULL);
166
167         w = o->w;
168         if (w == G_MININT) w = c->area.width;
169
170         h = o->h;
171         if (h == G_MININT) h = c->area.height;
172
173         /* it might not be able to resize how they requested, so find out what
174            it will actually be resized to */
175         x = c->area.x;
176         y = c->area.y;
177         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
178
179         /* get the frame's size */
180         w += c->frame->size.left + c->frame->size.right;
181         h += c->frame->size.top + c->frame->size.bottom;
182
183         x = o->x;
184         if (o->xcenter) x = (area->width - w) / 2;
185         else if (x == G_MININT) x = c->frame->area.x - carea->x;
186         else if (o->xopposite) x = area->width - w - x;
187         x += area->x;
188
189         y = o->y;
190         if (o->ycenter) y = (area->height - h) / 2;
191         else if (y == G_MININT) y = c->frame->area.y - carea->y;
192         else if (o->yopposite) y = area->height - h - y;
193         y += area->y;
194
195         /* get the client's size back */
196         w -= c->frame->size.left + c->frame->size.right;
197         h -= c->frame->size.top + c->frame->size.bottom;
198
199         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
200         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
201         /* force it on screen if its moving to another monitor */
202         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
203
204         actions_client_move(data, TRUE);
205         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
206         actions_client_move(data, FALSE);
207
208         g_free(area);
209         g_free(carea);
210     }
211
212     return FALSE;
213 }