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.
24 #include <sys/types.h>
27 #include <glib/gspawn.h>
34 /** 20 seconds timeout */
35 #define AWESOME_SPAWN_TIMEOUT 20.0
37 /** Wrapper for unrefing startup sequence.
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 array The startup sequence array.
52 * \param s The startup sequence to found, remove and unref.
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
);
67 spawn_monitor_timeout(struct ev_loop
*loop
, ev_timer
*w
, int revents
)
69 spawn_sequence_remove(w
->data
);
74 spawn_monitor_event(SnMonitorEvent
*event
, void *data
)
76 if(globalconf
.hooks
.startup_notification
== LUA_REFNIL
)
79 SnStartupSequence
*sequence
= sn_monitor_event_get_startup_sequence(event
);
80 SnMonitorEventType event_type
= sn_monitor_event_get_type(event
);
82 lua_createtable(globalconf
.L
, 0, 2);
83 lua_pushstring(globalconf
.L
, sn_startup_sequence_get_id(sequence
));
84 lua_setfield(globalconf
.L
, -2, "id");
88 case SN_MONITOR_EVENT_INITIATED
:
89 sn_startup_sequence_ref(sequence
);
90 SnStartupSequence_array_append(&sn_waits
, sequence
);
91 lua_pushliteral(globalconf
.L
, "initiated");
92 lua_setfield(globalconf
.L
, -2, "type");
94 /* Add a timeout function so we do not wait for this event to complete
96 struct ev_timer
*ev_timeout
= p_new(struct ev_timer
, 1);
97 ev_timer_init(ev_timeout
, spawn_monitor_timeout
, AWESOME_SPAWN_TIMEOUT
, 0.);
98 ev_timeout
->data
= sequence
;
99 ev_timer_start(globalconf
.loop
, ev_timeout
);
101 case SN_MONITOR_EVENT_CHANGED
:
102 lua_pushliteral(globalconf
.L
, "change");
103 lua_setfield(globalconf
.L
, -2, "type");
105 case SN_MONITOR_EVENT_COMPLETED
:
106 lua_pushliteral(globalconf
.L
, "completed");
107 lua_setfield(globalconf
.L
, -2, "type");
109 case SN_MONITOR_EVENT_CANCELED
:
110 lua_pushliteral(globalconf
.L
, "canceled");
111 lua_setfield(globalconf
.L
, -2, "type");
118 case SN_MONITOR_EVENT_INITIATED
:
119 case SN_MONITOR_EVENT_CHANGED
:
121 const char *s
= sn_startup_sequence_get_name(sequence
);
124 lua_pushstring(globalconf
.L
, s
);
125 lua_setfield(globalconf
.L
, -2, "name");
128 if((s
= sn_startup_sequence_get_description(sequence
)))
130 lua_pushstring(globalconf
.L
, s
);
131 lua_setfield(globalconf
.L
, -2, "description");
134 lua_pushnumber(globalconf
.L
, sn_startup_sequence_get_workspace(sequence
));
135 lua_setfield(globalconf
.L
, -2, "workspace");
137 if((s
= sn_startup_sequence_get_binary_name(sequence
)))
139 lua_pushstring(globalconf
.L
, s
);
140 lua_setfield(globalconf
.L
, -2, "binary_name");
143 if((s
= sn_startup_sequence_get_icon_name(sequence
)))
145 lua_pushstring(globalconf
.L
, s
);
146 lua_setfield(globalconf
.L
, -2, "icon_name");
149 if((s
= sn_startup_sequence_get_wmclass(sequence
)))
151 lua_pushstring(globalconf
.L
, s
);
152 lua_setfield(globalconf
.L
, -2, "wmclass");
156 case SN_MONITOR_EVENT_COMPLETED
:
157 case SN_MONITOR_EVENT_CANCELED
:
158 spawn_sequence_remove(sequence
);
162 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.startup_notification
, 1, 0);
165 /** Tell the spawn module that an app has been started.
166 * \param c The client that just started.
169 spawn_start_notify(client_t
*c
)
171 foreach(_seq
, sn_waits
)
173 SnStartupSequence
*seq
= *_seq
;
175 const char *seqid
= sn_startup_sequence_get_id(seq
);
177 if(!a_strcmp(seqid
, c
->startup_id
))
181 const char *seqclass
= sn_startup_sequence_get_wmclass(seq
);
182 if(!a_strcmp(seqclass
, c
->class) || !a_strcmp(seqclass
, c
->instance
))
186 const char *seqbin
= sn_startup_sequence_get_binary_name(seq
);
187 if(!a_strcasecmp(seqbin
, c
->class) || !a_strcasecmp(seqbin
, c
->instance
))
194 sn_startup_sequence_complete(seq
);
200 /** Initialize program spawner.
205 globalconf
.sndisplay
= sn_xcb_display_new(globalconf
.connection
, NULL
, NULL
);
207 const int screen_max
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
209 for(int screen
= 0; screen
< screen_max
; screen
++)
210 globalconf
.screens
.tab
[screen
].snmonitor
= sn_monitor_context_new(globalconf
.sndisplay
,
217 spawn_launchee_timeout(struct ev_loop
*loop
, ev_timer
*w
, int revents
)
219 sn_launcher_context_complete(w
->data
);
220 sn_launcher_context_unref(w
->data
);
225 * This function is multi-head (Zaphod) aware and will set display to
226 * the right screen according to mouse position.
227 * \param L The Lua VM state.
228 * \return The number of elements pushed on stack
230 * \lparam The command to launch.
231 * \lparam Use startup-notification, true or false, default to true.
232 * \lparam The optional screen number to spawn the command on.
233 * \lreturn Nothing if launch was successful, an error string otherwise.
236 luaA_spawn(lua_State
*L
)
238 char *host
, newdisplay
[128];
241 int screen
= 0, screenp
, displayp
;
243 if(lua_gettop(L
) >= 2)
244 use_sn
= luaA_checkboolean(L
, 2);
246 if(lua_gettop(L
) == 3)
248 screen
= luaL_checknumber(L
, 3) - 1;
249 luaA_checkscreen(screen
);
252 cmd
= luaL_checkstring(L
, 1);
254 if(!globalconf
.xinerama_is_active
)
256 xcb_parse_display(NULL
, &host
, &displayp
, &screenp
);
257 snprintf(newdisplay
, sizeof(newdisplay
), "%s:%d.%d", host
, displayp
, screen
);
258 setenv("DISPLAY", newdisplay
, 1);
262 SnLauncherContext
*context
= NULL
;
265 char *cmdname
, *space
;
266 if((space
= strchr(cmd
, ' ')))
267 cmdname
= a_strndup(cmd
, space
- cmd
);
269 cmdname
= a_strdup(cmd
);
271 context
= sn_launcher_context_new(globalconf
.sndisplay
, screen_virttophys(screen
));
272 sn_launcher_context_set_name(context
, "awesome");
273 sn_launcher_context_set_description(context
, "awesome spawn");
274 sn_launcher_context_set_binary_name(context
, cmdname
);
275 sn_launcher_context_initiate(context
, "awesome", cmdname
, XCB_CURRENT_TIME
);
278 /* app will have AWESOME_SPAWN_TIMEOUT seconds to complete,
279 * or the timeout function will terminate the launch sequence anyway */
280 struct ev_timer
*ev_timeout
= p_new(struct ev_timer
, 1);
281 ev_timer_init(ev_timeout
, spawn_launchee_timeout
, AWESOME_SPAWN_TIMEOUT
, 0.);
282 ev_timeout
->data
= context
;
283 ev_timer_start(globalconf
.loop
, ev_timeout
);
284 sn_launcher_context_setup_child_process(context
);
287 GError
*error
= NULL
;
288 if(!g_spawn_command_line_async(cmd
, &error
))
290 /* push error on stack */
291 lua_pushstring(L
, error
->message
);
294 sn_launcher_context_complete(context
);
301 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80