]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/actions/execute.c
Merge branch 'backport' into work
[mikachu/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);
80     }
81 }
82
83 static Options* dup_options(Options *in)
84 {
85     Options *o = g_new(Options, 1);
86     o->cmd = g_strdup(in->cmd);
87     o->sn = in->sn;
88     o->sn_name = g_strdup(in->sn_name);
89     o->sn_icon = g_strdup(in->sn_icon);
90     o->sn_wmclass = g_strdup(in->sn_wmclass);
91     o->prompt = NULL;
92     return o;
93 }
94
95 static gboolean run_func(ObActionsData *data, gpointer options);
96
97 static void prompt_cb(ObPrompt *p, gint result, gpointer data)
98 {
99     Options *options = data;
100
101     if (result)
102         run_func(NULL, options);
103
104     prompt_unref(p);
105
106     g_free(options->cmd);
107     g_free(options->sn_name);
108     g_free(options->sn_icon);
109     g_free(options->sn_wmclass);
110     g_free(options);
111 }
112
113 /* Always return FALSE because its not interactive */
114 static gboolean run_func(ObActionsData *data, gpointer options)
115 {
116     GError *e = NULL;
117     gchar **argv = NULL;
118     gchar *cmd;
119     Options *o = options;
120
121     if (!o->cmd) return FALSE;
122
123     if (o->prompt) {
124         ObPrompt *p;
125         Options *ocp;
126         ObPromptAnswer answers[] = {
127             { _("No"), 0 },
128             { _("Yes"), 1 }
129         };
130
131         ocp = dup_options(options);
132         p = prompt_new(o->prompt, answers, 2, 0, 0, prompt_cb, ocp);
133         prompt_show(p, NULL, FALSE);
134
135         return FALSE;
136     }
137
138     cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
139     if (!cmd) {
140         g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
141         return FALSE;
142     }
143
144     /* If there is a keyboard grab going on then we need to cancel
145        it so the application can grab things */
146     event_cancel_all_key_grabs();
147
148     if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
149         g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
150         g_error_free(e);
151     }
152     else {
153         gchar *program = NULL;
154
155         if (o->sn) {
156             program = g_path_get_basename(argv[0]);
157             /* sets up the environment */
158             sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
159                                        o->sn_wmclass,
160                                        /* launch it on the current desktop */
161                                        screen_desktop);
162         }
163
164         if (!g_spawn_async(NULL, argv, NULL,
165                            G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
166                            NULL, NULL, NULL, &e))
167         {
168             g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
169             g_error_free(e);
170
171             if (o->sn)
172                 sn_spawn_cancel();
173         }
174         if (o->sn)
175             unsetenv("DESKTOP_STARTUP_ID");
176
177         g_free(program);
178         g_strfreev(argv);
179     }
180
181     g_free(cmd);
182
183     return FALSE;
184 }