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.
20 #include "actions_list.h"
31 #include "actions/all.h"
33 static void actions_definition_ref(ObActionsDefinition *def);
34 static void actions_definition_unref(ObActionsDefinition *def);
35 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
36 static void actions_interactive_end_act();
37 static ObActionsAct* actions_act_find_name(const gchar *name);
39 static ObActionsAct *interactive_act = NULL;
40 static guint interactive_initial_state = 0;
42 struct _ObActionsDefinition {
47 gboolean canbeinteractive;
49 ObActionsIDataSetupFunc i;
50 ObActionsDataSetupFunc n;
52 ObActionsDataFreeFunc free;
54 ObActionsShutdownFunc shutdown;
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_new(ObActionsDefinition);
109 def->name = g_strdup(name);
112 def->shutdown = NULL;
114 registered = g_slist_prepend(registered, def);
118 gboolean actions_register_i(const gchar *name,
119 ObActionsIDataSetupFunc setup,
120 ObActionsDataFreeFunc free,
121 ObActionsRunFunc run)
123 ObActionsDefinition *def = do_register(name, free, run);
125 def->canbeinteractive = TRUE;
126 def->setup.i = setup;
131 gboolean actions_register(const gchar *name,
132 ObActionsDataSetupFunc setup,
133 ObActionsDataFreeFunc free,
134 ObActionsRunFunc run)
136 ObActionsDefinition *def = do_register(name, free, run);
138 def->canbeinteractive = FALSE;
139 def->setup.n = setup;
144 gboolean actions_set_shutdown(const gchar *name,
145 ObActionsShutdownFunc shutdown)
148 ObActionsDefinition *def;
150 for (it = registered; it; it = g_slist_next(it)) {
152 if (!g_ascii_strcasecmp(name, def->name)) {
153 def->shutdown = shutdown;
160 static void actions_definition_ref(ObActionsDefinition *def)
165 static void actions_definition_unref(ObActionsDefinition *def)
167 if (def && --def->ref == 0) {
169 g_slice_free(ObActionsDefinition, def);
173 static ObActionsAct* actions_act_find_name(const gchar *name)
176 ObActionsDefinition *def = NULL;
177 ObActionsAct *act = NULL;
179 /* find the requested action */
180 for (it = registered; it; it = g_slist_next(it)) {
182 if (!g_ascii_strcasecmp(name, def->name))
187 /* if we found the action */
189 act = g_slice_new(ObActionsAct);
192 actions_definition_ref(act->def);
195 act->i_cancel = NULL;
199 g_message(_("Invalid action \"%s\" requested. No such action exists."),
205 ObActionsAct* actions_act_new(const gchar *name, GHashTable *config)
207 ObActionsAct *act = NULL;
209 act = actions_act_find_name(name);
211 /* there is more stuff to parse here */
212 if (act->def->canbeinteractive) {
213 if (act->def->setup.i)
214 act->options = act->def->setup.i(config,
221 if (act->def->setup.n)
222 act->options = act->def->setup.n(config);
229 gboolean actions_act_is_interactive(ObActionsAct *act)
231 return act->i_input != NULL;
234 void actions_act_ref(ObActionsAct *act)
239 void actions_act_unref(ObActionsAct *act)
241 if (act && --act->ref == 0) {
242 /* free the action specific options */
244 act->def->free(act->options);
245 /* unref the definition */
246 actions_definition_unref(act->def);
247 g_slice_free(ObActionsAct, act);
251 static void actions_setup_data(ObActionsData *data,
258 struct _ObClient *client)
264 data->button = button;
266 data->client = client;
269 gboolean actions_run_acts(ObActionsList *acts,
276 struct _ObClient *client)
278 gboolean ran_interactive;
279 gboolean update_user_time;
281 /* Don't allow saving the initial state when running things from the
283 if (uact == OB_USER_ACTION_MENU_SELECTION)
285 /* If x and y are < 0 then use the current pointer position */
287 screen_pointer_pos(&x, &y);
289 ran_interactive = FALSE;
290 update_user_time = FALSE;
296 if (acts->isfilter) {
297 g_warning("filters not implemented!");
302 act = acts->u.action;
305 actions_setup_data(&data, uact, state, x, y, button, con, client);
307 /* if they have the same run function, then we'll assume they are
308 cooperating and not cancel eachother out */
309 if (!interactive_act || interactive_act->def->run != act->def->run) {
310 if (actions_act_is_interactive(act)) {
311 /* cancel the old one */
313 actions_interactive_cancel_act();
315 if (!act->i_pre(state, act->options))
316 act->i_input = NULL; /* remove the interactivity */
317 ran_interactive = TRUE;
319 /* check again cuz it might have been cancelled */
320 if (actions_act_is_interactive(act)) {
321 ok = actions_interactive_begin_act(act, state);
322 ran_interactive = TRUE;
326 /* fire the action's run function with this data */
328 if (!act->def->run(&data, act->options)) {
329 if (actions_act_is_interactive(act))
330 actions_interactive_end_act();
331 if (client && client == focus_client)
332 update_user_time = TRUE;
334 /* make sure its interactive if it returned TRUE */
335 g_assert(act->i_input);
337 /* no actions are run after the interactive one */
343 if (update_user_time)
344 event_update_user_time();
345 return ran_interactive;
348 gboolean actions_interactive_act_running(void)
350 return interactive_act != NULL;
353 void actions_interactive_cancel_act(void)
355 if (interactive_act) {
356 if (interactive_act->i_cancel)
357 interactive_act->i_cancel(interactive_act->options);
358 actions_interactive_end_act();
362 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
364 if (grab_keyboard()) {
365 interactive_act = act;
366 actions_act_ref(interactive_act);
368 interactive_initial_state = state;
370 /* if using focus_delay, stop the timer now so that focus doesn't go
371 moving on us, which would kill the action */
372 event_halt_focus_delay();
380 static void actions_interactive_end_act(void)
382 if (interactive_act) {
383 ObActionsAct *ia = interactive_act;
385 /* set this to NULL first so the i_post() function can't cause this to
386 get called again (if it decides it wants to cancel any ongoing
387 interactive action). */
388 interactive_act = NULL;
393 ia->i_post(ia->options);
395 actions_act_unref(ia);
399 gboolean actions_interactive_input_event(XEvent *e)
401 gboolean used = FALSE;
402 if (interactive_act) {
403 if (!interactive_act->i_input(interactive_initial_state, e,
404 grab_input_context(),
405 interactive_act->options, &used))
407 used = TRUE; /* if it cancelled the action then it has to of
409 actions_interactive_end_act();
415 void actions_client_move(ObActionsData *data, gboolean start)
417 static gulong ignore_start = 0;
419 ignore_start = event_start_ignore_all_enters();
420 else if (config_focus_follow &&
421 data->context != OB_FRAME_CONTEXT_CLIENT)
423 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
426 /* usually this is sorta redundant, but with a press action
427 that moves windows our from under the cursor, the enter
428 event will come as a GrabNotify which is ignored, so this
429 makes a fake enter event
431 don't do this if there is a grab on the pointer. enter events
432 are ignored during a grab, so don't force fake ones when they
435 if (!grab_on_pointer()) {
436 if ((c = client_under_pointer()) && c != data->client) {
437 ob_debug_type(OB_DEBUG_FOCUS,
438 "Generating fake enter because we did a "
439 "mouse-event action");
440 event_enter_client(c);
442 else if (!c && c != data->client) {
443 ob_debug_type(OB_DEBUG_FOCUS,
444 "Generating fake leave because we did a "
445 "mouse-event action");
446 event_enter_client(data->client);
450 else if (!data->button && !config_focus_under_mouse)
451 event_end_ignore_all_enters(ignore_start);