]> Dogcows Code - chaz/openbox/blob - openbox/actions/execute.c
instead of passing things to the environment, substitute $pid and $window inline...
[chaz/openbox] / 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 void prompt_cb(ObPrompt *p, gint result, gpointer options)
100 {
101 if (result)
102 run_func(NULL, options);
103
104 prompt_unref(p);
105 free_func(options);
106 }
107
108 /* Always return FALSE because its not interactive */
109 static gboolean run_func(ObActionsData *data, gpointer options)
110 {
111 GError *e = NULL;
112 gchar **argv = NULL;
113 gchar *cmd;
114 Options *o = options;
115
116 if (!o->cmd) return FALSE;
117
118 if (o->prompt) {
119 ObPrompt *p;
120 Options *ocp;
121 ObPromptAnswer answers[] = {
122 { _("No"), 0 },
123 { _("Yes"), 1 }
124 };
125
126 ocp = dup_options(options);
127 p = prompt_new(o->prompt, answers, 2, 0, 0, prompt_cb, ocp);
128 prompt_show(p, NULL, FALSE);
129
130 return FALSE;
131 }
132
133 cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
134 if (!cmd) {
135 g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
136 return FALSE;
137 }
138
139 if (data->client) {
140 gchar *c, *before, *expand;
141
142 /* replace occurances of $pid and $window */
143
144 expand = NULL;
145 before = cmd;
146
147 while ((c = strchr(before, '$'))) {
148 if ((c[1] == 'p' || c[1] == 'P') &&
149 (c[2] == 'i' || c[2] == 'I') &&
150 (c[3] == 'd' || c[3] == 'D') &&
151 !g_ascii_isalnum(c[4]))
152 {
153 /* found $pid */
154 gchar *tmp;
155
156 *c = '\0';
157 tmp = expand;
158 expand = g_strdup_printf("%s%s%u",
159 (expand ? expand : ""),
160 before,
161 data->client->pid);
162 g_free(tmp);
163
164 before = c + 4; /* 4 = strlen("$pid") */
165 }
166
167 if ((c[1] == 'w' || c[1] == 'W') &&
168 (c[2] == 'i' || c[2] == 'I') &&
169 (c[3] == 'n' || c[3] == 'N') &&
170 (c[4] == 'd' || c[4] == 'D') &&
171 (c[5] == 'o' || c[5] == 'O') &&
172 (c[6] == 'w' || c[6] == 'W') &&
173 !g_ascii_isalnum(c[7]))
174 {
175 /* found $window */
176 gchar *tmp;
177
178 *c = '\0';
179 tmp = expand;
180 expand = g_strdup_printf("%s%s%lu",
181 (expand ? expand : ""),
182 before,
183 data->client->window);
184 g_free(tmp);
185
186 before = c + 7; /* 4 = strlen("$window") */
187 }
188 }
189
190 if (expand) {
191 gchar *tmp;
192
193 /* add on the end of the string after the last replacement */
194 tmp = expand;
195 expand = g_strconcat(expand, before, NULL);
196 g_free(tmp);
197
198 /* replace the command with the expanded one */
199 g_free(cmd);
200 cmd = expand;
201 }
202 }
203
204 /* If there is a keyboard grab going on then we need to cancel
205 it so the application can grab things */
206 event_cancel_all_key_grabs();
207
208 if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
209 g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
210 g_error_free(e);
211 }
212 else {
213 gchar *program = NULL;
214
215 if (o->sn) {
216 program = g_path_get_basename(argv[0]);
217 /* sets up the environment */
218 sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
219 o->sn_wmclass,
220 /* launch it on the current desktop */
221 screen_desktop);
222 }
223
224 if (!g_spawn_async(NULL, argv, NULL,
225 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
226 NULL, NULL, NULL, &e))
227 {
228 g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
229 g_error_free(e);
230
231 if (o->sn)
232 sn_spawn_cancel();
233 }
234 if (o->sn)
235 unsetenv("DESKTOP_STARTUP_ID");
236
237 g_free(program);
238 g_strfreev(argv);
239 }
240
241 g_free(cmd);
242
243 return FALSE;
244 }
This page took 0.051212 seconds and 5 git commands to generate.