i3
src/startup.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * startup.c: Startup notification code. Ensures a startup notification context
00008  *            is setup when launching applications. We store the current
00009  *            workspace to open windows in that startup notification context on
00010  *            the appropriate workspace.
00011  *
00012  */
00013 #include "all.h"
00014 
00015 #include <sys/types.h>
00016 #include <sys/wait.h>
00017 
00018 #define SN_API_NOT_YET_FROZEN 1
00019 #include <libsn/sn-launcher.h>
00020 
00021 static TAILQ_HEAD(startup_sequence_head, Startup_Sequence) startup_sequences =
00022     TAILQ_HEAD_INITIALIZER(startup_sequences);
00023 
00024 /*
00025  * After 60 seconds, a timeout will be triggered for each startup sequence.
00026  *
00027  * The timeout will just trigger completion of the sequence, so the normal
00028  * completion process takes place (startup_monitor_event will free it).
00029  *
00030  */
00031 static void startup_timeout(EV_P_ ev_timer *w, int revents) {
00032     const char *id = sn_launcher_context_get_startup_id(w->data);
00033     DLOG("Timeout for startup sequence %s\n", id);
00034 
00035     struct Startup_Sequence *current, *sequence = NULL;
00036     TAILQ_FOREACH(current, &startup_sequences, sequences) {
00037         if (strcmp(current->id, id) != 0)
00038             continue;
00039 
00040         sequence = current;
00041         break;
00042     }
00043 
00044     /* Unref the context (for the timeout itself, see start_application) */
00045     sn_launcher_context_unref(w->data);
00046 
00047     if (!sequence) {
00048         DLOG("Sequence already deleted, nevermind.\n");
00049         return;
00050     }
00051 
00052     /* Complete the startup sequence, will trigger its deletion. */
00053     sn_launcher_context_complete(w->data);
00054     free(w);
00055 }
00056 
00057 /*
00058  * Starts the given application by passing it through a shell. We use double fork
00059  * to avoid zombie processes. As the started application’s parent exits (immediately),
00060  * the application is reparented to init (process-id 1), which correctly handles
00061  * childs, so we don’t have to do it :-).
00062  *
00063  * The shell is determined by looking for the SHELL environment variable. If it
00064  * does not exist, /bin/sh is used.
00065  *
00066  * The no_startup_id flag determines whether a startup notification context
00067  * (and ID) should be created, which is the default and encouraged behavior.
00068  *
00069  */
00070 void start_application(const char *command, bool no_startup_id) {
00071     SnLauncherContext *context;
00072 
00073     if (!no_startup_id) {
00074         /* Create a startup notification context to monitor the progress of this
00075          * startup. */
00076         context = sn_launcher_context_new(sndisplay, conn_screen);
00077         sn_launcher_context_set_name(context, "i3");
00078         sn_launcher_context_set_description(context, "exec command in i3");
00079         /* Chop off everything starting from the first space (if there are any
00080          * spaces in the command), since we don’t want the parameters. */
00081         char *first_word = sstrdup(command);
00082         char *space = strchr(first_word, ' ');
00083         if (space)
00084             *space = '\0';
00085         sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
00086         free(first_word);
00087 
00088         /* Trigger a timeout after 60 seconds */
00089         struct ev_timer *timeout = scalloc(sizeof(struct ev_timer));
00090         ev_timer_init(timeout, startup_timeout, 60.0, 0.);
00091         timeout->data = context;
00092         ev_timer_start(main_loop, timeout);
00093 
00094         LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
00095 
00096         /* Save the ID and current workspace in our internal list of startup
00097          * sequences */
00098         Con *ws = con_get_workspace(focused);
00099         struct Startup_Sequence *sequence = scalloc(sizeof(struct Startup_Sequence));
00100         sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
00101         sequence->workspace = sstrdup(ws->name);
00102         sequence->context = context;
00103         TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
00104 
00105         /* Increase the refcount once (it starts with 1, so it will be 2 now) for
00106          * the timeout. Even if the sequence gets completed, the timeout still
00107          * needs the context (but will unref it then) */
00108         sn_launcher_context_ref(context);
00109     }
00110 
00111     LOG("executing: %s\n", command);
00112     if (fork() == 0) {
00113         /* Child process */
00114         setsid();
00115         setrlimit(RLIMIT_CORE, &original_rlimit_core);
00116         if (fork() == 0) {
00117             /* Setup the environment variable(s) */
00118             if (!no_startup_id)
00119                 sn_launcher_context_setup_child_process(context);
00120 
00121             /* Stores the path of the shell */
00122             static const char *shell = NULL;
00123 
00124             if (shell == NULL)
00125                 if ((shell = getenv("SHELL")) == NULL)
00126                     shell = "/bin/sh";
00127 
00128             /* This is the child */
00129             execl(shell, shell, "-c", command, (void*)NULL);
00130             /* not reached */
00131         }
00132         _exit(0);
00133     }
00134     wait(0);
00135 
00136     if (!no_startup_id) {
00137         /* Change the pointer of the root window to indicate progress */
00138         if (xcursor_supported)
00139             xcursor_set_root_cursor(XCURSOR_CURSOR_WATCH);
00140         else xcb_set_root_cursor(XCURSOR_CURSOR_WATCH);
00141     }
00142 }
00143 
00144 /*
00145  * Called by libstartup-notification when something happens
00146  *
00147  */
00148 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
00149     SnStartupSequence *snsequence;
00150 
00151     snsequence = sn_monitor_event_get_startup_sequence(event);
00152 
00153     /* Get the corresponding internal startup sequence */
00154     const char *id = sn_startup_sequence_get_id(snsequence);
00155     struct Startup_Sequence *current, *sequence = NULL;
00156     TAILQ_FOREACH(current, &startup_sequences, sequences) {
00157         if (strcmp(current->id, id) != 0)
00158             continue;
00159 
00160         sequence = current;
00161         break;
00162     }
00163 
00164     if (!sequence) {
00165         DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
00166         return;
00167     }
00168 
00169     switch (sn_monitor_event_get_type(event)) {
00170         case SN_MONITOR_EVENT_COMPLETED:
00171             DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
00172 
00173             /* Unref the context, will be free()d */
00174             sn_launcher_context_unref(sequence->context);
00175 
00176             /* Delete our internal sequence */
00177             TAILQ_REMOVE(&startup_sequences, sequence, sequences);
00178 
00179             if (TAILQ_EMPTY(&startup_sequences)) {
00180                 DLOG("No more startup sequences running, changing root window cursor to default pointer.\n");
00181                 /* Change the pointer of the root window to indicate progress */
00182                 if (xcursor_supported)
00183                     xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
00184                 else xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
00185             }
00186             break;
00187         default:
00188             /* ignore */
00189             break;
00190     }
00191 }
00192 
00193 /*
00194  * Checks if the given window belongs to a startup notification by checking if
00195  * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
00196  * unset).
00197  *
00198  * If so, returns the workspace on which the startup was initiated.
00199  * Returns NULL otherwise.
00200  *
00201  */
00202 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
00203     /* The _NET_STARTUP_ID is only needed during this function, so we get it
00204      * here and don’t save it in the 'cwindow'. */
00205     if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
00206         FREE(startup_id_reply);
00207         DLOG("No _NET_STARTUP_ID set on this window\n");
00208         if (cwindow->leader == XCB_NONE)
00209             return NULL;
00210 
00211         xcb_get_property_cookie_t cookie;
00212         cookie = xcb_get_property(conn, false, cwindow->leader, A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
00213         DLOG("Checking leader window 0x%08x\n", cwindow->leader);
00214         startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
00215 
00216         if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
00217             DLOG("No _NET_STARTUP_ID set on the leader either\n");
00218             FREE(startup_id_reply);
00219             return NULL;
00220         }
00221     }
00222 
00223     char *startup_id;
00224     if (asprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
00225                  (char*)xcb_get_property_value(startup_id_reply)) == -1) {
00226         perror("asprintf()");
00227         DLOG("Could not get _NET_STARTUP_ID\n");
00228         free(startup_id_reply);
00229         return NULL;
00230     }
00231 
00232     struct Startup_Sequence *current, *sequence = NULL;
00233     TAILQ_FOREACH(current, &startup_sequences, sequences) {
00234         if (strcmp(current->id, startup_id) != 0)
00235             continue;
00236 
00237         sequence = current;
00238         break;
00239     }
00240 
00241     free(startup_id);
00242     free(startup_id_reply);
00243 
00244     if (!sequence) {
00245         DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
00246         return NULL;
00247     }
00248 
00249     return sequence->workspace;
00250 }