use select to tell when there are events to read
[dana/dcompmgr.git] / dcompmgr.c
index 542f1ad..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,54 +14,138 @@ typedef struct {
     int foo;
 } d_options_t;
 
-static gint
-all_screens(d_display_t *dpy, d_screen_t **list)
+static void
+read_options(int argc, char **argv, d_options_t *opts)
 {
-    static const xcb_setup_t *setup;
-    xcb_screen_iterator_t it;
-    int count, i;
-    d_screen_t sc;
-
-    setup = xcb_get_setup(dpy->conn);
-
-    count = i = 0;
-    for (it = xcb_setup_roots_iterator(setup); it.rem; xcb_screen_next(&it)) {
-        sc.super = *it.data;
-        sc.dpy = dpy;
-        sc.num = i++;
-        if (screen_register(&sc)) {
-            ++count;
-            *list = realloc(*list, sizeof(d_screen_t)*count);
-            (*list)[count-1] = sc;
-            printf(_("managing screen %d\n"), sc.num);
-        }
-    }
-    return count;
+    opts->foo = argc && argv;
 }
 
-static d_screen_t*
-screen_from_root(d_screen_t *list, int n, xcb_window_t root)
+static void
+event(d_display_t *dpy)
 {
-    int i;
-    for (i = 0; i < n; ++i)
-        if (list->super.root == root) return &list[i];
-    g_assert_not_reached();
-    return NULL;
+    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 read_options(int argc, char **argv, d_options_t *opts)
+static void
+run(d_display_t *dpy)
 {
-    opts->foo = argc && argv;
+    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;
-    d_screen_t          *screens = NULL;
-    int                  nscreens;
-    xcb_generic_event_t *ev;
     d_options_t          opts;
 
     read_options(argc, argv, &opts);
@@ -98,97 +183,13 @@ main(int argc, char **argv)
         return 1;
     }
 
-    nscreens = all_screens(dpy, &screens);
-    if (nscreens < 1) {
+    if (!display_claim_screens(dpy)) {
         printf(_("found no screens to run on\n"));
         display_unref(dpy);
         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 = screen_from_root(screens, nscreens, 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 = screen_from_root(screens, nscreens, 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 = screen_from_root(screens, nscreens, 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 = screen_from_root(screens, nscreens, mev->event);
-            if (!sc) break;
-            w = screen_find_window(sc, mev->window);
-            window_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 = screen_from_root(screens, nscreens, mev->event);
-            if (!sc) break;
-            w = screen_find_window(sc, mev->window);
-            window_hide(w);
-            break;
-        }
-        default:
-            break;
-        }
-        free(ev);
-    }
+    run(dpy);
 
     display_unref(dpy);
     return 0;