]> git.sur5r.net Git - i3/i3/blob - src/startup.c
5fc875a3a3737bb6bf66e8d7fe96b6cdb5babc0b
[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 }