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