remove a debug print, and make it wait 0 instead of 1 microsecond when it needs to...
[dana/dcompmgr.git] / dcompmgr.c
index 9126010..c20025a 100644 (file)
@@ -1,74 +1,38 @@
 #include "screen.h"
 #include "window.h"
+#include "list.h"
+#include "display.h"
 #include "gettext.h"
+#include "time.h"
+#include "render.h"
 
 #include <glib.h>
 #include <stdio.h>
+#include <sys/select.h>
 #include <stdlib.h>
 #include <string.h>
 #include <xcb/xcb.h>
 
-static gint
-all_screens(xcb_connection_t *conn, d_screen_t **list)
-{
-    static const xcb_setup_t *setup;
-    xcb_screen_iterator_t it;
-    int count, i;
-    d_screen_t sc;
-
-    setup = xcb_get_setup(conn);
-
-    count = i = 0;
-    for (it = xcb_setup_roots_iterator(setup); it.rem; xcb_screen_next(&it)) {
-        sc.super = *it.data;
-        sc.conn = conn;
-        sc.num = i++;
-        if (screen_register(&sc)) {
-            ++count;
-            *list = g_renew(d_screen_t, *list, count);
-            (*list)[count-1] = sc;
-            printf(_("managing screen %d\n"), sc.num);
-        }
-    }
-    return count;
-}
+typedef struct {
+    int foo;
+} d_options_t;
 
-d_screen_t*
-screen_from_root(d_screen_t *list, int n, xcb_window_t root)
+static void
+read_options(int argc, char **argv, d_options_t *opts)
 {
-    int i;
-    for (i = 0; i < n; ++i)
-        if (list->super.root == root) return &list[i];
-    g_assert_not_reached();
-    return NULL;
+    opts->foo = argc && argv;
 }
 
-int
-main(int argc, char **argv)
+static void
+event(d_display_t *dpy)
 {
-    xcb_connection_t    *conn;
-    d_screen_t          *screens = NULL;
-    int                  nscreens;
     xcb_generic_event_t *ev;
 
-    conn = xcb_connect(NULL, NULL);
-    if (!conn) {
-        printf(_("Unable to connect to display\n"));
-        return 1;
-    }
-
-    nscreens = all_screens(conn, &screens);
-    if (nscreens < 1) {
-        printf(_("found no screens to run on\n"));
-        xcb_disconnect(conn);
-        return 0;
-    }
-
-    while ((ev = xcb_wait_for_event(conn))) {
+    while ((ev = xcb_poll_for_event(dpy->conn))) {
         printf("event %d\n", ev->response_type);
 
         if (!ev->response_type) {
-            /* XXX handle error */
+            display_error(dpy, (xcb_generic_error_t*)ev);
             free(ev);
             continue;
         }
@@ -80,7 +44,7 @@ main(int argc, char **argv)
             d_screen_t *sc;
 
             cev = (xcb_create_notify_event_t*)ev;
-            sc = screen_from_root(screens, nscreens, cev->parent);
+            sc = display_screen_from_root(dpy, cev->parent);
             if (!sc) break;
             screen_add_window(sc, cev->window);
             break;
@@ -92,11 +56,13 @@ main(int argc, char **argv)
             d_window_t *w;
 
             dev = (xcb_destroy_notify_event_t*)ev;
-            sc = screen_from_root(screens, nscreens, dev->event);
+            sc = display_screen_from_root(dpy, dev->event);
             if (!sc) break;
             w = screen_find_window(sc, dev->window);
-            w->hide(w);
+            sc->window_hide(w);
             screen_remove_window(sc, w);
+            printf("** refresh needed **\n");
+            screen_refresh(sc);
             break;
         }
         case XCB_REPARENT_NOTIFY:
@@ -106,23 +72,212 @@ main(int argc, char **argv)
             d_window_t *w;
 
             rev = (xcb_reparent_notify_event_t*)ev;
-            sc = screen_from_root(screens, nscreens, rev->event);
+            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);
+                sc->window_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);
+            sc->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 = display_screen_from_root(dpy, mev->event);
+            if (!sc) break;
+            w = screen_find_window(sc, mev->window);
+            sc->window_hide(w);
+            break;
+        }
         default:
             break;
         }
         free(ev);
     }
+}
+
+static void
+paint(d_display_t *dpy)
+{
+    d_list_it_t *it;
+    struct timeval now;
+
+    gettimeofday(&now, NULL);
+    
+    for (it = list_top(dpy->screens); it; it = it->next) {
+        d_screen_t *sc = it->data;
+
+        if (time_compare(&sc->next_repaint, &now) <= 0)
+            sc->screen_paint(sc);
+    }
+}
+
+static void
+run(d_display_t *dpy)
+{
+    gboolean quit;
+
+    paint(dpy);
+
+    quit = FALSE;
+    while (!quit) {
+        struct timeval next, now, *wait;
+        int            r, npaint;
+        d_list_it_t   *it;
+        fd_set         fds;
+
+        npaint = 0;
+        for (it = list_top(dpy->screens); it; it = it->next) {
+            d_screen_t *sc = it->data;
+            if (sc->need_repaint &&
+                (!npaint || time_compare(&sc->next_repaint, &next) < 0))
+            {
+                next = sc->next_repaint;
+                ++npaint;
+            }
+        }
+
+        gettimeofday(&now, 0);
+
+        if (!npaint)
+            /* wait forever, there is nothing that needs drawing */
+            wait = NULL;
+        else if (time_compare(&next, &now) > 0) {
+            /* wait until the next allowed redraw time */
+            time_difference(&next, &now, &next);
+            wait = &next;
+        }
+        else {
+            /* don't wait cuz a redraw is due now already */
+            next.tv_sec = 0;
+            next.tv_usec = 0;
+            wait = &next;
+        }
+
+        FD_ZERO(&fds);
+        FD_SET(dpy->fd, &fds);
+
+        r = select(dpy->fd+1, &fds, NULL, NULL, wait);
+        if (r < 0)
+            printf("select error\n");
+        else if (r == 0) {
+            printf("select timeout\n");
+            paint(dpy);
+        }
+        else {
+            printf("select data\n");
+            /*if (FD_ISSET(dpy->fd, &fds))*/ {
+                event(dpy);
+            }
+        }
+
+        if (xcb_connection_has_error(dpy->conn))
+            quit = TRUE;
+        else
+            xcb_flush(dpy->conn);
+    }
+}
+
+static void
+setup_functions(d_display_t *dpy)
+{
+    d_list_it_t *it;
+
+    for (it = list_top(dpy->screens); it; it = it->next) {
+        d_screen_t *sc = it->data;
+        screen_setup_default_functions(sc);
+
+        /* these can be plugins.. */
+        render_init(sc);
+    }
+}
+
+static void
+cleanup_functions(d_display_t *dpy)
+{
+    d_list_it_t *it;
+
+    for (it = list_top(dpy->screens); it; it = it->next) {
+        d_screen_t *sc = it->data;
+
+        /* these can be plugins.. */
+        render_free(sc);
+    }
+}
+
+int
+main(int argc, char **argv)
+{
+    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;
+    }
+
+    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;
+    }
+
+    setup_functions(dpy);
+
+    run(dpy);
+
+    cleanup_functions(dpy);
 
-    xcb_disconnect(conn);
+    display_unref(dpy);
     return 0;
 }