]> Dogcows Code - chaz/openbox/blob - openbox/actions.c
Fix an unsigned/signed warning.
[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 #include "event.h"
24 #include "config.h"
25 #include "client.h"
26 #include "openbox.h"
27 #include "debug.h"
28
29 #include "actions/all.h"
30
31 static void actions_definition_ref(ObActionsDefinition *def);
32 static void actions_definition_unref(ObActionsDefinition *def);
33 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
34 static void actions_interactive_end_act();
35 static ObActionsAct* actions_build_act_from_string(const gchar *name);
36
37 static ObActionsAct *interactive_act = NULL;
38 static guint interactive_initial_state = 0;
39
40 struct _ObActionsDefinition {
41 guint ref;
42
43 gchar *name;
44
45 ObActionsDataSetupFunc setup;
46 ObActionsDataFreeFunc free;
47 ObActionsRunFunc run;
48 ObActionsInteractiveInputFunc i_input;
49 ObActionsInteractiveCancelFunc i_cancel;
50 };
51
52 struct _ObActionsAct {
53 guint ref;
54
55 ObActionsDefinition *def;
56 gpointer options;
57 };
58
59 static GSList *registered = NULL;
60
61 void actions_startup(gboolean reconfig)
62 {
63 if (reconfig) return;
64
65 action_all_startup();
66 }
67
68 void actions_shutdown(gboolean reconfig)
69 {
70 actions_interactive_cancel_act();
71
72 if (reconfig) return;
73
74 /* free all the registered actions */
75 while (registered) {
76 actions_definition_unref(registered->data);
77 registered = g_slist_delete_link(registered, registered);
78 }
79 }
80
81 gboolean actions_register(const gchar *name,
82 ObActionsDataSetupFunc setup,
83 ObActionsDataFreeFunc free,
84 ObActionsRunFunc run,
85 ObActionsInteractiveInputFunc i_input,
86 ObActionsInteractiveCancelFunc i_cancel)
87 {
88 GSList *it;
89 ObActionsDefinition *def;
90
91 g_assert(run != NULL);
92 g_assert((i_input == NULL) == (i_cancel == NULL));
93
94 for (it = registered; it; it = g_slist_next(it)) {
95 def = it->data;
96 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
97 return FALSE;
98 }
99
100 def = g_new(ObActionsDefinition, 1);
101 def->ref = 1;
102 def->name = g_strdup(name);
103 def->setup = setup;
104 def->free = free;
105 def->run = run;
106 def->i_input = i_input;
107 def->i_cancel = i_cancel;
108
109 registered = g_slist_prepend(registered, def);
110
111 return TRUE;
112 }
113
114 static void actions_definition_ref(ObActionsDefinition *def)
115 {
116 ++def->ref;
117 }
118
119 static void actions_definition_unref(ObActionsDefinition *def)
120 {
121 if (def && --def->ref == 0) {
122 g_free(def->name);
123 g_free(def);
124 }
125 }
126
127 static ObActionsAct* actions_build_act_from_string(const gchar *name)
128 {
129 GSList *it;
130 ObActionsDefinition *def = NULL;
131 ObActionsAct *act = NULL;
132
133 /* find the requested action */
134 for (it = registered; it; it = g_slist_next(it)) {
135 def = it->data;
136 if (!g_ascii_strcasecmp(name, def->name))
137 break;
138 def = NULL;
139 }
140
141 /* if we found the action */
142 if (def) {
143 act = g_new(ObActionsAct, 1);
144 act->ref = 1;
145 act->def = def;
146 actions_definition_ref(act->def);
147 act->options = NULL;
148 } else
149 g_message(_("Invalid action \"%s\" requested. No such action exists."),
150 name);
151
152 return act;
153 }
154
155 ObActionsAct* actions_parse_string(const gchar *name)
156 {
157 ObActionsAct *act = NULL;
158
159 if ((act = actions_build_act_from_string(name)))
160 if (act->def->setup)
161 act->options = act->def->setup(NULL);
162
163 return act;
164 }
165
166 ObActionsAct* actions_parse(xmlNodePtr node)
167 {
168 gchar *name;
169 ObActionsAct *act = NULL;
170
171 if (obt_parse_attr_string(node, "name", &name)) {
172 if ((act = actions_build_act_from_string(name)))
173 /* there is more stuff to parse here */
174 if (act->def->setup)
175 act->options = act->def->setup(node->children);
176
177 g_free(name);
178 }
179
180 return act;
181 }
182
183 gboolean actions_act_is_interactive(ObActionsAct *act)
184 {
185 return act->def->i_cancel != NULL;
186 }
187
188 void actions_act_ref(ObActionsAct *act)
189 {
190 ++act->ref;
191 }
192
193 void actions_act_unref(ObActionsAct *act)
194 {
195 if (act && --act->ref == 0) {
196 /* free the action specific options */
197 if (act->def->free)
198 act->def->free(act->options);
199 /* unref the definition */
200 actions_definition_unref(act->def);
201 g_free(act);
202 }
203 }
204
205 static void actions_setup_data(ObActionsData *data,
206 ObUserAction uact,
207 guint state,
208 gint x,
209 gint y,
210 gint button,
211 ObFrameContext con,
212 struct _ObClient *client)
213 {
214 data->uact = uact;
215 data->state = state;
216 data->x = x;
217 data->y = y;
218 data->button = button;
219 data->context = con;
220 data->client = client;
221 }
222
223 void actions_run_acts(GSList *acts,
224 ObUserAction uact,
225 guint state,
226 gint x,
227 gint y,
228 gint button,
229 ObFrameContext con,
230 struct _ObClient *client)
231 {
232 GSList *it;
233
234 /* Don't allow saving the initial state when running things from the
235 menu */
236 if (uact == OB_USER_ACTION_MENU_SELECTION)
237 state = 0;
238 /* If x and y are < 0 then use the current pointer position */
239 if (x < 0 && y < 0)
240 screen_pointer_pos(&x, &y);
241
242 for (it = acts; it; it = g_slist_next(it)) {
243 ObActionsData data;
244 ObActionsAct *act = it->data;
245 gboolean ok = TRUE;
246
247 actions_setup_data(&data, uact, state, x, y, button, con, client);
248
249 /* if they have the same run function, then we'll assume they are
250 cooperating and not cancel eachother out */
251 if (!interactive_act || interactive_act->def->run != act->def->run) {
252 if (actions_act_is_interactive(act)) {
253 /* cancel the old one */
254 if (interactive_act)
255 actions_interactive_cancel_act();
256 ok = actions_interactive_begin_act(act, state);
257 }
258 }
259
260 /* fire the action's run function with this data */
261 if (ok) {
262 if (!act->def->run(&data, act->options)) {
263 if (actions_act_is_interactive(act))
264 actions_interactive_end_act();
265 } else {
266 /* make sure its interactive if it returned TRUE */
267 g_assert(act->def->i_cancel && act->def->i_input);
268
269 /* no actions are run after the interactive one */
270 break;
271 }
272 }
273 }
274 }
275
276 gboolean actions_interactive_act_running(void)
277 {
278 return interactive_act != NULL;
279 }
280
281 void actions_interactive_cancel_act(void)
282 {
283 if (interactive_act) {
284 interactive_act->def->i_cancel(interactive_act->options);
285 actions_interactive_end_act();
286 }
287 }
288
289 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
290 {
291 if (grab_keyboard()) {
292 interactive_act = act;
293 actions_act_ref(interactive_act);
294
295 interactive_initial_state = state;
296
297 /* if using focus_delay, stop the timer now so that focus doesn't go
298 moving on us, which would kill the action */
299 event_halt_focus_delay();
300
301 return TRUE;
302 }
303 else
304 return FALSE;
305 }
306
307 static void actions_interactive_end_act(void)
308 {
309 if (interactive_act) {
310 ungrab_keyboard();
311
312 actions_act_unref(interactive_act);
313 interactive_act = NULL;
314 }
315 }
316
317 gboolean actions_interactive_input_event(XEvent *e)
318 {
319 gboolean used = FALSE;
320 if (interactive_act) {
321 if (!interactive_act->def->i_input(interactive_initial_state, e,
322 interactive_act->options, &used))
323 {
324 used = TRUE; /* if it cancelled the action then it has to of
325 been used */
326 actions_interactive_end_act();
327 }
328 }
329 return used;
330 }
331
332 void actions_client_move(ObActionsData *data, gboolean start)
333 {
334 static gulong ignore_start = 0;
335 if (start)
336 ignore_start = event_start_ignore_all_enters();
337 else if (config_focus_follow &&
338 data->context != OB_FRAME_CONTEXT_CLIENT)
339 {
340 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
341 struct _ObClient *c;
342
343 /* usually this is sorta redundant, but with a press action
344 that moves windows our from under the cursor, the enter
345 event will come as a GrabNotify which is ignored, so this
346 makes a fake enter event
347
348 don't do this if there is a grab on the pointer. enter events
349 are ignored during a grab, so don't force fake ones when they
350 should be ignored
351 */
352 if ((c = client_under_pointer()) && c != data->client &&
353 !grab_on_pointer())
354 {
355 ob_debug_type(OB_DEBUG_FOCUS,
356 "Generating fake enter because we did a "
357 "mouse-event action");
358 event_enter_client(c);
359 }
360 }
361 else if (!data->button && !config_focus_under_mouse)
362 event_end_ignore_all_enters(ignore_start);
363 }
364 }
This page took 0.055558 seconds and 4 git commands to generate.