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