]> icculus.org git repositories - manmower/openbox.git/blob - openbox/action.c
fix mem leak
[manmower/openbox.git] / openbox / action.c
1 #include "debug.h"
2 #include "client.h"
3 #include "focus.h"
4 #include "moveresize.h"
5 #include "menu.h"
6 #include "prop.h"
7 #include "stacking.h"
8 #include "screen.h"
9 #include "action.h"
10 #include "openbox.h"
11 #include "grab.h"
12 #include "keyboard.h"
13
14 #include <glib.h>
15
16 typedef struct ActionString {
17     char *name;
18     void (*func)(union ActionData *);
19     void (*setup)(ObAction **, ObUserAction uact);
20 } ActionString;
21
22 static ObAction *action_new(void (*func)(union ActionData *data),
23                             ObUserAction uact)
24 {
25     ObAction *a = g_new0(ObAction, 1);
26     a->func = func;
27
28     return a;
29 }
30
31 void action_free(ObAction *a)
32 {
33     if (a == NULL) return;
34
35     /* deal with pointers */
36     if (a->func == action_execute || a->func == action_restart)
37         g_free(a->data.execute.path);
38     else if (a->func == action_showmenu)
39         g_free(a->data.showmenu.name);
40
41     g_free(a);
42 }
43
44 void setup_action_directional_focus_north(ObAction **a, ObUserAction uact)
45
46     (*a)->data.interdiraction.inter.any.interactive = TRUE;
47     (*a)->data.interdiraction.direction = OB_DIRECTION_NORTH;
48 }
49
50 void setup_action_directional_focus_east(ObAction **a, ObUserAction uact)
51 {
52     (*a)->data.interdiraction.inter.any.interactive = TRUE;
53     (*a)->data.interdiraction.direction = OB_DIRECTION_EAST;
54 }
55
56 void setup_action_directional_focus_south(ObAction **a, ObUserAction uact)
57 {
58     (*a)->data.interdiraction.inter.any.interactive = TRUE;
59     (*a)->data.interdiraction.direction = OB_DIRECTION_SOUTH;
60 }
61
62 void setup_action_directional_focus_west(ObAction **a, ObUserAction uact)
63 {
64     (*a)->data.interdiraction.inter.any.interactive = TRUE;
65     (*a)->data.interdiraction.direction = OB_DIRECTION_WEST;
66 }
67
68 void setup_action_directional_focus_northeast(ObAction **a, ObUserAction uact)
69 {
70     (*a)->data.interdiraction.inter.any.interactive = TRUE;
71     (*a)->data.interdiraction.direction = OB_DIRECTION_NORTHEAST;
72 }
73
74 void setup_action_directional_focus_southeast(ObAction **a, ObUserAction uact)
75 {
76     (*a)->data.interdiraction.inter.any.interactive = TRUE;
77     (*a)->data.interdiraction.direction = OB_DIRECTION_SOUTHEAST;
78 }
79
80 void setup_action_directional_focus_southwest(ObAction **a, ObUserAction uact)
81 {
82     (*a)->data.interdiraction.inter.any.interactive = TRUE;
83     (*a)->data.interdiraction.direction = OB_DIRECTION_SOUTHWEST;
84 }
85
86 void setup_action_directional_focus_northwest(ObAction **a, ObUserAction uact)
87 {
88     (*a)->data.interdiraction.inter.any.interactive = TRUE;
89     (*a)->data.interdiraction.direction = OB_DIRECTION_NORTHWEST;
90 }
91
92 void setup_action_send_to_desktop(ObAction **a, ObUserAction uact)
93 {
94     (*a)->data.sendto.follow = TRUE;
95 }
96
97 void setup_action_send_to_desktop_prev(ObAction **a, ObUserAction uact)
98 {
99     (*a)->data.sendtodir.inter.any.interactive = TRUE;
100     (*a)->data.sendtodir.dir = OB_DIRECTION_WEST;
101     (*a)->data.sendtodir.linear = TRUE;
102     (*a)->data.sendtodir.wrap = TRUE;
103     (*a)->data.sendtodir.follow = TRUE;
104 }
105
106 void setup_action_send_to_desktop_next(ObAction **a, ObUserAction uact)
107 {
108     (*a)->data.sendtodir.inter.any.interactive = TRUE;
109     (*a)->data.sendtodir.dir = OB_DIRECTION_EAST;
110     (*a)->data.sendtodir.linear = TRUE;
111     (*a)->data.sendtodir.wrap = TRUE;
112     (*a)->data.sendtodir.follow = TRUE;
113 }
114
115 void setup_action_send_to_desktop_left(ObAction **a, ObUserAction uact)
116 {
117     (*a)->data.sendtodir.inter.any.interactive = TRUE;
118     (*a)->data.sendtodir.dir = OB_DIRECTION_WEST;
119     (*a)->data.sendtodir.linear = FALSE;
120     (*a)->data.sendtodir.wrap = TRUE;
121     (*a)->data.sendtodir.follow = TRUE;
122 }
123
124 void setup_action_send_to_desktop_right(ObAction **a, ObUserAction uact)
125 {
126     (*a)->data.sendtodir.inter.any.interactive = TRUE;
127     (*a)->data.sendtodir.dir = OB_DIRECTION_EAST;
128     (*a)->data.sendtodir.linear = FALSE;
129     (*a)->data.sendtodir.wrap = TRUE;
130     (*a)->data.sendtodir.follow = TRUE;
131 }
132
133 void setup_action_send_to_desktop_up(ObAction **a, ObUserAction uact)
134 {
135     (*a)->data.sendtodir.inter.any.interactive = TRUE;
136     (*a)->data.sendtodir.dir = OB_DIRECTION_NORTH;
137     (*a)->data.sendtodir.linear = FALSE;
138     (*a)->data.sendtodir.wrap = TRUE;
139     (*a)->data.sendtodir.follow = TRUE;
140 }
141
142 void setup_action_send_to_desktop_down(ObAction **a, ObUserAction uact)
143 {
144     (*a)->data.sendtodir.inter.any.interactive = TRUE;
145     (*a)->data.sendtodir.dir = OB_DIRECTION_SOUTH;
146     (*a)->data.sendtodir.linear = FALSE;
147     (*a)->data.sendtodir.wrap = TRUE;
148     (*a)->data.sendtodir.follow = TRUE;
149 }
150
151 void setup_action_desktop_prev(ObAction **a, ObUserAction uact)
152 {
153     (*a)->data.desktopdir.inter.any.interactive = TRUE;
154     (*a)->data.desktopdir.dir = OB_DIRECTION_WEST;
155     (*a)->data.desktopdir.linear = TRUE;
156     (*a)->data.desktopdir.wrap = TRUE;
157 }
158
159 void setup_action_desktop_next(ObAction **a, ObUserAction uact)
160 {
161     (*a)->data.desktopdir.inter.any.interactive = TRUE;
162     (*a)->data.desktopdir.dir = OB_DIRECTION_EAST;
163     (*a)->data.desktopdir.linear = TRUE;
164     (*a)->data.desktopdir.wrap = TRUE;
165 }
166
167 void setup_action_desktop_left(ObAction **a, ObUserAction uact)
168 {
169     (*a)->data.desktopdir.inter.any.interactive = TRUE;
170     (*a)->data.desktopdir.dir = OB_DIRECTION_WEST;
171     (*a)->data.desktopdir.linear = FALSE;
172     (*a)->data.desktopdir.wrap = TRUE;
173 }
174
175 void setup_action_desktop_right(ObAction **a, ObUserAction uact)
176 {
177     (*a)->data.desktopdir.inter.any.interactive = TRUE;
178     (*a)->data.desktopdir.dir = OB_DIRECTION_EAST;
179     (*a)->data.desktopdir.linear = FALSE;
180     (*a)->data.desktopdir.wrap = TRUE;
181 }
182
183 void setup_action_desktop_up(ObAction **a, ObUserAction uact)
184 {
185     (*a)->data.desktopdir.inter.any.interactive = TRUE;
186     (*a)->data.desktopdir.dir = OB_DIRECTION_NORTH;
187     (*a)->data.desktopdir.linear = FALSE;
188     (*a)->data.desktopdir.wrap = TRUE;
189 }
190
191 void setup_action_desktop_down(ObAction **a, ObUserAction uact)
192 {
193     (*a)->data.desktopdir.inter.any.interactive = TRUE;
194     (*a)->data.desktopdir.dir = OB_DIRECTION_SOUTH;
195     (*a)->data.desktopdir.linear = FALSE;
196     (*a)->data.desktopdir.wrap = TRUE;
197 }
198
199 void setup_action_cycle_windows_next(ObAction **a, ObUserAction uact)
200 {
201     (*a)->data.cycle.inter.any.interactive = TRUE;
202     (*a)->data.cycle.linear = FALSE;
203     (*a)->data.cycle.forward = TRUE;
204 }
205
206 void setup_action_cycle_windows_previous(ObAction **a, ObUserAction uact)
207 {
208     (*a)->data.cycle.inter.any.interactive = TRUE;
209     (*a)->data.cycle.linear = FALSE;
210     (*a)->data.cycle.forward = FALSE;
211 }
212
213 void setup_action_movetoedge_north(ObAction **a, ObUserAction uact)
214 {
215     (*a)->data.diraction.direction = OB_DIRECTION_NORTH;
216 }
217
218 void setup_action_movetoedge_south(ObAction **a, ObUserAction uact)
219 {
220     (*a)->data.diraction.direction = OB_DIRECTION_SOUTH;
221 }
222
223 void setup_action_movetoedge_east(ObAction **a, ObUserAction uact)
224 {
225     (*a)->data.diraction.direction = OB_DIRECTION_EAST;
226 }
227
228 void setup_action_movetoedge_west(ObAction **a, ObUserAction uact)
229 {
230     (*a)->data.diraction.direction = OB_DIRECTION_WEST;
231 }
232
233 void setup_action_growtoedge_north(ObAction **a, ObUserAction uact)
234 {
235     (*a)->data.diraction.direction = OB_DIRECTION_NORTH;
236 }
237
238 void setup_action_growtoedge_south(ObAction **a, ObUserAction uact)
239 {
240     (*a)->data.diraction.direction = OB_DIRECTION_SOUTH;
241 }
242
243 void setup_action_growtoedge_east(ObAction **a, ObUserAction uact)
244 {
245     (*a)->data.diraction.direction = OB_DIRECTION_EAST;
246 }
247
248 void setup_action_growtoedge_west(ObAction **a, ObUserAction uact)
249 {
250     (*a)->data.diraction.direction = OB_DIRECTION_WEST;
251 }
252
253 void setup_action_top_layer(ObAction **a, ObUserAction uact)
254 {
255     (*a)->data.layer.layer = 1;
256 }
257
258 void setup_action_normal_layer(ObAction **a, ObUserAction uact)
259 {
260     (*a)->data.layer.layer = 0;
261 }
262
263 void setup_action_bottom_layer(ObAction **a, ObUserAction uact)
264 {
265     (*a)->data.layer.layer = -1;
266 }
267
268 void setup_action_move(ObAction **a, ObUserAction uact)
269 {
270     (*a)->data.moveresize.move = TRUE;
271     (*a)->data.moveresize.keyboard =
272         (uact == OB_USER_ACTION_KEYBOARD_KEY ||
273          uact == OB_USER_ACTION_MENU_SELECTION);
274 }
275
276 void setup_action_resize(ObAction **a, ObUserAction uact)
277 {
278     (*a)->data.moveresize.move = FALSE;
279     (*a)->data.moveresize.keyboard =
280         (uact == OB_USER_ACTION_KEYBOARD_KEY ||
281          uact == OB_USER_ACTION_MENU_SELECTION);
282 }
283
284 void setup_action_showmenu(ObAction **a, ObUserAction uact)
285 {
286     /* you cannot call ShowMenu from inside a menu, cuz the menu code makes
287        assumptions that there is only one menu (and submenus) open at
288        a time! */
289     if (uact == OB_USER_ACTION_MENU_SELECTION) {
290         action_free(*a);
291         a = NULL;
292     }
293 }
294
295 ActionString actionstrings[] =
296 {
297     {
298         "execute", 
299         action_execute, 
300         NULL
301     },
302     {
303         "directionalfocusnorth", 
304         action_directional_focus, 
305         setup_action_directional_focus_north
306     },
307     {
308         "directionalfocuseast", 
309         action_directional_focus, 
310         setup_action_directional_focus_east
311     },
312     {
313         "directionalfocussouth", 
314         action_directional_focus, 
315         setup_action_directional_focus_south
316     },
317     {
318         "directionalfocuswest",
319         action_directional_focus,
320         setup_action_directional_focus_west
321     },
322     {
323         "directionalfocusnortheast",
324         action_directional_focus,
325         setup_action_directional_focus_northeast
326     },
327     {
328         "directionalfocussoutheast",
329         action_directional_focus,
330         setup_action_directional_focus_southeast
331     },
332     {
333         "directionalfocussouthwest",
334         action_directional_focus,
335         setup_action_directional_focus_southwest
336     },
337     {
338         "directionalfocusnorthwest",
339         action_directional_focus,
340         setup_action_directional_focus_northwest
341     },
342     {
343         "activate",
344         action_activate,
345         NULL
346     },
347     {
348         "focus",
349         action_focus,
350         NULL
351     },
352     {
353         "unfocus",
354         action_unfocus,
355         NULL
356     },
357     {
358         "iconify",
359         action_iconify,
360         NULL
361     },
362     {
363         "raiselower",
364         action_raiselower,
365         NULL
366     },
367     {
368         "raise",
369         action_raise,
370         NULL
371     },
372     {
373         "lower",
374         action_lower,
375         NULL
376     },
377     {
378         "close",
379         action_close,
380         NULL
381     },
382     {
383         "kill",
384         action_kill,
385         NULL
386     },
387     {
388         "shadelower",
389         action_shadelower,
390         NULL
391     },
392     {
393         "unshaderaise",
394         action_unshaderaise,
395         NULL
396     },
397     {
398         "shade",
399         action_shade,
400         NULL
401     },
402     {
403         "unshade",
404         action_unshade,
405         NULL
406     },
407     {
408         "toggleshade",
409         action_toggle_shade,
410         NULL
411     },
412     {
413         "toggleomnipresent",
414         action_toggle_omnipresent,
415         NULL
416     },
417     {
418         "moverelativehorz",
419         action_move_relative_horz,
420         NULL
421     },
422     {
423         "moverelativevert",
424         action_move_relative_vert,
425         NULL
426     },
427     {
428         "resizerelativehorz",
429         action_resize_relative_horz,
430         NULL
431     },
432     {
433         "resizerelativevert",
434         action_resize_relative_vert,
435         NULL
436     },
437     {
438         "maximizefull",
439         action_maximize_full,
440         NULL
441     },
442     {
443         "unmaximizefull",
444         action_unmaximize_full,
445         NULL
446     },
447     {
448         "togglemaximizefull",
449         action_toggle_maximize_full,
450         NULL
451     },
452     {
453         "maximizehorz",
454         action_maximize_horz,
455         NULL
456     },
457     {
458         "unmaximizehorz",
459         action_unmaximize_horz,
460         NULL
461     },
462     {
463         "togglemaximizehorz",
464         action_toggle_maximize_horz,
465         NULL
466     },
467     {
468         "maximizevert",
469         action_maximize_vert,
470         NULL
471     },
472     {
473         "unmaximizevert",
474         action_unmaximize_vert,
475         NULL
476     },
477     {
478         "togglemaximizevert",
479         action_toggle_maximize_vert,
480         NULL
481     },
482     {
483         "sendtodesktop",
484         action_send_to_desktop,
485         setup_action_send_to_desktop
486     },
487     {
488         "sendtodesktopnext",
489         action_send_to_desktop_dir,
490         setup_action_send_to_desktop_next
491     },
492     {
493         "sendtodesktopprevious",
494         action_send_to_desktop_dir,
495         setup_action_send_to_desktop_prev
496     },
497     {
498         "sendtodesktopright",
499         action_send_to_desktop_dir,
500         setup_action_send_to_desktop_right
501     },
502     {
503         "sendtodesktopleft",
504         action_send_to_desktop_dir,
505         setup_action_send_to_desktop_left
506     },
507     {
508         "sendtodesktopup",
509         action_send_to_desktop_dir,
510         setup_action_send_to_desktop_up
511     },
512     {
513         "sendtodesktopdown",
514         action_send_to_desktop_dir,
515         setup_action_send_to_desktop_down
516     },
517     {
518         "desktop",
519         action_desktop,
520         NULL
521     },
522     {
523         "desktopnext",
524         action_desktop_dir,
525         setup_action_desktop_next
526     },
527     {
528         "desktopprevious",
529         action_desktop_dir,
530         setup_action_desktop_prev
531     },
532     {
533         "desktopright",
534         action_desktop_dir,
535         setup_action_desktop_right
536     },
537     {
538         "desktopleft",
539         action_desktop_dir,
540         setup_action_desktop_left
541     },
542     {
543         "desktopup",
544         action_desktop_dir,
545         setup_action_desktop_up
546     },
547     {
548         "desktopdown",
549         action_desktop_dir,
550         setup_action_desktop_down
551     },
552     {
553         "toggledecorations",
554         action_toggle_decorations,
555         NULL
556     },
557     {
558         "move",
559         action_moveresize,
560         setup_action_move
561     },
562     {
563         "resize",
564         action_moveresize,
565         setup_action_resize
566     },
567     {
568         "toggleshowdesktop",
569         action_toggle_show_desktop,
570         NULL
571     },
572     {
573         "showdesktop",
574         action_show_desktop,
575         NULL
576     },
577     {
578         "unshowdesktop",
579         action_unshow_desktop,
580         NULL
581     },
582     {
583         "desktoplast",
584         action_desktop_last,
585         NULL
586     },
587     {
588         "reconfigure",
589         action_reconfigure,
590         NULL
591     },
592     {
593         "restart",
594         action_restart,
595         NULL
596     },
597     {
598         "exit",
599         action_exit,
600         NULL
601     },
602     {
603         "showmenu",
604         action_showmenu,
605         setup_action_showmenu
606     },
607     {
608         "sendtotoplayer",
609         action_send_to_layer,
610         setup_action_top_layer
611     },
612     {
613         "togglealwaysontop",
614         action_toggle_layer,
615         setup_action_top_layer
616     },
617     {
618         "sendtonormallayer",
619         action_send_to_layer,
620         setup_action_normal_layer
621     },
622     {
623         "sendtobottomlayer",
624         action_send_to_layer,
625         setup_action_bottom_layer
626     },
627     {
628         "togglealwaysonbottom",
629         action_toggle_layer,
630         setup_action_bottom_layer
631     },
632     {
633         "nextwindow",
634         action_cycle_windows,
635         setup_action_cycle_windows_next
636     },
637     {
638         "previouswindow",
639         action_cycle_windows,
640         setup_action_cycle_windows_previous
641     },
642     {
643         "movetoedgenorth",
644         action_movetoedge,
645         setup_action_movetoedge_north
646     },
647     {
648         "movetoedgesouth",
649         action_movetoedge,
650         setup_action_movetoedge_south
651     },
652     {
653         "movetoedgewest",
654         action_movetoedge,
655         setup_action_movetoedge_west
656     },
657     {
658         "movetoedgeeast",
659         action_movetoedge,
660         setup_action_movetoedge_east
661     },
662     {
663         "growtoedgenorth",
664         action_growtoedge,
665         setup_action_growtoedge_north
666     },
667     {
668         "growtoedgesouth",
669         action_growtoedge,
670         setup_action_growtoedge_south
671     },
672     {
673         "growtoedgewest",
674         action_growtoedge,
675         setup_action_growtoedge_west
676     },
677     {
678         "growtoedgeeast",
679         action_growtoedge,
680         setup_action_growtoedge_east
681     },
682     {
683         NULL,
684         NULL,
685         NULL
686     }
687 };
688
689 ObAction *action_from_string(char *name, ObUserAction uact)
690 {
691     ObAction *a = NULL;
692     gboolean exist = FALSE;
693     int i;
694
695     for (i = 0; actionstrings[i].name; i++)
696         if (!g_ascii_strcasecmp(name, actionstrings[i].name)) {
697             exist = TRUE;
698             a = action_new(actionstrings[i].func, uact);
699             if (actionstrings[i].setup)
700                 actionstrings[i].setup(&a, uact);
701             /* only key bindings can be interactive. thus saith the xor. */
702             if (uact != OB_USER_ACTION_KEYBOARD_KEY)
703                 a->data.any.interactive = FALSE;
704             break;
705         }
706     if (!exist)
707         g_warning("Invalid action '%s' requested. No such action exists.",
708                   name);
709     if (!a)
710         g_warning("Invalid use of action '%s'. Action will be ignored.", name);
711     return a;
712 }
713
714 ObAction *action_parse(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
715                        ObUserAction uact)
716 {
717     char *actname;
718     ObAction *act = NULL;
719     xmlNodePtr n;
720
721     if (parse_attr_string("name", node, &actname)) {
722         if ((act = action_from_string(actname, uact))) {
723             if (act->func == action_execute || act->func == action_restart) {
724                 if ((n = parse_find_node("execute", node->xmlChildrenNode))) {
725                     gchar *s = parse_string(doc, n);
726                     act->data.execute.path = ob_expand_tilde(s);
727                     g_free(s);
728                 }
729             } else if (act->func == action_showmenu) {
730                 if ((n = parse_find_node("menu", node->xmlChildrenNode)))
731                     act->data.showmenu.name = parse_string(doc, n);
732             } else if (act->func == action_move_relative_horz ||
733                        act->func == action_move_relative_vert ||
734                        act->func == action_resize_relative_horz ||
735                        act->func == action_resize_relative_vert) {
736                 if ((n = parse_find_node("delta", node->xmlChildrenNode)))
737                     act->data.relative.delta = parse_int(doc, n);
738             } else if (act->func == action_desktop) {
739                 if ((n = parse_find_node("desktop", node->xmlChildrenNode)))
740                     act->data.desktop.desk = parse_int(doc, n);
741                 if (act->data.desktop.desk > 0) act->data.desktop.desk--;
742            } else if (act->func == action_send_to_desktop) {
743                 if ((n = parse_find_node("desktop", node->xmlChildrenNode)))
744                     act->data.sendto.desk = parse_int(doc, n);
745                 if (act->data.sendto.desk > 0) act->data.sendto.desk--;
746                 if ((n = parse_find_node("follow", node->xmlChildrenNode)))
747                     act->data.sendto.follow = parse_bool(doc, n);
748             } else if (act->func == action_desktop_dir) {
749                 if ((n = parse_find_node("wrap", node->xmlChildrenNode)))
750                     act->data.desktopdir.wrap = parse_bool(doc, n); 
751             } else if (act->func == action_send_to_desktop_dir) {
752                 if ((n = parse_find_node("wrap", node->xmlChildrenNode)))
753                     act->data.sendtodir.wrap = parse_bool(doc, n);
754                 if ((n = parse_find_node("follow", node->xmlChildrenNode)))
755                     act->data.sendtodir.follow = parse_bool(doc, n);
756             } else if (act->func == action_activate) {
757                 if ((n = parse_find_node("here", node->xmlChildrenNode)))
758                     act->data.activate.here = parse_bool(doc, n);
759             } else if (act->func == action_cycle_windows) {
760                 if ((n = parse_find_node("linear", node->xmlChildrenNode)))
761                     act->data.cycle.linear = parse_bool(doc, n);
762             }
763         }
764         g_free(actname);
765     }
766     return act;
767 }
768
769 void action_run_full(ObAction *a, struct _ObClient *c,
770                      guint state, guint button, gint x, gint y,
771                      gboolean cancel, gboolean done)
772 {
773     if (x < 0 && y < 0)
774         screen_pointer_pos(&x, &y);
775
776     a->data.any.c = c;
777     a->data.any.x = x;
778     a->data.any.y = y;
779
780     a->data.any.button = button;
781
782     if (a->data.any.interactive) {
783         a->data.inter.cancel = cancel;
784         a->data.inter.final = done;
785         if (!(cancel || done))
786             keyboard_interactive_grab(state, c, a);
787     }
788
789     a->func(&a->data);
790 }
791
792 void action_execute(union ActionData *data)
793 {
794     GError *e = NULL;
795     char *cmd;
796     if (data->execute.path) {
797         cmd = g_filename_from_utf8(data->execute.path, -1, NULL, NULL, NULL);
798         if (cmd) {
799             if (!g_spawn_command_line_async(cmd, &e)) {
800                 g_warning("failed to execute '%s': %s",
801                           cmd, e->message);
802             }
803             g_free(cmd);
804         } else {
805             g_warning("failed to convert '%s' from utf8", data->execute.path);
806         }
807     }
808 }
809
810 void action_activate(union ActionData *data)
811 {
812     if (data->activate.any.c)
813         client_activate(data->activate.any.c, data->activate.here);
814 }
815
816 void action_focus(union ActionData *data)
817 {
818     if (data->client.any.c)
819         client_focus(data->client.any.c);
820 }
821
822 void action_unfocus (union ActionData *data)
823 {
824     if (data->client.any.c)
825         client_unfocus(data->client.any.c);
826 }
827
828 void action_iconify(union ActionData *data)
829 {
830     if (data->client.any.c)
831         client_iconify(data->client.any.c, TRUE, TRUE);
832 }
833
834 void action_raiselower(union ActionData *data)
835 {
836     ObClient *c = data->client.any.c;
837     GList *it;
838     gboolean raise = FALSE;
839
840     if (!c) return;
841
842     for (it = stacking_list; it; it = g_list_next(it)) {
843         ObClient *cit = it->data;
844
845         if (cit == c) break;
846         if (client_normal(cit) == client_normal(c) &&
847             cit->layer == c->layer &&
848             cit->frame->visible)
849         {
850             if (RECT_INTERSECTS_RECT(cit->frame->area, c->frame->area)) {
851                 raise = TRUE;
852                 break;
853             }
854         }
855     }
856
857     if (raise)
858         stacking_raise(CLIENT_AS_WINDOW(c));
859     else
860         stacking_lower(CLIENT_AS_WINDOW(c));
861 }
862
863 void action_raise(union ActionData *data)
864 {
865     if (data->client.any.c)
866         stacking_raise(CLIENT_AS_WINDOW(data->client.any.c));
867 }
868
869 void action_unshaderaise(union ActionData *data)
870 {
871     if (data->client.any.c) {
872         if (data->client.any.c->shaded) {
873             grab_pointer(TRUE, OB_CURSOR_NONE);
874             client_shade(data->client.any.c, FALSE);
875             grab_pointer(FALSE, OB_CURSOR_NONE);
876         } else
877             stacking_raise(CLIENT_AS_WINDOW(data->client.any.c));
878     }
879 }
880
881 void action_shadelower(union ActionData *data)
882 {
883     if (data->client.any.c) {
884         if (data->client.any.c->shaded)
885             stacking_lower(CLIENT_AS_WINDOW(data->client.any.c));
886         else {
887             grab_pointer(TRUE, OB_CURSOR_NONE);
888             client_shade(data->client.any.c, TRUE);
889             grab_pointer(FALSE, OB_CURSOR_NONE);
890         }
891     }
892 }
893
894 void action_lower(union ActionData *data)
895 {
896     if (data->client.any.c)
897         stacking_lower(CLIENT_AS_WINDOW(data->client.any.c));
898 }
899
900 void action_close(union ActionData *data)
901 {
902     if (data->client.any.c)
903         client_close(data->client.any.c);
904 }
905
906 void action_kill(union ActionData *data)
907 {
908     if (data->client.any.c)
909         client_kill(data->client.any.c);
910 }
911
912 void action_shade(union ActionData *data)
913 {
914     if (data->client.any.c) { 
915         grab_pointer(TRUE, OB_CURSOR_NONE);
916         client_shade(data->client.any.c, TRUE);
917         grab_pointer(FALSE, OB_CURSOR_NONE);
918     }
919 }
920
921 void action_unshade(union ActionData *data)
922 {
923     if (data->client.any.c) {
924         grab_pointer(TRUE, OB_CURSOR_NONE);
925         client_shade(data->client.any.c, FALSE);
926         grab_pointer(FALSE, OB_CURSOR_NONE);
927     }
928 }
929
930 void action_toggle_shade(union ActionData *data)
931 {
932     if (data->client.any.c) {
933         grab_pointer(TRUE, OB_CURSOR_NONE);
934         client_shade(data->client.any.c, !data->client.any.c->shaded);
935         grab_pointer(FALSE, OB_CURSOR_NONE);
936     }
937 }
938
939 void action_toggle_omnipresent(union ActionData *data)
940
941     if (data->client.any.c)
942         client_set_desktop(data->client.any.c,
943                            data->client.any.c->desktop == DESKTOP_ALL ?
944                            screen_desktop : DESKTOP_ALL, FALSE);
945 }
946
947 void action_move_relative_horz(union ActionData *data)
948 {
949     ObClient *c = data->relative.any.c;
950     if (c) {
951         grab_pointer(TRUE, OB_CURSOR_NONE);
952         client_move(c, c->area.x + data->relative.delta, c->area.y);
953         grab_pointer(FALSE, OB_CURSOR_NONE);
954     }
955 }
956
957 void action_move_relative_vert(union ActionData *data)
958 {
959     ObClient *c = data->relative.any.c;
960     if (c) {
961         grab_pointer(TRUE, OB_CURSOR_NONE);
962         client_move(c, c->area.x, c->area.y + data->relative.delta);
963         grab_pointer(FALSE, OB_CURSOR_NONE);
964     }
965 }
966
967 void action_resize_relative_horz(union ActionData *data)
968 {
969     ObClient *c = data->relative.any.c;
970     if (c) {
971         grab_pointer(TRUE, OB_CURSOR_NONE);
972         client_resize(c,
973                       c->area.width + data->relative.delta * c->size_inc.width,
974                       c->area.height);
975         grab_pointer(FALSE, OB_CURSOR_NONE);
976     }
977 }
978
979 void action_resize_relative_vert(union ActionData *data)
980 {
981     ObClient *c = data->relative.any.c;
982     if (c && !c->shaded) {
983         grab_pointer(TRUE, OB_CURSOR_NONE);
984         client_resize(c, c->area.width, c->area.height +
985                       data->relative.delta * c->size_inc.height);
986         grab_pointer(FALSE, OB_CURSOR_NONE);
987     }
988 }
989
990 void action_maximize_full(union ActionData *data)
991 {
992     if (data->client.any.c)
993         client_maximize(data->client.any.c, TRUE, 0, TRUE);
994 }
995
996 void action_unmaximize_full(union ActionData *data)
997 {
998     if (data->client.any.c)
999         client_maximize(data->client.any.c, FALSE, 0, TRUE);
1000 }
1001
1002 void action_toggle_maximize_full(union ActionData *data)
1003 {
1004     if (data->client.any.c)
1005         client_maximize(data->client.any.c,
1006                         !(data->client.any.c->max_horz ||
1007                           data->client.any.c->max_vert),
1008                         0, TRUE);
1009 }
1010
1011 void action_maximize_horz(union ActionData *data)
1012 {
1013     if (data->client.any.c)
1014         client_maximize(data->client.any.c, TRUE, 1, TRUE);
1015 }
1016
1017 void action_unmaximize_horz(union ActionData *data)
1018 {
1019     if (data->client.any.c)
1020         client_maximize(data->client.any.c, FALSE, 1, TRUE);
1021 }
1022
1023 void action_toggle_maximize_horz(union ActionData *data)
1024 {
1025     if (data->client.any.c)
1026         client_maximize(data->client.any.c,
1027                         !data->client.any.c->max_horz, 1, TRUE);
1028 }
1029
1030 void action_maximize_vert(union ActionData *data)
1031 {
1032     if (data->client.any.c)
1033         client_maximize(data->client.any.c, TRUE, 2, TRUE);
1034 }
1035
1036 void action_unmaximize_vert(union ActionData *data)
1037 {
1038     if (data->client.any.c)
1039         client_maximize(data->client.any.c, FALSE, 2, TRUE);
1040 }
1041
1042 void action_toggle_maximize_vert(union ActionData *data)
1043 {
1044     if (data->client.any.c)
1045         client_maximize(data->client.any.c, !data->client.any.c->max_vert, 2, TRUE);
1046 }
1047
1048 void action_send_to_desktop(union ActionData *data)
1049 {
1050     ObClient *c = data->sendto.any.c;
1051
1052     if (!c || !client_normal(c)) return;
1053
1054     if (data->sendto.desk < screen_num_desktops ||
1055         data->sendto.desk == DESKTOP_ALL) {
1056         client_set_desktop(c, data->sendto.desk, data->sendto.follow);
1057         if (data->sendto.follow)
1058             screen_set_desktop(data->sendto.desk);
1059     }
1060 }
1061
1062 void action_desktop(union ActionData *data)
1063 {
1064     if (data->desktop.desk < screen_num_desktops ||
1065         data->desktop.desk == DESKTOP_ALL)
1066         screen_set_desktop(data->desktop.desk);
1067 }
1068
1069 void action_desktop_dir(union ActionData *data)
1070 {
1071     guint d;
1072
1073     d = screen_cycle_desktop(data->desktopdir.dir,
1074                              data->desktopdir.wrap,
1075                              data->sendtodir.linear,
1076                              data->desktopdir.inter.any.interactive,
1077                              data->desktopdir.inter.final,
1078                              data->desktopdir.inter.cancel);
1079     screen_set_desktop(d);
1080 }
1081
1082 void action_send_to_desktop_dir(union ActionData *data)
1083 {
1084     ObClient *c = data->sendtodir.inter.any.c;
1085     guint d;
1086
1087     if (!c || !client_normal(c)) return;
1088
1089     d = screen_cycle_desktop(data->sendtodir.dir, data->sendtodir.wrap,
1090                              data->sendtodir.linear,
1091                              data->sendtodir.inter.any.interactive,
1092                              data->sendtodir.inter.final,
1093                              data->sendtodir.inter.cancel);
1094     client_set_desktop(c, d, data->sendtodir.follow);
1095     if (data->sendtodir.follow)
1096         screen_set_desktop(d);
1097 }
1098
1099 void action_desktop_last(union ActionData *data)
1100 {
1101     screen_set_desktop(screen_last_desktop);
1102 }
1103
1104 void action_toggle_decorations(union ActionData *data)
1105 {
1106     ObClient *c = data->client.any.c;
1107
1108     if (!c) return;
1109
1110     c->decorate = !c->decorate;
1111     client_setup_decor_and_functions(c);
1112 }
1113
1114 static guint32 pick_corner(int x, int y, int cx, int cy, int cw, int ch)
1115 {
1116     if (x - cx > cw / 2) {
1117         if (y - cy > ch / 2)
1118             return prop_atoms.net_wm_moveresize_size_bottomright;
1119         else
1120             return prop_atoms.net_wm_moveresize_size_topright;
1121     } else {
1122         if (y - cy > ch / 2)
1123             return prop_atoms.net_wm_moveresize_size_bottomleft;
1124         else
1125             return prop_atoms.net_wm_moveresize_size_topleft;
1126     }
1127 }
1128
1129 void action_moveresize(union ActionData *data)
1130 {
1131     ObClient *c = data->moveresize.any.c;
1132     guint32 corner;
1133
1134     if (!c || !client_normal(c)) return;
1135
1136     if (data->moveresize.keyboard) {
1137         corner = (data->moveresize.move ?
1138                   prop_atoms.net_wm_moveresize_move_keyboard :
1139                   prop_atoms.net_wm_moveresize_size_keyboard);
1140     } else {
1141         corner = (data->moveresize.move ?
1142                   prop_atoms.net_wm_moveresize_move :
1143                   pick_corner(data->any.x, data->any.y,
1144                               c->frame->area.x, c->frame->area.y,
1145                               /* use the client size because the frame
1146                                  can be differently sized (shaded
1147                                  windows) and we want this based on the
1148                                  clients size */
1149                               c->area.width + c->frame->size.left +
1150                               c->frame->size.right,
1151                               c->area.height + c->frame->size.top +
1152                               c->frame->size.bottom));
1153     }
1154
1155     moveresize_start(c, data->any.x, data->any.y, data->any.button, corner);
1156 }
1157
1158 void action_reconfigure(union ActionData *data)
1159 {
1160     ob_reconfigure();
1161 }
1162
1163 void action_restart(union ActionData *data)
1164 {
1165     ob_restart_other(data->execute.path);
1166 }
1167
1168 void action_exit(union ActionData *data)
1169 {
1170     ob_exit();
1171 }
1172
1173 void action_showmenu(union ActionData *data)
1174 {
1175     if (data->showmenu.name) {
1176         menu_show(data->showmenu.name, data->any.x, data->any.y,
1177                   data->showmenu.any.c);
1178     }
1179 }
1180
1181 void action_cycle_windows(union ActionData *data)
1182 {
1183     focus_cycle(data->cycle.forward, data->cycle.linear,
1184                 data->cycle.inter.any.interactive,
1185                 data->cycle.inter.final, data->cycle.inter.cancel);
1186 }
1187
1188 void action_directional_focus(union ActionData *data)
1189 {
1190     focus_directional_cycle(data->interdiraction.direction,
1191                             data->interdiraction.inter.any.interactive,
1192                             data->interdiraction.inter.final,
1193                             data->interdiraction.inter.cancel);
1194 }
1195
1196 void action_movetoedge(union ActionData *data)
1197 {
1198     int x, y;
1199     ObClient *c = data->diraction.any.c;
1200
1201     if (!c)
1202         return;
1203     x = c->frame->area.x;
1204     y = c->frame->area.y;
1205     
1206     switch(data->diraction.direction) {
1207     case OB_DIRECTION_NORTH:
1208         y = client_directional_edge_search(c, OB_DIRECTION_NORTH);
1209         break;
1210     case OB_DIRECTION_WEST:
1211         x = client_directional_edge_search(c, OB_DIRECTION_WEST);
1212         break;
1213     case OB_DIRECTION_SOUTH:
1214         y = client_directional_edge_search(c, OB_DIRECTION_SOUTH) -
1215             c->frame->area.height;
1216         break;
1217     case OB_DIRECTION_EAST:
1218         x = client_directional_edge_search(c, OB_DIRECTION_EAST) -
1219             c->frame->area.width;
1220         break;
1221     default:
1222         g_assert_not_reached();
1223     }
1224     frame_frame_gravity(c->frame, &x, &y);
1225     grab_pointer(TRUE, OB_CURSOR_NONE);
1226     client_move(c, x, y);
1227     grab_pointer(FALSE, OB_CURSOR_NONE);
1228
1229 }
1230
1231 void action_growtoedge(union ActionData *data)
1232 {
1233     int x, y, width, height, dest;
1234     ObClient *c = data->diraction.any.c;
1235     Rect *a;
1236
1237     if (!c)
1238         return;
1239     
1240     a = screen_area(c->desktop);
1241     x = c->frame->area.x;
1242     y = c->frame->area.y;
1243     width = c->frame->area.width;
1244     height = c->frame->area.height;
1245
1246     switch(data->diraction.direction) {
1247     case OB_DIRECTION_NORTH:
1248         dest = client_directional_edge_search(c, OB_DIRECTION_NORTH);
1249         if (a->y == y)
1250             height = c->frame->area.height / 2;
1251         else {
1252             height = c->frame->area.y + c->frame->area.height - dest;
1253             y = dest;
1254         }
1255         break;
1256     case OB_DIRECTION_WEST:
1257         dest = client_directional_edge_search(c, OB_DIRECTION_WEST);
1258         if (a->x == x)
1259             width = c->frame->area.width / 2;
1260         else {
1261             width = c->frame->area.x + c->frame->area.width - dest;
1262             x = dest;
1263         }
1264         break;
1265     case OB_DIRECTION_SOUTH:
1266         dest = client_directional_edge_search(c, OB_DIRECTION_SOUTH);
1267         if (a->y + a->height == y + c->frame->area.height) {
1268             height = c->frame->area.height / 2;
1269             y = a->y + a->height - height;
1270         } else
1271             height = dest - c->frame->area.y;
1272         y += (height - c->frame->area.height) % c->size_inc.height;
1273         height -= (height - c->frame->area.height) % c->size_inc.height;
1274         break;
1275     case OB_DIRECTION_EAST:
1276         dest = client_directional_edge_search(c, OB_DIRECTION_EAST);
1277         if (a->x + a->width == x + c->frame->area.width) {
1278             width = c->frame->area.width / 2;
1279             x = a->x + a->width - width;
1280         } else
1281             width = dest - c->frame->area.x;
1282         x += (width - c->frame->area.width) % c->size_inc.width;
1283         width -= (width - c->frame->area.width) % c->size_inc.width;
1284         break;
1285     default:
1286         g_assert_not_reached();
1287     }
1288     frame_frame_gravity(c->frame, &x, &y);
1289     width -= c->frame->size.left + c->frame->size.right;
1290     height -= c->frame->size.top + c->frame->size.bottom;
1291     grab_pointer(TRUE, OB_CURSOR_NONE);
1292     client_move_resize(c, x, y, width, height);
1293     grab_pointer(FALSE, OB_CURSOR_NONE);
1294 }
1295
1296 void action_send_to_layer(union ActionData *data)
1297 {
1298     if (data->layer.any.c)
1299         client_set_layer(data->layer.any.c, data->layer.layer);
1300 }
1301
1302 void action_toggle_layer(union ActionData *data)
1303 {
1304     ObClient *c = data->layer.any.c;
1305
1306     if (c) {
1307         if (data->layer.layer < 0)
1308             client_set_layer(c, c->below ? 0 : -1);
1309         else if (data->layer.layer > 0)
1310             client_set_layer(c, c->above ? 0 : 1);
1311     }
1312 }
1313
1314 void action_toggle_show_desktop(union ActionData *data)
1315 {
1316     screen_show_desktop(!screen_showing_desktop);
1317 }
1318
1319 void action_show_desktop(union ActionData *data)
1320 {
1321     screen_show_desktop(TRUE);
1322 }
1323
1324 void action_unshow_desktop(union ActionData *data)
1325 {
1326     screen_show_desktop(FALSE);
1327 }