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/screen.h"
5 #include "obt/paths.h"
6 #include "gettext.h"
7
8 #ifdef HAVE_STDLIB_H
9 #  include <stdlib.h>
10 #endif
11
12 typedef struct {
13     gchar   *cmd;
14     gboolean sn;
15     gchar   *sn_name;
16     gchar   *sn_icon;
17     gchar   *sn_wmclass;
18 } Options;
19
20 static gpointer setup_func(xmlNodePtr node);
21 static void     free_func(gpointer options);
22 static gboolean run_func(ObActionsData *data, gpointer options);
23 /*
24 static gboolean i_input_func(guint initial_state,
25                              XEvent *e,
26                              gpointer options,
27                              gboolean *used);
28 static void     i_cancel_func(gpointer options);
29 */
30
31 void action_execute_startup(void)
32 {
33     actions_register("Execute", setup_func, free_func, run_func, NULL, NULL);
34 }
35
36 static gpointer setup_func(xmlNodePtr node)
37 {
38     xmlNodePtr n;
39     Options *o;
40
41     o = g_new0(Options, 1);
42
43     if ((n = obt_parse_find_node(node, "command")) ||
44         (n = obt_parse_find_node(node, "execute")))
45     {
46         gchar *s = obt_parse_node_string(n);
47         o->cmd = obt_paths_expand_tilde(s);
48         g_free(s);
49     }
50
51     if ((n = obt_parse_find_node(node, "startupnotify"))) {
52         xmlNodePtr m;
53         if ((m = obt_parse_find_node(n->children, "enabled")))
54             o->sn = obt_parse_node_bool(m);
55         if ((m = obt_parse_find_node(n->children, "name")))
56             o->sn_name = obt_parse_node_string(m);
57         if ((m = obt_parse_find_node(n->children, "icon")))
58             o->sn_icon = obt_parse_node_string(m);
59         if ((m = obt_parse_find_node(n->children, "wmclass")))
60             o->sn_wmclass = obt_parse_node_string(m);
61     }
62     return o;
63 }
64
65 static void free_func(gpointer options)
66 {
67     Options *o = options;
68
69     if (o) {
70         g_free(o->cmd);
71         g_free(o->sn_name);
72         g_free(o->sn_icon);
73         g_free(o->sn_wmclass);
74         g_free(o);
75     }
76 }
77
78 /* Always return FALSE because its not interactive */
79 static gboolean run_func(ObActionsData *data, gpointer options)
80 {
81     GError *e = NULL;
82     gchar **argv = NULL;
83     gchar *cmd;
84     Options *o = options;
85
86     if (!o->cmd) return FALSE;
87     cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
88     if (!cmd) {
89         g_message(_("Failed to convert the path '%s' from utf8"), o->cmd);
90         return FALSE;
91     }
92
93     /* If there is a keyboard grab going on then we need to cancel
94        it so the application can grab things */
95     event_cancel_all_key_grabs();
96
97     if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
98         g_message(_("Failed to execute '%s': %s"), o->cmd, e->message);
99         g_error_free(e);
100     }
101     else {
102         gchar *program = NULL;
103
104         if (o->sn) {
105             program = g_path_get_basename(argv[0]);
106             /* sets up the environment */
107             sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
108                                        o->sn_wmclass,
109                                        /* launch it on the current desktop */
110                                        screen_desktop);
111         }
112
113         if (!g_spawn_async(NULL, argv, NULL,
114                            G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
115                            NULL, NULL, NULL, &e))
116         {
117             g_message(_("Failed to execute '%s': %s"), o->cmd, e->message);
118             g_error_free(e);
119
120             if (o->sn)
121                 sn_spawn_cancel();
122         }
123         if (o->sn)
124             unsetenv("DESKTOP_STARTUP_ID");
125
126         g_free(program);
127         g_strfreev(argv);
128     }
129
130     g_free(cmd);
131
132     return FALSE;
133 }