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