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;
52 ObActionsShutdownFunc shutdown;
55 struct _ObActionsAct {
58 ObActionsDefinition *def;
59 ObActionsIPreFunc i_pre;
60 ObActionsIInputFunc i_input;
61 ObActionsICancelFunc i_cancel;
62 ObActionsIPostFunc i_post;
66 static GSList *registered = NULL;
68 void actions_startup(gboolean reconfig)
75 void actions_shutdown(gboolean reconfig)
77 actions_interactive_cancel_act();
81 /* free all the registered actions */
83 ObActionsDefinition *d = registered->data;
84 if (d->shutdown) d->shutdown();
85 actions_definition_unref(d);
86 registered = g_slist_delete_link(registered, registered);
90 ObActionsDefinition* do_register(const gchar *name,
91 ObActionsDataFreeFunc free,
95 ObActionsDefinition *def;
97 g_assert(run != NULL);
99 for (it = registered; it; it = g_slist_next(it)) {
101 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
105 def = g_slice_new(ObActionsDefinition);
107 def->name = g_strdup(name);
110 def->shutdown = NULL;
112 registered = g_slist_prepend(registered, def);
116 gboolean actions_register_i(const gchar *name,
117 ObActionsIDataSetupFunc setup,
118 ObActionsDataFreeFunc free,
119 ObActionsRunFunc run)
121 ObActionsDefinition *def = do_register(name, free, run);
123 def->canbeinteractive = TRUE;
124 def->setup.i = setup;
129 gboolean actions_register(const gchar *name,
130 ObActionsDataSetupFunc setup,
131 ObActionsDataFreeFunc free,
132 ObActionsRunFunc run)
134 ObActionsDefinition *def = do_register(name, free, run);
136 def->canbeinteractive = FALSE;
137 def->setup.n = setup;
142 gboolean actions_set_shutdown(const gchar *name,
143 ObActionsShutdownFunc shutdown)
146 ObActionsDefinition *def;
148 for (it = registered; it; it = g_slist_next(it)) {
150 if (!g_ascii_strcasecmp(name, def->name)) {
151 def->shutdown = shutdown;
158 static void actions_definition_ref(ObActionsDefinition *def)
163 static void actions_definition_unref(ObActionsDefinition *def)
165 if (def && --def->ref == 0) {
167 g_slice_free(ObActionsDefinition, def);
171 static ObActionsAct* actions_build_act_from_string(const gchar *name)
174 ObActionsDefinition *def = NULL;
175 ObActionsAct *act = NULL;
177 /* find the requested action */
178 for (it = registered; it; it = g_slist_next(it)) {
180 if (!g_ascii_strcasecmp(name, def->name))
185 /* if we found the action */
187 act = g_slice_new(ObActionsAct);
190 actions_definition_ref(act->def);
193 act->i_cancel = NULL;
197 g_message(_("Invalid action \"%s\" requested. No such action exists."),
203 ObActionsAct* actions_parse_string(const gchar *name)
205 ObActionsAct *act = NULL;
207 if ((act = actions_build_act_from_string(name))) {
208 if (act->def->canbeinteractive) {
209 if (act->def->setup.i)
210 act->options = act->def->setup.i(NULL,
217 if (act->def->setup.n)
218 act->options = act->def->setup.n(NULL);
226 ObActionsAct* actions_parse(xmlNodePtr node)
229 ObActionsAct *act = NULL;
231 if (obt_xml_attr_string(node, "name", &name)) {
232 if ((act = actions_build_act_from_string(name))) {
233 /* there is more stuff to parse here */
234 if (act->def->canbeinteractive) {
235 if (act->def->setup.i)
236 act->options = act->def->setup.i(node->children,
243 if (act->def->setup.n)
244 act->options = act->def->setup.n(node->children);
253 gboolean actions_act_is_interactive(ObActionsAct *act)
255 return act->i_input != NULL;
258 void actions_act_ref(ObActionsAct *act)
263 void actions_act_unref(ObActionsAct *act)
265 if (act && --act->ref == 0) {
266 /* free the action specific options */
268 act->def->free(act->options);
269 /* unref the definition */
270 actions_definition_unref(act->def);
271 g_slice_free(ObActionsAct, act);
275 static void actions_setup_data(ObActionsData *data,
282 struct _ObClient *client)
288 data->button = button;
290 data->client = client;
293 void actions_run_acts(GSList *acts,
300 struct _ObClient *client)
304 /* Don't allow saving the initial state when running things from the
306 if (uact == OB_USER_ACTION_MENU_SELECTION)
308 /* If x and y are < 0 then use the current pointer position */
310 screen_pointer_pos(&x, &y);
312 for (it = acts; it; it = g_slist_next(it)) {
314 ObActionsAct *act = it->data;
317 actions_setup_data(&data, uact, state, x, y, button, con, client);
319 /* if they have the same run function, then we'll assume they are
320 cooperating and not cancel eachother out */
321 if (!interactive_act || interactive_act->def->run != act->def->run) {
322 if (actions_act_is_interactive(act)) {
323 /* cancel the old one */
325 actions_interactive_cancel_act();
327 if (!act->i_pre(state, act->options))
328 act->i_input = NULL; /* remove the interactivity */
330 /* check again cuz it might have been cancelled */
331 if (actions_act_is_interactive(act))
332 ok = actions_interactive_begin_act(act, state);
335 /* fire the action's run function with this data */
337 if (!act->def->run(&data, act->options)) {
338 if (actions_act_is_interactive(act))
339 actions_interactive_end_act();
341 /* make sure its interactive if it returned TRUE */
342 g_assert(act->i_input);
344 /* no actions are run after the interactive one */
351 gboolean actions_interactive_act_running(void)
353 return interactive_act != NULL;
356 void actions_interactive_cancel_act(void)
358 if (interactive_act) {
359 if (interactive_act->i_cancel)
360 interactive_act->i_cancel(interactive_act->options);
361 actions_interactive_end_act();
365 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
367 if (grab_keyboard()) {
368 interactive_act = act;
369 actions_act_ref(interactive_act);
371 interactive_initial_state = obt_keyboard_only_modmasks(state);
373 /* if using focus_delay, stop the timer now so that focus doesn't go
374 moving on us, which would kill the action */
375 event_halt_focus_delay();
383 static void actions_interactive_end_act(void)
385 if (interactive_act) {
388 if (interactive_act->i_post)
389 interactive_act->i_post(interactive_act->options);
391 actions_act_unref(interactive_act);
392 interactive_act = NULL;
396 gboolean actions_interactive_input_event(XEvent *e)
398 gboolean used = FALSE;
399 if (interactive_act) {
400 if (!interactive_act->i_input(interactive_initial_state, e,
401 grab_input_context(),
402 interactive_act->options, &used))
404 used = TRUE; /* if it cancelled the action then it has to of
406 actions_interactive_end_act();
412 void actions_client_move(ObActionsData *data, gboolean start)
414 static gulong ignore_start = 0;
416 ignore_start = event_start_ignore_all_enters();
417 else if (config_focus_follow &&
418 data->context != OB_FRAME_CONTEXT_CLIENT)
420 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
423 /* usually this is sorta redundant, but with a press action
424 that moves windows our from under the cursor, the enter
425 event will come as a GrabNotify which is ignored, so this
426 makes a fake enter event
428 don't do this if there is a grab on the pointer. enter events
429 are ignored during a grab, so don't force fake ones when they
432 if (!grab_on_pointer()) {
433 if ((c = client_under_pointer()) && c != data->client) {
434 ob_debug_type(OB_DEBUG_FOCUS,
435 "Generating fake enter because we did a "
436 "mouse-event action");
437 event_enter_client(c);
439 else if (!c && c != data->client) {
440 ob_debug_type(OB_DEBUG_FOCUS,
441 "Generating fake leave because we did a "
442 "mouse-event action");
443 event_enter_client(data->client);
447 else if (!data->button && !config_focus_under_mouse)
448 event_end_ignore_all_enters(ignore_start);