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