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 ObActionsDataSetupFunc setup;
46 ObActionsDataFreeFunc free;
48 ObActionsInteractiveInputFunc i_input;
49 ObActionsInteractiveCancelFunc i_cancel;
50 ObActionsShutdownFunc shutdown;
53 struct _ObActionsAct {
56 ObActionsDefinition *def;
60 static GSList *registered = NULL;
62 void actions_startup(gboolean reconfig)
69 void actions_shutdown(gboolean reconfig)
71 actions_interactive_cancel_act();
75 /* free all the registered actions */
77 ObActionsDefinition *d = registered->data;
78 if (d->shutdown) d->shutdown();
79 actions_definition_unref(d);
80 registered = g_slist_delete_link(registered, registered);
84 gboolean actions_register(const gchar *name,
85 ObActionsDataSetupFunc setup,
86 ObActionsDataFreeFunc free,
88 ObActionsInteractiveInputFunc i_input,
89 ObActionsInteractiveCancelFunc i_cancel)
92 ObActionsDefinition *def;
94 g_assert(run != NULL);
95 g_assert((i_input == NULL) == (i_cancel == NULL));
97 for (it = registered; it; it = g_slist_next(it)) {
99 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
103 def = g_new(ObActionsDefinition, 1);
105 def->name = g_strdup(name);
109 def->i_input = i_input;
110 def->i_cancel = i_cancel;
112 registered = g_slist_prepend(registered, def);
117 gboolean actions_set_shutdown(const gchar *name,
118 ObActionsShutdownFunc shutdown)
121 ObActionsDefinition *def;
123 for (it = registered; it; it = g_slist_next(it)) {
125 if (!g_ascii_strcasecmp(name, def->name)) {
126 def->shutdown = shutdown;
133 static void actions_definition_ref(ObActionsDefinition *def)
138 static void actions_definition_unref(ObActionsDefinition *def)
140 if (def && --def->ref == 0) {
146 static ObActionsAct* actions_build_act_from_string(const gchar *name)
149 ObActionsDefinition *def = NULL;
150 ObActionsAct *act = NULL;
152 /* find the requested action */
153 for (it = registered; it; it = g_slist_next(it)) {
155 if (!g_ascii_strcasecmp(name, def->name))
160 /* if we found the action */
162 act = g_new(ObActionsAct, 1);
165 actions_definition_ref(act->def);
168 g_message(_("Invalid action \"%s\" requested. No such action exists."),
174 ObActionsAct* actions_parse_string(const gchar *name)
176 ObActionsAct *act = NULL;
178 if ((act = actions_build_act_from_string(name)))
180 act->options = act->def->setup(NULL, NULL, NULL);
185 ObActionsAct* actions_parse(ObParseInst *i,
190 ObActionsAct *act = NULL;
192 if (parse_attr_string("name", node, &name)) {
193 if ((act = actions_build_act_from_string(name)))
194 /* there is more stuff to parse here */
196 act->options = act->def->setup(i, doc, node->xmlChildrenNode);
204 gboolean actions_act_is_interactive(ObActionsAct *act)
206 return act->def->i_cancel != NULL;
209 void actions_act_ref(ObActionsAct *act)
214 void actions_act_unref(ObActionsAct *act)
216 if (act && --act->ref == 0) {
217 /* free the action specific options */
219 act->def->free(act->options);
220 /* unref the definition */
221 actions_definition_unref(act->def);
226 static void actions_setup_data(ObActionsData *data,
233 struct _ObClient *client)
239 data->button = button;
241 data->client = client;
244 void actions_run_acts(GSList *acts,
251 struct _ObClient *client)
255 /* Don't allow saving the initial state when running things from the
257 if (uact == OB_USER_ACTION_MENU_SELECTION)
259 /* If x and y are < 0 then use the current pointer position */
261 screen_pointer_pos(&x, &y);
263 for (it = acts; it; it = g_slist_next(it)) {
265 ObActionsAct *act = it->data;
268 actions_setup_data(&data, uact, state, x, y, button, con, client);
270 /* if they have the same run function, then we'll assume they are
271 cooperating and not cancel eachother out */
272 if (!interactive_act || interactive_act->def->run != act->def->run) {
273 if (actions_act_is_interactive(act)) {
274 /* cancel the old one */
276 actions_interactive_cancel_act();
277 ok = actions_interactive_begin_act(act, state);
281 /* fire the action's run function with this data */
283 if (!act->def->run(&data, act->options)) {
284 if (actions_act_is_interactive(act))
285 actions_interactive_end_act();
287 /* make sure its interactive if it returned TRUE */
288 g_assert(act->def->i_cancel && act->def->i_input);
290 /* no actions are run after the interactive one */
297 gboolean actions_interactive_act_running(void)
299 return interactive_act != NULL;
302 void actions_interactive_cancel_act(void)
304 if (interactive_act) {
305 interactive_act->def->i_cancel(interactive_act->options);
306 actions_interactive_end_act();
310 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
312 if (grab_keyboard()) {
313 interactive_act = act;
314 actions_act_ref(interactive_act);
316 interactive_initial_state = state;
318 /* if using focus_delay, stop the timer now so that focus doesn't go
319 moving on us, which would kill the action */
320 event_halt_focus_delay();
328 static void actions_interactive_end_act(void)
330 if (interactive_act) {
333 actions_act_unref(interactive_act);
334 interactive_act = NULL;
338 gboolean actions_interactive_input_event(XEvent *e)
340 gboolean used = FALSE;
341 if (interactive_act) {
342 if (!interactive_act->def->i_input(interactive_initial_state, e,
343 interactive_act->options, &used))
345 used = TRUE; /* if it cancelled the action then it has to of
347 actions_interactive_end_act();
353 void actions_client_move(ObActionsData *data, gboolean start)
355 static gulong ignore_start = 0;
357 ignore_start = event_start_ignore_all_enters();
358 else if (config_focus_follow &&
359 data->context != OB_FRAME_CONTEXT_CLIENT)
361 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
364 /* usually this is sorta redundant, but with a press action
365 that moves windows our from under the cursor, the enter
366 event will come as a GrabNotify which is ignored, so this
367 makes a fake enter event
369 don't do this if there is a grab on the pointer. enter events
370 are ignored during a grab, so don't force fake ones when they
373 if ((c = client_under_pointer()) && c != data->client &&
376 ob_debug_type(OB_DEBUG_FOCUS,
377 "Generating fake enter because we did a "
378 "mouse-event action");
379 event_enter_client(c);
382 else if (!data->button && !config_focus_under_mouse)
383 event_end_ignore_all_enters(ignore_start);