super fading
[dana/dcompmgr.git] / dcompmgr.c
index f441f8a..71f3eee 100644 (file)
@@ -1,22 +1,32 @@
+#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 "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>
 
 typedef struct {
     int foo;
 } d_options_t;
 
+static gboolean quit = FALSE;
+
 static void
 read_options(int argc, char **argv, d_options_t *opts)
 {
@@ -24,12 +34,19 @@ read_options(int argc, char **argv, d_options_t *opts)
 }
 
 static void
+signal_quit_handler(int sig)
+{
+    printf("caught signal %d, quitting\n", sig);
+    quit = TRUE;
+}
+
+static void
 event(d_display_t *dpy)
 {
     xcb_generic_event_t *ev;
 
-    while ((ev = xcb_poll_for_event(dpy->conn))) {
-        printf("event %d\n", ev->response_type);
+    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);
@@ -42,11 +59,12 @@ event(d_display_t *dpy)
         {
             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;
-            screen_add_window(sc, cev->window);
+            w = screen_add_window(sc, cev->window);
             break;
         }
         case XCB_DESTROY_NOTIFY:
@@ -54,15 +72,19 @@ event(d_display_t *dpy)
             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);
-            sc->window_hide(w);
+            vis = window_is_mapped(w);
+            if (vis) {
+                sc->window_become_zombie(w);
+                sc->window_hide(w);
+            }
             screen_remove_window(sc, w);
-            printf("** refresh needed **\n");
-            screen_refresh(sc);
+            if (vis) screen_refresh(sc);
             break;
         }
         case XCB_REPARENT_NOTIFY:
@@ -78,9 +100,13 @@ event(d_display_t *dpy)
             if (rev->parent == sc->super.root)
                 screen_add_window(sc, rev->window);
             else {
-                sc->window_hide(w);
+                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:
@@ -94,6 +120,7 @@ event(d_display_t *dpy)
             if (!sc) break;
             w = screen_find_window(sc, mev->window);
             sc->window_show(w);
+            screen_refresh(w->sc);
             break;
         }
         case XCB_UNMAP_NOTIFY:
@@ -106,13 +133,151 @@ 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;
+            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);
+            screen_stacking_move_above(sc, 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) {
+                        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) {
+                        sc->window_reshape(w);
+                        screen_refresh(w->sc);
+                        break;
+                    }
+                }
+            }
             break;
         }
         free(ev);
+        xcb_flush(dpy->conn);
+    }
+}
+
+static void
+timeouts(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;
+
+        render_timeout(sc, &now);
+        fade_timeout(sc, &now);
     }
 }
 
@@ -135,27 +300,40 @@ paint(d_display_t *dpy)
 static void
 run(d_display_t *dpy)
 {
-    gboolean quit;
-    fd_set   fds;
-    int      max = -1;
-
-    FD_ZERO(&fds);
-    FD_SET(dpy->fd, &fds);
-    max = MAX(max, dpy->fd);
-
-    paint(dpy);
-
-    quit = FALSE;
     while (!quit) {
         struct timeval next, now, *wait;
-        int            r, npaint;
+        int            r, npaint, ntime;
         d_list_it_t   *it;
+        fd_set         fds;
+
+        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 (render_next_timeout(sc, &next_timeout)) {
+                if ((!npaint && !ntime) ||
+                    time_compare(&next_timeout, &next) < 0)
+                {
+                    next = next_timeout;
+                    ++ntime;
+                }
+            }
+            if (fade_next_timeout(sc, &next_timeout)) {
+                if ((!npaint && !ntime) ||
+                    time_compare(&next_timeout, &next) < 0)
+                {
+                    next = next_timeout;
+                    ++ntime;
+                }
+            }
+
             if (sc->need_repaint &&
-                (!npaint || time_compare(&sc->next_repaint, &next) < 0))
+                ((!npaint && !ntime) ||
+                 time_compare(&sc->next_repaint, &next) < 0))
             {
                 next = sc->next_repaint;
                 ++npaint;
@@ -164,9 +342,7 @@ run(d_display_t *dpy)
 
         gettimeofday(&now, 0);
 
-        printf("* loop paint %d *\n", npaint);
-
-        if (!npaint)
+        if (!npaint && !ntime)
             /* wait forever, there is nothing that needs drawing */
             wait = NULL;
         else if (time_compare(&next, &now) > 0) {
@@ -175,24 +351,23 @@ run(d_display_t *dpy)
             wait = &next;
         }
         else {
-            /* don't wait cuz a redraw is due now already */
+            /* don't wait cuz a timer is due now already */
             next.tv_sec = 0;
-            next.tv_usec = 1;
+            next.tv_usec = 0;
             wait = &next;
         }
 
-        r = select(max+1, &fds, NULL, NULL, wait);
-        if (r < 0)
-            printf("select error\n");
-        else if (r == 0) {
-            printf("select timeout\n");
+        FD_ZERO(&fds);
+        FD_SET(dpy->fd, &fds);
+
+        //printf("select %d\n", npaint);
+
+        r = select(dpy->fd+1, &fds, NULL, NULL, wait);
+        if (r == 0) {
+            //printf("select timeout\n");
+            timeouts(dpy);
             paint(dpy);
-        }
-        else {
-            printf("select data\n");
-            /*if (FD_ISSET(dpy->fd, &fds))*/ {
-                event(dpy);
-            }
+            xcb_flush(dpy->conn);
         }
 
         if (xcb_connection_has_error(dpy->conn))
@@ -207,10 +382,13 @@ setup_functions(d_display_t *dpy)
 
     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.. */
-        render_init(sc);
+        id = 1;
+        render_init(sc, id++);
+        fade_init(sc, id++);
     }
 }
 
@@ -223,6 +401,7 @@ cleanup_functions(d_display_t *dpy)
         d_screen_t *sc = it->data;
 
         /* these can be plugins.. */
+        fade_free(sc);
         render_free(sc);
     }
 }
@@ -274,10 +453,42 @@ main(int argc, char **argv)
         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);
 
+    {
+        /* 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);
+        }
+    }
+
     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);