1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 action.c for the Openbox window manager
4 Copyright (c) 2007-2011 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
20 #include "action_list.h"
31 #include "actions/all.h"
33 static void action_definition_ref(ObActionDefinition *def);
34 static void action_definition_unref(ObActionDefinition *def);
35 static gboolean action_interactive_begin_act(ObAction *act, guint state);
36 static void action_interactive_end_act();
37 static ObAction* action_find_by_name(const gchar *name);
39 static ObAction *interactive_act = NULL;
40 static guint interactive_initial_state = 0;
42 struct _ObActionDefinition {
47 gboolean canbeinteractive;
49 ObActionIDataSetupFunc i;
50 ObActionDataSetupFunc n;
52 ObActionDataFreeFunc free;
54 ObActionShutdownFunc shutdown;
60 ObActionDefinition *def;
61 ObActionIPreFunc i_pre;
62 ObActionIInputFunc i_input;
63 ObActionICancelFunc i_cancel;
64 ObActionIPostFunc i_post;
68 static GSList *registered = NULL;
70 void action_startup(gboolean reconfig)
74 actions_all_startup();
77 void action_shutdown(gboolean reconfig)
79 action_interactive_cancel_act();
83 /* free all the registered actions */
85 ObActionDefinition *d = registered->data;
86 if (d->shutdown) d->shutdown();
87 action_definition_unref(d);
88 registered = g_slist_delete_link(registered, registered);
92 ObActionDefinition* do_register(const gchar *name,
93 ObActionDataFreeFunc free,
97 ObActionDefinition *def;
99 g_assert(run != NULL);
101 for (it = registered; it; it = g_slist_next(it)) {
103 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
107 def = g_slice_new(ObActionDefinition);
109 def->name = g_strdup(name);
112 def->shutdown = NULL;
114 registered = g_slist_prepend(registered, def);
118 gboolean action_register_i(const gchar *name,
119 ObActionIDataSetupFunc setup,
120 ObActionDataFreeFunc free,
123 ObActionDefinition *def = do_register(name, free, run);
125 def->canbeinteractive = TRUE;
126 def->setup.i = setup;
131 gboolean action_register(const gchar *name,
132 ObActionDataSetupFunc setup,
133 ObActionDataFreeFunc free,
136 ObActionDefinition *def = do_register(name, free, run);
138 def->canbeinteractive = FALSE;
139 def->setup.n = setup;
144 gboolean action_set_shutdown(const gchar *name,
145 ObActionShutdownFunc shutdown)
148 ObActionDefinition *def;
150 for (it = registered; it; it = g_slist_next(it)) {
152 if (!g_ascii_strcasecmp(name, def->name)) {
153 def->shutdown = shutdown;
160 static void action_definition_ref(ObActionDefinition *def)
165 static void action_definition_unref(ObActionDefinition *def)
167 if (def && --def->ref == 0) {
169 g_slice_free(ObActionDefinition, def);
173 static ObAction* action_find_by_name(const gchar *name)
176 ObActionDefinition *def = NULL;
177 ObAction *act = NULL;
179 /* find the requested action */
180 for (it = registered; it; it = g_slist_next(it)) {
182 if (!g_ascii_strcasecmp(name, def->name))
187 /* if we found the action */
189 act = g_slice_new(ObAction);
192 action_definition_ref(act->def);
195 act->i_cancel = NULL;
199 g_message(_("Invalid action \"%s\" requested. No such action exists."),
205 ObAction* action_new(const gchar *name, GHashTable *config)
207 ObAction *act = NULL;
209 act = action_find_by_name(name);
211 /* there is more stuff to parse here */
212 if (act->def->canbeinteractive) {
213 if (act->def->setup.i)
214 act->options = act->def->setup.i(config,
221 if (act->def->setup.n)
222 act->options = act->def->setup.n(config);
229 gboolean action_is_interactive(ObAction *act)
231 return act->i_input != NULL;
234 void action_ref(ObAction *act)
239 void action_unref(ObAction *act)
241 if (act && --act->ref == 0) {
242 /* free the action specific options */
244 act->def->free(act->options);
245 /* unref the definition */
246 action_definition_unref(act->def);
247 g_slice_free(ObAction, act);
251 static void action_setup_data(ObActionData *data,
258 struct _ObClient *client)
264 data->button = button;
266 data->client = client;
269 gboolean action_run_acts(ObActionList *acts,
276 struct _ObClient *client)
278 gboolean ran_interactive;
279 gboolean update_user_time;
281 /* Don't allow saving the initial state when running things from the
283 if (uact == OB_USER_ACTION_MENU_SELECTION)
285 /* If x and y are < 0 then use the current pointer position */
287 screen_pointer_pos(&x, &y);
289 ran_interactive = FALSE;
290 update_user_time = FALSE;
296 if (acts->isfilter) {
297 g_warning("filters not implemented!");
302 act = acts->u.action;
305 action_setup_data(&data, uact, state, x, y, button, con, client);
307 /* if they have the same run function, then we'll assume they are
308 cooperating and not cancel eachother out */
309 if (!interactive_act || interactive_act->def->run != act->def->run) {
310 if (action_is_interactive(act)) {
311 /* cancel the old one */
313 action_interactive_cancel_act();
315 if (!act->i_pre(state, act->options))
316 act->i_input = NULL; /* remove the interactivity */
317 ran_interactive = TRUE;
319 /* check again cuz it might have been cancelled */
320 if (action_is_interactive(act)) {
321 ok = action_interactive_begin_act(act, state);
322 ran_interactive = TRUE;
326 /* fire the action's run function with this data */
328 if (!act->def->run(&data, act->options)) {
329 if (action_is_interactive(act))
330 action_interactive_end_act();
331 if (client && client == focus_client)
332 update_user_time = TRUE;
334 /* make sure its interactive if it returned TRUE */
335 g_assert(act->i_input);
337 /* no actions are run after the interactive one */
343 if (update_user_time)
344 event_update_user_time();
345 return ran_interactive;
348 gboolean action_interactive_act_running(void)
350 return interactive_act != NULL;
353 void action_interactive_cancel_act(void)
355 if (interactive_act) {
356 if (interactive_act->i_cancel)
357 interactive_act->i_cancel(interactive_act->options);
358 action_interactive_end_act();
362 static gboolean action_interactive_begin_act(ObAction *act, guint state)
364 if (grab_keyboard()) {
365 interactive_act = act;
366 action_ref(interactive_act);
368 interactive_initial_state = state;
370 /* if using focus_delay, stop the timer now so that focus doesn't go
371 moving on us, which would kill the action */
372 event_halt_focus_delay();
380 static void action_interactive_end_act(void)
382 if (interactive_act) {
383 ObAction *ia = interactive_act;
385 /* set this to NULL first so the i_post() function can't cause this to
386 get called again (if it decides it wants to cancel any ongoing
387 interactive action). */
388 interactive_act = NULL;
393 ia->i_post(ia->options);
399 gboolean action_interactive_input_event(XEvent *e)
401 gboolean used = FALSE;
402 if (interactive_act) {
403 if (!interactive_act->i_input(interactive_initial_state, e,
404 grab_input_context(),
405 interactive_act->options, &used))
407 used = TRUE; /* if it cancelled the action then it has to of
409 action_interactive_end_act();
415 void action_client_move(ObActionData *data, gboolean start)
417 static gulong ignore_start = 0;
419 ignore_start = event_start_ignore_all_enters();
420 else if (config_focus_follow &&
421 data->context != OB_FRAME_CONTEXT_CLIENT)
423 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
426 /* usually this is sorta redundant, but with a press action
427 that moves windows our from under the cursor, the enter
428 event will come as a GrabNotify which is ignored, so this
429 makes a fake enter event
431 don't do this if there is a grab on the pointer. enter events
432 are ignored during a grab, so don't force fake ones when they
435 if (!grab_on_pointer()) {
436 if ((c = client_under_pointer()) && c != data->client) {
437 ob_debug_type(OB_DEBUG_FOCUS,
438 "Generating fake enter because we did a "
439 "mouse-event action");
440 event_enter_client(c);
442 else if (!c && c != data->client) {
443 ob_debug_type(OB_DEBUG_FOCUS,
444 "Generating fake leave because we did a "
445 "mouse-event action");
446 event_enter_client(data->client);
450 else if (!data->button && !config_focus_under_mouse)
451 event_end_ignore_all_enters(ignore_start);