initialize gl on the overlay window
[dana/dcompmgr.git] / dcompmgr.c
index c16dc5e..47c6a1d 100644 (file)
@@ -6,7 +6,11 @@
 #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 <signal.h>
 #include <xcb/xcb.h>
 #include <xcb/damage.h>
+#include <xcb/shape.h>
 
 typedef struct {
     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)
@@ -75,8 +81,10 @@ event(d_display_t *dpy)
             if (!sc) break;
             w = screen_find_window(sc, dev->window);
             vis = window_is_mapped(w);
-            if (vis)
+            if (vis) {
+                sc->window_become_zombie(w);
                 sc->window_hide(w);
+            }
             screen_remove_window(sc, w);
             if (vis) screen_refresh(sc);
             break;
@@ -94,8 +102,10 @@ event(d_display_t *dpy)
             if (rev->parent == sc->super.root)
                 screen_add_window(sc, rev->window);
             else {
-                if (window_is_mapped(w))
+                if (window_is_mapped(w)) {
+                    sc->window_become_zombie(w);
                     sc->window_hide(w);
+                }
                 screen_remove_window(sc, w);
             }
             screen_refresh(sc);
@@ -125,10 +135,27 @@ event(d_display_t *dpy)
             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;
@@ -157,23 +184,42 @@ event(d_display_t *dpy)
                 }
             }
             above = screen_find_window(sc, cev->above_sibling);
-            screen_stacking_move_above(sc, w, above);
+            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;
-            d_screen_t *sc;
 
             pev = (xcb_property_notify_event_t*)ev;
-            sc = display_screen_from_root(dpy, pev->window);
-            if (!sc) break;
             if (pev->atom == dpy->a.xrootpmap_id ||
                 pev->atom == dpy->a.esetroot_pmap_id ||
                 pev->atom == dpy->a.xsetroot_id)
             {
-                sc->screen_root_pixmap_changed(sc);
+                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;
         }
@@ -189,6 +235,7 @@ event(d_display_t *dpy)
 
                     w = screen_find_window(sc, dev->drawable);
                     if (w) {
+                        sc->window_damage(w);
                         screen_refresh(w->sc);
                         break;
                     }
@@ -196,6 +243,25 @@ event(d_display_t *dpy)
                 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);
@@ -204,17 +270,31 @@ event(d_display_t *dpy)
 }
 
 static void
-paint(d_display_t *dpy)
+timeouts(d_display_t *dpy, const struct timeval *now)
 {
     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;
+        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);
+    }
+}
+
+static void
+paint(d_display_t *dpy)
+{
+    d_list_it_t *it;
 
-        if (time_compare(&sc->next_repaint, &now) <= 0)
+    for (it = list_top(dpy->screens); it; it = it->next) {
+        d_screen_t *sc = it->data;
+        if (sc->need_repaint)
             sc->screen_paint(sc);
     }
 }
@@ -222,57 +302,100 @@ paint(d_display_t *dpy)
 static void
 run(d_display_t *dpy)
 {
+    struct timeval now, next_repaint;
+
+    running = TRUE;
+
+    gettimeofday(&now, NULL);
+    next_repaint = now;
+
     while (!quit) {
-        struct timeval next, now, *wait;
-        int            r, npaint;
+        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;
-            if (sc->need_repaint &&
-                (!npaint || time_compare(&sc->next_repaint, &next) < 0))
-            {
-                next = sc->next_repaint;
-                ++npaint;
+            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;
         }
 
-        gettimeofday(&now, 0);
+        if (npaint) {
+            if (!ntime || time_compare(&next_repaint, &next) < 0) {
+                next = next_repaint;
+            }
+        }
 
-        if (!npaint)
+        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 redraw is due now already */
-            next.tv_sec = 0;
-            next.tv_usec = 100;
-            wait = &next;
+            /* don't wait cuz a timer is due now already */
+            due = TRUE;
         }
 
-        FD_ZERO(&fds);
-        FD_SET(dpy->fd, &fds);
+        //printf("select? %d %d\n", due, npaint);
 
-        //printf("select %d\n", npaint);
+        if (!due) {
+            FD_ZERO(&fds);
+            FD_SET(dpy->fd, &fds);
+            r = select(dpy->fd+1, &fds, NULL, NULL, wait);
+        }
+        else
+            r = 0;
 
-        r = select(dpy->fd+1, &fds, NULL, NULL, wait);
+        gettimeofday(&now, NULL);
         if (r == 0) {
             //printf("select timeout\n");
-            paint(dpy);
+            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
@@ -287,7 +410,9 @@ setup_functions(d_display_t *dpy)
 
         /* these can be plugins.. */
         id = 1;
-        render_init(sc, id++);
+        glxrender_init(sc, id++);
+        //render_init(sc, id++);
+        fade_init(sc, id++);
     }
 }
 
@@ -300,7 +425,9 @@ cleanup_functions(d_display_t *dpy)
         d_screen_t *sc = it->data;
 
         /* these can be plugins.. */
-        render_free(sc);
+        fade_free(sc);
+        //render_free(sc);
+        glxrender_free(sc);
     }
 }
 
@@ -371,6 +498,7 @@ main(int argc, char **argv)
         }
     }
 
+    printf("running\n");
     run(dpy);
 
     {
@@ -392,3 +520,9 @@ main(int argc, char **argv)
     display_unref(dpy);
     return 0;
 }
+
+gboolean
+dcompmgr_running(void)
+{
+    return running;
+}