button: change copy method
[awesome.git] / spawn.c
blob34a107d17828c673533e278369cd47ed026741d4
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 array The startup sequence array.
52 * \param s The startup sequence to found, remove and unref.
54 static inline void
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 break;
66 static void
67 spawn_monitor_timeout(struct ev_loop *loop, ev_timer *w, int revents)
69 spawn_sequence_remove(w->data);
70 p_delete(&w);
73 static void
74 spawn_monitor_event(SnMonitorEvent *event, void *data)
76 if(globalconf.hooks.startup_notification == LUA_REFNIL)
77 return;
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");
86 switch(event_type)
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
95 * for ever */
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);
100 break;
101 case SN_MONITOR_EVENT_CHANGED:
102 lua_pushliteral(globalconf.L, "change");
103 lua_setfield(globalconf.L, -2, "type");
104 break;
105 case SN_MONITOR_EVENT_COMPLETED:
106 lua_pushliteral(globalconf.L, "completed");
107 lua_setfield(globalconf.L, -2, "type");
108 break;
109 case SN_MONITOR_EVENT_CANCELED:
110 lua_pushliteral(globalconf.L, "canceled");
111 lua_setfield(globalconf.L, -2, "type");
112 break;
115 /* common actions */
116 switch(event_type)
118 case SN_MONITOR_EVENT_INITIATED:
119 case SN_MONITOR_EVENT_CHANGED:
121 const char *s = sn_startup_sequence_get_name(sequence);
122 if(s)
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");
155 break;
156 case SN_MONITOR_EVENT_COMPLETED:
157 case SN_MONITOR_EVENT_CANCELED:
158 spawn_sequence_remove(sequence);
159 break;
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.
168 void
169 spawn_start_notify(client_t *c)
171 foreach(_seq, sn_waits)
173 SnStartupSequence *seq = *_seq;
174 bool found = false;
175 const char *seqid = sn_startup_sequence_get_id(seq);
177 if(!a_strcmp(seqid, c->startup_id))
178 found = true;
179 else
181 const char *seqclass = sn_startup_sequence_get_wmclass(seq);
182 if(!a_strcmp(seqclass, c->class) || !a_strcmp(seqclass, c->instance))
183 found = true;
184 else
186 const char *seqbin = sn_startup_sequence_get_binary_name(seq);
187 if(!a_strcasecmp(seqbin, c->class) || !a_strcasecmp(seqbin, c->instance))
188 found = true;
192 if(found)
194 sn_startup_sequence_complete(seq);
195 break;
200 /** Initialize program spawner.
202 void
203 spawn_init(void)
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,
211 screen,
212 spawn_monitor_event,
213 NULL, NULL);
216 static void
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);
221 p_delete(&w);
224 /** Spawn a program.
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
229 * \luastack
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];
239 const char *cmd;
240 bool use_sn = true;
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);
259 p_delete(&host);
262 SnLauncherContext *context = NULL;
263 if(use_sn)
265 char *cmdname, *space;
266 if((space = strchr(cmd, ' ')))
267 cmdname = a_strndup(cmd, space - cmd);
268 else
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);
276 p_delete(&cmdname);
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);
292 g_error_free(error);
293 if(context)
294 sn_launcher_context_complete(context);
295 return 1;
298 return 0;
301 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80