1 #include "openbox/action.h"
2 #include "openbox/action_list_run.h"
3 #include "openbox/config_value.h"
4 #include "openbox/client.h"
5 #include "openbox/client_set.h"
6 #include "openbox/event.h"
7 #include "openbox/prompt.h"
8 #include "openbox/screen.h"
9 #include "openbox/startupnotify.h"
10 #include "obt/paths.h"
25 /* for the prompt callback */
26 ObActionListRun *data;
30 static gpointer setup_func(GHashTable *config);
31 static void free_func(gpointer options);
32 static gboolean run_func(const ObClientSet *set,
33 const ObActionListRun *data, gpointer options);
34 static void shutdown_func(void);
35 static void client_dest(ObClient *client, gpointer data);
37 static GSList *prompt_opts = NULL;
39 void action_execute_startup(void)
41 action_register("Execute", OB_ACTION_DEFAULT_FILTER_SINGLE,
42 setup_func, free_func, run_func);
43 action_set_shutdown("Execute", shutdown_func);
45 client_add_destroy_notify(client_dest, NULL);
48 static void client_dest(ObClient *client, gpointer data)
52 for (it = prompt_opts; it; it = g_slist_next(it)) {
53 Options *o = it->data;
54 if (o->data->target == client)
55 o->data->target = NULL;
59 static gpointer setup_func(GHashTable *config)
64 o = g_slice_new0(Options);
66 v = g_hash_table_lookup(config, "command");
67 if (v && config_value_is_string(v))
68 o->cmd = obt_paths_expand_tilde(config_value_string(v));
70 v = g_hash_table_lookup(config, "prompt");
71 if (v && config_value_is_string(v))
72 o->prompt = g_strdup(config_value_string(v));
74 v = g_hash_table_lookup(config, "startupnotify");
75 if (v && config_value_is_string(v) && config_value_bool(v)) {
77 v = g_hash_table_lookup(config, "name");
78 if (v && config_value_is_string(v))
79 o->sn_name = g_strdup(config_value_string(v));
80 v = g_hash_table_lookup(config, "icon");
81 if (v && config_value_is_string(v))
82 o->sn_icon = g_strdup(config_value_string(v));
83 v = g_hash_table_lookup(config, "wmclass");
84 if (v && config_value_is_string(v))
85 o->sn_wmclass = g_strdup(config_value_string(v));
90 static void shutdown_func(void)
92 client_remove_destroy_notify(client_dest);
95 static void free_func(gpointer options)
100 prompt_opts = g_slist_remove(prompt_opts, o);
105 g_free(o->sn_wmclass);
107 if (o->set) client_set_destroy(o->set);
108 if (o->data) g_slice_free(ObActionListRun, o->data);
109 g_slice_free(Options, o);
113 static Options* dup_options(Options *in, const ObClientSet *set,
114 const ObActionListRun *data)
116 Options *o = g_slice_new(Options);
117 o->cmd = g_strdup(in->cmd);
119 o->sn_name = g_strdup(in->sn_name);
120 o->sn_icon = g_strdup(in->sn_icon);
121 o->sn_wmclass = g_strdup(in->sn_wmclass);
123 o->set = client_set_clone(set);
124 o->data = g_slice_new(ObActionListRun);
125 memcpy(o->data, data, sizeof(ObActionListRun));
129 static gboolean do_execute_each(ObClient *client,
130 const ObActionListRun *data, gpointer options)
132 Options *o = options;
137 cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
139 g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
144 gchar *c, *before, *expand;
146 /* replace occurrences of $pid and $wid */
151 while ((c = strchr(before, '$'))) {
152 if ((c[1] == 'p' || c[1] == 'P') &&
153 (c[2] == 'i' || c[2] == 'I') &&
154 (c[3] == 'd' || c[3] == 'D') &&
155 !g_ascii_isalnum(c[4]))
162 expand = g_strdup_printf("%s%s%u",
163 (expand ? expand : ""),
168 before = c + 4; /* 4 = strlen("$pid") */
170 else if ((c[1] == 'w' || c[1] == 'W') &&
171 (c[2] == 'i' || c[2] == 'I') &&
172 (c[3] == 'd' || c[3] == 'D') &&
173 !g_ascii_isalnum(c[4]))
180 expand = g_strdup_printf("%s%s%lu",
181 (expand ? expand : ""),
186 before = c + 4; /* 4 = strlen("$wid") */
189 before = c + 1; /* no infinite loops plz */
195 /* add on the end of the string after the last replacement */
197 expand = g_strconcat(expand, before, NULL);
200 /* replace the command with the expanded one */
206 /* If there is a keyboard grab going on then we need to cancel
207 it so the application can grab things */
208 if (data->user_act != OB_USER_ACTION_MENU_SELECTION)
209 event_cancel_all_key_grabs();
212 if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
213 g_message("%s", e->message);
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("%s", e->message);
240 if (!ok) sn_spawn_cancel();
241 g_unsetenv("DESKTOP_STARTUP_ID");
253 /* Always return FALSE because its not interactive */
254 static gboolean do_execute(const ObClientSet *set,
255 const ObActionListRun *data, gpointer options)
257 if (client_set_is_empty(set))
258 do_execute_each(NULL, data, options);
260 client_set_run(set, data, do_execute_each, options);
264 static gboolean prompt_cb(ObPrompt *p, gint result, gpointer options)
266 Options *o = options;
268 run_func(o->set, o->data, o);
269 return TRUE; /* call the cleanup func */
272 static void prompt_cleanup(ObPrompt *p, gpointer options)
278 /* Always return FALSE because its not interactive */
279 static gboolean run_func(const ObClientSet *set,
280 const ObActionListRun *data, gpointer options)
282 Options *o = options;
284 if (!o->cmd) return FALSE;
289 ObPromptAnswer answers[] = {
294 ocp = dup_options(options, set, data);
295 p = prompt_new(o->prompt, _("Execute"), answers, 2, 0, 0,
296 prompt_cb, prompt_cleanup, ocp);
297 prompt_show(p, NULL, FALSE);
302 return do_execute(set, data, options);