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;
54 gboolean modifies_focused_window;
58 struct _ObActionsAct {
61 ObActionsDefinition *def;
62 ObActionsIPreFunc i_pre;
63 ObActionsIInputFunc i_input;
64 ObActionsICancelFunc i_cancel;
65 ObActionsIPostFunc i_post;
69 static GSList *registered = NULL;
71 void actions_startup(gboolean reconfig)
78 void actions_shutdown(gboolean reconfig)
80 actions_interactive_cancel_act();
84 /* free all the registered actions */
86 ObActionsDefinition *d = registered->data;
87 if (d->shutdown) d->shutdown();
88 actions_definition_unref(d);
89 registered = g_slist_delete_link(registered, registered);
93 ObActionsDefinition* do_register(const gchar *name,
94 ObActionsDataFreeFunc free,
98 ObActionsDefinition *def;
100 g_assert(run != NULL);
102 for (it = registered; it; it = g_slist_next(it)) {
104 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
108 def = g_slice_new0(ObActionsDefinition);
110 def->name = g_strdup(name);
113 def->shutdown = NULL;
114 def->modifies_focused_window = TRUE;
115 def->can_stop = FALSE;
117 registered = g_slist_prepend(registered, def);
121 gboolean actions_register_i(const gchar *name,
122 ObActionsIDataSetupFunc setup,
123 ObActionsDataFreeFunc free,
124 ObActionsRunFunc run)
126 ObActionsDefinition *def = do_register(name, free, run);
128 def->canbeinteractive = TRUE;
129 def->setup.i = setup;
134 gboolean actions_register(const gchar *name,
135 ObActionsDataSetupFunc setup,
136 ObActionsDataFreeFunc free,
137 ObActionsRunFunc run)
139 ObActionsDefinition *def = do_register(name, free, run);
141 def->canbeinteractive = FALSE;
142 def->setup.n = setup;
147 gboolean actions_set_shutdown(const gchar *name,
148 ObActionsShutdownFunc shutdown)
151 ObActionsDefinition *def;
153 for (it = registered; it; it = g_slist_next(it)) {
155 if (!g_ascii_strcasecmp(name, def->name)) {
156 def->shutdown = shutdown;
163 gboolean actions_set_modifies_focused_window(const gchar *name,
167 ObActionsDefinition *def;
169 for (it = registered; it; it = g_slist_next(it)) {
171 if (!g_ascii_strcasecmp(name, def->name)) {
172 def->modifies_focused_window = modifies;
179 gboolean actions_set_can_stop(const gchar *name,
183 ObActionsDefinition *def;
185 for (it = registered; it; it = g_slist_next(it)) {
187 if (!g_ascii_strcasecmp(name, def->name)) {
188 def->can_stop = can_stop;
195 static void actions_definition_ref(ObActionsDefinition *def)
200 static void actions_definition_unref(ObActionsDefinition *def)
202 if (def && --def->ref == 0) {
204 g_slice_free(ObActionsDefinition, def);
208 static ObActionsAct* actions_build_act_from_string(const gchar *name)
211 ObActionsDefinition *def = NULL;
212 ObActionsAct *act = NULL;
214 /* find the requested action */
215 for (it = registered; it; it = g_slist_next(it)) {
217 if (!g_ascii_strcasecmp(name, def->name))
222 /* if we found the action */
224 act = g_slice_new(ObActionsAct);
227 actions_definition_ref(act->def);
230 act->i_cancel = NULL;
234 g_message(_("Invalid action \"%s\" requested. No such action exists."),
240 ObActionsAct* actions_parse_string(const gchar *name)
242 ObActionsAct *act = NULL;
244 if ((act = actions_build_act_from_string(name))) {
245 if (act->def->canbeinteractive) {
246 if (act->def->setup.i)
247 act->options = act->def->setup.i(NULL,
254 if (act->def->setup.n)
255 act->options = act->def->setup.n(NULL);
263 ObActionsAct* actions_parse(xmlNodePtr node)
266 ObActionsAct *act = NULL;
268 if (obt_xml_attr_string(node, "name", &name)) {
269 if ((act = actions_build_act_from_string(name))) {
270 /* there is more stuff to parse here */
271 if (act->def->canbeinteractive) {
272 if (act->def->setup.i)
273 act->options = act->def->setup.i(node->children,
280 if (act->def->setup.n)
281 act->options = act->def->setup.n(node->children);
290 gboolean actions_act_is_interactive(ObActionsAct *act)
292 return act->i_input != NULL;
295 void actions_act_ref(ObActionsAct *act)
300 void actions_act_unref(ObActionsAct *act)
302 if (act && --act->ref == 0) {
303 /* free the action specific options */
305 act->def->free(act->options);
306 /* unref the definition */
307 actions_definition_unref(act->def);
308 g_slice_free(ObActionsAct, act);
312 static void actions_setup_data(ObActionsData *data,
319 struct _ObClient *client)
325 data->button = button;
327 data->client = client;
330 void actions_run_acts(GSList *acts,
337 struct _ObClient *client)
340 gboolean update_user_time;
342 /* Don't allow saving the initial state when running things from the
344 if (uact == OB_USER_ACTION_MENU_SELECTION)
346 /* If x and y are < 0 then use the current pointer position */
348 screen_pointer_pos(&x, &y);
350 update_user_time = FALSE;
351 for (it = acts; it; it = g_slist_next(it)) {
353 ObActionsAct *act = it->data;
356 actions_setup_data(&data, uact, state, x, y, button, con, client);
358 /* if they have the same run function, then we'll assume they are
359 cooperating and not cancel eachother out */
360 if (!interactive_act || interactive_act->def->run != act->def->run) {
361 if (actions_act_is_interactive(act)) {
362 /* cancel the old one */
364 actions_interactive_cancel_act();
366 if (!act->i_pre(state, act->options))
367 act->i_input = NULL; /* remove the interactivity */
369 /* check again cuz it might have been cancelled */
370 if (actions_act_is_interactive(act))
371 ok = actions_interactive_begin_act(act, state);
374 /* fire the action's run function with this data */
376 if (!act->def->run(&data, act->options)) {
377 if (actions_act_is_interactive(act)) {
378 actions_interactive_end_act();
380 if (client && client == focus_client &&
381 act->def->modifies_focused_window)
383 update_user_time = TRUE;
386 /* make sure its interactive or allowed to stop
387 if it returned TRUE */
388 g_assert(act->i_input || act->def->can_stop);
390 /* no actions are run after the interactive one */
395 if (update_user_time)
396 event_update_user_time();
399 gboolean actions_interactive_act_running(void)
401 return interactive_act != NULL;
404 void actions_interactive_cancel_act(void)
406 if (interactive_act) {
407 if (interactive_act->i_cancel)
408 interactive_act->i_cancel(interactive_act->options);
409 actions_interactive_end_act();
413 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
415 if (grab_keyboard()) {
416 interactive_act = act;
417 actions_act_ref(interactive_act);
419 interactive_initial_state = state;
421 /* if using focus_delay, stop the timer now so that focus doesn't go
422 moving on us, which would kill the action */
423 event_halt_focus_delay();
431 static void actions_interactive_end_act(void)
433 if (interactive_act) {
434 ObActionsAct *ia = interactive_act;
436 /* set this to NULL first so the i_post() function can't cause this to
437 get called again (if it decides it wants to cancel any ongoing
438 interactive action). */
439 interactive_act = NULL;
444 ia->i_post(ia->options);
446 actions_act_unref(ia);
450 gboolean actions_interactive_input_event(XEvent *e)
452 gboolean used = FALSE;
453 if (interactive_act) {
454 if (!interactive_act->i_input(interactive_initial_state, e,
455 grab_input_context(),
456 interactive_act->options, &used))
458 used = TRUE; /* if it cancelled the action then it has to of
460 actions_interactive_end_act();
466 void actions_client_move(ObActionsData *data, gboolean start)
468 static gulong ignore_start = 0;
470 ignore_start = event_start_ignore_all_enters();
471 else if (config_focus_follow &&
472 data->context != OB_FRAME_CONTEXT_CLIENT)
474 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
477 /* usually this is sorta redundant, but with a press action
478 that moves windows our from under the cursor, the enter
479 event will come as a GrabNotify which is ignored, so this
480 makes a fake enter event
482 don't do this if there is a grab on the pointer. enter events
483 are ignored during a grab, so don't force fake ones when they
486 if (!grab_on_pointer()) {
487 if ((c = client_under_pointer()) && c != data->client) {
488 ob_debug_type(OB_DEBUG_FOCUS,
489 "Generating fake enter because we did a "
490 "mouse-event action");
491 event_enter_client(c);
493 else if (!c && c != data->client) {
494 ob_debug_type(OB_DEBUG_FOCUS,
495 "Generating fake leave because we did a "
496 "mouse-event action");
497 event_leave_client(data->client);
501 else if (!data->button && !config_focus_under_mouse)
502 event_end_ignore_all_enters(ignore_start);