Allow windows created by execute actions to steal focus if the user isn't interacting...
[dana/openbox.git] / openbox / actions / execute.c
1 #include "openbox/actions.h"
2 #include "openbox/event.h"
3 #include "openbox/startupnotify.h"
4 #include "openbox/client.h"
5 #include "openbox/prompt.h"
6 #include "openbox/screen.h"
7 #include "obt/paths.h"
8 #include "gettext.h"
9
10 #ifdef HAVE_STDLIB_H
11 #  include <stdlib.h>
12 #endif
13
14 typedef struct {
15     gchar   *cmd;
16     gboolean sn;
17     gchar   *sn_name;
18     gchar   *sn_icon;
19     gchar   *sn_wmclass;
20     gchar   *prompt;
21     ObActionsData *data;
22 } Options;
23
24 static gpointer setup_func(xmlNodePtr node);
25 static void     free_func(gpointer options);
26 static gboolean run_func(ObActionsData *data, gpointer options);
27 static void shutdown_func(void);
28 static void client_dest(ObClient *client, gpointer data);
29
30 static GSList *prompt_opts = NULL;
31
32 void action_execute_startup(void)
33 {
34     actions_register("Execute", setup_func, free_func, run_func);
35     actions_set_shutdown("Execute", shutdown_func);
36     actions_set_modifies_focused_window("Execute", FALSE);
37
38     client_add_destroy_notify(client_dest, NULL);
39 }
40
41 static void client_dest(ObClient *client, gpointer data)
42 {
43     GSList *it;
44
45     for (it = prompt_opts; it; it = g_slist_next(it)) {
46         Options *o = it->data;
47         if (o->data->client == client)
48             o->data->client = NULL;
49     }
50 }
51
52 static gpointer setup_func(xmlNodePtr node)
53 {
54     xmlNodePtr n;
55     Options *o;
56
57     o = g_slice_new0(Options);
58
59     if ((n = obt_xml_find_node(node, "command")) ||
60         (n = obt_xml_find_node(node, "execute")))
61     {
62         gchar *s = obt_xml_node_string(n);
63         o->cmd = obt_paths_expand_tilde(s);
64         g_free(s);
65     }
66
67     if ((n = obt_xml_find_node(node, "prompt")))
68         o->prompt = obt_xml_node_string(n);
69
70     if ((n = obt_xml_find_node(node, "startupnotify"))) {
71         xmlNodePtr m;
72         if ((m = obt_xml_find_node(n->children, "enabled")))
73             o->sn = obt_xml_node_bool(m);
74         if ((m = obt_xml_find_node(n->children, "name")))
75             o->sn_name = obt_xml_node_string(m);
76         if ((m = obt_xml_find_node(n->children, "icon")))
77             o->sn_icon = obt_xml_node_string(m);
78         if ((m = obt_xml_find_node(n->children, "wmclass")))
79             o->sn_wmclass = obt_xml_node_string(m);
80     }
81     return o;
82 }
83
84 static void shutdown_func(void)
85 {
86     client_remove_destroy_notify(client_dest);
87 }
88
89 static void free_func(gpointer options)
90 {
91     Options *o = options;
92
93     if (o) {
94         prompt_opts = g_slist_remove(prompt_opts, o);
95
96         g_free(o->cmd);
97         g_free(o->sn_name);
98         g_free(o->sn_icon);
99         g_free(o->sn_wmclass);
100         g_free(o->prompt);
101         if (o->data) g_slice_free(ObActionsData, o->data);
102         g_slice_free(Options, o);
103     }
104 }
105
106 static Options* dup_options(Options *in, ObActionsData *data)
107 {
108     Options *o = g_slice_new(Options);
109     o->cmd = g_strdup(in->cmd);
110     o->sn = in->sn;
111     o->sn_name = g_strdup(in->sn_name);
112     o->sn_icon = g_strdup(in->sn_icon);
113     o->sn_wmclass = g_strdup(in->sn_wmclass);
114     o->prompt = NULL;
115     o->data = g_slice_new(ObActionsData);
116     memcpy(o->data, data, sizeof(ObActionsData));
117     return o;
118 }
119
120 static gboolean prompt_cb(ObPrompt *p, gint result, gpointer options)
121 {
122     Options *o = options;
123     if (result)
124         run_func(o->data, o);
125     return TRUE; /* call the cleanup func */
126 }
127
128 static void prompt_cleanup(ObPrompt *p, gpointer options)
129 {
130     prompt_unref(p);
131     free_func(options);
132 }
133
134 /* Always return FALSE because its not interactive */
135 static gboolean run_func(ObActionsData *data, gpointer options)
136 {
137     GError *e;
138     gchar **argv = NULL;
139     gchar *cmd;
140     Options *o = options;
141
142     if (!o->cmd) return FALSE;
143
144     if (o->prompt) {
145         ObPrompt *p;
146         Options *ocp;
147         ObPromptAnswer answers[] = {
148             { _("No"), 0 },
149             { _("Yes"), 1 }
150         };
151
152         ocp = dup_options(options, data);
153         p = prompt_new(o->prompt, _("Execute"), answers, 2, 0, 0,
154                        prompt_cb, prompt_cleanup, ocp);
155         prompt_show(p, NULL, FALSE);
156
157         return FALSE;
158     }
159
160     cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
161     if (!cmd) {
162         g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
163         return FALSE;
164     }
165
166     if (data->client) {
167         gchar *c, *before, *expand;
168
169         /* replace occurrences of $pid and $wid */
170
171         expand = NULL;
172         before = cmd;
173
174         while ((c = strchr(before, '$'))) {
175             if ((c[1] == 'p' || c[1] == 'P') &&
176                 (c[2] == 'i' || c[2] == 'I') &&
177                 (c[3] == 'd' || c[3] == 'D') &&
178                 !g_ascii_isalnum(c[4]))
179             {
180                 /* found $pid */
181                 gchar *tmp;
182
183                 *c = '\0';
184                 tmp = expand;
185                 expand = g_strdup_printf("%s%s%u",
186                                          (expand ? expand : ""),
187                                          before,
188                                          data->client->pid);
189                 g_free(tmp);
190
191                 before = c + 4; /* 4 = strlen("$pid") */
192             }
193             else if ((c[1] == 'w' || c[1] == 'W') &&
194                      (c[2] == 'i' || c[2] == 'I') &&
195                      (c[3] == 'd' || c[3] == 'D') &&
196                      !g_ascii_isalnum(c[4]))
197             {
198                 /* found $wid */
199                 gchar *tmp;
200
201                 *c = '\0';
202                 tmp = expand;
203                 expand = g_strdup_printf("%s%s%lu",
204                                          (expand ? expand : ""),
205                                          before,
206                                          data->client->window);
207                 g_free(tmp);
208
209                 before = c + 4; /* 4 = strlen("$wid") */
210             }
211             else
212                 before = c + 1; /* no infinite loops plz */
213         }
214
215         if (expand) {
216             gchar *tmp;
217
218             /* add on the end of the string after the last replacement */
219             tmp = expand;
220             expand = g_strconcat(expand, before, NULL);
221             g_free(tmp);
222
223             /* replace the command with the expanded one */
224             g_free(cmd);
225             cmd = expand;
226         }
227     }
228
229     /* If there is a keyboard grab going on then we need to cancel
230        it so the application can grab things */
231     if (data->uact != OB_USER_ACTION_MENU_SELECTION)
232         event_cancel_all_key_grabs();
233
234     e = NULL;
235     if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
236         g_message("%s", e->message);
237         g_error_free(e);
238     }
239     else {
240         gchar *program = NULL;
241         gboolean ok;
242
243         if (o->sn) {
244             program = g_path_get_basename(argv[0]);
245             /* sets up the environment */
246             sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
247                                        o->sn_wmclass,
248                                        /* launch it on the current desktop */
249                                        screen_desktop);
250         }
251
252         e = NULL;
253         ok = g_spawn_async(NULL, argv, NULL,
254                            G_SPAWN_SEARCH_PATH |
255                            G_SPAWN_DO_NOT_REAP_CHILD,
256                            NULL, NULL, NULL, &e);
257         if (!ok) {
258             g_message("%s", e->message);
259             g_error_free(e);
260         }
261
262         if (o->sn) {
263             if (!ok) sn_spawn_cancel();
264             g_unsetenv("DESKTOP_STARTUP_ID");
265         }
266
267         g_free(program);
268         g_strfreev(argv);
269     }
270
271     g_free(cmd);
272
273     return FALSE;
274 }