2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2011 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * startup.c: Startup notification code
13 #include <sys/types.h>
16 #define SN_API_NOT_YET_FROZEN 1
17 #include <libsn/sn-launcher.h>
21 static TAILQ_HEAD(startup_sequence_head, Startup_Sequence) startup_sequences =
22 TAILQ_HEAD_INITIALIZER(startup_sequences);
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 :-).
30 * The shell is determined by looking for the SHELL environment variable. If it
31 * does not exist, /bin/sh is used.
34 void start_application(const char *command) {
35 /* Create a startup notification context to monitor the progress of this
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, ' ');
47 sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
50 LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
52 /* Save the ID and current workspace in our internal list of startup
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);
60 LOG("executing: %s\n", command);
65 /* Setup the environment variable(s) */
66 sn_launcher_context_setup_child_process(context);
68 /* Stores the path of the shell */
69 static const char *shell = NULL;
72 if ((shell = getenv("SHELL")) == NULL)
75 /* This is the child */
76 execl(shell, shell, "-c", command, (void*)NULL);
85 * Called by libstartup-notification when something happens
88 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
89 SnStartupSequence *snsequence;
91 snsequence = sn_monitor_event_get_startup_sequence(event);
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)
105 DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
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));
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
124 * If so, returns the workspace on which the startup was initiated.
125 * Returns NULL otherwise.
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)
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);
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);
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);
158 struct Startup_Sequence *current, *sequence = NULL;
159 TAILQ_FOREACH(current, &startup_sequences, sequences) {
160 if (strcmp(current->id, startup_id) != 0)
168 free(startup_id_reply);
171 DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
175 return sequence->workspace;