i3
startup.c
Go to the documentation of this file.
1 #undef I3__FILE__
2 #define I3__FILE__ "startup.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * startup.c: Startup notification code. Ensures a startup notification context
10  * is setup when launching applications. We store the current
11  * workspace to open windows in that startup notification context on
12  * the appropriate workspace.
13  *
14  */
15 #include "all.h"
16 #include "sd-daemon.h"
17 
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <paths.h>
21 
22 #define SN_API_NOT_YET_FROZEN 1
23 #include <libsn/sn-launcher.h>
24 
25 static TAILQ_HEAD(startup_sequence_head, Startup_Sequence) startup_sequences =
26  TAILQ_HEAD_INITIALIZER(startup_sequences);
27 
28 /*
29  * After 60 seconds, a timeout will be triggered for each startup sequence.
30  *
31  * The timeout will just trigger completion of the sequence, so the normal
32  * completion process takes place (startup_monitor_event will free it).
33  *
34  */
35 static void startup_timeout(EV_P_ ev_timer *w, int revents) {
36  const char *id = sn_launcher_context_get_startup_id(w->data);
37  DLOG("Timeout for startup sequence %s\n", id);
38 
39  struct Startup_Sequence *current, *sequence = NULL;
40  TAILQ_FOREACH(current, &startup_sequences, sequences) {
41  if (strcmp(current->id, id) != 0)
42  continue;
43 
44  sequence = current;
45  break;
46  }
47 
48  /* Unref the context (for the timeout itself, see start_application) */
49  sn_launcher_context_unref(w->data);
50 
51  if (!sequence) {
52  DLOG("Sequence already deleted, nevermind.\n");
53  return;
54  }
55 
56  /* Complete the startup sequence, will trigger its deletion. */
57  sn_launcher_context_complete(w->data);
58  free(w);
59 }
60 
61 /*
62  * Some applications (such as Firefox) mark a startup sequence as completed
63  * *before* they even map a window. Therefore, we cannot entirely delete the
64  * startup sequence once it’s marked as complete. Instead, we’ll mark it for
65  * deletion in 30 seconds and use that chance to delete old sequences.
66  *
67  * This function returns the number of active (!) startup notifications, that
68  * is, those which are not marked for deletion yet. This is used for changing
69  * the root window cursor.
70  *
71  */
72 static int _prune_startup_sequences(void) {
73  time_t current_time = time(NULL);
74  int active_sequences = 0;
75 
76  /* Traverse the list and delete everything which was marked for deletion 30
77  * seconds ago or earlier. */
78  struct Startup_Sequence *current, *next;
79  for (next = TAILQ_FIRST(&startup_sequences);
80  next != TAILQ_END(&startup_sequences);
81  ) {
82  current = next;
83  next = TAILQ_NEXT(next, sequences);
84 
85  if (current->delete_at == 0) {
86  active_sequences++;
87  continue;
88  }
89 
90  if (current_time <= current->delete_at)
91  continue;
92 
93  startup_sequence_delete(current);
94  }
95 
96  return active_sequences;
97 
98 }
99 
107  assert(sequence != NULL);
108  DLOG("Deleting startup sequence %s, delete_at = %ld, current_time = %ld\n",
109  sequence->id, sequence->delete_at, time(NULL));
110 
111  /* Unref the context, will be free()d */
112  sn_launcher_context_unref(sequence->context);
113 
114  /* Delete our internal sequence */
115  TAILQ_REMOVE(&startup_sequences, sequence, sequences);
116 
117  free(sequence->id);
118  free(sequence->workspace);
119  FREE(sequence);
120 }
121 
122 /*
123  * Starts the given application by passing it through a shell. We use double fork
124  * to avoid zombie processes. As the started application’s parent exits (immediately),
125  * the application is reparented to init (process-id 1), which correctly handles
126  * childs, so we don’t have to do it :-).
127  *
128  * The shell is determined by looking for the SHELL environment variable. If it
129  * does not exist, /bin/sh is used.
130  *
131  * The no_startup_id flag determines whether a startup notification context
132  * (and ID) should be created, which is the default and encouraged behavior.
133  *
134  */
135 void start_application(const char *command, bool no_startup_id) {
136  SnLauncherContext *context;
137 
138  if (!no_startup_id) {
139  /* Create a startup notification context to monitor the progress of this
140  * startup. */
141  context = sn_launcher_context_new(sndisplay, conn_screen);
142  sn_launcher_context_set_name(context, "i3");
143  sn_launcher_context_set_description(context, "exec command in i3");
144  /* Chop off everything starting from the first space (if there are any
145  * spaces in the command), since we don’t want the parameters. */
146  char *first_word = sstrdup(command);
147  char *space = strchr(first_word, ' ');
148  if (space)
149  *space = '\0';
150  sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
151  free(first_word);
152 
153  /* Trigger a timeout after 60 seconds */
154  struct ev_timer *timeout = scalloc(sizeof(struct ev_timer));
155  ev_timer_init(timeout, startup_timeout, 60.0, 0.);
156  timeout->data = context;
157  ev_timer_start(main_loop, timeout);
158 
159  LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
160 
161  /* Save the ID and current workspace in our internal list of startup
162  * sequences */
164  struct Startup_Sequence *sequence = scalloc(sizeof(struct Startup_Sequence));
165  sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
166  sequence->workspace = sstrdup(ws->name);
167  sequence->context = context;
168  TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
169 
170  /* Increase the refcount once (it starts with 1, so it will be 2 now) for
171  * the timeout. Even if the sequence gets completed, the timeout still
172  * needs the context (but will unref it then) */
173  sn_launcher_context_ref(context);
174  }
175 
176  LOG("executing: %s\n", command);
177  if (fork() == 0) {
178  /* Child process */
179  setsid();
180  setrlimit(RLIMIT_CORE, &original_rlimit_core);
181  /* Close all socket activation file descriptors explicitly, we disabled
182  * FD_CLOEXEC to keep them open when restarting i3. */
183  for (int fd = SD_LISTEN_FDS_START;
185  fd++) {
186  close(fd);
187  }
188  unsetenv("LISTEN_PID");
189  unsetenv("LISTEN_FDS");
190  if (fork() == 0) {
191  /* Setup the environment variable(s) */
192  if (!no_startup_id)
193  sn_launcher_context_setup_child_process(context);
194 
195  execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void*)NULL);
196  /* not reached */
197  }
198  _exit(0);
199  }
200  wait(0);
201 
202  if (!no_startup_id) {
203  /* Change the pointer of the root window to indicate progress */
204  if (xcursor_supported)
207  }
208 }
209 
210 /*
211  * Called by libstartup-notification when something happens
212  *
213  */
214 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
215  SnStartupSequence *snsequence;
216 
217  snsequence = sn_monitor_event_get_startup_sequence(event);
218 
219  /* Get the corresponding internal startup sequence */
220  const char *id = sn_startup_sequence_get_id(snsequence);
221  struct Startup_Sequence *current, *sequence = NULL;
222  TAILQ_FOREACH(current, &startup_sequences, sequences) {
223  if (strcmp(current->id, id) != 0)
224  continue;
225 
226  sequence = current;
227  break;
228  }
229 
230  if (!sequence) {
231  DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
232  return;
233  }
234 
235  switch (sn_monitor_event_get_type(event)) {
236  case SN_MONITOR_EVENT_COMPLETED:
237  DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
238 
239  /* Mark the given sequence for deletion in 30 seconds. */
240  time_t current_time = time(NULL);
241  sequence->delete_at = current_time + 30;
242  DLOG("Will delete startup sequence %s at timestamp %ld\n",
243  sequence->id, sequence->delete_at);
244 
245  if (_prune_startup_sequences() == 0) {
246  DLOG("No more startup sequences running, changing root window cursor to default pointer.\n");
247  /* Change the pointer of the root window to indicate progress */
248  if (xcursor_supported)
251  }
252  break;
253  default:
254  /* ignore */
255  break;
256  }
257 }
258 
264  xcb_get_property_reply_t *startup_id_reply, bool ignore_mapped_leader) {
265  /* The _NET_STARTUP_ID is only needed during this function, so we get it
266  * here and don’t save it in the 'cwindow'. */
267  if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
268  FREE(startup_id_reply);
269  DLOG("No _NET_STARTUP_ID set on window 0x%08x\n", cwindow->id);
270  if (cwindow->leader == XCB_NONE)
271  return NULL;
272 
273  /* This is a special case that causes the leader's startup sequence
274  * to only be returned if it has never been mapped, useful primarily
275  * when trying to delete a sequence.
276  *
277  * It's generally inappropriate to delete a leader's sequence when
278  * moving a child window, but if the leader has no container, it's
279  * likely permanently unmapped and the child is the "real" window. */
280  if (ignore_mapped_leader && con_by_window_id(cwindow->leader) != NULL) {
281  DLOG("Ignoring leader window 0x%08x\n", cwindow->leader);
282  return NULL;
283  }
284 
285  DLOG("Checking leader window 0x%08x\n", cwindow->leader);
286 
287  xcb_get_property_cookie_t cookie;
288 
289  cookie = xcb_get_property(conn, false, cwindow->leader,
290  A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
291  startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
292 
293  if (startup_id_reply == NULL ||
294  xcb_get_property_value_length(startup_id_reply) == 0) {
295  FREE(startup_id_reply);
296  DLOG("No _NET_STARTUP_ID set on the leader either\n");
297  return NULL;
298  }
299  }
300 
301  char *startup_id;
302  if (asprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
303  (char*)xcb_get_property_value(startup_id_reply)) == -1) {
304  perror("asprintf()");
305  DLOG("Could not get _NET_STARTUP_ID\n");
306  free(startup_id_reply);
307  return NULL;
308  }
309 
310  struct Startup_Sequence *current, *sequence = NULL;
311  TAILQ_FOREACH(current, &startup_sequences, sequences) {
312  if (strcmp(current->id, startup_id) != 0)
313  continue;
314 
315  sequence = current;
316  break;
317  }
318 
319  if (!sequence) {
320  DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
321  free(startup_id);
322  free(startup_id_reply);
323  return NULL;
324  }
325 
326  free(startup_id);
327  free(startup_id_reply);
328 
329  return sequence;
330 }
331 
332 /*
333  * Checks if the given window belongs to a startup notification by checking if
334  * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
335  * unset).
336  *
337  * If so, returns the workspace on which the startup was initiated.
338  * Returns NULL otherwise.
339  *
340  */
341 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
342  struct Startup_Sequence *sequence = startup_sequence_get(cwindow, startup_id_reply, false);
343  if (sequence == NULL)
344  return NULL;
345 
346  /* If the startup sequence's time span has elapsed, delete it. */
347  time_t current_time = time(NULL);
348  if (sequence->delete_at > 0 && current_time > sequence->delete_at) {
349  DLOG("Deleting expired startup sequence %s\n", sequence->id);
350  startup_sequence_delete(sequence);
351  return NULL;
352  }
353 
354  return sequence->workspace;
355 }