Make clang happier
[dana/openbox.git] / openbox / actions / desktop.c
1 #include "openbox/actions.h"
2 #include "openbox/screen.h"
3 #include "openbox/client.h"
4 #include <glib.h>
5
6 typedef enum {
7     LAST,
8     RELATIVE,
9     ABSOLUTE
10 } SwitchType;
11
12 typedef struct {
13     SwitchType type;
14     union {
15         struct {
16             guint desktop;
17         } abs;
18
19         struct {
20             gboolean linear;
21             gboolean wrap;
22             ObDirection dir;
23         } rel;
24     } u;
25     gboolean send;
26     gboolean follow;
27 } Options;
28
29 static gpointer setup_go_func(xmlNodePtr node);
30 static gpointer setup_send_func(xmlNodePtr node);
31 static gboolean run_func(ObActionsData *data, gpointer options);
32 /* 3.4-compatibility */
33 static gpointer setup_go_last_func(xmlNodePtr node);
34 static gpointer setup_send_last_func(xmlNodePtr node);
35 static gpointer setup_go_abs_func(xmlNodePtr node);
36 static gpointer setup_send_abs_func(xmlNodePtr node);
37 static gpointer setup_go_next_func(xmlNodePtr node);
38 static gpointer setup_send_next_func(xmlNodePtr node);
39 static gpointer setup_go_prev_func(xmlNodePtr node);
40 static gpointer setup_send_prev_func(xmlNodePtr node);
41 static gpointer setup_go_left_func(xmlNodePtr node);
42 static gpointer setup_send_left_func(xmlNodePtr node);
43 static gpointer setup_go_right_func(xmlNodePtr node);
44 static gpointer setup_send_right_func(xmlNodePtr node);
45 static gpointer setup_go_up_func(xmlNodePtr node);
46 static gpointer setup_send_up_func(xmlNodePtr node);
47 static gpointer setup_go_down_func(xmlNodePtr node);
48 static gpointer setup_send_down_func(xmlNodePtr node);
49
50 void action_desktop_startup(void)
51 {
52     actions_register("GoToDesktop", setup_go_func, g_free, run_func,
53                      NULL, NULL);
54     actions_register("SendToDesktop", setup_send_func, g_free, run_func,
55                      NULL, NULL);
56     /* 3.4-compatibility */
57     actions_register("DesktopLast", setup_go_last_func, g_free, run_func,
58                      NULL, NULL);
59     actions_register("SendToDesktopLast", setup_send_last_func, g_free, run_func,
60                      NULL, NULL);
61     actions_register("Desktop", setup_go_abs_func, g_free, run_func,
62                      NULL, NULL);
63     actions_register("SendToDesktop", setup_send_abs_func, g_free, run_func,
64                      NULL, NULL);
65     actions_register("DesktopNext", setup_go_next_func, g_free, run_func,
66                      NULL, NULL);
67     actions_register("SendToDesktopNext", setup_send_next_func, g_free, run_func,
68                      NULL, NULL);
69     actions_register("DesktopPrevious", setup_go_prev_func, g_free, run_func,
70                      NULL, NULL);
71     actions_register("SendToDesktopPrevious", setup_send_prev_func, g_free, run_func,
72                      NULL, NULL);
73     actions_register("DesktopLeft", setup_go_left_func, g_free, run_func,
74                      NULL, NULL);
75     actions_register("SendToDesktopLeft", setup_send_left_func, g_free, run_func,
76                      NULL, NULL);
77     actions_register("DesktopRight", setup_go_right_func, g_free, run_func,
78                      NULL, NULL);
79     actions_register("SendToDesktopRight", setup_send_right_func, g_free, run_func,
80                      NULL, NULL);
81     actions_register("DesktopUp", setup_go_up_func, g_free, run_func,
82                      NULL, NULL);
83     actions_register("SendToDesktopUp", setup_send_up_func, g_free, run_func,
84                      NULL, NULL);
85     actions_register("DesktopDown", setup_go_down_func, g_free, run_func,
86                      NULL, NULL);
87     actions_register("SendToDesktopDown", setup_send_down_func, g_free, run_func,
88                      NULL, NULL);
89 }
90
91 static gpointer setup_go_func(xmlNodePtr node)
92 {
93     xmlNodePtr n;
94     Options *o;
95
96     o = g_new0(Options, 1);
97     /* don't go anywhere if there are no options given */
98     o->type = ABSOLUTE;
99     o->u.abs.desktop = screen_desktop;
100     /* wrap by default - it's handy! */
101     o->u.rel.wrap = TRUE;
102
103     if ((n = obt_parse_find_node(node, "to"))) {
104         gchar *s = obt_parse_node_string(n);
105         if (!g_ascii_strcasecmp(s, "last"))
106             o->type = LAST;
107         else if (!g_ascii_strcasecmp(s, "next")) {
108             o->type = RELATIVE;
109             o->u.rel.linear = TRUE;
110             o->u.rel.dir = OB_DIRECTION_EAST;
111         }
112         else if (!g_ascii_strcasecmp(s, "previous")) {
113             o->type = RELATIVE;
114             o->u.rel.linear = TRUE;
115             o->u.rel.dir = OB_DIRECTION_WEST;
116         }
117         else if (!g_ascii_strcasecmp(s, "north") ||
118                  !g_ascii_strcasecmp(s, "up")) {
119             o->type = RELATIVE;
120             o->u.rel.dir = OB_DIRECTION_NORTH;
121         }
122         else if (!g_ascii_strcasecmp(s, "south") ||
123                  !g_ascii_strcasecmp(s, "down")) {
124             o->type = RELATIVE;
125             o->u.rel.dir = OB_DIRECTION_SOUTH;
126         }
127         else if (!g_ascii_strcasecmp(s, "west") ||
128                  !g_ascii_strcasecmp(s, "left")) {
129             o->type = RELATIVE;
130             o->u.rel.dir = OB_DIRECTION_WEST;
131         }
132         else if (!g_ascii_strcasecmp(s, "east") ||
133                  !g_ascii_strcasecmp(s, "right")) {
134             o->type = RELATIVE;
135             o->u.rel.dir = OB_DIRECTION_EAST;
136         }
137         else {
138             o->type = ABSOLUTE;
139             o->u.abs.desktop = atoi(s) - 1;
140         }
141         g_free(s);
142     }
143
144     if ((n = obt_parse_find_node(node, "wrap")))
145         o->u.rel.wrap = obt_parse_node_bool(n);
146
147     return o;
148 }
149
150 static gpointer setup_send_func(xmlNodePtr node)
151 {
152     xmlNodePtr n;
153     Options *o;
154
155     o = setup_go_func(node);
156     o->send = TRUE;
157     o->follow = TRUE;
158
159     if ((n = obt_parse_find_node(node, "follow")))
160         o->follow = obt_parse_node_bool(n);
161
162     return o;
163 }
164
165 /* Always return FALSE because its not interactive */
166 static gboolean run_func(ObActionsData *data, gpointer options)
167 {
168     Options *o = options;
169     guint d;
170
171     switch (o->type) {
172     case LAST:
173         d = screen_last_desktop;
174         break;
175     case ABSOLUTE:
176         d = o->u.abs.desktop;
177         break;
178     case RELATIVE:
179         d = screen_find_desktop(screen_desktop,
180                                 o->u.rel.dir, o->u.rel.wrap, o->u.rel.linear);
181         break;
182     default:
183         g_assert_not_reached();
184     }
185
186     if (d < screen_num_desktops && d != screen_desktop) {
187         gboolean go = TRUE;
188
189         actions_client_move(data, TRUE);
190         if (o->send && data->client && client_normal(data->client)) {
191             client_set_desktop(data->client, d, o->follow, FALSE);
192             go = o->follow;
193         }
194
195         if (go) {
196             screen_set_desktop(d, TRUE);
197             if (data->client)
198                 client_bring_helper_windows(data->client);
199         }
200
201         actions_client_move(data, FALSE);
202     }
203     return FALSE;
204 }
205
206 /* 3.4-compatilibity */
207 static gpointer setup_follow(xmlNodePtr node)
208 {
209     xmlNodePtr n;
210     Options *o = g_new0(Options, 1);
211     o->send = TRUE;
212     o->follow = TRUE;
213     if ((n = obt_parse_find_node(node, "follow")))
214         o->follow = obt_parse_node_bool(n);
215     return o;
216 }
217
218 static gpointer setup_go_last_func(xmlNodePtr node)
219 {
220     Options *o = g_new0(Options, 1);
221     o->type = LAST;
222     return o;
223 }
224
225 static gpointer setup_send_last_func(xmlNodePtr node)
226 {
227     Options *o = setup_follow(node);
228     o->type = LAST;
229     return o;
230 }
231
232 static gpointer setup_go_abs_func(xmlNodePtr node)
233 {
234     xmlNodePtr n;
235     Options *o = g_new0(Options, 1);
236     o->type = ABSOLUTE;
237     if ((n = obt_parse_find_node(node, "desktop")))
238         o->u.abs.desktop = obt_parse_node_int(n) - 1;
239     else
240         o->u.abs.desktop = screen_desktop;
241     return o;
242 }
243
244 static gpointer setup_send_abs_func(xmlNodePtr node)
245 {
246     xmlNodePtr n;
247     Options *o = setup_follow(node);
248     o->type = ABSOLUTE;
249     if ((n = obt_parse_find_node(node, "desktop")))
250         o->u.abs.desktop = obt_parse_node_int(n) - 1;
251     else
252         o->u.abs.desktop = screen_desktop;
253     return o;
254 }
255
256 static void setup_rel(Options *o, xmlNodePtr node, gboolean lin, ObDirection dir)
257 {
258     xmlNodePtr n;
259
260     o->type = RELATIVE;
261     o->u.rel.linear = lin;
262     o->u.rel.dir = dir;
263     o->u.rel.wrap = TRUE;
264
265     if ((n = obt_parse_find_node(node, "wrap")))
266         o->u.rel.wrap = obt_parse_node_bool(n);
267 }
268
269 static gpointer setup_go_next_func(xmlNodePtr node)
270 {
271     Options *o = g_new0(Options, 1);
272     setup_rel(o, node, TRUE, OB_DIRECTION_EAST);
273     return o;
274 }
275
276 static gpointer setup_send_next_func(xmlNodePtr node)
277 {
278     Options *o = setup_follow(node);
279     setup_rel(o, node, TRUE, OB_DIRECTION_EAST);
280     return o;
281 }
282
283 static gpointer setup_go_prev_func(xmlNodePtr node)
284 {
285     Options *o = g_new0(Options, 1);
286     setup_rel(o, node, TRUE, OB_DIRECTION_WEST);
287     return o;
288 }
289
290 static gpointer setup_send_prev_func(xmlNodePtr node)
291 {
292     Options *o = setup_follow(node);
293     setup_rel(o, node, TRUE, OB_DIRECTION_WEST);
294     return o;
295 }
296
297 static gpointer setup_go_left_func(xmlNodePtr node)
298 {
299     Options *o = g_new0(Options, 1);
300     setup_rel(o, node, FALSE, OB_DIRECTION_WEST);
301     return o;
302 }
303
304 static gpointer setup_send_left_func(xmlNodePtr node)
305 {
306     Options *o = setup_follow(node);
307     setup_rel(o, node, FALSE, OB_DIRECTION_WEST);
308     return o;
309 }
310
311 static gpointer setup_go_right_func(xmlNodePtr node)
312 {
313     Options *o = g_new0(Options, 1);
314     setup_rel(o, node, FALSE, OB_DIRECTION_EAST);
315     return o;
316 }
317
318 static gpointer setup_send_right_func(xmlNodePtr node)
319 {
320     Options *o = setup_follow(node);
321     setup_rel(o, node, FALSE, OB_DIRECTION_EAST);
322     return o;
323 }
324
325 static gpointer setup_go_up_func(xmlNodePtr node)
326 {
327     Options *o = g_new0(Options, 1);
328     setup_rel(o, node, FALSE, OB_DIRECTION_NORTH);
329     return o;
330 }
331
332 static gpointer setup_send_up_func(xmlNodePtr node)
333 {
334     Options *o = setup_follow(node);
335     setup_rel(o, node, FALSE, OB_DIRECTION_NORTH);
336     return o;
337 }
338
339 static gpointer setup_go_down_func(xmlNodePtr node)
340 {
341     Options *o = g_new0(Options, 1);
342     setup_rel(o, node, FALSE, OB_DIRECTION_SOUTH);
343     return o;
344 }
345
346 static gpointer setup_send_down_func(xmlNodePtr node)
347 {
348     Options *o = setup_follow(node);
349     setup_rel(o, node, FALSE, OB_DIRECTION_SOUTH);
350     return o;
351 }