awesomerc: stop handling beautiful
[awesome.git] / spawn.c
blob72d4e83bb0af457a701f4d85c6e6d6762cb37ba7
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))
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(w->data));
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);
89 sn_startup_sequence_unref(w->data);
90 p_delete(&w);
93 static void
94 spawn_monitor_event(SnMonitorEvent *event, void *data)
96 SnStartupSequence *sequence = sn_monitor_event_get_startup_sequence(event);
97 SnMonitorEventType event_type = sn_monitor_event_get_type(event);
99 lua_createtable(globalconf.L, 0, 2);
100 lua_pushstring(globalconf.L, sn_startup_sequence_get_id(sequence));
101 lua_setfield(globalconf.L, -2, "id");
103 const char *event_type_str = NULL;
105 switch(event_type)
107 case SN_MONITOR_EVENT_INITIATED:
108 /* ref the sequence for the array */
109 sn_startup_sequence_ref(sequence);
110 SnStartupSequence_array_append(&sn_waits, sequence);
111 event_type_str = "spawn::initiated";
113 /* Add a timeout function so we do not wait for this event to complete
114 * for ever */
115 struct ev_timer *ev_timeout = p_new(struct ev_timer, 1);
116 ev_timer_init(ev_timeout, spawn_monitor_timeout, AWESOME_SPAWN_TIMEOUT, 0.);
117 /* ref the sequence for the callback event */
118 sn_startup_sequence_ref(sequence);
119 ev_timeout->data = sequence;
120 ev_timer_start(globalconf.loop, ev_timeout);
121 break;
122 case SN_MONITOR_EVENT_CHANGED:
123 event_type_str = "spawn::change";
124 break;
125 case SN_MONITOR_EVENT_COMPLETED:
126 event_type_str = "spawn::completed";
127 break;
128 case SN_MONITOR_EVENT_CANCELED:
129 event_type_str = "spawn::canceled";
130 break;
133 /* common actions */
134 switch(event_type)
136 case SN_MONITOR_EVENT_INITIATED:
137 case SN_MONITOR_EVENT_CHANGED:
139 const char *s = sn_startup_sequence_get_name(sequence);
140 if(s)
142 lua_pushstring(globalconf.L, s);
143 lua_setfield(globalconf.L, -2, "name");
146 if((s = sn_startup_sequence_get_description(sequence)))
148 lua_pushstring(globalconf.L, s);
149 lua_setfield(globalconf.L, -2, "description");
152 lua_pushnumber(globalconf.L, sn_startup_sequence_get_workspace(sequence));
153 lua_setfield(globalconf.L, -2, "workspace");
155 if((s = sn_startup_sequence_get_binary_name(sequence)))
157 lua_pushstring(globalconf.L, s);
158 lua_setfield(globalconf.L, -2, "binary_name");
161 if((s = sn_startup_sequence_get_icon_name(sequence)))
163 lua_pushstring(globalconf.L, s);
164 lua_setfield(globalconf.L, -2, "icon_name");
167 if((s = sn_startup_sequence_get_wmclass(sequence)))
169 lua_pushstring(globalconf.L, s);
170 lua_setfield(globalconf.L, -2, "wmclass");
173 break;
174 case SN_MONITOR_EVENT_COMPLETED:
175 case SN_MONITOR_EVENT_CANCELED:
176 spawn_sequence_remove(sequence);
177 break;
180 /* send the signal */
181 signal_t *sig = signal_array_getbyid(&global_signals,
182 a_strhash((const unsigned char *) event_type_str));
184 if(sig)
186 foreach(func, sig->sigfuncs)
188 lua_pushvalue(globalconf.L, -1);
189 luaA_object_push(globalconf.L, (void *) *func);
190 luaA_dofunction(globalconf.L, 1, 0);
192 lua_pop(globalconf.L, 1);
196 /** Tell the spawn module that an app has been started.
197 * \param c The client that just started.
199 void
200 spawn_start_notify(client_t *c)
202 foreach(_seq, sn_waits)
204 SnStartupSequence *seq = *_seq;
205 bool found = false;
206 const char *seqid = sn_startup_sequence_get_id(seq);
208 if(!a_strcmp(seqid, c->startup_id))
209 found = true;
210 else
212 const char *seqclass = sn_startup_sequence_get_wmclass(seq);
213 if(!a_strcmp(seqclass, c->class) || !a_strcmp(seqclass, c->instance))
214 found = true;
215 else
217 const char *seqbin = sn_startup_sequence_get_binary_name(seq);
218 if(!a_strcasecmp(seqbin, c->class) || !a_strcasecmp(seqbin, c->instance))
219 found = true;
223 if(found)
225 sn_startup_sequence_complete(seq);
226 break;
231 /** Initialize program spawner.
233 void
234 spawn_init(void)
236 globalconf.sndisplay = sn_xcb_display_new(globalconf.connection, NULL, NULL);
238 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
240 for(int screen = 0; screen < screen_max; screen++)
241 globalconf.screens.tab[screen].snmonitor = sn_monitor_context_new(globalconf.sndisplay,
242 screen,
243 spawn_monitor_event,
244 NULL, NULL);
247 static void
248 spawn_launchee_timeout(struct ev_loop *loop, ev_timer *w, int revents)
250 sn_launcher_context_complete(w->data);
251 sn_launcher_context_unref(w->data);
252 p_delete(&w);
255 /** Spawn a program.
256 * This function is multi-head (Zaphod) aware and will set display to
257 * the right screen according to mouse position.
258 * \param L The Lua VM state.
259 * \return The number of elements pushed on stack
260 * \luastack
261 * \lparam The command to launch.
262 * \lparam Use startup-notification, true or false, default to true.
263 * \lparam The optional screen number to spawn the command on.
264 * \lreturn Nothing if launch was successful, an error string otherwise.
267 luaA_spawn(lua_State *L)
269 char *host, newdisplay[128];
270 const char *cmd;
271 bool use_sn = true;
272 int screen = 0, screenp, displayp;
274 if(lua_gettop(L) >= 2)
275 use_sn = luaA_checkboolean(L, 2);
277 if(lua_gettop(L) == 3)
279 screen = luaL_checknumber(L, 3) - 1;
280 luaA_checkscreen(screen);
283 cmd = luaL_checkstring(L, 1);
285 if(!globalconf.xinerama_is_active)
287 xcb_parse_display(NULL, &host, &displayp, &screenp);
288 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
289 setenv("DISPLAY", newdisplay, 1);
290 p_delete(&host);
293 SnLauncherContext *context = NULL;
294 if(use_sn)
296 char *cmdname, *space;
297 const char *first_no_space_char = cmd;
298 /* Look for the first char which is not space */
299 while(*first_no_space_char && *first_no_space_char == ' ')
300 first_no_space_char++;
301 /* Look for space in the string to get the command name. */
302 if((space = strchr(first_no_space_char, ' ')))
303 cmdname = a_strndup(cmd, space - cmd);
304 else
305 cmdname = a_strdup(cmd);
307 context = sn_launcher_context_new(globalconf.sndisplay, screen_virttophys(screen));
308 sn_launcher_context_set_name(context, "awesome");
309 sn_launcher_context_set_description(context, "awesome spawn");
310 sn_launcher_context_set_binary_name(context, cmdname);
311 sn_launcher_context_initiate(context, "awesome", cmdname, XCB_CURRENT_TIME);
312 p_delete(&cmdname);
314 /* app will have AWESOME_SPAWN_TIMEOUT seconds to complete,
315 * or the timeout function will terminate the launch sequence anyway */
316 struct ev_timer *ev_timeout = p_new(struct ev_timer, 1);
317 ev_timer_init(ev_timeout, spawn_launchee_timeout, AWESOME_SPAWN_TIMEOUT, 0.);
318 ev_timeout->data = context;
319 ev_timer_start(globalconf.loop, ev_timeout);
320 sn_launcher_context_setup_child_process(context);
323 GError *error = NULL;
324 if(!g_spawn_command_line_async(cmd, &error))
326 /* push error on stack */
327 lua_pushstring(L, error->message);
328 g_error_free(error);
329 if(context)
330 sn_launcher_context_complete(context);
331 return 1;
334 return 0;
337 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80