From 553db28664719fbbfd1cc4a2dafabf6b2da5b1da Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 1 Jun 2009 20:59:40 +0200 Subject: [PATCH] Switch to libev for the event loop to build a base for IPC stuff. Please test! --- Makefile | 1 + debian/control | 2 +- src/mainx.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 9f82b1a9..a42099d4 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ LDFLAGS += -lxcb-aux LDFLAGS += -lxcb-icccm LDFLAGS += -lxcb-xinerama LDFLAGS += -lX11 +LDFLAGS += -lev LDFLAGS += -L/usr/local/lib -L/usr/pkg/lib ifeq ($(UNAME),NetBSD) diff --git a/debian/control b/debian/control index e0807987..63997e86 100644 --- a/debian/control +++ b/debian/control @@ -3,7 +3,7 @@ Section: utils Priority: optional Maintainer: Michael Stapelberg DM-Upload-Allowed: yes -Build-Depends: debhelper (>= 5), libx11-dev, libxcb-aux0-dev (>= 0.3.3), libxcb-keysyms1-dev, libxcb-xinerama0-dev (>= 1.1), libxcb-event1-dev (>= 0.3.3), libxcb-property1-dev (>= 0.3.3), libxcb-atom1-dev (>= 0.3.3), libxcb-icccm1-dev (>= 0.3.3), asciidoc (>= 8.4.4-1), xmlto, docbook-xml, pkg-config +Build-Depends: debhelper (>= 5), libx11-dev, libxcb-aux0-dev (>= 0.3.3), libxcb-keysyms1-dev, libxcb-xinerama0-dev (>= 1.1), libxcb-event1-dev (>= 0.3.3), libxcb-property1-dev (>= 0.3.3), libxcb-atom1-dev (>= 0.3.3), libxcb-icccm1-dev (>= 0.3.3), asciidoc (>= 8.4.4-1), xmlto, docbook-xml, pkg-config, libev-dev Standards-Version: 3.8.0 Homepage: http://i3.zekjur.net/ diff --git a/src/mainx.c b/src/mainx.c index 79a21e14..5de87700 100644 --- a/src/mainx.c +++ b/src/mainx.c @@ -31,6 +31,8 @@ #include #include +#include + #include "config.h" #include "data.h" #include "debug.h" @@ -69,6 +71,34 @@ xcb_atom_t atoms[NUM_ATOMS]; int num_screens = 0; +/* + * Callback for activity on the connection to the X server + * + */ +static void xcb_got_event(EV_P_ struct ev_io *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). */ + } + + /* Make sure all replies are handled/discarded */ + xcb_aux_sync(evenths.c); +} + int main(int argc, char *argv[], char *env[]) { int i, screens, opt; char *override_configpath = NULL; @@ -307,8 +337,21 @@ 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); + + /* 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"); + + ev_io xcb_watcher; + 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); + + /* Enter the libev eventloop */ + ev_io_start(loop, &xcb_watcher); + ev_loop(loop, 0); /* not reached */ return 0; -- 2.39.5