Merge branch 'backport' into 3.4-working
[mikachu/openbox.git] / openbox / actions / addremovedesktop.c
1 #include "openbox/actions.h"
2 #include "openbox/screen.h"
3 #include <glib.h>
4
5 typedef struct {
6     gboolean current;
7     gboolean add;
8 } Options;
9
10 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
11 static gpointer setup_add_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
12 static gpointer setup_remove_func(ObParseInst *i,
13                                   xmlDocPtr doc, xmlNodePtr node);
14 static gpointer setup_addcurrent_func(ObParseInst *i,
15                                       xmlDocPtr doc, xmlNodePtr node);
16 static gpointer setup_addlast_func(ObParseInst *i,
17                                    xmlDocPtr doc, xmlNodePtr node);
18 static gpointer setup_removecurrent_func(ObParseInst *i,
19                                       xmlDocPtr doc, xmlNodePtr node);
20 static gpointer setup_removelast_func(ObParseInst *i,
21                                       xmlDocPtr doc, xmlNodePtr node);
22 static void     free_func(gpointer options);
23 static gboolean run_func(ObActionsData *data, gpointer options);
24
25 void action_addremovedesktop_startup(void)
26 {
27     actions_register("AddDesktop",
28                      setup_add_func,
29                      free_func,
30                      run_func,
31                      NULL, NULL);
32     actions_register("RemoveDesktop",
33                      setup_remove_func,
34                      free_func,
35                      run_func,
36                      NULL, NULL);
37     actions_register("AddDesktopLast",
38                      setup_addlast_func,
39                      free_func,
40                      run_func,
41                      NULL, NULL);
42     actions_register("RemoveDesktopLast",
43                      setup_removelast_func,
44                      free_func,
45                      run_func,
46                      NULL, NULL);
47     actions_register("AddDesktopCurrent",
48                      setup_addcurrent_func,
49                      free_func,
50                      run_func,
51                      NULL, NULL);
52     actions_register("RemoveDesktopCurrent",
53                      setup_removecurrent_func,
54                      free_func,
55                      run_func,
56                      NULL, NULL);
57 }
58
59 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
60 {
61     xmlNodePtr n;
62     Options *o;
63
64     o = g_new0(Options, 1);
65
66     if ((n = parse_find_node("where", node))) {
67         gchar *s = parse_string(doc, n);
68         if (!g_ascii_strcasecmp(s, "last"))
69             o->current = FALSE;
70         else if (!g_ascii_strcasecmp(s, "current"))
71             o->current = TRUE;
72         g_free(s);
73     }
74
75     return o;
76 }
77
78 static gpointer setup_add_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
79 {
80     Options *o = setup_func(i, doc, node);
81     o->add = TRUE;
82     return o;
83 }
84
85 static gpointer setup_remove_func(ObParseInst *i,
86                                   xmlDocPtr doc, xmlNodePtr node)
87 {
88     Options *o = setup_func(i, doc, node);
89     o->add = FALSE;
90     return o;
91 }
92
93 static gpointer setup_addcurrent_func(ObParseInst *i,
94                                       xmlDocPtr doc, xmlNodePtr node)
95 {
96     Options *o = setup_add_func(i, doc, node);
97     o->current = TRUE;
98     return o;
99 }
100
101 static gpointer setup_addlast_func(ObParseInst *i,
102                                    xmlDocPtr doc, xmlNodePtr node)
103 {
104     Options *o = setup_add_func(i, doc, node);
105     o->current = FALSE;
106     return o;
107 }
108
109 static gpointer setup_removecurrent_func(ObParseInst *i,
110                                       xmlDocPtr doc, xmlNodePtr node)
111 {
112     Options *o = setup_remove_func(i, doc, node);
113     o->current = TRUE;
114     return o;
115 }
116
117 static gpointer setup_removelast_func(ObParseInst *i,
118                                       xmlDocPtr doc, xmlNodePtr node)
119 {
120     Options *o = setup_remove_func(i, doc, node);
121     o->current = FALSE;
122     return o;
123 }
124
125 static void free_func(gpointer options)
126 {
127     Options *o = options;
128
129     g_free(o);
130 }
131
132 /* Always return FALSE because its not interactive */
133 static gboolean run_func(ObActionsData *data, gpointer options)
134 {
135     Options *o = options;
136
137     actions_client_move(data, TRUE);
138
139     if (o->add)
140         screen_add_desktop(o->current);
141     else
142         screen_remove_desktop(o->current);
143
144     actions_client_move(data, FALSE);
145
146     return FALSE;
147 }