i3

src/mainx.c

Go to the documentation of this file.
00001 /*
00002  * vim:ts=8:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  *
00006  * © 2009-2010 Michael Stapelberg and contributors
00007  *
00008  * See file LICENSE for license information.
00009  *
00010  */
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <sys/types.h>
00015 #include <unistd.h>
00016 #include <stdbool.h>
00017 #include <assert.h>
00018 #include <limits.h>
00019 #include <locale.h>
00020 #include <fcntl.h>
00021 #include <getopt.h>
00022 
00023 #include <X11/XKBlib.h>
00024 #include <X11/extensions/XKB.h>
00025 
00026 #include <xcb/xcb.h>
00027 #include <xcb/xcb_atom.h>
00028 #include <xcb/xcb_aux.h>
00029 #include <xcb/xcb_event.h>
00030 #include <xcb/xcb_property.h>
00031 #include <xcb/xcb_keysyms.h>
00032 #include <xcb/xcb_icccm.h>
00033 
00034 #include <ev.h>
00035 
00036 #include "config.h"
00037 #include "data.h"
00038 #include "debug.h"
00039 #include "handlers.h"
00040 #include "click.h"
00041 #include "i3.h"
00042 #include "layout.h"
00043 #include "queue.h"
00044 #include "table.h"
00045 #include "util.h"
00046 #include "xcb.h"
00047 #include "randr.h"
00048 #include "xinerama.h"
00049 #include "manage.h"
00050 #include "ipc.h"
00051 #include "log.h"
00052 #include "sighandler.h"
00053 
00054 static int xkb_event_base;
00055 
00056 int xkb_current_group;
00057 
00058 xcb_connection_t *global_conn;
00059 
00060 /* This is the path to i3, copied from argv[0] when starting up */
00061 char **start_argv;
00062 
00063 /* This is our connection to X11 for use with XKB */
00064 Display *xkbdpy;
00065 
00066 xcb_key_symbols_t *keysyms;
00067 
00068 /* The list of key bindings */
00069 struct bindings_head *bindings;
00070 
00071 /* The list of exec-lines */
00072 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
00073 
00074 /* The list of assignments */
00075 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
00076 
00077 /* This is a list of Stack_Windows, global, for easier/faster access on expose events */
00078 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
00079 
00080 /* The event handlers need to be global because they are accessed by our custom event handler
00081    in handle_button_press(), needed for graphical resizing */
00082 xcb_event_handlers_t evenths;
00083 xcb_atom_t atoms[NUM_ATOMS];
00084 
00085 xcb_window_t root;
00086 int num_screens = 0;
00087 
00088 /* The depth of the root screen (used e.g. for creating new pixmaps later) */
00089 uint8_t root_depth;
00090 
00091 /* We hope that XKB is supported and set this to false */
00092 bool xkb_supported = true;
00093 
00094 /*
00095  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
00096  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
00097  *
00098  */
00099 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
00100         /* empty, because xcb_prepare_cb and xcb_check_cb are used */
00101 }
00102 
00103 /*
00104  * Flush before blocking (and waiting for new events)
00105  *
00106  */
00107 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
00108         xcb_flush(evenths.c);
00109 }
00110 
00111 /*
00112  * Instead of polling the X connection socket we leave this to
00113  * xcb_poll_for_event() which knows better than we can ever know.
00114  *
00115  */
00116 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
00117         xcb_generic_event_t *event;
00118 
00119         while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
00120                 xcb_event_handle(&evenths, event);
00121                 free(event);
00122         }
00123 }
00124 
00125 /*
00126  * When using xmodmap to change the keyboard mapping, this event
00127  * is only sent via XKB. Therefore, we need this special handler.
00128  *
00129  */
00130 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
00131         DLOG("Handling XKB event\n");
00132         XkbEvent ev;
00133 
00134         /* When using xmodmap, every change (!) gets an own event.
00135          * Therefore, we just read all events and only handle the
00136          * mapping_notify once. */
00137         bool mapping_changed = false;
00138         while (XPending(xkbdpy)) {
00139                 XNextEvent(xkbdpy, (XEvent*)&ev);
00140                 /* While we should never receive a non-XKB event,
00141                  * better do sanity checking */
00142                 if (ev.type != xkb_event_base)
00143                         continue;
00144 
00145                 if (ev.any.xkb_type == XkbMapNotify) {
00146                         mapping_changed = true;
00147                         continue;
00148                 }
00149 
00150                 if (ev.any.xkb_type != XkbStateNotify) {
00151                         ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
00152                         continue;
00153                 }
00154 
00155                 /* See The XKB Extension: Library Specification, section 14.1 */
00156                 /* We check if the current group (each group contains
00157                  * two levels) has been changed. Mode_switch activates
00158                  * group XkbGroup2Index */
00159                 if (xkb_current_group == ev.state.group)
00160                         continue;
00161 
00162                 xkb_current_group = ev.state.group;
00163 
00164                 if (ev.state.group == XkbGroup2Index) {
00165                         DLOG("Mode_switch enabled\n");
00166                         grab_all_keys(global_conn, true);
00167                 }
00168 
00169                 if (ev.state.group == XkbGroup1Index) {
00170                         DLOG("Mode_switch disabled\n");
00171                         ungrab_all_keys(global_conn);
00172                         grab_all_keys(global_conn, false);
00173                 }
00174         }
00175 
00176         if (!mapping_changed)
00177                 return;
00178 
00179         DLOG("Keyboard mapping changed, updating keybindings\n");
00180         xcb_key_symbols_free(keysyms);
00181         keysyms = xcb_key_symbols_alloc(global_conn);
00182 
00183         xcb_get_numlock_mask(global_conn);
00184 
00185         ungrab_all_keys(global_conn);
00186         DLOG("Re-grabbing...\n");
00187         translate_keysyms();
00188         grab_all_keys(global_conn, (xkb_current_group == XkbGroup2Index));
00189         DLOG("Done\n");
00190 }
00191 
00192 
00193 int main(int argc, char *argv[], char *env[]) {
00194         int i, screens, opt;
00195         char *override_configpath = NULL;
00196         bool autostart = true;
00197         bool only_check_config = false;
00198         bool force_xinerama = false;
00199         xcb_connection_t *conn;
00200         xcb_property_handlers_t prophs;
00201         xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
00202         static struct option long_options[] = {
00203                 {"no-autostart", no_argument, 0, 'a'},
00204                 {"config", required_argument, 0, 'c'},
00205                 {"version", no_argument, 0, 'v'},
00206                 {"help", no_argument, 0, 'h'},
00207                 {"force-xinerama", no_argument, 0, 0},
00208                 {0, 0, 0, 0}
00209         };
00210         int option_index = 0;
00211 
00212         setlocale(LC_ALL, "");
00213 
00214         /* Disable output buffering to make redirects in .xsession actually useful for debugging */
00215         if (!isatty(fileno(stdout)))
00216                 setbuf(stdout, NULL);
00217 
00218         start_argv = argv;
00219 
00220         while ((opt = getopt_long(argc, argv, "c:Cvahld:V", long_options, &option_index)) != -1) {
00221                 switch (opt) {
00222                         case 'a':
00223                                 LOG("Autostart disabled using -a\n");
00224                                 autostart = false;
00225                                 break;
00226                         case 'c':
00227                                 override_configpath = sstrdup(optarg);
00228                                 break;
00229                         case 'C':
00230                                 LOG("Checking configuration file only (-C)\n");
00231                                 only_check_config = true;
00232                                 break;
00233                         case 'v':
00234                                 printf("i3 version " I3_VERSION " © 2009-2010 Michael Stapelberg and contributors\n");
00235                                 exit(EXIT_SUCCESS);
00236                         case 'V':
00237                                 set_verbosity(true);
00238                                 break;
00239                         case 'd':
00240                                 LOG("Enabling debug loglevel %s\n", optarg);
00241                                 add_loglevel(optarg);
00242                                 break;
00243                         case 'l':
00244                                 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
00245                                 break;
00246                         case 0:
00247                                 if (strcmp(long_options[option_index].name, "force-xinerama") == 0) {
00248                                         force_xinerama = true;
00249                                         ELOG("Using Xinerama instead of RandR. This option should be "
00250                                              "avoided at all cost because it does not refresh the list "
00251                                              "of screens, so you cannot configure displays at runtime. "
00252                                              "Please check if your driver really does not support RandR "
00253                                              "and disable this option as soon as you can.\n");
00254                                         break;
00255                                 }
00256                                 /* fall-through */
00257                         default:
00258                                 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
00259                                 fprintf(stderr, "\n");
00260                                 fprintf(stderr, "-a: disable autostart\n");
00261                                 fprintf(stderr, "-v: display version and exit\n");
00262                                 fprintf(stderr, "-V: enable verbose mode\n");
00263                                 fprintf(stderr, "-d <loglevel>: enable debug loglevel <loglevel>\n");
00264                                 fprintf(stderr, "-c <configfile>: use the provided configfile instead\n");
00265                                 fprintf(stderr, "-C: check configuration file and exit\n");
00266                                 fprintf(stderr, "--force-xinerama: Use Xinerama instead of RandR. This "
00267                                                 "option should only be used if you are stuck with the "
00268                                                 "nvidia closed source driver which does not support RandR.\n");
00269                                 exit(EXIT_FAILURE);
00270                 }
00271         }
00272 
00273         LOG("i3 version " I3_VERSION " starting\n");
00274 
00275         /* Initialize the table data structures for each workspace */
00276         init_table();
00277 
00278         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
00279         memset(&prophs, 0, sizeof(xcb_property_handlers_t));
00280 
00281         conn = global_conn = xcb_connect(NULL, &screens);
00282 
00283         if (xcb_connection_has_error(conn))
00284                 die("Cannot open display\n");
00285 
00286         /* Get the root window */
00287         xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
00288         root = root_screen->root;
00289         root_depth = root_screen->root_depth;
00290 
00291         load_configuration(conn, override_configpath, false);
00292         if (only_check_config) {
00293                 LOG("Done checking configuration file. Exiting.\n");
00294                 exit(0);
00295         }
00296 
00297         /* Create the initial container on the first workspace. This used to
00298          * be part of init_table, but since it possibly requires an X
00299          * connection and a loaded configuration (default mode for new
00300          * containers may be stacking, which requires a new window to be
00301          * created), it had to be delayed. */
00302         expand_table_cols(TAILQ_FIRST(workspaces));
00303         expand_table_rows(TAILQ_FIRST(workspaces));
00304 
00305         /* Place requests for the atoms we need as soon as possible */
00306         #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
00307 
00308         REQUEST_ATOM(_NET_SUPPORTED);
00309         REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
00310         REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
00311         REQUEST_ATOM(_NET_WM_NAME);
00312         REQUEST_ATOM(_NET_WM_STATE);
00313         REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
00314         REQUEST_ATOM(_NET_WM_DESKTOP);
00315         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
00316         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
00317         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
00318         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
00319         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
00320         REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
00321         REQUEST_ATOM(WM_PROTOCOLS);
00322         REQUEST_ATOM(WM_DELETE_WINDOW);
00323         REQUEST_ATOM(UTF8_STRING);
00324         REQUEST_ATOM(WM_STATE);
00325         REQUEST_ATOM(WM_CLIENT_LEADER);
00326         REQUEST_ATOM(_NET_CURRENT_DESKTOP);
00327         REQUEST_ATOM(_NET_ACTIVE_WINDOW);
00328         REQUEST_ATOM(_NET_WORKAREA);
00329 
00330         /* TODO: this has to be more beautiful somewhen */
00331         int major, minor, error;
00332 
00333         major = XkbMajorVersion;
00334         minor = XkbMinorVersion;
00335 
00336         int errBase;
00337 
00338         if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &xkb_event_base, &errBase, &major, &minor, &error)) == NULL) {
00339                 ELOG("ERROR: XkbOpenDisplay() failed, disabling XKB support\n");
00340                 xkb_supported = false;
00341         }
00342 
00343         if (xkb_supported) {
00344                 if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
00345                         fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
00346                         return 1;
00347                 }
00348 
00349                 int i1;
00350                 if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
00351                         fprintf(stderr, "XKB not supported by X-server\n");
00352                         return 1;
00353                 }
00354                 /* end of ugliness */
00355 
00356                 if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
00357                                      XkbMapNotifyMask | XkbStateNotifyMask,
00358                                      XkbMapNotifyMask | XkbStateNotifyMask)) {
00359                         fprintf(stderr, "Could not set XKB event mask\n");
00360                         return 1;
00361                 }
00362         }
00363 
00364         /* Initialize event loop using libev */
00365         struct ev_loop *loop = ev_loop_new(0);
00366         if (loop == NULL)
00367                 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
00368 
00369         struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
00370         struct ev_io *xkb = scalloc(sizeof(struct ev_io));
00371         struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
00372         struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
00373 
00374         ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
00375         ev_io_start(loop, xcb_watcher);
00376 
00377         if (xkb_supported) {
00378                 ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
00379                 ev_io_start(loop, xkb);
00380 
00381                 /* Flush the buffer so that libev can properly get new events */
00382                 XFlush(xkbdpy);
00383         }
00384 
00385         ev_check_init(xcb_check, xcb_check_cb);
00386         ev_check_start(loop, xcb_check);
00387 
00388         ev_prepare_init(xcb_prepare, xcb_prepare_cb);
00389         ev_prepare_start(loop, xcb_prepare);
00390 
00391         /* Grab the server to delay any events until we enter the eventloop */
00392         xcb_grab_server(conn);
00393 
00394         xcb_event_handlers_init(conn, &evenths);
00395 
00396         /* DEBUG: Trap all events and print them */
00397         for (i = 2; i < 128; ++i)
00398                 xcb_event_set_handler(&evenths, i, handle_event, 0);
00399 
00400         for (i = 0; i < 256; ++i)
00401                 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
00402 
00403         /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
00404         xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
00405 
00406         /* Key presses are pretty obvious, I think */
00407         xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
00408 
00409         /* Enter window = user moved his mouse over the window */
00410         xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
00411 
00412         /* Button press = user pushed a mouse button over one of our windows */
00413         xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
00414 
00415         /* Map notify = there is a new window */
00416         xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
00417 
00418         /* Unmap notify = window disappeared. When sent from a client, we don’t manage
00419            it any longer. Usually, the client destroys the window shortly afterwards. */
00420         xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
00421 
00422         /* Destroy notify is handled the same as unmap notify */
00423         xcb_event_set_destroy_notify_handler(&evenths, handle_destroy_notify_event, NULL);
00424 
00425         /* Configure notify = window’s configuration (geometry, stacking, …). We only need
00426            it to set up ignore the following enter_notify events */
00427         xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
00428 
00429         /* Configure request = window tried to change size on its own */
00430         xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
00431 
00432         /* Motion notify = user moved his cursor (over the root window and may
00433          * cross virtual screen boundaries doing that) */
00434         xcb_event_set_motion_notify_handler(&evenths, handle_motion_notify, NULL);
00435 
00436         /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
00437         xcb_event_set_mapping_notify_handler(&evenths, handle_mapping_notify, NULL);
00438 
00439         /* Client message are sent to the root window. The only interesting client message
00440            for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
00441         xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
00442 
00443         /* Initialize the property handlers */
00444         xcb_property_handlers_init(&prophs, &evenths);
00445 
00446         /* Watch size hints (to obey correct aspect ratio) */
00447         xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
00448 
00449         /* set event mask */
00450         uint32_t mask = XCB_CW_EVENT_MASK;
00451         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
00452                               XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
00453                                                                            projector), the root window gets a
00454                                                                            ConfigureNotify */
00455                               XCB_EVENT_MASK_POINTER_MOTION |
00456                               XCB_EVENT_MASK_PROPERTY_CHANGE |
00457                               XCB_EVENT_MASK_ENTER_WINDOW };
00458         xcb_void_cookie_t cookie;
00459         cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
00460         check_error(conn, cookie, "Another window manager seems to be running");
00461 
00462         /* Setup NetWM atoms */
00463         #define GET_ATOM(name) { \
00464                 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
00465                 if (!reply) { \
00466                         ELOG("Could not get atom " #name "\n"); \
00467                         exit(-1); \
00468                 } \
00469                 atoms[name] = reply->atom; \
00470                 free(reply); \
00471         }
00472 
00473         GET_ATOM(_NET_SUPPORTED);
00474         GET_ATOM(_NET_WM_STATE_FULLSCREEN);
00475         GET_ATOM(_NET_SUPPORTING_WM_CHECK);
00476         GET_ATOM(_NET_WM_NAME);
00477         GET_ATOM(_NET_WM_STATE);
00478         GET_ATOM(_NET_WM_WINDOW_TYPE);
00479         GET_ATOM(_NET_WM_DESKTOP);
00480         GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
00481         GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
00482         GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
00483         GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
00484         GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
00485         GET_ATOM(_NET_WM_STRUT_PARTIAL);
00486         GET_ATOM(WM_PROTOCOLS);
00487         GET_ATOM(WM_DELETE_WINDOW);
00488         GET_ATOM(UTF8_STRING);
00489         GET_ATOM(WM_STATE);
00490         GET_ATOM(WM_CLIENT_LEADER);
00491         GET_ATOM(_NET_CURRENT_DESKTOP);
00492         GET_ATOM(_NET_ACTIVE_WINDOW);
00493         GET_ATOM(_NET_WORKAREA);
00494 
00495         xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
00496         /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
00497 
00498         /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
00499         xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
00500 
00501         /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
00502         xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
00503 
00504         /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
00505         xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
00506 
00507         /* Watch WM_CLASS (= class of the window) */
00508         xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
00509 
00510         /* Watch WM_CLIENT_LEADER (= logical parent window for toolbars etc.) */
00511         xcb_property_set_handler(&prophs, atoms[WM_CLIENT_LEADER], UINT_MAX, handle_clientleader_change, NULL);
00512 
00513         /* Watch WM_HINTS (contains the urgent property) */
00514         xcb_property_set_handler(&prophs, WM_HINTS, UINT_MAX, handle_hints, NULL);
00515 
00516         /* Set up the atoms we support */
00517         check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
00518                        ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
00519         /* Set up the window manager’s name */
00520         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
00521         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
00522 
00523         keysyms = xcb_key_symbols_alloc(conn);
00524 
00525         xcb_get_numlock_mask(conn);
00526 
00527         translate_keysyms();
00528         grab_all_keys(conn, false);
00529 
00530         int randr_base = -1;
00531         if (force_xinerama) {
00532                 initialize_xinerama(conn);
00533         } else {
00534                 DLOG("Checking for XRandR...\n");
00535                 initialize_randr(conn, &randr_base);
00536 
00537                 if (randr_base != -1)
00538                     xcb_event_set_handler(&evenths,
00539                                           randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
00540                                           handle_screen_change,
00541                                           NULL);
00542         }
00543 
00544         xcb_flush(conn);
00545 
00546         /* Get pointer position to see on which screen we’re starting */
00547         xcb_query_pointer_reply_t *reply;
00548         if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
00549                 ELOG("Could not get pointer position\n");
00550                 return 1;
00551         }
00552 
00553         Output *screen = get_output_containing(reply->root_x, reply->root_y);
00554         if (screen == NULL) {
00555                 ELOG("ERROR: No screen at %d x %d, starting on the first screen\n",
00556                     reply->root_x, reply->root_y);
00557                 screen = get_first_output();
00558         }
00559 
00560         DLOG("Starting on %p\n", screen->current_workspace);
00561         c_ws = screen->current_workspace;
00562 
00563         manage_existing_windows(conn, &prophs, root);
00564 
00565         /* Create the UNIX domain socket for IPC */
00566         if (config.ipc_socket_path != NULL) {
00567                 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
00568                 if (ipc_socket == -1) {
00569                         ELOG("Could not create the IPC socket, IPC disabled\n");
00570                 } else {
00571                         struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
00572                         ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
00573                         ev_io_start(loop, ipc_io);
00574                 }
00575         }
00576 
00577         /* Handle the events which arrived until now */
00578         xcb_check_cb(NULL, NULL, 0);
00579 
00580         setup_signal_handler();
00581 
00582         /* Ignore SIGPIPE to survive errors when an IPC client disconnects
00583          * while we are sending him a message */
00584         signal(SIGPIPE, SIG_IGN);
00585 
00586         /* Ungrab the server to receive events and enter libev’s eventloop */
00587         xcb_ungrab_server(conn);
00588 
00589         /* Autostarting exec-lines */
00590         struct Autostart *exec;
00591         if (autostart) {
00592                 TAILQ_FOREACH(exec, &autostarts, autostarts) {
00593                         LOG("auto-starting %s\n", exec->command);
00594                         start_application(exec->command);
00595                 }
00596         }
00597 
00598         ev_loop(loop, 0);
00599 
00600         /* not reached */
00601         return 0;
00602 }