Merge branch 'backport' into work
[dana/openbox.git] / openbox / actions / execute.c
1 #include "openbox/actions.h"
2 #include "openbox/event.h"
3 #include "openbox/startupnotify.h"
4 #include "openbox/prompt.h"
5 #include "openbox/screen.h"
6 #include "obt/paths.h"
7 #include "gettext.h"
8
9 #ifdef HAVE_STDLIB_H
10 #  include <stdlib.h>
11 #endif
12
13 typedef struct {
14     gchar   *cmd;
15     gboolean sn;
16     gchar   *sn_name;
17     gchar   *sn_icon;
18     gchar   *sn_wmclass;
19     gchar   *prompt;
20 } Options;
21
22 static gpointer setup_func(xmlNodePtr node);
23 static void     free_func(gpointer options);
24 static gboolean run_func(ObActionsData *data, gpointer options);
25 /*
26 static gboolean i_input_func(guint initial_state,
27                              XEvent *e,
28                              gpointer options,
29                              gboolean *used);
30 static void     i_cancel_func(gpointer options);
31 */
32
33 void action_execute_startup(void)
34 {
35     actions_register("Execute", setup_func, free_func, run_func, NULL, NULL);
36 }
37
38 static gpointer setup_func(xmlNodePtr node)
39 {
40     xmlNodePtr n;
41     Options *o;
42
43     o = g_new0(Options, 1);
44
45     if ((n = obt_parse_find_node(node, "command")) ||
46         (n = obt_parse_find_node(node, "execute")))
47     {
48         gchar *s = obt_parse_node_string(n);
49         o->cmd = obt_paths_expand_tilde(s);
50         g_free(s);
51     }
52
53     if ((n = obt_parse_find_node(node, "prompt")))
54         o->prompt = obt_parse_node_string(n);
55
56     if ((n = obt_parse_find_node(node, "startupnotify"))) {
57         xmlNodePtr m;
58         if ((m = obt_parse_find_node(n->children, "enabled")))
59             o->sn = obt_parse_node_bool(m);
60         if ((m = obt_parse_find_node(n->children, "name")))
61             o->sn_name = obt_parse_node_string(m);
62         if ((m = obt_parse_find_node(n->children, "icon")))
63             o->sn_icon = obt_parse_node_string(m);
64         if ((m = obt_parse_find_node(n->children, "wmclass")))
65             o->sn_wmclass = obt_parse_node_string(m);
66     }
67     return o;
68 }
69
70 static void free_func(gpointer options)
71 {
72     Options *o = options;
73
74     if (o) {
75         g_free(o->cmd);
76         g_free(o->sn_name);
77         g_free(o->sn_icon);
78         g_free(o->sn_wmclass);
79         g_free(o->prompt);
80         g_free(o);
81     }
82 }
83
84 static Options* dup_options(Options *in)
85 {
86     Options *o = g_new(Options, 1);
87     o->cmd = g_strdup(in->cmd);
88     o->sn = in->sn;
89     o->sn_name = g_strdup(in->sn_name);
90     o->sn_icon = g_strdup(in->sn_icon);
91     o->sn_wmclass = g_strdup(in->sn_wmclass);
92     o->prompt = NULL;
93     return o;
94 }
95
96 static gboolean run_func(ObActionsData *data, gpointer options);
97
98 static void prompt_cb(ObPrompt *p, gint result, gpointer options)
99 {
100     if (result)
101         run_func(NULL, options);
102
103     prompt_unref(p);
104     free_func(options);
105 }
106
107 /* Always return FALSE because its not interactive */
108 static gboolean run_func(ObActionsData *data, gpointer options)
109 {
110     GError *e = NULL;
111     gchar **argv = NULL;
112     gchar *cmd;
113     Options *o = options;
114
115     if (!o->cmd) return FALSE;
116
117     if (o->prompt) {
118         ObPrompt *p;
119         Options *ocp;
120         ObPromptAnswer answers[] = {
121             { _("No"), 0 },
122             { _("Yes"), 1 }
123         };
124
125         ocp = dup_options(options);
126         p = prompt_new(o->prompt, answers, 2, 0, 0, prompt_cb, ocp);
127         prompt_show(p, NULL, FALSE);
128
129         return FALSE;
130     }
131
132     cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
133     if (!cmd) {
134         g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
135         return FALSE;
136     }
137
138     /* If there is a keyboard grab going on then we need to cancel
139        it so the application can grab things */
140     event_cancel_all_key_grabs();
141
142     if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
143         g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
144         g_error_free(e);
145     }
146     else {
147         gchar *program = NULL;
148
149         if (o->sn) {
150             program = g_path_get_basename(argv[0]);
151             /* sets up the environment */
152             sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
153                                        o->sn_wmclass,
154                                        /* launch it on the current desktop */
155                                        screen_desktop);
156         }
157
158         if (!g_spawn_async(NULL, argv, NULL,
159                            G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
160                            NULL, NULL, NULL, &e))
161         {
162             g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
163             g_error_free(e);
164
165             if (o->sn)
166                 sn_spawn_cancel();
167         }
168         if (o->sn)
169             unsetenv("DESKTOP_STARTUP_ID");
170
171         g_free(program);
172         g_strfreev(argv);
173     }
174
175     g_free(cmd);
176
177     return FALSE;
178 }