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_slice_new0(Options
);
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_slice_free(ObActionsData
, o
->data
);
101 g_slice_free(Options
, o
);
105 static Options
* dup_options(Options
*in
, ObActionsData
*data
)
107 Options
*o
= g_slice_new(Options
);
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_slice_new(ObActionsData
);
115 memcpy(o
->data
, data
, sizeof(ObActionsData
));
119 static gboolean
prompt_cb(ObPrompt
*p
, gint result
, gpointer options
)
121 Options
*o
= options
;
123 run_func(o
->data
, o
);
124 return TRUE
; /* call the cleanup func */
127 static void prompt_cleanup(ObPrompt
*p
, gpointer options
)
133 /* Always return FALSE because its not interactive */
134 static gboolean
run_func(ObActionsData
*data
, gpointer options
)
139 Options
*o
= options
;
141 if (!o
->cmd
) return FALSE
;
146 ObPromptAnswer answers
[] = {
151 ocp
= dup_options(options
, data
);
152 p
= prompt_new(o
->prompt
, _("Execute"), answers
, 2, 0, 0,
153 prompt_cb
, prompt_cleanup
, ocp
);
154 prompt_show(p
, NULL
, FALSE
);
159 cmd
= g_filename_from_utf8(o
->cmd
, -1, NULL
, NULL
, NULL
);
161 g_message(_("Failed to convert the path \"%s\" from utf8"), o
->cmd
);
166 gchar
*c
, *before
, *expand
;
168 /* replace occurrences of $pid and $wid */
173 while ((c
= strchr(before
, '$'))) {
174 if ((c
[1] == 'p' || c
[1] == 'P') &&
175 (c
[2] == 'i' || c
[2] == 'I') &&
176 (c
[3] == 'd' || c
[3] == 'D') &&
177 !g_ascii_isalnum(c
[4]))
184 expand
= g_strdup_printf("%s%s%u",
185 (expand
? expand
: ""),
190 before
= c
+ 4; /* 4 = strlen("$pid") */
192 else if ((c
[1] == 'w' || c
[1] == 'W') &&
193 (c
[2] == 'i' || c
[2] == 'I') &&
194 (c
[3] == 'd' || c
[3] == 'D') &&
195 !g_ascii_isalnum(c
[4]))
202 expand
= g_strdup_printf("%s%s%lu",
203 (expand
? expand
: ""),
205 data
->client
->window
);
208 before
= c
+ 4; /* 4 = strlen("$wid") */
211 before
= c
+ 1; /* no infinite loops plz */
217 /* add on the end of the string after the last replacement */
219 expand
= g_strconcat(expand
, before
, NULL
);
222 /* replace the command with the expanded one */
228 /* If there is a keyboard grab going on then we need to cancel
229 it so the application can grab things */
230 if (data
->uact
!= OB_USER_ACTION_MENU_SELECTION
)
231 event_cancel_all_key_grabs();
234 if (!g_shell_parse_argv(cmd
, NULL
, &argv
, &e
)) {
235 g_message("%s", e
->message
);
239 gchar
*program
= NULL
;
243 program
= g_path_get_basename(argv
[0]);
244 /* sets up the environment */
245 sn_setup_spawn_environment(program
, o
->sn_name
, o
->sn_icon
,
247 /* launch it on the current desktop */
252 ok
= g_spawn_async(NULL
, argv
, NULL
,
253 G_SPAWN_SEARCH_PATH
|
254 G_SPAWN_DO_NOT_REAP_CHILD
,
255 NULL
, NULL
, NULL
, &e
);
257 g_message("%s", e
->message
);
262 if (!ok
) sn_spawn_cancel();
263 g_unsetenv("DESKTOP_STARTUP_ID");