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;
57 struct _ObActionsAct {
60 ObActionsDefinition *def;
61 ObActionsIPreFunc i_pre;
62 ObActionsIInputFunc i_input;
63 ObActionsICancelFunc i_cancel;
64 ObActionsIPostFunc i_post;
68 static GSList *registered = NULL;
70 void actions_startup(gboolean reconfig)
77 void actions_shutdown(gboolean reconfig)
79 actions_interactive_cancel_act();
83 /* free all the registered actions */
85 ObActionsDefinition *d = registered->data;
86 if (d->shutdown) d->shutdown();
87 actions_definition_unref(d);
88 registered = g_slist_delete_link(registered, registered);
92 ObActionsDefinition* do_register(const gchar *name,
93 ObActionsDataFreeFunc free,
97 ObActionsDefinition *def;
99 g_assert(run != NULL);
101 for (it = registered; it; it = g_slist_next(it)) {
103 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
107 def = g_slice_new0(ObActionsDefinition);
109 def->name = g_strdup(name);
112 def->shutdown = NULL;
113 def->modifies_focused_window = TRUE;
115 registered = g_slist_prepend(registered, def);
119 gboolean actions_register_i(const gchar *name,
120 ObActionsIDataSetupFunc setup,
121 ObActionsDataFreeFunc free,
122 ObActionsRunFunc run)
124 ObActionsDefinition *def = do_register(name, free, run);
126 def->canbeinteractive = TRUE;
127 def->setup.i = setup;
132 gboolean actions_register(const gchar *name,
133 ObActionsDataSetupFunc setup,
134 ObActionsDataFreeFunc free,
135 ObActionsRunFunc run)
137 ObActionsDefinition *def = do_register(name, free, run);
139 def->canbeinteractive = FALSE;
140 def->setup.n = setup;
145 gboolean actions_set_shutdown(const gchar *name,
146 ObActionsShutdownFunc shutdown)
149 ObActionsDefinition *def;
151 for (it = registered; it; it = g_slist_next(it)) {
153 if (!g_ascii_strcasecmp(name, def->name)) {
154 def->shutdown = shutdown;
161 gboolean actions_set_modifies_focused_window(const gchar *name,
165 ObActionsDefinition *def;
167 for (it = registered; it; it = g_slist_next(it)) {
169 if (!g_ascii_strcasecmp(name, def->name)) {
170 def->modifies_focused_window = modifies;
177 static void actions_definition_ref(ObActionsDefinition *def)
182 static void actions_definition_unref(ObActionsDefinition *def)
184 if (def && --def->ref == 0) {
186 g_slice_free(ObActionsDefinition, def);
190 static ObActionsAct* actions_build_act_from_string(const gchar *name)
193 ObActionsDefinition *def = NULL;
194 ObActionsAct *act = NULL;
196 /* find the requested action */
197 for (it = registered; it; it = g_slist_next(it)) {
199 if (!g_ascii_strcasecmp(name, def->name))
204 /* if we found the action */
206 act = g_slice_new(ObActionsAct);
209 actions_definition_ref(act->def);
212 act->i_cancel = NULL;
216 g_message(_("Invalid action \"%s\" requested. No such action exists."),
222 ObActionsAct* actions_parse_string(const gchar *name)
224 ObActionsAct *act = NULL;
226 if ((act = actions_build_act_from_string(name))) {
227 if (act->def->canbeinteractive) {
228 if (act->def->setup.i)
229 act->options = act->def->setup.i(NULL,
236 if (act->def->setup.n)
237 act->options = act->def->setup.n(NULL);
245 ObActionsAct* actions_parse(xmlNodePtr node)
248 ObActionsAct *act = NULL;
250 if (obt_xml_attr_string(node, "name", &name)) {
251 if ((act = actions_build_act_from_string(name))) {
252 /* there is more stuff to parse here */
253 if (act->def->canbeinteractive) {
254 if (act->def->setup.i)
255 act->options = act->def->setup.i(node->children,
262 if (act->def->setup.n)
263 act->options = act->def->setup.n(node->children);
272 gboolean actions_act_is_interactive(ObActionsAct *act)
274 return act->i_input != NULL;
277 void actions_act_ref(ObActionsAct *act)
282 void actions_act_unref(ObActionsAct *act)
284 if (act && --act->ref == 0) {
285 /* free the action specific options */
287 act->def->free(act->options);
288 /* unref the definition */
289 actions_definition_unref(act->def);
290 g_slice_free(ObActionsAct, act);
294 static void actions_setup_data(ObActionsData *data,
301 struct _ObClient *client)
307 data->button = button;
309 data->client = client;
312 void actions_run_acts(GSList *acts,
319 struct _ObClient *client)
322 gboolean update_user_time;
324 /* Don't allow saving the initial state when running things from the
326 if (uact == OB_USER_ACTION_MENU_SELECTION)
328 /* If x and y are < 0 then use the current pointer position */
330 screen_pointer_pos(&x, &y);
332 update_user_time = FALSE;
333 for (it = acts; it; it = g_slist_next(it)) {
335 ObActionsAct *act = it->data;
338 actions_setup_data(&data, uact, state, x, y, button, con, client);
340 /* if they have the same run function, then we'll assume they are
341 cooperating and not cancel eachother out */
342 if (!interactive_act || interactive_act->def->run != act->def->run) {
343 if (actions_act_is_interactive(act)) {
344 /* cancel the old one */
346 actions_interactive_cancel_act();
348 if (!act->i_pre(state, act->options))
349 act->i_input = NULL; /* remove the interactivity */
351 /* check again cuz it might have been cancelled */
352 if (actions_act_is_interactive(act))
353 ok = actions_interactive_begin_act(act, state);
356 /* fire the action's run function with this data */
358 if (!act->def->run(&data, act->options)) {
359 if (actions_act_is_interactive(act))
360 actions_interactive_end_act();
361 if (client && client == focus_client &&
362 act->def->modifies_focused_window)
364 update_user_time = TRUE;
367 /* make sure its interactive if it returned TRUE */
368 g_assert(act->i_input);
370 /* no actions are run after the interactive one */
375 if (update_user_time)
376 event_update_user_time();
379 gboolean actions_interactive_act_running(void)
381 return interactive_act != NULL;
384 void actions_interactive_cancel_act(void)
386 if (interactive_act) {
387 if (interactive_act->i_cancel)
388 interactive_act->i_cancel(interactive_act->options);
389 actions_interactive_end_act();
393 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
395 if (grab_keyboard()) {
396 interactive_act = act;
397 actions_act_ref(interactive_act);
399 interactive_initial_state = state;
401 /* if using focus_delay, stop the timer now so that focus doesn't go
402 moving on us, which would kill the action */
403 event_halt_focus_delay();
411 static void actions_interactive_end_act(void)
413 if (interactive_act) {
414 ObActionsAct *ia = interactive_act;
416 /* set this to NULL first so the i_post() function can't cause this to
417 get called again (if it decides it wants to cancel any ongoing
418 interactive action). */
419 interactive_act = NULL;
424 ia->i_post(ia->options);
426 actions_act_unref(ia);
430 gboolean actions_interactive_input_event(XEvent *e)
432 gboolean used = FALSE;
433 if (interactive_act) {
434 if (!interactive_act->i_input(interactive_initial_state, e,
435 grab_input_context(),
436 interactive_act->options, &used))
438 used = TRUE; /* if it cancelled the action then it has to of
440 actions_interactive_end_act();
446 void actions_client_move(ObActionsData *data, gboolean start)
448 static gulong ignore_start = 0;
450 ignore_start = event_start_ignore_all_enters();
451 else if (config_focus_follow &&
452 data->context != OB_FRAME_CONTEXT_CLIENT)
454 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
457 /* usually this is sorta redundant, but with a press action
458 that moves windows our from under the cursor, the enter
459 event will come as a GrabNotify which is ignored, so this
460 makes a fake enter event
462 don't do this if there is a grab on the pointer. enter events
463 are ignored during a grab, so don't force fake ones when they
466 if (!grab_on_pointer()) {
467 if ((c = client_under_pointer()) && c != data->client) {
468 ob_debug_type(OB_DEBUG_FOCUS,
469 "Generating fake enter because we did a "
470 "mouse-event action");
471 event_enter_client(c);
473 else if (!c && c != data->client) {
474 ob_debug_type(OB_DEBUG_FOCUS,
475 "Generating fake leave because we did a "
476 "mouse-event action");
477 event_leave_client(data->client);
481 else if (!data->button && !config_focus_under_mouse)
482 event_end_ignore_all_enters(ignore_start);