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;
39 static gboolean replay_pointer = FALSE;
41 struct _ObActionsDefinition {
46 ObActionsDataSetupFunc setup;
47 ObActionsDataFreeFunc free;
49 ObActionsInteractiveInputFunc i_input;
50 ObActionsInteractiveCancelFunc i_cancel;
53 struct _ObActionsAct {
56 ObActionsDefinition *def;
60 static GSList *registered = NULL;
63 void actions_startup(gboolean reconfig)
70 void actions_shutdown(gboolean reconfig)
72 actions_interactive_cancel_act();
76 /* free all the registered actions */
78 actions_definition_unref(registered->data);
79 registered = g_slist_delete_link(registered, registered);
83 gboolean actions_register(const gchar *name,
84 ObActionsDataSetupFunc setup,
85 ObActionsDataFreeFunc free,
87 ObActionsInteractiveInputFunc i_input,
88 ObActionsInteractiveCancelFunc i_cancel)
91 ObActionsDefinition *def;
93 g_assert(run != NULL);
94 g_assert((i_input == NULL) == (i_cancel == NULL));
96 for (it = registered; it; it = g_slist_next(it)) {
98 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
102 def = g_new(ObActionsDefinition, 1);
104 def->name = g_strdup(name);
108 def->i_input = i_input;
109 def->i_cancel = i_cancel;
111 registered = g_slist_prepend(registered, def);
116 static void actions_definition_ref(ObActionsDefinition *def)
121 static void actions_definition_unref(ObActionsDefinition *def)
123 if (def && --def->ref == 0) {
129 static ObActionsAct* actions_build_act_from_string(const gchar *name)
132 ObActionsDefinition *def = NULL;
133 ObActionsAct *act = NULL;
135 /* find the requested action */
136 for (it = registered; it; it = g_slist_next(it)) {
138 if (!g_ascii_strcasecmp(name, def->name))
143 /* if we found the action */
145 act = g_new(ObActionsAct, 1);
148 actions_definition_ref(act->def);
151 g_message(_("Invalid action '%s' requested. No such action exists."),
157 ObActionsAct* actions_parse_string(const gchar *name)
159 ObActionsAct *act = NULL;
161 if ((act = actions_build_act_from_string(name)))
163 act->options = act->def->setup(NULL);
168 ObActionsAct* actions_parse(xmlNodePtr node)
171 ObActionsAct *act = NULL;
173 if (obt_parse_attr_string(node, "name", &name)) {
174 if ((act = actions_build_act_from_string(name)))
175 /* there is more stuff to parse here */
177 act->options = act->def->setup(node->children);
185 gboolean actions_act_is_interactive(ObActionsAct *act)
187 return act->def->i_cancel != NULL;
190 void actions_act_ref(ObActionsAct *act)
195 void actions_act_unref(ObActionsAct *act)
197 if (act && --act->ref == 0) {
198 /* free the action specific options */
200 act->def->free(act->options);
201 /* unref the definition */
202 actions_definition_unref(act->def);
207 static void actions_setup_data(ObActionsData *data,
214 struct _ObClient *client)
220 data->button = button;
222 data->client = client;
225 void actions_set_need_pointer_replay_before_move(gboolean replay)
227 replay_pointer = replay;
230 gboolean actions_get_need_pointer_replay_before_move()
232 return replay_pointer;
235 void actions_run_acts(GSList *acts,
242 struct _ObClient *client)
246 /* Don't allow saving the initial state when running things from the
248 if (uact == OB_USER_ACTION_MENU_SELECTION)
250 /* If x and y are < 0 then use the current pointer position */
252 screen_pointer_pos(&x, &y);
254 for (it = acts; it; it = g_slist_next(it)) {
256 ObActionsAct *act = it->data;
259 actions_setup_data(&data, uact, state, x, y, button, con, client);
261 /* if they have the same run function, then we'll assume they are
262 cooperating and not cancel eachother out */
263 if (!interactive_act || interactive_act->def->run != act->def->run) {
264 if (actions_act_is_interactive(act)) {
265 /* cancel the old one */
267 actions_interactive_cancel_act();
268 ok = actions_interactive_begin_act(act, state);
272 /* fire the action's run function with this data */
274 if (!act->def->run(&data, act->options)) {
275 if (actions_act_is_interactive(act))
276 actions_interactive_end_act();
278 /* make sure its interactive if it returned TRUE */
279 g_assert(act->def->i_cancel && act->def->i_input);
281 /* no actions are run after the interactive one */
288 gboolean actions_interactive_act_running(void)
290 return interactive_act != NULL;
293 void actions_interactive_cancel_act(void)
295 if (interactive_act) {
296 interactive_act->def->i_cancel(interactive_act->options);
297 actions_interactive_end_act();
301 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
303 if (grab_keyboard()) {
304 interactive_act = act;
305 actions_act_ref(interactive_act);
307 interactive_initial_state = state;
309 /* if using focus_delay, stop the timer now so that focus doesn't go
310 moving on us, which would kill the action */
311 event_halt_focus_delay();
319 static void actions_interactive_end_act(void)
321 if (interactive_act) {
324 actions_act_unref(interactive_act);
325 interactive_act = NULL;
329 gboolean actions_interactive_input_event(XEvent *e)
331 gboolean used = FALSE;
332 if (interactive_act) {
333 if (!interactive_act->def->i_input(interactive_initial_state, e,
334 interactive_act->options, &used))
336 used = TRUE; /* if it cancelled the action then it has to of
338 actions_interactive_end_act();
344 void actions_client_move(ObActionsData *data, gboolean start)
346 static gulong ignore_start = 0;
348 ignore_start = event_start_ignore_all_enters();
349 if (replay_pointer) {
350 /* replay the pointer event before any windows move */
351 XAllowEvents(obt_display, ReplayPointer, event_curtime);
352 replay_pointer = FALSE;
355 else if (config_focus_follow &&
356 data->context != OB_FRAME_CONTEXT_CLIENT)
358 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
361 /* usually this is sorta redundant, but with a press action
362 that moves windows our from under the cursor, the enter
363 event will come as a GrabNotify which is ignored, so this
364 makes a fake enter event
366 if ((c = client_under_pointer()) && c != data->client) {
367 ob_debug_type(OB_DEBUG_FOCUS,
368 "Generating fake enter because we did a "
369 "mouse-event action");
370 event_enter_client(c);
373 else if (!data->button && !config_focus_under_mouse)
374 event_end_ignore_all_enters(ignore_start);