]> git.sur5r.net Git - i3/i3/blobdiff - src/mainx.c
Initial implementation of IPC via UNIX domain sockets
[i3/i3] / src / mainx.c
index ca1bc8ad7ce8bf042786042be80e0c5abc1ca1b4..684526c653059886e8675dde406970d0069281fa 100644 (file)
@@ -45,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;
@@ -69,34 +72,41 @@ 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;
+
 /*
- * Callback for activity on the connection to the X server
+ * 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;
 
-        /* When an event is available… */
         while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
-                /* …we handle all events in a row: */
-                do {
-                        xcb_event_handle(&evenths, event);
-                        xcb_aux_sync(evenths.c);
-                        free(event);
-                } while ((event = xcb_poll_for_event(evenths.c)));
-
-                /* Make sure all replies are handled/discarded */
-                xcb_aux_sync(evenths.c);
-
-                /* Afterwards, there may be new events available which would
-                 * not trigger the select() (libev) immediately, so we check
-                 * again (and don’t bail out of the loop). */
+                xcb_event_handle(&evenths, event);
+                free(event);
         }
-
-        /* Make sure all replies are handled/discarded */
-        xcb_aux_sync(evenths.c);
 }
 
 int main(int argc, char *argv[], char *env[]) {
@@ -105,7 +115,6 @@ int main(int argc, char *argv[], char *env[]) {
         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, "");
@@ -142,12 +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));
 
-        conn = xcb_connect(NULL, &screens);
+        conn = global_conn = xcb_connect(NULL, &screens);
 
         if (xcb_connection_has_error(conn))
                 die("Cannot open display\n");
 
-        load_configuration(conn, override_configpath);
+        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);
@@ -190,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 */
@@ -237,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 |
@@ -301,21 +333,7 @@ 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;
@@ -351,20 +369,21 @@ int main(int argc, char *argv[], char *env[]) {
                 c_ws = &workspaces[screen->current_workspace];
         }
 
+        /* 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);
+        }
 
-        /* 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));
-        ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
-
-        /* Call the handler to work all events which arrived before the libev-stuff was set up */
-        xcb_got_event(NULL, xcb_watcher, 0);
+        /* Handle the events which arrived until now */
+        xcb_check_cb(NULL, NULL, 0);
 
-        /* Enter the libev eventloop */
-        ev_io_start(loop, xcb_watcher);
+        /* Ungrab the server to receive events and enter libev’s eventloop */
+        xcb_ungrab_server(conn);
         ev_loop(loop, 0);
 
         /* not reached */