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