]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/actions.c
add the move action
[mikachu/openbox.git] / openbox / actions.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    actions.h for the Openbox window manager
4    Copyright (c) 2007        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "actions.h"
20 #include "gettext.h"
21 #include "grab.h"
22 #include "screen.h"
23
24 #include "actions/all.h"
25
26 static void     actions_definition_ref(ObActionsDefinition *def);
27 static void     actions_definition_unref(ObActionsDefinition *def);
28 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
29 static void     actions_interactive_end_act();
30
31 static ObActionsAct *interactive_act = NULL;
32 static guint         interactive_initial_state = 0;
33
34 struct _ObActionsDefinition {
35     guint ref;
36
37     gchar *name;
38
39     ObActionsDataSetupFunc setup;
40     ObActionsDataFreeFunc free;
41     ObActionsRunFunc run;
42     ObActionsInteractiveInputFunc i_input;
43     ObActionsInteractiveCancelFunc i_cancel;
44 };
45
46 struct _ObActionsAct {
47     guint ref;
48
49     ObActionsDefinition *def;
50     gpointer options;
51 };
52
53 static GSList *registered = NULL;
54
55
56 void actions_startup(gboolean reconfig)
57 {
58     if (reconfig) return;
59
60     action_all_startup();
61 }
62
63 void actions_shutdown(gboolean reconfig)
64 {
65     actions_interactive_cancel_act();
66
67     if (reconfig) return;
68
69     /* free all the registered actions */
70     while (registered) {
71         actions_definition_unref(registered->data);
72         registered = g_slist_delete_link(registered, registered);
73     }
74 }
75
76 gboolean actions_register(const gchar *name,
77                           ObActionsDataSetupFunc setup,
78                           ObActionsDataFreeFunc free,
79                           ObActionsRunFunc run,
80                           ObActionsInteractiveInputFunc i_input,
81                           ObActionsInteractiveCancelFunc i_cancel)
82 {
83     GSList *it;
84     ObActionsDefinition *def;
85
86     g_assert(run != NULL);
87     g_assert((i_input == NULL) == (i_cancel == NULL));
88
89     for (it = registered; it; it = g_slist_next(it)) {
90         def = it->data;
91         if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
92             return FALSE;
93     }
94
95     def = g_new(ObActionsDefinition, 1);
96     def->ref = 1;
97     def->name = g_strdup(name);
98     def->setup = setup;
99     def->free = free;
100     def->run = run;
101     def->i_input = i_input;
102     def->i_cancel = i_cancel;
103
104     registered = g_slist_prepend(registered, def);
105
106     return TRUE;
107 }
108
109 static void actions_definition_ref(ObActionsDefinition *def)
110 {
111     ++def->ref;
112 }
113
114 static void actions_definition_unref(ObActionsDefinition *def)
115 {
116     if (def && --def->ref == 0) {
117         g_free(def->name);
118         g_free(def);
119     }
120 }
121
122 ObActionsAct* actions_parse_string(const gchar *name)
123 {
124     GSList *it;
125     ObActionsDefinition *def = NULL;
126     ObActionsAct *act = NULL;
127
128     /* find the requested action */
129     for (it = registered; it; it = g_slist_next(it)) {
130         def = it->data;
131         if (!g_ascii_strcasecmp(name, def->name))
132             break;
133         def = NULL;
134     }
135
136     /* if we found the action */
137     if (def) {
138         act = g_new(ObActionsAct, 1);
139         act->ref = 1;
140         act->def = def;
141         actions_definition_ref(act->def);
142         act->options = NULL;
143     } else
144         g_message(_("Invalid action '%s' requested. No such action exists."),
145                   name);
146
147     return act;
148 }
149
150 ObActionsAct* actions_parse(ObParseInst *i,
151                             xmlDocPtr doc,
152                             xmlNodePtr node)
153 {
154     gchar *name;
155     ObActionsAct *act = NULL;
156
157     if (parse_attr_string("name", node, &name)) {
158         if ((act = actions_parse_string(name)))
159             /* there is more stuff to parse here */
160             if (act->def->setup)
161                 act->options = act->def->setup(i, doc, node->xmlChildrenNode);
162
163         g_free(name);
164     }
165
166     return act;
167 }
168
169 gboolean actions_act_is_interactive(ObActionsAct *act)
170 {
171     return act->def->i_cancel != NULL;
172 }
173
174 void actions_act_ref(ObActionsAct *act)
175 {
176     ++act->ref;
177 }
178
179 void actions_act_unref(ObActionsAct *act)
180 {
181     if (act && --act->ref == 0) {
182         /* free the action specific options */
183         if (act->def->free)
184             act->def->free(act->options);
185         /* unref the definition */
186         actions_definition_unref(act->def);
187         g_free(act);
188     }
189 }
190
191 static void actions_setup_data(ObActionsData *data,
192                                ObUserAction uact,
193                                guint state,
194                                gint x,
195                                gint y,
196                                gint button,
197                                ObFrameContext con,
198                                struct _ObClient *client)
199 {
200     data->uact = uact;
201     data->state = state;
202     data->x = x;
203     data->y = y;
204     data->button = button;
205     data->context = con;
206     data->client = client;
207 }
208
209 void actions_run_acts(GSList *acts,
210                       ObUserAction uact,
211                       guint state,
212                       gint x,
213                       gint y,
214                       gint button,
215                       ObFrameContext con,
216                       struct _ObClient *client)
217 {
218     GSList *it;
219
220     /* Don't allow saving the initial state when running things from the
221        menu */
222     if (uact == OB_USER_ACTION_MENU_SELECTION)
223         state = 0;
224     /* If x and y are < 0 then use the current pointer position */
225     if (x < 0 && y < 0)
226         screen_pointer_pos(&x, &y);
227
228     for (it = acts; it; it = g_slist_next(it)) {
229         ObActionsData data;
230         ObActionsAct *act = it->data;
231         gboolean ok = TRUE;
232
233         actions_setup_data(&data, uact, state, x, y, button, con, client);
234
235         if (actions_act_is_interactive(act) &&
236             (!interactive_act || interactive_act->def != act->def))
237         {
238             ok = actions_interactive_begin_act(act, state);
239         }
240
241         /* fire the action's run function with this data */
242         if (ok) {
243             if (!act->def->run(&data, act->options))
244                 actions_interactive_end_act();
245             else {
246                 /* make sure its interactive if it returned TRUE */
247                 g_assert(act->def->i_cancel && act->def->i_input);
248
249                 /* no actions are run after the interactive one */
250                 break;
251             }
252         }
253     }
254 }
255
256 gboolean actions_interactive_act_running()
257 {
258     return interactive_act != NULL;
259 }
260
261 void actions_interactive_cancel_act()
262 {
263     if (interactive_act) {
264         interactive_act->def->i_cancel(interactive_act->options);
265         actions_interactive_end_act();
266     }
267 }
268
269 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
270 {
271     /* cancel the old one */
272     if (interactive_act)
273         actions_interactive_cancel_act();
274
275     if (grab_keyboard()) {
276         interactive_act = act;
277         actions_act_ref(interactive_act);
278
279         interactive_initial_state = state;
280         return TRUE;
281     }
282     else
283         return FALSE;
284 }
285
286 static void actions_interactive_end_act()
287 {
288     if (interactive_act) {
289         ungrab_keyboard();
290
291         actions_act_unref(interactive_act);
292         interactive_act = NULL;
293     }
294 }
295
296 gboolean actions_interactive_input_event(XEvent *e)
297 {
298     gboolean used = FALSE;
299     if (interactive_act) {
300         if (!interactive_act->def->i_input(interactive_initial_state, e,
301                                            interactive_act->options, &used))
302         {
303             used = TRUE; /* if it cancelled the action then it has to of
304                             been used */
305             actions_interactive_end_act();
306         }
307     }
308     return used;
309 }