Merge branch 'backport' into work
[mikachu/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     }
183
184     if (d < screen_num_desktops && d != screen_desktop) {
185         gboolean go = TRUE;
186
187         actions_client_move(data, TRUE);
188         if (o->send && data->client && client_normal(data->client)) {
189             client_set_desktop(data->client, d, o->follow, FALSE);
190             go = o->follow;
191         }
192
193         if (go) {
194             screen_set_desktop(d, TRUE);
195             if (data->client)
196                 client_bring_helper_windows(data->client);
197         }
198
199         actions_client_move(data, FALSE);
200     }
201     return FALSE;
202 }
203
204 /* 3.4-compatilibity */
205 static gpointer setup_follow(xmlNodePtr node)
206 {
207     xmlNodePtr n;
208     Options *o = g_new0(Options, 1);
209     o->send = TRUE;
210     o->follow = TRUE;
211     if ((n = obt_parse_find_node(node, "follow")))
212         o->follow = obt_parse_node_bool(n);
213     return o;
214 }
215
216 static gpointer setup_go_last_func(xmlNodePtr node)
217 {
218     Options *o = g_new0(Options, 1);
219     o->type = LAST;
220     return o;
221 }
222
223 static gpointer setup_send_last_func(xmlNodePtr node)
224 {
225     Options *o = setup_follow(node);
226     o->type = LAST;
227     return o;
228 }
229
230 static gpointer setup_go_abs_func(xmlNodePtr node)
231 {
232     xmlNodePtr n;
233     Options *o = g_new0(Options, 1);
234     o->type = ABSOLUTE;
235     if ((n = obt_parse_find_node(node, "desktop")))
236         o->u.abs.desktop = obt_parse_node_int(n) - 1;
237     else
238         o->u.abs.desktop = screen_desktop;
239     return o;
240 }
241
242 static gpointer setup_send_abs_func(xmlNodePtr node)
243 {
244     xmlNodePtr n;
245     Options *o = setup_follow(node);
246     o->type = ABSOLUTE;
247     if ((n = obt_parse_find_node(node, "desktop")))
248         o->u.abs.desktop = obt_parse_node_int(n) - 1;
249     else
250         o->u.abs.desktop = screen_desktop;
251     return o;
252 }
253
254 static void setup_rel(Options *o, xmlNodePtr node, gboolean lin, ObDirection dir)
255 {
256     xmlNodePtr n;
257
258     o->type = RELATIVE;
259     o->u.rel.linear = lin;
260     o->u.rel.dir = dir;
261     o->u.rel.wrap = TRUE;
262
263     if ((n = obt_parse_find_node(node, "wrap")))
264         o->u.rel.wrap = obt_parse_node_bool(n);
265 }
266
267 static gpointer setup_go_next_func(xmlNodePtr node)
268 {
269     Options *o = g_new0(Options, 1);
270     setup_rel(o, node, TRUE, OB_DIRECTION_EAST);
271     return o;
272 }
273
274 static gpointer setup_send_next_func(xmlNodePtr node)
275 {
276     Options *o = setup_follow(node);
277     setup_rel(o, node, TRUE, OB_DIRECTION_EAST);
278     return o;
279 }
280
281 static gpointer setup_go_prev_func(xmlNodePtr node)
282 {
283     Options *o = g_new0(Options, 1);
284     setup_rel(o, node, TRUE, OB_DIRECTION_WEST);
285     return o;
286 }
287
288 static gpointer setup_send_prev_func(xmlNodePtr node)
289 {
290     Options *o = setup_follow(node);
291     setup_rel(o, node, TRUE, OB_DIRECTION_WEST);
292     return o;
293 }
294
295 static gpointer setup_go_left_func(xmlNodePtr node)
296 {
297     Options *o = g_new0(Options, 1);
298     setup_rel(o, node, FALSE, OB_DIRECTION_WEST);
299     return o;
300 }
301
302 static gpointer setup_send_left_func(xmlNodePtr node)
303 {
304     Options *o = setup_follow(node);
305     setup_rel(o, node, FALSE, OB_DIRECTION_WEST);
306     return o;
307 }
308
309 static gpointer setup_go_right_func(xmlNodePtr node)
310 {
311     Options *o = g_new0(Options, 1);
312     setup_rel(o, node, FALSE, OB_DIRECTION_EAST);
313     return o;
314 }
315
316 static gpointer setup_send_right_func(xmlNodePtr node)
317 {
318     Options *o = setup_follow(node);
319     setup_rel(o, node, FALSE, OB_DIRECTION_EAST);
320     return o;
321 }
322
323 static gpointer setup_go_up_func(xmlNodePtr node)
324 {
325     Options *o = g_new0(Options, 1);
326     setup_rel(o, node, FALSE, OB_DIRECTION_NORTH);
327     return o;
328 }
329
330 static gpointer setup_send_up_func(xmlNodePtr node)
331 {
332     Options *o = setup_follow(node);
333     setup_rel(o, node, FALSE, OB_DIRECTION_NORTH);
334     return o;
335 }
336
337 static gpointer setup_go_down_func(xmlNodePtr node)
338 {
339     Options *o = g_new0(Options, 1);
340     setup_rel(o, node, FALSE, OB_DIRECTION_SOUTH);
341     return o;
342 }
343
344 static gpointer setup_send_down_func(xmlNodePtr node)
345 {
346     Options *o = setup_follow(node);
347     setup_rel(o, node, FALSE, OB_DIRECTION_SOUTH);
348     return o;
349 }