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