2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * startup.c: Startup notification code. Ensures a startup notification context
8 * is setup when launching applications. We store the current
9 * workspace to open windows in that startup notification context on
10 * the appropriate workspace.
15 #include "sd-daemon.h"
17 #include <sys/types.h>
21 #define SN_API_NOT_YET_FROZEN 1
22 #include <libsn/sn-launcher.h>
24 static TAILQ_HEAD(startup_sequence_head, Startup_Sequence) startup_sequences =
25 TAILQ_HEAD_INITIALIZER(startup_sequences);
28 * After 60 seconds, a timeout will be triggered for each startup sequence.
30 * The timeout will just trigger completion of the sequence, so the normal
31 * completion process takes place (startup_monitor_event will free it).
34 static void startup_timeout(EV_P_ ev_timer *w, int revents) {
35 const char *id = sn_launcher_context_get_startup_id(w->data);
36 DLOG("Timeout for startup sequence %s\n", id);
38 struct Startup_Sequence *current, *sequence = NULL;
39 TAILQ_FOREACH(current, &startup_sequences, sequences) {
40 if (strcmp(current->id, id) != 0)
47 /* Unref the context (for the timeout itself, see start_application) */
48 sn_launcher_context_unref(w->data);
51 DLOG("Sequence already deleted, nevermind.\n");
55 /* Complete the startup sequence, will trigger its deletion. */
56 sn_launcher_context_complete(w->data);
61 * Some applications (such as Firefox) mark a startup sequence as completed
62 * *before* they even map a window. Therefore, we cannot entirely delete the
63 * startup sequence once it’s marked as complete. Instead, we’ll mark it for
64 * deletion in 30 seconds and use that chance to delete old sequences.
66 * This function returns the number of active (!) startup notifications, that
67 * is, those which are not marked for deletion yet. This is used for changing
68 * the root window cursor.
71 static int _prune_startup_sequences(void) {
72 time_t current_time = time(NULL);
73 int active_sequences = 0;
75 /* Traverse the list and delete everything which was marked for deletion 30
76 * seconds ago or earlier. */
77 struct Startup_Sequence *current, *next;
78 for (next = TAILQ_FIRST(&startup_sequences);
79 next != TAILQ_END(&startup_sequences);) {
81 next = TAILQ_NEXT(next, sequences);
83 if (current->delete_at == 0) {
88 if (current_time <= current->delete_at)
91 startup_sequence_delete(current);
94 return active_sequences;
98 * Deletes a startup sequence, ignoring whether its timeout has elapsed.
99 * Useful when e.g. a window is moved between workspaces and its children
100 * shouldn't spawn on the original workspace.
103 void startup_sequence_delete(struct Startup_Sequence *sequence) {
104 assert(sequence != NULL);
105 DLOG("Deleting startup sequence %s, delete_at = %lld, current_time = %lld\n",
106 sequence->id, (long long)sequence->delete_at, (long long)time(NULL));
108 /* Unref the context, will be free()d */
109 sn_launcher_context_unref(sequence->context);
111 /* Delete our internal sequence */
112 TAILQ_REMOVE(&startup_sequences, sequence, sequences);
115 free(sequence->workspace);
120 * Starts the given application by passing it through a shell. We use double fork
121 * to avoid zombie processes. As the started application’s parent exits (immediately),
122 * the application is reparented to init (process-id 1), which correctly handles
123 * childs, so we don’t have to do it :-).
125 * The shell used to start applications is the system's bourne shell (i.e.,
128 * The no_startup_id flag determines whether a startup notification context
129 * (and ID) should be created, which is the default and encouraged behavior.
132 void start_application(const char *command, bool no_startup_id) {
133 SnLauncherContext *context = NULL;
135 if (!no_startup_id) {
136 /* Create a startup notification context to monitor the progress of this
138 context = sn_launcher_context_new(sndisplay, conn_screen);
139 sn_launcher_context_set_name(context, "i3");
140 sn_launcher_context_set_description(context, "exec command in i3");
141 /* Chop off everything starting from the first space (if there are any
142 * spaces in the command), since we don’t want the parameters. */
143 char *first_word = sstrdup(command);
144 char *space = strchr(first_word, ' ');
147 sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
150 /* Trigger a timeout after 60 seconds */
151 struct ev_timer *timeout = scalloc(1, sizeof(struct ev_timer));
152 ev_timer_init(timeout, startup_timeout, 60.0, 0.);
153 timeout->data = context;
154 ev_timer_start(main_loop, timeout);
156 LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
158 /* Save the ID and current workspace in our internal list of startup
160 Con *ws = con_get_workspace(focused);
161 struct Startup_Sequence *sequence = scalloc(1, sizeof(struct Startup_Sequence));
162 sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
163 sequence->workspace = sstrdup(ws->name);
164 sequence->context = context;
165 TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
167 /* Increase the refcount once (it starts with 1, so it will be 2 now) for
168 * the timeout. Even if the sequence gets completed, the timeout still
169 * needs the context (but will unref it then) */
170 sn_launcher_context_ref(context);
173 LOG("executing: %s\n", command);
177 setrlimit(RLIMIT_CORE, &original_rlimit_core);
178 /* Close all socket activation file descriptors explicitly, we disabled
179 * FD_CLOEXEC to keep them open when restarting i3. */
180 for (int fd = SD_LISTEN_FDS_START;
181 fd < (SD_LISTEN_FDS_START + listen_fds);
185 unsetenv("LISTEN_PID");
186 unsetenv("LISTEN_FDS");
187 signal(SIGPIPE, SIG_DFL);
189 /* Setup the environment variable(s) */
191 sn_launcher_context_setup_child_process(context);
193 execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void *)NULL);
200 if (!no_startup_id) {
201 /* Change the pointer of the root window to indicate progress */
202 if (xcursor_supported)
203 xcursor_set_root_cursor(XCURSOR_CURSOR_WATCH);
205 xcb_set_root_cursor(XCURSOR_CURSOR_WATCH);
210 * Called by libstartup-notification when something happens
213 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
214 SnStartupSequence *snsequence;
216 snsequence = sn_monitor_event_get_startup_sequence(event);
218 /* Get the corresponding internal startup sequence */
219 const char *id = sn_startup_sequence_get_id(snsequence);
220 struct Startup_Sequence *current, *sequence = NULL;
221 TAILQ_FOREACH(current, &startup_sequences, sequences) {
222 if (strcmp(current->id, id) != 0)
230 DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
234 switch (sn_monitor_event_get_type(event)) {
235 case SN_MONITOR_EVENT_COMPLETED:
236 DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
238 /* Mark the given sequence for deletion in 30 seconds. */
239 time_t current_time = time(NULL);
240 sequence->delete_at = current_time + 30;
241 DLOG("Will delete startup sequence %s at timestamp %lld\n",
242 sequence->id, (long long)sequence->delete_at);
244 if (_prune_startup_sequences() == 0) {
245 DLOG("No more startup sequences running, changing root window cursor to default pointer.\n");
246 /* Change the pointer of the root window to indicate progress */
247 if (xcursor_supported)
248 xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
250 xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
260 * Renames workspaces that are mentioned in the startup sequences.
263 void startup_sequence_rename_workspace(const char *old_name, const char *new_name) {
264 struct Startup_Sequence *current;
265 TAILQ_FOREACH(current, &startup_sequences, sequences) {
266 if (strcmp(current->workspace, old_name) != 0)
268 DLOG("Renaming workspace \"%s\" to \"%s\" in startup sequence %s.\n",
269 old_name, new_name, current->id);
270 free(current->workspace);
271 current->workspace = sstrdup(new_name);
276 * Gets the stored startup sequence for the _NET_STARTUP_ID of a given window.
279 struct Startup_Sequence *startup_sequence_get(i3Window *cwindow,
280 xcb_get_property_reply_t *startup_id_reply, bool ignore_mapped_leader) {
281 /* The _NET_STARTUP_ID is only needed during this function, so we get it
282 * here and don’t save it in the 'cwindow'. */
283 if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
284 FREE(startup_id_reply);
285 DLOG("No _NET_STARTUP_ID set on window 0x%08x\n", cwindow->id);
286 if (cwindow->leader == XCB_NONE)
289 /* This is a special case that causes the leader's startup sequence
290 * to only be returned if it has never been mapped, useful primarily
291 * when trying to delete a sequence.
293 * It's generally inappropriate to delete a leader's sequence when
294 * moving a child window, but if the leader has no container, it's
295 * likely permanently unmapped and the child is the "real" window. */
296 if (ignore_mapped_leader && con_by_window_id(cwindow->leader) != NULL) {
297 DLOG("Ignoring leader window 0x%08x\n", cwindow->leader);
301 DLOG("Checking leader window 0x%08x\n", cwindow->leader);
303 xcb_get_property_cookie_t cookie;
305 cookie = xcb_get_property(conn, false, cwindow->leader,
306 A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
307 startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
309 if (startup_id_reply == NULL ||
310 xcb_get_property_value_length(startup_id_reply) == 0) {
311 FREE(startup_id_reply);
312 DLOG("No _NET_STARTUP_ID set on the leader either\n");
318 sasprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
319 (char *)xcb_get_property_value(startup_id_reply));
320 struct Startup_Sequence *current, *sequence = NULL;
321 TAILQ_FOREACH(current, &startup_sequences, sequences) {
322 if (strcmp(current->id, startup_id) != 0)
330 DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
332 free(startup_id_reply);
337 free(startup_id_reply);
343 * Checks if the given window belongs to a startup notification by checking if
344 * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
347 * If so, returns the workspace on which the startup was initiated.
348 * Returns NULL otherwise.
351 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
352 struct Startup_Sequence *sequence = startup_sequence_get(cwindow, startup_id_reply, false);
353 if (sequence == NULL)
356 /* If the startup sequence's time span has elapsed, delete it. */
357 time_t current_time = time(NULL);
358 if (sequence->delete_at > 0 && current_time > sequence->delete_at) {
359 DLOG("Deleting expired startup sequence %s\n", sequence->id);
360 startup_sequence_delete(sequence);
364 return sequence->workspace;