2 #define I3__FILE__ "startup.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * startup.c: Startup notification code. Ensures a startup notification context
10 * is setup when launching applications. We store the current
11 * workspace to open windows in that startup notification context on
12 * the appropriate workspace.
16 #include "sd-daemon.h"
18 #include <sys/types.h>
22 #define SN_API_NOT_YET_FROZEN 1
23 #include <libsn/sn-launcher.h>
25 static TAILQ_HEAD(startup_sequence_head, Startup_Sequence) startup_sequences =
26 TAILQ_HEAD_INITIALIZER(startup_sequences);
29 * After 60 seconds, a timeout will be triggered for each startup sequence.
31 * The timeout will just trigger completion of the sequence, so the normal
32 * completion process takes place (startup_monitor_event will free it).
35 static void startup_timeout(EV_P_ ev_timer *w, int revents) {
36 const char *id = sn_launcher_context_get_startup_id(w->data);
37 DLOG("Timeout for startup sequence %s\n", id);
39 struct Startup_Sequence *current, *sequence = NULL;
40 TAILQ_FOREACH(current, &startup_sequences, sequences) {
41 if (strcmp(current->id, id) != 0)
48 /* Unref the context (for the timeout itself, see start_application) */
49 sn_launcher_context_unref(w->data);
52 DLOG("Sequence already deleted, nevermind.\n");
56 /* Complete the startup sequence, will trigger its deletion. */
57 sn_launcher_context_complete(w->data);
62 * Some applications (such as Firefox) mark a startup sequence as completed
63 * *before* they even map a window. Therefore, we cannot entirely delete the
64 * startup sequence once it’s marked as complete. Instead, we’ll mark it for
65 * deletion in 30 seconds and use that chance to delete old sequences.
67 * This function returns the number of active (!) startup notifications, that
68 * is, those which are not marked for deletion yet. This is used for changing
69 * the root window cursor.
72 static int _prune_startup_sequences(void) {
73 time_t current_time = time(NULL);
74 int active_sequences = 0;
76 /* Traverse the list and delete everything which was marked for deletion 30
77 * seconds ago or earlier. */
78 struct Startup_Sequence *current, *next;
79 for (next = TAILQ_FIRST(&startup_sequences);
80 next != TAILQ_END(&startup_sequences);) {
82 next = TAILQ_NEXT(next, sequences);
84 if (current->delete_at == 0) {
89 if (current_time <= current->delete_at)
92 startup_sequence_delete(current);
95 return active_sequences;
99 * Deletes a startup sequence, ignoring whether its timeout has elapsed.
100 * Useful when e.g. a window is moved between workspaces and its children
101 * shouldn't spawn on the original workspace.
104 void startup_sequence_delete(struct Startup_Sequence *sequence) {
105 assert(sequence != NULL);
106 DLOG("Deleting startup sequence %s, delete_at = %ld, current_time = %ld\n",
107 sequence->id, sequence->delete_at, time(NULL));
109 /* Unref the context, will be free()d */
110 sn_launcher_context_unref(sequence->context);
112 /* Delete our internal sequence */
113 TAILQ_REMOVE(&startup_sequences, sequence, sequences);
116 free(sequence->workspace);
121 * Starts the given application by passing it through a shell. We use double fork
122 * to avoid zombie processes. As the started application’s parent exits (immediately),
123 * the application is reparented to init (process-id 1), which correctly handles
124 * childs, so we don’t have to do it :-).
126 * The shell used to start applications is the system's bourne shell (i.e.,
129 * The no_startup_id flag determines whether a startup notification context
130 * (and ID) should be created, which is the default and encouraged behavior.
133 void start_application(const char *command, bool no_startup_id) {
134 SnLauncherContext *context = NULL;
136 if (!no_startup_id) {
137 /* Create a startup notification context to monitor the progress of this
139 context = sn_launcher_context_new(sndisplay, conn_screen);
140 sn_launcher_context_set_name(context, "i3");
141 sn_launcher_context_set_description(context, "exec command in i3");
142 /* Chop off everything starting from the first space (if there are any
143 * spaces in the command), since we don’t want the parameters. */
144 char *first_word = sstrdup(command);
145 char *space = strchr(first_word, ' ');
148 sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
151 /* Trigger a timeout after 60 seconds */
152 struct ev_timer *timeout = scalloc(1, sizeof(struct ev_timer));
153 ev_timer_init(timeout, startup_timeout, 60.0, 0.);
154 timeout->data = context;
155 ev_timer_start(main_loop, timeout);
157 LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
159 /* Save the ID and current workspace in our internal list of startup
161 Con *ws = con_get_workspace(focused);
162 struct Startup_Sequence *sequence = scalloc(1, sizeof(struct Startup_Sequence));
163 sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
164 sequence->workspace = sstrdup(ws->name);
165 sequence->context = context;
166 TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
168 /* Increase the refcount once (it starts with 1, so it will be 2 now) for
169 * the timeout. Even if the sequence gets completed, the timeout still
170 * needs the context (but will unref it then) */
171 sn_launcher_context_ref(context);
174 LOG("executing: %s\n", command);
178 setrlimit(RLIMIT_CORE, &original_rlimit_core);
179 /* Close all socket activation file descriptors explicitly, we disabled
180 * FD_CLOEXEC to keep them open when restarting i3. */
181 for (int fd = SD_LISTEN_FDS_START;
182 fd < (SD_LISTEN_FDS_START + listen_fds);
186 unsetenv("LISTEN_PID");
187 unsetenv("LISTEN_FDS");
188 signal(SIGPIPE, SIG_DFL);
190 /* Setup the environment variable(s) */
192 sn_launcher_context_setup_child_process(context);
194 execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void *)NULL);
201 if (!no_startup_id) {
202 /* Change the pointer of the root window to indicate progress */
203 if (xcursor_supported)
204 xcursor_set_root_cursor(XCURSOR_CURSOR_WATCH);
206 xcb_set_root_cursor(XCURSOR_CURSOR_WATCH);
211 * Called by libstartup-notification when something happens
214 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
215 SnStartupSequence *snsequence;
217 snsequence = sn_monitor_event_get_startup_sequence(event);
219 /* Get the corresponding internal startup sequence */
220 const char *id = sn_startup_sequence_get_id(snsequence);
221 struct Startup_Sequence *current, *sequence = NULL;
222 TAILQ_FOREACH(current, &startup_sequences, sequences) {
223 if (strcmp(current->id, id) != 0)
231 DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
235 switch (sn_monitor_event_get_type(event)) {
236 case SN_MONITOR_EVENT_COMPLETED:
237 DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
239 /* Mark the given sequence for deletion in 30 seconds. */
240 time_t current_time = time(NULL);
241 sequence->delete_at = current_time + 30;
242 DLOG("Will delete startup sequence %s at timestamp %ld\n",
243 sequence->id, sequence->delete_at);
245 if (_prune_startup_sequences() == 0) {
246 DLOG("No more startup sequences running, changing root window cursor to default pointer.\n");
247 /* Change the pointer of the root window to indicate progress */
248 if (xcursor_supported)
249 xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
251 xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
261 * Renames workspaces that are mentioned in the startup sequences.
264 void startup_sequence_rename_workspace(const char *old_name, const char *new_name) {
265 struct Startup_Sequence *current;
266 TAILQ_FOREACH(current, &startup_sequences, sequences) {
267 if (strcmp(current->workspace, old_name) != 0)
269 DLOG("Renaming workspace \"%s\" to \"%s\" in startup sequence %s.\n",
270 old_name, new_name, current->id);
271 free(current->workspace);
272 current->workspace = sstrdup(new_name);
277 * Gets the stored startup sequence for the _NET_STARTUP_ID of a given window.
280 struct Startup_Sequence *startup_sequence_get(i3Window *cwindow,
281 xcb_get_property_reply_t *startup_id_reply, bool ignore_mapped_leader) {
282 /* The _NET_STARTUP_ID is only needed during this function, so we get it
283 * here and don’t save it in the 'cwindow'. */
284 if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
285 FREE(startup_id_reply);
286 DLOG("No _NET_STARTUP_ID set on window 0x%08x\n", cwindow->id);
287 if (cwindow->leader == XCB_NONE)
290 /* This is a special case that causes the leader's startup sequence
291 * to only be returned if it has never been mapped, useful primarily
292 * when trying to delete a sequence.
294 * It's generally inappropriate to delete a leader's sequence when
295 * moving a child window, but if the leader has no container, it's
296 * likely permanently unmapped and the child is the "real" window. */
297 if (ignore_mapped_leader && con_by_window_id(cwindow->leader) != NULL) {
298 DLOG("Ignoring leader window 0x%08x\n", cwindow->leader);
302 DLOG("Checking leader window 0x%08x\n", cwindow->leader);
304 xcb_get_property_cookie_t cookie;
306 cookie = xcb_get_property(conn, false, cwindow->leader,
307 A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
308 startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
310 if (startup_id_reply == NULL ||
311 xcb_get_property_value_length(startup_id_reply) == 0) {
312 FREE(startup_id_reply);
313 DLOG("No _NET_STARTUP_ID set on the leader either\n");
319 sasprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
320 (char *)xcb_get_property_value(startup_id_reply));
321 struct Startup_Sequence *current, *sequence = NULL;
322 TAILQ_FOREACH(current, &startup_sequences, sequences) {
323 if (strcmp(current->id, startup_id) != 0)
331 DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
333 free(startup_id_reply);
338 free(startup_id_reply);
344 * Checks if the given window belongs to a startup notification by checking if
345 * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
348 * If so, returns the workspace on which the startup was initiated.
349 * Returns NULL otherwise.
352 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
353 struct Startup_Sequence *sequence = startup_sequence_get(cwindow, startup_id_reply, false);
354 if (sequence == NULL)
357 /* If the startup sequence's time span has elapsed, delete it. */
358 time_t current_time = time(NULL);
359 if (sequence->delete_at > 0 && current_time > sequence->delete_at) {
360 DLOG("Deleting expired startup sequence %s\n", sequence->id);
361 startup_sequence_delete(sequence);
365 return sequence->workspace;