]> git.sur5r.net Git - i3/i3/blob - src/startup.c
Merge branch 'master' into next
[i3/i3] / src / startup.c
1 #undef I3__FILE__
2 #define I3__FILE__ "startup.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
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.
13  *
14  */
15 #include "all.h"
16 #include "sd-daemon.h"
17
18 #include <sys/types.h>
19 #include <sys/wait.h>
20
21 #define SN_API_NOT_YET_FROZEN 1
22 #include <libsn/sn-launcher.h>
23
24 static TAILQ_HEAD(startup_sequence_head, Startup_Sequence) startup_sequences =
25     TAILQ_HEAD_INITIALIZER(startup_sequences);
26
27 /*
28  * After 60 seconds, a timeout will be triggered for each startup sequence.
29  *
30  * The timeout will just trigger completion of the sequence, so the normal
31  * completion process takes place (startup_monitor_event will free it).
32  *
33  */
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);
37
38     struct Startup_Sequence *current, *sequence = NULL;
39     TAILQ_FOREACH(current, &startup_sequences, sequences) {
40         if (strcmp(current->id, id) != 0)
41             continue;
42
43         sequence = current;
44         break;
45     }
46
47     /* Unref the context (for the timeout itself, see start_application) */
48     sn_launcher_context_unref(w->data);
49
50     if (!sequence) {
51         DLOG("Sequence already deleted, nevermind.\n");
52         return;
53     }
54
55     /* Complete the startup sequence, will trigger its deletion. */
56     sn_launcher_context_complete(w->data);
57     free(w);
58 }
59
60 /*
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.
65  *
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.
69  *
70  */
71 static int _prune_startup_sequences(void) {
72     time_t current_time = time(NULL);
73     int active_sequences = 0;
74
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);
80          ) {
81         current = next;
82         next = TAILQ_NEXT(next, sequences);
83
84         if (current->delete_at == 0) {
85             active_sequences++;
86             continue;
87         }
88
89         if (current_time <= current->delete_at)
90             continue;
91
92         startup_sequence_delete(current);
93     }
94
95     return active_sequences;
96
97 }
98
99 /**
100  * Deletes a startup sequence, ignoring whether its timeout has elapsed.
101  * Useful when e.g. a window is moved between workspaces and its children
102  * shouldn't spawn on the original workspace.
103  *
104  */
105 void startup_sequence_delete(struct Startup_Sequence *sequence) {
106     assert(sequence != NULL);
107     DLOG("Deleting startup sequence %s, delete_at = %ld, current_time = %ld\n",
108          sequence->id, sequence->delete_at, time(NULL));
109
110     /* Unref the context, will be free()d */
111     sn_launcher_context_unref(sequence->context);
112
113     /* Delete our internal sequence */
114     TAILQ_REMOVE(&startup_sequences, sequence, sequences);
115
116     free(sequence->id);
117     free(sequence->workspace);
118     FREE(sequence);
119 }
120
121 /*
122  * Starts the given application by passing it through a shell. We use double fork
123  * to avoid zombie processes. As the started application’s parent exits (immediately),
124  * the application is reparented to init (process-id 1), which correctly handles
125  * childs, so we don’t have to do it :-).
126  *
127  * The shell is determined by looking for the SHELL environment variable. If it
128  * does not exist, /bin/sh is used.
129  *
130  * The no_startup_id flag determines whether a startup notification context
131  * (and ID) should be created, which is the default and encouraged behavior.
132  *
133  */
134 void start_application(const char *command, bool no_startup_id) {
135     SnLauncherContext *context;
136
137     if (!no_startup_id) {
138         /* Create a startup notification context to monitor the progress of this
139          * startup. */
140         context = sn_launcher_context_new(sndisplay, conn_screen);
141         sn_launcher_context_set_name(context, "i3");
142         sn_launcher_context_set_description(context, "exec command in i3");
143         /* Chop off everything starting from the first space (if there are any
144          * spaces in the command), since we don’t want the parameters. */
145         char *first_word = sstrdup(command);
146         char *space = strchr(first_word, ' ');
147         if (space)
148             *space = '\0';
149         sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
150         free(first_word);
151
152         /* Trigger a timeout after 60 seconds */
153         struct ev_timer *timeout = scalloc(sizeof(struct ev_timer));
154         ev_timer_init(timeout, startup_timeout, 60.0, 0.);
155         timeout->data = context;
156         ev_timer_start(main_loop, timeout);
157
158         LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
159
160         /* Save the ID and current workspace in our internal list of startup
161          * sequences */
162         Con *ws = con_get_workspace(focused);
163         struct Startup_Sequence *sequence = scalloc(sizeof(struct Startup_Sequence));
164         sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
165         sequence->workspace = sstrdup(ws->name);
166         sequence->context = context;
167         TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
168
169         /* Increase the refcount once (it starts with 1, so it will be 2 now) for
170          * the timeout. Even if the sequence gets completed, the timeout still
171          * needs the context (but will unref it then) */
172         sn_launcher_context_ref(context);
173     }
174
175     LOG("executing: %s\n", command);
176     if (fork() == 0) {
177         /* Child process */
178         setsid();
179         setrlimit(RLIMIT_CORE, &original_rlimit_core);
180         /* Close all socket activation file descriptors explicitly, we disabled
181          * FD_CLOEXEC to keep them open when restarting i3. */
182         for (int fd = SD_LISTEN_FDS_START;
183              fd < (SD_LISTEN_FDS_START + listen_fds);
184              fd++) {
185             close(fd);
186         }
187         unsetenv("LISTEN_PID");
188         unsetenv("LISTEN_FDS");
189         if (fork() == 0) {
190             /* Setup the environment variable(s) */
191             if (!no_startup_id)
192                 sn_launcher_context_setup_child_process(context);
193
194             /* Stores the path of the shell */
195             static const char *shell = NULL;
196
197             if (shell == NULL)
198                 if ((shell = getenv("SHELL")) == NULL)
199                     shell = "/bin/sh";
200
201             /* This is the child */
202             execl(shell, shell, "-c", command, (void*)NULL);
203             /* not reached */
204         }
205         _exit(0);
206     }
207     wait(0);
208
209     if (!no_startup_id) {
210         /* Change the pointer of the root window to indicate progress */
211         if (xcursor_supported)
212             xcursor_set_root_cursor(XCURSOR_CURSOR_WATCH);
213         else xcb_set_root_cursor(XCURSOR_CURSOR_WATCH);
214     }
215 }
216
217 /*
218  * Called by libstartup-notification when something happens
219  *
220  */
221 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
222     SnStartupSequence *snsequence;
223
224     snsequence = sn_monitor_event_get_startup_sequence(event);
225
226     /* Get the corresponding internal startup sequence */
227     const char *id = sn_startup_sequence_get_id(snsequence);
228     struct Startup_Sequence *current, *sequence = NULL;
229     TAILQ_FOREACH(current, &startup_sequences, sequences) {
230         if (strcmp(current->id, id) != 0)
231             continue;
232
233         sequence = current;
234         break;
235     }
236
237     if (!sequence) {
238         DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
239         return;
240     }
241
242     switch (sn_monitor_event_get_type(event)) {
243         case SN_MONITOR_EVENT_COMPLETED:
244             DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
245
246             /* Mark the given sequence for deletion in 30 seconds. */
247             time_t current_time = time(NULL);
248             sequence->delete_at = current_time + 30;
249             DLOG("Will delete startup sequence %s at timestamp %ld\n",
250                  sequence->id, sequence->delete_at);
251
252             if (_prune_startup_sequences() == 0) {
253                 DLOG("No more startup sequences running, changing root window cursor to default pointer.\n");
254                 /* Change the pointer of the root window to indicate progress */
255                 if (xcursor_supported)
256                     xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
257                 else xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
258             }
259             break;
260         default:
261             /* ignore */
262             break;
263     }
264 }
265
266 /**
267  * Gets the stored startup sequence for the _NET_STARTUP_ID of a given window.
268  *
269  */
270 struct Startup_Sequence *startup_sequence_get(i3Window *cwindow,
271     xcb_get_property_reply_t *startup_id_reply, bool ignore_mapped_leader) {
272     /* The _NET_STARTUP_ID is only needed during this function, so we get it
273      * here and don’t save it in the 'cwindow'. */
274     if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
275         FREE(startup_id_reply);
276         DLOG("No _NET_STARTUP_ID set on window 0x%08x\n", cwindow->id);
277         if (cwindow->leader == XCB_NONE)
278             return NULL;
279
280         /* This is a special case that causes the leader's startup sequence
281          * to only be returned if it has never been mapped, useful primarily
282          * when trying to delete a sequence.
283          *
284          * It's generally inappropriate to delete a leader's sequence when
285          * moving a child window, but if the leader has no container, it's
286          * likely permanently unmapped and the child is the "real" window. */
287         if (ignore_mapped_leader && con_by_window_id(cwindow->leader) != NULL) {
288             DLOG("Ignoring leader window 0x%08x\n", cwindow->leader);
289             return NULL;
290         }
291
292         DLOG("Checking leader window 0x%08x\n", cwindow->leader);
293
294         xcb_get_property_cookie_t cookie;
295
296         cookie = xcb_get_property(conn, false, cwindow->leader,
297             A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
298         startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
299
300         if (startup_id_reply == NULL ||
301             xcb_get_property_value_length(startup_id_reply) == 0) {
302             FREE(startup_id_reply);
303             DLOG("No _NET_STARTUP_ID set on the leader either\n");
304             return NULL;
305         }
306     }
307
308     char *startup_id;
309     if (asprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
310                  (char*)xcb_get_property_value(startup_id_reply)) == -1) {
311         perror("asprintf()");
312         DLOG("Could not get _NET_STARTUP_ID\n");
313         free(startup_id_reply);
314         return NULL;
315     }
316
317     struct Startup_Sequence *current, *sequence = NULL;
318     TAILQ_FOREACH(current, &startup_sequences, sequences) {
319         if (strcmp(current->id, startup_id) != 0)
320             continue;
321
322         sequence = current;
323         break;
324     }
325
326     if (!sequence) {
327         DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
328         free(startup_id);
329         free(startup_id_reply);
330         return NULL;
331     }
332
333     free(startup_id);
334     free(startup_id_reply);
335
336     return sequence;
337 }
338
339 /*
340  * Checks if the given window belongs to a startup notification by checking if
341  * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
342  * unset).
343  *
344  * If so, returns the workspace on which the startup was initiated.
345  * Returns NULL otherwise.
346  *
347  */
348 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
349     struct Startup_Sequence *sequence = startup_sequence_get(cwindow, startup_id_reply, false);
350     if (sequence == NULL)
351         return NULL;
352
353     /* If the startup sequence's time span has elapsed, delete it. */
354     time_t current_time = time(NULL);
355     if (sequence->delete_at > 0 && current_time > sequence->delete_at) {
356         DLOG("Deleting expired startup sequence %s\n", sequence->id);
357         startup_sequence_delete(sequence);
358         return NULL;
359     }
360
361     return sequence->workspace;
362 }