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