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"
23 static gpointer setup_func(xmlNodePtr node);
24 static void free_func(gpointer options);
25 static gboolean run_func(ObActionsData *data, gpointer options);
27 static gboolean i_input_func(guint initial_state,
31 static void i_cancel_func(gpointer options);
34 void action_execute_startup(void)
36 actions_register("Execute", setup_func, free_func, run_func, NULL, NULL);
39 static gpointer setup_func(xmlNodePtr node)
44 o = g_new0(Options, 1);
46 if ((n = obt_parse_find_node(node, "command")) ||
47 (n = obt_parse_find_node(node, "execute")))
49 gchar *s = obt_parse_node_string(n);
50 o->cmd = obt_paths_expand_tilde(s);
54 if ((n = obt_parse_find_node(node, "prompt")))
55 o->prompt = obt_parse_node_string(n);
57 if ((n = obt_parse_find_node(node, "startupnotify"))) {
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);
71 static void free_func(gpointer options)
79 g_free(o->sn_wmclass);
85 static Options* dup_options(Options *in)
87 Options *o = g_new(Options, 1);
88 o->cmd = g_strdup(in->cmd);
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);
97 static gboolean run_func(ObActionsData *data, gpointer options);
99 static gboolean prompt_cb(ObPrompt *p, gint result, gpointer options)
102 run_func(NULL, options);
103 return TRUE; /* call the cleanup func */
106 static void prompt_cleanup(ObPrompt *p, gpointer options)
112 /* Always return FALSE because its not interactive */
113 static gboolean run_func(ObActionsData *data, gpointer options)
118 Options *o = options;
120 if (!o->cmd) return FALSE;
125 ObPromptAnswer answers[] = {
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);
138 cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
140 g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
145 gchar *c, *before, *expand;
147 /* replace occurrences of $pid and $wid */
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]))
163 expand = g_strdup_printf("%s%s%u",
164 (expand ? expand : ""),
169 before = c + 4; /* 4 = strlen("$pid") */
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]))
181 expand = g_strdup_printf("%s%s%lu",
182 (expand ? expand : ""),
184 data->client->window);
187 before = c + 4; /* 4 = strlen("$wid") */
190 before = c + 1; /* no infinite loops plz */
196 /* add on the end of the string after the last replacement */
198 expand = g_strconcat(expand, before, NULL);
201 /* replace the command with the expanded one */
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();
212 if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
213 g_message(e->message, o->cmd);
217 gchar *program = NULL;
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,
225 /* launch it on the current desktop */
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);
235 g_message(e->message, o->cmd);
240 if (!ok) sn_spawn_cancel();
241 unsetenv("DESKTOP_STARTUP_ID");