i3
main.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 * main.c: Initialization, main loop
8 *
9 */
10#include "all.h"
11#include "shmlog.h"
12
13#include <libev/ev.h>
14#include <fcntl.h>
15#include <getopt.h>
16#include <libgen.h>
17#include <locale.h>
18#include <signal.h>
19#include <sys/mman.h>
20#include <sys/resource.h>
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/time.h>
24#include <sys/types.h>
25#include <sys/un.h>
26#include <unistd.h>
27#include <xcb/xcb_atom.h>
28#include <xcb/xinerama.h>
29#include <xcb/bigreq.h>
30
31#ifdef I3_ASAN_ENABLED
32#include <sanitizer/lsan_interface.h>
33#endif
34
35#include "sd-daemon.h"
36
39
40/* The original value of RLIMIT_CORE when i3 was started. We need to restore
41 * this before starting any other process, since we set RLIMIT_CORE to
42 * RLIM_INFINITY for i3 debugging versions. */
44
45/* The number of file descriptors passed via socket activation. */
47
48/* We keep the xcb_prepare watcher around to be able to enable and disable it
49 * temporarily for drag_pointer(). */
50static struct ev_prepare *xcb_prepare;
51
53
54xcb_connection_t *conn;
55/* The screen (0 when you are using DISPLAY=:0) of the connection 'conn' */
57
58/* Display handle for libstartup-notification */
59SnDisplay *sndisplay;
60
61/* The last timestamp we got from X11 (timestamps are included in some events
62 * and are used for some things, like determining a unique ID in startup
63 * notification). */
64xcb_timestamp_t last_timestamp = XCB_CURRENT_TIME;
65
66xcb_screen_t *root_screen;
67xcb_window_t root;
68
70xcb_atom_t wm_sn;
71
72/* Color depth, visual id and colormap to use when creating windows and
73 * pixmaps. Will use 32 bit depth and an appropriate visual, if available,
74 * otherwise the root window’s default (usually 24 bit TrueColor). */
75uint8_t root_depth;
76xcb_visualtype_t *visual_type;
77xcb_colormap_t colormap;
78
79struct ev_loop *main_loop;
80
81xcb_key_symbols_t *keysyms;
82
83/* Default shmlog size if not set by user. */
84const int default_shmlog_size = 25 * 1024 * 1024;
85
86/* The list of key bindings */
87struct bindings_head *bindings;
88const char *current_binding_mode = NULL;
89
90/* The list of exec-lines */
92
93/* The list of exec_always lines */
95
96/* The list of assignments */
98
99/* The list of workspace assignments (which workspace should end up on which
100 * output) */
102
103/* We hope that those are supported and set them to true */
104bool xkb_supported = true;
105bool shape_supported = true;
106
107bool force_xinerama = false;
108
109/* Define all atoms as global variables */
110#define xmacro(atom) xcb_atom_t A_##atom;
113#undef xmacro
114
115/*
116 * This callback is only a dummy, see xcb_prepare_cb.
117 * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
118 *
119 */
120static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
121 /* empty, because xcb_prepare_cb are used */
122}
123
124/*
125 * Called just before the event loop sleeps. Ensures xcb’s incoming and outgoing
126 * queues are empty so that any activity will trigger another event loop
127 * iteration, and hence another xcb_prepare_cb invocation.
128 *
129 */
130static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
131 /* Process all queued (and possibly new) events before the event loop
132 sleeps. */
133 xcb_generic_event_t *event;
134
135 while ((event = xcb_poll_for_event(conn)) != NULL) {
136 if (event->response_type == 0) {
137 if (event_is_ignored(event->sequence, 0))
138 DLOG("Expected X11 Error received for sequence %x\n", event->sequence);
139 else {
140 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
141 DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
142 error->sequence, error->error_code);
143 }
144 free(event);
145 continue;
146 }
147
148 /* Strip off the highest bit (set if the event is generated) */
149 int type = (event->response_type & 0x7F);
150
151 handle_event(type, event);
152
153 free(event);
154 }
155
156 /* Flush all queued events to X11. */
157 xcb_flush(conn);
158}
159
160/*
161 * Enable or disable the main X11 event handling function.
162 * This is used by drag_pointer() which has its own, modal event handler, which
163 * takes precedence over the normal event handler.
164 *
165 */
166void main_set_x11_cb(bool enable) {
167 DLOG("Setting main X11 callback to enabled=%d\n", enable);
168 if (enable) {
169 ev_prepare_start(main_loop, xcb_prepare);
170 /* Trigger the watcher explicitly to handle all remaining X11 events.
171 * drag_pointer()’s event handler exits in the middle of the loop. */
172 ev_feed_event(main_loop, xcb_prepare, 0);
173 } else {
174 ev_prepare_stop(main_loop, xcb_prepare);
175 }
176}
177
178/*
179 * Exit handler which destroys the main_loop. Will trigger cleanup handlers.
180 *
181 */
182static void i3_exit(void) {
183 if (*shmlogname != '\0') {
184 fprintf(stderr, "Closing SHM log \"%s\"\n", shmlogname);
185 fflush(stderr);
186 shm_unlink(shmlogname);
187 }
189 unlink(config.ipc_socket_path);
190 if (current_log_stream_socket_path != NULL) {
192 }
193 xcb_disconnect(conn);
194
195 /* If a nagbar is active, kill it */
198
199/* We need ev >= 4 for the following code. Since it is not *that* important (it
200 * only makes sure that there are no i3-nagbar instances left behind) we still
201 * support old systems with libev 3. */
202#if EV_VERSION_MAJOR >= 4
203 ev_loop_destroy(main_loop);
204#endif
205
206#ifdef I3_ASAN_ENABLED
207 __lsan_do_leak_check();
208#endif
209}
210
211/*
212 * (One-shot) Handler for all signals with default action "Core", see signal(7)
213 *
214 * Unlinks the SHM log and re-raises the signal.
215 *
216 */
217static void handle_core_signal(int sig, siginfo_t *info, void *data) {
218 if (*shmlogname != '\0') {
219 shm_unlink(shmlogname);
220 }
221 raise(sig);
222}
223
224/*
225 * (One-shot) Handler for all signals with default action "Term", see signal(7)
226 *
227 * Exits the program gracefully.
228 *
229 */
230static void handle_term_signal(struct ev_loop *loop, ev_signal *signal, int revents) {
231 /* We exit gracefully here in the sense that cleanup handlers
232 * installed via atexit are invoked. */
233 exit(128 + signal->signum);
234}
235
236/*
237 * Set up handlers for all signals with default action "Term", see signal(7)
238 *
239 */
240static void setup_term_handlers(void) {
241 static struct ev_signal signal_watchers[6];
242 size_t num_watchers = sizeof(signal_watchers) / sizeof(signal_watchers[0]);
243
244 /* We have to rely on libev functionality here and should not use
245 * sigaction handlers because we need to invoke the exit handlers
246 * and cannot do so from an asynchronous signal handling context as
247 * not all code triggered during exit is signal safe (and exiting
248 * the main loop from said handler is not easily possible). libev's
249 * signal handlers does not impose such a constraint on us. */
250 ev_signal_init(&signal_watchers[0], handle_term_signal, SIGHUP);
251 ev_signal_init(&signal_watchers[1], handle_term_signal, SIGINT);
252 ev_signal_init(&signal_watchers[2], handle_term_signal, SIGALRM);
253 ev_signal_init(&signal_watchers[3], handle_term_signal, SIGTERM);
254 ev_signal_init(&signal_watchers[4], handle_term_signal, SIGUSR1);
255 ev_signal_init(&signal_watchers[5], handle_term_signal, SIGUSR1);
256 for (size_t i = 0; i < num_watchers; i++) {
257 ev_signal_start(main_loop, &signal_watchers[i]);
258 /* The signal handlers should not block ev_run from returning
259 * and so none of the signal handlers should hold a reference to
260 * the main loop. */
261 ev_unref(main_loop);
262 }
263}
264
265static int parse_restart_fd(void) {
266 const char *restart_fd = getenv("_I3_RESTART_FD");
267 if (restart_fd == NULL) {
268 return -1;
269 }
270
271 long int fd = -1;
272 if (!parse_long(restart_fd, &fd, 10)) {
273 ELOG("Malformed _I3_RESTART_FD \"%s\"\n", restart_fd);
274 return -1;
275 }
276 return fd;
277}
278
279int main(int argc, char *argv[]) {
280 /* Keep a symbol pointing to the I3_VERSION string constant so that we have
281 * it in gdb backtraces. */
282 static const char *_i3_version __attribute__((used)) = I3_VERSION;
283 char *override_configpath = NULL;
284 bool autostart = true;
285 char *layout_path = NULL;
286 bool delete_layout_path = false;
287 bool disable_randr15 = false;
288 char *fake_outputs = NULL;
289 bool disable_signalhandler = false;
290 bool only_check_config = false;
291 bool replace_wm = false;
292 static struct option long_options[] = {
293 {"no-autostart", no_argument, 0, 'a'},
294 {"config", required_argument, 0, 'c'},
295 {"version", no_argument, 0, 'v'},
296 {"moreversion", no_argument, 0, 'm'},
297 {"more-version", no_argument, 0, 'm'},
298 {"more_version", no_argument, 0, 'm'},
299 {"help", no_argument, 0, 'h'},
300 {"layout", required_argument, 0, 'L'},
301 {"restart", required_argument, 0, 0},
302 {"force-xinerama", no_argument, 0, 0},
303 {"force_xinerama", no_argument, 0, 0},
304 {"disable-randr15", no_argument, 0, 0},
305 {"disable_randr15", no_argument, 0, 0},
306 {"disable-signalhandler", no_argument, 0, 0},
307 {"shmlog-size", required_argument, 0, 0},
308 {"shmlog_size", required_argument, 0, 0},
309 {"get-socketpath", no_argument, 0, 0},
310 {"get_socketpath", no_argument, 0, 0},
311 {"fake_outputs", required_argument, 0, 0},
312 {"fake-outputs", required_argument, 0, 0},
313 {"force-old-config-parser-v4.4-only", no_argument, 0, 0},
314 {"replace", no_argument, 0, 'r'},
315 {0, 0, 0, 0}};
316 int option_index = 0, opt;
317
318 setlocale(LC_ALL, "");
319
320 /* Get the RLIMIT_CORE limit at startup time to restore this before
321 * starting processes. */
322 getrlimit(RLIMIT_CORE, &original_rlimit_core);
323
324 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
325 if (!isatty(fileno(stdout)))
326 setbuf(stdout, NULL);
327
328 srand(time(NULL));
329
330 /* Init logging *before* initializing debug_build to guarantee early
331 * (file) logging. */
332 init_logging();
333
334 /* On release builds, disable SHM logging by default. */
335 shmlog_size = (is_debug_build() || strstr(argv[0], "i3-with-shmlog") != NULL ? default_shmlog_size : 0);
336
337 start_argv = argv;
338
339 while ((opt = getopt_long(argc, argv, "c:CvmaL:hld:Vr", long_options, &option_index)) != -1) {
340 switch (opt) {
341 case 'a':
342 LOG("Autostart disabled using -a\n");
343 autostart = false;
344 break;
345 case 'L':
346 FREE(layout_path);
347 layout_path = sstrdup(optarg);
348 delete_layout_path = false;
349 break;
350 case 'c':
351 FREE(override_configpath);
352 override_configpath = sstrdup(optarg);
353 break;
354 case 'C':
355 LOG("Checking configuration file only (-C)\n");
356 only_check_config = true;
357 break;
358 case 'v':
359 printf("i3 version %s © 2009 Michael Stapelberg and contributors\n", i3_version);
360 exit(EXIT_SUCCESS);
361 break;
362 case 'm':
363 printf("Binary i3 version: %s © 2009 Michael Stapelberg and contributors\n", i3_version);
365 exit(EXIT_SUCCESS);
366 break;
367 case 'V':
368 set_verbosity(true);
369 break;
370 case 'd':
371 LOG("Enabling debug logging\n");
372 set_debug_logging(true);
373 break;
374 case 'l':
375 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
376 break;
377 case 'r':
378 replace_wm = true;
379 break;
380 case 0:
381 if (strcmp(long_options[option_index].name, "force-xinerama") == 0 ||
382 strcmp(long_options[option_index].name, "force_xinerama") == 0) {
383 force_xinerama = true;
384 ELOG("Using Xinerama instead of RandR. This option should be "
385 "avoided at all cost because it does not refresh the list "
386 "of screens, so you cannot configure displays at runtime. "
387 "Please check if your driver really does not support RandR "
388 "and disable this option as soon as you can.\n");
389 break;
390 } else if (strcmp(long_options[option_index].name, "disable-randr15") == 0 ||
391 strcmp(long_options[option_index].name, "disable_randr15") == 0) {
392 disable_randr15 = true;
393 break;
394 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
395 disable_signalhandler = true;
396 break;
397 } else if (strcmp(long_options[option_index].name, "get-socketpath") == 0 ||
398 strcmp(long_options[option_index].name, "get_socketpath") == 0) {
399 char *socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
400 if (socket_path) {
401 printf("%s\n", socket_path);
402 /* With -O2 (i.e. the buildtype=debugoptimized meson
403 * option, which we set by default), gcc 9.2.1 optimizes
404 * away socket_path at this point, resulting in a Leak
405 * Sanitizer report. An explicit free helps: */
406 free(socket_path);
407 exit(EXIT_SUCCESS);
408 }
409
410 exit(EXIT_FAILURE);
411 } else if (strcmp(long_options[option_index].name, "shmlog-size") == 0 ||
412 strcmp(long_options[option_index].name, "shmlog_size") == 0) {
413 shmlog_size = atoi(optarg);
414 /* Re-initialize logging immediately to get as many
415 * logmessages as possible into the SHM log. */
416 init_logging();
417 LOG("Limiting SHM log size to %d bytes\n", shmlog_size);
418 break;
419 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
420 FREE(layout_path);
421 layout_path = sstrdup(optarg);
422 delete_layout_path = true;
423 break;
424 } else if (strcmp(long_options[option_index].name, "fake-outputs") == 0 ||
425 strcmp(long_options[option_index].name, "fake_outputs") == 0) {
426 LOG("Initializing fake outputs: %s\n", optarg);
427 fake_outputs = sstrdup(optarg);
428 break;
429 } else if (strcmp(long_options[option_index].name, "force-old-config-parser-v4.4-only") == 0) {
430 ELOG("You are passing --force-old-config-parser-v4.4-only, but that flag was removed by now.\n");
431 break;
432 }
433 /* fall-through */
434 default:
435 fprintf(stderr, "Usage: %s [-c configfile] [-d all] [-a] [-v] [-V] [-C]\n", argv[0]);
436 fprintf(stderr, "\n");
437 fprintf(stderr, "\t-a disable autostart ('exec' lines in config)\n");
438 fprintf(stderr, "\t-c <file> use the provided configfile instead\n");
439 fprintf(stderr, "\t-C validate configuration file and exit\n");
440 fprintf(stderr, "\t-d all enable debug output\n");
441 fprintf(stderr, "\t-L <file> path to the serialized layout during restarts\n");
442 fprintf(stderr, "\t-v display version and exit\n");
443 fprintf(stderr, "\t-V enable verbose mode\n");
444 fprintf(stderr, "\n");
445 fprintf(stderr, "\t--force-xinerama\n"
446 "\tUse Xinerama instead of RandR.\n"
447 "\tThis option should only be used if you are stuck with the\n"
448 "\told nVidia closed source driver (older than 302.17), which does\n"
449 "\tnot support RandR.\n");
450 fprintf(stderr, "\n");
451 fprintf(stderr, "\t--get-socketpath\n"
452 "\tRetrieve the i3 IPC socket path from X11, print it, then exit.\n");
453 fprintf(stderr, "\n");
454 fprintf(stderr, "\t--shmlog-size <limit>\n"
455 "\tLimits the size of the i3 SHM log to <limit> bytes. Setting this\n"
456 "\tto 0 disables SHM logging entirely.\n"
457 "\tThe default is %d bytes.\n",
459 fprintf(stderr, "\n");
460 fprintf(stderr, "\t--replace\n"
461 "\tReplace an existing window manager.\n");
462 fprintf(stderr, "\n");
463 fprintf(stderr, "If you pass plain text arguments, i3 will interpret them as a command\n"
464 "to send to a currently running i3 (like i3-msg). This allows you to\n"
465 "use nice and logical commands, such as:\n"
466 "\n"
467 "\ti3 border none\n"
468 "\ti3 floating toggle\n"
469 "\ti3 kill window\n"
470 "\n");
471 exit(opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE);
472 }
473 }
474
475 if (only_check_config) {
476 exit(load_configuration(override_configpath, C_VALIDATE) ? EXIT_SUCCESS : EXIT_FAILURE);
477 }
478
479 /* If the user passes more arguments, we act like i3-msg would: Just send
480 * the arguments as an IPC message to i3. This allows for nice semantic
481 * commands such as 'i3 border none'. */
482 if (optind < argc) {
483 /* We enable verbose mode so that the user knows what’s going on.
484 * This should make it easier to find mistakes when the user passes
485 * arguments by mistake. */
486 set_verbosity(true);
487
488 LOG("Additional arguments passed. Sending them as a command to i3.\n");
489 char *payload = NULL;
490 while (optind < argc) {
491 if (!payload) {
492 payload = sstrdup(argv[optind]);
493 } else {
494 char *both;
495 sasprintf(&both, "%s %s", payload, argv[optind]);
496 free(payload);
497 payload = both;
498 }
499 optind++;
500 }
501 DLOG("Command is: %s (%zd bytes)\n", payload, strlen(payload));
502 char *socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
503 if (!socket_path) {
504 ELOG("Could not get i3 IPC socket path\n");
505 return 1;
506 }
507
508 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
509 if (sockfd == -1)
510 err(EXIT_FAILURE, "Could not create socket");
511
512 struct sockaddr_un addr;
513 memset(&addr, 0, sizeof(struct sockaddr_un));
514 addr.sun_family = AF_LOCAL;
515 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
516 FREE(socket_path);
517 if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
518 err(EXIT_FAILURE, "Could not connect to i3");
519
520 if (ipc_send_message(sockfd, strlen(payload), I3_IPC_MESSAGE_TYPE_RUN_COMMAND,
521 (uint8_t *)payload) == -1)
522 err(EXIT_FAILURE, "IPC: write()");
523 FREE(payload);
524
525 uint32_t reply_length;
526 uint32_t reply_type;
527 uint8_t *reply;
528 int ret;
529 if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
530 if (ret == -1)
531 err(EXIT_FAILURE, "IPC: read()");
532 return 1;
533 }
534 if (reply_type != I3_IPC_REPLY_TYPE_COMMAND)
535 errx(EXIT_FAILURE, "IPC: received reply of type %d but expected %d (COMMAND)", reply_type, I3_IPC_REPLY_TYPE_COMMAND);
536 printf("%.*s\n", reply_length, reply);
537 FREE(reply);
538 return 0;
539 }
540
541 /* Enable logging to handle the case when the user did not specify --shmlog-size */
542 init_logging();
543
544 /* Try to enable core dumps by default when running a debug build */
545 if (is_debug_build()) {
546 struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY};
547 setrlimit(RLIMIT_CORE, &limit);
548
549 /* The following code is helpful, but not required. We thus don’t pay
550 * much attention to error handling, non-linux or other edge cases. */
551 LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
552 size_t cwd_size = 1024;
553 char *cwd = smalloc(cwd_size);
554 char *cwd_ret;
555 while ((cwd_ret = getcwd(cwd, cwd_size)) == NULL && errno == ERANGE) {
556 cwd_size = cwd_size * 2;
557 cwd = srealloc(cwd, cwd_size);
558 }
559 if (cwd_ret != NULL)
560 LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
561 int patternfd;
562 if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
563 memset(cwd, '\0', cwd_size);
564 if (read(patternfd, cwd, cwd_size) > 0)
565 /* a trailing newline is included in cwd */
566 LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
567 close(patternfd);
568 }
569 free(cwd);
570 }
571
572 LOG("i3 %s starting\n", i3_version);
573
574 conn = xcb_connect(NULL, &conn_screen);
575 if (xcb_connection_has_error(conn))
576 errx(EXIT_FAILURE, "Cannot open display");
577
578 sndisplay = sn_xcb_display_new(conn, NULL, NULL);
579
580 /* Initialize the libev event loop. This needs to be done before loading
581 * the config file because the parser will install an ev_child watcher
582 * for the nagbar when config errors are found.
583 *
584 * Main loop must be ev's default loop because (at the moment of writing)
585 * only the default loop can handle ev_child events and reap zombies
586 * (the start_application routine relies on that too). */
587 main_loop = EV_DEFAULT;
588 if (main_loop == NULL)
589 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
590
591 root_screen = xcb_aux_get_screen(conn, conn_screen);
592 root = root_screen->root;
593
594 /* Prefetch X11 extensions that we are interested in. */
595 xcb_prefetch_extension_data(conn, &xcb_xkb_id);
596 xcb_prefetch_extension_data(conn, &xcb_shape_id);
597 /* BIG-REQUESTS is used by libxcb internally. */
598 xcb_prefetch_extension_data(conn, &xcb_big_requests_id);
599 if (force_xinerama) {
600 xcb_prefetch_extension_data(conn, &xcb_xinerama_id);
601 } else {
602 xcb_prefetch_extension_data(conn, &xcb_randr_id);
603 }
604
605 /* Prepare for us to get a current timestamp as recommended by ICCCM */
606 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_PROPERTY_CHANGE});
607 xcb_change_property(conn, XCB_PROP_MODE_APPEND, root, XCB_ATOM_SUPERSCRIPT_X, XCB_ATOM_CARDINAL, 32, 0, "");
608
609 /* Place requests for the atoms we need as soon as possible */
610#define xmacro(atom) \
611 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
614#undef xmacro
615
616 root_depth = root_screen->root_depth;
617 colormap = root_screen->default_colormap;
618 visual_type = xcb_aux_find_visual_by_attrs(root_screen, -1, 32);
619 if (visual_type != NULL) {
620 root_depth = xcb_aux_get_depth_of_visual(root_screen, visual_type->visual_id);
621 colormap = xcb_generate_id(conn);
622
623 xcb_void_cookie_t cm_cookie = xcb_create_colormap_checked(conn,
624 XCB_COLORMAP_ALLOC_NONE,
625 colormap,
626 root,
627 visual_type->visual_id);
628
629 xcb_generic_error_t *error = xcb_request_check(conn, cm_cookie);
630 if (error != NULL) {
631 ELOG("Could not create colormap. Error code: %d\n", error->error_code);
632 exit(EXIT_FAILURE);
633 }
634 } else {
636 }
637
638 xcb_prefetch_maximum_request_length(conn);
639
640 init_dpi();
641
642 DLOG("root_depth = %d, visual_id = 0x%08x.\n", root_depth, visual_type->visual_id);
643 DLOG("root_screen->height_in_pixels = %d, root_screen->height_in_millimeters = %d\n",
644 root_screen->height_in_pixels, root_screen->height_in_millimeters);
645 DLOG("One logical pixel corresponds to %d physical pixels on this display.\n", logical_px(1));
646
647 xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
648 xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
649
650 /* Get the PropertyNotify event we caused above */
651 xcb_flush(conn);
652 {
653 xcb_generic_event_t *event;
654 DLOG("waiting for PropertyNotify event\n");
655 while ((event = xcb_wait_for_event(conn)) != NULL) {
656 if (event->response_type == XCB_PROPERTY_NOTIFY) {
657 last_timestamp = ((xcb_property_notify_event_t *)event)->time;
658 free(event);
659 break;
660 }
661 free(event);
662 }
663 DLOG("got timestamp %d\n", last_timestamp);
664 }
665
666 /* Setup NetWM atoms */
667#define xmacro(name) \
668 do { \
669 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
670 if (!reply) { \
671 ELOG("Could not get atom " #name "\n"); \
672 exit(-1); \
673 } \
674 A_##name = reply->atom; \
675 free(reply); \
676 } while (0);
679#undef xmacro
680
681 load_configuration(override_configpath, C_LOAD);
682
683 if (config.ipc_socket_path == NULL) {
684 /* Fall back to a file name in /tmp/ based on the PID */
685 if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
687 else
689 }
690 /* Create the UNIX domain socket for IPC */
692 if (ipc_socket == -1) {
693 die("Could not create the IPC socket: %s", config.ipc_socket_path);
694 }
695
697 force_xinerama = true;
698 }
699
700 /* Acquire the WM_Sn selection. */
701 {
702 /* Get the WM_Sn atom */
703 char *atom_name = xcb_atom_name_by_screen("WM", conn_screen);
704 wm_sn_selection_owner = xcb_generate_id(conn);
705
706 if (atom_name == NULL) {
707 ELOG("xcb_atom_name_by_screen(\"WM\", %d) failed, exiting\n", conn_screen);
708 return 1;
709 }
710
711 xcb_intern_atom_reply_t *atom_reply;
712 atom_reply = xcb_intern_atom_reply(conn,
713 xcb_intern_atom_unchecked(conn,
714 0,
715 strlen(atom_name),
716 atom_name),
717 NULL);
718 free(atom_name);
719 if (atom_reply == NULL) {
720 ELOG("Failed to intern the WM_Sn atom, exiting\n");
721 return 1;
722 }
723 wm_sn = atom_reply->atom;
724 free(atom_reply);
725
726 /* Check if the selection is already owned */
727 xcb_get_selection_owner_reply_t *selection_reply =
728 xcb_get_selection_owner_reply(conn,
729 xcb_get_selection_owner(conn, wm_sn),
730 NULL);
731 if (selection_reply && selection_reply->owner != XCB_NONE && !replace_wm) {
732 ELOG("Another window manager is already running (WM_Sn is owned)");
733 return 1;
734 }
735
736 /* Become the selection owner */
737 xcb_create_window(conn,
738 root_screen->root_depth,
739 wm_sn_selection_owner, /* window id */
740 root_screen->root, /* parent */
741 -1, -1, 1, 1, /* geometry */
742 0, /* border width */
743 XCB_WINDOW_CLASS_INPUT_OUTPUT,
744 root_screen->root_visual,
745 0, NULL);
746 xcb_change_property(conn,
747 XCB_PROP_MODE_REPLACE,
749 XCB_ATOM_WM_CLASS,
750 XCB_ATOM_STRING,
751 8,
752 (strlen("i3-WM_Sn") + 1) * 2,
753 "i3-WM_Sn\0i3-WM_Sn\0");
754
755 xcb_set_selection_owner(conn, wm_sn_selection_owner, wm_sn, last_timestamp);
756
757 if (selection_reply && selection_reply->owner != XCB_NONE) {
758 unsigned int usleep_time = 100000; /* 0.1 seconds */
759 int check_rounds = 150; /* Wait for a maximum of 15 seconds */
760 xcb_get_geometry_reply_t *geom_reply = NULL;
761
762 DLOG("waiting for old WM_Sn selection owner to exit");
763 do {
764 free(geom_reply);
765 usleep(usleep_time);
766 if (check_rounds-- == 0) {
767 ELOG("The old window manager is not exiting");
768 return 1;
769 }
770 geom_reply = xcb_get_geometry_reply(conn,
771 xcb_get_geometry(conn, selection_reply->owner),
772 NULL);
773 } while (geom_reply != NULL);
774 }
775 free(selection_reply);
776
777 /* Announce that we are the new owner */
778 /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
779 * In order to properly initialize these bytes, we allocate 32 bytes even
780 * though we only need less for an xcb_client_message_event_t */
781 union {
782 xcb_client_message_event_t message;
783 char storage[32];
784 } event;
785 memset(&event, 0, sizeof(event));
786 event.message.response_type = XCB_CLIENT_MESSAGE;
787 event.message.window = root_screen->root;
788 event.message.format = 32;
789 event.message.type = A_MANAGER;
790 event.message.data.data32[0] = last_timestamp;
791 event.message.data.data32[1] = wm_sn;
792 event.message.data.data32[2] = wm_sn_selection_owner;
793
794 xcb_send_event(conn, 0, root_screen->root, XCB_EVENT_MASK_STRUCTURE_NOTIFY, event.storage);
795 }
796
797 xcb_void_cookie_t cookie;
798 cookie = xcb_change_window_attributes_checked(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
799 xcb_generic_error_t *error = xcb_request_check(conn, cookie);
800 if (error != NULL) {
801 ELOG("Another window manager seems to be running (X error %d)\n", error->error_code);
802#ifdef I3_ASAN_ENABLED
803 __lsan_do_leak_check();
804#endif
805 return 1;
806 }
807
808 xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(conn, gcookie, NULL);
809 if (greply == NULL) {
810 ELOG("Could not get geometry of the root window, exiting\n");
811 return 1;
812 }
813 DLOG("root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
814
816
817 /* Set a cursor for the root window (otherwise the root window will show no
818 cursor until the first client is launched). */
820
821 const xcb_query_extension_reply_t *extreply;
822 extreply = xcb_get_extension_data(conn, &xcb_xkb_id);
823 xkb_supported = extreply->present;
824 if (!extreply->present) {
825 DLOG("xkb is not present on this server\n");
826 } else {
827 DLOG("initializing xcb-xkb\n");
828 xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
829 xcb_xkb_select_events(conn,
830 XCB_XKB_ID_USE_CORE_KBD,
831 XCB_XKB_EVENT_TYPE_STATE_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY,
832 0,
833 XCB_XKB_EVENT_TYPE_STATE_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY,
834 0xff,
835 0xff,
836 NULL);
837
838 /* Setting both, XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE and
839 * XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED, will lead to the
840 * X server sending us the full XKB state in KeyPress and KeyRelease:
841 * https://cgit.freedesktop.org/xorg/xserver/tree/xkb/xkbEvents.c?h=xorg-server-1.20.0#n927
842 *
843 * XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT enable detectable autorepeat:
844 * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Detectable_Autorepeat
845 * This affects bindings using the --release flag: instead of getting multiple KeyRelease
846 * events we get only one event when the key is physically released by the user.
847 */
848 const uint32_t mask = XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE |
849 XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED |
850 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT;
851 xcb_xkb_per_client_flags_reply_t *pcf_reply;
852 /* The last three parameters are unset because they are only relevant
853 * when using a feature called “automatic reset of boolean controls”:
854 * https://www.x.org/releases/X11R7.7/doc/kbproto/xkbproto.html#Automatic_Reset_of_Boolean_Controls
855 * */
856 pcf_reply = xcb_xkb_per_client_flags_reply(
857 conn,
858 xcb_xkb_per_client_flags(
859 conn,
860 XCB_XKB_ID_USE_CORE_KBD,
861 mask,
862 mask,
863 0 /* uint32_t ctrlsToChange */,
864 0 /* uint32_t autoCtrls */,
865 0 /* uint32_t autoCtrlsValues */),
866 NULL);
867
868#define PCF_REPLY_ERROR(_value) \
869 do { \
870 if (pcf_reply == NULL || !(pcf_reply->value & (_value))) { \
871 ELOG("Could not set " #_value "\n"); \
872 } \
873 } while (0)
874
875 PCF_REPLY_ERROR(XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE);
876 PCF_REPLY_ERROR(XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED);
877 PCF_REPLY_ERROR(XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT);
878
879 free(pcf_reply);
880 xkb_base = extreply->first_event;
881 }
882
883 /* Check for Shape extension. We want to handle input shapes which is
884 * introduced in 1.1. */
885 extreply = xcb_get_extension_data(conn, &xcb_shape_id);
886 if (extreply->present) {
887 shape_base = extreply->first_event;
888 xcb_shape_query_version_cookie_t cookie = xcb_shape_query_version(conn);
889 xcb_shape_query_version_reply_t *version =
890 xcb_shape_query_version_reply(conn, cookie, NULL);
891 shape_supported = version && version->minor_version >= 1;
892 free(version);
893 } else {
894 shape_supported = false;
895 }
896 if (!shape_supported) {
897 DLOG("shape 1.1 is not present on this server\n");
898 }
899
901
903
905
906 keysyms = xcb_key_symbols_alloc(conn);
907
909
910 if (!load_keymap())
911 die("Could not load keymap\n");
912
915
916 bool needs_tree_init = true;
917 if (layout_path != NULL) {
918 LOG("Trying to restore the layout from \"%s\".\n", layout_path);
919 needs_tree_init = !tree_restore(layout_path, greply);
920 if (delete_layout_path) {
921 unlink(layout_path);
922 const char *dir = dirname(layout_path);
923 /* possibly fails with ENOTEMPTY if there are files (or
924 * sockets) left. */
925 rmdir(dir);
926 }
927 }
928 if (needs_tree_init)
929 tree_init(greply);
930
931 free(greply);
932
933 /* Setup fake outputs for testing */
934 if (fake_outputs == NULL && config.fake_outputs != NULL)
935 fake_outputs = config.fake_outputs;
936
937 if (fake_outputs != NULL) {
938 fake_outputs_init(fake_outputs);
939 FREE(fake_outputs);
940 config.fake_outputs = NULL;
941 } else if (force_xinerama) {
942 /* Force Xinerama (for drivers which don't support RandR yet, esp. the
943 * nVidia binary graphics driver), when specified either in the config
944 * file or on command-line */
946 } else {
947 DLOG("Checking for XRandR...\n");
948 randr_init(&randr_base, disable_randr15 || config.disable_randr15);
949 }
950
951 /* We need to force disabling outputs which have been loaded from the
952 * layout file but are no longer active. This can happen if the output has
953 * been disabled in the short time between writing the restart layout file
954 * and restarting i3. See #2326. */
955 if (layout_path != NULL && randr_base > -1) {
956 Con *con;
957 TAILQ_FOREACH (con, &(croot->nodes_head), nodes) {
958 Output *output;
959 TAILQ_FOREACH (output, &outputs, outputs) {
960 if (output->active || strcmp(con->name, output_primary_name(output)) != 0)
961 continue;
962
963 /* This will correctly correlate the output with its content
964 * container. We need to make the connection to properly
965 * disable the output. */
966 if (output->con == NULL) {
967 output_init_con(output);
968 output->changed = false;
969 }
970
971 output->to_be_disabled = true;
972 randr_disable_output(output);
973 }
974 }
975 }
976 FREE(layout_path);
977
979
980 xcb_query_pointer_reply_t *pointerreply;
981 Output *output = NULL;
982 if (!(pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL))) {
983 ELOG("Could not query pointer position, using first screen\n");
984 } else {
985 DLOG("Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
986 output = get_output_containing(pointerreply->root_x, pointerreply->root_y);
987 if (!output) {
988 ELOG("ERROR: No screen at (%d, %d), starting on the first screen\n",
989 pointerreply->root_x, pointerreply->root_y);
990 }
991 }
992 if (!output) {
993 output = get_first_output();
994 }
996 free(pointerreply);
997
998 tree_render();
999
1000 /* Listen to the IPC socket for clients */
1001 struct ev_io *ipc_io = scalloc(1, sizeof(struct ev_io));
1002 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
1003 ev_io_start(main_loop, ipc_io);
1004
1005 /* Chose a file name in /tmp/ based on the PID */
1006 char *log_stream_socket_path = get_process_filename("log-stream-socket");
1007 int log_socket = create_socket(log_stream_socket_path, &current_log_stream_socket_path);
1008 free(log_stream_socket_path);
1009 struct ev_io *log_io = NULL;
1010 if (log_socket == -1) {
1011 ELOG("Could not create the log socket, i3-dump-log -f will not work\n");
1012 } else {
1013 log_io = scalloc(1, sizeof(struct ev_io));
1014 ev_io_init(log_io, log_new_client, log_socket, EV_READ);
1015 ev_io_start(main_loop, log_io);
1016 }
1017
1018 /* Also handle the UNIX domain sockets passed via socket
1019 * activation. The parameter 0 means "do not remove the
1020 * environment variables", we need to be able to reexec. */
1021 struct ev_io *socket_ipc_io = NULL;
1023 if (listen_fds < 0) {
1024 ELOG("socket activation: Error in sd_listen_fds\n");
1025 } else if (listen_fds == 0) {
1026 DLOG("socket activation: no sockets passed\n");
1027 } else {
1028 int flags;
1029 for (int fd = SD_LISTEN_FDS_START;
1031 fd++) {
1032 DLOG("socket activation: also listening on fd %d\n", fd);
1033
1034 /* sd_listen_fds() enables FD_CLOEXEC by default.
1035 * However, we need to keep the file descriptors open for in-place
1036 * restarting, therefore we explicitly disable FD_CLOEXEC. */
1037 if ((flags = fcntl(fd, F_GETFD)) < 0 ||
1038 fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) < 0) {
1039 ELOG("Could not disable FD_CLOEXEC on fd %d\n", fd);
1040 }
1041
1042 socket_ipc_io = scalloc(1, sizeof(struct ev_io));
1043 ev_io_init(socket_ipc_io, ipc_new_client, fd, EV_READ);
1044 ev_io_start(main_loop, socket_ipc_io);
1045 }
1046 }
1047
1048 {
1049 const int restart_fd = parse_restart_fd();
1050 if (restart_fd != -1) {
1051 DLOG("serving restart fd %d", restart_fd);
1052 ipc_client *client = ipc_new_client_on_fd(main_loop, restart_fd);
1053 ipc_confirm_restart(client);
1054 unsetenv("_I3_RESTART_FD");
1055 }
1056 }
1057
1058 /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
1061
1062 /* Set the ewmh desktop properties. */
1064
1065 struct ev_io *xcb_watcher = scalloc(1, sizeof(struct ev_io));
1066 xcb_prepare = scalloc(1, sizeof(struct ev_prepare));
1067
1068 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
1069 ev_io_start(main_loop, xcb_watcher);
1070
1071 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
1072 ev_prepare_start(main_loop, xcb_prepare);
1073
1074 xcb_flush(conn);
1075
1076 /* What follows is a fugly consequence of X11 protocol race conditions like
1077 * the following: In an i3 in-place restart, i3 will reparent all windows
1078 * to the root window, then exec() itself. In the new process, it calls
1079 * manage_existing_windows. However, in case any application sent a
1080 * generated UnmapNotify message to the WM (as GIMP does), this message
1081 * will be handled by i3 *after* managing the window, thus i3 thinks the
1082 * window just closed itself. In reality, the message was sent in the time
1083 * period where i3 wasn’t running yet.
1084 *
1085 * To prevent this, we grab the server (disables processing of any other
1086 * connections), then discard all pending events (since we didn’t do
1087 * anything, there cannot be any meaningful responses), then ungrab the
1088 * server. */
1089 xcb_grab_server(conn);
1090 {
1091 xcb_aux_sync(conn);
1092 xcb_generic_event_t *event;
1093 while ((event = xcb_poll_for_event(conn)) != NULL) {
1094 if (event->response_type == 0) {
1095 free(event);
1096 continue;
1097 }
1098
1099 /* Strip off the highest bit (set if the event is generated) */
1100 int type = (event->response_type & 0x7F);
1101
1102 /* We still need to handle MapRequests which are sent in the
1103 * timespan starting from when we register as a window manager and
1104 * this piece of code which drops events. */
1105 if (type == XCB_MAP_REQUEST)
1106 handle_event(type, event);
1107
1108 free(event);
1109 }
1111 }
1112 xcb_ungrab_server(conn);
1113
1114 if (autostart) {
1115 /* When the root's window background is set to NONE, that might mean
1116 * that old content stays visible when a window is closed. That has
1117 * unpleasant effect of "my terminal (does not seem to) close!".
1118 *
1119 * There does not seem to be an easy way to query for this problem, so
1120 * we test for it: Open & close a window and check if the background is
1121 * redrawn or the window contents stay visible.
1122 */
1123 LOG("This is not an in-place restart, checking if a wallpaper is set.\n");
1124
1125 xcb_screen_t *root = xcb_aux_get_screen(conn, conn_screen);
1126 if (is_background_set(conn, root)) {
1127 LOG("A wallpaper is set, so no screenshot is necessary.\n");
1128 } else {
1129 LOG("No wallpaper set, copying root window contents to a pixmap\n");
1131 }
1132 }
1133
1134#if defined(__OpenBSD__)
1135 if (pledge("stdio rpath wpath cpath proc exec unix", NULL) == -1)
1136 err(EXIT_FAILURE, "pledge");
1137#endif
1138
1139 if (!disable_signalhandler)
1141 else {
1142 struct sigaction action;
1143
1144 action.sa_sigaction = handle_core_signal;
1145 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
1146 sigemptyset(&action.sa_mask);
1147
1148 /* Catch all signals with default action "Core", see signal(7) */
1149 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
1150 sigaction(SIGILL, &action, NULL) == -1 ||
1151 sigaction(SIGABRT, &action, NULL) == -1 ||
1152 sigaction(SIGFPE, &action, NULL) == -1 ||
1153 sigaction(SIGSEGV, &action, NULL) == -1)
1154 ELOG("Could not setup signal handler.\n");
1155 }
1156
1158 /* Ignore SIGPIPE to survive errors when an IPC client disconnects
1159 * while we are sending them a message */
1160 signal(SIGPIPE, SIG_IGN);
1161
1162 /* Autostarting exec-lines */
1163 if (autostart) {
1164 while (!TAILQ_EMPTY(&autostarts)) {
1165 struct Autostart *exec = TAILQ_FIRST(&autostarts);
1166
1167 LOG("auto-starting %s\n", exec->command);
1169
1170 FREE(exec->command);
1172 FREE(exec);
1173 }
1174 }
1175
1176 /* Autostarting exec_always-lines */
1177 while (!TAILQ_EMPTY(&autostarts_always)) {
1178 struct Autostart *exec_always = TAILQ_FIRST(&autostarts_always);
1179
1180 LOG("auto-starting (always!) %s\n", exec_always->command);
1181 start_application(exec_always->command, exec_always->no_startup_id);
1182
1183 FREE(exec_always->command);
1185 FREE(exec_always);
1186 }
1187
1188 /* Start i3bar processes for all configured bars */
1189 Barconfig *barconfig;
1190 TAILQ_FOREACH (barconfig, &barconfigs, configs) {
1191 char *command = NULL;
1192 sasprintf(&command, "%s %s --bar_id=%s --socket=\"%s\"",
1193 barconfig->i3bar_command ? barconfig->i3bar_command : "exec i3bar",
1194 barconfig->verbose ? "-V" : "",
1195 barconfig->id, current_socketpath);
1196 LOG("Starting bar process: %s\n", command);
1198 free(command);
1199 }
1200
1201 /* Make sure to destroy the event loop to invoke the cleanup callbacks
1202 * when calling exit() */
1203 atexit(i3_exit);
1204
1205 sd_notify(1, "READY=1");
1206 ev_loop(main_loop, 0);
1207
1208 /* Free these heap allocations just to satisfy LeakSanitizer. */
1209 FREE(ipc_io);
1210 FREE(socket_ipc_io);
1211 FREE(log_io);
1212 FREE(xcb_watcher);
1213}
bool load_keymap(void)
Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
Definition bindings.c:956
void grab_all_keys(xcb_connection_t *conn)
Grab the bound keys (tell X to send us keypress events for those keycodes)
Definition bindings.c:149
pid_t command_error_nagbar_pid
Definition bindings.c:19
void translate_keysyms(void)
Translates keysymbols to keycodes for all bindings which use keysyms.
Definition bindings.c:434
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition con.c:287
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1590
Config config
Definition config.c:19
bool load_configuration(const char *override_configpath, config_load_t load_type)
(Re-)loads the configuration file (sets useful defaults before).
Definition config.c:167
struct barconfig_head barconfigs
Definition config.c:21
pid_t config_error_nagbar_pid
void display_running_version(void)
Connects to i3 to find out the currently running version.
void ewmh_setup_hints(void)
Set up the EWMH hints on the root window.
Definition ewmh.c:307
void ewmh_update_desktop_properties(void)
Updates all the EWMH desktop properties.
Definition ewmh.c:118
void ewmh_update_workarea(void)
i3 currently does not support _NET_WORKAREA, because it does not correspond to i3’s concept of worksp...
Definition ewmh.c:239
void fake_outputs_init(const char *output_spec)
Creates outputs according to the given specification.
int randr_base
Definition handlers.c:20
void handle_event(int type, xcb_generic_event_t *event)
Takes an xcb_generic_event_t and calls the appropriate handler, based on the event type.
Definition handlers.c:1389
bool event_is_ignored(const int sequence, const int response_type)
Checks if the given sequence is ignored and returns true if so.
Definition handlers.c:52
int xkb_base
Definition handlers.c:21
void property_handlers_init(void)
Sets the appropriate atoms for the property handlers after the atoms were received from X11.
Definition handlers.c:1326
int shape_base
Definition handlers.c:23
void manage_existing_windows(xcb_window_t root)
Go through all existing windows (if the window manager is restarted) and manage them.
Definition manage.c:44
char * output_primary_name(Output *output)
Retrieves the primary name of an output.
Definition output.c:53
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition output.c:16
Output * get_output_containing(unsigned int x, unsigned int y)
Returns the active (!) output which contains the coordinates x, y or NULL if there is no output which...
Definition randr.c:121
void output_init_con(Output *output)
Initializes a CT_OUTPUT Con (searches existing ones from inplace restart before) to use for the given...
Definition randr.c:334
struct outputs_head outputs
Definition randr.c:22
void randr_init(int *event_base, const bool disable_randr15)
We have just established a connection to the X server and need the initial XRandR information to setu...
Definition randr.c:1072
Output * get_first_output(void)
Returns the first output which is active.
Definition randr.c:80
void randr_disable_output(Output *output)
Disables the output and moves its content.
Definition randr.c:1044
void restore_connect(void)
Opens a separate connection to X11 for placeholder windows when restoring layouts.
void scratchpad_fix_resolution(void)
When starting i3 initially (and after each change to the connected outputs), this function fixes the ...
Definition scratchpad.c:247
int sd_notify(int unset_environment, const char *state)
Definition sd-daemon.c:319
int sd_listen_fds(int unset_environment)
Definition sd-daemon.c:47
void setup_signal_handler(void)
Configured a signal handler to gracefully handle crashes and allow the user to generate a backtrace a...
Definition sighandler.c:334
void start_application(const char *command, bool no_startup_id)
Starts the given application by passing it through a shell.
Definition startup.c:132
bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry)
Loads tree from ~/.i3/_restart.json (used for in-place restarts).
Definition tree.c:66
struct Con * croot
Definition tree.c:12
void tree_init(xcb_get_geometry_reply_t *geometry)
Initializes the tree by creating the root node, adding all RandR outputs to the tree (that means rand...
Definition tree.c:130
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition tree.c:451
__attribute__((pure))
Definition util.c:67
void kill_nagbar(pid_t nagbar_pid, bool wait_for_it)
Kills the i3-nagbar process, if nagbar_pid != -1.
Definition util.c:387
bool parse_long(const char *str, long *out, int base)
Converts a string into a long using strtol().
Definition util.c:409
const char * i3_version
Git commit identifier, from version.c.
Definition version.c:13
void x_set_i3_atoms(void)
Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
Definition x.c:1459
unsigned int xcb_numlock_mask
Definition xcb.c:12
void xcursor_load_cursors(void)
Definition xcursor.c:22
void xcursor_set_root_cursor(int cursor_id)
Sets the cursor of the root window to the 'pointer' cursor.
Definition xcursor.c:49
void xinerama_init(void)
We have just established a connection to the X server and need the initial Xinerama information to se...
Definition xinerama.c:111
void ipc_confirm_restart(ipc_client *client)
Sends a restart reply to the IPC client on the specified fd.
Definition ipc.c:1719
ipc_client * ipc_new_client_on_fd(EV_P_ int fd)
ipc_new_client_on_fd() only sets up the event handler for activity on the new connection and inserts ...
Definition ipc.c:1561
char * current_socketpath
Definition ipc.c:26
void ipc_shutdown(shutdown_reason_t reason, int exempt_fd)
Calls shutdown() on each socket and closes it.
Definition ipc.c:192
void ipc_new_client(EV_P_ struct ev_io *w, int revents)
Handler for activity on the listening socket, meaning that a new client has just connected and we sho...
Definition ipc.c:1536
int shmlog_size
Definition log.c:47
void log_new_client(EV_P_ struct ev_io *w, int revents)
Definition log.c:389
void init_logging(void)
Initializes logging by creating an error logfile in /tmp (or XDG_RUNTIME_DIR, see get_process_filenam...
Definition log.c:95
void set_debug_logging(const bool _debug_logging)
Set debug logging.
Definition log.c:220
char * current_log_stream_socket_path
Definition log.c:380
char * shmlogname
Definition log.c:44
void set_verbosity(bool _verbose)
Set verbosity of i3.
Definition log.c:204
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:64
xcb_atom_t wm_sn
Definition main.c:70
int main(int argc, char *argv[])
Definition main.c:279
const int default_shmlog_size
Definition main.c:84
#define PCF_REPLY_ERROR(_value)
bool xkb_supported
Definition main.c:104
static void handle_term_signal(struct ev_loop *loop, ev_signal *signal, int revents)
Definition main.c:230
int conn_screen
Definition main.c:56
xcb_connection_t * conn
XCB connection and root screen.
Definition main.c:54
xcb_colormap_t colormap
Definition main.c:77
int listen_fds
The number of file descriptors passed via socket activation.
Definition main.c:46
bool force_xinerama
Definition main.c:107
xcb_key_symbols_t * keysyms
Definition main.c:81
uint8_t root_depth
Definition main.c:75
xcb_window_t wm_sn_selection_owner
Definition main.c:69
struct autostarts_always_head autostarts_always
Definition main.c:94
I3_NET_SUPPORTED_ATOMS_XMACRO static I3_REST_ATOMS_XMACRO void xcb_got_event(EV_P_ struct ev_io *w, int revents)
Definition main.c:120
SnDisplay * sndisplay
Definition main.c:59
static struct ev_prepare * xcb_prepare
Definition main.c:50
xcb_window_t root
Definition main.c:67
static void i3_exit(void)
Definition main.c:182
struct rlimit original_rlimit_core
The original value of RLIMIT_CORE when i3 was started.
Definition main.c:43
xcb_screen_t * root_screen
Definition main.c:66
const char * current_binding_mode
Definition main.c:88
static void setup_term_handlers(void)
Definition main.c:240
xcb_visualtype_t * visual_type
Definition main.c:76
static void handle_core_signal(int sig, siginfo_t *info, void *data)
Definition main.c:217
void main_set_x11_cb(bool enable)
Enable or disable the main X11 event handling function.
Definition main.c:166
struct autostarts_head autostarts
Definition main.c:91
char ** start_argv
Definition main.c:52
bool shape_supported
Definition main.c:105
static int parse_restart_fd(void)
Definition main.c:265
struct ev_loop * main_loop
Definition main.c:79
static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents)
Definition main.c:130
struct assignments_head assignments
Definition main.c:97
struct ws_assignments_head ws_assignments
Definition main.c:101
struct bindings_head * bindings
Definition main.c:87
@ C_VALIDATE
@ C_LOAD
#define I3_NET_SUPPORTED_ATOMS_XMACRO
#define I3_REST_ATOMS_XMACRO
bool only_check_config
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
#define DLOG(fmt,...)
Definition libi3.h:105
#define LOG(fmt,...)
Definition libi3.h:95
void set_screenshot_as_wallpaper(xcb_connection_t *conn, xcb_screen_t *screen)
Grab a screenshot of the screen's root window and set it as the wallpaper.
int create_socket(const char *filename, char **out_socketpath)
Creates the UNIX domain socket at the given path, sets it to non-blocking mode, bind()s and listen()s...
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define ELOG(fmt,...)
Definition libi3.h:100
int ipc_recv_message(int sockfd, uint32_t *message_type, uint32_t *reply_length, uint8_t **reply)
Reads a message from the given socket file descriptor and stores its length (reply_length) as well as...
uint32_t aio_get_mod_mask_for(uint32_t keysym, xcb_key_symbols_t *symbols)
All-in-one function which returns the modifier mask (XCB_MOD_MASK_*) for the given keysymbol,...
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...
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...
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
char * root_atom_contents(const char *atomname, xcb_connection_t *provided_conn, int screen)
Try to get the contents of the given atom (for example I3_SOCKET_PATH) from the X11 root window and r...
char * get_process_filename(const char *prefix)
Returns the name of a temporary file with the specified prefix.
int ipc_send_message(int sockfd, const uint32_t message_size, const uint32_t message_type, const uint8_t *payload)
Formats a message (payload) of the given size and type and sends it to i3 via the given socket file d...
bool is_background_set(xcb_connection_t *conn, xcb_screen_t *screen)
Test whether the screen's root window has a background set.
bool is_debug_build(void) __attribute__((const))
Returns true if this version of i3 is a debug build (anything which is not a release version),...
void init_dpi(void)
Initialize the DPI setting.
xcb_visualtype_t * get_visualtype(xcb_screen_t *screen)
Returns the visual type associated with the given screen.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:402
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:324
#define TAILQ_EMPTY(head)
Definition queue.h:344
#define SD_LISTEN_FDS_START
Definition sd-daemon.h:102
#define die(...)
Definition util.h:19
#define FREE(pointer)
Definition util.h:47
#define XCB_NUM_LOCK
Definition xcb.h:22
#define ROOT_EVENT_MASK
Definition xcb.h:42
@ XCURSOR_CURSOR_POINTER
Definition xcursor.h:17
@ SHUTDOWN_REASON_EXIT
Definition ipc.h:95
char * fake_outputs
Overwrites output detection (for testing), see src/fake_outputs.c.
char * ipc_socket_path
bool disable_randr15
Don’t use RandR 1.5 for querying outputs.
bool force_xinerama
By default, use the RandR API for multi-monitor setups.
Holds the status bar configuration (i3bar).
char * i3bar_command
Command that should be run to execute i3bar, give a full path if i3bar is not in your $PATH.
char * id
Automatically generated ID for this bar config.
bool verbose
Enable verbose mode? Useful for debugging purposes.
Holds a command specified by either an:
Definition data.h:373
bool no_startup_id
no_startup_id flag for start_application().
Definition data.h:378
char * command
Command, like in command mode.
Definition data.h:375
An Output is a physical output on your graphics driver.
Definition data.h:395
Con * con
Pointer to the Con which represents this output.
Definition data.h:415
bool changed
Internal flags, necessary for querying RandR screens (happens in two stages)
Definition data.h:405
bool to_be_disabled
Definition data.h:406
bool active
Whether the output is currently active (has a CRTC attached with a valid mode)
Definition data.h:401
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:647
char * name
Definition data.h:696