i3
startup.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * startup.c: Startup notification code. Ensures a startup notification context
8  * is setup when launching applications. We store the current
9  * workspace to open windows in that startup notification context on
10  * the appropriate workspace.
11  *
12  */
13 #include "all.h"
14 
15 #include "sd-daemon.h"
16 
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <paths.h>
20 
21 #define SN_API_NOT_YET_FROZEN 1
22 #include <libsn/sn-launcher.h>
23 
24 static TAILQ_HEAD(startup_sequence_head, Startup_Sequence) startup_sequences =
25  TAILQ_HEAD_INITIALIZER(startup_sequences);
26 
27 /*
28  * After 60 seconds, a timeout will be triggered for each startup sequence.
29  *
30  * The timeout will just trigger completion of the sequence, so the normal
31  * completion process takes place (startup_monitor_event will free it).
32  *
33  */
34 static void startup_timeout(EV_P_ ev_timer *w, int revents) {
35  const char *id = sn_launcher_context_get_startup_id(w->data);
36  DLOG("Timeout for startup sequence %s\n", id);
37 
38  struct Startup_Sequence *current, *sequence = NULL;
39  TAILQ_FOREACH(current, &startup_sequences, sequences) {
40  if (strcmp(current->id, id) != 0)
41  continue;
42 
43  sequence = current;
44  break;
45  }
46 
47  /* Unref the context (for the timeout itself, see start_application) */
48  sn_launcher_context_unref(w->data);
49 
50  if (!sequence) {
51  DLOG("Sequence already deleted, nevermind.\n");
52  free(w);
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  current = next;
82  next = TAILQ_NEXT(next, sequences);
83 
84  if (current->delete_at == 0) {
85  active_sequences++;
86  continue;
87  }
88 
89  if (current_time <= current->delete_at)
90  continue;
91 
92  startup_sequence_delete(current);
93  }
94 
95  return active_sequences;
96 }
97 
98 /*
99  * Deletes a startup sequence, ignoring whether its timeout has elapsed.
100  * Useful when e.g. a window is moved between workspaces and its children
101  * shouldn't spawn on the original workspace.
102  *
103  */
105  assert(sequence != NULL);
106  DLOG("Deleting startup sequence %s, delete_at = %lld, current_time = %lld\n",
107  sequence->id, (long long)sequence->delete_at, (long long)time(NULL));
108 
109  /* Unref the context, will be free()d */
110  sn_launcher_context_unref(sequence->context);
111 
112  /* Delete our internal sequence */
113  TAILQ_REMOVE(&startup_sequences, sequence, sequences);
114 
115  free(sequence->id);
116  free(sequence->workspace);
117  FREE(sequence);
118 }
119 
120 /*
121  * Starts the given application by passing it through a shell. We use double
122  * fork to avoid zombie processes. As the started application’s parent exits
123  * (immediately), the application is reparented to init (process-id 1), which
124  * correctly handles children, so we don’t have to do it :-).
125  *
126  * The shell used to start applications is the system's bourne shell (i.e.,
127  * /bin/sh).
128  *
129  * The no_startup_id flag determines whether a startup notification context
130  * (and ID) should be created, which is the default and encouraged behavior.
131  *
132  */
133 void start_application(const char *command, bool no_startup_id) {
134  SnLauncherContext *context = NULL;
135 
136  if (!no_startup_id) {
137  /* Create a startup notification context to monitor the progress of this
138  * startup. */
139  context = sn_launcher_context_new(sndisplay, conn_screen);
140  sn_launcher_context_set_name(context, "i3");
141  sn_launcher_context_set_description(context, "exec command in i3");
142  /* Chop off everything starting from the first space (if there are any
143  * spaces in the command), since we don’t want the parameters. */
144  char *first_word = sstrdup(command);
145  char *space = strchr(first_word, ' ');
146  if (space)
147  *space = '\0';
148  sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
149  free(first_word);
150 
151  /* Trigger a timeout after 60 seconds */
152  struct ev_timer *timeout = scalloc(1, sizeof(struct ev_timer));
153  ev_timer_init(timeout, startup_timeout, 60.0, 0.);
154  timeout->data = context;
155  ev_timer_start(main_loop, timeout);
156 
157  LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
158 
159  /* Save the ID and current workspace in our internal list of startup
160  * sequences */
162  struct Startup_Sequence *sequence = scalloc(1, sizeof(struct Startup_Sequence));
163  sequence->id = sstrdup(sn_launcher_context_get_startup_id(context));
164  sequence->workspace = sstrdup(ws->name);
165  sequence->context = context;
166  TAILQ_INSERT_TAIL(&startup_sequences, sequence, sequences);
167 
168  /* Increase the refcount once (it starts with 1, so it will be 2 now) for
169  * the timeout. Even if the sequence gets completed, the timeout still
170  * needs the context (but will unref it then) */
171  sn_launcher_context_ref(context);
172  }
173 
174  LOG("executing: %s\n", command);
175  if (fork() == 0) {
176  /* Child process */
177  setsid();
178  setrlimit(RLIMIT_CORE, &original_rlimit_core);
179  /* Close all socket activation file descriptors explicitly, we disabled
180  * FD_CLOEXEC to keep them open when restarting i3. */
181  for (int fd = SD_LISTEN_FDS_START;
183  fd++) {
184  close(fd);
185  }
186  unsetenv("LISTEN_PID");
187  unsetenv("LISTEN_FDS");
188  signal(SIGPIPE, SIG_DFL);
189  if (fork() == 0) {
190  /* Setup the environment variable(s) */
191  if (!no_startup_id)
192  sn_launcher_context_setup_child_process(context);
193  setenv("I3SOCK", current_socketpath, 1);
194 
195  execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, 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)
206  else
208  }
209 }
210 
211 /*
212  * Called by libstartup-notification when something happens
213  *
214  */
215 void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
216  SnStartupSequence *snsequence;
217 
218  snsequence = sn_monitor_event_get_startup_sequence(event);
219 
220  /* Get the corresponding internal startup sequence */
221  const char *id = sn_startup_sequence_get_id(snsequence);
222  struct Startup_Sequence *current, *sequence = NULL;
223  TAILQ_FOREACH(current, &startup_sequences, sequences) {
224  if (strcmp(current->id, id) != 0)
225  continue;
226 
227  sequence = current;
228  break;
229  }
230 
231  if (!sequence) {
232  DLOG("Got event for startup sequence that we did not initiate (ID = %s). Ignoring.\n", id);
233  return;
234  }
235 
236  switch (sn_monitor_event_get_type(event)) {
237  case SN_MONITOR_EVENT_COMPLETED:
238  DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(snsequence));
239 
240  /* Mark the given sequence for deletion in 30 seconds. */
241  time_t current_time = time(NULL);
242  sequence->delete_at = current_time + 30;
243  DLOG("Will delete startup sequence %s at timestamp %lld\n",
244  sequence->id, (long long)sequence->delete_at);
245 
246  if (_prune_startup_sequences() == 0) {
247  DLOG("No more startup sequences running, changing root window cursor to default pointer.\n");
248  /* Change the pointer of the root window to indicate progress */
249  if (xcursor_supported)
251  else
253  }
254  break;
255  default:
256  /* ignore */
257  break;
258  }
259 }
260 
261 /*
262  * Renames workspaces that are mentioned in the startup sequences.
263  *
264  */
265 void startup_sequence_rename_workspace(const char *old_name, const char *new_name) {
266  struct Startup_Sequence *current;
267  TAILQ_FOREACH(current, &startup_sequences, sequences) {
268  if (strcmp(current->workspace, old_name) != 0)
269  continue;
270  DLOG("Renaming workspace \"%s\" to \"%s\" in startup sequence %s.\n",
271  old_name, new_name, current->id);
272  free(current->workspace);
273  current->workspace = sstrdup(new_name);
274  }
275 }
276 
277 /*
278  * Gets the stored startup sequence for the _NET_STARTUP_ID of a given window.
279  *
280  */
282  xcb_get_property_reply_t *startup_id_reply, bool ignore_mapped_leader) {
283  /* The _NET_STARTUP_ID is only needed during this function, so we get it
284  * here and don’t save it in the 'cwindow'. */
285  if (startup_id_reply == NULL || xcb_get_property_value_length(startup_id_reply) == 0) {
286  FREE(startup_id_reply);
287  DLOG("No _NET_STARTUP_ID set on window 0x%08x\n", cwindow->id);
288  if (cwindow->leader == XCB_NONE)
289  return NULL;
290 
291  /* This is a special case that causes the leader's startup sequence
292  * to only be returned if it has never been mapped, useful primarily
293  * when trying to delete a sequence.
294  *
295  * It's generally inappropriate to delete a leader's sequence when
296  * moving a child window, but if the leader has no container, it's
297  * likely permanently unmapped and the child is the "real" window. */
298  if (ignore_mapped_leader && con_by_window_id(cwindow->leader) != NULL) {
299  DLOG("Ignoring leader window 0x%08x\n", cwindow->leader);
300  return NULL;
301  }
302 
303  DLOG("Checking leader window 0x%08x\n", cwindow->leader);
304 
305  xcb_get_property_cookie_t cookie;
306 
307  cookie = xcb_get_property(conn, false, cwindow->leader,
308  A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
309  startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
310 
311  if (startup_id_reply == NULL ||
312  xcb_get_property_value_length(startup_id_reply) == 0) {
313  FREE(startup_id_reply);
314  DLOG("No _NET_STARTUP_ID set on the leader either\n");
315  return NULL;
316  }
317  }
318 
319  char *startup_id;
320  sasprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
321  (char *)xcb_get_property_value(startup_id_reply));
322  struct Startup_Sequence *current, *sequence = NULL;
323  TAILQ_FOREACH(current, &startup_sequences, sequences) {
324  if (strcmp(current->id, startup_id) != 0)
325  continue;
326 
327  sequence = current;
328  break;
329  }
330 
331  if (!sequence) {
332  DLOG("WARNING: This sequence (ID %s) was not found\n", startup_id);
333  free(startup_id);
334  free(startup_id_reply);
335  return NULL;
336  }
337 
338  free(startup_id);
339  free(startup_id_reply);
340 
341  return sequence;
342 }
343 
344 /*
345  * Checks if the given window belongs to a startup notification by checking if
346  * the _NET_STARTUP_ID property is set on the window (or on its leader, if it’s
347  * unset).
348  *
349  * If so, returns the workspace on which the startup was initiated.
350  * Returns NULL otherwise.
351  *
352  */
353 char *startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply) {
354  struct Startup_Sequence *sequence = startup_sequence_get(cwindow, startup_id_reply, false);
355  if (sequence == NULL)
356  return NULL;
357 
358  /* If the startup sequence's time span has elapsed, delete it. */
359  time_t current_time = time(NULL);
360  if (sequence->delete_at > 0 && current_time > sequence->delete_at) {
361  DLOG("Deleting expired startup sequence %s\n", sequence->id);
362  startup_sequence_delete(sequence);
363  return NULL;
364  }
365 
366  return sequence->workspace;
367 }
#define FREE(pointer)
Definition: util.h:47
char * id
startup ID for this sequence, generated by libstartup-notification
Definition: data.h:231
char * current_socketpath
Definition: ipc.c:23
Stores internal information about a startup sequence, like the workspace it was initiated on...
Definition: data.h:229
time_t delete_at
time at which this sequence should be deleted (after it was marked as completed)
Definition: data.h:238
void xcb_set_root_cursor(int cursor)
Set the cursor of the root window to the given cursor id.
Definition: xcb.c:184
#define DLOG(fmt,...)
Definition: libi3.h:104
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:44
struct Startup_Sequence * startup_sequence_get(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply, bool ignore_mapped_leader)
Gets the stored startup sequence for the _NET_STARTUP_ID of a given window.
Definition: startup.c:281
bool xcursor_supported
Definition: main.c:90
void start_application(const char *command, bool no_startup_id)
Starts the given application by passing it through a shell.
Definition: startup.c:133
SnLauncherContext * context
libstartup-notification context for this launch
Definition: data.h:235
char * workspace
workspace on which this startup was initiated
Definition: data.h:233
void startup_monitor_event(SnMonitorEvent *event, void *userdata)
Called by libstartup-notification when something happens.
Definition: startup.c:215
Con * con_by_window_id(xcb_window_t window)
Returns the container with the given client window ID or NULL if no such container exists...
Definition: con.c:612
A &#39;Window&#39; is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:410
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
struct rlimit original_rlimit_core
The original value of RLIMIT_CORE when i3 was started.
Definition: main.c:33
static int _prune_startup_sequences(void)
Definition: startup.c:72
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:613
#define TAILQ_FIRST(head)
Definition: queue.h:336
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:418
xcb_window_t leader
Holds the xcb_window_t (just an ID) for the leader window (logical parent for toolwindows and similar...
Definition: data.h:415
#define SD_LISTEN_FDS_START
Definition: sd-daemon.h:102
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
static TAILQ_HEAD(startup_sequence_head, Startup_Sequence)
Definition: startup.c:24
char * name
Definition: data.h:659
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
char * startup_workspace_for_window(i3Window *cwindow, xcb_get_property_reply_t *startup_id_reply)
Checks if the given window belongs to a startup notification by checking if the _NET_STARTUP_ID prope...
Definition: startup.c:353
#define TAILQ_END(head)
Definition: queue.h:337
#define LOG(fmt,...)
Definition: libi3.h:94
SnDisplay * sndisplay
Definition: main.c:49
struct Con * focused
Definition: tree.c:13
void startup_sequence_rename_workspace(const char *old_name, const char *new_name)
Renames workspaces that are mentioned in the startup sequences.
Definition: startup.c:265
void startup_sequence_delete(struct Startup_Sequence *sequence)
Deletes a startup sequence, ignoring whether its timeout has elapsed.
Definition: startup.c:104
Used during the config file lexing/parsing to keep the state of the lexer in order to provide useful ...
Definition: configuration.h:34
void xcursor_set_root_cursor(int cursor_id)
Sets the cursor of the root window to the &#39;pointer&#39; cursor.
Definition: xcursor.c:57
struct ev_loop * main_loop
Definition: main.c:66
int conn_screen
Definition: main.c:46
xcb_timestamp_t last_timestamp
The last timestamp we got from X11 (timestamps are included in some events and are used for some thin...
Definition: main.c:54
static struct context * context
Definition: config_parser.c:48
xcb_window_t id
Definition: data.h:411
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
int listen_fds
The number of file descriptors passed via socket activation.
Definition: main.c:36