]> Dogcows Code - chaz/openbox/blob - openbox/actions/execute.c
make it $pid and $wid, it's a little faster this way and they look the same so why not
[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;
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] == 'd' || c[3] == 'D') &&
170 !g_ascii_isalnum(c[7]))
171 {
172 /* found $window */
173 gchar *tmp;
174
175 *c = '\0';
176 tmp = expand;
177 expand = g_strdup_printf("%s%s%lu",
178 (expand ? expand : ""),
179 before,
180 data->client->window);
181 g_free(tmp);
182
183 before = c + 7; /* 4 = strlen("$window") */
184 }
185 }
186
187 if (expand) {
188 gchar *tmp;
189
190 /* add on the end of the string after the last replacement */
191 tmp = expand;
192 expand = g_strconcat(expand, before, NULL);
193 g_free(tmp);
194
195 /* replace the command with the expanded one */
196 g_free(cmd);
197 cmd = expand;
198 }
199 }
200
201 /* If there is a keyboard grab going on then we need to cancel
202 it so the application can grab things */
203 event_cancel_all_key_grabs();
204
205 e = NULL;
206 if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
207 g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
208 g_error_free(e);
209 }
210 else {
211 gchar *program = NULL;
212 gboolean ok;
213
214 if (o->sn) {
215 program = g_path_get_basename(argv[0]);
216 /* sets up the environment */
217 sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
218 o->sn_wmclass,
219 /* launch it on the current desktop */
220 screen_desktop);
221 }
222
223 e = NULL;
224 ok = g_spawn_async(NULL, argv, NULL,
225 G_SPAWN_SEARCH_PATH |
226 G_SPAWN_DO_NOT_REAP_CHILD,
227 NULL, NULL, NULL, &e);
228 if (!ok) {
229 g_message(_("Failed to execute \"%s\": %s"),
230 o->cmd, e->message);
231 g_error_free(e);
232 }
233
234 if (o->sn) {
235 if (!ok) sn_spawn_cancel();
236 unsetenv("DESKTOP_STARTUP_ID");
237 }
238
239 g_free(program);
240 g_strfreev(argv);
241 }
242
243 g_free(cmd);
244
245 return FALSE;
246 }
This page took 0.044299 seconds and 5 git commands to generate.