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