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