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