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