]> Dogcows Code - chaz/openbox/blob - openbox/actions/execute.c
Allow non-interactive focus cycling.
[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 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 /* Replace occurrences of $variables */
135 static gchar* expand_variables(gchar* cmd, ObActionsData* data)
136 {
137 gchar *c, *before, *expand;
138
139 expand = NULL;
140 before = cmd;
141
142 while ((c = strchr(before, '$'))) {
143 if ((c[1] == 'p' || c[1] == 'P') &&
144 (c[2] == 'i' || c[2] == 'I') &&
145 (c[3] == 'd' || c[3] == 'D') &&
146 !g_ascii_isalnum(c[4]))
147 {
148 /* found $pid */
149 gchar *tmp;
150
151 *c = '\0';
152 tmp = expand;
153 expand = g_strdup_printf("%s%s%u",
154 (expand ? expand : ""),
155 before,
156 data->client ? data->client->pid : 0);
157 g_free(tmp);
158
159 before = c + 4; /* 4 = strlen("$pid") */
160 }
161 else if ((c[1] == 'w' || c[1] == 'W') &&
162 (c[2] == 'i' || c[2] == 'I') &&
163 (c[3] == 'd' || c[3] == 'D') &&
164 !g_ascii_isalnum(c[4]))
165 {
166 /* found $wid */
167 gchar *tmp;
168
169 *c = '\0';
170 tmp = expand;
171 expand = g_strdup_printf("%s%s%lu",
172 (expand ? expand : ""),
173 before,
174 data->client ? data->client->window : 0);
175 g_free(tmp);
176
177 before = c + 4; /* 4 = strlen("$wid") */
178 }
179 else if ((c[1] == 'p' || c[1] == 'P') &&
180 (c[2] == 'o' || c[2] == 'O') &&
181 (c[3] == 'i' || c[3] == 'I') &&
182 (c[4] == 'n' || c[4] == 'N') &&
183 (c[5] == 't' || c[5] == 'T') &&
184 (c[6] == 'e' || c[6] == 'E') &&
185 (c[7] == 'r' || c[7] == 'R') &&
186 !g_ascii_isalnum(c[8]))
187 {
188 /* found $pointer */
189 gchar *tmp;
190
191 *c = '\0';
192 tmp = expand;
193 expand = g_strdup_printf("%s%s%u %u",
194 (expand ? expand : ""),
195 before,
196 data->x, data->y);
197 g_free(tmp);
198
199 before = c + 8; /* 4 = strlen("$pointer") */
200 }
201 else {
202 /* found something unknown, copy the $ and continue */
203 gchar *tmp;
204
205 *c = '\0';
206 tmp = expand;
207 expand = g_strdup_printf("%s%s$",
208 (expand ? expand : ""),
209 before);
210 g_free(tmp);
211
212 before = c + 1; /* 1 = strlen("$") */
213 }
214 }
215
216 if (expand) {
217 gchar *tmp;
218
219 /* add on the end of the string after the last replacement */
220 tmp = expand;
221 expand = g_strconcat(expand, before, NULL);
222 g_free(tmp);
223
224 /* replace the command with the expanded one */
225 g_free(cmd);
226 cmd = expand;
227 }
228 return cmd;
229 }
230
231 /* Always return FALSE because its not interactive */
232 static gboolean run_func(ObActionsData *data, gpointer options)
233 {
234 GError *e;
235 gchar **argv = NULL;
236 gchar *cmd;
237 Options *o = options;
238
239 if (!o->cmd) return FALSE;
240
241 if (o->prompt) {
242 ObPrompt *p;
243 Options *ocp;
244 ObPromptAnswer answers[] = {
245 { _("No"), 0 },
246 { _("Yes"), 1 }
247 };
248
249 ocp = dup_options(options, data);
250 p = prompt_new(o->prompt, _("Execute"), answers, 2, 0, 0,
251 prompt_cb, prompt_cleanup, ocp);
252 prompt_show(p, NULL, FALSE);
253
254 return FALSE;
255 }
256
257 cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
258 if (!cmd) {
259 g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
260 return FALSE;
261 }
262
263 cmd = expand_variables(cmd, data);
264
265 /* If there is a keyboard grab going on then we need to cancel
266 it so the application can grab things */
267 if (data->uact != OB_USER_ACTION_MENU_SELECTION)
268 event_cancel_all_key_grabs();
269
270 e = NULL;
271 if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
272 g_message("%s", e->message);
273 g_error_free(e);
274 }
275 else {
276 gchar *program = NULL;
277 gboolean ok;
278
279 if (o->sn) {
280 program = g_path_get_basename(argv[0]);
281 /* sets up the environment */
282 sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
283 o->sn_wmclass,
284 /* launch it on the current desktop */
285 screen_desktop);
286 }
287
288 e = NULL;
289 ok = g_spawn_async(NULL, argv, NULL,
290 G_SPAWN_SEARCH_PATH |
291 G_SPAWN_DO_NOT_REAP_CHILD,
292 NULL, NULL, NULL, &e);
293 if (!ok) {
294 g_message("%s", e->message);
295 g_error_free(e);
296 }
297
298 if (o->sn) {
299 if (!ok) sn_spawn_cancel();
300 g_unsetenv("DESKTOP_STARTUP_ID");
301 }
302
303 g_free(program);
304 g_strfreev(argv);
305 }
306
307 g_free(cmd);
308
309 return FALSE;
310 }
This page took 0.05131 seconds and 4 git commands to generate.