initialize gl on the overlay window
[dana/dcompmgr.git] / dcompmgr.c
index 84d6a78..47c6a1d 100644 (file)
+#include "efence.h"
+
+#include "screen.h"
+#include "window.h"
+#include "list.h"
+#include "display.h"
+#include "gettext.h"
+#include "time.h"
+
+/* these can be plugins */
+#include "render.h"
+#include "glxrender.h"
+#include "fade.h"
+
 #include <glib.h>
 #include <stdio.h>
+#include <sys/select.h>
+#include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 #include <xcb/xcb.h>
+#include <xcb/damage.h>
+#include <xcb/shape.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 gboolean quit = FALSE;
+static gboolean running = FALSE;
+
+static void
+read_options(int argc, char **argv, d_options_t *opts)
+{
+    opts->foo = argc && argv;
+}
 
-static xcb_connection_t  *conn;
-static const xcb_setup_t *setup;
-static d_screen_t        *screens = NULL;
+static void
+signal_quit_handler(int sig)
+{
+    printf("caught signal %d, quitting\n", sig);
+    quit = TRUE;
+}
 
-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;
+
+    while ((ev = xcb_poll_for_event(dpy->conn)) && !quit) {
+        //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;
+            d_window_t *w;
+
+            cev = (xcb_create_notify_event_t*)ev;
+            sc = display_screen_from_root(dpy, cev->parent);
+            if (!sc) break;
+            w = 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;
+            gboolean vis;
+
+            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);
+            vis = window_is_mapped(w);
+            if (vis) {
+                sc->window_become_zombie(w);
+                sc->window_hide(w);
+            }
+            screen_remove_window(sc, w);
+            if (vis) screen_refresh(sc);
+            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 {
+                if (window_is_mapped(w)) {
+                    sc->window_become_zombie(w);
+                    sc->window_hide(w);
+                }
+                screen_remove_window(sc, w);
+            }
+            screen_refresh(sc);
+            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);
+            screen_refresh(w->sc);
+            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_become_zombie(w);
+            sc->window_hide(w);
+            screen_refresh(w->sc);
+            break;
+        }
+        case XCB_CIRCULATE_NOTIFY:
+        {
+            xcb_circulate_notify_event_t *cev;
+            d_screen_t *sc;
+            d_window_t *w;
+
+            cev = (xcb_circulate_notify_event_t*)ev;
+            sc = display_screen_from_root(dpy, cev->event);
+            if (!sc) break;
+            w = screen_find_window(sc, cev->window);
+            if (cev->place == XCB_PLACE_ON_TOP)
+                screen_stacking_move_to_top(sc, w);
+            else
+                screen_stacking_move_to_bottom(sc, w);
+            screen_refresh(w->sc);
+        }
+        case XCB_CONFIGURE_NOTIFY:
+        {
+            xcb_configure_notify_event_t *cev;
+            d_screen_t *sc;
+            d_window_t *w, *above;
+            int x, y, width, height, bwidth;
+
+            cev = (xcb_configure_notify_event_t*)ev;
+            sc = display_screen_from_root(dpy, cev->event);
+            if (!sc) break;
+            //printf("configure 0x%x", cev->window);
+            w = screen_find_window(sc, cev->window);
+            window_get_area(w, &x, &y, &width, &height, &bwidth);
+            if (x != cev->x || y != cev->y || width != cev->width ||
+                height != cev->height || bwidth != cev->border_width)
+            {
+                window_configure(w, cev->x, cev->y,
+                                 cev->width, cev->height,
+                                 cev->border_width);
+                if (window_is_mapped(w)) {
+                    if (x != cev->x || y != cev->y)
+                        sc->window_move(w);
+                    if (width != cev->width ||
+                        height != cev->height || bwidth != cev->border_width)
+                        sc->window_resize(w);
+                }
+            }
+            above = screen_find_window(sc, cev->above_sibling);
+            if (window_is_mapped(w))
+                sc->window_restack(w, above);
+            screen_refresh(w->sc);
+            break;
+        }
+        case XCB_PROPERTY_NOTIFY:
+        {
+            xcb_property_notify_event_t *pev;
+
+            pev = (xcb_property_notify_event_t*)ev;
+            if (pev->atom == dpy->a.xrootpmap_id ||
+                pev->atom == dpy->a.esetroot_pmap_id ||
+                pev->atom == dpy->a.xsetroot_id)
+            {
+                d_screen_t *sc;
+
+                sc = display_screen_from_root(dpy, pev->window);
+                if (sc) sc->screen_root_pixmap_change(sc);
+            }
+            else if (pev->atom == dpy->a.net_wm_window_opacity) {
+                d_list_it_t *it;
+
+                for (it = list_top(dpy->screens); it; it = it->next) {
+                    d_screen_t *sc = it->data;
+                    d_window_t *w;
+
+                    w = screen_find_window(sc, pev->window);
+                    if (w) {
+                        window_update_user_opacity(w);
+                        if (window_is_mapped(w)) {
+                            sc->window_opacity_change(w);
+                            screen_refresh(w->sc);
+                        }
+                    }
+                }
+                    
+            }
+            break;
+        }
+        default:
+            if (ev->response_type - dpy->damage.event == XCB_DAMAGE_NOTIFY) {
+                xcb_damage_notify_event_t *dev;
+                d_list_it_t *it;
+
+                dev = (xcb_damage_notify_event_t*)ev;
+                for (it = list_top(dpy->screens); it; it = it->next) {
+                    d_screen_t *sc = it->data;
+                    d_window_t *w;
+
+                    w = screen_find_window(sc, dev->drawable);
+                    if (w) {
+                        sc->window_damage(w);
+                        screen_refresh(w->sc);
+                        break;
+                    }
+                }
+                xcb_damage_subtract(dpy->conn, dev->damage,
+                                    XCB_NONE, XCB_NONE);
+            }
+            else if (dpy->shape.present &&
+                     ev->response_type - dpy->shape.event == XCB_SHAPE_NOTIFY)
+            {
+                xcb_shape_notify_event_t *sev;
+                d_list_it_t *it;
+
+                sev = (xcb_shape_notify_event_t*)ev;
+                for (it = list_top(dpy->screens); it; it = it->next) {
+                    d_screen_t *sc = it->data;
+                    d_window_t *w;
+
+                    w = screen_find_window(sc, sev->affected_window);
+                    if (w && window_is_mapped(w)) {
+                        sc->window_reshape(w);
+                        screen_refresh(w->sc);
+                        break;
+                    }
+                }
+            }
+            break;
+        }
+        free(ev);
+        xcb_flush(dpy->conn);
     }
