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