2 #define I3__FILE__ "main.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * main.c: Initialization, main loop
14 #include <sys/types.h>
15 #include <sys/socket.h>
18 #include <sys/resource.h>
23 #include "sd-daemon.h"
25 /* The original value of RLIMIT_CORE when i3 was started. We need to restore
26 * this before starting any other process, since we set RLIMIT_CORE to
27 * RLIM_INFINITY for i3 debugging versions. */
28 struct rlimit original_rlimit_core;
30 /** The number of file descriptors passed via socket activation. */
33 static int xkb_event_base;
35 int xkb_current_group;
41 xcb_connection_t *conn;
42 /* The screen (0 when you are using DISPLAY=:0) of the connection 'conn' */
45 /* Display handle for libstartup-notification */
48 /* The last timestamp we got from X11 (timestamps are included in some events
49 * and are used for some things, like determining a unique ID in startup
51 xcb_timestamp_t last_timestamp = XCB_CURRENT_TIME;
53 xcb_screen_t *root_screen;
56 /* Color depth, visual id and colormap to use when creating windows and
57 * pixmaps. Will use 32 bit depth and an appropriate visual, if available,
58 * otherwise the root window’s default (usually 24 bit TrueColor). */
60 xcb_visualid_t visual_id;
61 xcb_colormap_t colormap;
63 struct ev_loop *main_loop;
65 xcb_key_symbols_t *keysyms;
67 /* Those are our connections to X11 for use with libXcursor and XKB */
68 Display *xlibdpy, *xkbdpy;
70 /* The list of key bindings */
71 struct bindings_head *bindings;
73 /* The list of exec-lines */
74 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
76 /* The list of exec_always lines */
77 struct autostarts_always_head autostarts_always = TAILQ_HEAD_INITIALIZER(autostarts_always);
79 /* The list of assignments */
80 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
82 /* The list of workspace assignments (which workspace should end up on which
84 struct ws_assignments_head ws_assignments = TAILQ_HEAD_INITIALIZER(ws_assignments);
86 /* We hope that those are supported and set them to true */
87 bool xcursor_supported = true;
88 bool xkb_supported = true;
90 /* This will be set to true when -C is used so that functions can behave
91 * slightly differently. We don’t want i3-nagbar to be started when validating
92 * the config, for example. */
93 bool only_check_config = false;
96 * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
97 * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
100 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
101 /* empty, because xcb_prepare_cb and xcb_check_cb are used */
105 * Flush before blocking (and waiting for new events)
108 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
113 * Instead of polling the X connection socket we leave this to
114 * xcb_poll_for_event() which knows better than we can ever know.
117 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
118 xcb_generic_event_t *event;
120 while ((event = xcb_poll_for_event(conn)) != NULL) {
121 if (event->response_type == 0) {
122 if (event_is_ignored(event->sequence, 0))
123 DLOG("Expected X11 Error received for sequence %x\n", event->sequence);
125 xcb_generic_error_t *error = (xcb_generic_error_t*)event;
126 DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
127 error->sequence, error->error_code);
133 /* Strip off the highest bit (set if the event is generated) */
134 int type = (event->response_type & 0x7F);
136 handle_event(type, event);
144 * When using xmodmap to change the keyboard mapping, this event
145 * is only sent via XKB. Therefore, we need this special handler.
148 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
149 DLOG("Handling XKB event\n");
152 /* When using xmodmap, every change (!) gets an own event.
153 * Therefore, we just read all events and only handle the
154 * mapping_notify once. */
155 bool mapping_changed = false;
156 while (XPending(xkbdpy)) {
157 XNextEvent(xkbdpy, (XEvent*)&ev);
158 /* While we should never receive a non-XKB event,
159 * better do sanity checking */
160 if (ev.type != xkb_event_base)
163 if (ev.any.xkb_type == XkbMapNotify) {
164 mapping_changed = true;
168 if (ev.any.xkb_type != XkbStateNotify) {
169 ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
173 /* See The XKB Extension: Library Specification, section 14.1 */
174 /* We check if the current group (each group contains
175 * two levels) has been changed. Mode_switch activates
176 * group XkbGroup2Index */
177 if (xkb_current_group == ev.state.group)
180 xkb_current_group = ev.state.group;
182 if (ev.state.group == XkbGroup2Index) {
183 DLOG("Mode_switch enabled\n");
184 grab_all_keys(conn, true);
187 if (ev.state.group == XkbGroup1Index) {
188 DLOG("Mode_switch disabled\n");
189 ungrab_all_keys(conn);
190 grab_all_keys(conn, false);
194 if (!mapping_changed)
197 DLOG("Keyboard mapping changed, updating keybindings\n");
198 xcb_key_symbols_free(keysyms);
199 keysyms = xcb_key_symbols_alloc(conn);
201 xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
203 ungrab_all_keys(conn);
204 DLOG("Re-grabbing...\n");
206 grab_all_keys(conn, (xkb_current_group == XkbGroup2Index));
211 * Exit handler which destroys the main_loop. Will trigger cleanup handlers.
214 static void i3_exit(void) {
215 /* We need ev >= 4 for the following code. Since it is not *that* important (it
216 * only makes sure that there are no i3-nagbar instances left behind) we still
217 * support old systems with libev 3. */
218 #if EV_VERSION_MAJOR >= 4
219 ev_loop_destroy(main_loop);
222 if (*shmlogname != '\0') {
223 fprintf(stderr, "Closing SHM log \"%s\"\n", shmlogname);
225 shm_unlink(shmlogname);
230 * (One-shot) Handler for all signals with default action "Term", see signal(7)
232 * Unlinks the SHM log and re-raises the signal.
235 static void handle_signal(int sig, siginfo_t *info, void *data) {
236 fprintf(stderr, "Received signal %d, terminating\n", sig);
237 if (*shmlogname != '\0') {
238 fprintf(stderr, "Closing SHM log \"%s\"\n", shmlogname);
239 shm_unlink(shmlogname);
245 int main(int argc, char *argv[]) {
246 /* Keep a symbol pointing to the I3_VERSION string constant so that we have
247 * it in gdb backtraces. */
248 const char *i3_version __attribute__ ((unused)) = I3_VERSION;
249 char *override_configpath = NULL;
250 bool autostart = true;
251 char *layout_path = NULL;
252 bool delete_layout_path = false;
253 bool force_xinerama = false;
254 char *fake_outputs = NULL;
255 bool disable_signalhandler = false;
256 static struct option long_options[] = {
257 {"no-autostart", no_argument, 0, 'a'},
258 {"config", required_argument, 0, 'c'},
259 {"version", no_argument, 0, 'v'},
260 {"help", no_argument, 0, 'h'},
261 {"layout", required_argument, 0, 'L'},
262 {"restart", required_argument, 0, 0},
263 {"force-xinerama", no_argument, 0, 0},
264 {"force_xinerama", no_argument, 0, 0},
265 {"disable-signalhandler", no_argument, 0, 0},
266 {"shmlog-size", required_argument, 0, 0},
267 {"shmlog_size", required_argument, 0, 0},
268 {"get-socketpath", no_argument, 0, 0},
269 {"get_socketpath", no_argument, 0, 0},
270 {"fake_outputs", required_argument, 0, 0},
271 {"fake-outputs", required_argument, 0, 0},
274 int option_index = 0, opt;
276 setlocale(LC_ALL, "");
278 /* Get the RLIMIT_CORE limit at startup time to restore this before
279 * starting processes. */
280 getrlimit(RLIMIT_CORE, &original_rlimit_core);
282 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
283 if (!isatty(fileno(stdout)))
284 setbuf(stdout, NULL);
288 /* Init logging *before* initializing debug_build to guarantee early
292 /* On non-release builds, disable SHM logging by default. */
293 shmlog_size = (is_debug_build() ? 25 * 1024 * 1024 : 0);
297 while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
300 LOG("Autostart disabled using -a\n");
305 layout_path = sstrdup(optarg);
306 delete_layout_path = false;
309 FREE(override_configpath);
310 override_configpath = sstrdup(optarg);
313 LOG("Checking configuration file only (-C)\n");
314 only_check_config = true;
317 printf("i3 version " I3_VERSION " © 2009-2012 Michael Stapelberg and contributors\n");
323 LOG("Enabling debug logging\n");
324 set_debug_logging(true);
327 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
330 if (strcmp(long_options[option_index].name, "force-xinerama") == 0 ||
331 strcmp(long_options[option_index].name, "force_xinerama") == 0) {
332 force_xinerama = true;
333 ELOG("Using Xinerama instead of RandR. This option should be "
334 "avoided at all cost because it does not refresh the list "
335 "of screens, so you cannot configure displays at runtime. "
336 "Please check if your driver really does not support RandR "
337 "and disable this option as soon as you can.\n");
339 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
340 disable_signalhandler = true;
342 } else if (strcmp(long_options[option_index].name, "get-socketpath") == 0 ||
343 strcmp(long_options[option_index].name, "get_socketpath") == 0) {
344 char *socket_path = root_atom_contents("I3_SOCKET_PATH");
346 printf("%s\n", socket_path);
351 } else if (strcmp(long_options[option_index].name, "shmlog-size") == 0 ||
352 strcmp(long_options[option_index].name, "shmlog_size") == 0) {
353 shmlog_size = atoi(optarg);
354 /* Re-initialize logging immediately to get as many
355 * logmessages as possible into the SHM log. */
357 LOG("Limiting SHM log size to %d bytes\n", shmlog_size);
359 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
361 layout_path = sstrdup(optarg);
362 delete_layout_path = true;
364 } else if (strcmp(long_options[option_index].name, "fake-outputs") == 0 ||
365 strcmp(long_options[option_index].name, "fake_outputs") == 0) {
366 LOG("Initializing fake outputs: %s\n", optarg);
367 fake_outputs = sstrdup(optarg);
372 fprintf(stderr, "Usage: %s [-c configfile] [-d all] [-a] [-v] [-V] [-C]\n", argv[0]);
373 fprintf(stderr, "\n");
374 fprintf(stderr, "\t-a disable autostart ('exec' lines in config)\n");
375 fprintf(stderr, "\t-c <file> use the provided configfile instead\n");
376 fprintf(stderr, "\t-C validate configuration file and exit\n");
377 fprintf(stderr, "\t-d all enable debug output\n");
378 fprintf(stderr, "\t-L <file> path to the serialized layout during restarts\n");
379 fprintf(stderr, "\t-v display version and exit\n");
380 fprintf(stderr, "\t-V enable verbose mode\n");
381 fprintf(stderr, "\n");
382 fprintf(stderr, "\t--force-xinerama\n"
383 "\tUse Xinerama instead of RandR.\n"
384 "\tThis option should only be used if you are stuck with the\n"
385 "\tnvidia closed source driver which does not support RandR.\n");
386 fprintf(stderr, "\n");
387 fprintf(stderr, "\t--get-socketpath\n"
388 "\tRetrieve the i3 IPC socket path from X11, print it, then exit.\n");
389 fprintf(stderr, "\n");
390 fprintf(stderr, "\t--shmlog-size <limit>\n"
391 "\tLimits the size of the i3 SHM log to <limit> bytes. Setting this\n"
392 "\tto 0 disables SHM logging entirely.\n"
393 "\tThe default is %d bytes.\n", shmlog_size);
394 fprintf(stderr, "\n");
395 fprintf(stderr, "If you pass plain text arguments, i3 will interpret them as a command\n"
396 "to send to a currently running i3 (like i3-msg). This allows you to\n"
397 "use nice and logical commands, such as:\n"
400 "\ti3 floating toggle\n"
407 /* If the user passes more arguments, we act like i3-msg would: Just send
408 * the arguments as an IPC message to i3. This allows for nice semantic
409 * commands such as 'i3 border none'. */
410 if (!only_check_config && optind < argc) {
411 /* We enable verbose mode so that the user knows what’s going on.
412 * This should make it easier to find mistakes when the user passes
413 * arguments by mistake. */
416 LOG("Additional arguments passed. Sending them as a command to i3.\n");
417 char *payload = NULL;
418 while (optind < argc) {
420 payload = sstrdup(argv[optind]);
423 sasprintf(&both, "%s %s", payload, argv[optind]);
429 DLOG("Command is: %s (%zd bytes)\n", payload, strlen(payload));
430 char *socket_path = root_atom_contents("I3_SOCKET_PATH");
432 ELOG("Could not get i3 IPC socket path\n");
436 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
438 err(EXIT_FAILURE, "Could not create socket");
440 struct sockaddr_un addr;
441 memset(&addr, 0, sizeof(struct sockaddr_un));
442 addr.sun_family = AF_LOCAL;
443 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
444 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
445 err(EXIT_FAILURE, "Could not connect to i3");
447 if (ipc_send_message(sockfd, strlen(payload), I3_IPC_MESSAGE_TYPE_COMMAND,
448 (uint8_t*)payload) == -1)
449 err(EXIT_FAILURE, "IPC: write()");
451 uint32_t reply_length;
454 if ((ret = ipc_recv_message(sockfd, I3_IPC_MESSAGE_TYPE_COMMAND,
455 &reply_length, &reply)) != 0) {
457 err(EXIT_FAILURE, "IPC: read()");
460 printf("%.*s\n", reply_length, reply);
464 /* Enable logging to handle the case when the user did not specify --shmlog-size */
467 /* Try to enable core dumps by default when running a debug build */
468 if (is_debug_build()) {
469 struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
470 setrlimit(RLIMIT_CORE, &limit);
472 /* The following code is helpful, but not required. We thus don’t pay
473 * much attention to error handling, non-linux or other edge cases. */
475 LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
476 if (getcwd(cwd, sizeof(cwd)) != NULL)
477 LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
479 if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
480 memset(cwd, '\0', sizeof(cwd));
481 if (read(patternfd, cwd, sizeof(cwd)) > 0)
482 /* a trailing newline is included in cwd */
483 LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
488 LOG("i3 (tree) version " I3_VERSION " starting\n");
490 conn = xcb_connect(NULL, &conn_screen);
491 if (xcb_connection_has_error(conn))
492 errx(EXIT_FAILURE, "Cannot open display\n");
494 sndisplay = sn_xcb_display_new(conn, NULL, NULL);
496 /* Initialize the libev event loop. This needs to be done before loading
497 * the config file because the parser will install an ev_child watcher
498 * for the nagbar when config errors are found. */
499 main_loop = EV_DEFAULT;
500 if (main_loop == NULL)
501 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
503 root_screen = xcb_aux_get_screen(conn, conn_screen);
504 root = root_screen->root;
506 /* By default, we use the same depth and visual as the root window, which
507 * usually is TrueColor (24 bit depth) and the corresponding visual.
508 * However, we also check if a 32 bit depth and visual are available (for
509 * transparency) and use it if so. */
510 root_depth = root_screen->root_depth;
511 visual_id = root_screen->root_visual;
512 colormap = root_screen->default_colormap;
514 DLOG("root_depth = %d, visual_id = 0x%08x.\n", root_depth, visual_id);
516 xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
517 xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
519 load_configuration(conn, override_configpath, false);
520 if (only_check_config) {
521 LOG("Done checking configuration file. Exiting.\n");
525 if (config.ipc_socket_path == NULL) {
526 /* Fall back to a file name in /tmp/ based on the PID */
527 if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
528 config.ipc_socket_path = get_process_filename("ipc-socket");
530 config.ipc_socket_path = sstrdup(config.ipc_socket_path);
533 uint32_t mask = XCB_CW_EVENT_MASK;
534 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
535 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
536 projector), the root window gets a
538 XCB_EVENT_MASK_POINTER_MOTION |
539 XCB_EVENT_MASK_PROPERTY_CHANGE |
540 XCB_EVENT_MASK_ENTER_WINDOW };
541 xcb_void_cookie_t cookie;
542 cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
543 check_error(conn, cookie, "Another window manager seems to be running");
545 xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(conn, gcookie, NULL);
546 if (greply == NULL) {
547 ELOG("Could not get geometry of the root window, exiting\n");
550 DLOG("root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
552 /* Place requests for the atoms we need as soon as possible */
553 #define xmacro(atom) \
554 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
555 #include "atoms.xmacro"
558 /* Initialize the Xlib connection */
559 xlibdpy = xkbdpy = XOpenDisplay(NULL);
561 /* Try to load the X cursors and initialize the XKB extension */
562 if (xlibdpy == NULL) {
563 ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n");
564 xcursor_supported = false;
565 xkb_supported = false;
566 } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) {
567 ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
570 xcursor_load_cursors();
574 /* Set a cursor for the root window (otherwise the root window will show no
575 cursor until the first client is launched). */
576 if (xcursor_supported)
577 xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
578 else xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
582 major = XkbMajorVersion,
583 minor = XkbMinorVersion;
585 if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
586 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
591 if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
592 fprintf(stderr, "XKB not supported by X-server\n");
595 /* end of ugliness */
597 if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
598 XkbMapNotifyMask | XkbStateNotifyMask,
599 XkbMapNotifyMask | XkbStateNotifyMask)) {
600 fprintf(stderr, "Could not set XKB event mask\n");
605 /* Setup NetWM atoms */
606 #define xmacro(name) \
608 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
610 ELOG("Could not get atom " #name "\n"); \
613 A_ ## name = reply->atom; \
616 #include "atoms.xmacro"
619 property_handlers_init();
623 keysyms = xcb_key_symbols_alloc(conn);
625 xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
628 grab_all_keys(conn, false);
630 bool needs_tree_init = true;
632 LOG("Trying to restore the layout from %s...", layout_path);
633 needs_tree_init = !tree_restore(layout_path, greply);
634 if (delete_layout_path)
643 /* Setup fake outputs for testing */
644 if (fake_outputs == NULL && config.fake_outputs != NULL)
645 fake_outputs = config.fake_outputs;
647 if (fake_outputs != NULL) {
648 fake_outputs_init(fake_outputs);
650 config.fake_outputs = NULL;
651 } else if (force_xinerama || config.force_xinerama) {
652 /* Force Xinerama (for drivers which don't support RandR yet, esp. the
653 * nVidia binary graphics driver), when specified either in the config
654 * file or on command-line */
657 DLOG("Checking for XRandR...\n");
658 randr_init(&randr_base);
661 scratchpad_fix_resolution();
663 xcb_query_pointer_reply_t *pointerreply;
664 Output *output = NULL;
665 if (!(pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL))) {
666 ELOG("Could not query pointer position, using first screen\n");
668 DLOG("Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
669 output = get_output_containing(pointerreply->root_x, pointerreply->root_y);
671 ELOG("ERROR: No screen at (%d, %d), starting on the first screen\n",
672 pointerreply->root_x, pointerreply->root_y);
673 output = get_first_output();
676 con_focus(con_descend_focused(output_get_content(output->con)));
681 /* Create the UNIX domain socket for IPC */
682 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
683 if (ipc_socket == -1) {
684 ELOG("Could not create the IPC socket, IPC disabled\n");
686 free(config.ipc_socket_path);
687 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
688 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
689 ev_io_start(main_loop, ipc_io);
692 /* Also handle the UNIX domain sockets passed via socket activation. The
693 * parameter 1 means "remove the environment variables", we don’t want to
694 * pass these to child processes. */
695 listen_fds = sd_listen_fds(0);
697 ELOG("socket activation: Error in sd_listen_fds\n");
698 else if (listen_fds == 0)
699 DLOG("socket activation: no sockets passed\n");
702 for (int fd = SD_LISTEN_FDS_START;
703 fd < (SD_LISTEN_FDS_START + listen_fds);
705 DLOG("socket activation: also listening on fd %d\n", fd);
707 /* sd_listen_fds() enables FD_CLOEXEC by default.
708 * However, we need to keep the file descriptors open for in-place
709 * restarting, therefore we explicitly disable FD_CLOEXEC. */
710 if ((flags = fcntl(fd, F_GETFD)) < 0 ||
711 fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) < 0) {
712 ELOG("Could not disable FD_CLOEXEC on fd %d\n", fd);
715 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
716 ev_io_init(ipc_io, ipc_new_client, fd, EV_READ);
717 ev_io_start(main_loop, ipc_io);
721 /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
724 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
725 struct ev_io *xkb = scalloc(sizeof(struct ev_io));
726 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
727 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
729 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
730 ev_io_start(main_loop, xcb_watcher);
734 ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
735 ev_io_start(main_loop, xkb);
737 /* Flush the buffer so that libev can properly get new events */
741 ev_check_init(xcb_check, xcb_check_cb);
742 ev_check_start(main_loop, xcb_check);
744 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
745 ev_prepare_start(main_loop, xcb_prepare);
749 manage_existing_windows(root);
751 struct sigaction action;
753 action.sa_sigaction = handle_signal;
754 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
755 sigemptyset(&action.sa_mask);
757 if (!disable_signalhandler)
758 setup_signal_handler();
760 /* Catch all signals with default action "Core", see signal(7) */
761 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
762 sigaction(SIGILL, &action, NULL) == -1 ||
763 sigaction(SIGABRT, &action, NULL) == -1 ||
764 sigaction(SIGFPE, &action, NULL) == -1 ||
765 sigaction(SIGSEGV, &action, NULL) == -1)
766 ELOG("Could not setup signal handler");
769 /* Catch all signals with default action "Term", see signal(7) */
770 if (sigaction(SIGHUP, &action, NULL) == -1 ||
771 sigaction(SIGINT, &action, NULL) == -1 ||
772 sigaction(SIGALRM, &action, NULL) == -1 ||
773 sigaction(SIGUSR1, &action, NULL) == -1 ||
774 sigaction(SIGUSR2, &action, NULL) == -1)
775 ELOG("Could not setup signal handler");
777 /* Ignore SIGPIPE to survive errors when an IPC client disconnects
778 * while we are sending him a message */
779 signal(SIGPIPE, SIG_IGN);
781 /* Autostarting exec-lines */
783 struct Autostart *exec;
784 TAILQ_FOREACH(exec, &autostarts, autostarts) {
785 LOG("auto-starting %s\n", exec->command);
786 start_application(exec->command, exec->no_startup_id);
790 /* Autostarting exec_always-lines */
791 struct Autostart *exec_always;
792 TAILQ_FOREACH(exec_always, &autostarts_always, autostarts_always) {
793 LOG("auto-starting (always!) %s\n", exec_always->command);
794 start_application(exec_always->command, exec_always->no_startup_id);
797 /* Start i3bar processes for all configured bars */
798 Barconfig *barconfig;
799 TAILQ_FOREACH(barconfig, &barconfigs, configs) {
800 char *command = NULL;
801 sasprintf(&command, "%s --bar_id=%s --socket=\"%s\"",
802 barconfig->i3bar_command ? barconfig->i3bar_command : "i3bar",
803 barconfig->id, current_socketpath);
804 LOG("Starting bar process: %s\n", command);
805 start_application(command, true);
809 /* Make sure to destroy the event loop to invoke the cleeanup callbacks
810 * when calling exit() */
813 ev_loop(main_loop, 0);