1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 actions.h for the Openbox window manager
4 Copyright (c) 2007 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.
29 #include "actions/all.h"
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);
37 static ObActionsAct *interactive_act = NULL;
38 static guint interactive_initial_state = 0;
40 struct _ObActionsDefinition {
45 gboolean canbeinteractive;
47 ObActionsIDataSetupFunc i;
48 ObActionsDataSetupFunc n;
50 ObActionsDataFreeFunc free;
54 struct _ObActionsAct {
57 ObActionsDefinition *def;
58 ObActionsIPreFunc i_pre;
59 ObActionsIInputFunc i_input;
60 ObActionsICancelFunc i_cancel;
61 ObActionsIPostFunc i_post;
65 static GSList *registered = NULL;
67 void actions_startup(gboolean reconfig)
74 void actions_shutdown(gboolean reconfig)
76 actions_interactive_cancel_act();
80 /* free all the registered actions */
82 actions_definition_unref(registered->data);
83 registered = g_slist_delete_link(registered, registered);
87 ObActionsDefinition* do_register(const gchar *name,
88 ObActionsDataFreeFunc free,
92 ObActionsDefinition *def;
94 g_assert(run != NULL);
96 for (it = registered; it; it = g_slist_next(it)) {
98 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
102 def = g_new(ObActionsDefinition, 1);
104 def->name = g_strdup(name);
108 registered = g_slist_prepend(registered, def);
112 gboolean actions_register_i(const gchar *name,
113 ObActionsIDataSetupFunc setup,
114 ObActionsDataFreeFunc free,
115 ObActionsRunFunc run)
117 ObActionsDefinition *def = do_register(name, free, run);
119 def->canbeinteractive = TRUE;
120 def->setup.i = setup;
125 gboolean actions_register(const gchar *name,
126 ObActionsDataSetupFunc setup,
127 ObActionsDataFreeFunc free,
128 ObActionsRunFunc run)
130 ObActionsDefinition *def = do_register(name, free, run);
132 def->canbeinteractive = FALSE;
133 def->setup.n = setup;
138 static void actions_definition_ref(ObActionsDefinition *def)
143 static void actions_definition_unref(ObActionsDefinition *def)
145 if (def && --def->ref == 0) {
151 static ObActionsAct* actions_build_act_from_string(const gchar *name)
154 ObActionsDefinition *def = NULL;
155 ObActionsAct *act = NULL;
157 /* find the requested action */
158 for (it = registered; it; it = g_slist_next(it)) {
160 if (!g_ascii_strcasecmp(name, def->name))
165 /* if we found the action */
167 act = g_new(ObActionsAct, 1);
170 actions_definition_ref(act->def);
173 act->i_cancel = NULL;
177 g_message(_("Invalid action \"%s\" requested. No such action exists."),
183 ObActionsAct* actions_parse_string(const gchar *name)
185 ObActionsAct *act = NULL;
187 if ((act = actions_build_act_from_string(name))) {
188 if (act->def->canbeinteractive) {
189 if (act->def->setup.i)
190 act->options = act->def->setup.i(NULL,
197 if (act->def->setup.n)
198 act->options = act->def->setup.n(NULL);
206 ObActionsAct* actions_parse(xmlNodePtr node)
209 ObActionsAct *act = NULL;
211 if (obt_parse_attr_string(node, "name", &name)) {
212 if ((act = actions_build_act_from_string(name))) {
213 /* there is more stuff to parse here */
214 if (act->def->canbeinteractive) {
215 if (act->def->setup.i)
216 act->options = act->def->setup.i(node->children,
223 if (act->def->setup.n)
224 act->options = act->def->setup.n(node->children);
233 gboolean actions_act_is_interactive(ObActionsAct *act)
235 return act->i_input != NULL;
238 void actions_act_ref(ObActionsAct *act)
243 void actions_act_unref(ObActionsAct *act)
245 if (act && --act->ref == 0) {
246 /* free the action specific options */
248 act->def->free(act->options);
249 /* unref the definition */
250 actions_definition_unref(act->def);
255 static void actions_setup_data(ObActionsData *data,
262 struct _ObClient *client)
268 data->button = button;
270 data->client = client;
273 void actions_run_acts(GSList *acts,
280 struct _ObClient *client)
284 /* Don't allow saving the initial state when running things from the
286 if (uact == OB_USER_ACTION_MENU_SELECTION)
288 /* If x and y are < 0 then use the current pointer position */
290 screen_pointer_pos(&x, &y);
292 for (it = acts; it; it = g_slist_next(it)) {
294 ObActionsAct *act = it->data;
297 actions_setup_data(&data, uact, state, x, y, button, con, client);
299 /* if they have the same run function, then we'll assume they are
300 cooperating and not cancel eachother out */
301 if (!interactive_act || interactive_act->def->run != act->def->run) {
302 if (actions_act_is_interactive(act)) {
303 /* cancel the old one */
305 actions_interactive_cancel_act();
307 act->i_pre(act->options);
308 ok = actions_interactive_begin_act(act, state);
312 /* fire the action's run function with this data */
314 if (!act->def->run(&data, act->options)) {
315 if (actions_act_is_interactive(act))
316 actions_interactive_end_act();
318 /* make sure its interactive if it returned TRUE */
319 g_assert(act->i_input);
321 /* no actions are run after the interactive one */
328 gboolean actions_interactive_act_running(void)
330 return interactive_act != NULL;
333 void actions_interactive_cancel_act(void)
335 if (interactive_act) {
336 if (interactive_act->i_cancel)
337 interactive_act->i_cancel(interactive_act->options);
338 actions_interactive_end_act();
342 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
344 if (grab_keyboard()) {
345 interactive_act = act;
346 actions_act_ref(interactive_act);
348 interactive_initial_state = state;
350 /* if using focus_delay, stop the timer now so that focus doesn't go
351 moving on us, which would kill the action */
352 event_halt_focus_delay();
360 static void actions_interactive_end_act(void)
362 if (interactive_act) {
365 if (interactive_act->i_post)
366 interactive_act->i_post(interactive_act->options);
368 actions_act_unref(interactive_act);
369 interactive_act = NULL;
373 gboolean actions_interactive_input_event(XEvent *e)
375 gboolean used = FALSE;
376 if (interactive_act) {
377 if (!interactive_act->i_input(interactive_initial_state, e,
378 interactive_act->options, &used))
380 used = TRUE; /* if it cancelled the action then it has to of
382 actions_interactive_end_act();
388 void actions_client_move(ObActionsData *data, gboolean start)
390 static gulong ignore_start = 0;
392 ignore_start = event_start_ignore_all_enters();
393 else if (config_focus_follow &&
394 data->context != OB_FRAME_CONTEXT_CLIENT)
396 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
399 /* usually this is sorta redundant, but with a press action
400 that moves windows our from under the cursor, the enter
401 event will come as a GrabNotify which is ignored, so this
402 makes a fake enter event
404 don't do this if there is a grab on the pointer. enter events
405 are ignored during a grab, so don't force fake ones when they
408 if ((c = client_under_pointer()) && c != data->client &&
411 ob_debug_type(OB_DEBUG_FOCUS,
412 "Generating fake enter because we did a "
413 "mouse-event action");
414 event_enter_client(c);
417 else if (!data->button && !config_focus_under_mouse)
418 event_end_ignore_all_enters(ignore_start);