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.
23 static void actions_definition_ref(ObActionsDefinition *def);
24 static void actions_definition_unref(ObActionsDefinition *def);
25 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
26 static void actions_interactive_end_act();
28 static ObActionsAct *interactive_act = NULL;
29 static guint interactive_initial_state = 0;
31 struct _ObActionsDefinition {
37 ObActionsDataSetupFunc setup;
38 ObActionsDataFreeFunc free;
40 ObActionsInteractiveInputFunc i_input;
41 ObActionsInteractiveCancelFunc i_cancel;
44 struct _ObActionsAct {
47 ObActionsDefinition *def;
51 static GSList *registered = NULL;
54 void actions_startup(gboolean reconfig)
61 void actions_shutdown(gboolean reconfig)
65 /* free all the registered actions */
67 actions_definition_unref(registered->data);
68 registered = g_slist_delete_link(registered, registered);
72 gboolean actions_register(const gchar *name,
74 ObActionsDataSetupFunc setup,
75 ObActionsDataFreeFunc free,
77 ObActionsInteractiveInputFunc i_input,
78 ObActionsInteractiveCancelFunc i_cancel)
81 ObActionsDefinition *def;
83 for (it = registered; it; it = g_slist_next(it)) {
85 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
89 g_assert((i_input == NULL) == (i_cancel == NULL));
91 def = g_new(ObActionsDefinition, 1);
93 def->name = g_strdup(name);
98 def->i_input = i_input;
99 def->i_cancel = i_cancel;
103 static void actions_definition_ref(ObActionsDefinition *def)
108 static void actions_definition_unref(ObActionsDefinition *def)
110 if (def && --def->ref == 0) {
116 ObActionsAct* actions_parse_string(const gchar *name)
119 ObActionsDefinition *def;
120 ObActionsAct *act = NULL;
122 /* find the requested action */
123 for (it = registered; it; it = g_slist_next(it)) {
125 if (!g_ascii_strcasecmp(name, def->name))
130 /* if we found the action */
132 act = g_new(ObActionsAct, 1);
135 actions_definition_ref(act->def);
138 g_message(_("Invalid action '%s' requested. No such action exists."),
144 ObActionsAct* actions_parse(ObParseInst *i,
149 ObActionsAct *act = NULL;
151 if (parse_attr_string("name", node, &name)) {
152 if ((act = actions_parse_string(name)))
153 /* there is more stuff to parse here */
154 act->options = act->def->setup(i, doc, node->children);
162 gboolean actions_act_is_interactive(ObActionsAct *act)
164 return act->def->i_cancel != NULL;
167 void actions_act_ref(ObActionsAct *act)
172 void actions_act_unref(ObActionsAct *act)
174 if (act && --act->ref == 0) {
175 /* free the action specific options */
176 act->def->free(act->options);
177 /* unref the definition */
178 actions_definition_unref(act->def);
183 static void actions_setup_data(ObActionsData *data,
190 data->any.uact = uact;
191 data->any.time = time;
192 data->any.state = state;
197 void actions_run_acts(GSList *acts,
204 struct _ObClient *client)
208 for (it = acts; it; it = g_slist_next(it)) {
210 ObActionsAct *act = it->data;
213 data.type = act->def->type;
214 actions_setup_data(&data, uact, time, state, x, y);
216 case OB_ACTION_TYPE_GLOBAL:
218 case OB_ACTION_TYPE_CLIENT:
219 data.client.context = con;
220 data.client.c = client;
223 g_assert_not_reached();
226 if (actions_act_is_interactive(act) &&
227 (!interactive_act || interactive_act->def != act->def))
229 ok = actions_interactive_begin_act(act, state);
232 /* fire the action's run function with this data */
234 if (!act->def->run(&data, act->options))
235 actions_interactive_end_act();
237 break; /* no actions are run after the interactive one */
242 gboolean actions_interactive_act_running()
244 return interactive_act != NULL;
247 void actions_interactive_cancel_act()
249 if (interactive_act) {
250 interactive_act->def->i_cancel(interactive_act->options);
251 actions_interactive_end_act();
255 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
257 /* cancel the old one */
259 actions_interactive_cancel_act();
261 if (grab_keyboard()) {
262 interactive_act = act;
263 actions_act_ref(interactive_act);
265 interactive_initial_state = state;
272 static void actions_interactive_end_act()
274 if (interactive_act) {
277 actions_act_unref(interactive_act);
278 interactive_act = NULL;
282 gboolean actions_interactive_input_event(XEvent *e)
284 gboolean used = FALSE;
285 if (interactive_act) {
286 if (!interactive_act->def->i_input(interactive_initial_state, e,
287 interactive_act->options, &used))
289 used = TRUE; /* if it cancelled the action then it has to of
291 actions_interactive_end_act();