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.
24 static void actions_definition_ref(ObActionsDefinition *def);
25 static void actions_definition_unref(ObActionsDefinition *def);
26 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
27 static void actions_interactive_end_act();
29 static ObActionsAct *interactive_act = NULL;
30 static guint interactive_initial_state = 0;
32 struct _ObActionsDefinition {
38 ObActionsDataSetupFunc setup;
39 ObActionsDataFreeFunc free;
41 ObActionsInteractiveInputFunc i_input;
42 ObActionsInteractiveCancelFunc i_cancel;
45 struct _ObActionsAct {
48 ObActionsDefinition *def;
52 static GSList *registered = NULL;
55 void actions_startup(gboolean reconfig)
60 void actions_shutdown(gboolean reconfig)
62 actions_interactive_cancel_act();
66 /* free all the registered actions */
68 actions_definition_unref(registered->data);
69 registered = g_slist_delete_link(registered, registered);
73 gboolean actions_register(const gchar *name,
75 ObActionsDataSetupFunc setup,
76 ObActionsDataFreeFunc free,
78 ObActionsInteractiveInputFunc i_input,
79 ObActionsInteractiveCancelFunc i_cancel)
82 ObActionsDefinition *def;
84 for (it = registered; it; it = g_slist_next(it)) {
86 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
90 g_assert((i_input == NULL) == (i_cancel == NULL));
92 def = g_new(ObActionsDefinition, 1);
94 def->name = g_strdup(name);
99 def->i_input = i_input;
100 def->i_cancel = i_cancel;
104 static void actions_definition_ref(ObActionsDefinition *def)
109 static void actions_definition_unref(ObActionsDefinition *def)
111 if (def && --def->ref == 0) {
117 ObActionsAct* actions_parse_string(const gchar *name)
120 ObActionsDefinition *def;
121 ObActionsAct *act = NULL;
123 /* find the requested action */
124 for (it = registered; it; it = g_slist_next(it)) {
126 if (!g_ascii_strcasecmp(name, def->name))
131 /* if we found the action */
133 act = g_new(ObActionsAct, 1);
136 actions_definition_ref(act->def);
139 g_message(_("Invalid action '%s' requested. No such action exists."),
145 ObActionsAct* actions_parse(ObParseInst *i,
150 ObActionsAct *act = NULL;
152 if (parse_attr_string("name", node, &name)) {
153 if ((act = actions_parse_string(name)))
154 /* there is more stuff to parse here */
155 act->options = act->def->setup(i, doc, node->children);
163 gboolean actions_act_is_interactive(ObActionsAct *act)
165 return act->def->i_cancel != NULL;
168 void actions_act_ref(ObActionsAct *act)
173 void actions_act_unref(ObActionsAct *act)
175 if (act && --act->ref == 0) {
176 /* free the action specific options */
177 act->def->free(act->options);
178 /* unref the definition */
179 actions_definition_unref(act->def);
184 static void actions_setup_data(ObActionsData *data,
191 data->any.uact = uact;
192 data->any.time = time;
193 data->any.state = state;
198 void actions_run_acts(GSList *acts,
205 struct _ObClient *client)
209 /* Don't allow saving the initial state when running things from the
211 if (uact == OB_USER_ACTION_MENU_SELECTION)
213 /* If x and y are < 0 then use the current pointer position */
215 screen_pointer_pos(&x, &y);
217 for (it = acts; it; it = g_slist_next(it)) {
219 ObActionsAct *act = it->data;
222 data.type = act->def->type;
223 actions_setup_data(&data, uact, time, state, x, y);
225 case OB_ACTION_TYPE_GLOBAL:
227 case OB_ACTION_TYPE_CLIENT:
228 data.client.context = con;
229 data.client.c = client;
232 g_assert_not_reached();
235 if (actions_act_is_interactive(act) &&
236 (!interactive_act || interactive_act->def != act->def))
238 ok = actions_interactive_begin_act(act, state);
241 /* fire the action's run function with this data */
243 if (!act->def->run(&data, act->options))
244 actions_interactive_end_act();
246 break; /* no actions are run after the interactive one */
251 gboolean actions_interactive_act_running()
253 return interactive_act != NULL;
256 void actions_interactive_cancel_act()
258 if (interactive_act) {
259 interactive_act->def->i_cancel(interactive_act->options);
260 actions_interactive_end_act();
264 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
266 /* cancel the old one */
268 actions_interactive_cancel_act();
270 if (grab_keyboard()) {
271 interactive_act = act;
272 actions_act_ref(interactive_act);
274 interactive_initial_state = state;
281 static void actions_interactive_end_act()
283 if (interactive_act) {
286 actions_act_unref(interactive_act);
287 interactive_act = NULL;
291 gboolean actions_interactive_input_event(XEvent *e)
293 gboolean used = FALSE;
294 if (interactive_act) {
295 if (!interactive_act->def->i_input(interactive_initial_state, e,
296 interactive_act->options, &used))
298 used = TRUE; /* if it cancelled the action then it has to of
300 actions_interactive_end_act();