]> git.sur5r.net Git - i3/i3/blobdiff - src/main.c
Add force_xinerama configuration option
[i3/i3] / src / main.c
index 7910c8b1a6655c51b391e54e3a58a69ee6a16ecb..cc60d69cd8fd07a5a1cf4142d7e9510ad0e4016d 100644 (file)
@@ -3,9 +3,10 @@
  */
 #include <ev.h>
 #include <fcntl.h>
-#include <limits.h>
 #include "all.h"
 
+#include "sd-daemon.h"
+
 static int xkb_event_base;
 
 int xkb_current_group;
@@ -16,6 +17,7 @@ char **start_argv;
 
 xcb_connection_t *conn;
 
+xcb_screen_t *root_screen;
 xcb_window_t root;
 uint8_t root_depth;
 
@@ -32,6 +34,9 @@ struct bindings_head *bindings;
 /* The list of exec-lines */
 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
 
+/* The list of exec_always lines */
+struct autostarts_always_head autostarts_always = TAILQ_HEAD_INITIALIZER(autostarts_always);
+
 /* The list of assignments */
 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
 
@@ -72,7 +77,12 @@ static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
         if (event->response_type == 0) {
             if (event_is_ignored(event->sequence, 0))
                 DLOG("Expected X11 Error received for sequence %x\n", event->sequence);
-            else ELOG("X11 Error received! sequence %x\n", event->sequence);
+            else {
+                xcb_generic_error_t *error = (xcb_generic_error_t*)event;
+                ELOG("X11 Error received! sequence 0x%x, error_code = %d\n",
+                     error->sequence, error->error_code);
+            }
+            free(event);
             continue;
         }
 
@@ -153,6 +163,14 @@ static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
     DLOG("Done\n");
 }
 
+/*
+ * Exit handler which destroys the main_loop. Will trigger cleanup handlers.
+ *
+ */
+static void i3_exit() {
+    ev_loop_destroy(main_loop);
+}
+
 int main(int argc, char *argv[]) {
     //parse_cmd("[ foo ] attach, attach ; focus");
     int screens;
@@ -267,10 +285,11 @@ int main(int argc, char *argv[]) {
     if (main_loop == NULL)
             die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
 
-    xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
+    root_screen = xcb_aux_get_screen(conn, screens);
     root = root_screen->root;
     root_depth = root_screen->root_depth;
     xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
+    xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
 
     load_configuration(conn, override_configpath, false);
     if (only_check_config) {
@@ -327,6 +346,20 @@ int main(int argc, char *argv[]) {
         /*init_xkb();*/
     }
 
+    /* Set a cursor for the root window (otherwise the root window will show no
+       cursor until the first client is launched). */
+    if (xcursor_supported) {
+        xcursor_set_root_cursor();
+    } else {
+        xcb_cursor_t cursor_id = xcb_generate_id(conn);
+        i3Font cursor_font = load_font("cursor", false);
+        int xcb_cursor = xcursor_get_xcb_cursor(XCURSOR_CURSOR_POINTER);
+        xcb_create_glyph_cursor(conn, cursor_id, cursor_font.id, cursor_font.id,
+                xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
+        xcb_change_window_attributes(conn, root, XCB_CW_CURSOR, &cursor_id);
+        xcb_free_cursor(conn, cursor_id);
+    }
+
     if (xkb_supported) {
         int errBase,
             major = XkbMajorVersion,
@@ -374,9 +407,9 @@ int main(int argc, char *argv[]) {
 #include "atoms.xmacro"
 #undef xmacro
     };
-    xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, A_ATOM, 32, 7, supported_atoms);
+    xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, XCB_ATOM_ATOM, 32, 16, supported_atoms);
     /* Set up the window manager’s name */
-    xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, A_WINDOW, 32, 1, &root);
+    xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &root);
     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
 
     keysyms = xcb_key_symbols_alloc(conn);
@@ -399,13 +432,33 @@ int main(int argc, char *argv[]) {
 
     free(greply);
 
-    if (force_xinerama) {
+    /* Force Xinerama (for drivers which don't support RandR yet, esp. the
+     * nVidia binary graphics driver), when specified either in the config
+     * file or on command-line */
+    if (force_xinerama || config.force_xinerama) {
         xinerama_init();
     } else {
         DLOG("Checking for XRandR...\n");
         randr_init(&randr_base);
     }
 
+    xcb_query_pointer_reply_t *pointerreply;
+    Output *output = NULL;
+    if (!(pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL))) {
+        ELOG("Could not query pointer position, using first screen\n");
+        output = get_first_output();
+    } else {
+        DLOG("Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
+        output = get_output_containing(pointerreply->root_x, pointerreply->root_y);
+        if (!output) {
+            ELOG("ERROR: No screen at (%d, %d), starting on the first screen\n",
+                 pointerreply->root_x, pointerreply->root_y);
+            output = get_first_output();
+        }
+
+        con_focus(con_descend_focused(output_get_content(output->con)));
+    }
+
     tree_render();
 
     /* Create the UNIX domain socket for IPC */
@@ -419,6 +472,21 @@ int main(int argc, char *argv[]) {
         ev_io_start(main_loop, ipc_io);
     }
 
+    /* Also handle the UNIX domain sockets passed via socket activation */
+    int fds = sd_listen_fds(1);
+    if (fds < 0)
+        ELOG("socket activation: Error in sd_listen_fds\n");
+    else if (fds == 0)
+        DLOG("socket activation: no sockets passed\n");
+    else {
+        for (int fd = SD_LISTEN_FDS_START; fd < (SD_LISTEN_FDS_START + fds); fd++) {
+            DLOG("socket activation: also listening on fd %d\n", fd);
+            struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
+            ev_io_init(ipc_io, ipc_new_client, fd, EV_READ);
+            ev_io_start(main_loop, ipc_io);
+        }
+    }
+
     /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
     x_set_i3_atoms();
 
@@ -465,5 +533,16 @@ int main(int argc, char *argv[]) {
         }
     }
 
+    /* Autostarting exec_always-lines */
+    struct Autostart *exec_always;
+    TAILQ_FOREACH(exec_always, &autostarts_always, autostarts_always) {
+        LOG("auto-starting (always!) %s\n", exec_always->command);
+        start_application(exec_always->command);
+    }
+
+    /* Make sure to destroy the event loop to invoke the cleeanup callbacks
+     * when calling exit() */
+    atexit(i3_exit);
+
     ev_loop(main_loop, 0);
 }