+}
+
+static void
+timeouts(d_display_t *dpy, const struct timeval *now)
+{
+    d_list_it_t *it;
 
-    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;
+    for (it = list_top(dpy->screens); it; it = it->next) {
+        d_screen_t *sc = it->data;
+        struct timeval tv;
+
+        if (glxrender_next_timeout(sc, &tv) && time_compare(&tv, now) <= 0)
+            glxrender_timeout(sc, now);
+        if (render_next_timeout(sc, &tv) && time_compare(&tv, now) <= 0)
+            render_timeout(sc, now);
+        if (fade_next_timeout(sc, &tv) && time_compare(&tv, now) <= 0)
+            fade_timeout(sc, now);
     }
-    else {
-        xcb_destroy_window(conn, w);
-        return FALSE;
+}
+
+static void
+paint(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;
+        if (sc->need_repaint)
+            sc->screen_paint(sc);
     }
 }
 
-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);
+    struct timeval now, next_repaint;
+
+    running = TRUE;
+
+    gettimeofday(&now, NULL);
+    next_repaint = now;
+
+    while (!quit) {
+        struct timeval next, *wait;
+        int            r, npaint, ntime;
+        d_list_it_t   *it;
+        fd_set         fds;
+        gboolean due;
+
+        event(dpy);
+
+        npaint = 0;
+        ntime = 0;
+        for (it = list_top(dpy->screens); it; it = it->next) {
+            d_screen_t *sc = it->data;
+            struct timeval next_timeout;
+
+            if (glxrender_next_timeout(sc, &next_timeout)) {
+                if (!ntime || time_compare(&next_timeout, &next) < 0) {
+                    next = next_timeout;
+                    ++ntime;
+                }
+            }
+            if (render_next_timeout(sc, &next_timeout)) {
+                if (!ntime || time_compare(&next_timeout, &next) < 0) {
+                    next = next_timeout;
+                    ++ntime;
+                }
+            }
+            if (fade_next_timeout(sc, &next_timeout)) {
+                if (!ntime || time_compare(&next_timeout, &next) < 0) {
+                    next = next_timeout;
+                    ++ntime;
+                }
+            }
+
+            if (sc->need_repaint)
+                ++npaint;
         }
+
+        if (npaint) {
+            if (!ntime || time_compare(&next_repaint, &next) < 0) {
+                next = next_repaint;
+            }
+        }
+
+        if (!npaint && !ntime) {
+            /* wait forever, there is nothing that needs drawing */
+            wait = NULL;
+            due = FALSE;
+        }
+        else if (time_compare(&next, &now) > 0) {
+            /* wait until the next allowed redraw time */
+            time_difference(&next, &now, &next);
+            wait = &next;
+            due = FALSE;
+        }
+        else {
+            /* don't wait cuz a timer is due now already */
+            due = TRUE;
+        }
+
+        //printf("select? %d %d\n", due, npaint);
+
+        if (!due) {
+            FD_ZERO(&fds);
+            FD_SET(dpy->fd, &fds);
+            r = select(dpy->fd+1, &fds, NULL, NULL, wait);
+        }
+        else
+            r = 0;
+
+        gettimeofday(&now, NULL);
+        if (r == 0) {
+            //printf("select timeout\n");
+            timeouts(dpy, &now);
+            if (time_compare(&next_repaint, &now) <= 0) {
+                paint(dpy);
+                next_repaint = now;
+                time_add(&next_repaint, 1000000/90); /* 60hz */
+            }
+            xcb_flush(dpy->conn);
+        }
+
+        if (xcb_connection_has_error(dpy->conn))
+            quit = TRUE;
+    }
+
+    running = FALSE;
+}
+
+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;
+        int id;
+        screen_setup_default_functions(sc);
+
+        /* these can be plugins.. */
+        id = 1;
+        glxrender_init(sc, id++);
+        //render_init(sc, id++);
+        fade_init(sc, id++);
+    }
+}
+
+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.. */
+        fade_free(sc);
+        //render_free(sc);
+        glxrender_free(sc);
     }
