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
);
36 actions_set_modifies_focused_window("Execute", FALSE
);
38 client_add_destroy_notify(client_dest
, NULL
);
41 static void client_dest(ObClient
*client
, gpointer data
)
45 for (it
= prompt_opts
; it
; it
= g_slist_next(it
)) {
46 Options
*o
= it
->data
;
47 if (o
->data
->client
== client
)
48 o
->data
->client
= NULL
;
52 static gpointer
setup_func(xmlNodePtr node
)
57 o
= g_slice_new0(Options
);
59 if ((n
= obt_xml_find_node(node
, "command")) ||
60 (n
= obt_xml_find_node(node
, "execute")))
62 gchar
*s
= obt_xml_node_string(n
);
63 o
->cmd
= obt_paths_expand_tilde(s
);
67 if ((n
= obt_xml_find_node(node
, "prompt")))
68 o
->prompt
= obt_xml_node_string(n
);
70 if ((n
= obt_xml_find_node(node
, "startupnotify"))) {
72 if ((m
= obt_xml_find_node(n
->children
, "enabled")))
73 o
->sn
= obt_xml_node_bool(m
);
74 if ((m
= obt_xml_find_node(n
->children
, "name")))
75 o
->sn_name
= obt_xml_node_string(m
);
76 if ((m
= obt_xml_find_node(n
->children
, "icon")))
77 o
->sn_icon
= obt_xml_node_string(m
);
78 if ((m
= obt_xml_find_node(n
->children
, "wmclass")))
79 o
->sn_wmclass
= obt_xml_node_string(m
);
84 static void shutdown_func(void)
86 client_remove_destroy_notify(client_dest
);
89 static void free_func(gpointer options
)
94 prompt_opts
= g_slist_remove(prompt_opts
, o
);
99 g_free(o
->sn_wmclass
);
101 if (o
->data
) g_slice_free(ObActionsData
, o
->data
);
102 g_slice_free(Options
, o
);
106 static Options
* dup_options(Options
*in
, ObActionsData
*data
)
108 Options
*o
= g_slice_new(Options
);
109 o
->cmd
= g_strdup(in
->cmd
);
111 o
->sn_name
= g_strdup(in
->sn_name
);
112 o
->sn_icon
= g_strdup(in
->sn_icon
);
113 o
->sn_wmclass
= g_strdup(in
->sn_wmclass
);
115 o
->data
= g_slice_new(ObActionsData
);
116 memcpy(o
->data
, data
, sizeof(ObActionsData
));
120 static gboolean
prompt_cb(ObPrompt
*p
, gint result
, gpointer options
)
122 Options
*o
= options
;
124 run_func(o
->data
, o
);
125 return TRUE
; /* call the cleanup func */
128 static void prompt_cleanup(ObPrompt
*p
, gpointer options
)
134 /* Always return FALSE because its not interactive */
135 static gboolean
run_func(ObActionsData
*data
, gpointer options
)
140 Options
*o
= options
;
142 if (!o
->cmd
) return FALSE
;
147 ObPromptAnswer answers
[] = {
152 ocp
= dup_options(options
, data
);
153 p
= prompt_new(o
->prompt
, _("Execute"), answers
, 2, 0, 0,
154 prompt_cb
, prompt_cleanup
, ocp
);
155 prompt_show(p
, NULL
, FALSE
);
160 cmd
= g_filename_from_utf8(o
->cmd
, -1, NULL
, NULL
, NULL
);
162 g_message(_("Failed to convert the path \"%s\" from utf8"), o
->cmd
);
167 gchar
*c
, *before
, *expand
;
169 /* replace occurrences of $pid and $wid */
174 while ((c
= strchr(before
, '$'))) {
175 if ((c
[1] == 'p' || c
[1] == 'P') &&
176 (c
[2] == 'i' || c
[2] == 'I') &&
177 (c
[3] == 'd' || c
[3] == 'D') &&
178 !g_ascii_isalnum(c
[4]))
185 expand
= g_strdup_printf("%s%s%u",
186 (expand
? expand
: ""),
191 before
= c
+ 4; /* 4 = strlen("$pid") */
193 else if ((c
[1] == 'w' || c
[1] == 'W') &&
194 (c
[2] == 'i' || c
[2] == 'I') &&
195 (c
[3] == 'd' || c
[3] == 'D') &&
196 !g_ascii_isalnum(c
[4]))
203 expand
= g_strdup_printf("%s%s%lu",
204 (expand
? expand
: ""),
206 data
->client
->window
);
209 before
= c
+ 4; /* 4 = strlen("$wid") */
212 before
= c
+ 1; /* no infinite loops plz */
218 /* add on the end of the string after the last replacement */
220 expand
= g_strconcat(expand
, before
, NULL
);
223 /* replace the command with the expanded one */
229 /* If there is a keyboard grab going on then we need to cancel
230 it so the application can grab things */
231 if (data
->uact
!= OB_USER_ACTION_MENU_SELECTION
)
232 event_cancel_all_key_grabs();
235 if (!g_shell_parse_argv(cmd
, NULL
, &argv
, &e
)) {
236 g_message("%s", e
->message
);
240 gchar
*program
= NULL
;
244 program
= g_path_get_basename(argv
[0]);
245 /* sets up the environment */
246 sn_setup_spawn_environment(program
, o
->sn_name
, o
->sn_icon
,
248 /* launch it on the current desktop */
253 ok
= g_spawn_async(NULL
, argv
, NULL
,
254 G_SPAWN_SEARCH_PATH
|
255 G_SPAWN_DO_NOT_REAP_CHILD
,
256 NULL
, NULL
, NULL
, &e
);
258 g_message("%s", e
->message
);
263 if (!ok
) sn_spawn_cancel();
264 g_unsetenv("DESKTOP_STARTUP_ID");