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