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));