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