fix some more memory problems
[dana/dcompmgr.git] / dcompmgr.c
index b8124fb..b9fde3c 100644 (file)
@@ -1,3 +1,5 @@
+#include "efence.h"
+
 #include "screen.h"
 #include "window.h"
 #include "list.h"
@@ -11,6 +13,7 @@
 #include <sys/select.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 #include <xcb/xcb.h>
 #include <xcb/damage.h>
 
@@ -18,6 +21,8 @@ typedef struct {
     int foo;
 } d_options_t;
 
+static gboolean quit = FALSE;
+
 static void
 read_options(int argc, char **argv, d_options_t *opts)
 {
@@ -25,11 +30,18 @@ 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))) {
+    while ((ev = xcb_poll_for_event(dpy->conn)) && !quit) {
         //printf("event %d\n", ev->response_type);
 
         if (!ev->response_type) {
@@ -43,11 +55,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:
@@ -62,7 +75,8 @@ event(d_display_t *dpy)
             if (!sc) break;
             w = screen_find_window(sc, dev->window);
             vis = window_is_mapped(w);
-            sc->window_hide(w);
+            if (vis)
+                sc->window_hide(w);
             screen_remove_window(sc, w);
             if (vis) screen_refresh(sc);
             break;
@@ -80,7 +94,8 @@ 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_hide(w);
                 screen_remove_window(sc, w);
             }
             screen_refresh(w->sc);
@@ -124,6 +139,7 @@ event(d_display_t *dpy)
             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 ||
@@ -132,8 +148,13 @@ event(d_display_t *dpy)
                 window_configure(w, cev->x, cev->y,
                                  cev->width, cev->height,
                                  cev->border_width);
-                if (window_is_mapped(w))
-                    sc->window_reconfigure(w);
+                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);
@@ -185,9 +206,6 @@ paint(d_display_t *dpy)
 static void
 run(d_display_t *dpy)
 {
-    gboolean quit;
-
-    quit = FALSE;
     while (!quit) {
         struct timeval next, now, *wait;
         int            r, npaint;
@@ -220,7 +238,7 @@ run(d_display_t *dpy)
         else {
             /* don't wait cuz a redraw is due now already */
             next.tv_sec = 0;
-            next.tv_usec = 0;
+            next.tv_usec = 100;
             wait = &next;
         }
 
@@ -230,9 +248,7 @@ run(d_display_t *dpy)
         //printf("select %d\n", npaint);
 
         r = select(dpy->fd+1, &fds, NULL, NULL, wait);
-        if (r < 0)
-            printf("select error\n");
-        else if (r == 0) {
+        if (r == 0) {
             //printf("select timeout\n");
             paint(dpy);
             xcb_flush(dpy->conn);
@@ -250,10 +266,12 @@ 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++);
     }
 }
 
@@ -317,6 +335,11 @@ 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);
 
     {
@@ -326,14 +349,32 @@ main(int argc, char **argv)
         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))
+            for (it = list_bottom(sc->stacking); it; it = it->prev)
+                if (window_is_mapped(it->data)) {
+                    /* make the window think it is unmapped so that the
+                       show works right */
+                    window_fake_unmapped(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);