]> git.sur5r.net Git - i3/i3/blobdiff - src/mainx.c
Initial implementation of IPC via UNIX domain sockets
[i3/i3] / src / mainx.c
index 60cd6a0f863149569e56246ab6dd9e469ac9ac21..684526c653059886e8675dde406970d0069281fa 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"
@@ -43,6 +45,9 @@
 #include "xcb.h"
 #include "xinerama.h"
 #include "manage.h"
+#include "ipc.h"
+
+xcb_connection_t *global_conn;
 
 /* This is the path to i3, copied from argv[0] when starting up */
 char **start_argv;
@@ -67,14 +72,49 @@ struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
 xcb_event_handlers_t evenths;
 xcb_atom_t atoms[NUM_ATOMS];
 
+xcb_window_t root;
 int num_screens = 0;
 
+/* The depth of the root screen (used e.g. for creating new pixmaps later) */
+uint8_t root_depth;
+
+/*
+ * 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;
         xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
 
         setlocale(LC_ALL, "");
@@ -85,8 +125,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,9 +151,12 @@ 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 = global_conn = xcb_connect(NULL, &screens);
 
-        conn = xcb_connect(NULL, &screens);
+        if (xcb_connection_has_error(conn))
+                die("Cannot open display\n");
+
+        load_configuration(conn, override_configpath, false);
 
         /* 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 +169,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 +199,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 */
@@ -195,7 +267,9 @@ int main(int argc, char *argv[], char *env[]) {
         xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
 
         /* Get the root window and set the event mask */
-        root = xcb_aux_get_screen(conn, screens)->root;
+        xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
+        root = root_screen->root;
+        root_depth = root_screen->root_depth;
 
         uint32_t mask = XCB_CW_EVENT_MASK;
         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
@@ -225,6 +299,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 +315,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);
 
@@ -252,27 +333,15 @@ int main(int argc, char *argv[], char *env[]) {
 
         xcb_get_numlock_mask(conn);
 
-        /* Grab the bound keys */
-        Binding *bind;
-        TAILQ_FOREACH(bind, &bindings, bindings) {
-                LOG("Grabbing %d\n", bind->keycode);
-                if (bind->mods & BIND_MODE_SWITCH)
-                        xcb_grab_key(conn, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
-                else {
-                        /* Grab the key in all combinations */
-                        #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
-                                                                XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
-                        GRAB_KEY(bind->mods);
-                        GRAB_KEY(bind->mods | xcb_numlock_mask);
-                        GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
-                }
-        }
+        grab_all_keys(conn);
 
         /* 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 +369,22 @@ 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);
+        /* Create the UNIX domain socket for IPC */
+        int ipc_socket = ipc_create_socket("/tmp/i3.s");
+        if (ipc_socket == -1) {
+                LOG("Could not create the IPC socket, IPC disabled\n");
+        } else {
+                struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
+                ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
+                ev_io_start(loop, ipc_io);
+        }
+
+        /* 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;