]> git.sur5r.net Git - i3/i3/blob - src/startup.c
implement a startup monitor, move code to src/startup.c
[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 /*
22  * Starts the given application by passing it through a shell. We use double fork
23  * to avoid zombie processes. As the started application’s parent exits (immediately),
24  * the application is reparented to init (process-id 1), which correctly handles
25  * childs, so we don’t have to do it :-).
26  *
27  * The shell is determined by looking for the SHELL environment variable. If it
28  * does not exist, /bin/sh is used.
29  *
30  */
31 void start_application(const char *command) {
32     /* Create a startup notification context to monitor the progress of this
33      * startup. */
34     SnLauncherContext *context;
35     context = sn_launcher_context_new(sndisplay, conn_screen);
36     sn_launcher_context_set_name(context, "i3");
37     sn_launcher_context_set_description(context, "exec command in i3");
38     /* Chop off everything starting from the first space (if there are any
39      * spaces in the command), since we don’t want the parameters. */
40     char *first_word = sstrdup(command);
41     char *space = strchr(first_word, ' ');
42     if (space)
43         *space = '\0';
44     sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
45     free(first_word);
46
47     LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
48
49     LOG("executing: %s\n", command);
50     if (fork() == 0) {
51         /* Child process */
52         setsid();
53         if (fork() == 0) {
54             /* Setup the environment variable(s) */
55             sn_launcher_context_setup_child_process(context);
56
57             /* Stores the path of the shell */
58             static const char *shell = NULL;
59
60             if (shell == NULL)
61                 if ((shell = getenv("SHELL")) == NULL)
62                     shell = "/bin/sh";
63
64             /* This is the child */
65             execl(shell, shell, "-c", command, (void*)NULL);
66             /* not reached */
67         }
68         exit(0);
69     }
70     wait(0);
71 }
72
73 /*
74  * Called by libstartup-notification when something happens
75  *
76  */
77 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
78     SnStartupSequence *sequence;
79
80     DLOG("something happened\n");
81     sequence = sn_monitor_event_get_startup_sequence(event);
82
83     switch (sn_monitor_event_get_type(event)) {
84         case SN_MONITOR_EVENT_COMPLETED:
85             DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(sequence));
86             break;
87         default:
88             /* ignore */
89             break;
90     }
91 }