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
);
37 client_add_destroy_notify(client_dest
, NULL
);
40 static void client_dest(ObClient
*client
, gpointer data
)
44 for (it
= prompt_opts
; it
; it
= g_slist_next(it
)) {
45 Options
*o
= it
->data
;
46 if (o
->data
->client
== client
)
47 o
->data
->client
= NULL
;
51 static gpointer
setup_func(xmlNodePtr node
)
56 o
= g_new0(Options
, 1);
58 if ((n
= obt_xml_find_node(node
, "command")) ||
59 (n
= obt_xml_find_node(node
, "execute")))
61 gchar
*s
= obt_xml_node_string(n
);
62 o
->cmd
= obt_paths_expand_tilde(s
);
66 if ((n
= obt_xml_find_node(node
, "prompt")))
67 o
->prompt
= obt_xml_node_string(n
);
69 if ((n
= obt_xml_find_node(node
, "startupnotify"))) {
71 if ((m
= obt_xml_find_node(n
->children
, "enabled")))
72 o
->sn
= obt_xml_node_bool(m
);
73 if ((m
= obt_xml_find_node(n
->children
, "name")))
74 o
->sn_name
= obt_xml_node_string(m
);
75 if ((m
= obt_xml_find_node(n
->children
, "icon")))
76 o
->sn_icon
= obt_xml_node_string(m
);
77 if ((m
= obt_xml_find_node(n
->children
, "wmclass")))
78 o
->sn_wmclass
= obt_xml_node_string(m
);
83 static void shutdown_func(void)
85 client_remove_destroy_notify(client_dest
);
88 static void free_func(gpointer options
)
93 prompt_opts
= g_slist_remove(prompt_opts
, o
);
98 g_free(o
->sn_wmclass
);
100 if (o
->data
) g_free(o
->data
);
105 static Options
* dup_options(Options
*in
, ObActionsData
*data
)
107 Options
*o
= g_new(Options
, 1);
108 o
->cmd
= g_strdup(in
->cmd
);
110 o
->sn_name
= g_strdup(in
->sn_name
);
111 o
->sn_icon
= g_strdup(in
->sn_icon
);
112 o
->sn_wmclass
= g_strdup(in
->sn_wmclass
);
114 o
->data
= g_memdup(data
, sizeof(ObActionsData
));
118 static gboolean
prompt_cb(ObPrompt
*p
, gint result
, gpointer options
)
120 Options
*o
= options
;
122 run_func(o
->data
, o
);
123 return TRUE
; /* call the cleanup func */
126 static void prompt_cleanup(ObPrompt
*p
, gpointer options
)
132 /* Always return FALSE because its not interactive */
133 static gboolean
run_func(ObActionsData
*data
, gpointer options
)
138 Options
*o
= options
;
140 if (!o
->cmd
) return FALSE
;
145 ObPromptAnswer answers
[] = {
150 ocp
= dup_options(options
, data
);
151 p
= prompt_new(o
->prompt
, _("Execute"), answers
, 2, 0, 0,
152 prompt_cb
, prompt_cleanup
, ocp
);
153 prompt_show(p
, NULL
, FALSE
);
158 cmd
= g_filename_from_utf8(o
->cmd
, -1, NULL
, NULL
, NULL
);
160 g_message(_("Failed to convert the path \"%s\" from utf8"), o
->cmd
);
165 gchar
*c
, *before
, *expand
;
167 /* replace occurrences of $pid and $wid */
172 while ((c
= strchr(before
, '$'))) {
173 if ((c
[1] == 'p' || c
[1] == 'P') &&
174 (c
[2] == 'i' || c
[2] == 'I') &&
175 (c
[3] == 'd' || c
[3] == 'D') &&
176 !g_ascii_isalnum(c
[4]))
183 expand
= g_strdup_printf("%s%s%u",
184 (expand
? expand
: ""),
189 before
= c
+ 4; /* 4 = strlen("$pid") */
191 else if ((c
[1] == 'w' || c
[1] == 'W') &&
192 (c
[2] == 'i' || c
[2] == 'I') &&
193 (c
[3] == 'd' || c
[3] == 'D') &&
194 !g_ascii_isalnum(c
[4]))
201 expand
= g_strdup_printf("%s%s%lu",
202 (expand
? expand
: ""),
204 data
->client
->window
);
207 before
= c
+ 4; /* 4 = strlen("$wid") */
210 before
= c
+ 1; /* no infinite loops plz */
216 /* add on the end of the string after the last replacement */
218 expand
= g_strconcat(expand
, before
, NULL
);
221 /* replace the command with the expanded one */
227 /* If there is a keyboard grab going on then we need to cancel
228 it so the application can grab things */
229 if (data
->uact
!= OB_USER_ACTION_MENU_SELECTION
)
230 event_cancel_all_key_grabs();
233 if (!g_shell_parse_argv(cmd
, NULL
, &argv
, &e
)) {
234 g_message(e
->message
, o
->cmd
);
238 gchar
*program
= NULL
;
242 program
= g_path_get_basename(argv
[0]);
243 /* sets up the environment */
244 sn_setup_spawn_environment(program
, o
->sn_name
, o
->sn_icon
,
246 /* launch it on the current desktop */
251 ok
= g_spawn_async(NULL
, argv
, NULL
,
252 G_SPAWN_SEARCH_PATH
|
253 G_SPAWN_DO_NOT_REAP_CHILD
,
254 NULL
, NULL
, NULL
, &e
);
256 g_message(e
->message
, o
->cmd
);
261 if (!ok
) sn_spawn_cancel();
262 unsetenv("DESKTOP_STARTUP_ID");