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;
59 struct _ObActionsAct {
62 ObActionsDefinition *def;
63 ObActionsIPreFunc i_pre;
64 ObActionsIInputFunc i_input;
65 ObActionsICancelFunc i_cancel;
66 ObActionsIPostFunc i_post;
70 static GSList *registered = NULL;
72 void actions_startup(gboolean reconfig)
79 void actions_shutdown(gboolean reconfig)
81 actions_interactive_cancel_act();
85 /* free all the registered actions */
87 ObActionsDefinition *d = registered->data;
88 if (d->shutdown) d->shutdown();
89 actions_definition_unref(d);
90 registered = g_slist_delete_link(registered, registered);
94 ObActionsDefinition* do_register(const gchar *name,
95 ObActionsDataFreeFunc free,
99 ObActionsDefinition *def;
101 g_assert(run != NULL);
103 for (it = registered; it; it = g_slist_next(it)) {
105 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
109 def = g_slice_new0(ObActionsDefinition);
111 def->name = g_strdup(name);
114 def->shutdown = NULL;
115 def->modifies_focused_window = TRUE;
116 def->can_stop = FALSE;
118 registered = g_slist_prepend(registered, def);
122 gboolean actions_register_i(const gchar *name,
123 ObActionsIDataSetupFunc setup,
124 ObActionsDataFreeFunc free,
125 ObActionsRunFunc run)
127 ObActionsDefinition *def = do_register(name, free, run);
129 def->canbeinteractive = TRUE;
130 def->setup.i = setup;
135 gboolean actions_register(const gchar *name,
136 ObActionsDataSetupFunc setup,
137 ObActionsDataFreeFunc free,
138 ObActionsRunFunc run)
140 ObActionsDefinition *def = do_register(name, free, run);
142 def->canbeinteractive = FALSE;
143 def->setup.n = setup;
148 gboolean actions_set_shutdown(const gchar *name,
149 ObActionsShutdownFunc shutdown)
152 ObActionsDefinition *def;
154 for (it = registered; it; it = g_slist_next(it)) {
156 if (!g_ascii_strcasecmp(name, def->name)) {
157 def->shutdown = shutdown;
164 gboolean actions_set_modifies_focused_window(const gchar *name,
168 ObActionsDefinition *def;
170 for (it = registered; it; it = g_slist_next(it)) {
172 if (!g_ascii_strcasecmp(name, def->name)) {
173 def->modifies_focused_window = modifies;
180 gboolean actions_set_can_stop(const gchar *name,
184 ObActionsDefinition *def;
186 for (it = registered; it; it = g_slist_next(it)) {
188 if (!g_ascii_strcasecmp(name, def->name)) {
189 def->can_stop = can_stop;
196 static void actions_definition_ref(ObActionsDefinition *def)
201 static void actions_definition_unref(ObActionsDefinition *def)
203 if (def && --def->ref == 0) {
205 g_slice_free(ObActionsDefinition, def);
209 static ObActionsAct* actions_build_act_from_string(const gchar *name)
212 ObActionsDefinition *def = NULL;
213 ObActionsAct *act = NULL;
215 /* find the requested action */
216 for (it = registered; it; it = g_slist_next(it)) {
218 if (!g_ascii_strcasecmp(name, def->name))
223 /* if we found the action */
225 act = g_slice_new(ObActionsAct);
228 actions_definition_ref(act->def);
231 act->i_cancel = NULL;
235 g_message(_("Invalid action \"%s\" requested. No such action exists."),
241 ObActionsAct* actions_parse_string(const gchar *name)
243 ObActionsAct *act = NULL;
245 if ((act = actions_build_act_from_string(name))) {
246 if (act->def->canbeinteractive) {
247 if (act->def->setup.i)
248 act->options = act->def->setup.i(NULL,
255 if (act->def->setup.n)
256 act->options = act->def->setup.n(NULL);
264 ObActionsAct* actions_parse(xmlNodePtr node)
267 ObActionsAct *act = NULL;
269 if (obt_xml_attr_string(node, "name", &name)) {
270 if ((act = actions_build_act_from_string(name))) {
271 /* there is more stuff to parse here */
272 if (act->def->canbeinteractive) {
273 if (act->def->setup.i)
274 act->options = act->def->setup.i(node->children,
281 if (act->def->setup.n)
282 act->options = act->def->setup.n(node->children);
291 gboolean actions_act_is_interactive(ObActionsAct *act)
293 return act->i_input != NULL;
296 void actions_act_ref(ObActionsAct *act)
301 void actions_act_unref(ObActionsAct *act)
303 if (act && --act->ref == 0) {
304 /* free the action specific options */
306 act->def->free(act->options);
307 /* unref the definition */
308 actions_definition_unref(act->def);
309 g_slice_free(ObActionsAct, act);
313 static void actions_setup_data(ObActionsData *data,
320 struct _ObClient *client)
326 data->button = button;
328 data->client = client;
331 void actions_stop_running()
336 void actions_run_acts(GSList *acts,
343 struct _ObClient *client)
346 gboolean update_user_time;
348 stop_running = FALSE;
350 /* Don't allow saving the initial state when running things from the
352 if (uact == OB_USER_ACTION_MENU_SELECTION)
354 /* If x and y are < 0 then use the current pointer position */
356 screen_pointer_pos(&x, &y);
358 update_user_time = FALSE;
359 for (it = acts; it; it = g_slist_next(it)) {
361 ObActionsAct *act = it->data;
364 actions_setup_data(&data, uact, state, x, y, button, con, client);
366 /* if they have the same run function, then we'll assume they are
367 cooperating and not cancel eachother out */
368 if (!interactive_act || interactive_act->def->run != act->def->run) {
369 if (actions_act_is_interactive(act)) {
370 /* cancel the old one */
372 actions_interactive_cancel_act();
374 if (!act->i_pre(state, act->options))
375 act->i_input = NULL; /* remove the interactivity */
377 /* check again cuz it might have been cancelled */
378 if (actions_act_is_interactive(act))
379 ok = actions_interactive_begin_act(act, state);
382 /* fire the action's run function with this data */
384 if (!act->def->run(&data, act->options)) {
385 if (actions_act_is_interactive(act)) {
386 actions_interactive_end_act();
387 } else if (stop_running) {
388 stop_running = FALSE;
391 if (client && client == focus_client &&
392 act->def->modifies_focused_window)
394 update_user_time = TRUE;
397 /* make sure its interactive or allowed to stop
398 if it returned TRUE */
399 g_assert(act->i_input || act->def->can_stop);
401 /* no actions are run after the interactive one */
406 if (update_user_time)
407 event_update_user_time();
410 gboolean actions_interactive_act_running(void)
412 return interactive_act != NULL;
415 void actions_interactive_cancel_act(void)
417 if (interactive_act) {
418 if (interactive_act->i_cancel)
419 interactive_act->i_cancel(interactive_act->options);
420 actions_interactive_end_act();
424 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
426 if (grab_keyboard()) {
427 interactive_act = act;
428 actions_act_ref(interactive_act);
430 interactive_initial_state = state;
432 /* if using focus_delay, stop the timer now so that focus doesn't go
433 moving on us, which would kill the action */
434 event_halt_focus_delay();
442 static void actions_interactive_end_act(void)
444 if (interactive_act) {
445 ObActionsAct *ia = interactive_act;
447 /* set this to NULL first so the i_post() function can't cause this to
448 get called again (if it decides it wants to cancel any ongoing
449 interactive action). */
450 interactive_act = NULL;
455 ia->i_post(ia->options);
457 actions_act_unref(ia);
461 gboolean actions_interactive_input_event(XEvent *e)
463 gboolean used = FALSE;
464 if (interactive_act) {
465 if (!interactive_act->i_input(interactive_initial_state, e,
466 grab_input_context(),
467 interactive_act->options, &used))
469 used = TRUE; /* if it cancelled the action then it has to of
471 actions_interactive_end_act();
477 void actions_client_move(ObActionsData *data, gboolean start)
479 static gulong ignore_start = 0;
481 ignore_start = event_start_ignore_all_enters();
482 else if (config_focus_follow &&
483 data->context != OB_FRAME_CONTEXT_CLIENT)
485 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
488 /* usually this is sorta redundant, but with a press action
489 that moves windows our from under the cursor, the enter
490 event will come as a GrabNotify which is ignored, so this
491 makes a fake enter event
493 don't do this if there is a grab on the pointer. enter events
494 are ignored during a grab, so don't force fake ones when they
497 if (!grab_on_pointer()) {
498 if ((c = client_under_pointer()) && c != data->client) {
499 ob_debug_type(OB_DEBUG_FOCUS,
500 "Generating fake enter because we did a "
501 "mouse-event action");
502 event_enter_client(c);
504 else if (!c && c != data->client) {
505 ob_debug_type(OB_DEBUG_FOCUS,
506 "Generating fake leave because we did a "
507 "mouse-event action");
508 event_leave_client(data->client);
512 else if (!data->button && !config_focus_under_mouse)
513 event_end_ignore_all_enters(ignore_start);
517 gboolean actions_client_locked(ObActionsData *data)
519 ObClient *c = data->client;
521 return !c || (c && c->locked);