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