change codename
[awesome.git] / spawn.c
blob5790d79f33e012aa5ef76927db6004c311fdf754
1 /*
2 * spawn.c - Lua configuration management
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <unistd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
27 #include <glib.h>
29 #include "spawn.h"
30 #include "screen.h"
31 #include "luaa.h"
32 #include "event.h"
34 /** 20 seconds timeout */
35 #define AWESOME_SPAWN_TIMEOUT 20.0
37 /** Wrapper for unrefing startup sequence.
39 static inline void
40 a_sn_startup_sequence_unref(SnStartupSequence **sss)
42 return sn_startup_sequence_unref(*sss);
45 DO_ARRAY(SnStartupSequence *, SnStartupSequence, a_sn_startup_sequence_unref)
47 /** The array of startup sequence running */
48 SnStartupSequence_array_t sn_waits;
50 /** Remove a SnStartupSequence pointer from an array and forget about it.
51 * \param s The startup sequence to found, remove and unref.
52 * \return True if found and removed.
54 static inline bool
55 spawn_sequence_remove(SnStartupSequence *s)
57 for(int i = 0; i < sn_waits.len; i++)
58 if(sn_waits.tab[i] == s)
60 SnStartupSequence_array_take(&sn_waits, i);
61 sn_startup_sequence_unref(s);
62 return true;
64 return false;
67 static gboolean
68 spawn_monitor_timeout(gpointer sequence)
70 if(spawn_sequence_remove(sequence))
72 signal_t *sig = signal_array_getbyid(&global_signals,
73 a_strhash((const unsigned char *) "spawn::timeout"));
74 if(sig)
76 /* send a timeout signal */
77 lua_createtable(globalconf.L, 0, 2);
78 lua_pushstring(globalconf.L, sn_startup_sequence_get_id(sequence));
79 lua_setfield(globalconf.L, -2, "id");
80 foreach(func, sig->sigfuncs)
82 lua_pushvalue(globalconf.L, -1);
83 luaA_object_push(globalconf.L, (void *) *func);
84 luaA_dofunction(globalconf.L, 1, 0);
86 lua_pop(globalconf.L, 1);
88 else
89 warn("spawn::timeout signal is missing");
91 sn_startup_sequence_unref(sequence);
92 return FALSE;
95 static void
96 spawn_monitor_event(SnMonitorEvent *event, void *data)
98 SnStartupSequence *sequence = sn_monitor_event_get_startup_sequence(event);
99 SnMonitorEventType event_type = sn_monitor_event_get_type(event);
101 lua_createtable(globalconf.L, 0, 2);
102 lua_pushstring(globalconf.L, sn_startup_sequence_get_id(sequence));
103 lua_setfield(globalconf.L, -2, "id");
105 const char *event_type_str = NULL;
107 switch(event_type)
109 case SN_MONITOR_EVENT_INITIATED:
110 /* ref the sequence for the array */
111 sn_startup_sequence_ref(sequence);
112 SnStartupSequence_array_append(&sn_waits, sequence);
113 event_type_str = "spawn::initiated";
115 /* Add a timeout function so we do not wait for this event to complete
116 * for ever */
117 g_timeout_add_seconds(AWESOME_SPAWN_TIMEOUT, spawn_monitor_timeout, sequence);
118 /* ref the sequence for the callback event */
119 sn_startup_sequence_ref(sequence);
120 break;
121 case SN_MONITOR_EVENT_CHANGED:
122 event_type_str = "spawn::change";
123 break;
124 case SN_MONITOR_EVENT_COMPLETED:
125 event_type_str = "spawn::completed";
126 break;
127 case SN_MONITOR_EVENT_CANCELED:
128 event_type_str = "spawn::canceled";
129 break;
132 /* common actions */
133 switch(event_type)
135 case SN_MONITOR_EVENT_INITIATED:
136 case SN_MONITOR_EVENT_CHANGED:
138 const char *s = sn_startup_sequence_get_name(sequence);
139 if(s)
141 lua_pushstring(globalconf.L, s);
142 lua_setfield(globalconf.L, -2, "name");
145 if((s = sn_startup_sequence_get_description(sequence)))
147 lua_pushstring(globalconf.L, s);
148 lua_setfield(globalconf.L, -2, "description");
151 lua_pushnumber(globalconf.L, sn_startup_sequence_get_workspace(sequence));
152 lua_setfield(globalconf.L, -2, "workspace");
154 if((s = sn_startup_sequence_get_binary_name(sequence)))
156 lua_pushstring(globalconf.L, s);
157 lua_setfield(globalconf.L, -2, "binary_name");
160 if((s = sn_startup_sequence_get_icon_name(sequence)))
162 lua_pushstring(globalconf.L, s);
163 lua_setfield(globalconf.L, -2, "icon_name");
166 if((s = sn_startup_sequence_get_wmclass(sequence)))
168 lua_pushstring(globalconf.L, s);
169 lua_setfield(globalconf.L, -2, "wmclass");
172 break;
173 case SN_MONITOR_EVENT_COMPLETED:
174 case SN_MONITOR_EVENT_CANCELED:
175 spawn_sequence_remove(sequence);
176 break;
179 /* send the signal */
180 signal_t *sig = signal_array_getbyid(&global_signals,
181 a_strhash((const unsigned char *) event_type_str));
183 if(sig)
185 foreach(func, sig->sigfuncs)
187 lua_pushvalue(globalconf.L, -1);
188 luaA_object_push(globalconf.L, (void *) *func);
189 luaA_dofunction(globalconf.L, 1, 0);
191 lua_pop(globalconf.L, 1);
193 else
194 warn("%s signal is missing", event_type_str);
197 /** Tell the spawn module that an app has been started.
198 * \param c The client that just started.
199 * \param startup_id The startup id of the started application.
201 void
202 spawn_start_notify(client_t *c, const char * startup_id)
204 foreach(_seq, sn_waits)
206 SnStartupSequence *seq = *_seq;
207 bool found = false;
208 const char *seqid = sn_startup_sequence_get_id(seq);
210 if (A_STRNEQ(seqid, startup_id))
211 found = true;
212 else
214 const char *seqclass = sn_startup_sequence_get_wmclass(seq);
215 if (A_STREQ(seqclass, c->class) || A_STREQ(seqclass, c->instance))
216 found = true;
217 else
219 const char *seqbin = sn_startup_sequence_get_binary_name(seq);
220 if (A_STREQ_CASE(seqbin, c->class) || A_STREQ_CASE(seqbin, c->instance))
221 found = true;
225 if(found)
227 sn_startup_sequence_complete(seq);
228 break;
233 /** Initialize program spawner.
235 void
236 spawn_init(void)
238 globalconf.sndisplay = sn_xcb_display_new(globalconf.connection, NULL, NULL);
240 globalconf.snmonitor = sn_monitor_context_new(globalconf.sndisplay,
241 globalconf.default_screen,
242 spawn_monitor_event,
243 NULL, NULL);
245 signal_add(&global_signals, "spawn::canceled");
246 signal_add(&global_signals, "spawn::change");
247 signal_add(&global_signals, "spawn::completed");
248 signal_add(&global_signals, "spawn::initiated");
249 signal_add(&global_signals, "spawn::timeout");
252 static gboolean
253 spawn_launchee_timeout(gpointer context)
255 sn_launcher_context_complete(context);
256 sn_launcher_context_unref(context);
257 return FALSE;
260 static void
261 spawn_callback(gpointer user_data)
263 setsid();
266 /** Spawn a command.
267 * \param command_line The command line to launch.
268 * \param error A error pointer to fill with the possible error from
269 * g_spawn_async.
270 * \return g_spawn_async value.
272 static GPid
273 spawn_command(const gchar *command_line, GError **error)
275 gboolean retval;
276 GPid pid;
277 gchar **argv = 0;
279 if(!g_shell_parse_argv(command_line, NULL, &argv, error))
280 return 0;
282 retval = g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
283 spawn_callback, NULL, &pid, error);
284 g_strfreev (argv);
286 if (!retval)
287 return 0;
288 return pid;
291 /** Spawn a program.
292 * This function is multi-head (Zaphod) aware and will set display to
293 * the right screen according to mouse position.
294 * \param L The Lua VM state.
295 * \return The number of elements pushed on stack
296 * \luastack
297 * \lparam The command to launch.
298 * \lparam Use startup-notification, true or false, default to true.
299 * \lreturn Process ID if everything is OK, or an error string if an error occured.
302 luaA_spawn(lua_State *L)
304 const char *cmd;
305 bool use_sn = true;
307 if(lua_gettop(L) >= 2)
308 use_sn = luaA_checkboolean(L, 2);
310 cmd = luaL_checkstring(L, 1);
312 SnLauncherContext *context = NULL;
313 if(use_sn)
315 char *cmdname, *space;
316 const char *first_no_space_char = cmd;
317 /* Look for the first char which is not space */
318 while(*first_no_space_char && *first_no_space_char == ' ')
319 first_no_space_char++;
320 /* Look for space in the string to get the command name. */
321 if((space = strchr(first_no_space_char, ' ')))
322 cmdname = a_strndup(cmd, space - cmd);
323 else
324 cmdname = a_strdup(cmd);
326 context = sn_launcher_context_new(globalconf.sndisplay, globalconf.default_screen);
327 sn_launcher_context_set_name(context, "awesome");
328 sn_launcher_context_set_description(context, "awesome spawn");
329 sn_launcher_context_set_binary_name(context, cmdname);
330 sn_launcher_context_initiate(context, "awesome", cmdname, globalconf.timestamp);
331 p_delete(&cmdname);
333 /* app will have AWESOME_SPAWN_TIMEOUT seconds to complete,
334 * or the timeout function will terminate the launch sequence anyway */
335 g_timeout_add_seconds(AWESOME_SPAWN_TIMEOUT, spawn_launchee_timeout, context);
336 sn_launcher_context_setup_child_process(context);
339 GError *error = NULL;
340 GPid pid = spawn_command(cmd, &error);
341 if(!pid)
343 /* push error on stack */
344 lua_pushstring(L, error->message);
345 g_error_free(error);
346 if(context)
347 sn_launcher_context_complete(context);
348 return 1;
351 /* push pid on stack */
352 lua_pushnumber(L, pid);
354 return 1;
357 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80