cancel interactive actions when anothr action runs properly
[mikachu/openbox.git] / openbox / actions.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    actions.h for the Openbox window manager
4    Copyright (c) 2007        Dana Jansens
5
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.
10
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.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "actions.h"
20 #include "gettext.h"
21 #include "grab.h"
22 #include "screen.h"
23 #include "event.h"
24 #include "config.h"
25 #include "client.h"
26 #include "debug.h"
27
28 #include "actions/all.h"
29
30 static void     actions_definition_ref(ObActionsDefinition *def);
31 static void     actions_definition_unref(ObActionsDefinition *def);
32 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
33 static void     actions_interactive_end_act();
34
35 static ObActionsAct *interactive_act = NULL;
36 static guint         interactive_initial_state = 0;
37
38 struct _ObActionsDefinition {
39     guint ref;
40
41     gchar *name;
42
43     ObActionsDataSetupFunc setup;
44     ObActionsDataFreeFunc free;
45     ObActionsRunFunc run;
46     ObActionsInteractiveInputFunc i_input;
47     ObActionsInteractiveCancelFunc i_cancel;
48 };
49
50 struct _ObActionsAct {
51     guint ref;
52
53     ObActionsDefinition *def;
54     gpointer options;
55 };
56
57 static GSList *registered = NULL;
58
59
60 void actions_startup(gboolean reconfig)
61 {
62     if (reconfig) return;
63
64     action_all_startup();
65 }
66
67 void actions_shutdown(gboolean reconfig)
68 {
69     actions_interactive_cancel_act();
70
71     if (reconfig) return;
72
73     /* free all the registered actions */
74     while (registered) {
75         actions_definition_unref(registered->data);
76         registered = g_slist_delete_link(registered, registered);
77     }
78 }
79
80 gboolean actions_register(const gchar *name,
81                           ObActionsDataSetupFunc setup,
82                           ObActionsDataFreeFunc free,
83                           ObActionsRunFunc run,
84                           ObActionsInteractiveInputFunc i_input,
85                           ObActionsInteractiveCancelFunc i_cancel)
86 {
87     GSList *it;
88     ObActionsDefinition *def;
89
90     g_assert(run != NULL);
91     g_assert((i_input == NULL) == (i_cancel == NULL));
92
93     for (it = registered; it; it = g_slist_next(it)) {
94         def = it->data;
95         if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
96             return FALSE;
97     }
98
99     def = g_new(ObActionsDefinition, 1);
100     def->ref = 1;
101     def->name = g_strdup(name);
102     def->setup = setup;
103     def->free = free;
104     def->run = run;
105     def->i_input = i_input;
106     def->i_cancel = i_cancel;
107
108     registered = g_slist_prepend(registered, def);
109
110     return TRUE;
111 }
112
113 static void actions_definition_ref(ObActionsDefinition *def)
114 {
115     ++def->ref;
116 }
117
118 static void actions_definition_unref(ObActionsDefinition *def)
119 {
120     if (def && --def->ref == 0) {
121         g_free(def->name);
122         g_free(def);
123     }
124 }
125
126 ObActionsAct* actions_parse_string(const gchar *name)
127 {
128     GSList *it;
129     ObActionsDefinition *def = NULL;
130     ObActionsAct *act = NULL;
131
132     /* find the requested action */
133     for (it = registered; it; it = g_slist_next(it)) {
134         def = it->data;
135         if (!g_ascii_strcasecmp(name, def->name))
136             break;
137         def = NULL;
138     }
139
140     /* if we found the action */
141     if (def) {
142         act = g_new(ObActionsAct, 1);
143         act->ref = 1;
144         act->def = def;
145         actions_definition_ref(act->def);
146         act->options = NULL;
147     } else
148         g_message(_("Invalid action '%s' requested. No such action exists."),
149                   name);
150
151     return act;
152 }
153
154 ObActionsAct* actions_parse(ObParseInst *i,
155                             xmlDocPtr doc,
156                             xmlNodePtr node)
157 {
158     gchar *name;
159     ObActionsAct *act = NULL;
160
161     if (parse_attr_string("name", node, &name)) {
162         if ((act = actions_parse_string(name)))
163             /* there is more stuff to parse here */
164             if (act->def->setup)
165                 act->options = act->def->setup(i, doc, node->xmlChildrenNode);
166
167         g_free(name);
168     }
169
170     return act;
171 }
172
173 gboolean actions_act_is_interactive(ObActionsAct *act)
174 {
175     return act->def->i_cancel != NULL;
176 }
177
178 void actions_act_ref(ObActionsAct *act)
179 {
180     ++act->ref;
181 }
182
183 void actions_act_unref(ObActionsAct *act)
184 {
185     if (act && --act->ref == 0) {
186         /* free the action specific options */
187         if (act->def->free)
188             act->def->free(act->options);
189         /* unref the definition */
190         actions_definition_unref(act->def);
191         g_free(act);
192     }
193 }
194
195 static void actions_setup_data(ObActionsData *data,
196                                ObUserAction uact,
197                                guint state,
198                                gint x,
199                                gint y,
200                                gint button,
201                                ObFrameContext con,
202                                struct _ObClient *client)
203 {
204     data->uact = uact;
205     data->state = state;
206     data->x = x;
207     data->y = y;
208     data->button = button;
209     data->context = con;
210     data->client = client;
211 }
212
213 void actions_run_acts(GSList *acts,
214                       ObUserAction uact,
215                       guint state,
216                       gint x,
217                       gint y,
218                       gint button,
219                       ObFrameContext con,
220                       struct _ObClient *client)
221 {
222     GSList *it;
223
224     /* Don't allow saving the initial state when running things from the
225        menu */
226     if (uact == OB_USER_ACTION_MENU_SELECTION)
227         state = 0;
228     /* If x and y are < 0 then use the current pointer position */
229     if (x < 0 && y < 0)
230         screen_pointer_pos(&x, &y);
231
232     for (it = acts; it; it = g_slist_next(it)) {
233         ObActionsData data;
234         ObActionsAct *act = it->data;
235         gboolean ok = TRUE;
236
237         actions_setup_data(&data, uact, state, x, y, button, con, client);
238
239         if (!interactive_act || interactive_act->def != act->def) {
240             /* cancel the old one */
241             if (interactive_act)
242                 actions_interactive_cancel_act();
243             if (actions_act_is_interactive(act))
244                 ok = actions_interactive_begin_act(act, state);
245         }
246
247         /* fire the action's run function with this data */
248         if (ok) {
249             if (!act->def->run(&data, act->options))
250                 actions_interactive_end_act();
251             else {
252                 /* make sure its interactive if it returned TRUE */
253                 g_assert(act->def->i_cancel && act->def->i_input);
254
255                 /* no actions are run after the interactive one */
256                 break;
257             }
258         }
259     }
260 }
261
262 gboolean actions_interactive_act_running()
263 {
264     return interactive_act != NULL;
265 }
266
267 void actions_interactive_cancel_act()
268 {
269     if (interactive_act) {
270         interactive_act->def->i_cancel(interactive_act->options);
271         actions_interactive_end_act();
272     }
273 }
274
275 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
276 {
277     if (grab_keyboard()) {
278         interactive_act = act;
279         actions_act_ref(interactive_act);
280
281         interactive_initial_state = state;
282         return TRUE;
283     }
284     else
285         return FALSE;
286 }
287
288 static void actions_interactive_end_act()
289 {
290     if (interactive_act) {
291         ungrab_keyboard();
292
293         actions_act_unref(interactive_act);
294         interactive_act = NULL;
295     }
296 }
297
298 gboolean actions_interactive_input_event(XEvent *e)
299 {
300     gboolean used = FALSE;
301     if (interactive_act) {
302         if (!interactive_act->def->i_input(interactive_initial_state, e,
303                                            interactive_act->options, &used))
304         {
305             used = TRUE; /* if it cancelled the action then it has to of
306                             been used */
307             actions_interactive_end_act();
308         }
309     }
310     return used;
311 }
312
313 void actions_client_move(ObActionsData *data, gboolean start)
314 {
315     static gulong ignore_start = 0;
316     if (start)
317         ignore_start = event_start_ignore_all_enters();
318     else if (config_focus_follow &&
319              data->context != OB_FRAME_CONTEXT_CLIENT)
320     {
321         if (!data->button && data->client && !config_focus_under_mouse)
322             event_end_ignore_all_enters(ignore_start);
323         else {
324             struct _ObClient *c;
325
326             /* usually this is sorta redundant, but with a press action
327                that moves windows our from under the cursor, the enter
328                event will come as a GrabNotify which is ignored, so this
329                makes a fake enter event
330             */
331             if ((c = client_under_pointer()) && c != data->client) {
332                 ob_debug_type(OB_DEBUG_FOCUS,
333                               "Generating fake enter because we did a "
334                               "mouse-event action");
335                 event_enter_client(c);
336             }
337         }
338     }
339 }