]> Dogcows Code - chaz/openbox/blob - openbox/actions/execute.c
rename the obt_parse library to obt_xml (since it is very xml specific)
[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);
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_xml_find_node(node, "command")) ||
47 (n = obt_xml_find_node(node, "execute")))
48 {
49 gchar *s = obt_xml_node_string(n);
50 o->cmd = obt_paths_expand_tilde(s);
51 g_free(s);
52 }
53
54 if ((n = obt_xml_find_node(node, "prompt")))
55 o->prompt = obt_xml_node_string(n);
56
57 if ((n = obt_xml_find_node(node, "startupnotify"))) {
58 xmlNodePtr m;
59 if ((m = obt_xml_find_node(n->children, "enabled")))
60 o->sn = obt_xml_node_bool(m);
61 if ((m = obt_xml_find_node(n->children, "name")))
62 o->sn_name = obt_xml_node_string(m);
63 if ((m = obt_xml_find_node(n->children, "icon")))
64 o->sn_icon = obt_xml_node_string(m);
65 if ((m = obt_xml_find_node(n->children, "wmclass")))
66 o->sn_wmclass = obt_xml_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 occurrences of $pid and $wid */
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 else if ((c[1] == 'w' || c[1] == 'W') &&
172 (c[2] == 'i' || c[2] == 'I') &&
173 (c[3] == 'd' || c[3] == 'D') &&
174 !g_ascii_isalnum(c[4]))
175 {
176 /* found $wid */
177 gchar *tmp;
178
179 *c = '\0';
180 tmp = expand;
181 expand = g_strdup_printf("%s%s%lu",
182 (expand ? expand : ""),
183 before,
184 data->client->window);
185 g_free(tmp);
186
187 before = c + 4; /* 4 = strlen("$wid") */
188 }
189 else
190 before = c + 1; /* no infinite loops plz */
191 }
192
193 if (expand) {
194 gchar *tmp;
195
196 /* add on the end of the string after the last replacement */
197 tmp = expand;
198 expand = g_strconcat(expand, before, NULL);
199 g_free(tmp);
200
201 /* replace the command with the expanded one */
202 g_free(cmd);
203 cmd = expand;
204 }
205 }
206
207 /* If there is a keyboard grab going on then we need to cancel
208 it so the application can grab things */
209 event_cancel_all_key_grabs();
210
211 e = NULL;
212 if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
213 g_message(e->message, o->cmd);
214 g_error_free(e);
215 }
216 else {
217 gchar *program = NULL;
218 gboolean ok;
219
220 if (o->sn) {
221 program = g_path_get_basename(argv[0]);
222 /* sets up the environment */
223 sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
224 o->sn_wmclass,
225 /* launch it on the current desktop */
226 screen_desktop);
227 }
228
229 e = NULL;
230 ok = g_spawn_async(NULL, argv, NULL,
231 G_SPAWN_SEARCH_PATH |
232 G_SPAWN_DO_NOT_REAP_CHILD,
233 NULL, NULL, NULL, &e);
234 if (!ok) {
235 g_message(e->message, o->cmd);
236 g_error_free(e);
237 }
238
239 if (o->sn) {
240 if (!ok) sn_spawn_cancel();
241 unsetenv("DESKTOP_STARTUP_ID");
242 }
243
244 g_free(program);
245 g_strfreev(argv);
246 }
247
248 g_free(cmd);
249
250 return FALSE;
251 }
This page took 0.047585 seconds and 4 git commands to generate.