]> git.sur5r.net Git - i3/i3/blob - src/startup.c
dca7b999d39a38a47b480500e37915049ad36c40
[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  * Starts the given application by passing it through a shell. We use double fork
26  * to avoid zombie processes. As the started application’s parent exits (immediately),
27  * the application is reparented to init (process-id 1), which correctly handles
28  * childs, so we don’t have to do it :-).
29  *
30  * The shell is determined by looking for the SHELL environment variable. If it
31  * does not exist, /bin/sh is used.
32  *
33  */
34 void start_application(const char *command) {
35     /* Create a startup notification context to monitor the progress of this
36      * startup. */
37     SnLauncherContext *context;
38     context = sn_launcher_context_new(sndisplay, conn_screen);
39     sn_launcher_context_set_name(context, "i3");
40     sn_launcher_context_set_description(context, "exec command in i3");
41     /* Chop off everything starting from the first space (if there are any
42      * spaces in the command), since we don’t want the parameters. */
43     char *first_word = sstrdup(command);
44     char *space = strchr(first_word, ' ');
45     if (space)
46         *space = '\0';
47     sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
48     free(first_word);
49
50     LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
51
52     /* Save the ID and current workspace in our internal list of startup
53      * sequences */
54     Con *ws = con_get_workspace(focused);
55     struct Startup_Sequence *sequence = scalloc(sizeof(struct Startup_Sequence));
56     sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
57     sequence->workspace = sstrdup(ws->name);
58     TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
59
60     LOG("executing: %s\n", command);
61     if (fork() == 0) {
62         /* Child process */
63         setsid();
64         if (fork() == 0) {
65             /* Setup the environment variable(s) */
66             sn_launcher_context_setup_child_process(context);
67
68             /* Stores the path of the shell */
69             static const char *shell = NULL;
70
71             if (shell == NULL)
72                 if ((shell = getenv("SHELL")) == NULL)
73                     shell = "/bin/sh";
74
75             /* This is the child */
76             execl(shell, shell, "-c", command, (void*)NULL);
77             /* not reached */
78         }
79         exit(0);
80     }
81     wait(0);
82 }
83
84 /*
85  * Called by libstartup-notification when something happens
86  *
87  */
88 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
89     SnStartupSequence *snsequence;
90
91     snsequence = sn_monitor_event_get_startup_sequence(event);
92
93     /* Get the corresponding internal startup sequence */
94     const char *id = sn_startup_sequence_get_id(snsequence);
95     struct Startup_Sequence *current, *sequence = NULL;
96     TAILQ_FOREACH(current, &startup_sequences, sequences) {
97         if (strcmp(current->id, id) != 0)
98             continue;
99
100         sequence = current;
101         break;
102     }
103
104     if (!sequence) {
105         DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
106         return;
107     }
108
109     switch (sn_monitor_event_get_type(event)) {
110         case SN_MONITOR_EVENT_COMPLETED:
111             DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
112             break;
113         default:
114             /* ignore */
115             break;
116     }
117 }
118
119 /*
120  * Checks if the given window belongs to a startup notification by checking if
121  * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
122  * unset).
123  *
124  * If so, returns the workspace on which the startup was initiated.
125  * Returns NULL otherwise.
126  *
127  */
128 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
129     /* The _NET_STARTUP_ID is only needed during this function, so we get it
130      * here and don’t save it in the 'cwindow'. */
131     if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
132         FREE(startup_id_reply);
133         DLOG("No _NET_STARTUP_ID set on this window\n");
134         if (cwindow->leader == XCB_NONE)
135             return NULL;
136
137         xcb_get_property_cookie_t cookie;
138         cookie = xcb_get_property(conn, false, cwindow->leader, A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
139         DLOG("Checking leader window 0x%08x\n", cwindow->leader);
140         startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
141
142         if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
143             DLOG("No _NET_STARTUP_ID set on the leader either\n");
144             FREE(startup_id_reply);
145             return NULL;
146         }
147     }
148
149     char *startup_id;
150     if (asprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
151                  (char*)xcb_get_property_value(startup_id_reply)) == -1) {
152         perror("asprintf()");
153         DLOG("Could not get _NET_STARTUP_ID\n");
154         free(startup_id_reply);
155         return NULL;
156     }
157
158     struct Startup_Sequence *current, *sequence = NULL;
159     TAILQ_FOREACH(current, &startup_sequences, sequences) {
160         if (strcmp(current->id, startup_id) != 0)
161             continue;
162
163         sequence = current;
164         break;
165     }
166
167     free(startup_id);
168     free(startup_id_reply);
169
170     if (!sequence) {
171         DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
172         return NULL;
173     }
174
175     return sequence->workspace;
176 }