read all the available events at once
[dana/dcompmgr.git] / screen.c
index 684d6c5..e238bc7 100644 (file)
--- a/screen.c
+++ b/screen.c
@@ -1,5 +1,8 @@
 #include "screen.h"
+#include "display.h"
+#include "list.h"
 #include "window.h"
+#include "render.h"
 #include "gettext.h"
 #include <string.h>
 #include <stdlib.h>
 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
                         XCB_EVENT_MASK_PROPERTY_CHANGE)
 
-static void screen_init(d_screen_t *sc);
+static gboolean screen_init(d_screen_t *sc);
 static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
+static void screen_add_existing_windows(d_screen_t *sc);
 
 gboolean
-screen_register(d_screen_t *sc)
+screen_register(struct d_display *dpy, int num, d_screen_t *sc)
 {
     char *name;
     xcb_window_t w;
@@ -27,19 +31,23 @@ screen_register(d_screen_t *sc)
     uint32_t event_mask;
     gboolean taken, ret;
 
-    w = xcb_generate_id(sc->conn);
+    sc->dpy = dpy;
+    sc->num = num;
+    w = xcb_generate_id(sc->dpy->conn);
     event_mask = SELECTION_MASK;
-    xcb_create_window(sc->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
+    xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
                       0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
                       sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
 
     name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
-    ack = xcb_intern_atom(sc->conn, FALSE, strlen(name), name);
-    arep = xcb_intern_atom_reply(sc->conn, ack, NULL);
+    ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
+    arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
     g_free(name);
 
-    sck = xcb_get_selection_owner(sc->conn, arep->atom);
-    srep = xcb_get_selection_owner_reply(sc->conn, sck, NULL);
+    xcb_grab_server(sc->dpy->conn);
+
+    sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
+    srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
     taken = !!srep->owner;
     free(srep);
     if (taken) {
@@ -55,67 +63,129 @@ screen_register(d_screen_t *sc)
 
         time = screen_timestamp(sc);
 
-        xcb_grab_server(sc->conn);
-        xcb_set_selection_owner(sc->conn, w, arep->atom, time);
-        sck = xcb_get_selection_owner(sc->conn, arep->atom);
-        srep = xcb_get_selection_owner_reply(sc->conn, sck, NULL);
+        xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
+        sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
+        srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
         taken = srep->owner == w;
-        if (taken) {
-            screen_init(sc);
+        if (taken && screen_init(sc)) {
+            screen_add_existing_windows(sc);
             ret = TRUE;
         }
         else {
-            xcb_destroy_window(sc->conn, w);
+            xcb_destroy_window(sc->dpy->conn, w);
             ret = FALSE;
         }
-        xcb_ungrab_server(sc->conn);
-        xcb_flush(sc->conn);
     }
+
+    xcb_ungrab_server(sc->dpy->conn);
+    xcb_flush(sc->dpy->conn);
     return ret;
 }
 
 static guint
-window_hash(xcb_window_t *w) { return *w; }
+xcb_window_hash(xcb_window_t *w) { return *w; }
 
 static gboolean
-window_compare(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
+xcb_window_compare(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
 
-static void
+static gboolean
 screen_init(d_screen_t *sc)
 {
     uint32_t mask;
+    xcb_generic_error_t *err;
+    xcb_void_cookie_t redir_ck;
+    xcb_composite_get_overlay_window_cookie_t overlay_ck;
+    xcb_composite_get_overlay_window_reply_t *overlay_rep;
+
+    redir_ck =
+        xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
+                                          XCB_COMPOSITE_REDIRECT_MANUAL);
+
+    overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
+                                                  sc->super.root);
+
+    /* check that the redirect worked */
+    err = xcb_request_check(sc->dpy->conn, redir_ck);
+    if (err) {
+        printf(_("unable to redirect rendering, another composite manager must be running"));
+        free(err);
+        return FALSE;
+    }
 
-    sc->winhash = g_hash_table_new((GHashFunc)window_hash,
-                                   (GCompareFunc)window_compare);
-
-//    xcb_composite_redirect_subwindows(sc->conn, sc->super.root,
-//                                      XCB_COMPOSITE_REDIRECT_MANUAL);
+    /* get the overlay window reply */
+    overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
+                                                         overlay_ck,
+                                                         NULL);
+    if (!overlay_rep) {
+        printf(_("unable to get the composite overlay window\n"));
+        return FALSE;
+    }
+    sc->overlay = overlay_rep->overlay_win;
+    free(overlay_rep);
 
     mask = SELECTION_MASK;
-    xcb_change_window_attributes(sc->conn, sc->selwin,
+    xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
                                  XCB_CW_EVENT_MASK, &mask);
     mask = ROOT_MASK;
-    xcb_change_window_attributes(sc->conn, sc->super.root,
+    xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
                                  XCB_CW_EVENT_MASK, &mask);
+
+    /* use the render backend */
+    sc->paint = render_paint_screen;
+
+    sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
+                                   (GCompareFunc)xcb_window_compare);
+    sc->stacking = list_new();
+
+    return TRUE;
+}
+
+void screen_free(d_screen_t *sc)
+{
+    g_hash_table_unref(sc->winhash);
+    list_unref(sc->stacking);
 }
 
 void
