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