use select to tell when there are events to read
[dana/dcompmgr.git] / dcompmgr.c
index 6b8417a..f72bf57 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <glib.h>
 #include <stdio.h>
+#include <sys/select.h>
 #include <stdlib.h>
 #include <string.h>
 #include <xcb/xcb.h>
@@ -13,17 +14,138 @@ typedef struct {
     int foo;
 } d_options_t;
 
-static
-void read_options(int argc, char **argv, d_options_t *opts)
+static void
+read_options(int argc, char **argv, d_options_t *opts)
 {
     opts->foo = argc && argv;
 }
 
+static void
+event(d_display_t *dpy)
+{
+    xcb_generic_event_t *ev;
+
+    ev = xcb_poll_for_event(dpy->conn);
+    printf("event %d\n", ev->response_type);
+
+    if (!ev->response_type) {
+        display_error(dpy, (xcb_generic_error_t*)ev);
+        free(ev);
+        return;
+    }
+
+    switch (ev->response_type) {
+    case XCB_CREATE_NOTIFY:
+    {
+        xcb_create_notify_event_t *cev;
+        d_screen_t *sc;
+
+        cev = (xcb_create_notify_event_t*)ev;
+        sc = display_screen_from_root(dpy, cev->parent);
+        if (!sc) break;
+        screen_add_window(sc, cev->window);
+        break;
+    }
+    case XCB_DESTROY_NOTIFY:
+    {
+        xcb_destroy_notify_event_t *dev;
+        d_screen_t *sc;
+        d_window_t *w;
+
+        dev = (xcb_destroy_notify_event_t*)ev;
+        sc = display_screen_from_root(dpy, dev->event);
+        if (!sc) break;
+        w = screen_find_window(sc, dev->window);
+        w->hide(w);
+        screen_remove_window(sc, w);
+        break;
+    }
+    case XCB_REPARENT_NOTIFY:
+    {
+        xcb_reparent_notify_event_t *rev;
+        d_screen_t *sc;
+        d_window_t *w;
+
+        rev = (xcb_reparent_notify_event_t*)ev;
+        sc = display_screen_from_root(dpy, rev->event);
+        if (!sc) break;
+        w = screen_find_window(sc, rev->window);
+        if (rev->parent == sc->super.root)
+            screen_add_window(sc, rev->window);
+        else {
+            w->hide(w);
+            screen_remove_window(sc, w);
+        }
+        break;
+    }
+    case XCB_MAP_NOTIFY:
+    {
+        xcb_map_notify_event_t *mev;
+        d_screen_t *sc;
+        d_window_t *w;
+
+        mev = (xcb_map_notify_event_t*)ev;
+        sc = display_screen_from_root(dpy, mev->event);
+        if (!sc) break;
+        w = screen_find_window(sc, mev->window);
+        w->show(w);
+        break;
+    }
+    case XCB_UNMAP_NOTIFY:
+    {
+        xcb_unmap_notify_event_t *mev;
+        d_screen_t *sc;
+        d_window_t *w;
+
+        mev = (xcb_unmap_notify_event_t*)ev;
+        sc = display_screen_from_root(dpy, mev->event);
+        if (!sc) break;
+        w = screen_find_window(sc, mev->window);
+        w->hide(w);
+        break;
+    }
+    default:
+        break;
+    }
+    free(ev);
+}
+
+static void
+run(d_display_t *dpy)
+{
+    gboolean quit;
+    fd_set   fds;
+    int      max = -1;
+
+    FD_ZERO(&fds);
+    FD_SET(dpy->fd, &fds);
+    max = MAX(max, dpy->fd);
+
+    quit = FALSE;
+    while (!quit) {
+        int r;
+
+        r = select(max+1, &fds, NULL, NULL, NULL);
+        if (r < 0)
+            printf("select error\n");
+        else if (r == 0)
+            printf("select timeout\n");
+        else {
+            printf("select data\n");
+            /*if (FD_ISSET(dpy->fd, &fds))*/ {
+                event(dpy);
+            }
+        }
+
+        if (xcb_connection_has_error(dpy->conn))
+            quit = TRUE;
+    }
+}
+
 int
 main(int argc, char **argv)
 {
     d_display_t         *dpy;
-    xcb_generic_event_t *ev;
     d_options_t          opts;
 
     read_options(argc, argv, &opts);
@@ -67,90 +189,7 @@ main(int argc, char **argv)
         return 0;
     }
 
-    while ((ev = xcb_wait_for_event(dpy->conn))) {
-        printf("event %d\n", ev->response_type);
-
-        if (!ev->response_type) {
-            display_error(dpy, (xcb_generic_error_t*)ev);
-            free(ev);
-            continue;
-        }
-
-        switch (ev->response_type) {
-        case XCB_CREATE_NOTIFY:
-        {
-            xcb_create_notify_event_t *cev;
-            d_screen_t *sc;
-
-            cev = (xcb_create_notify_event_t*)ev;
-            sc = display_screen_from_root(dpy, cev->parent);
-            if (!sc) break;
-            screen_add_window(sc, cev->window);
-            break;
-        }
-        case XCB_DESTROY_NOTIFY:
-        {
-            xcb_destroy_notify_event_t *dev;
-            d_screen_t *sc;
-            d_window_t *w;
-
-            dev = (xcb_destroy_notify_event_t*)ev;
-            sc = display_screen_from_root(dpy, dev->event);
-            if (!sc) break;
-            w = screen_find_window(sc, dev->window);
-            w->hide(w);
-            screen_remove_window(sc, w);
-            break;
-        }
-        case XCB_REPARENT_NOTIFY:
-        {
-            xcb_reparent_notify_event_t *rev;
-            d_screen_t *sc;
-            d_window_t *w;
-
-            rev = (xcb_reparent_notify_event_t*)ev;
-            sc = display_screen_from_root(dpy, rev->event);
-            if (!sc) break;
-            w = screen_find_window(sc, rev->window);
-            if (rev->parent == sc->super.root)
-                screen_add_window(sc, rev->window);
-            else {
-                w->hide(w);
-                screen_remove_window(sc, w);
-            }
-            break;
-        }
-        case XCB_MAP_NOTIFY:
-        {
-            xcb_map_notify_event_t *mev;
-            d_screen_t *sc;
-            d_window_t *w;
-
-            mev = (xcb_map_notify_event_t*)ev;
-            sc = display_screen_from_root(dpy, mev->event);
-            if (!sc) break;
-            w = screen_find_window(sc, mev->window);
-            w->show(w);
-            break;
-        }
-        case XCB_UNMAP_NOTIFY:
-        {
-            xcb_unmap_notify_event_t *mev;
-            d_screen_t *sc;
-            d_window_t *w;
-
-            mev = (xcb_unmap_notify_event_t*)ev;
-            sc = display_screen_from_root(dpy, mev->event);
-            if (!sc) break;
-            w = screen_find_window(sc, mev->window);
-            w->hide(w);
-            break;
-        }
-        default:
-            break;
-        }
-        free(ev);
-    }
+    run(dpy);
 
     display_unref(dpy);
     return 0;