i3
src/main.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * main.c: Initialization, main loop
00008  *
00009  */
00010 #include <ev.h>
00011 #include <fcntl.h>
00012 #include <sys/types.h>
00013 #include <sys/socket.h>
00014 #include <sys/un.h>
00015 #include <sys/time.h>
00016 #include <sys/resource.h>
00017 #include "all.h"
00018 
00019 #include "sd-daemon.h"
00020 
00021 /* The original value of RLIMIT_CORE when i3 was started. We need to restore
00022  * this before starting any other process, since we set RLIMIT_CORE to
00023  * RLIM_INFINITY for i3 debugging versions. */
00024 struct rlimit original_rlimit_core;
00025 
00026 static int xkb_event_base;
00027 
00028 int xkb_current_group;
00029 
00030 extern Con *focused;
00031 
00032 char **start_argv;
00033 
00034 xcb_connection_t *conn;
00035 /* The screen (0 when you are using DISPLAY=:0) of the connection 'conn' */
00036 int conn_screen;
00037 
00038 /* Display handle for libstartup-notification */
00039 SnDisplay *sndisplay;
00040 
00041 /* The last timestamp we got from X11 (timestamps are included in some events
00042  * and are used for some things, like determining a unique ID in startup
00043  * notification). */
00044 xcb_timestamp_t last_timestamp = XCB_CURRENT_TIME;
00045 
00046 xcb_screen_t *root_screen;
00047 xcb_window_t root;
00048 uint8_t root_depth;
00049 
00050 struct ev_loop *main_loop;
00051 
00052 xcb_key_symbols_t *keysyms;
00053 
00054 /* Those are our connections to X11 for use with libXcursor and XKB */
00055 Display *xlibdpy, *xkbdpy;
00056 
00057 /* The list of key bindings */
00058 struct bindings_head *bindings;
00059 
00060 /* The list of exec-lines */
00061 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
00062 
00063 /* The list of exec_always lines */
00064 struct autostarts_always_head autostarts_always = TAILQ_HEAD_INITIALIZER(autostarts_always);
00065 
00066 /* The list of assignments */
00067 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
00068 
00069 /* The list of workspace assignments (which workspace should end up on which
00070  * output) */
00071 struct ws_assignments_head ws_assignments = TAILQ_HEAD_INITIALIZER(ws_assignments);
00072 
00073 /* We hope that those are supported and set them to true */
00074 bool xcursor_supported = true;
00075 bool xkb_supported = true;
00076 
00077 /* This will be set to true when -C is used so that functions can behave
00078  * slightly differently. We don’t want i3-nagbar to be started when validating
00079  * the config, for example. */
00080 bool only_check_config = false;
00081 
00082 /*
00083  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
00084  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
00085  *
00086  */
00087 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
00088     /* empty, because xcb_prepare_cb and xcb_check_cb are used */
00089 }
00090 
00091 /*
00092  * Flush before blocking (and waiting for new events)
00093  *
00094  */
00095 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
00096     xcb_flush(conn);
00097 }
00098 
00099 /*
00100  * Instead of polling the X connection socket we leave this to
00101  * xcb_poll_for_event() which knows better than we can ever know.
00102  *
00103  */
00104 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
00105     xcb_generic_event_t *event;
00106 
00107     while ((event = xcb_poll_for_event(conn)) != NULL) {
00108         if (event->response_type == 0) {
00109             if (event_is_ignored(event->sequence, 0))
00110                 DLOG("Expected X11 Error received for sequence %x\n", event->sequence);
00111             else {
00112                 xcb_generic_error_t *error = (xcb_generic_error_t*)event;
00113                 ELOG("X11 Error received! sequence 0x%x, error_code = %d\n",
00114                      error->sequence, error->error_code);
00115             }
00116             free(event);
00117             continue;
00118         }
00119 
00120         /* Strip off the highest bit (set if the event is generated) */
00121         int type = (event->response_type & 0x7F);
00122 
00123         handle_event(type, event);
00124 
00125         free(event);
00126     }
00127 }
00128 
00129 
00130 /*
00131  * When using xmodmap to change the keyboard mapping, this event
00132  * is only sent via XKB. Therefore, we need this special handler.
00133  *
00134  */
00135 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
00136     DLOG("Handling XKB event\n");
00137     XkbEvent ev;
00138 
00139     /* When using xmodmap, every change (!) gets an own event.
00140      * Therefore, we just read all events and only handle the
00141      * mapping_notify once. */
00142     bool mapping_changed = false;
00143     while (XPending(xkbdpy)) {
00144         XNextEvent(xkbdpy, (XEvent*)&ev);
00145         /* While we should never receive a non-XKB event,
00146          * better do sanity checking */
00147         if (ev.type != xkb_event_base)
00148             continue;
00149 
00150         if (ev.any.xkb_type == XkbMapNotify) {
00151             mapping_changed = true;
00152             continue;
00153         }
00154 
00155         if (ev.any.xkb_type != XkbStateNotify) {
00156             ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
00157             continue;
00158         }
00159 
00160         /* See The XKB Extension: Library Specification, section 14.1 */
00161         /* We check if the current group (each group contains
00162          * two levels) has been changed. Mode_switch activates
00163          * group XkbGroup2Index */
00164         if (xkb_current_group == ev.state.group)
00165             continue;
00166 
00167         xkb_current_group = ev.state.group;
00168 
00169         if (ev.state.group == XkbGroup2Index) {
00170             DLOG("Mode_switch enabled\n");
00171             grab_all_keys(conn, true);
00172         }
00173 
00174         if (ev.state.group == XkbGroup1Index) {
00175             DLOG("Mode_switch disabled\n");
00176             ungrab_all_keys(conn);
00177             grab_all_keys(conn, false);
00178         }
00179     }
00180 
00181     if (!mapping_changed)
00182         return;
00183 
00184     DLOG("Keyboard mapping changed, updating keybindings\n");
00185     xcb_key_symbols_free(keysyms);
00186     keysyms = xcb_key_symbols_alloc(conn);
00187 
00188     xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
00189 
00190     ungrab_all_keys(conn);
00191     DLOG("Re-grabbing...\n");
00192     translate_keysyms();
00193     grab_all_keys(conn, (xkb_current_group == XkbGroup2Index));
00194     DLOG("Done\n");
00195 }
00196 
00197 /*
00198  * Exit handler which destroys the main_loop. Will trigger cleanup handlers.
00199  *
00200  */
00201 static void i3_exit() {
00202 /* We need ev >= 4 for the following code. Since it is not *that* important (it
00203  * only makes sure that there are no i3-nagbar instances left behind) we still
00204  * support old systems with libev 3. */
00205 #if EV_VERSION_MAJOR >= 4
00206     ev_loop_destroy(main_loop);
00207 #endif
00208 }
00209 
00210 int main(int argc, char *argv[]) {
00211     char *override_configpath = NULL;
00212     bool autostart = true;
00213     char *layout_path = NULL;
00214     bool delete_layout_path = false;
00215     bool force_xinerama = false;
00216     bool disable_signalhandler = false;
00217     static struct option long_options[] = {
00218         {"no-autostart", no_argument, 0, 'a'},
00219         {"config", required_argument, 0, 'c'},
00220         {"version", no_argument, 0, 'v'},
00221         {"help", no_argument, 0, 'h'},
00222         {"layout", required_argument, 0, 'L'},
00223         {"restart", required_argument, 0, 0},
00224         {"force-xinerama", no_argument, 0, 0},
00225         {"force_xinerama", no_argument, 0, 0},
00226         {"disable-signalhandler", no_argument, 0, 0},
00227         {"get-socketpath", no_argument, 0, 0},
00228         {"get_socketpath", no_argument, 0, 0},
00229         {0, 0, 0, 0}
00230     };
00231     int option_index = 0, opt;
00232 
00233     setlocale(LC_ALL, "");
00234 
00235     /* Get the RLIMIT_CORE limit at startup time to restore this before
00236      * starting processes. */
00237     getrlimit(RLIMIT_CORE, &original_rlimit_core);
00238 
00239     /* Disable output buffering to make redirects in .xsession actually useful for debugging */
00240     if (!isatty(fileno(stdout)))
00241         setbuf(stdout, NULL);
00242 
00243     srand(time(NULL));
00244 
00245     init_logging();
00246 
00247     start_argv = argv;
00248 
00249     while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
00250         switch (opt) {
00251             case 'a':
00252                 LOG("Autostart disabled using -a\n");
00253                 autostart = false;
00254                 break;
00255             case 'L':
00256                 FREE(layout_path);
00257                 layout_path = sstrdup(optarg);
00258                 delete_layout_path = false;
00259                 break;
00260             case 'c':
00261                 FREE(override_configpath);
00262                 override_configpath = sstrdup(optarg);
00263                 break;
00264             case 'C':
00265                 LOG("Checking configuration file only (-C)\n");
00266                 only_check_config = true;
00267                 break;
00268             case 'v':
00269                 printf("i3 version " I3_VERSION " © 2009-2011 Michael Stapelberg and contributors\n");
00270                 exit(EXIT_SUCCESS);
00271             case 'V':
00272                 set_verbosity(true);
00273                 break;
00274             case 'd':
00275                 LOG("Enabling debug loglevel %s\n", optarg);
00276                 add_loglevel(optarg);
00277                 break;
00278             case 'l':
00279                 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
00280                 break;
00281             case 0:
00282                 if (strcmp(long_options[option_index].name, "force-xinerama") == 0 ||
00283                     strcmp(long_options[option_index].name, "force_xinerama") == 0) {
00284                     force_xinerama = true;
00285                     ELOG("Using Xinerama instead of RandR. This option should be "
00286                          "avoided at all cost because it does not refresh the list "
00287                          "of screens, so you cannot configure displays at runtime. "
00288                          "Please check if your driver really does not support RandR "
00289                          "and disable this option as soon as you can.\n");
00290                     break;
00291                 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
00292                     disable_signalhandler = true;
00293                     break;
00294                 } else if (strcmp(long_options[option_index].name, "get-socketpath") == 0 ||
00295                            strcmp(long_options[option_index].name, "get_socketpath") == 0) {
00296                     char *socket_path = socket_path_from_x11();
00297                     if (socket_path) {
00298                         printf("%s\n", socket_path);
00299                         return 0;
00300                     }
00301 
00302                     return 1;
00303                 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
00304                     FREE(layout_path);
00305                     layout_path = sstrdup(optarg);
00306                     delete_layout_path = true;
00307                     break;
00308                 }
00309                 /* fall-through */
00310             default:
00311                 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
00312                 fprintf(stderr, "\n");
00313                 fprintf(stderr, "\t-a          disable autostart ('exec' lines in config)\n");
00314                 fprintf(stderr, "\t-c <file>   use the provided configfile instead\n");
00315                 fprintf(stderr, "\t-C          validate configuration file and exit\n");
00316                 fprintf(stderr, "\t-d <level>  enable debug output with the specified loglevel\n");
00317                 fprintf(stderr, "\t-L <file>   path to the serialized layout during restarts\n");
00318                 fprintf(stderr, "\t-v          display version and exit\n");
00319                 fprintf(stderr, "\t-V          enable verbose mode\n");
00320                 fprintf(stderr, "\n");
00321                 fprintf(stderr, "\t--force-xinerama\n"
00322                                 "\tUse Xinerama instead of RandR.\n"
00323                                 "\tThis option should only be used if you are stuck with the\n"
00324                                 "\tnvidia closed source driver which does not support RandR.\n");
00325                 fprintf(stderr, "\n");
00326                 fprintf(stderr, "\t--get-socketpath\n"
00327                                 "\tRetrieve the i3 IPC socket path from X11, print it, then exit.\n");
00328                 fprintf(stderr, "\n");
00329                 fprintf(stderr, "If you pass plain text arguments, i3 will interpret them as a command\n"
00330                                 "to send to a currently running i3 (like i3-msg). This allows you to\n"
00331                                 "use nice and logical commands, such as:\n"
00332                                 "\n"
00333                                 "\ti3 border none\n"
00334                                 "\ti3 floating toggle\n"
00335                                 "\ti3 kill window\n"
00336                                 "\n");
00337                 exit(EXIT_FAILURE);
00338         }
00339     }
00340 
00341     /* If the user passes more arguments, we act like i3-msg would: Just send
00342      * the arguments as an IPC message to i3. This allows for nice semantic
00343      * commands such as 'i3 border none'. */
00344     if (optind < argc) {
00345         /* We enable verbose mode so that the user knows what’s going on.
00346          * This should make it easier to find mistakes when the user passes
00347          * arguments by mistake. */
00348         set_verbosity(true);
00349 
00350         LOG("Additional arguments passed. Sending them as a command to i3.\n");
00351         char *payload = NULL;
00352         while (optind < argc) {
00353             if (!payload) {
00354                 payload = sstrdup(argv[optind]);
00355             } else {
00356                 char *both;
00357                 sasprintf(&both, "%s %s", payload, argv[optind]);
00358                 free(payload);
00359                 payload = both;
00360             }
00361             optind++;
00362         }
00363         LOG("Command is: %s (%d bytes)\n", payload, strlen(payload));
00364         char *socket_path = socket_path_from_x11();
00365         if (!socket_path) {
00366             ELOG("Could not get i3 IPC socket path\n");
00367             return 1;
00368         }
00369 
00370         int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
00371         if (sockfd == -1)
00372             err(EXIT_FAILURE, "Could not create socket");
00373 
00374         struct sockaddr_un addr;
00375         memset(&addr, 0, sizeof(struct sockaddr_un));
00376         addr.sun_family = AF_LOCAL;
00377         strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
00378         if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
00379             err(EXIT_FAILURE, "Could not connect to i3");
00380 
00381         if (ipc_send_message(sockfd, strlen(payload), I3_IPC_MESSAGE_TYPE_COMMAND,
00382                              (uint8_t*)payload) == -1)
00383             err(EXIT_FAILURE, "IPC: write()");
00384 
00385         uint32_t reply_length;
00386         uint8_t *reply;
00387         int ret;
00388         if ((ret = ipc_recv_message(sockfd, I3_IPC_MESSAGE_TYPE_COMMAND,
00389                                     &reply_length, &reply)) != 0) {
00390             if (ret == -1)
00391                 err(EXIT_FAILURE, "IPC: read()");
00392             return 1;
00393         }
00394         printf("%.*s\n", reply_length, reply);
00395         return 0;
00396     }
00397 
00398     /* I3_VERSION contains either something like this:
00399      *     "4.0.2 (2011-11-11, branch "release")".
00400      * or: "4.0.2-123-gCOFFEEBABE (2011-11-11, branch "next")".
00401      *
00402      * So we check for the offset of the first opening round bracket to
00403      * determine whether this is a git version or a release version. */
00404     if ((strchr(I3_VERSION, '(') - I3_VERSION) > 10) {
00405         struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
00406         setrlimit(RLIMIT_CORE, &limit);
00407 
00408         /* The following code is helpful, but not required. We thus don’t pay
00409          * much attention to error handling, non-linux or other edge cases. */
00410         char cwd[PATH_MAX];
00411         LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
00412         if (getcwd(cwd, sizeof(cwd)) != NULL)
00413             LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
00414         int patternfd;
00415         if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
00416             memset(cwd, '\0', sizeof(cwd));
00417             if (read(patternfd, cwd, sizeof(cwd)) > 0)
00418                 /* a trailing newline is included in cwd */
00419                 LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
00420             close(patternfd);
00421         }
00422     }
00423 
00424     LOG("i3 (tree) version " I3_VERSION " starting\n");
00425 
00426     conn = xcb_connect(NULL, &conn_screen);
00427     if (xcb_connection_has_error(conn))
00428         errx(EXIT_FAILURE, "Cannot open display\n");
00429 
00430     sndisplay = sn_xcb_display_new(conn, NULL, NULL);
00431 
00432     /* Initialize the libev event loop. This needs to be done before loading
00433      * the config file because the parser will install an ev_child watcher
00434      * for the nagbar when config errors are found. */
00435     main_loop = EV_DEFAULT;
00436     if (main_loop == NULL)
00437             die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
00438 
00439     root_screen = xcb_aux_get_screen(conn, conn_screen);
00440     root = root_screen->root;
00441     root_depth = root_screen->root_depth;
00442     xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
00443     xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
00444 
00445     load_configuration(conn, override_configpath, false);
00446     if (only_check_config) {
00447         LOG("Done checking configuration file. Exiting.\n");
00448         exit(0);
00449     }
00450 
00451     if (config.ipc_socket_path == NULL) {
00452         /* Fall back to a file name in /tmp/ based on the PID */
00453         if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
00454             config.ipc_socket_path = get_process_filename("ipc-socket");
00455         else
00456             config.ipc_socket_path = sstrdup(config.ipc_socket_path);
00457     }
00458 
00459     uint32_t mask = XCB_CW_EVENT_MASK;
00460     uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
00461                           XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
00462                                                                            projector), the root window gets a
00463                                                                            ConfigureNotify */
00464                           XCB_EVENT_MASK_POINTER_MOTION |
00465                           XCB_EVENT_MASK_PROPERTY_CHANGE |
00466                           XCB_EVENT_MASK_ENTER_WINDOW };
00467     xcb_void_cookie_t cookie;
00468     cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
00469     check_error(conn, cookie, "Another window manager seems to be running");
00470 
00471     xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(conn, gcookie, NULL);
00472     if (greply == NULL) {
00473         ELOG("Could not get geometry of the root window, exiting\n");
00474         return 1;
00475     }
00476     DLOG("root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
00477 
00478     /* Place requests for the atoms we need as soon as possible */
00479     #define xmacro(atom) \
00480         xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
00481     #include "atoms.xmacro"
00482     #undef xmacro
00483 
00484     /* Initialize the Xlib connection */
00485     xlibdpy = xkbdpy = XOpenDisplay(NULL);
00486 
00487     /* Try to load the X cursors and initialize the XKB extension */
00488     if (xlibdpy == NULL) {
00489         ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n");
00490         xcursor_supported = false;
00491         xkb_supported = false;
00492     } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) {
00493         ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
00494         return 1;
00495     } else {
00496         xcursor_load_cursors();
00497         /*init_xkb();*/
00498     }
00499 
00500     /* Set a cursor for the root window (otherwise the root window will show no
00501        cursor until the first client is launched). */
00502     if (xcursor_supported)
00503         xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
00504     else xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
00505 
00506     if (xkb_supported) {
00507         int errBase,
00508             major = XkbMajorVersion,
00509             minor = XkbMinorVersion;
00510 
00511         if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
00512             fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
00513             return 1;
00514         }
00515 
00516         int i1;
00517         if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
00518             fprintf(stderr, "XKB not supported by X-server\n");
00519             return 1;
00520         }
00521         /* end of ugliness */
00522 
00523         if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
00524                              XkbMapNotifyMask | XkbStateNotifyMask,
00525                              XkbMapNotifyMask | XkbStateNotifyMask)) {
00526             fprintf(stderr, "Could not set XKB event mask\n");
00527             return 1;
00528         }
00529     }
00530 
00531     /* Setup NetWM atoms */
00532     #define xmacro(name) \
00533         do { \
00534             xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
00535             if (!reply) { \
00536                 ELOG("Could not get atom " #name "\n"); \
00537                 exit(-1); \
00538             } \
00539             A_ ## name = reply->atom; \
00540             free(reply); \
00541         } while (0);
00542     #include "atoms.xmacro"
00543     #undef xmacro
00544 
00545     property_handlers_init();
00546 
00547     ewmh_setup_hints();
00548 
00549     keysyms = xcb_key_symbols_alloc(conn);
00550 
00551     xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
00552 
00553     translate_keysyms();
00554     grab_all_keys(conn, false);
00555 
00556     bool needs_tree_init = true;
00557     if (layout_path) {
00558         LOG("Trying to restore the layout from %s...", layout_path);
00559         needs_tree_init = !tree_restore(layout_path, greply);
00560         if (delete_layout_path)
00561             unlink(layout_path);
00562         free(layout_path);
00563     }
00564     if (needs_tree_init)
00565         tree_init(greply);
00566 
00567     free(greply);
00568 
00569     /* Force Xinerama (for drivers which don't support RandR yet, esp. the
00570      * nVidia binary graphics driver), when specified either in the config
00571      * file or on command-line */
00572     if (force_xinerama || config.force_xinerama) {
00573         xinerama_init();
00574     } else {
00575         DLOG("Checking for XRandR...\n");
00576         randr_init(&randr_base);
00577     }
00578 
00579     xcb_query_pointer_reply_t *pointerreply;
00580     Output *output = NULL;
00581     if (!(pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL))) {
00582         ELOG("Could not query pointer position, using first screen\n");
00583         output = get_first_output();
00584     } else {
00585         DLOG("Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
00586         output = get_output_containing(pointerreply->root_x, pointerreply->root_y);
00587         if (!output) {
00588             ELOG("ERROR: No screen at (%d, %d), starting on the first screen\n",
00589                  pointerreply->root_x, pointerreply->root_y);
00590             output = get_first_output();
00591         }
00592 
00593         con_focus(con_descend_focused(output_get_content(output->con)));
00594     }
00595 
00596     tree_render();
00597 
00598     /* Create the UNIX domain socket for IPC */
00599     int ipc_socket = ipc_create_socket(config.ipc_socket_path);
00600     if (ipc_socket == -1) {
00601         ELOG("Could not create the IPC socket, IPC disabled\n");
00602     } else {
00603         free(config.ipc_socket_path);
00604         struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
00605         ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
00606         ev_io_start(main_loop, ipc_io);
00607     }
00608 
00609     /* Also handle the UNIX domain sockets passed via socket activation */
00610     int fds = sd_listen_fds(1);
00611     if (fds < 0)
00612         ELOG("socket activation: Error in sd_listen_fds\n");
00613     else if (fds == 0)
00614         DLOG("socket activation: no sockets passed\n");
00615     else {
00616         for (int fd = SD_LISTEN_FDS_START; fd < (SD_LISTEN_FDS_START + fds); fd++) {
00617             DLOG("socket activation: also listening on fd %d\n", fd);
00618             struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
00619             ev_io_init(ipc_io, ipc_new_client, fd, EV_READ);
00620             ev_io_start(main_loop, ipc_io);
00621         }
00622     }
00623 
00624     /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
00625     x_set_i3_atoms();
00626 
00627     struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
00628     struct ev_io *xkb = scalloc(sizeof(struct ev_io));
00629     struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
00630     struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
00631 
00632     ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
00633     ev_io_start(main_loop, xcb_watcher);
00634 
00635 
00636     if (xkb_supported) {
00637         ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
00638         ev_io_start(main_loop, xkb);
00639 
00640         /* Flush the buffer so that libev can properly get new events */
00641         XFlush(xkbdpy);
00642     }
00643 
00644     ev_check_init(xcb_check, xcb_check_cb);
00645     ev_check_start(main_loop, xcb_check);
00646 
00647     ev_prepare_init(xcb_prepare, xcb_prepare_cb);
00648     ev_prepare_start(main_loop, xcb_prepare);
00649 
00650     xcb_flush(conn);
00651 
00652     manage_existing_windows(root);
00653 
00654     if (!disable_signalhandler)
00655         setup_signal_handler();
00656 
00657     /* Ignore SIGPIPE to survive errors when an IPC client disconnects
00658      * while we are sending him a message */
00659     signal(SIGPIPE, SIG_IGN);
00660 
00661     /* Autostarting exec-lines */
00662     if (autostart) {
00663         struct Autostart *exec;
00664         TAILQ_FOREACH(exec, &autostarts, autostarts) {
00665             LOG("auto-starting %s\n", exec->command);
00666             start_application(exec->command, exec->no_startup_id);
00667         }
00668     }
00669 
00670     /* Autostarting exec_always-lines */
00671     struct Autostart *exec_always;
00672     TAILQ_FOREACH(exec_always, &autostarts_always, autostarts_always) {
00673         LOG("auto-starting (always!) %s\n", exec_always->command);
00674         start_application(exec_always->command, exec_always->no_startup_id);
00675     }
00676 
00677     /* Start i3bar processes for all configured bars */
00678     Barconfig *barconfig;
00679     TAILQ_FOREACH(barconfig, &barconfigs, configs) {
00680         char *command = NULL;
00681         sasprintf(&command, "i3bar --bar_id=%s --socket=\"%s\"",
00682                   barconfig->id, current_socketpath);
00683         LOG("Starting bar process: %s\n", command);
00684         start_application(command, true);
00685         free(command);
00686     }
00687 
00688     /* Make sure to destroy the event loop to invoke the cleeanup callbacks
00689      * when calling exit() */
00690     atexit(i3_exit);
00691 
00692     ev_loop(main_loop, 0);
00693 }