]> git.sur5r.net Git - i3/i3/blobdiff - src/mainx.c
Bugfix: Repeatedly try to find screens if none are available (Thanks mxf)
[i3/i3] / src / mainx.c
index 60cd6a0f863149569e56246ab6dd9e469ac9ac21..d95ca4653dcff818b0d8cb19c82d8b49db373421 100644 (file)
@@ -31,6 +31,8 @@
 #include <xcb/xcb_icccm.h>
 #include <xcb/xinerama.h>
 
+#include <ev.h>
+
 #include "config.h"
 #include "data.h"
 #include "debug.h"
@@ -69,9 +71,41 @@ xcb_atom_t atoms[NUM_ATOMS];
 
 int num_screens = 0;
 
+/*
+ * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
+ * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
+ *
+ */
+static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
+        /* empty, because xcb_prepare_cb and xcb_check_cb are used */
+}
+
+/*
+ * Flush before blocking (and waiting for new events)
+ *
+ */
+static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
+        xcb_flush(evenths.c);
+}
+
+/*
+ * Instead of polling the X connection socket we leave this to
+ * xcb_poll_for_event() which knows better than we can ever know.
+ *
+ */
+static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
+        xcb_generic_event_t *event;
+
+        while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
+                xcb_event_handle(&evenths, event);
+                free(event);
+        }
+}
+
 int main(int argc, char *argv[], char *env[]) {
         int i, screens, opt;
         char *override_configpath = NULL;
+        bool autostart = true;
         xcb_connection_t *conn;
         xcb_property_handlers_t prophs;
         xcb_window_t root;
@@ -85,8 +119,12 @@ int main(int argc, char *argv[], char *env[]) {
 
         start_argv = argv;
 
-        while ((opt = getopt(argc, argv, "c:v")) != -1) {
+        while ((opt = getopt(argc, argv, "c:va")) != -1) {
                 switch (opt) {
+                        case 'a':
+                                LOG("Autostart disabled using -a\n");
+                                autostart = false;
+                                break;
                         case 'c':
                                 override_configpath = sstrdup(optarg);
                                 break;
@@ -107,10 +145,13 @@ int main(int argc, char *argv[], char *env[]) {
         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
         memset(&prophs, 0, sizeof(xcb_property_handlers_t));
 
-        load_configuration(override_configpath);
-
         conn = xcb_connect(NULL, &screens);
 
+        if (xcb_connection_has_error(conn))
+                die("Cannot open display\n");
+
+        load_configuration(conn, override_configpath);
+
         /* Place requests for the atoms we need as soon as possible */
         #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
 
@@ -122,6 +163,10 @@ int main(int argc, char *argv[], char *env[]) {
         REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
         REQUEST_ATOM(_NET_WM_DESKTOP);
         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
+        REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
+        REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
+        REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
+        REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
         REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
         REQUEST_ATOM(WM_PROTOCOLS);
         REQUEST_ATOM(WM_DELETE_WINDOW);
@@ -148,6 +193,27 @@ int main(int argc, char *argv[], char *env[]) {
         }
         /* end of ugliness */
 
+        /* Initialize event loop using libev */
+        struct ev_loop *loop = ev_default_loop(0);
+        if (loop == NULL)
+                die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
+
+        struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
+        struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
+        struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
+
+        ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
+        ev_io_start(loop, xcb_watcher);
+
+        ev_check_init(xcb_check, xcb_check_cb);
+        ev_check_start(loop, xcb_check);
+
+        ev_prepare_init(xcb_prepare, xcb_prepare_cb);
+        ev_prepare_start(loop, xcb_prepare);
+
+        /* Grab the server to delay any events until we enter the eventloop */
+        xcb_grab_server(conn);
+
         xcb_event_handlers_init(conn, &evenths);
 
         /* DEBUG: Trap all events and print them */
@@ -225,6 +291,10 @@ int main(int argc, char *argv[], char *env[]) {
         GET_ATOM(_NET_WM_WINDOW_TYPE);
         GET_ATOM(_NET_WM_DESKTOP);
         GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
+        GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
+        GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
+        GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
+        GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
         GET_ATOM(_NET_WM_STRUT_PARTIAL);
         GET_ATOM(WM_PROTOCOLS);
         GET_ATOM(WM_DELETE_WINDOW);
@@ -237,6 +307,9 @@ int main(int argc, char *argv[], char *env[]) {
         /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
         xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
 
+        /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
+        xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
+
         /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
         xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
 
@@ -270,9 +343,11 @@ int main(int argc, char *argv[], char *env[]) {
 
         /* Autostarting exec-lines */
         struct Autostart *exec;
-        TAILQ_FOREACH(exec, &autostarts, autostarts) {
-                LOG("auto-starting %s\n", exec->command);
-                start_application(exec->command);
+        if (autostart) {
+                TAILQ_FOREACH(exec, &autostarts, autostarts) {
+                        LOG("auto-starting %s\n", exec->command);
+                        start_application(exec->command);
+                }
         }
 
         /* check for Xinerama */
@@ -300,8 +375,12 @@ int main(int argc, char *argv[], char *env[]) {
                 c_ws = &workspaces[screen->current_workspace];
         }
 
-        /* Enter xcb’s event handler */
-        xcb_event_wait_for_event_loop(&evenths);
+        /* Handle the events which arrived until now */
+        xcb_check_cb(NULL, NULL, 0);
+
+        /* Ungrab the server to receive events and enter libev’s eventloop */
+        xcb_ungrab_server(conn);
+        ev_loop(loop, 0);
 
         /* not reached */
         return 0;