awful.layout: do not pass specific param in arrange (FS#541)
[awesome.git] / spawn.c
blob2998dec6f5b433a1408271b85a478b251bf88ae4
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/gspawn.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 void
68 spawn_monitor_timeout(struct ev_loop *loop, ev_timer *w, int revents)
70 if(spawn_sequence_remove(w->data)
71 && globalconf.hooks.startup_notification != LUA_REFNIL)
73 /* send a timeout event to hook function */
74 lua_createtable(globalconf.L, 0, 2);
75 lua_pushstring(globalconf.L, sn_startup_sequence_get_id(w->data));
76 lua_setfield(globalconf.L, -2, "id");
77 lua_pushliteral(globalconf.L, "timedout");
78 lua_setfield(globalconf.L, -2, "type");
79 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.startup_notification, 1, 0);
81 sn_startup_sequence_unref(w->data);
82 p_delete(&w);
85 static void
86 spawn_monitor_event(SnMonitorEvent *event, void *data)
88 if(globalconf.hooks.startup_notification == LUA_REFNIL)
89 return;
91 SnStartupSequence *sequence = sn_monitor_event_get_startup_sequence(event);
92 SnMonitorEventType event_type = sn_monitor_event_get_type(event);
94 lua_createtable(globalconf.L, 0, 2);
95 lua_pushstring(globalconf.L, sn_startup_sequence_get_id(sequence));
96 lua_setfield(globalconf.L, -2, "id");
98 switch(event_type)
100 case SN_MONITOR_EVENT_INITIATED:
101 /* ref the sequence for the array */
102 sn_startup_sequence_ref(sequence);
103 SnStartupSequence_array_append(&sn_waits, sequence);
104 lua_pushliteral(globalconf.L, "initiated");
105 lua_setfield(globalconf.L, -2, "type");
107 /* Add a timeout function so we do not wait for this event to complete
108 * for ever */
109 struct ev_timer *ev_timeout = p_new(struct ev_timer, 1);
110 ev_timer_init(ev_timeout, spawn_monitor_timeout, AWESOME_SPAWN_TIMEOUT, 0.);
111 /* ref the sequence for the callback event */
112 sn_startup_sequence_ref(sequence);
113 ev_timeout->data = sequence;
114 ev_timer_start(globalconf.loop, ev_timeout);
115 break;
116 case SN_MONITOR_EVENT_CHANGED:
117 lua_pushliteral(globalconf.L, "change");
118 lua_setfield(globalconf.L, -2, "type");
119 break;
120 case SN_MONITOR_EVENT_COMPLETED:
121 lua_pushliteral(globalconf.L, "completed");
122 lua_setfield(globalconf.L, -2, "type");
123 break;
124 case SN_MONITOR_EVENT_CANCELED:
125 lua_pushliteral(globalconf.L, "canceled");
126 lua_setfield(globalconf.L, -2, "type");
127 break;
130 /* common actions */
131 switch(event_type)
133 case SN_MONITOR_EVENT_INITIATED:
134 case SN_MONITOR_EVENT_CHANGED:
136 const char *s = sn_startup_sequence_get_name(sequence);
137 if(s)
139 lua_pushstring(globalconf.L, s);
140 lua_setfield(globalconf.L, -2, "name");
143 if((s = sn_startup_sequence_get_description(sequence)))
145 lua_pushstring(globalconf.L, s);
146 lua_setfield(globalconf.L, -2, "description");
149 lua_pushnumber(globalconf.L, sn_startup_sequence_get_workspace(sequence));
150 lua_setfield(globalconf.L, -2, "workspace");
152 if((s = sn_startup_sequence_get_binary_name(sequence)))
154 lua_pushstring(globalconf.L, s);
155 lua_setfield(globalconf.L, -2, "binary_name");
158 if((s = sn_startup_sequence_get_icon_name(sequence)))
160 lua_pushstring(globalconf.L, s);
161 lua_setfield(globalconf.L, -2, "icon_name");
164 if((s = sn_startup_sequence_get_wmclass(sequence)))
166 lua_pushstring(globalconf.L, s);
167 lua_setfield(globalconf.L, -2, "wmclass");
170 break;
171 case SN_MONITOR_EVENT_COMPLETED:
172 case SN_MONITOR_EVENT_CANCELED:
173 spawn_sequence_remove(sequence);
174 break;
177 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.startup_notification, 1, 0);
180 /** Tell the spawn module that an app has been started.
181 * \param c The client that just started.
183 void
184 spawn_start_notify(client_t *c)
186 foreach(_seq, sn_waits)
188 SnStartupSequence *seq = *_seq;
189 bool found = false;
190 const char *seqid = sn_startup_sequence_get_id(seq);
192 if(!a_strcmp(seqid, c->startup_id))
193 found = true;
194 else
196 const char *seqclass = sn_startup_sequence_get_wmclass(seq);
197 if(!a_strcmp(seqclass, c->class) || !a_strcmp(seqclass, c->instance))
198 found = true;
199 else
201 const char *seqbin = sn_startup_sequence_get_binary_name(seq);
202 if(!a_strcasecmp(seqbin, c->class) || !a_strcasecmp(seqbin, c->instance))
203 found = true;
207 if(found)
209 sn_startup_sequence_complete(seq);
210 break;
215 /** Initialize program spawner.
217 void
218 spawn_init(void)
220 globalconf.sndisplay = sn_xcb_display_new(globalconf.connection, NULL, NULL);
222 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
224 for(int screen = 0; screen < screen_max; screen++)
225 globalconf.screens.tab[screen].snmonitor = sn_monitor_context_new(globalconf.sndisplay,
226 screen,
227 spawn_monitor_event,
228 NULL, NULL);
231 static void
232 spawn_launchee_timeout(struct ev_loop *loop, ev_timer *w, int revents)
234 sn_launcher_context_complete(w->data);
235 sn_launcher_context_unref(w->data);
236 p_delete(&w);
239 /** Spawn a program.
240 * This function is multi-head (Zaphod) aware and will set display to
241 * the right screen according to mouse position.
242 * \param L The Lua VM state.
243 * \return The number of elements pushed on stack
244 * \luastack
245 * \lparam The command to launch.
246 * \lparam Use startup-notification, true or false, default to true.
247 * \lparam The optional screen number to spawn the command on.
248 * \lreturn Nothing if launch was successful, an error string otherwise.
251 luaA_spawn(lua_State *L)
253 char *host, newdisplay[128];
254 const char *cmd;
255 bool use_sn = true;
256 int screen = 0, screenp, displayp;
258 if(lua_gettop(L) >= 2)
259 use_sn = luaA_checkboolean(L, 2);
261 if(lua_gettop(L) == 3)
263 screen = luaL_checknumber(L, 3) - 1;
264 luaA_checkscreen(screen);
267 cmd = luaL_checkstring(L, 1);
269 if(!globalconf.xinerama_is_active)
271 xcb_parse_display(NULL, &host, &displayp, &screenp);
272 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
273 setenv("DISPLAY", newdisplay, 1);
274 p_delete(&host);
277 SnLauncherContext *context = NULL;
278 if(use_sn)
280 char *cmdname, *space;
281 const char *first_no_space_char = cmd;
282 /* Look for the first char which is not space */
283 while(*first_no_space_char && *first_no_space_char == ' ')
284 first_no_space_char++;
285 /* Look for space in the string to get the command name. */
286 if((space = strchr(first_no_space_char, ' ')))
287 cmdname = a_strndup(cmd, space - cmd);
288 else
289 cmdname = a_strdup(cmd);
291 context = sn_launcher_context_new(globalconf.sndisplay, screen_virttophys(screen));
292 sn_launcher_context_set_name(context, "awesome");
293 sn_launcher_context_set_description(context, "awesome spawn");
294 sn_launcher_context_set_binary_name(context, cmdname);
295 sn_launcher_context_initiate(context, "awesome", cmdname, XCB_CURRENT_TIME);
296 p_delete(&cmdname);
298 /* app will have AWESOME_SPAWN_TIMEOUT seconds to complete,
299 * or the timeout function will terminate the launch sequence anyway */
300 struct ev_timer *ev_timeout = p_new(struct ev_timer, 1);
301 ev_timer_init(ev_timeout, spawn_launchee_timeout, AWESOME_SPAWN_TIMEOUT, 0.);
302 ev_timeout->data = context;
303 ev_timer_start(globalconf.loop, ev_timeout);
304 sn_launcher_context_setup_child_process(context);
307 GError *error = NULL;
308 if(!g_spawn_command_line_async(cmd, &error))
310 /* push error on stack */
311 lua_pushstring(L, error->message);
312 g_error_free(error);
313 if(context)
314 sn_launcher_context_complete(context);
315 return 1;
318 return 0;
321 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80