]> Dogcows Code - chaz/openbox/blob - openbox/actions.c
the execute action works again.
[chaz/openbox] / 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 for (it = registered; it; it = g_slist_next(it)) {
87 def = it->data;
88 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
89 return FALSE;
90 }
91
92 g_assert((i_input == NULL) == (i_cancel == NULL));
93
94 def = g_new(ObActionsDefinition, 1);
95 def->ref = 1;
96 def->name = g_strdup(name);
97 def->setup = setup;
98 def->free = free;
99 def->run = run;
100 def->i_input = i_input;
101 def->i_cancel = i_cancel;
102
103 registered = g_slist_prepend(registered, def);
104
105 return TRUE;
106 }
107
108 static void actions_definition_ref(ObActionsDefinition *def)
109 {
110 ++def->ref;
111 }
112
113 static void actions_definition_unref(ObActionsDefinition *def)
114 {
115 if (def && --def->ref == 0) {
116 g_free(def->name);
117 g_free(def);
118 }
119 }
120
121 ObActionsAct* actions_parse_string(const gchar *name)
122 {
123 GSList *it;
124 ObActionsDefinition *def = NULL;
125 ObActionsAct *act = NULL;
126
127 /* find the requested action */
128 for (it = registered; it; it = g_slist_next(it)) {
129 def = it->data;
130 if (!g_ascii_strcasecmp(name, def->name))
131 break;
132 def = NULL;
133 }
134
135 /* if we found the action */
136 if (def) {
137 act = g_new(ObActionsAct, 1);
138 act->ref = 1;
139 act->def = def;
140 actions_definition_ref(act->def);
141 act->options = NULL;
142 } else
143 g_message(_("Invalid action '%s' requested. No such action exists."),
144 name);
145
146 return act;
147 }
148
149 ObActionsAct* actions_parse(ObParseInst *i,
150 xmlDocPtr doc,
151 xmlNodePtr node)
152 {
153 gchar *name;
154 ObActionsAct *act = NULL;
155
156 if (parse_attr_string("name", node, &name)) {
157 if ((act = actions_parse_string(name)))
158 /* there is more stuff to parse here */
159 act->options = act->def->setup(i, doc, node->xmlChildrenNode);
160
161 g_free(name);
162 }
163
164 return act;
165 }
166
167 gboolean actions_act_is_interactive(ObActionsAct *act)
168 {
169 return act->def->i_cancel != NULL;
170 }
171
172 void actions_act_ref(ObActionsAct *act)
173 {
174 ++act->ref;
175 }
176
177 void actions_act_unref(ObActionsAct *act)
178 {
179 if (act && --act->ref == 0) {
180 /* free the action specific options */
181 act->def->free(act->options);
182 /* unref the definition */
183 actions_definition_unref(act->def);
184 g_free(act);
185 }
186 }
187
188 static void actions_setup_data(ObActionsData *data,
189 ObUserAction uact,
190 Time time,
191 guint state,
192 gint x,
193 gint y,
194 ObFrameContext con,
195 struct _ObClient *client)
196 {
197 data->uact = uact;
198 data->time = time;
199 data->state = state;
200 data->x = x;
201 data->y = y;
202 data->context = con;
203 data->client = client;
204 }
205
206 void actions_run_acts(GSList *acts,
207 ObUserAction uact,
208 Time time,
209 guint state,
210 gint x,
211 gint y,
212 ObFrameContext con,
213 struct _ObClient *client)
214 {
215 GSList *it;
216
217 /* Don't allow saving the initial state when running things from the
218 menu */
219 if (uact == OB_USER_ACTION_MENU_SELECTION)
220 state = 0;
221 /* If x and y are < 0 then use the current pointer position */
222 if (x < 0 && y < 0)
223 screen_pointer_pos(&x, &y);
224
225 for (it = acts; it; it = g_slist_next(it)) {
226 ObActionsData data;
227 ObActionsAct *act = it->data;
228 gboolean ok = TRUE;
229
230 actions_setup_data(&data, uact, time, state, x, y, con, client);
231
232 if (actions_act_is_interactive(act) &&
233 (!interactive_act || interactive_act->def != act->def))
234 {
235 ok = actions_interactive_begin_act(act, state);
236 }
237
238 /* fire the action's run function with this data */
239 if (ok) {
240 if (!act->def->run(&data, act->options))
241 actions_interactive_end_act();
242 else {
243 /* make sure its interactive if it returned TRUE */
244 g_assert(act->def->i_cancel && act->def->i_input);
245
246 /* no actions are run after the interactive one */
247 break;
248 }
249 }
250 }
251 }
252
253 gboolean actions_interactive_act_running()
254 {
255 return interactive_act != NULL;
256 }
257
258 void actions_interactive_cancel_act()
259 {
260 if (interactive_act) {
261 interactive_act->def->i_cancel(interactive_act->options);
262 actions_interactive_end_act();
263 }
264 }
265
266 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
267 {
268 /* cancel the old one */
269 if (interactive_act)
270 actions_interactive_cancel_act();
271
272 if (grab_keyboard()) {
273 interactive_act = act;
274 actions_act_ref(interactive_act);
275
276 interactive_initial_state = state;
277 return TRUE;
278 }
279 else
280 return FALSE;
281 }
282
283 static void actions_interactive_end_act()
284 {
285 if (interactive_act) {
286 ungrab_keyboard();
287
288 actions_act_unref(interactive_act);
289 interactive_act = NULL;
290 }
291 }
292
293 gboolean actions_interactive_input_event(XEvent *e)
294 {
295 gboolean used = FALSE;
296 if (interactive_act) {
297 if (!interactive_act->def->i_input(interactive_initial_state, e,
298 interactive_act->options, &used))
299 {
300 used = TRUE; /* if it cancelled the action then it has to of
301 been used */
302 actions_interactive_end_act();
303 }
304 }
305 return used;
306 }
This page took 0.048959 seconds and 5 git commands to generate.