redirect rendering and get the composite overlay window
[dana/dcompmgr.git] / screen.c
index f268ede..dfad6e4 100644 (file)
--- a/screen.c
+++ b/screen.c
 #include "screen.h"
+#include "display.h"
+#include "window.h"
 #include "gettext.h"
 #include <string.h>
+#include <stdlib.h>
 #include <stdio.h>
+#include <xcb/composite.h>
 
-#define ROOT_MASK (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY)
+#define ROOT_MASK      (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
+                        XCB_EVENT_MASK_STRUCTURE_NOTIFY)
+
+#define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
+                        XCB_EVENT_MASK_PROPERTY_CHANGE)
+
+static gboolean screen_init(d_screen_t *sc);
+static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
 
 gboolean
-screen_register(xcb_connection_t *conn, d_screen_t *sc)
+screen_register(struct d_display *dpy, int num, d_screen_t *sc)
 {
     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;
+    uint32_t event_mask;
+    gboolean taken, ret;
 
-    w = xcb_generate_id(conn);
-    xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
+    sc->dpy = dpy;
+    sc->num = num;
+    w = xcb_generate_id(sc->dpy->conn);
+    event_mask = SELECTION_MASK;
+    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, 0, NULL);
+                      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(conn, FALSE, strlen(name), name);
-    arep = xcb_intern_atom_reply(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(conn, arep->atom);
-    srep = xcb_get_selection_owner_reply(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) {
         printf(_("screen %d already has a composite manager, skipping\n"),
                sc->num);
-        return FALSE;
+        ret = FALSE;
     }
+    else {
+        xcb_timestamp_t time;
 
-    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;
+        sc->selatom = arep->atom;
+
+        time = screen_timestamp(sc);
+
+        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))
+            ret = TRUE;
+        else {
+            xcb_destroy_window(sc->dpy->conn, w);
+            ret = FALSE;
+        }
     }
-    else {
-        xcb_destroy_window(conn, w);
+
+    xcb_ungrab_server(sc->dpy->conn);
+    xcb_flush(sc->dpy->conn);
+    return ret;
+}
+
+static guint
+window_hash(xcb_window_t *w) { return *w; }
+
+static gboolean
+window_compare(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
+
+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;
+
+    sc->winhash = g_hash_table_new((GHashFunc)window_hash,
+                                   (GCompareFunc)window_compare);
+
+    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;
+    }
+
+    /* 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->dpy->conn, sc->selwin,
+                                 XCB_CW_EVENT_MASK, &mask);
+    mask = ROOT_MASK;
+    xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
+                                 XCB_CW_EVENT_MASK, &mask);
+    return TRUE;
 }
 
-void screen_listen(xcb_connection_t *conn, d_screen_t *sc)
+void
+screen_add_window(d_screen_t *sc, xcb_window_t wid)
 {
-    const uint32_t mask = ROOT_MASK;
-    xcb_change_window_attributes(conn, sc->super.root,
-                                 XCB_CW_EVENT_MASK, &mask);
+    d_window_t *w;
+
+    /* XXX xgetwindowattributes - size and tings */
+
+    w = window_new(wid, sc);
+    g_hash_table_insert(sc->winhash, &w->id, w);
+
+    printf("screen added window 0x%x\n", w->id);
+}
+
+void
+screen_remove_window(d_screen_t *sc, struct d_window *w)
+{
+    printf("screen removed window 0x%x\n", w->id);
+
+    g_hash_table_remove(sc->winhash, &w->id);
+    window_unref(w);
+}
+
+d_window_t*
+screen_find_window(d_screen_t *sc, xcb_window_t id)
+{
+    return g_hash_table_lookup(sc->winhash, &id);
+}
+
+static xcb_timestamp_t
+screen_timestamp(d_screen_t *sc)
+{
+    xcb_void_cookie_t ck;
+    xcb_timestamp_t   time;
+
+    ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
+                             sc->selatom, sc->selatom, 32, 0, NULL);
+    xcb_flush(sc->dpy->conn);
+    while (1) {
+        xcb_generic_event_t *ev;
+
+        ev = xcb_wait_for_event(sc->dpy->conn);
+        if (!ev) {
+            printf(_("IO error\n"));
+            exit(0);
+        }
+
+        /* expect errors.. */
+        if (!ev->response_type) {
+            display_error(sc->dpy, (xcb_generic_error_t*)ev);
+            free(ev);
+            continue;
+        }
+
+        if (ev->response_type == XCB_PROPERTY_NOTIFY &&
+            ev->full_sequence == ck.sequence)
+        {
+            time = ((xcb_property_notify_event_t*)ev)->time;
+            free(ev);
+            break;
+        }
+    }
+    printf("created timestamp %lu\n", (unsigned long) time);
+    return time;
 }