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