]> git.sur5r.net Git - i3/i3/blob - src/startup.c
Merge branch 'master' into next
[i3/i3] / src / startup.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
6  *
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.
11  *
12  */
13 #include "all.h"
14 #include "sd-daemon.h"
15
16 #include <sys/types.h>
17 #include <sys/wait.h>
18
19 #define SN_API_NOT_YET_FROZEN 1
20 #include <libsn/sn-launcher.h>
21
22 static TAILQ_HEAD(startup_sequence_head, Startup_Sequence) startup_sequences =
23     TAILQ_HEAD_INITIALIZER(startup_sequences);
24
25 /*
26  * After 60 seconds, a timeout will be triggered for each startup sequence.
27  *
28  * The timeout will just trigger completion of the sequence, so the normal
29  * completion process takes place (startup_monitor_event will free it).
30  *
31  */
32 static void startup_timeout(EV_P_ ev_timer *w, int revents) {
33     const char *id = sn_launcher_context_get_startup_id(w->data);
34     DLOG("Timeout for startup sequence %s\n", id);
35
36     struct Startup_Sequence *current, *sequence = NULL;
37     TAILQ_FOREACH(current, &startup_sequences, sequences) {
38         if (strcmp(current->id, id) != 0)
39             continue;
40
41         sequence = current;
42         break;
43     }
44
45     /* Unref the context (for the timeout itself, see start_application) */
46     sn_launcher_context_unref(w->data);
47
48     if (!sequence) {
49         DLOG("Sequence already deleted, nevermind.\n");
50         return;
51     }
52
53     /* Complete the startup sequence, will trigger its deletion. */
54     sn_launcher_context_complete(w->data);
55     free(w);
56 }
57
58 /*
59  * Starts the given application by passing it through a shell. We use double fork
60  * to avoid zombie processes. As the started application’s parent exits (immediately),
61  * the application is reparented to init (process-id 1), which correctly handles
62  * childs, so we don’t have to do it :-).
63  *
64  * The shell is determined by looking for the SHELL environment variable. If it
65  * does not exist, /bin/sh is used.
66  *
67  * The no_startup_id flag determines whether a startup notification context
68  * (and ID) should be created, which is the default and encouraged behavior.
69  *
70  */
71 void start_application(const char *command, bool no_startup_id) {
72     SnLauncherContext *context;
73
74     if (!no_startup_id) {
75         /* Create a startup notification context to monitor the progress of this
76          * startup. */
77         context = sn_launcher_context_new(sndisplay, conn_screen);
78         sn_launcher_context_set_name(context, "i3");
79         sn_launcher_context_set_description(context, "exec command in i3");
80         /* Chop off everything starting from the first space (if there are any
81          * spaces in the command), since we don’t want the parameters. */
82         char *first_word = sstrdup(command);
83         char *space = strchr(first_word, ' ');
84         if (space)
85             *space = '\0';
86         sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
87         free(first_word);
88
89         /* Trigger a timeout after 60 seconds */
90         struct ev_timer *timeout = scalloc(sizeof(struct ev_timer));
91         ev_timer_init(timeout, startup_timeout, 60.0, 0.);
92         timeout->data = context;
93         ev_timer_start(main_loop, timeout);
94
95         LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
96
97         /* Save the ID and current workspace in our internal list of startup
98          * sequences */
99         Con *ws = con_get_workspace(focused);
100         struct Startup_Sequence *sequence = scalloc(sizeof(struct Startup_Sequence));
101         sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
102         sequence->workspace = sstrdup(ws->name);
103         sequence->context = context;
104         TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
105
106         /* Increase the refcount once (it starts with 1, so it will be 2 now) for
107          * the timeout. Even if the sequence gets completed, the timeout still
108          * needs the context (but will unref it then) */
109         sn_launcher_context_ref(context);
110     }
111
112     LOG("executing: %s\n", command);
113     if (fork() == 0) {
114         /* Child process */
115         setsid();
116         setrlimit(RLIMIT_CORE, &original_rlimit_core);
117         /* Close all socket activation file descriptors explicitly, we disabled
118          * FD_CLOEXEC to keep them open when restarting i3. */
119         for (int fd = SD_LISTEN_FDS_START;
120              fd < (SD_LISTEN_FDS_START + listen_fds);
121              fd++) {
122             close(fd);
123         }
124         unsetenv("LISTEN_PID");
125         unsetenv("LISTEN_FDS");
126         if (fork() == 0) {
127             /* Setup the environment variable(s) */
128             if (!no_startup_id)
129                 sn_launcher_context_setup_child_process(context);
130
131             /* Stores the path of the shell */
132             static const char *shell = NULL;
133
134             if (shell == NULL)
135                 if ((shell = getenv("SHELL")) == NULL)
136                     shell = "/bin/sh";
137
138             /* This is the child */
139             execl(shell, shell, "-c", command, (void*)NULL);
140             /* not reached */
141         }
142         _exit(0);
143     }
144     wait(0);
145
146     if (!no_startup_id) {
147         /* Change the pointer of the root window to indicate progress */
148         if (xcursor_supported)
149             xcursor_set_root_cursor(XCURSOR_CURSOR_WATCH);
150         else xcb_set_root_cursor(XCURSOR_CURSOR_WATCH);
151     }
152 }
153
154 /*
155  * Called by libstartup-notification when something happens
156  *
157  */
158 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
159     SnStartupSequence *snsequence;
160
161     snsequence = sn_monitor_event_get_startup_sequence(event);
162
163     /* Get the corresponding internal startup sequence */
164     const char *id = sn_startup_sequence_get_id(snsequence);
165     struct Startup_Sequence *current, *sequence = NULL;
166     TAILQ_FOREACH(current, &startup_sequences, sequences) {
167         if (strcmp(current->id, id) != 0)
168             continue;
169
170         sequence = current;
171         break;
172     }
173
174     if (!sequence) {
175         DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
176         return;
177     }
178
179     switch (sn_monitor_event_get_type(event)) {
180         case SN_MONITOR_EVENT_COMPLETED:
181             DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
182
183             /* Unref the context, will be free()d */
184             sn_launcher_context_unref(sequence->context);
185
186             /* Delete our internal sequence */
187             TAILQ_REMOVE(&startup_sequences, sequence, sequences);
188
189             if (TAILQ_EMPTY(&startup_sequences)) {
190                 DLOG("No more startup sequences running, changing root window cursor to default pointer.\n");
191                 /* Change the pointer of the root window to indicate progress */
192                 if (xcursor_supported)
193                     xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
194                 else xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
195             }
196             break;
197         default:
198             /* ignore */
199             break;
200     }
201 }
202
203 /*
204  * Checks if the given window belongs to a startup notification by checking if
205  * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
206  * unset).
207  *
208  * If so, returns the workspace on which the startup was initiated.
209  * Returns NULL otherwise.
210  *
211  */
212 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
213     /* The _NET_STARTUP_ID is only needed during this function, so we get it
214      * here and don’t save it in the 'cwindow'. */
215     if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
216         FREE(startup_id_reply);
217         DLOG("No _NET_STARTUP_ID set on this window\n");
218         if (cwindow->leader == XCB_NONE)
219             return NULL;
220
221         xcb_get_property_cookie_t cookie;
222         cookie = xcb_get_property(conn, false, cwindow->leader, A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
223         DLOG("Checking leader window 0x%08x\n", cwindow->leader);
224         startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
225
226         if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
227             DLOG("No _NET_STARTUP_ID set on the leader either\n");
228             FREE(startup_id_reply);
229             return NULL;
230         }
231     }
232
233     char *startup_id;
234     if (asprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
235                  (char*)xcb_get_property_value(startup_id_reply)) == -1) {
236         perror("asprintf()");
237         DLOG("Could not get _NET_STARTUP_ID\n");
238         free(startup_id_reply);
239         return NULL;
240     }
241
242     struct Startup_Sequence *current, *sequence = NULL;
243     TAILQ_FOREACH(current, &startup_sequences, sequences) {
244         if (strcmp(current->id, startup_id) != 0)
245             continue;
246
247         sequence = current;
248         break;
249     }
250
251     free(startup_id);
252     free(startup_id_reply);
253
254     if (!sequence) {
255         DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
256         return NULL;
257     }
258
259     return sequence->workspace;
260 }