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