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"
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);
30 static GSList *prompt_opts = NULL;
32 void action_execute_startup(void)
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);
38 client_add_destroy_notify(client_dest, NULL);
41 static void client_dest(ObClient *client, gpointer data)
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;
52 static gpointer setup_func(xmlNodePtr node)
57 o = g_slice_new0(Options);
59 if ((n = obt_xml_find_node(node, "command")) ||
60 (n = obt_xml_find_node(node, "execute")))
62 gchar *s = obt_xml_node_string(n);
63 o->cmd = obt_paths_expand_tilde(s);
67 if ((n = obt_xml_find_node(node, "prompt")))
68 o->prompt = obt_xml_node_string(n);
70 if ((n = obt_xml_find_node(node, "startupnotify"))) {
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);
84 static void shutdown_func(void)
86 client_remove_destroy_notify(client_dest);
89 static void free_func(gpointer options)
94 prompt_opts = g_slist_remove(prompt_opts, o);
99 g_free(o->sn_wmclass);
101 if (o->data) g_slice_free(ObActionsData, o->data);
102 g_slice_free(Options, o);
106 static Options* dup_options(Options *in, ObActionsData *data)
108 Options *o = g_slice_new(Options);
109 o->cmd = g_strdup(in->cmd);
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);
115 o->data = g_slice_new(ObActionsData);
116 memcpy(o->data, data, sizeof(ObActionsData));
120 static gboolean prompt_cb(ObPrompt *p, gint result, gpointer options)
122 Options *o = options;
124 run_func(o->data, o);
125 return TRUE; /* call the cleanup func */
128 static void prompt_cleanup(ObPrompt *p, gpointer options)
134 /* Replace occurrences of $variables */
135 static gchar* expand_variables(gchar* cmd, ObActionsData* data)
137 gchar *c, *before, *expand;
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]))
153 expand = g_strdup_printf("%s%s%u",
154 (expand ? expand : ""),
156 data->client ? data->client->pid : 0);
159 before = c + 4; /* 4 = strlen("$pid") */
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]))
171 expand = g_strdup_printf("%s%s%lu",
172 (expand ? expand : ""),
174 data->client ? data->client->window : 0);
177 before = c + 4; /* 4 = strlen("$wid") */
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]))
193 expand = g_strdup_printf("%s%s%u %u",
194 (expand ? expand : ""),
199 before = c + 8; /* 4 = strlen("$pointer") */
202 /* found something unknown, copy the $ and continue */
207 expand = g_strdup_printf("%s%s$",
208 (expand ? expand : ""),
212 before = c + 1; /* 1 = strlen("$") */
219 /* add on the end of the string after the last replacement */
221 expand = g_strconcat(expand, before, NULL);
224 /* replace the command with the expanded one */
231 /* Always return FALSE because its not interactive */
232 static gboolean run_func(ObActionsData *data, gpointer options)
237 Options *o = options;
239 if (!o->cmd) return FALSE;
244 ObPromptAnswer answers[] = {
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);
257 cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
259 g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
263 cmd = expand_variables(cmd, data);
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();
271 if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
272 g_message("%s", e->message);
276 gchar *program = NULL;
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,
284 /* launch it on the current desktop */
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);
294 g_message("%s", e->message);
299 if (!ok) sn_spawn_cancel();
300 g_unsetenv("DESKTOP_STARTUP_ID");