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_new(ObActionsDefinition, 1);
107 def->name = g_strdup(name);
111 registered = g_slist_prepend(registered, def);
115 gboolean actions_register_i(const gchar *name,
116 ObActionsIDataSetupFunc setup,
117 ObActionsDataFreeFunc free,
118 ObActionsRunFunc run)
120 ObActionsDefinition *def = do_register(name, free, run);
122 def->canbeinteractive = TRUE;
123 def->setup.i = setup;
128 gboolean actions_register(const gchar *name,
129 ObActionsDataSetupFunc setup,
130 ObActionsDataFreeFunc free,
131 ObActionsRunFunc run)
133 ObActionsDefinition *def = do_register(name, free, run);
135 def->canbeinteractive = FALSE;
136 def->setup.n = setup;
141 gboolean actions_set_shutdown(const gchar *name,
142 ObActionsShutdownFunc shutdown)
145 ObActionsDefinition *def;
147 for (it = registered; it; it = g_slist_next(it)) {
149 if (!g_ascii_strcasecmp(name, def->name)) {
150 def->shutdown = shutdown;
157 static void actions_definition_ref(ObActionsDefinition *def)
162 static void actions_definition_unref(ObActionsDefinition *def)
164 if (def && --def->ref == 0) {
170 static ObActionsAct* actions_build_act_from_string(const gchar *name)
173 ObActionsDefinition *def = NULL;
174 ObActionsAct *act = NULL;
176 /* find the requested action */
177 for (it = registered; it; it = g_slist_next(it)) {
179 if (!g_ascii_strcasecmp(name, def->name))
184 /* if we found the action */
186 act = g_new(ObActionsAct, 1);
189 actions_definition_ref(act->def);
192 act->i_cancel = NULL;
196 g_message(_("Invalid action \"%s\" requested. No such action exists."),
202 ObActionsAct* actions_parse_string(const gchar *name)
204 ObActionsAct *act = NULL;
206 if ((act = actions_build_act_from_string(name))) {
207 if (act->def->canbeinteractive) {
208 if (act->def->setup.i)
209 act->options = act->def->setup.i(NULL,
216 if (act->def->setup.n)
217 act->options = act->def->setup.n(NULL);
225 ObActionsAct* actions_parse(xmlNodePtr node)
228 ObActionsAct *act = NULL;
230 if (obt_xml_attr_string(node, "name", &name)) {
231 if ((act = actions_build_act_from_string(name))) {
232 /* there is more stuff to parse here */
233 if (act->def->canbeinteractive) {
234 if (act->def->setup.i)
235 act->options = act->def->setup.i(node->children,
242 if (act->def->setup.n)
243 act->options = act->def->setup.n(node->children);
252 gboolean actions_act_is_interactive(ObActionsAct *act)
254 return act->i_input != NULL;
257 void actions_act_ref(ObActionsAct *act)
262 void actions_act_unref(ObActionsAct *act)
264 if (act && --act->ref == 0) {
265 /* free the action specific options */
267 act->def->free(act->options);
268 /* unref the definition */
269 actions_definition_unref(act->def);
274 static void actions_setup_data(ObActionsData *data,
281 struct _ObClient *client)
287 data->button = button;
289 data->client = client;
292 void actions_run_acts(GSList *acts,
299 struct _ObClient *client)
303 /* Don't allow saving the initial state when running things from the
305 if (uact == OB_USER_ACTION_MENU_SELECTION)
307 /* If x and y are < 0 then use the current pointer position */
309 screen_pointer_pos(&x, &y);
311 for (it = acts; it; it = g_slist_next(it)) {
313 ObActionsAct *act = it->data;
316 actions_setup_data(&data, uact, state, x, y, button, con, client);
318 /* if they have the same run function, then we'll assume they are
319 cooperating and not cancel eachother out */
320 if (!interactive_act || interactive_act->def->run != act->def->run) {
321 if (actions_act_is_interactive(act)) {
322 /* cancel the old one */
324 actions_interactive_cancel_act();
326 if (!act->i_pre(state, act->options))
327 act->i_input = NULL; /* remove the interactivity */
329 /* check again cuz it might have been cancelled */
330 if (actions_act_is_interactive(act))
331 ok = actions_interactive_begin_act(act, state);
334 /* fire the action's run function with this data */
336 if (!act->def->run(&data, act->options)) {
337 if (actions_act_is_interactive(act))
338 actions_interactive_end_act();
340 /* make sure its interactive if it returned TRUE */
341 g_assert(act->i_input);
343 /* no actions are run after the interactive one */
350 gboolean actions_interactive_act_running(void)
352 return interactive_act != NULL;
355 void actions_interactive_cancel_act(void)
357 if (interactive_act) {
358 if (interactive_act->i_cancel)
359 interactive_act->i_cancel(interactive_act->options);
360 actions_interactive_end_act();
364 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
366 if (grab_keyboard()) {
367 interactive_act = act;
368 actions_act_ref(interactive_act);
370 interactive_initial_state = state;
372 /* if using focus_delay, stop the timer now so that focus doesn't go
373 moving on us, which would kill the action */
374 event_halt_focus_delay();
382 static void actions_interactive_end_act(void)
384 if (interactive_act) {
387 if (interactive_act->i_post)
388 interactive_act->i_post(interactive_act->options);
390 actions_act_unref(interactive_act);
391 interactive_act = NULL;
395 gboolean actions_interactive_input_event(XEvent *e)
397 gboolean used = FALSE;
398 if (interactive_act) {
399 if (!interactive_act->i_input(interactive_initial_state, e,
400 interactive_act->options, &used))
402 used = TRUE; /* if it cancelled the action then it has to of
404 actions_interactive_end_act();
410 void actions_client_move(ObActionsData *data, gboolean start)
412 static gulong ignore_start = 0;
414 ignore_start = event_start_ignore_all_enters();
415 else if (config_focus_follow &&
416 data->context != OB_FRAME_CONTEXT_CLIENT)
418 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
421 /* usually this is sorta redundant, but with a press action
422 that moves windows our from under the cursor, the enter
423 event will come as a GrabNotify which is ignored, so this
424 makes a fake enter event
426 don't do this if there is a grab on the pointer. enter events
427 are ignored during a grab, so don't force fake ones when they
430 if (!grab_on_pointer()) {
431 if ((c = client_under_pointer()) && c != data->client) {
432 ob_debug_type(OB_DEBUG_FOCUS,
433 "Generating fake enter because we did a "
434 "mouse-event action");
435 event_enter_client(c);
437 else if (!c && c != data->client) {
438 ob_debug_type(OB_DEBUG_FOCUS,
439 "Generating fake leave because we did a "
440 "mouse-event action");
441 event_enter_client(data->client);
445 else if (!data->button && !config_focus_under_mouse)
446 event_end_ignore_all_enters(ignore_start);