2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7 * main.c: Initialization, main loop
12 #include <sys/types.h>
13 #include <sys/socket.h>
16 #include <sys/resource.h>
21 #include "sd-daemon.h"
23 /* The original value of RLIMIT_CORE when i3 was started. We need to restore
24 * this before starting any other process, since we set RLIMIT_CORE to
25 * RLIM_INFINITY for i3 debugging versions. */
26 struct rlimit original_rlimit_core;
28 /* Whether this version of i3 is a debug build or a release build. */
29 bool debug_build = false;
31 /** The number of file descriptors passed via socket activation. */
34 static int xkb_event_base;
36 int xkb_current_group;
42 xcb_connection_t *conn;
43 /* The screen (0 when you are using DISPLAY=:0) of the connection 'conn' */
46 /* Display handle for libstartup-notification */
49 /* The last timestamp we got from X11 (timestamps are included in some events
50 * and are used for some things, like determining a unique ID in startup
52 xcb_timestamp_t last_timestamp = XCB_CURRENT_TIME;
54 xcb_screen_t *root_screen;
57 /* Color depth, visual id and colormap to use when creating windows and
58 * pixmaps. Will use 32 bit depth and an appropriate visual, if available,
59 * otherwise the root window’s default (usually 24 bit TrueColor). */
61 xcb_visualid_t visual_id;
62 xcb_colormap_t colormap;
64 struct ev_loop *main_loop;
66 xcb_key_symbols_t *keysyms;
68 /* Those are our connections to X11 for use with libXcursor and XKB */
69 Display *xlibdpy, *xkbdpy;
71 /* The list of key bindings */
72 struct bindings_head *bindings;
74 /* The list of exec-lines */
75 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
77 /* The list of exec_always lines */
78 struct autostarts_always_head autostarts_always = TAILQ_HEAD_INITIALIZER(autostarts_always);
80 /* The list of assignments */
81 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
83 /* The list of workspace assignments (which workspace should end up on which
85 struct ws_assignments_head ws_assignments = TAILQ_HEAD_INITIALIZER(ws_assignments);
87 /* We hope that those are supported and set them to true */
88 bool xcursor_supported = true;
89 bool xkb_supported = true;
91 /* This will be set to true when -C is used so that functions can behave
92 * slightly differently. We don’t want i3-nagbar to be started when validating
93 * the config, for example. */
94 bool only_check_config = false;
97 * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
98 * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
101 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
102 /* empty, because xcb_prepare_cb and xcb_check_cb are used */
106 * Flush before blocking (and waiting for new events)
109 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
114 * Instead of polling the X connection socket we leave this to
115 * xcb_poll_for_event() which knows better than we can ever know.
118 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
119 xcb_generic_event_t *event;
121 while ((event = xcb_poll_for_event(conn)) != NULL) {
122 if (event->response_type == 0) {
123 if (event_is_ignored(event->sequence, 0))
124 DLOG("Expected X11 Error received for sequence %x\n", event->sequence);
126 xcb_generic_error_t *error = (xcb_generic_error_t*)event;
127 ELOG("X11 Error received! sequence 0x%x, error_code = %d\n",
128 error->sequence, error->error_code);
134 /* Strip off the highest bit (set if the event is generated) */
135 int type = (event->response_type & 0x7F);
137 handle_event(type, event);
145 * When using xmodmap to change the keyboard mapping, this event
146 * is only sent via XKB. Therefore, we need this special handler.
149 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
150 DLOG("Handling XKB event\n");
153 /* When using xmodmap, every change (!) gets an own event.
154 * Therefore, we just read all events and only handle the
155 * mapping_notify once. */
156 bool mapping_changed = false;
157 while (XPending(xkbdpy)) {
158 XNextEvent(xkbdpy, (XEvent*)&ev);
159 /* While we should never receive a non-XKB event,
160 * better do sanity checking */
161 if (ev.type != xkb_event_base)
164 if (ev.any.xkb_type == XkbMapNotify) {
165 mapping_changed = true;
169 if (ev.any.xkb_type != XkbStateNotify) {
170 ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
174 /* See The XKB Extension: Library Specification, section 14.1 */
175 /* We check if the current group (each group contains
176 * two levels) has been changed. Mode_switch activates
177 * group XkbGroup2Index */
178 if (xkb_current_group == ev.state.group)
181 xkb_current_group = ev.state.group;
183 if (ev.state.group == XkbGroup2Index) {
184 DLOG("Mode_switch enabled\n");
185 grab_all_keys(conn, true);
188 if (ev.state.group == XkbGroup1Index) {
189 DLOG("Mode_switch disabled\n");
190 ungrab_all_keys(conn);
191 grab_all_keys(conn, false);
195 if (!mapping_changed)
198 DLOG("Keyboard mapping changed, updating keybindings\n");
199 xcb_key_symbols_free(keysyms);
200 keysyms = xcb_key_symbols_alloc(conn);
202 xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
204 ungrab_all_keys(conn);
205 DLOG("Re-grabbing...\n");
207 grab_all_keys(conn, (xkb_current_group == XkbGroup2Index));
212 * Exit handler which destroys the main_loop. Will trigger cleanup handlers.
215 static void i3_exit(void) {
216 /* We need ev >= 4 for the following code. Since it is not *that* important (it
217 * only makes sure that there are no i3-nagbar instances left behind) we still
218 * support old systems with libev 3. */
219 #if EV_VERSION_MAJOR >= 4
220 ev_loop_destroy(main_loop);
223 if (*shmlogname != '\0') {
224 fprintf(stderr, "Closing SHM log \"%s\"\n", shmlogname);
226 shm_unlink(shmlogname);
231 * (One-shot) Handler for all signals with default action "Term", see signal(7)
233 * Unlinks the SHM log and re-raises the signal.
236 static void handle_signal(int sig, siginfo_t *info, void *data) {
237 fprintf(stderr, "Received signal %d, terminating\n", sig);
238 if (*shmlogname != '\0') {
239 fprintf(stderr, "Closing SHM log \"%s\"\n", shmlogname);
240 shm_unlink(shmlogname);
246 int main(int argc, char *argv[]) {
247 /* Keep a symbol pointing to the I3_VERSION string constant so that we have
248 * it in gdb backtraces. */
249 const char *i3_version = I3_VERSION;
250 char *override_configpath = NULL;
251 bool autostart = true;
252 char *layout_path = NULL;
253 bool delete_layout_path = false;
254 bool force_xinerama = false;
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},
272 int option_index = 0, opt;
273 xcb_void_cookie_t colormap_cookie;
275 setlocale(LC_ALL, "");
277 /* Get the RLIMIT_CORE limit at startup time to restore this before
278 * starting processes. */
279 getrlimit(RLIMIT_CORE, &original_rlimit_core);
281 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
282 if (!isatty(fileno(stdout)))
283 setbuf(stdout, NULL);
287 /* Init logging *before* initializing debug_build to guarantee early
291 /* i3_version contains either something like this:
292 * "4.0.2 (2011-11-11, branch "release")".
293 * or: "4.0.2-123-gCOFFEEBABE (2011-11-11, branch "next")".
295 * So we check for the offset of the first opening round bracket to
296 * determine whether this is a git version or a release version. */
297 debug_build = ((strchr(i3_version, '(') - i3_version) > 10);
299 /* On non-release builds, disable SHM logging by default. */
300 shmlog_size = (debug_build ? 25 * 1024 * 1024 : 0);
304 while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
307 LOG("Autostart disabled using -a\n");
312 layout_path = sstrdup(optarg);
313 delete_layout_path = false;
316 FREE(override_configpath);
317 override_configpath = sstrdup(optarg);
320 LOG("Checking configuration file only (-C)\n");
321 only_check_config = true;
324 printf("i3 version " I3_VERSION " © 2009-2011 Michael Stapelberg and contributors\n");
330 LOG("Enabling debug loglevel %s\n", optarg);
331 add_loglevel(optarg);
334 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
337 if (strcmp(long_options[option_index].name, "force-xinerama") == 0 ||
338 strcmp(long_options[option_index].name, "force_xinerama") == 0) {
339 force_xinerama = true;
340 ELOG("Using Xinerama instead of RandR. This option should be "
341 "avoided at all cost because it does not refresh the list "
342 "of screens, so you cannot configure displays at runtime. "
343 "Please check if your driver really does not support RandR "
344 "and disable this option as soon as you can.\n");
346 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
347 disable_signalhandler = true;
349 } else if (strcmp(long_options[option_index].name, "get-socketpath") == 0 ||
350 strcmp(long_options[option_index].name, "get_socketpath") == 0) {
351 char *socket_path = root_atom_contents("I3_SOCKET_PATH");
353 printf("%s\n", socket_path);
358 } else if (strcmp(long_options[option_index].name, "shmlog-size") == 0 ||
359 strcmp(long_options[option_index].name, "shmlog_size") == 0) {
360 shmlog_size = atoi(optarg);
361 /* Re-initialize logging immediately to get as many
362 * logmessages as possible into the SHM log. */
364 LOG("Limiting SHM log size to %d bytes\n", shmlog_size);
366 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
368 layout_path = sstrdup(optarg);
369 delete_layout_path = true;
374 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
375 fprintf(stderr, "\n");
376 fprintf(stderr, "\t-a disable autostart ('exec' lines in config)\n");
377 fprintf(stderr, "\t-c <file> use the provided configfile instead\n");
378 fprintf(stderr, "\t-C validate configuration file and exit\n");
379 fprintf(stderr, "\t-d <level> enable debug output with the specified loglevel\n");
380 fprintf(stderr, "\t-L <file> path to the serialized layout during restarts\n");
381 fprintf(stderr, "\t-v display version and exit\n");
382 fprintf(stderr, "\t-V enable verbose mode\n");
383 fprintf(stderr, "\n");
384 fprintf(stderr, "\t--force-xinerama\n"
385 "\tUse Xinerama instead of RandR.\n"
386 "\tThis option should only be used if you are stuck with the\n"
387 "\tnvidia closed source driver which does not support RandR.\n");
388 fprintf(stderr, "\n");
389 fprintf(stderr, "\t--get-socketpath\n"
390 "\tRetrieve the i3 IPC socket path from X11, print it, then exit.\n");
391 fprintf(stderr, "\n");
392 fprintf(stderr, "\t--shmlog-size <limit>\n"
393 "\tLimits the size of the i3 SHM log to <limit> bytes. Setting this\n"
394 "\tto 0 disables SHM logging entirely.\n"
395 "\tThe default is %d bytes.\n", shmlog_size);
396 fprintf(stderr, "\n");
397 fprintf(stderr, "If you pass plain text arguments, i3 will interpret them as a command\n"
398 "to send to a currently running i3 (like i3-msg). This allows you to\n"
399 "use nice and logical commands, such as:\n"
402 "\ti3 floating toggle\n"
409 /* If the user passes more arguments, we act like i3-msg would: Just send
410 * the arguments as an IPC message to i3. This allows for nice semantic
411 * commands such as 'i3 border none'. */
413 /* We enable verbose mode so that the user knows what’s going on.
414 * This should make it easier to find mistakes when the user passes
415 * arguments by mistake. */
418 LOG("Additional arguments passed. Sending them as a command to i3.\n");
419 char *payload = NULL;
420 while (optind < argc) {
422 payload = sstrdup(argv[optind]);
425 sasprintf(&both, "%s %s", payload, argv[optind]);
431 LOG("Command is: %s (%d bytes)\n", payload, strlen(payload));
432 char *socket_path = root_atom_contents("I3_SOCKET_PATH");
434 ELOG("Could not get i3 IPC socket path\n");
438 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
440 err(EXIT_FAILURE, "Could not create socket");
442 struct sockaddr_un addr;
443 memset(&addr, 0, sizeof(struct sockaddr_un));
444 addr.sun_family = AF_LOCAL;
445 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
446 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
447 err(EXIT_FAILURE, "Could not connect to i3");
449 if (ipc_send_message(sockfd, strlen(payload), I3_IPC_MESSAGE_TYPE_COMMAND,
450 (uint8_t*)payload) == -1)
451 err(EXIT_FAILURE, "IPC: write()");
453 uint32_t reply_length;
456 if ((ret = ipc_recv_message(sockfd, I3_IPC_MESSAGE_TYPE_COMMAND,
457 &reply_length, &reply)) != 0) {
459 err(EXIT_FAILURE, "IPC: read()");
462 printf("%.*s\n", reply_length, reply);
466 /* Enable logging to handle the case when the user did not specify --shmlog-size */
469 /* Try to enable core dumps by default when running a debug build */
471 struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
472 setrlimit(RLIMIT_CORE, &limit);
474 /* The following code is helpful, but not required. We thus don’t pay
475 * much attention to error handling, non-linux or other edge cases. */
477 LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
478 if (getcwd(cwd, sizeof(cwd)) != NULL)
479 LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
481 if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
482 memset(cwd, '\0', sizeof(cwd));
483 if (read(patternfd, cwd, sizeof(cwd)) > 0)
484 /* a trailing newline is included in cwd */
485 LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
490 LOG("i3 (tree) version " I3_VERSION " starting\n");
492 conn = xcb_connect(NULL, &conn_screen);
493 if (xcb_connection_has_error(conn))
494 errx(EXIT_FAILURE, "Cannot open display\n");
496 sndisplay = sn_xcb_display_new(conn, NULL, NULL);
498 /* Initialize the libev event loop. This needs to be done before loading
499 * the config file because the parser will install an ev_child watcher
500 * for the nagbar when config errors are found. */
501 main_loop = EV_DEFAULT;
502 if (main_loop == NULL)
503 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
505 root_screen = xcb_aux_get_screen(conn, conn_screen);
506 root = root_screen->root;
508 /* By default, we use the same depth and visual as the root window, which
509 * usually is TrueColor (24 bit depth) and the corresponding visual.
510 * However, we also check if a 32 bit depth and visual are available (for
511 * transparency) and use it if so. */
512 root_depth = root_screen->root_depth;
513 visual_id = root_screen->root_visual;
514 colormap = root_screen->default_colormap;
516 DLOG("root_depth = %d, visual_id = 0x%08x.\n", root_depth, visual_id);
518 xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
519 xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
521 load_configuration(conn, override_configpath, false);
522 if (only_check_config) {
523 LOG("Done checking configuration file. Exiting.\n");
527 if (config.ipc_socket_path == NULL) {
528 /* Fall back to a file name in /tmp/ based on the PID */
529 if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
530 config.ipc_socket_path = get_process_filename("ipc-socket");
532 config.ipc_socket_path = sstrdup(config.ipc_socket_path);
535 uint32_t mask = XCB_CW_EVENT_MASK;
536 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
537 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
538 projector), the root window gets a
540 XCB_EVENT_MASK_POINTER_MOTION |
541 XCB_EVENT_MASK_PROPERTY_CHANGE |
542 XCB_EVENT_MASK_ENTER_WINDOW };
543 xcb_void_cookie_t cookie;
544 cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
545 check_error(conn, cookie, "Another window manager seems to be running");
547 /* By now we already checked for replies once, so let’s see if colormap
548 * creation worked (if requested). */
549 if (colormap != root_screen->default_colormap) {
550 xcb_generic_error_t *error = xcb_request_check(conn, colormap_cookie);
552 ELOG("Could not create ColorMap for 32 bit visual, falling back to X11 default.\n");
553 root_depth = root_screen->root_depth;
554 visual_id = root_screen->root_visual;
555 colormap = root_screen->default_colormap;
556 DLOG("root_depth = %d, visual_id = 0x%08x.\n", root_depth, visual_id);
561 xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(conn, gcookie, NULL);
562 if (greply == NULL) {
563 ELOG("Could not get geometry of the root window, exiting\n");
566 DLOG("root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
568 /* Place requests for the atoms we need as soon as possible */
569 #define xmacro(atom) \
570 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
571 #include "atoms.xmacro"
574 /* Initialize the Xlib connection */
575 xlibdpy = xkbdpy = XOpenDisplay(NULL);
577 /* Try to load the X cursors and initialize the XKB extension */
578 if (xlibdpy == NULL) {
579 ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n");
580 xcursor_supported = false;
581 xkb_supported = false;
582 } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) {
583 ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
586 xcursor_load_cursors();
590 /* Set a cursor for the root window (otherwise the root window will show no
591 cursor until the first client is launched). */
592 if (xcursor_supported)
593 xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
594 else xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
598 major = XkbMajorVersion,
599 minor = XkbMinorVersion;
601 if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
602 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
607 if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
608 fprintf(stderr, "XKB not supported by X-server\n");
611 /* end of ugliness */
613 if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
614 XkbMapNotifyMask | XkbStateNotifyMask,
615 XkbMapNotifyMask | XkbStateNotifyMask)) {
616 fprintf(stderr, "Could not set XKB event mask\n");
621 /* Setup NetWM atoms */
622 #define xmacro(name) \
624 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
626 ELOG("Could not get atom " #name "\n"); \
629 A_ ## name = reply->atom; \
632 #include "atoms.xmacro"
635 property_handlers_init();
639 keysyms = xcb_key_symbols_alloc(conn);
641 xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
644 grab_all_keys(conn, false);
646 bool needs_tree_init = true;
648 LOG("Trying to restore the layout from %s...", layout_path);
649 needs_tree_init = !tree_restore(layout_path, greply);
650 if (delete_layout_path)
659 /* Force Xinerama (for drivers which don't support RandR yet, esp. the
660 * nVidia binary graphics driver), when specified either in the config
661 * file or on command-line */
662 if (force_xinerama || config.force_xinerama) {
665 DLOG("Checking for XRandR...\n");
666 randr_init(&randr_base);
669 xcb_query_pointer_reply_t *pointerreply;
670 Output *output = NULL;
671 if (!(pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL))) {
672 ELOG("Could not query pointer position, using first screen\n");
673 output = get_first_output();
675 DLOG("Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
676 output = get_output_containing(pointerreply->root_x, pointerreply->root_y);
678 ELOG("ERROR: No screen at (%d, %d), starting on the first screen\n",
679 pointerreply->root_x, pointerreply->root_y);
680 output = get_first_output();
683 con_focus(con_descend_focused(output_get_content(output->con)));
688 /* Create the UNIX domain socket for IPC */
689 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
690 if (ipc_socket == -1) {
691 ELOG("Could not create the IPC socket, IPC disabled\n");
693 free(config.ipc_socket_path);
694 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
695 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
696 ev_io_start(main_loop, ipc_io);
699 /* Also handle the UNIX domain sockets passed via socket activation. The
700 * parameter 1 means "remove the environment variables", we don’t want to
701 * pass these to child processes. */
702 listen_fds = sd_listen_fds(0);
704 ELOG("socket activation: Error in sd_listen_fds\n");
705 else if (listen_fds == 0)
706 DLOG("socket activation: no sockets passed\n");
709 for (int fd = SD_LISTEN_FDS_START;
710 fd < (SD_LISTEN_FDS_START + listen_fds);
712 DLOG("socket activation: also listening on fd %d\n", fd);
714 /* sd_listen_fds() enables FD_CLOEXEC by default.
715 * However, we need to keep the file descriptors open for in-place
716 * restarting, therefore we explicitly disable FD_CLOEXEC. */
717 if ((flags = fcntl(fd, F_GETFD)) < 0 ||
718 fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) < 0) {
719 ELOG("Could not disable FD_CLOEXEC on fd %d\n", fd);
722 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
723 ev_io_init(ipc_io, ipc_new_client, fd, EV_READ);
724 ev_io_start(main_loop, ipc_io);
728 /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
731 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
732 struct ev_io *xkb = scalloc(sizeof(struct ev_io));
733 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
734 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
736 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
737 ev_io_start(main_loop, xcb_watcher);
741 ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
742 ev_io_start(main_loop, xkb);
744 /* Flush the buffer so that libev can properly get new events */
748 ev_check_init(xcb_check, xcb_check_cb);
749 ev_check_start(main_loop, xcb_check);
751 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
752 ev_prepare_start(main_loop, xcb_prepare);
756 manage_existing_windows(root);
758 struct sigaction action;
760 action.sa_sigaction = handle_signal;
761 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
762 sigemptyset(&action.sa_mask);
764 if (!disable_signalhandler)
765 setup_signal_handler();
767 /* Catch all signals with default action "Core", see signal(7) */
768 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
769 sigaction(SIGILL, &action, NULL) == -1 ||
770 sigaction(SIGABRT, &action, NULL) == -1 ||
771 sigaction(SIGFPE, &action, NULL) == -1 ||
772 sigaction(SIGSEGV, &action, NULL) == -1)
773 ELOG("Could not setup signal handler");
776 /* Catch all signals with default action "Term", see signal(7) */
777 if (sigaction(SIGHUP, &action, NULL) == -1 ||
778 sigaction(SIGINT, &action, NULL) == -1 ||
779 sigaction(SIGALRM, &action, NULL) == -1 ||
780 sigaction(SIGUSR1, &action, NULL) == -1 ||
781 sigaction(SIGUSR2, &action, NULL) == -1)
782 ELOG("Could not setup signal handler");
784 /* Ignore SIGPIPE to survive errors when an IPC client disconnects
785 * while we are sending him a message */
786 signal(SIGPIPE, SIG_IGN);
788 /* Autostarting exec-lines */
790 struct Autostart *exec;
791 TAILQ_FOREACH(exec, &autostarts, autostarts) {
792 LOG("auto-starting %s\n", exec->command);
793 start_application(exec->command, exec->no_startup_id);
797 /* Autostarting exec_always-lines */
798 struct Autostart *exec_always;
799 TAILQ_FOREACH(exec_always, &autostarts_always, autostarts_always) {
800 LOG("auto-starting (always!) %s\n", exec_always->command);
801 start_application(exec_always->command, exec_always->no_startup_id);
804 /* Start i3bar processes for all configured bars */
805 Barconfig *barconfig;
806 TAILQ_FOREACH(barconfig, &barconfigs, configs) {
807 char *command = NULL;
808 sasprintf(&command, "%s --bar_id=%s --socket=\"%s\"",
809 barconfig->i3bar_command ? barconfig->i3bar_command : "i3bar",
810 barconfig->id, current_socketpath);
811 LOG("Starting bar process: %s\n", command);
812 start_application(command, true);
816 /* Make sure to destroy the event loop to invoke the cleeanup callbacks
817 * when calling exit() */
820 ev_loop(main_loop, 0);