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 occurances 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") */
172 if ((c
[1] == 'w' || c
[1] == 'W') &&
173 (c
[2] == 'i' || c
[2] == 'I') &&
174 (c
[3] == 'd' || c
[3] == 'D') &&
175 !g_ascii_isalnum(c
[4]))
182 expand
= g_strdup_printf("%s%s%lu",
183 (expand
? expand
: ""),
185 data
->client
->window
);
188 before
= c
+ 4; /* 4 = strlen("$wid") */
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 event_cancel_all_key_grabs();
211 if (!g_shell_parse_argv(cmd
, NULL
, &argv
, &e
)) {
212 g_message(e
->message
, o
->cmd
);
216 gchar
*program
= NULL
;
220 program
= g_path_get_basename(argv
[0]);
221 /* sets up the environment */
222 sn_setup_spawn_environment(program
, o
->sn_name
, o
->sn_icon
,
224 /* launch it on the current desktop */
229 ok
= g_spawn_async(NULL
, argv
, NULL
,
230 G_SPAWN_SEARCH_PATH
|
231 G_SPAWN_DO_NOT_REAP_CHILD
,
232 NULL
, NULL
, NULL
, &e
);
234 g_message(e
->message
, o
->cmd
);
239 if (!ok
) sn_spawn_cancel();
240 unsetenv("DESKTOP_STARTUP_ID");