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 void prompt_cb(ObPrompt
*p
, gint result
, gpointer options
)
102 run_func(NULL
, options
);
108 /* Always return FALSE because its not interactive */
109 static gboolean
run_func(ObActionsData
*data
, gpointer options
)
114 Options
*o
= options
;
116 if (!o
->cmd
) return FALSE
;
121 ObPromptAnswer answers
[] = {
126 ocp
= dup_options(options
);
127 p
= prompt_new(o
->prompt
, answers
, 2, 0, 0, prompt_cb
, ocp
);
128 prompt_show(p
, NULL
, FALSE
);
133 cmd
= g_filename_from_utf8(o
->cmd
, -1, NULL
, NULL
, NULL
);
135 g_message(_("Failed to convert the path \"%s\" from utf8"), o
->cmd
);
140 gchar
*c
, *before
, *expand
;
142 /* replace occurances of $pid and $window */
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]))
158 expand
= g_strdup_printf("%s%s%u",
159 (expand
? expand
: ""),
164 before
= c
+ 4; /* 4 = strlen("$pid") */
167 if ((c
[1] == 'w' || c
[1] == 'W') &&
168 (c
[2] == 'i' || c
[2] == 'I') &&
169 (c
[3] == 'n' || c
[3] == 'N') &&
170 (c
[4] == 'd' || c
[4] == 'D') &&
171 (c
[5] == 'o' || c
[5] == 'O') &&
172 (c
[6] == 'w' || c
[6] == 'W') &&
173 !g_ascii_isalnum(c
[7]))
180 expand
= g_strdup_printf("%s%s%lu",
181 (expand
? expand
: ""),
183 data
->client
->window
);
186 before
= c
+ 7; /* 4 = strlen("$window") */
193 /* add on the end of the string after the last replacement */
195 expand
= g_strconcat(expand
, before
, NULL
);
198 /* replace the command with the expanded one */
204 /* If there is a keyboard grab going on then we need to cancel
205 it so the application can grab things */
206 event_cancel_all_key_grabs();
209 if (!g_shell_parse_argv(cmd
, NULL
, &argv
, &e
)) {
210 g_message(_("Failed to execute \"%s\": %s"), o
->cmd
, e
->message
);
214 gchar
*program
= NULL
;
218 program
= g_path_get_basename(argv
[0]);
219 /* sets up the environment */
220 sn_setup_spawn_environment(program
, o
->sn_name
, o
->sn_icon
,
222 /* launch it on the current desktop */
227 ok
= g_spawn_async(NULL
, argv
, NULL
,
228 G_SPAWN_SEARCH_PATH
|
229 G_SPAWN_DO_NOT_REAP_CHILD
,
230 NULL
, NULL
, NULL
, &e
);
232 g_message(_("Failed to execute \"%s\": %s"),
238 if (!ok
) sn_spawn_cancel();
239 unsetenv("DESKTOP_STARTUP_ID");