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;
40 static gboolean stop_running = FALSE;
42 struct _ObActionsDefinition {
47 gboolean canbeinteractive;
49 ObActionsIDataSetupFunc i;
50 ObActionsDataSetupFunc n;
52 ObActionsDataFreeFunc free;
54 ObActionsShutdownFunc shutdown;
55 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;
116 registered = g_slist_prepend(registered, def);
120 gboolean actions_register_i(const gchar *name,
121 ObActionsIDataSetupFunc setup,
122 ObActionsDataFreeFunc free,
123 ObActionsRunFunc run)
125 ObActionsDefinition *def = do_register(name, free, run);
127 def->canbeinteractive = TRUE;
128 def->setup.i = setup;
133 gboolean actions_register(const gchar *name,
134 ObActionsDataSetupFunc setup,
135 ObActionsDataFreeFunc free,
136 ObActionsRunFunc run)
138 ObActionsDefinition *def = do_register(name, free, run);
140 def->canbeinteractive = FALSE;
141 def->setup.n = setup;
146 gboolean actions_set_shutdown(const gchar *name,
147 ObActionsShutdownFunc shutdown)
150 ObActionsDefinition *def;
152 for (it = registered; it; it = g_slist_next(it)) {
154 if (!g_ascii_strcasecmp(name, def->name)) {
155 def->shutdown = shutdown;
162 gboolean actions_set_modifies_focused_window(const gchar *name,
166 ObActionsDefinition *def;
168 for (it = registered; it; it = g_slist_next(it)) {
170 if (!g_ascii_strcasecmp(name, def->name)) {
171 def->modifies_focused_window = modifies;
178 static void actions_definition_ref(ObActionsDefinition *def)
183 static void actions_definition_unref(ObActionsDefinition *def)
185 if (def && --def->ref == 0) {
187 g_slice_free(ObActionsDefinition, def);
191 static ObActionsAct* actions_build_act_from_string(const gchar *name)
194 ObActionsDefinition *def = NULL;
195 ObActionsAct *act = NULL;
197 /* find the requested action */
198 for (it = registered; it; it = g_slist_next(it)) {
200 if (!g_ascii_strcasecmp(name, def->name))
205 /* if we found the action */
207 act = g_slice_new(ObActionsAct);
210 actions_definition_ref(act->def);
213 act->i_cancel = NULL;
217 g_message(_("Invalid action \"%s\" requested. No such action exists."),
223 ObActionsAct* actions_parse_string(const gchar *name)
225 ObActionsAct *act = NULL;
227 if ((act = actions_build_act_from_string(name))) {
228 if (act->def->canbeinteractive) {
229 if (act->def->setup.i)
230 act->options = act->def->setup.i(NULL,
237 if (act->def->setup.n)
238 act->options = act->def->setup.n(NULL);
246 ObActionsAct* actions_parse(xmlNodePtr node)
249 ObActionsAct *act = NULL;
251 if (obt_xml_attr_string(node, "name", &name)) {
252 if ((act = actions_build_act_from_string(name))) {
253 /* there is more stuff to parse here */
254 if (act->def->canbeinteractive) {
255 if (act->def->setup.i)
256 act->options = act->def->setup.i(node->children,
263 if (act->def->setup.n)
264 act->options = act->def->setup.n(node->children);
273 gboolean actions_act_is_interactive(ObActionsAct *act)
275 return act->i_input != NULL;
278 void actions_act_ref(ObActionsAct *act)
283 void actions_act_unref(ObActionsAct *act)
285 if (act && --act->ref == 0) {
286 /* free the action specific options */
288 act->def->free(act->options);
289 /* unref the definition */
290 actions_definition_unref(act->def);
291 g_slice_free(ObActionsAct, act);
295 static void actions_setup_data(ObActionsData *data,
302 struct _ObClient *client)
308 data->button = button;
310 data->client = client;
313 void actions_stop_running()
318 void actions_run_acts(GSList *acts,
325 struct _ObClient *client)
328 gboolean update_user_time;
330 stop_running = FALSE;
332 /* Don't allow saving the initial state when running things from the
334 if (uact == OB_USER_ACTION_MENU_SELECTION)
336 /* If x and y are < 0 then use the current pointer position */
338 screen_pointer_pos(&x, &y);
340 update_user_time = FALSE;
341 for (it = acts; it; it = g_slist_next(it)) {
343 ObActionsAct *act = it->data;
346 actions_setup_data(&data, uact, state, x, y, button, con, client);
348 /* if they have the same run function, then we'll assume they are
349 cooperating and not cancel eachother out */
350 if (!interactive_act || interactive_act->def->run != act->def->run) {
351 if (actions_act_is_interactive(act)) {
352 /* cancel the old one */
354 actions_interactive_cancel_act();
356 if (!act->i_pre(state, act->options))
357 act->i_input = NULL; /* remove the interactivity */
359 /* check again cuz it might have been cancelled */
360 if (actions_act_is_interactive(act))
361 ok = actions_interactive_begin_act(act, state);
364 /* fire the action's run function with this data */
366 if (!act->def->run(&data, act->options)) {
367 if (actions_act_is_interactive(act)) {
368 actions_interactive_end_act();
369 } else if (stop_running) {
370 stop_running = FALSE;
373 if (client && client == focus_client &&
374 act->def->modifies_focused_window)
376 update_user_time = TRUE;
379 /* make sure its interactive if it returned TRUE */
380 g_assert(act->i_input);
382 /* no actions are run after the interactive one */
387 if (update_user_time)
388 event_update_user_time();
391 gboolean actions_interactive_act_running(void)
393 return interactive_act != NULL;
396 void actions_interactive_cancel_act(void)
398 if (interactive_act) {
399 if (interactive_act->i_cancel)
400 interactive_act->i_cancel(interactive_act->options);
401 actions_interactive_end_act();
405 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
407 if (grab_keyboard()) {
408 interactive_act = act;
409 actions_act_ref(interactive_act);
411 interactive_initial_state = state;
413 /* if using focus_delay, stop the timer now so that focus doesn't go
414 moving on us, which would kill the action */
415 event_halt_focus_delay();
423 static void actions_interactive_end_act(void)
425 if (interactive_act) {
426 ObActionsAct *ia = interactive_act;
428 /* set this to NULL first so the i_post() function can't cause this to
429 get called again (if it decides it wants to cancel any ongoing
430 interactive action). */
431 interactive_act = NULL;
436 ia->i_post(ia->options);
438 actions_act_unref(ia);
442 gboolean actions_interactive_input_event(XEvent *e)
444 gboolean used = FALSE;
445 if (interactive_act) {
446 if (!interactive_act->i_input(interactive_initial_state, e,
447 grab_input_context(),
448 interactive_act->options, &used))
450 used = TRUE; /* if it cancelled the action then it has to of
452 actions_interactive_end_act();
458 void actions_client_move(ObActionsData *data, gboolean start)
460 static gulong ignore_start = 0;
462 ignore_start = event_start_ignore_all_enters();
463 else if (config_focus_follow &&
464 data->context != OB_FRAME_CONTEXT_CLIENT)
466 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
469 /* usually this is sorta redundant, but with a press action
470 that moves windows our from under the cursor, the enter
471 event will come as a GrabNotify which is ignored, so this
472 makes a fake enter event
474 don't do this if there is a grab on the pointer. enter events
475 are ignored during a grab, so don't force fake ones when they
478 if (!grab_on_pointer()) {
479 if ((c = client_under_pointer()) && c != data->client) {
480 ob_debug_type(OB_DEBUG_FOCUS,
481 "Generating fake enter because we did a "
482 "mouse-event action");
483 event_enter_client(c);
485 else if (!c && c != data->client) {
486 ob_debug_type(OB_DEBUG_FOCUS,
487 "Generating fake leave because we did a "
488 "mouse-event action");
489 event_leave_client(data->client);
493 else if (!data->button && !config_focus_under_mouse)
494 event_end_ignore_all_enters(ignore_start);
498 gboolean actions_client_locked(ObActionsData *data)
500 ObClient *c = data->client;
502 return !c || (c && c->locked);