pass an input context to the interactive input function
[dana/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 "openbox.h"
27 #include "debug.h"
28
29 #include "actions/all.h"
30
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);
36
37 static ObActionsAct *interactive_act = NULL;
38 static guint         interactive_initial_state = 0;
39
40 struct _ObActionsDefinition {
41     guint ref;
42
43     gchar *name;
44
45     gboolean canbeinteractive;
46     union {
47         ObActionsIDataSetupFunc i;
48         ObActionsDataSetupFunc n;
49     } setup;
50     ObActionsDataFreeFunc free;
51     ObActionsRunFunc run;
52     ObActionsShutdownFunc shutdown;
53 };
54
55 struct _ObActionsAct {
56     guint ref;
57
58     ObActionsDefinition *def;
59     ObActionsIPreFunc i_pre;
60     ObActionsIInputFunc i_input;
61     ObActionsICancelFunc i_cancel;
62     ObActionsIPostFunc i_post;
63     gpointer options;
64 };
65
66 static GSList *registered = NULL;
67
68 void actions_startup(gboolean reconfig)
69 {
70     if (reconfig) return;
71
72     action_all_startup();
73 }
74
75 void actions_shutdown(gboolean reconfig)
76 {
77     actions_interactive_cancel_act();
78
79     if (reconfig) return;
80
81     /* free all the registered actions */
82     while (registered) {
83         ObActionsDefinition *d = registered->data;
84         if (d->shutdown) d->shutdown();
85         actions_definition_unref(d);
86         registered = g_slist_delete_link(registered, registered);
87     }
88 }
89
90 ObActionsDefinition* do_register(const gchar *name,
91                                  ObActionsDataFreeFunc free,
92                                  ObActionsRunFunc run)
93 {
94     GSList *it;
95     ObActionsDefinition *def;
96
97     g_assert(run != NULL);
98
99     for (it = registered; it; it = g_slist_next(it)) {
100         def = it->data;
101         if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
102             return NULL;
103     }
104
105     def = g_new(ObActionsDefinition, 1);
106     def->ref = 1;
107     def->name = g_strdup(name);
108     def->free = free;
109     def->run = run;
110
111     registered = g_slist_prepend(registered, def);
112     return def;
113 }
114
115 gboolean actions_register_i(const gchar *name,
116                             ObActionsIDataSetupFunc setup,
117                             ObActionsDataFreeFunc free,
118                             ObActionsRunFunc run)
119 {
120     ObActionsDefinition *def = do_register(name, free, run);
121     if (def) {
122         def->canbeinteractive = TRUE;
123         def->setup.i = setup;
124     }
125     return def != NULL;
126 }
127
128 gboolean actions_register(const gchar *name,
129                           ObActionsDataSetupFunc setup,
130                           ObActionsDataFreeFunc free,
131                           ObActionsRunFunc run)
132 {
133     ObActionsDefinition *def = do_register(name, free, run);
134     if (def) {
135         def->canbeinteractive = FALSE;
136         def->setup.n = setup;
137     }
138     return def != NULL;
139 }
140
141 gboolean actions_set_shutdown(const gchar *name,
142                               ObActionsShutdownFunc shutdown)
143 {
144     GSList *it;
145     ObActionsDefinition *def;
146
147     for (it = registered; it; it = g_slist_next(it)) {
148         def = it->data;
149         if (!g_ascii_strcasecmp(name, def->name)) {
150             def->shutdown = shutdown;
151             return TRUE;
152         }
153     }
154     return FALSE;
155 }
156
157 static void actions_definition_ref(ObActionsDefinition *def)
158 {
159     ++def->ref;
160 }
161
162 static void actions_definition_unref(ObActionsDefinition *def)
163 {
164     if (def && --def->ref == 0) {
165         g_free(def->name);
166         g_free(def);
167     }
168 }
169
170 static ObActionsAct* actions_build_act_from_string(const gchar *name)
171 {
172     GSList *it;
173     ObActionsDefinition *def = NULL;
174     ObActionsAct *act = NULL;
175
176     /* find the requested action */
177     for (it = registered; it; it = g_slist_next(it)) {
178         def = it->data;
179         if (!g_ascii_strcasecmp(name, def->name))
180             break;
181         def = NULL;
182     }
183
184     /* if we found the action */
185     if (def) {
186         act = g_new(ObActionsAct, 1);
187         act->ref = 1;
188         act->def = def;
189         actions_definition_ref(act->def);
190         act->i_pre = NULL;
191         act->i_input = NULL;
192         act->i_cancel = NULL;
193         act->i_post = NULL;
194         act->options = NULL;
195     } else
196         g_message(_("Invalid action \"%s\" requested. No such action exists."),
197                   name);
198
199     return act;
200 }
201
202 ObActionsAct* actions_parse_string(const gchar *name)
203 {
204     ObActionsAct *act = NULL;
205
206     if ((act = actions_build_act_from_string(name))) {
207         if (act->def->canbeinteractive) {
208             if (act->def->setup.i)
209                 act->options = act->def->setup.i(NULL,
210                                                  &act->i_pre,
211                                                  &act->i_input,
212                                                  &act->i_cancel,
213                                                  &act->i_post);
214         }
215         else {
216             if (act->def->setup.n)
217                 act->options = act->def->setup.n(NULL);
218         }
219     }
220                 
221
222     return act;
223 }
224
225 ObActionsAct* actions_parse(xmlNodePtr node)
226 {
227     gchar *name;
228     ObActionsAct *act = NULL;
229
230     if (obt_xml_attr_string(node, "name", &name)) {
231         if ((act = actions_build_act_from_string(name))) {
232             /* there is more stuff to parse here */
233             if (act->def->canbeinteractive) {
234                 if (act->def->setup.i)
235                     act->options = act->def->setup.i(node->children,
236                                                      &act->i_pre,
237                                                      &act->i_input,
238                                                      &act->i_cancel,
239                                                      &act->i_post);
240             }
241             else {
242                 if (act->def->setup.n)
243                     act->options = act->def->setup.n(node->children);
244             }
245         }
246         g_free(name);
247     }
248
249     return act;
250 }
251
252 gboolean actions_act_is_interactive(ObActionsAct *act)
253 {
254     return act->i_input != NULL;
255 }
256
257 void actions_act_ref(ObActionsAct *act)
258 {
259     ++act->ref;
260 }
261
262 void actions_act_unref(ObActionsAct *act)
263 {
264     if (act && --act->ref == 0) {
265         /* free the action specific options */
266         if (act->def->free)
267             act->def->free(act->options);
268         /* unref the definition */
269         actions_definition_unref(act->def);
270         g_free(act);
271     }
272 }
273
274 static void actions_setup_data(ObActionsData *data,
275                                ObUserAction uact,
276                                guint state,
277                                gint x,
278                                gint y,
279                                gint button,
280                                ObFrameContext con,
281                                struct _ObClient *client)
282 {
283     data->uact = uact;
284     data->state = state;
285     data->x = x;
286     data->y = y;
287     data->button = button;
288     data->context = con;
289     data->client = client;
290 }
291
292 void actions_run_acts(GSList *acts,
293                       ObUserAction uact,
294                       guint state,
295                       gint x,
296                       gint y,
297                       gint button,
298                       ObFrameContext con,
299                       struct _ObClient *client)
300 {
301     GSList *it;
302
303     /* Don't allow saving the initial state when running things from the
304        menu */
305     if (uact == OB_USER_ACTION_MENU_SELECTION)
306         state = 0;
307     /* If x and y are < 0 then use the current pointer position */
308     if (x < 0 && y < 0)
309         screen_pointer_pos(&x, &y);
310
311     for (it = acts; it; it = g_slist_next(it)) {
312         ObActionsData data;
313         ObActionsAct *act = it->data;
314         gboolean ok = TRUE;
315
316         actions_setup_data(&data, uact, state, x, y, button, con, client);
317
318         /* if they have the same run function, then we'll assume they are
319            cooperating and not cancel eachother out */
320         if (!interactive_act || interactive_act->def->run != act->def->run) {
321             if (actions_act_is_interactive(act)) {
322                 /* cancel the old one */
323                 if (interactive_act)
324                     actions_interactive_cancel_act();
325                 if (act->i_pre)
326                     if (!act->i_pre(state, act->options))
327                         act->i_input = NULL; /* remove the interactivity */
328             }
329             /* check again cuz it might have been cancelled */
330             if (actions_act_is_interactive(act))
331                 ok = actions_interactive_begin_act(act, state);
332         }
333
334         /* fire the action's run function with this data */
335         if (ok) {
336             if (!act->def->run(&data, act->options)) {
337                 if (actions_act_is_interactive(act))
338                     actions_interactive_end_act();
339             } else {
340                 /* make sure its interactive if it returned TRUE */
341                 g_assert(act->i_input);
342
343                 /* no actions are run after the interactive one */
344                 break;
345             }
346         }
347     }
348 }
349
350 gboolean actions_interactive_act_running(void)
351 {
352     return interactive_act != NULL;
353 }
354
355 void actions_interactive_cancel_act(void)
356 {
357     if (interactive_act) {
358         if (interactive_act->i_cancel)
359             interactive_act->i_cancel(interactive_act->options);
360         actions_interactive_end_act();
361     }
362 }
363
364 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
365 {
366     if (grab_keyboard()) {
367         interactive_act = act;
368         actions_act_ref(interactive_act);
369
370         interactive_initial_state = state;
371
372         /* if using focus_delay, stop the timer now so that focus doesn't go
373            moving on us, which would kill the action */
374         event_halt_focus_delay();
375
376         return TRUE;
377     }
378     else
379         return FALSE;
380 }
381
382 static void actions_interactive_end_act(void)
383 {
384     if (interactive_act) {
385         ungrab_keyboard();
386
387         if (interactive_act->i_post)
388             interactive_act->i_post(interactive_act->options);
389
390         actions_act_unref(interactive_act);
391         interactive_act = NULL;
392     }
393 }
394
395 gboolean actions_interactive_input_event(XEvent *e)
396 {
397     gboolean used = FALSE;
398     if (interactive_act) {
399         if (!interactive_act->i_input(interactive_initial_state, e,
400                                       grab_input_context(),
401                                       interactive_act->options, &used))
402         {
403             used = TRUE; /* if it cancelled the action then it has to of
404                             been used */
405             actions_interactive_end_act();
406         }
407     }
408     return used;
409 }
410
411 void actions_client_move(ObActionsData *data, gboolean start)
412 {
413     static gulong ignore_start = 0;
414     if (start)
415         ignore_start = event_start_ignore_all_enters();
416     else if (config_focus_follow &&
417              data->context != OB_FRAME_CONTEXT_CLIENT)
418     {
419         if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
420             struct _ObClient *c;
421
422             /* usually this is sorta redundant, but with a press action
423                that moves windows our from under the cursor, the enter
424                event will come as a GrabNotify which is ignored, so this
425                makes a fake enter event
426
427                don't do this if there is a grab on the pointer.  enter events
428                are ignored during a grab, so don't force fake ones when they
429                should be ignored
430             */
431             if (!grab_on_pointer()) {
432                 if ((c = client_under_pointer()) && c != data->client) {
433                     ob_debug_type(OB_DEBUG_FOCUS,
434                                   "Generating fake enter because we did a "
435                                   "mouse-event action");
436                     event_enter_client(c);
437                 }
438                 else if (!c && c != data->client) {
439                     ob_debug_type(OB_DEBUG_FOCUS,
440                                   "Generating fake leave because we did a "
441                                   "mouse-event action");
442                     event_enter_client(data->client);
443                 }
444             }
445         }
446         else if (!data->button && !config_focus_under_mouse)
447             event_end_ignore_all_enters(ignore_start);
448     }
449 }