]> Dogcows Code - chaz/openbox/blob - openbox/actions.c
Merge branch 'master' into chaz
[chaz/openbox] / openbox / actions.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 actions.c 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 "focus.h"
27 #include "openbox.h"
28 #include "debug.h"
29
30 #include "actions/all.h"
31
32 static void actions_definition_ref(ObActionsDefinition *def);
33 static void actions_definition_unref(ObActionsDefinition *def);
34 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
35 static void actions_interactive_end_act();
36 static ObActionsAct* actions_build_act_from_string(const gchar *name);
37
38 static ObActionsAct *interactive_act = NULL;
39 static guint interactive_initial_state = 0;
40
41 struct _ObActionsDefinition {
42 guint ref;
43
44 gchar *name;
45
46 gboolean canbeinteractive;
47 union {
48 ObActionsIDataSetupFunc i;
49 ObActionsDataSetupFunc n;
50 } setup;
51 ObActionsDataFreeFunc free;
52 ObActionsRunFunc run;
53 ObActionsShutdownFunc shutdown;
54 gboolean modifies_focused_window;
55 gboolean can_stop;
56 };
57
58 struct _ObActionsAct {
59 guint ref;
60
61 ObActionsDefinition *def;
62 ObActionsIPreFunc i_pre;
63 ObActionsIInputFunc i_input;
64 ObActionsICancelFunc i_cancel;
65 ObActionsIPostFunc i_post;
66 gpointer options;
67 };
68
69 static GSList *registered = NULL;
70
71 void actions_startup(gboolean reconfig)
72 {
73 if (reconfig) return;
74
75 action_all_startup();
76 }
77
78 void actions_shutdown(gboolean reconfig)
79 {
80 actions_interactive_cancel_act();
81
82 if (reconfig) return;
83
84 /* free all the registered actions */
85 while (registered) {
86 ObActionsDefinition *d = registered->data;
87 if (d->shutdown) d->shutdown();
88 actions_definition_unref(d);
89 registered = g_slist_delete_link(registered, registered);
90 }
91 }
92
93 ObActionsDefinition* do_register(const gchar *name,
94 ObActionsDataFreeFunc free,
95 ObActionsRunFunc run)
96 {
97 GSList *it;
98 ObActionsDefinition *def;
99
100 g_assert(run != NULL);
101
102 for (it = registered; it; it = g_slist_next(it)) {
103 def = it->data;
104 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
105 return NULL;
106 }
107
108 def = g_slice_new0(ObActionsDefinition);
109 def->ref = 1;
110 def->name = g_strdup(name);
111 def->free = free;
112 def->run = run;
113 def->shutdown = NULL;
114 def->modifies_focused_window = TRUE;
115 def->can_stop = FALSE;
116
117 registered = g_slist_prepend(registered, def);
118 return def;
119 }
120
121 gboolean actions_register_i(const gchar *name,
122 ObActionsIDataSetupFunc setup,
123 ObActionsDataFreeFunc free,
124 ObActionsRunFunc run)
125 {
126 ObActionsDefinition *def = do_register(name, free, run);
127 if (def) {
128 def->canbeinteractive = TRUE;
129 def->setup.i = setup;
130 }
131 return def != NULL;
132 }
133
134 gboolean actions_register(const gchar *name,
135 ObActionsDataSetupFunc setup,
136 ObActionsDataFreeFunc free,
137 ObActionsRunFunc run)
138 {
139 ObActionsDefinition *def = do_register(name, free, run);
140 if (def) {
141 def->canbeinteractive = FALSE;
142 def->setup.n = setup;
143 }
144 return def != NULL;
145 }
146
147 gboolean actions_set_shutdown(const gchar *name,
148 ObActionsShutdownFunc shutdown)
149 {
150 GSList *it;
151 ObActionsDefinition *def;
152
153 for (it = registered; it; it = g_slist_next(it)) {
154 def = it->data;
155 if (!g_ascii_strcasecmp(name, def->name)) {
156 def->shutdown = shutdown;
157 return TRUE;
158 }
159 }
160 return FALSE;
161 }
162
163 gboolean actions_set_modifies_focused_window(const gchar *name,
164 gboolean modifies)
165 {
166 GSList *it;
167 ObActionsDefinition *def;
168
169 for (it = registered; it; it = g_slist_next(it)) {
170 def = it->data;
171 if (!g_ascii_strcasecmp(name, def->name)) {
172 def->modifies_focused_window = modifies;
173 return TRUE;
174 }
175 }
176 return FALSE;
177 }
178
179 gboolean actions_set_can_stop(const gchar *name,
180 gboolean can_stop)
181 {
182 GSList *it;
183 ObActionsDefinition *def;
184
185 for (it = registered; it; it = g_slist_next(it)) {
186 def = it->data;
187 if (!g_ascii_strcasecmp(name, def->name)) {
188 def->can_stop = can_stop;
189 return TRUE;
190 }
191 }
192 return FALSE;
193 }
194
195 static void actions_definition_ref(ObActionsDefinition *def)
196 {
197 ++def->ref;
198 }
199
200 static void actions_definition_unref(ObActionsDefinition *def)
201 {
202 if (def && --def->ref == 0) {
203 g_free(def->name);
204 g_slice_free(ObActionsDefinition, def);
205 }
206 }
207
208 static ObActionsAct* actions_build_act_from_string(const gchar *name)
209 {
210 GSList *it;
211 ObActionsDefinition *def = NULL;
212 ObActionsAct *act = NULL;
213
214 /* find the requested action */
215 for (it = registered; it; it = g_slist_next(it)) {
216 def = it->data;
217 if (!g_ascii_strcasecmp(name, def->name))
218 break;
219 def = NULL;
220 }
221
222 /* if we found the action */
223 if (def) {
224 act = g_slice_new(ObActionsAct);
225 act->ref = 1;
226 act->def = def;
227 actions_definition_ref(act->def);
228 act->i_pre = NULL;
229 act->i_input = NULL;
230 act->i_cancel = NULL;
231 act->i_post = NULL;
232 act->options = NULL;
233 } else
234 g_message(_("Invalid action \"%s\" requested. No such action exists."),
235 name);
236
237 return act;
238 }
239
240 ObActionsAct* actions_parse_string(const gchar *name)
241 {
242 ObActionsAct *act = NULL;
243
244 if ((act = actions_build_act_from_string(name))) {
245 if (act->def->canbeinteractive) {
246 if (act->def->setup.i)
247 act->options = act->def->setup.i(NULL,
248 &act->i_pre,
249 &act->i_input,
250 &act->i_cancel,
251 &act->i_post);
252 }
253 else {
254 if (act->def->setup.n)
255 act->options = act->def->setup.n(NULL);
256 }
257 }
258
259
260 return act;
261 }
262
263 ObActionsAct* actions_parse(xmlNodePtr node)
264 {
265 gchar *name;
266 ObActionsAct *act = NULL;
267
268 if (obt_xml_attr_string(node, "name", &name)) {
269 if ((act = actions_build_act_from_string(name))) {
270 /* there is more stuff to parse here */
271 if (act->def->canbeinteractive) {
272 if (act->def->setup.i)
273 act->options = act->def->setup.i(node->children,
274 &act->i_pre,
275 &act->i_input,
276 &act->i_cancel,
277 &act->i_post);
278 }
279 else {
280 if (act->def->setup.n)
281 act->options = act->def->setup.n(node->children);
282 }
283 }
284 g_free(name);
285 }
286
287 return act;
288 }
289
290 gboolean actions_act_is_interactive(ObActionsAct *act)
291 {
292 return act->i_input != NULL;
293 }
294
295 void actions_act_ref(ObActionsAct *act)
296 {
297 ++act->ref;
298 }
299
300 void actions_act_unref(ObActionsAct *act)
301 {
302 if (act && --act->ref == 0) {
303 /* free the action specific options */
304 if (act->def->free)
305 act->def->free(act->options);
306 /* unref the definition */
307 actions_definition_unref(act->def);
308 g_slice_free(ObActionsAct, act);
309 }
310 }
311
312 static void actions_setup_data(ObActionsData *data,
313 ObUserAction uact,
314 guint state,
315 gint x,
316 gint y,
317 gint button,
318 ObFrameContext con,
319 struct _ObClient *client)
320 {
321 data->uact = uact;
322 data->state = state;
323 data->x = x;
324 data->y = y;
325 data->button = button;
326 data->context = con;
327 data->client = client;
328 }
329
330 void actions_run_acts(GSList *acts,
331 ObUserAction uact,
332 guint state,
333 gint x,
334 gint y,
335 gint button,
336 ObFrameContext con,
337 struct _ObClient *client)
338 {
339 GSList *it;
340 gboolean update_user_time;
341
342 /* Don't allow saving the initial state when running things from the
343 menu */
344 if (uact == OB_USER_ACTION_MENU_SELECTION)
345 state = 0;
346 /* If x and y are < 0 then use the current pointer position */
347 if (x < 0 && y < 0)
348 screen_pointer_pos(&x, &y);
349
350 update_user_time = FALSE;
351 for (it = acts; it; it = g_slist_next(it)) {
352 ObActionsData data;
353 ObActionsAct *act = it->data;
354 gboolean ok = TRUE;
355
356 actions_setup_data(&data, uact, state, x, y, button, con, client);
357
358 /* if they have the same run function, then we'll assume they are
359 cooperating and not cancel eachother out */
360 if (!interactive_act || interactive_act->def->run != act->def->run) {
361 if (actions_act_is_interactive(act)) {
362 /* cancel the old one */
363 if (interactive_act)
364 actions_interactive_cancel_act();
365 if (act->i_pre)
366 if (!act->i_pre(state, act->options))
367 act->i_input = NULL; /* remove the interactivity */
368 }
369 /* check again cuz it might have been cancelled */
370 if (actions_act_is_interactive(act))
371 ok = actions_interactive_begin_act(act, state);
372 }
373
374 /* fire the action's run function with this data */
375 if (ok) {
376 if (!act->def->run(&data, act->options)) {
377 if (actions_act_is_interactive(act)) {
378 actions_interactive_end_act();
379 }
380 if (client && client == focus_client &&
381 act->def->modifies_focused_window)
382 {
383 update_user_time = TRUE;
384 }
385 } else {
386 /* make sure its interactive or allowed to stop
387 if it returned TRUE */
388 g_assert(act->i_input || act->def->can_stop);
389
390 /* no actions are run after the interactive one */
391 break;
392 }
393 }
394 }
395 if (update_user_time)
396 event_update_user_time();
397 }
398
399 gboolean actions_interactive_act_running(void)
400 {
401 return interactive_act != NULL;
402 }
403
404 void actions_interactive_cancel_act(void)
405 {
406 if (interactive_act) {
407 if (interactive_act->i_cancel)
408 interactive_act->i_cancel(interactive_act->options);
409 actions_interactive_end_act();
410 }
411 }
412
413 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
414 {
415 if (grab_keyboard()) {
416 interactive_act = act;
417 actions_act_ref(interactive_act);
418
419 interactive_initial_state = state;
420
421 /* if using focus_delay, stop the timer now so that focus doesn't go
422 moving on us, which would kill the action */
423 event_halt_focus_delay();
424
425 return TRUE;
426 }
427 else
428 return FALSE;
429 }
430
431 static void actions_interactive_end_act(void)
432 {
433 if (interactive_act) {
434 ObActionsAct *ia = interactive_act;
435
436 /* set this to NULL first so the i_post() function can't cause this to
437 get called again (if it decides it wants to cancel any ongoing
438 interactive action). */
439 interactive_act = NULL;
440
441 ungrab_keyboard();
442
443 if (ia->i_post)
444 ia->i_post(ia->options);
445
446 actions_act_unref(ia);
447 }
448 }
449
450 gboolean actions_interactive_input_event(XEvent *e)
451 {
452 gboolean used = FALSE;
453 if (interactive_act) {
454 if (!interactive_act->i_input(interactive_initial_state, e,
455 grab_input_context(),
456 interactive_act->options, &used))
457 {
458 used = TRUE; /* if it cancelled the action then it has to of
459 been used */
460 actions_interactive_end_act();
461 }
462 }
463 return used;
464 }
465
466 void actions_client_move(ObActionsData *data, gboolean start)
467 {
468 static gulong ignore_start = 0;
469 if (start)
470 ignore_start = event_start_ignore_all_enters();
471 else if (config_focus_follow &&
472 data->context != OB_FRAME_CONTEXT_CLIENT)
473 {
474 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
475 struct _ObClient *c;
476
477 /* usually this is sorta redundant, but with a press action
478 that moves windows our from under the cursor, the enter
479 event will come as a GrabNotify which is ignored, so this
480 makes a fake enter event
481
482 don't do this if there is a grab on the pointer. enter events
483 are ignored during a grab, so don't force fake ones when they
484 should be ignored
485 */
486 if (!grab_on_pointer()) {
487 if ((c = client_under_pointer()) && c != data->client) {
488 ob_debug_type(OB_DEBUG_FOCUS,
489 "Generating fake enter because we did a "
490 "mouse-event action");
491 event_enter_client(c);
492 }
493 else if (!c && c != data->client) {
494 ob_debug_type(OB_DEBUG_FOCUS,
495 "Generating fake leave because we did a "
496 "mouse-event action");
497 event_leave_client(data->client);
498 }
499 }
500 }
501 else if (!data->button && !config_focus_under_mouse)
502 event_end_ignore_all_enters(ignore_start);
503 }
504 }
This page took 0.049886 seconds and 4 git commands to generate.