]> Dogcows Code - chaz/openbox/blob - openbox/actions/execute.c
pass the targeted window's PID (if available) and WINDOW_ID to the environment of...
[chaz/openbox] / openbox / actions / execute.c
1 #include "openbox/actions.h"
2 #include "openbox/event.h"
3 #include "openbox/startupnotify.h"
4 #include "openbox/client.h"
5 #include "openbox/prompt.h"
6 #include "openbox/screen.h"
7 #include "obt/paths.h"
8 #include "gettext.h"
9
10 #ifdef HAVE_STDLIB_H
11 # include <stdlib.h>
12 #endif
13
14 typedef struct {
15 gchar *cmd;
16 gboolean sn;
17 gchar *sn_name;
18 gchar *sn_icon;
19 gchar *sn_wmclass;
20 gchar *prompt;
21 } Options;
22
23 static gpointer setup_func(xmlNodePtr node);
24 static void free_func(gpointer options);
25 static gboolean run_func(ObActionsData *data, gpointer options);
26 /*
27 static gboolean i_input_func(guint initial_state,
28 XEvent *e,
29 gpointer options,
30 gboolean *used);
31 static void i_cancel_func(gpointer options);
32 */
33
34 void action_execute_startup(void)
35 {
36 actions_register("Execute", setup_func, free_func, run_func, NULL, NULL);
37 }
38
39 static gpointer setup_func(xmlNodePtr node)
40 {
41 xmlNodePtr n;
42 Options *o;
43
44 o = g_new0(Options, 1);
45
46 if ((n = obt_parse_find_node(node, "command")) ||
47 (n = obt_parse_find_node(node, "execute")))
48 {
49 gchar *s = obt_parse_node_string(n);
50 o->cmd = obt_paths_expand_tilde(s);
51 g_free(s);
52 }
53
54 if ((n = obt_parse_find_node(node, "prompt")))
55 o->prompt = obt_parse_node_string(n);
56
57 if ((n = obt_parse_find_node(node, "startupnotify"))) {
58 xmlNodePtr m;
59 if ((m = obt_parse_find_node(n->children, "enabled")))
60 o->sn = obt_parse_node_bool(m);
61 if ((m = obt_parse_find_node(n->children, "name")))
62 o->sn_name = obt_parse_node_string(m);
63 if ((m = obt_parse_find_node(n->children, "icon")))
64 o->sn_icon = obt_parse_node_string(m);
65 if ((m = obt_parse_find_node(n->children, "wmclass")))
66 o->sn_wmclass = obt_parse_node_string(m);
67 }
68 return o;
69 }
70
71 static void free_func(gpointer options)
72 {
73 Options *o = options;
74
75 if (o) {
76 g_free(o->cmd);
77 g_free(o->sn_name);
78 g_free(o->sn_icon);
79 g_free(o->sn_wmclass);
80 g_free(o->prompt);
81 g_free(o);
82 }
83 }
84
85 static Options* dup_options(Options *in)
86 {
87 Options *o = g_new(Options, 1);
88 o->cmd = g_strdup(in->cmd);
89 o->sn = in->sn;
90 o->sn_name = g_strdup(in->sn_name);
91 o->sn_icon = g_strdup(in->sn_icon);
92 o->sn_wmclass = g_strdup(in->sn_wmclass);
93 o->prompt = NULL;
94 return o;
95 }
96
97 static gboolean run_func(ObActionsData *data, gpointer options);
98
99 static void prompt_cb(ObPrompt *p, gint result, gpointer options)
100 {
101 if (result)
102 run_func(NULL, options);
103
104 prompt_unref(p);
105 free_func(options);
106 }
107
108 /* Always return FALSE because its not interactive */
109 static gboolean run_func(ObActionsData *data, gpointer options)
110 {
111 GError *e = NULL;
112 gchar **argv = NULL;
113 gchar *cmd;
114 Options *o = options;
115
116 if (!o->cmd) return FALSE;
117
118 if (o->prompt) {
119 ObPrompt *p;
120 Options *ocp;
121 ObPromptAnswer answers[] = {
122 { _("No"), 0 },
123 { _("Yes"), 1 }
124 };
125
126 ocp = dup_options(options);
127 p = prompt_new(o->prompt, answers, 2, 0, 0, prompt_cb, ocp);
128 prompt_show(p, NULL, FALSE);
129
130 return FALSE;
131 }
132
133 cmd = g_filename_from_utf8(o->cmd, -1, NULL, NULL, NULL);
134 if (!cmd) {
135 g_message(_("Failed to convert the path \"%s\" from utf8"), o->cmd);
136 return FALSE;
137 }
138
139 /* If there is a keyboard grab going on then we need to cancel
140 it so the application can grab things */
141 event_cancel_all_key_grabs();
142
143 if (!g_shell_parse_argv(cmd, NULL, &argv, &e)) {
144 g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
145 g_error_free(e);
146 }
147 else {
148 gchar *program = NULL;
149
150 if (o->sn) {
151 program = g_path_get_basename(argv[0]);
152 /* sets up the environment */
153 sn_setup_spawn_environment(program, o->sn_name, o->sn_icon,
154 o->sn_wmclass,
155 /* launch it on the current desktop */
156 screen_desktop);
157 }
158
159 if (data->client && data->client->pid) {
160 gchar *pid;
161
162 pid = g_strdup_printf("%u", data->client->pid);
163 setenv("PID", pid, TRUE);
164 g_free(pid);
165 }
166 else
167 unsetenv("PID");
168
169 if (data->client) {
170 gchar *wid;
171
172 wid = g_strdup_printf("%u", data->client->window);
173 setenv("WINDOW_ID", wid, TRUE);
174 g_free(wid);
175 }
176 else
177 unsetenv("WINDOW_ID");
178
179 if (!g_spawn_async(NULL, argv, NULL,
180 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
181 NULL, NULL, NULL, &e))
182 {
183 g_message(_("Failed to execute \"%s\": %s"), o->cmd, e->message);
184 g_error_free(e);
185
186 if (o->sn)
187 sn_spawn_cancel();
188 }
189 if (o->sn)
190 unsetenv("DESKTOP_STARTUP_ID");
191
192 unsetenv("PID");
193 unsetenv("WINDOW_ID");
194
195 g_free(program);
196 g_strfreev(argv);
197 }
198
199 g_free(cmd);
200
201 return FALSE;
202 }
This page took 0.043306 seconds and 5 git commands to generate.