1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 actions.c 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.
30 #include "actions/all.h"
32 static void actions_definition_ref(ObActionsDefinition *def);
33 static void actions_definition_unref(ObActionsDefinition *def);
34 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
35 static void actions_interactive_end_act();
36 static ObActionsAct* actions_build_act_from_string(const gchar *name);
38 static ObActionsAct *interactive_act = NULL;
39 static guint interactive_initial_state = 0;
41 struct _ObActionsDefinition {
46 gboolean canbeinteractive;
48 ObActionsIDataSetupFunc i;
49 ObActionsDataSetupFunc n;
51 ObActionsDataFreeFunc free;
53 ObActionsShutdownFunc shutdown;
56 struct _ObActionsAct {
59 ObActionsDefinition *def;
60 ObActionsIPreFunc i_pre;
61 ObActionsIInputFunc i_input;
62 ObActionsICancelFunc i_cancel;
63 ObActionsIPostFunc i_post;
67 static GSList *registered = NULL;
69 void actions_startup(gboolean reconfig)
76 void actions_shutdown(gboolean reconfig)
78 actions_interactive_cancel_act();
82 /* free all the registered actions */
84 ObActionsDefinition *d = registered->data;
85 if (d->shutdown) d->shutdown();
86 actions_definition_unref(d);
87 registered = g_slist_delete_link(registered, registered);
91 ObActionsDefinition* do_register(const gchar *name,
92 ObActionsDataFreeFunc free,
96 ObActionsDefinition *def;
98 g_assert(run != NULL);
100 for (it = registered; it; it = g_slist_next(it)) {
102 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
106 def = g_slice_new(ObActionsDefinition);
108 def->name = g_strdup(name);
111 def->shutdown = NULL;
113 registered = g_slist_prepend(registered, def);
117 gboolean actions_register_i(const gchar *name,
118 ObActionsIDataSetupFunc setup,
119 ObActionsDataFreeFunc free,
120 ObActionsRunFunc run)
122 ObActionsDefinition *def = do_register(name, free, run);
124 def->canbeinteractive = TRUE;
125 def->setup.i = setup;
130 gboolean actions_register(const gchar *name,
131 ObActionsDataSetupFunc setup,
132 ObActionsDataFreeFunc free,
133 ObActionsRunFunc run)
135 ObActionsDefinition *def = do_register(name, free, run);
137 def->canbeinteractive = FALSE;
138 def->setup.n = setup;
143 gboolean actions_set_shutdown(const gchar *name,
144 ObActionsShutdownFunc shutdown)
147 ObActionsDefinition *def;
149 for (it = registered; it; it = g_slist_next(it)) {
151 if (!g_ascii_strcasecmp(name, def->name)) {
152 def->shutdown = shutdown;
159 static void actions_definition_ref(ObActionsDefinition *def)
164 static void actions_definition_unref(ObActionsDefinition *def)
166 if (def && --def->ref == 0) {
168 g_slice_free(ObActionsDefinition, def);
172 static ObActionsAct* actions_build_act_from_string(const gchar *name)
175 ObActionsDefinition *def = NULL;
176 ObActionsAct *act = NULL;
178 /* find the requested action */
179 for (it = registered; it; it = g_slist_next(it)) {
181 if (!g_ascii_strcasecmp(name, def->name))
186 /* if we found the action */
188 act = g_slice_new(ObActionsAct);
191 actions_definition_ref(act->def);
194 act->i_cancel = NULL;
198 g_message(_("Invalid action \"%s\" requested. No such action exists."),
204 ObActionsAct* actions_parse_string(const gchar *name)
206 ObActionsAct *act = NULL;
208 if ((act = actions_build_act_from_string(name))) {
209 if (act->def->canbeinteractive) {
210 if (act->def->setup.i)
211 act->options = act->def->setup.i(NULL,
218 if (act->def->setup.n)
219 act->options = act->def->setup.n(NULL);
227 ObActionsAct* actions_parse(xmlNodePtr node)
230 ObActionsAct *act = NULL;
232 if (obt_xml_attr_string(node, "name", &name)) {
233 if ((act = actions_build_act_from_string(name))) {
234 /* there is more stuff to parse here */
235 if (act->def->canbeinteractive) {
236 if (act->def->setup.i)
237 act->options = act->def->setup.i(node->children,
244 if (act->def->setup.n)
245 act->options = act->def->setup.n(node->children);
254 gboolean actions_act_is_interactive(ObActionsAct *act)
256 return act->i_input != NULL;
259 void actions_act_ref(ObActionsAct *act)
264 void actions_act_unref(ObActionsAct *act)
266 if (act && --act->ref == 0) {
267 /* free the action specific options */
269 act->def->free(act->options);
270 /* unref the definition */
271 actions_definition_unref(act->def);
272 g_slice_free(ObActionsAct, act);
276 static void actions_setup_data(ObActionsData *data,
283 struct _ObClient *client)
289 data->button = button;
291 data->client = client;
294 void actions_run_acts(GSList *acts,
301 struct _ObClient *client)
304 gboolean update_user_time;
306 /* Don't allow saving the initial state when running things from the
308 if (uact == OB_USER_ACTION_MENU_SELECTION)
310 /* If x and y are < 0 then use the current pointer position */
312 screen_pointer_pos(&x, &y);
314 update_user_time = FALSE;
315 for (it = acts; it; it = g_slist_next(it)) {
317 ObActionsAct *act = it->data;
320 actions_setup_data(&data, uact, state, x, y, button, con, client);
322 /* if they have the same run function, then we'll assume they are
323 cooperating and not cancel eachother out */
324 if (!interactive_act || interactive_act->def->run != act->def->run) {
325 if (actions_act_is_interactive(act)) {
326 /* cancel the old one */
328 actions_interactive_cancel_act();
330 if (!act->i_pre(state, act->options))
331 act->i_input = NULL; /* remove the interactivity */
333 /* check again cuz it might have been cancelled */
334 if (actions_act_is_interactive(act))
335 ok = actions_interactive_begin_act(act, state);
338 /* fire the action's run function with this data */
340 if (!act->def->run(&data, act->options)) {
341 if (actions_act_is_interactive(act))
342 actions_interactive_end_act();
343 if (client && client == focus_client)
344 update_user_time = TRUE;
346 /* make sure its interactive if it returned TRUE */
347 g_assert(act->i_input);
349 /* no actions are run after the interactive one */
354 if (update_user_time)
355 event_update_user_time();
358 gboolean actions_interactive_act_running(void)
360 return interactive_act != NULL;
363 void actions_interactive_cancel_act(void)
365 if (interactive_act) {
366 if (interactive_act->i_cancel)
367 interactive_act->i_cancel(interactive_act->options);
368 actions_interactive_end_act();
372 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
374 if (grab_keyboard()) {
375 interactive_act = act;
376 actions_act_ref(interactive_act);
378 interactive_initial_state = obt_keyboard_only_modmasks(state);
380 /* if using focus_delay, stop the timer now so that focus doesn't go
381 moving on us, which would kill the action */
382 event_halt_focus_delay();
390 static void actions_interactive_end_act(void)
392 if (interactive_act) {
393 ObActionsAct *ia = interactive_act;
395 /* set this to NULL first so the i_post() function can't cause this to
396 get called again (if it decides it wants to cancel any ongoing
397 interactive action). */
398 interactive_act = NULL;
403 ia->i_post(ia->options);
405 actions_act_unref(ia);
409 gboolean actions_interactive_input_event(XEvent *e)
411 gboolean used = FALSE;
412 if (interactive_act) {
413 if (!interactive_act->i_input(interactive_initial_state, e,
414 grab_input_context(),
415 interactive_act->options, &used))
417 used = TRUE; /* if it cancelled the action then it has to of
419 actions_interactive_end_act();
425 void actions_client_move(ObActionsData *data, gboolean start)
427 static gulong ignore_start = 0;
429 ignore_start = event_start_ignore_all_enters();
430 else if (config_focus_follow &&
431 data->context != OB_FRAME_CONTEXT_CLIENT)
433 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
436 /* usually this is sorta redundant, but with a press action
437 that moves windows our from under the cursor, the enter
438 event will come as a GrabNotify which is ignored, so this
439 makes a fake enter event
441 don't do this if there is a grab on the pointer. enter events
442 are ignored during a grab, so don't force fake ones when they
445 if (!grab_on_pointer()) {
446 if ((c = client_under_pointer()) && c != data->client) {
447 ob_debug_type(OB_DEBUG_FOCUS,
448 "Generating fake enter because we did a "
449 "mouse-event action");
450 event_enter_client(c);
452 else if (!c && c != data->client) {
453 ob_debug_type(OB_DEBUG_FOCUS,
454 "Generating fake leave because we did a "
455 "mouse-event action");
456 event_enter_client(data->client);
460 else if (!data->button && !config_focus_under_mouse)
461 event_end_ignore_all_enters(ignore_start);