-screen_add_window(d_screen_t *sc, xcb_create_notify_event_t *cev)
+screen_add_window(d_screen_t *sc, xcb_window_t wid)
 {
     d_window_t *w;
 
-    w = window_new(cev->window, sc);
+    /* XXX xgetwindowattributes - size and tings */
+
+    w = window_new(wid, sc);
     g_hash_table_insert(sc->winhash, &w->id, w);
 
-    printf("added window 0x%x\n", w->id);
+    printf("screen added window 0x%x\n", w->id);
+}
+
+static void
+screen_add_existing_windows(d_screen_t *sc)
+{
+    xcb_query_tree_cookie_t ck;
+    xcb_query_tree_reply_t *rep;
+
+    ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
+    rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
+    if (rep) {
+        xcb_window_iterator_t it;
+
+        printf("query\n");
+        it = xcb_query_tree_children_iterator(rep);
+        for (; it.rem; xcb_window_next(&it))
+            screen_add_window(sc, *it.data);
+
+        free(rep);
+    }
 }
 
 void
 screen_remove_window(d_screen_t *sc, struct d_window *w)
 {
-    printf("removed window 0x%x\n", w->id);
+    printf("screen removed window 0x%x\n", w->id);
 
     g_hash_table_remove(sc->winhash, &w->id);
+    w->become_zombie(w);
     window_unref(w);
 }
 
@@ -125,29 +195,19 @@ screen_find_window(d_screen_t *sc, xcb_window_t id)
     return g_hash_table_lookup(sc->winhash, &id);
 }
 
-void
-screen_show_window(d_screen_t *sc, struct d_window *w)
-{
-}
-
-void
-screen_hide_window(d_screen_t *sc, struct d_window *w)
-{
-}
-
 static xcb_timestamp_t
 screen_timestamp(d_screen_t *sc)
 {
     xcb_void_cookie_t ck;
     xcb_timestamp_t   time;
 
-    ck = xcb_change_property(sc->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
+    ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
                              sc->selatom, sc->selatom, 32, 0, NULL);
-    xcb_flush(sc->conn);
+    xcb_flush(sc->dpy->conn);
     while (1) {
         xcb_generic_event_t *ev;
 
-        ev = xcb_wait_for_event(sc->conn);
+        ev = xcb_wait_for_event(sc->dpy->conn);
         if (!ev) {
             printf(_("IO error\n"));
             exit(0);
@@ -155,7 +215,7 @@ screen_timestamp(d_screen_t *sc)
 
         /* expect errors.. */
         if (!ev->response_type) {
-            /* XXX handle error */
+            display_error(sc->dpy, (xcb_generic_error_t*)ev);
             free(ev);
             continue;
         }
@@ -171,3 +231,15 @@ screen_timestamp(d_screen_t *sc)
     printf("created timestamp %lu\n", (unsigned long) time);
     return time;
 }
+
+void
+screen_stacking_add(d_screen_t *sc, struct d_window *w)
+{
+    list_prepend(sc->stacking, w);
+}
+
+void
+screen_stacking_remove(d_screen_t *sc, struct d_window *w)
+{
+    list_remove(sc->stacking, w);
+}