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