-    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;
+    }
+
+    signal(SIGINT, signal_quit_handler);
+    signal(SIGHUP, signal_quit_handler);
+    signal(SIGTERM, signal_quit_handler);
+    signal(SIGQUIT, signal_quit_handler);
+
+    setup_functions(dpy);
 
-    xcb_disconnect(conn);
+    {
+        /* some of the windows may already be visible */
+        d_list_it_t *sc_it;
+
+        for (sc_it = list_top(dpy->screens); sc_it; sc_it = sc_it->next) {
+            d_screen_t *sc = sc_it->data;
+            d_list_it_t *it;
+            for (it = list_bottom(sc->stacking); it; it = it->prev)
+                if (window_is_attr_mapped(it->data))
+                    sc->window_show(it->data);
+        }
+    }
+
+    printf("running\n");
+    run(dpy);
+
+    {
+        /* make everything hidden */
+        d_list_it_t *sc_it;
+
+        for (sc_it = list_top(dpy->screens); sc_it; sc_it = sc_it->next) {
+            d_screen_t *sc = sc_it->data;
+            d_list_it_t *it;
+            for (it = list_top(sc->stacking); it; it = it->next) {
+                if (window_is_mapped(it->data))
+                    sc->window_hide(it->data);
+            }
+        }
+    }
+
+    cleanup_functions(dpy);
+
+    display_unref(dpy);
     return 0;
 }
+
+gboolean
+dcompmgr_running(void)
+{
+    return running;
+}