a3a1f6b9c121b00f6158a5ef42d14a1fb6681d7f
[mikachu/openbox.git] / openbox / actions / desktop.c
1 #include "openbox/actions.h"
2 #include "openbox/screen.h"
3 #include "openbox/client.h"
4 #include "openbox/openbox.h"
5 #include "obt/keyboard.h"
6
7 typedef enum {
8     LAST,
9     CURRENT,
10     RELATIVE,
11     ABSOLUTE
12 } SwitchType;
13
14 typedef struct {
15     SwitchType type;
16     union {
17         struct {
18             guint desktop;
19         } abs;
20
21         struct {
22             gboolean linear;
23             gboolean wrap;
24             ObDirection dir;
25         } rel;
26     } u;
27     gboolean send;
28     gboolean follow;
29     gboolean interactive;
30 } Options;
31
32 static gpointer setup_go_func(xmlNodePtr node,
33                               ObActionsIPreFunc *pre,
34                               ObActionsIInputFunc *input,
35                               ObActionsICancelFunc *cancel,
36                               ObActionsIPostFunc *post);
37 static gpointer setup_send_func(xmlNodePtr node,
38                                 ObActionsIPreFunc *pre,
39                                 ObActionsIInputFunc *input,
40                                 ObActionsICancelFunc *cancel,
41                                 ObActionsIPostFunc *post);
42 static void free_func(gpointer o);
43 static gboolean run_func(ObActionsData *data, gpointer options);
44
45 static gboolean i_pre_func(guint state, gpointer options);
46 static gboolean i_input_func(guint initial_state,
47                              XEvent *e,
48                              ObtIC *ic,
49                              gpointer options,
50                              gboolean *used);
51 static void i_post_func(gpointer options);
52
53 /* 3.4-compatibility */
54 static gpointer setup_go_last_func(xmlNodePtr node);
55 static gpointer setup_send_last_func(xmlNodePtr node);
56 static gpointer setup_go_abs_func(xmlNodePtr node);
57 static gpointer setup_go_next_func(xmlNodePtr node,
58                                    ObActionsIPreFunc *pre,
59                                    ObActionsIInputFunc *input,
60                                    ObActionsICancelFunc *cancel,
61                                    ObActionsIPostFunc *post);
62 static gpointer setup_send_next_func(xmlNodePtr node,
63                                      ObActionsIPreFunc *pre,
64                                      ObActionsIInputFunc *input,
65                                      ObActionsICancelFunc *cancel,
66                                      ObActionsIPostFunc *post);
67 static gpointer setup_go_prev_func(xmlNodePtr node,
68                                    ObActionsIPreFunc *pre,
69                                    ObActionsIInputFunc *input,
70                                    ObActionsICancelFunc *cancel,
71                                    ObActionsIPostFunc *post);
72 static gpointer setup_send_prev_func(xmlNodePtr node,
73                                      ObActionsIPreFunc *pre,
74                                      ObActionsIInputFunc *input,
75                                      ObActionsICancelFunc *cancel,
76                                      ObActionsIPostFunc *post);
77 static gpointer setup_go_left_func(xmlNodePtr node,
78                                    ObActionsIPreFunc *pre,
79                                    ObActionsIInputFunc *input,
80                                    ObActionsICancelFunc *cancel,
81                                    ObActionsIPostFunc *post);
82 static gpointer setup_send_left_func(xmlNodePtr node,
83                                      ObActionsIPreFunc *pre,
84                                      ObActionsIInputFunc *input,
85                                      ObActionsICancelFunc *cancel,
86                                      ObActionsIPostFunc *post);
87 static gpointer setup_go_right_func(xmlNodePtr node,
88                                     ObActionsIPreFunc *pre,
89                                     ObActionsIInputFunc *input,
90                                     ObActionsICancelFunc *cancel,
91                                     ObActionsIPostFunc *post);
92 static gpointer setup_send_right_func(xmlNodePtr node,
93                                       ObActionsIPreFunc *pre,
94                                       ObActionsIInputFunc *input,
95                                       ObActionsICancelFunc *cancel,
96                                       ObActionsIPostFunc *post);
97 static gpointer setup_go_up_func(xmlNodePtr node,
98                                  ObActionsIPreFunc *pre,
99                                  ObActionsIInputFunc *input,
100                                  ObActionsICancelFunc *cancel,
101                                  ObActionsIPostFunc *post);
102 static gpointer setup_send_up_func(xmlNodePtr node,
103                                    ObActionsIPreFunc *pre,
104                                    ObActionsIInputFunc *input,
105                                    ObActionsICancelFunc *cancel,
106                                    ObActionsIPostFunc *post);
107 static gpointer setup_go_down_func(xmlNodePtr node,
108                                    ObActionsIPreFunc *pre,
109                                    ObActionsIInputFunc *input,
110                                    ObActionsICancelFunc *cancel,
111                                    ObActionsIPostFunc *post);
112 static gpointer setup_send_down_func(xmlNodePtr node,
113                                      ObActionsIPreFunc *pre,
114                                      ObActionsIInputFunc *input,
115                                      ObActionsICancelFunc *cancel,
116                                      ObActionsIPostFunc *post);
117
118 void action_desktop_startup(void)
119 {
120     actions_register_i("GoToDesktop", setup_go_func, free_func, run_func);
121     actions_register_i("SendToDesktop", setup_send_func, free_func, run_func);
122     /* 3.4-compatibility */
123     actions_register("DesktopLast", setup_go_last_func, free_func, run_func);
124     actions_register("SendToDesktopLast", setup_send_last_func,
125                      free_func, run_func);
126     actions_register("Desktop", setup_go_abs_func, free_func, run_func);
127     actions_register_i("DesktopNext", setup_go_next_func, free_func, run_func);
128     actions_register_i("SendToDesktopNext", setup_send_next_func,
129                        free_func, run_func);
130     actions_register_i("DesktopPrevious", setup_go_prev_func,
131                        free_func, run_func);
132     actions_register_i("SendToDesktopPrevious", setup_send_prev_func,
133                        free_func, run_func);
134     actions_register_i("DesktopLeft", setup_go_left_func, free_func, run_func);
135     actions_register_i("SendToDesktopLeft", setup_send_left_func,
136                        free_func, run_func);
137     actions_register_i("DesktopRight", setup_go_right_func,
138                        free_func, run_func);
139     actions_register_i("SendToDesktopRight", setup_send_right_func,
140                        free_func, run_func);
141     actions_register_i("DesktopUp", setup_go_up_func, free_func, run_func);
142     actions_register_i("SendToDesktopUp", setup_send_up_func,
143                        free_func, run_func);
144     actions_register_i("DesktopDown", setup_go_down_func, free_func, run_func);
145     actions_register_i("SendToDesktopDown", setup_send_down_func,
146                        free_func, run_func);
147 }
148
149 static gpointer setup_func(xmlNodePtr node,
150                            ObActionsIPreFunc *pre,
151                            ObActionsIInputFunc *input,
152                            ObActionsICancelFunc *cancel,
153                            ObActionsIPostFunc *post)
154 {
155     xmlNodePtr n;
156     Options *o;
157
158     o = g_slice_new0(Options);
159     /* don't go anywhere if there are no options given */
160     o->type = ABSOLUTE;
161     o->u.abs.desktop = screen_desktop;
162     /* wrap by default - it's handy! */
163     o->u.rel.wrap = TRUE;
164
165     if ((n = obt_xml_find_node(node, "to"))) {
166         gchar *s = obt_xml_node_string(n);
167         if (!g_ascii_strcasecmp(s, "last"))
168             o->type = LAST;
169         else if (!g_ascii_strcasecmp(s, "current"))
170             o->type = CURRENT;
171         else if (!g_ascii_strcasecmp(s, "next")) {
172             o->type = RELATIVE;
173             o->u.rel.linear = TRUE;
174             o->u.rel.dir = OB_DIRECTION_EAST;
175         }
176         else if (!g_ascii_strcasecmp(s, "previous")) {
177             o->type = RELATIVE;
178             o->u.rel.linear = TRUE;
179             o->u.rel.dir = OB_DIRECTION_WEST;
180         }
181         else if (!g_ascii_strcasecmp(s, "north") ||
182                  !g_ascii_strcasecmp(s, "up")) {
183             o->type = RELATIVE;
184             o->u.rel.dir = OB_DIRECTION_NORTH;
185         }
186         else if (!g_ascii_strcasecmp(s, "south") ||
187                  !g_ascii_strcasecmp(s, "down")) {
188             o->type = RELATIVE;
189             o->u.rel.dir = OB_DIRECTION_SOUTH;
190         }
191         else if (!g_ascii_strcasecmp(s, "west") ||
192                  !g_ascii_strcasecmp(s, "left")) {
193             o->type = RELATIVE;
194             o->u.rel.dir = OB_DIRECTION_WEST;
195         }
196         else if (!g_ascii_strcasecmp(s, "east") ||
197                  !g_ascii_strcasecmp(s, "right")) {
198             o->type = RELATIVE;
199             o->u.rel.dir = OB_DIRECTION_EAST;
200         }
201         else {
202             o->type = ABSOLUTE;
203             o->u.abs.desktop = atoi(s) - 1;
204         }
205         g_free(s);
206     }
207
208     if ((n = obt_xml_find_node(node, "wrap")))
209         o->u.rel.wrap = obt_xml_node_bool(n);
210
211     return o;
212 }
213
214
215 static gpointer setup_go_func(xmlNodePtr node,
216                               ObActionsIPreFunc *pre,
217                               ObActionsIInputFunc *input,
218                               ObActionsICancelFunc *cancel,
219                               ObActionsIPostFunc *post)
220 {
221     Options *o;
222
223     o = setup_func(node, pre, input, cancel, post);
224     if (o->type == RELATIVE) {
225         o->interactive = TRUE;
226         *pre = i_pre_func;
227         *input = i_input_func;
228         *post = i_post_func;
229     }
230
231     return o;
232 }
233
234 static gpointer setup_send_func(xmlNodePtr node,
235                                 ObActionsIPreFunc *pre,
236                                 ObActionsIInputFunc *input,
237                                 ObActionsICancelFunc *cancel,
238                                 ObActionsIPostFunc *post)
239 {
240     xmlNodePtr n;
241     Options *o;
242
243     o = setup_func(node, pre, input, cancel, post);
244     if ((n = obt_xml_find_node(node, "desktop"))) {
245         /* 3.4 compatibility */
246         o->u.abs.desktop = obt_xml_node_int(n) - 1;
247         o->type = ABSOLUTE;
248     }
249     o->send = TRUE;
250     o->follow = TRUE;
251
252     if ((n = obt_xml_find_node(node, "follow")))
253         o->follow = obt_xml_node_bool(n);
254
255     if (o->type == RELATIVE && o->follow) {
256         o->interactive = TRUE;
257         *pre = i_pre_func;
258         *input = i_input_func;
259         *post = i_post_func;
260     }
261
262     return o;
263 }
264
265 static void free_func(gpointer o)
266 {
267     g_slice_free(Options, o);
268 }
269
270 static gboolean run_func(ObActionsData *data, gpointer options)
271 {
272     Options *o = options;
273     guint d;
274
275     switch (o->type) {
276     case LAST:
277         d = screen_last_desktop;
278         break;
279     case CURRENT:
280         d = screen_desktop;
281         break;
282     case ABSOLUTE:
283         d = o->u.abs.desktop;
284         break;
285     case RELATIVE:
286         d = screen_find_desktop(screen_desktop,
287                                 o->u.rel.dir, o->u.rel.wrap, o->u.rel.linear);
288         break;
289     default:
290         g_assert_not_reached();
291     }
292
293     if (d < screen_num_desktops &&
294         (d != screen_desktop ||
295          (data->client && data->client->desktop != screen_desktop))) {
296         gboolean go = TRUE;
297
298         actions_client_move(data, TRUE);
299         if (o->send && data->client && client_normal(data->client)) {
300             client_set_desktop(data->client, d, o->follow, FALSE);
301             go = o->follow;
302         }
303
304         if (go) {
305             screen_set_desktop(d, TRUE);
306             if (data->client)
307                 client_bring_helper_windows(data->client);
308         }
309
310         actions_client_move(data, FALSE);
311     }
312
313     return o->interactive;
314 }
315
316 static gboolean i_input_func(guint initial_state,
317                              XEvent *e,
318                              ObtIC *ic,
319                              gpointer options,
320                              gboolean *used)
321 {
322     guint mods;
323
324     mods = obt_keyboard_only_modmasks(e->xkey.state);
325     if (e->type == KeyRelease) {
326         /* remove from the state the mask of the modifier key being
327            released, if it is a modifier key being released that is */
328         mods &= ~obt_keyboard_keyevent_to_modmask(e);
329     }
330
331     if (e->type == KeyPress) {
332         KeySym sym = obt_keyboard_keypress_to_keysym(e);
333
334         /* Escape cancels no matter what */
335         if (sym == XK_Escape)
336             return FALSE;
337
338         /* There were no modifiers and they pressed enter */
339         else if ((sym == XK_Return || sym == XK_KP_Enter) && !initial_state)
340             return FALSE;
341     }
342     /* They released the modifiers */
343     else if (e->type == KeyRelease && initial_state && !(mods & initial_state))
344     {
345         return FALSE;
346     }
347
348     return TRUE;
349 }
350
351 static gboolean i_pre_func(guint initial_state, gpointer options)
352 {
353     if (!initial_state) {
354         Options *o = options;
355         o->interactive = FALSE;
356         return FALSE;
357     }
358     else {
359         screen_show_desktop_popup(screen_desktop, TRUE);
360         return TRUE;
361     }
362 }
363
364 static void i_post_func(gpointer options)
365 {
366     screen_hide_desktop_popup();
367 }
368
369 /* 3.4-compatilibity */
370 static gpointer setup_follow(xmlNodePtr node)
371 {
372     xmlNodePtr n;
373     Options *o = g_slice_new0(Options);
374     o->send = TRUE;
375     o->follow = TRUE;
376     if ((n = obt_xml_find_node(node, "follow")))
377         o->follow = obt_xml_node_bool(n);
378     return o;
379 }
380
381 static gpointer setup_go_last_func(xmlNodePtr node)
382 {
383     Options *o = g_slice_new0(Options);
384     o->type = LAST;
385     return o;
386 }
387
388 static gpointer setup_send_last_func(xmlNodePtr node)
389 {
390     Options *o = setup_follow(node);
391     o->type = LAST;
392     return o;
393 }
394
395 static gpointer setup_go_abs_func(xmlNodePtr node)
396 {
397     xmlNodePtr n;
398     Options *o = g_slice_new0(Options);
399     o->type = ABSOLUTE;
400     if ((n = obt_xml_find_node(node, "desktop")))
401         o->u.abs.desktop = obt_xml_node_int(n) - 1;
402     else
403         o->u.abs.desktop = screen_desktop;
404     return o;
405 }
406
407 static void setup_rel(Options *o, xmlNodePtr node, gboolean lin,
408                       ObDirection dir,
409                       ObActionsIPreFunc *pre,
410                       ObActionsIInputFunc *input,
411                       ObActionsIPostFunc *post)
412 {
413     xmlNodePtr n;
414
415     o->type = RELATIVE;
416     o->u.rel.linear = lin;
417     o->u.rel.dir = dir;
418     o->u.rel.wrap = TRUE;
419
420     if ((n = obt_xml_find_node(node, "wrap")))
421         o->u.rel.wrap = obt_xml_node_bool(n);
422
423     if (input) {
424         o->interactive = TRUE;
425         *pre = i_pre_func;
426         *input = i_input_func;
427         *post = i_post_func;
428     }
429 }
430
431 static gpointer setup_go_next_func(xmlNodePtr node,
432                                    ObActionsIPreFunc *pre,
433                                    ObActionsIInputFunc *input,
434                                    ObActionsICancelFunc *cancel,
435                                    ObActionsIPostFunc *post)
436 {
437     Options *o = g_slice_new0(Options);
438     setup_rel(o, node, TRUE, OB_DIRECTION_EAST, pre, input, post);
439     return o;
440 }
441
442 static gpointer setup_send_next_func(xmlNodePtr node,
443                                      ObActionsIPreFunc *pre,
444                                      ObActionsIInputFunc *input,
445                                      ObActionsICancelFunc *cancel,
446                                      ObActionsIPostFunc *post)
447 {
448     Options *o = setup_follow(node);
449     setup_rel(o, node, TRUE, OB_DIRECTION_EAST,
450               pre, (o->follow ? input : NULL), post);
451     return o;
452 }
453
454 static gpointer setup_go_prev_func(xmlNodePtr node,
455                                    ObActionsIPreFunc *pre,
456                                    ObActionsIInputFunc *input,
457                                    ObActionsICancelFunc *cancel,
458                                    ObActionsIPostFunc *post)
459 {
460     Options *o = g_slice_new0(Options);
461     setup_rel(o, node, TRUE, OB_DIRECTION_WEST, pre, input, post);
462     return o;
463 }
464
465 static gpointer setup_send_prev_func(xmlNodePtr node,
466                                      ObActionsIPreFunc *pre,
467                                      ObActionsIInputFunc *input,
468                                      ObActionsICancelFunc *cancel,
469                                      ObActionsIPostFunc *post)
470 {
471     Options *o = setup_follow(node);
472     setup_rel(o, node, TRUE, OB_DIRECTION_WEST,
473               pre, (o->follow ? input : NULL), post);
474     return o;
475 }
476
477 static gpointer setup_go_left_func(xmlNodePtr node,
478                                    ObActionsIPreFunc *pre,
479                                    ObActionsIInputFunc *input,
480                                    ObActionsICancelFunc *cancel,
481                                    ObActionsIPostFunc *post)
482 {
483     Options *o = g_slice_new0(Options);
484     setup_rel(o, node, FALSE, OB_DIRECTION_WEST, pre, input, post);
485     return o;
486 }
487
488 static gpointer setup_send_left_func(xmlNodePtr node,
489                                      ObActionsIPreFunc *pre,
490                                      ObActionsIInputFunc *input,
491                                      ObActionsICancelFunc *cancel,
492                                      ObActionsIPostFunc *post)
493 {
494     Options *o = setup_follow(node);
495     setup_rel(o, node, FALSE, OB_DIRECTION_WEST,
496               pre, (o->follow ? input : NULL), post);
497     return o;
498 }
499
500 static gpointer setup_go_right_func(xmlNodePtr node,
501                                     ObActionsIPreFunc *pre,
502                                     ObActionsIInputFunc *input,
503                                     ObActionsICancelFunc *cancel,
504                                     ObActionsIPostFunc *post)
505 {
506     Options *o = g_slice_new0(Options);
507     setup_rel(o, node, FALSE, OB_DIRECTION_EAST, pre, input, post);
508     return o;
509 }
510
511 static gpointer setup_send_right_func(xmlNodePtr node,
512                                       ObActionsIPreFunc *pre,
513                                       ObActionsIInputFunc *input,
514                                       ObActionsICancelFunc *cancel,
515                                       ObActionsIPostFunc *post)
516 {
517     Options *o = setup_follow(node);
518     setup_rel(o, node, FALSE, OB_DIRECTION_EAST,
519               pre, (o->follow ? input : NULL), post);
520     return o;
521 }
522
523 static gpointer setup_go_up_func(xmlNodePtr node,
524                                  ObActionsIPreFunc *pre,
525                                  ObActionsIInputFunc *input,
526                                  ObActionsICancelFunc *cancel,
527                                  ObActionsIPostFunc *post)
528 {
529     Options *o = g_slice_new0(Options);
530     setup_rel(o, node, FALSE, OB_DIRECTION_NORTH, pre, input, post);
531     return o;
532 }
533
534 static gpointer setup_send_up_func(xmlNodePtr node,
535                                    ObActionsIPreFunc *pre,
536                                    ObActionsIInputFunc *input,
537                                    ObActionsICancelFunc *cancel,
538                                    ObActionsIPostFunc *post)
539 {
540     Options *o = setup_follow(node);
541     setup_rel(o, node, FALSE, OB_DIRECTION_NORTH,
542               pre, (o->follow ? input : NULL), post);
543     return o;
544 }
545
546 static gpointer setup_go_down_func(xmlNodePtr node,
547                                    ObActionsIPreFunc *pre,
548                                    ObActionsIInputFunc *input,
549                                    ObActionsICancelFunc *cancel,
550                                    ObActionsIPostFunc *post)
551 {
552     Options *o = g_slice_new0(Options);
553     setup_rel(o, node, FALSE, OB_DIRECTION_SOUTH, pre, input, post);
554     return o;
555 }
556
557 static gpointer setup_send_down_func(xmlNodePtr node,
558                                      ObActionsIPreFunc *pre,
559                                      ObActionsIInputFunc *input,
560                                      ObActionsICancelFunc *cancel,
561                                      ObActionsIPostFunc *post)
562 {
563     Options *o = setup_follow(node);
564     setup_rel(o, node, FALSE, OB_DIRECTION_SOUTH,
565               pre, (o->follow ? input : NULL), post);
566     return o;
567 }