]> git.sur5r.net Git - i3/i3/blob - src/startup.c
Merge branch 'startup-notification' 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  *
6  * © 2009-2011 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * startup.c: Startup notification code. Ensures a startup notification context
11  * is setup when launching applications. We store the current workspace to open
12  * windows in that startup notification context on the appropriate workspace.
13  *
14  */
15 #include <sys/types.h>
16 #include <sys/wait.h>
17
18 #define SN_API_NOT_YET_FROZEN 1
19 #include <libsn/sn-launcher.h>
20
21 #include "all.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  */
69 void start_application(const char *command) {
70     /* Create a startup notification context to monitor the progress of this
71      * startup. */
72     SnLauncherContext *context;
73     context = sn_launcher_context_new(sndisplay, conn_screen);
74     sn_launcher_context_set_name(context, "i3");
75     sn_launcher_context_set_description(context, "exec command in i3");
76     /* Chop off everything starting from the first space (if there are any
77      * spaces in the command), since we don’t want the parameters. */
78     char *first_word = sstrdup(command);
79     char *space = strchr(first_word, ' ');
80     if (space)
81         *space = '\0';
82     sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
83     free(first_word);
84
85     /* Trigger a timeout after 60 seconds */
86     struct ev_timer *timeout = scalloc(sizeof(struct ev_timer));
87     ev_timer_init(timeout, startup_timeout, 60.0, 0.);
88     timeout->data = context;
89     ev_timer_start(main_loop, timeout);
90
91     LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
92
93     /* Save the ID and current workspace in our internal list of startup
94      * sequences */
95     Con *ws = con_get_workspace(focused);
96     struct Startup_Sequence *sequence = scalloc(sizeof(struct Startup_Sequence));
97     sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
98     sequence->workspace = sstrdup(ws->name);
99     sequence->context = context;
100     TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
101
102     /* Increase the refcount once (it starts with 1, so it will be 2 now) for
103      * the timeout. Even if the sequence gets completed, the timeout still
104      * needs the context (but will unref it then) */
105     sn_launcher_context_ref(context);
106
107     LOG("executing: %s\n", command);
108     if (fork() == 0) {
109         /* Child process */
110         setsid();
111         if (fork() == 0) {
112             /* Setup the environment variable(s) */
113             sn_launcher_context_setup_child_process(context);
114
115             /* Stores the path of the shell */
116             static const char *shell = NULL;
117
118             if (shell == NULL)
119                 if ((shell = getenv("SHELL")) == NULL)
120                     shell = "/bin/sh";
121
122             /* This is the child */
123             execl(shell, shell, "-c", command, (void*)NULL);
124             /* not reached */
125         }
126         exit(0);
127     }
128     wait(0);
129
130     /* Change the pointer of the root window to indicate progress */
131     if (xcursor_supported)
132         xcursor_set_root_cursor(XCURSOR_CURSOR_WATCH);
133     else xcb_set_root_cursor(XCURSOR_CURSOR_WATCH);
134 }
135
136 /*
137  * Called by libstartup-notification when something happens
138  *
139  */
140 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
141     SnStartupSequence *snsequence;
142
143     snsequence = sn_monitor_event_get_startup_sequence(event);
144
145     /* Get the corresponding internal startup sequence */
146     const char *id = sn_startup_sequence_get_id(snsequence);
147     struct Startup_Sequence *current, *sequence = NULL;
148     TAILQ_FOREACH(current, &startup_sequences, sequences) {
149         if (strcmp(current->id, id) != 0)
150             continue;
151
152         sequence = current;
153         break;
154     }
155
156     if (!sequence) {
157         DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
158         return;
159     }
160
161     switch (sn_monitor_event_get_type(event)) {
162         case SN_MONITOR_EVENT_COMPLETED:
163             DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
164
165             /* Unref the context, will be free()d */
166             sn_launcher_context_unref(sequence->context);
167
168             /* Delete our internal sequence */
169             TAILQ_REMOVE(&startup_sequences, sequence, sequences);
170
171             if (TAILQ_EMPTY(&startup_sequences)) {
172                 DLOG("No more startup sequences running, changing root window cursor to default pointer.\n");
173                 /* Change the pointer of the root window to indicate progress */
174                 if (xcursor_supported)
175                     xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
176                 else xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
177             }
178             break;
179         default:
180             /* ignore */
181             break;
182     }
183 }
184
185 /*
186  * Checks if the given window belongs to a startup notification by checking if
187  * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
188  * unset).
189  *
190  * If so, returns the workspace on which the startup was initiated.
191  * Returns NULL otherwise.
192  *
193  */
194 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
195     /* The _NET_STARTUP_ID is only needed during this function, so we get it
196      * here and don’t save it in the 'cwindow'. */
197     if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
198         FREE(startup_id_reply);
199         DLOG("No _NET_STARTUP_ID set on this window\n");
200         if (cwindow->leader == XCB_NONE)
201             return NULL;
202
203         xcb_get_property_cookie_t cookie;
204         cookie = xcb_get_property(conn, false, cwindow->leader, A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
205         DLOG("Checking leader window 0x%08x\n", cwindow->leader);
206         startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
207
208         if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
209             DLOG("No _NET_STARTUP_ID set on the leader either\n");
210             FREE(startup_id_reply);
211             return NULL;
212         }
213     }
214
215     char *startup_id;
216     if (asprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
217                  (char*)xcb_get_property_value(startup_id_reply)) == -1) {
218         perror("asprintf()");
219         DLOG("Could not get _NET_STARTUP_ID\n");
220         free(startup_id_reply);
221         return NULL;
222     }
223
224     struct Startup_Sequence *current, *sequence = NULL;
225     TAILQ_FOREACH(current, &startup_sequences, sequences) {
226         if (strcmp(current->id, startup_id) != 0)
227             continue;
228
229         sequence = current;
230         break;
231     }
232
233     free(startup_id);
234     free(startup_id_reply);
235
236     if (!sequence) {
237         DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
238         return NULL;
239     }
240
241     return sequence->workspace;
242 }