use select to tell when there are events to read
[dana/dcompmgr.git] / dcompmgr.c
index 84d6a78..f72bf57 100644 (file)
+#include "screen.h"
+#include "window.h"
+#include "display.h"
+#include "gettext.h"
+
 #include <glib.h>
 #include <stdio.h>
+#include <sys/select.h>
+#include <stdlib.h>
 #include <string.h>
 #include <xcb/xcb.h>
 
-#define _(a) (a)
-
-/* inherits from xcb_screen_t */
 typedef struct {
-    xcb_screen_t super;
-    int          num;
-    xcb_window_t selwin; /* for the selection */
-} d_screen_t;
+    int foo;
+} d_options_t;
 
-static xcb_connection_t  *conn;
-static const xcb_setup_t *setup;
-static d_screen_t        *screens = NULL;
+static void
+read_options(int argc, char **argv, d_options_t *opts)
+{
+    opts->foo = argc && argv;
+}
 
-static gboolean
-register_screen (d_screen_t *sc)
+static void
+event(d_display_t *dpy)
 {
-    char *name;
-    int len, s;
-    xcb_window_t w;
-    xcb_intern_atom_cookie_t ack;
-    xcb_intern_atom_reply_t *arep;
-    xcb_get_selection_owner_cookie_t sck;
-    xcb_get_selection_owner_reply_t *srep;
-    gboolean taken;
-
-    w = xcb_generate_id(conn);
-    xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
-                      0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
-                      sc->super.root_visual, 0, NULL);
-
-    name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
-    ack = xcb_intern_atom(conn, FALSE, strlen(name), name);
-    arep = xcb_intern_atom_reply(conn, ack, NULL);
-    g_free(name);
-
-    sck = xcb_get_selection_owner(conn, arep->atom);
-    srep = xcb_get_selection_owner_reply(conn, sck, NULL);
-    taken = !!srep->owner;
-    free(srep);
-    if (taken) {
-        printf(_("screen %d already has a composite manager, skipping\n"),
-               sc->num);
-        return FALSE;
+    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;
 
-    xcb_set_selection_owner(conn, w, arep->atom, XCB_CURRENT_TIME);
-    sck = xcb_get_selection_owner(conn, arep->atom);
-    srep = xcb_get_selection_owner_reply(conn, sck, NULL);
-    taken = srep->owner == w;
-    if (taken) {
-        sc->selwin = w;
-        return TRUE;
+        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;
     }
-    else {
-        xcb_destroy_window(conn, w);
-        return FALSE;
+    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 gint
-all_screens()
+static void
+run(d_display_t *dpy)
 {
-    xcb_screen_iterator_t it;
-    int count, i;
-    d_screen_t sc;
-
-    count = i = 0;
-    for (it = xcb_setup_roots_iterator(setup); it.rem; xcb_screen_next(&it)) {
-        sc.super = *it.data;
-        sc.num = i++;
-        if (register_screen(&sc)) {
-            ++count;
-            screens = g_renew(d_screen_t, screens, count);
-            screens[count-1] = sc;
-            printf(_("managing screen %d\n"), sc.num);
+    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;
     }
-    return count;
 }
 
 int
 main(int argc, char **argv)
 {
-    conn = xcb_connect(NULL, NULL);
-    if (!conn) {
+    d_display_t         *dpy;
+    d_options_t          opts;
+
+    read_options(argc, argv, &opts);
+
+    dpy = display_open(NULL);
+    if (!dpy) {
         printf(_("Unable to connect to display\n"));
         return 1;
     }
-    setup = xcb_get_setup(conn);
 
-    all_screens();
-    
+    if (!dpy->composite.present) {
+        printf(_("no composite extension present on the display\n"));
+        display_unref(dpy);
+        return 1;
+    }
+    if (!dpy->xfixes.present) {
+        printf(_("no xfixes extension present on the display\n"));
+        display_unref(dpy);
+        return 1;
+    }
+    if (!dpy->damage.present) {
+        printf(_("no damage extension present on the display\n"));
+        display_unref(dpy);
+        return 1;
+    }
+    if (!dpy->render.present) {
+        printf(_("no render extension present on the display\n"));
+        display_unref(dpy);
+        return 1;
+    }
+    if (dpy->composite.major_version <= 0 && dpy->composite.minor_version < 3)
+    {
+        printf(_("composite extension does not support the overlay window"));
+        display_unref(dpy);
+        return 1;
+    }
+
+    if (!display_claim_screens(dpy)) {
+        printf(_("found no screens to run on\n"));
+        display_unref(dpy);
+        return 0;
+    }
+
+    run(dpy);
 
-    xcb_disconnect(conn);
+    display_unref(dpy);
     return 0;
 }