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