[PATCH 02/19] Xephyr: start converting hostx.c over to xcb

Eric Anholt eric at anholt.net
Mon Aug 26 13:20:46 PDT 2013


From: Julien Cristau <jcristau at debian.org>

v2: Dropped the hostx_load_keymap changes, now that that function is
    gutted (anholt).

Reviewed-by: Mikhail Gusarov <dottedmag at dottedmag.net> (v1)
Signed-off-by: Julien Cristau <jcristau at debian.org>
Signed-off-by: Eric Anholt <eric at anholt.net>
---
 configure.ac            |   4 +-
 hw/kdrive/ephyr/hostx.c | 614 +++++++++++++++++++++++++++---------------------
 hw/kdrive/ephyr/hostx.h |   7 +
 3 files changed, 357 insertions(+), 268 deletions(-)

diff --git a/configure.ac b/configure.ac
index d27ca23..2c9585d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2116,12 +2116,12 @@ if test "$KDRIVE" = yes; then
        AC_DEFINE(KDRIVE_MOUSE, 1, [Enable KDrive mouse driver])
     fi
 
-    XEPHYR_REQUIRED_LIBS="x11 >= 1.6 $LIBXEXT xau xdmcp"
+    XEPHYR_REQUIRED_LIBS="x11 >= 1.6 $LIBXEXT xau xdmcp xcb x11-xcb xcb-shape xcb-aux xcb-image xcb-icccm xcb-shm"
     if test "x$XV" = xyes; then
         XEPHYR_REQUIRED_LIBS="$XEPHYR_REQUIRED_LIBS xv"
     fi
     if test "x$DRI" = xyes && test "x$GLX" = xyes; then
-        XEPHYR_REQUIRED_LIBS="$XEPHYR_REQUIRED_LIBS $LIBGL libdrm"
+        XEPHYR_REQUIRED_LIBS="$XEPHYR_REQUIRED_LIBS $LIBGL libdrm xcb-glx xcb-xf86dri"
     fi
 
     if test "x$XEPHYR" = xauto; then
diff --git a/hw/kdrive/ephyr/hostx.c b/hw/kdrive/ephyr/hostx.c
index f46770f..2ecda14 100644
--- a/hw/kdrive/ephyr/hostx.c
+++ b/hw/kdrive/ephyr/hostx.c
@@ -53,20 +53,22 @@
 
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
-#include <X11/Xatom.h>
 #include <X11/keysym.h>
-#include <X11/extensions/XShm.h>
 #include <X11/extensions/shape.h>
+#include <xcb/xcb.h>
+#include <xcb/xproto.h>
+#include <X11/Xlib-xcb.h>
+#include <xcb/xcb_icccm.h>
+#include <xcb/xcb_aux.h>
+#include <xcb/shm.h>
+#include <xcb/xcb_image.h>
+#include <xcb/shape.h>
 #ifdef XF86DRI
-#include <GL/glx.h>
-#endif                          /* XF86DRI */
+#include <xcb/xf86dri.h>
+#include <xcb/glx.h>
+#endif /* XF86DRI */
 #include "ephyrlog.h"
 
-#ifdef XF86DRI
-extern Bool XF86DRIQueryExtension(Display * dpy,
-                                  int *event_basep, int *error_basep);
-#endif
-
 /*  
  * All xlib calls go here, which gets built as its own .a .
  * Mixing kdrive and xlib headers causes all sorts of types
@@ -77,11 +79,11 @@ struct EphyrHostScreen {
     Window win;
     Window win_pre_existing;    /* Set via -parent option like xnest */
     Window peer_win;            /* Used for GL; should be at most one */
-    XImage *ximg;
+    xcb_image_t    *ximg;
     int win_width, win_height;
     int server_depth;
     unsigned char *fb_data;     /* only used when host bpp != server bpp */
-    XShmSegmentInfo shminfo;
+    xcb_shm_segment_info_t shminfo;
 
     void *info;                 /* Pointer to the screen this is associated with */
     int mynum;                  /* Screen number */
@@ -90,10 +92,11 @@ struct EphyrHostScreen {
 struct EphyrHostXVars {
     char *server_dpy_name;
     Display *dpy;
+    xcb_connection_t *conn;
     int screen;
     Visual *visual;
     Window winroot;
-    GC gc;
+    xcb_gcontext_t  gc;
     int depth;
     Bool use_host_cursor;
     Bool use_fullscreen;
@@ -126,11 +129,6 @@ char *ephyrTitle = NULL;
 static void
  hostx_set_fullscreen_hint(void);
 
-/* X Error traps */
-
-static int trapped_error_code = 0;
-static int (*old_error_handler) (Display * d, XErrorEvent * e);
-
 #define host_depth_matches_server(_vars) (HostX.depth == (_vars)->server_depth)
 
 static struct EphyrHostScreen *
@@ -146,27 +144,6 @@ host_screen_from_screen_info(EphyrScreenInfo * screen)
     return NULL;
 }
 
-static int
-error_handler(Display * display, XErrorEvent * error)
-{
-    trapped_error_code = error->error_code;
-    return 0;
-}
-
-static void
-hostx_errors_trap(void)
-{
-    trapped_error_code = 0;
-    old_error_handler = XSetErrorHandler(error_handler);
-}
-
-static int
-hostx_errors_untrap(void)
-{
-    XSetErrorHandler(old_error_handler);
-    return trapped_error_code;
-}
-
 int
 hostx_want_screen_size(EphyrScreenInfo screen, int *width, int *height)
 {
@@ -223,9 +200,13 @@ hostx_set_win_title(EphyrScreenInfo screen, const char *extra_text)
         return;
 
     if (ephyrTitle) {
-        XStoreName(HostX.dpy, host_screen->win, ephyrTitle);
-    }
-    else {
+        xcb_icccm_set_wm_name(HostX.conn,
+                              host_screen->win,
+                              XCB_ATOM_STRING,
+                              8,
+                              strlen(ephyrTitle),
+                              ephyrTitle);
+    } else {
 #define BUF_LEN 256
         char buf[BUF_LEN + 1];
 
@@ -234,7 +215,12 @@ hostx_set_win_title(EphyrScreenInfo screen, const char *extra_text)
                  HostX.server_dpy_name,
                  host_screen->mynum, (extra_text != NULL) ? extra_text : "");
 
-        XStoreName(HostX.dpy, host_screen->win, buf);
+        xcb_icccm_set_wm_name(HostX.conn,
+                              host_screen->win,
+                              XCB_ATOM_STRING,
+                              8,
+                              strlen(buf),
+                              buf);
     }
 }
 
@@ -275,21 +261,34 @@ hostx_want_fullscreen(void)
     return HostX.use_fullscreen;
 }
 
+static xcb_intern_atom_cookie_t cookie_WINDOW_STATE,
+				cookie_WINDOW_STATE_FULLSCREEN;
+
 static void
 hostx_set_fullscreen_hint(void)
 {
-    Atom atom_WINDOW_STATE, atom_WINDOW_STATE_FULLSCREEN;
+    xcb_atom_t atom_WINDOW_STATE, atom_WINDOW_STATE_FULLSCREEN;
     int index;
+    xcb_intern_atom_reply_t *reply;
+
+    reply = xcb_intern_atom_reply(HostX.conn, cookie_WINDOW_STATE, NULL);
+    atom_WINDOW_STATE = reply->atom;
+    free(reply);
 
-    atom_WINDOW_STATE = XInternAtom(HostX.dpy, "_NET_WM_STATE", False);
-    atom_WINDOW_STATE_FULLSCREEN
-        = XInternAtom(HostX.dpy, "_NET_WM_STATE_FULLSCREEN", False);
+    reply = xcb_intern_atom_reply(HostX.conn, cookie_WINDOW_STATE_FULLSCREEN,
+                                  NULL);
+    atom_WINDOW_STATE_FULLSCREEN = reply->atom;
+    free(reply);
 
     for (index = 0; index < HostX.n_screens; index++) {
-        XChangeProperty(HostX.dpy, HostX.screens[index].win,
-                        atom_WINDOW_STATE, XA_ATOM, 32,
-                        PropModeReplace,
-                        (unsigned char *) &atom_WINDOW_STATE_FULLSCREEN, 1);
+        xcb_change_property(HostX.conn,
+                            PropModeReplace,
+                            HostX.screens[index].win,
+                            atom_WINDOW_STATE,
+                            XCB_ATOM_ATOM,
+                            32,
+                            1,
+                            &atom_WINDOW_STATE_FULLSCREEN);
     }
 }
 
@@ -336,22 +335,26 @@ x_io_error_handler(Display * dpy)
 int
 hostx_init(void)
 {
-    XSetWindowAttributes attr;
-    Cursor empty_cursor;
-    Pixmap cursor_pxm;
-    XColor col;
+    uint32_t attr;
+    xcb_cursor_t empty_cursor;
+    xcb_pixmap_t cursor_pxm;
+    uint16_t red, green, blue;
+    uint32_t pixel;
     int index;
     char *tmpstr;
-    XClassHint *class_hint;
-
-    attr.event_mask =
-        ButtonPressMask
-        | ButtonReleaseMask
-        | PointerMotionMask
-        | KeyPressMask
-        | KeyReleaseMask
-        | ExposureMask
-        | StructureNotifyMask;
+    char *class_hint;
+    size_t class_len;
+    const xcb_query_extension_reply_t *shm_rep;
+    xcb_screen_t *screen;
+
+    attr =
+        XCB_EVENT_MASK_BUTTON_PRESS
+        | XCB_EVENT_MASK_BUTTON_RELEASE
+        | XCB_EVENT_MASK_POINTER_MOTION
+        | XCB_EVENT_MASK_KEY_PRESS
+        | XCB_EVENT_MASK_KEY_RELEASE
+        | XCB_EVENT_MASK_EXPOSURE
+        | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
 
     EPHYR_DBG("mark");
 
@@ -362,137 +365,190 @@ hostx_init(void)
 
     XSetIOErrorHandler(x_io_error_handler);
 
+    HostX.conn = XGetXCBConnection(HostX.dpy);
     HostX.screen = DefaultScreen(HostX.dpy);
+    screen = xcb_aux_get_screen(HostX.conn, HostX.screen);
     HostX.winroot = RootWindow(HostX.dpy, HostX.screen);
-    HostX.gc = XCreateGC(HostX.dpy, HostX.winroot, 0, NULL);
+    HostX.gc = xcb_generate_id(HostX.conn);
     HostX.depth = DefaultDepth(HostX.dpy, HostX.screen);
     HostX.visual = DefaultVisual(HostX.dpy, HostX.screen);
 
-    class_hint = XAllocClassHint();
+    xcb_create_gc(HostX.conn, HostX.gc, HostX.winroot, 0, NULL);
+    cookie_WINDOW_STATE = xcb_intern_atom(HostX.conn, False,
+                                          strlen("_NET_WM_STATE"),
+                                          "_NET_WM_STATE");
+    cookie_WINDOW_STATE_FULLSCREEN =
+        xcb_intern_atom(HostX.conn, False,
+                        strlen("_NET_WM_STATE_FULLSCREEN"),
+                        "_NET_WM_STATE_FULLSCREEN");
 
     for (index = 0; index < HostX.n_screens; index++) {
         struct EphyrHostScreen *host_screen = &HostX.screens[index];
 
+        host_screen->win = xcb_generate_id(HostX.conn);
         host_screen->server_depth = HostX.depth;
-        if (host_screen->win_pre_existing != None) {
-            Status result;
-            XWindowAttributes prewin_attr;
-
-            /* Get screen size from existing window */
-
-            hostx_errors_trap();
 
-            result = XGetWindowAttributes(HostX.dpy,
-                                          host_screen->win_pre_existing,
-                                          &prewin_attr);
+        if (host_screen->win_pre_existing != XCB_WINDOW_NONE) {
+            xcb_get_geometry_reply_t *prewin_geom;
+            xcb_get_geometry_cookie_t cookie;
+            xcb_generic_error_t *e = NULL;
 
-            if (hostx_errors_untrap() || !result) {
-                fprintf(stderr, "\nXephyr -parent window' does not exist!\n");
-                exit(1);
+            /* Get screen size from existing window */
+            cookie = xcb_get_geometry(HostX.conn,
+                                      host_screen->win_pre_existing);
+            prewin_geom = xcb_get_geometry_reply(HostX.conn, cookie, &e);
+
+            if (e) {
+                free(e);
+                free(prewin_geom);
+                fprintf (stderr, "\nXephyr -parent window' does not exist!\n");
+                exit (1);
             }
 
-            host_screen->win_width = prewin_attr.width;
-            host_screen->win_height = prewin_attr.height;
-
-            host_screen->win = XCreateWindow(HostX.dpy,
-                                             host_screen->win_pre_existing,
-                                             0, 0,
-                                             host_screen->win_width,
-                                             host_screen->win_height,
-                                             0,
-                                             CopyFromParent,
-                                             CopyFromParent,
-                                             CopyFromParent,
-                                             CWEventMask, &attr);
+            host_screen->win_width  = prewin_geom->width;
+            host_screen->win_height = prewin_geom->height;
+
+            free(prewin_geom);
+
+            xcb_create_window(HostX.conn,
+                              XCB_COPY_FROM_PARENT,
+                              host_screen->win,
+                              host_screen->win_pre_existing,
+                              0,0,
+                              host_screen->win_width,
+                              host_screen->win_height,
+                              0,
+                              XCB_WINDOW_CLASS_COPY_FROM_PARENT,
+                              XCB_COPY_FROM_PARENT,
+                              XCB_CW_EVENT_MASK,
+                              &attr);
         }
         else {
-            host_screen->win = XCreateWindow(HostX.dpy, HostX.winroot, 0, 0, 100, 100,  /* will resize */
-                                             0,
-                                             CopyFromParent,
-                                             CopyFromParent,
-                                             CopyFromParent,
-                                             CWEventMask, &attr);
-
-            hostx_set_win_title(host_screen->info,
-                                "(ctrl+shift grabs mouse and keyboard)");
+            xcb_create_window(HostX.conn,
+                              XCB_COPY_FROM_PARENT,
+                              host_screen->win,
+                              HostX.winroot,
+                              0,0,100,100, /* will resize */
+                              0,
+                              XCB_WINDOW_CLASS_COPY_FROM_PARENT,
+                              XCB_COPY_FROM_PARENT,
+                              XCB_CW_EVENT_MASK,
+                              &attr);
+
+            hostx_set_win_title (host_screen->info,
+                                 "(ctrl+shift grabs mouse and keyboard)");
 
             if (HostX.use_fullscreen) {
-                host_screen->win_width = DisplayWidth(HostX.dpy, HostX.screen);
-                host_screen->win_height =
-                    DisplayHeight(HostX.dpy, HostX.screen);
+                host_screen->win_width  = DisplayWidth(HostX.dpy, HostX.screen);
+                host_screen->win_height = DisplayHeight(HostX.dpy, HostX.screen);
 
                 hostx_set_fullscreen_hint();
             }
 
+            tmpstr = getenv("RESOURCE_NAME");
+            if (tmpstr && (!ephyrResNameFromCmd))
+                ephyrResName = tmpstr;
+            class_len = strlen(ephyrResName) + 1 + strlen("Xephyr") + 1;
+            class_hint = malloc(class_len);
             if (class_hint) {
-                tmpstr = getenv("RESOURCE_NAME");
-                if (tmpstr && (!ephyrResNameFromCmd))
-                    ephyrResName = tmpstr;
-                class_hint->res_name = ephyrResName;
-                class_hint->res_class = "Xephyr";
-                XSetClassHint(hostx_get_display(), host_screen->win,
-                              class_hint);
-
+                strcpy(class_hint, ephyrResName);
+                strcpy(class_hint + strlen(ephyrResName) + 1, "Xephyr");
+                xcb_change_property(HostX.conn,
+                                    XCB_PROP_MODE_REPLACE,
+                                    host_screen->win,
+                                    XCB_ATOM_WM_CLASS,
+                                    XCB_ATOM_STRING,
+                                    8,
+                                    class_len,
+                                    class_hint);
+                free(class_hint);
             }
-
         }
     }
 
-    if (class_hint)
-        XFree(class_hint);
+    if (!xcb_aux_parse_color("red", &red, &green, &blue)) {
+        xcb_lookup_color_cookie_t c =
+            xcb_lookup_color(HostX.conn, screen->default_colormap, 3, "red");
+        xcb_lookup_color_reply_t *reply =
+            xcb_lookup_color_reply(HostX.conn, c, NULL);
+        red = reply->exact_red;
+        green = reply->exact_green;
+        blue = reply->exact_blue;
+        free(reply);
+    }
 
-    XParseColor(HostX.dpy, DefaultColormap(HostX.dpy, HostX.screen),
-                "red", &col);
-    XAllocColor(HostX.dpy, DefaultColormap(HostX.dpy, HostX.screen), &col);
-    XSetForeground(HostX.dpy, HostX.gc, col.pixel);
+    {
+        xcb_alloc_color_cookie_t c = xcb_alloc_color(HostX.conn,
+                                                     screen->default_colormap,
+                                                     red, green, blue);
+        xcb_alloc_color_reply_t *r = xcb_alloc_color_reply(HostX.conn, c, NULL);
+        red = r->red;
+        green = r->green;
+        blue = r->blue;
+        pixel = r->pixel;
+        free(r);
+    }
+
+    xcb_change_gc(HostX.conn, HostX.gc, XCB_GC_FOREGROUND, &pixel);
 
-    if (!hostx_want_host_cursor()) {
+    if (!hostx_want_host_cursor ()) {
         /* Ditch the cursor, we provide our 'own' */
-        cursor_pxm = XCreatePixmap(HostX.dpy, HostX.winroot, 1, 1, 1);
-        memset(&col, 0, sizeof(col));
-        empty_cursor = XCreatePixmapCursor(HostX.dpy,
-                                           cursor_pxm, cursor_pxm,
-                                           &col, &col, 1, 1);
+        cursor_pxm = xcb_generate_id(HostX.conn);
+        xcb_create_pixmap(HostX.conn, 1, cursor_pxm, HostX.winroot, 1, 1);
+        empty_cursor = xcb_generate_id(HostX.conn);
+        xcb_create_cursor(HostX.conn,
+                          empty_cursor,
+                          cursor_pxm, cursor_pxm,
+                          0,0,0,
+                          0,0,0,
+                          1,1);
         for (index = 0; index < HostX.n_screens; index++) {
-            XDefineCursor(HostX.dpy, HostX.screens[index].win, empty_cursor);
+            xcb_change_window_attributes(HostX.conn,
+                                         HostX.screens[index].win,
+                                         XCB_CW_CURSOR,
+                                         &empty_cursor);
         }
-        XFreePixmap(HostX.dpy, cursor_pxm);
+        xcb_free_pixmap(HostX.conn, cursor_pxm);
     }
 
     for (index = 0; index < HostX.n_screens; index++) {
         HostX.screens[index].ximg = NULL;
     }
-    /* Try to get share memory ximages for a little bit more speed */
 
-    if (!XShmQueryExtension(HostX.dpy) || getenv("XEPHYR_NO_SHM")) {
+    /* Try to get share memory ximages for a little bit more speed */
+    shm_rep = xcb_get_extension_data(HostX.conn, &xcb_shm_id);
+    if (!shm_rep || !shm_rep->present || getenv("XEPHYR_NO_SHM")) {
         fprintf(stderr, "\nXephyr unable to use SHM XImages\n");
         HostX.have_shm = False;
     }
     else {
-        /* Really really check we have shm - better way ? */
-        XShmSegmentInfo shminfo;
+        /* Really really check we have shm - better way ?*/
+        xcb_shm_segment_info_t shminfo;
+        xcb_generic_error_t *e;
+        xcb_void_cookie_t cookie;
+        xcb_shm_seg_t shmseg;
 
         HostX.have_shm = True;
 
-        shminfo.shmid = shmget(IPC_PRIVATE, 1, IPC_CREAT | 0777);
-        shminfo.shmaddr = shmat(shminfo.shmid, 0, 0);
-        shminfo.readOnly = True;
+        shminfo.shmid = shmget(IPC_PRIVATE, 1, IPC_CREAT|0777);
+        shminfo.shmaddr = shmat(shminfo.shmid,0,0);
 
-        hostx_errors_trap();
+        shmseg = xcb_generate_id(HostX.conn);
+        cookie = xcb_shm_attach_checked(HostX.conn, shmseg, shminfo.shmid,
+                                        True);
+        e = xcb_request_check(HostX.conn, cookie);
 
-        XShmAttach(HostX.dpy, &shminfo);
-        XSync(HostX.dpy, False);
-
-        if (hostx_errors_untrap()) {
+        if (e) {
             fprintf(stderr, "\nXephyr unable to use SHM XImages\n");
             HostX.have_shm = False;
+            free(e);
         }
 
         shmdt(shminfo.shmaddr);
         shmctl(shminfo.shmid, IPC_RMID, 0);
     }
 
-    XFlush(HostX.dpy);
+    xcb_flush(HostX.conn);
 
     /* Setup the pause time between paints when debugging updates */
 
@@ -622,9 +678,7 @@ hostx_screen_init(EphyrScreenInfo screen,
                   int width, int height, int buffer_height,
                   int *bytes_per_line, int *bits_per_pixel)
 {
-    int bitmap_pad;
     Bool shm_success = False;
-    XSizeHints *size_hints;
 
     struct EphyrHostScreen *host_screen = host_screen_from_screen_info(screen);
 
@@ -642,8 +696,8 @@ hostx_screen_init(EphyrScreenInfo screen,
          */
 
         if (HostX.have_shm) {
-            XShmDetach(HostX.dpy, &host_screen->shminfo);
-            XDestroyImage(host_screen->ximg);
+            xcb_shm_detach(HostX.conn, host_screen->shminfo.shmseg);
+            xcb_image_destroy (host_screen->ximg);
             shmdt(host_screen->shminfo.shmaddr);
             shmctl(host_screen->shminfo.shmid, IPC_RMID, 0);
         }
@@ -651,70 +705,78 @@ hostx_screen_init(EphyrScreenInfo screen,
             free(host_screen->ximg->data);
             host_screen->ximg->data = NULL;
 
-            XDestroyImage(host_screen->ximg);
+            xcb_image_destroy(host_screen->ximg);
         }
     }
 
     if (HostX.have_shm) {
-        host_screen->ximg =
-            XShmCreateImage(HostX.dpy, HostX.visual, HostX.depth, ZPixmap, NULL,
-                            &host_screen->shminfo, width, buffer_height);
+        host_screen->ximg = xcb_image_create_native(HostX.conn,
+                                                    width,
+                                                    buffer_height,
+                                                    XCB_IMAGE_FORMAT_Z_PIXMAP,
+                                                    HostX.depth,
+                                                    NULL,
+                                                    ~0,
+                                                    NULL);
 
         host_screen->shminfo.shmid =
             shmget(IPC_PRIVATE,
-                   host_screen->ximg->bytes_per_line * buffer_height,
+                   host_screen->ximg->stride * buffer_height,
                    IPC_CREAT | 0777);
         host_screen->ximg->data = shmat(host_screen->shminfo.shmid, 0, 0);
         host_screen->shminfo.shmaddr = host_screen->ximg->data;
 
-        if (host_screen->ximg->data == (char *) -1) {
+        if (host_screen->ximg->data == (uint8_t *) -1) {
             EPHYR_DBG
                 ("Can't attach SHM Segment, falling back to plain XImages");
             HostX.have_shm = False;
-            XDestroyImage(host_screen->ximg);
+            xcb_image_destroy (host_screen->ximg);
             shmctl(host_screen->shminfo.shmid, IPC_RMID, 0);
         }
         else {
             EPHYR_DBG("SHM segment attached %p", host_screen->shminfo.shmaddr);
-            host_screen->shminfo.readOnly = False;
-            XShmAttach(HostX.dpy, &host_screen->shminfo);
+            host_screen->shminfo.shmseg = xcb_generate_id(HostX.conn);
+            xcb_shm_attach(HostX.conn,
+                           host_screen->shminfo.shmseg,
+                           host_screen->shminfo.shmid,
+                           False);
             shm_success = True;
         }
     }
 
     if (!shm_success) {
-        bitmap_pad = (HostX.depth > 16) ? 32 : ((HostX.depth > 8) ? 16 : 8);
-
         EPHYR_DBG("Creating image %dx%d for screen host_screen=%p\n",
                   width, buffer_height, host_screen);
-        host_screen->ximg = XCreateImage(HostX.dpy,
-                                         HostX.visual,
-                                         HostX.depth,
-                                         ZPixmap, 0, 0,
-                                         width, buffer_height, bitmap_pad, 0);
+        host_screen->ximg = xcb_image_create_native(HostX.conn,
+                                                    width,
+                                                    buffer_height,
+                                                    XCB_IMAGE_FORMAT_Z_PIXMAP,
+                                                    HostX.depth,
+                                                    NULL,
+                                                    ~0,
+                                                    NULL);
 
         host_screen->ximg->data =
-            malloc(host_screen->ximg->bytes_per_line * buffer_height);
+            malloc(host_screen->ximg->stride * buffer_height);
     }
 
-    *bytes_per_line = host_screen->ximg->bytes_per_line;
-    *bits_per_pixel = host_screen->ximg->bits_per_pixel;
-
-    XResizeWindow(HostX.dpy, host_screen->win, width, height);
+    *bytes_per_line = host_screen->ximg->stride;
+    *bits_per_pixel = host_screen->ximg->bpp;
 
-    /* Ask the WM to keep our size static */
     if (host_screen->win_pre_existing == None && !EphyrWantResize) {
-        size_hints = XAllocSizeHints();
-        size_hints->max_width = size_hints->min_width = width;
-        size_hints->max_height = size_hints->min_height = height;
-        size_hints->flags = PMinSize | PMaxSize;
-        XSetWMNormalHints(HostX.dpy, host_screen->win, size_hints);
-        XFree(size_hints);
+        /* Ask the WM to keep our size static */
+        xcb_size_hints_t size_hints = {0};
+        size_hints.max_width = size_hints.min_width = width;
+        size_hints.max_height = size_hints.min_height = height;
+        size_hints.flags = (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE |
+                            XCB_ICCCM_SIZE_HINT_P_MAX_SIZE);
+        xcb_icccm_set_wm_normal_hints(HostX.conn, host_screen->win,
+                                      &size_hints);
     }
 
-    XMapWindow(HostX.dpy, host_screen->win);
+    xcb_map_window(HostX.conn, host_screen->win);
 
-    XSync(HostX.dpy, False);
+    xcb_aux_sync(HostX.conn);
 
     host_screen->win_width = width;
     host_screen->win_height = height;
@@ -787,14 +849,15 @@ hostx_paint_rect(EphyrScreenInfo screen,
 
                     host_pixel = (r << 16) | (g << 8) | (b);
 
-                    XPutPixel(host_screen->ximg, x, y, host_pixel);
+                    xcb_image_put_pixel(host_screen->ximg, x, y, host_pixel);
                     break;
                 }
                 case 8:
                 {
                     unsigned char pixel =
                         *(unsigned char *) (host_screen->fb_data + idx);
-                    XPutPixel(host_screen->ximg, x, y, HostX.cmap[pixel]);
+                    xcb_image_put_pixel(host_screen->ximg, x, y,
+                                        HostX.cmap[pixel]);
                     break;
                 }
                 default:
@@ -804,16 +867,17 @@ hostx_paint_rect(EphyrScreenInfo screen,
     }
 
     if (HostX.have_shm) {
-        XShmPutImage(HostX.dpy, host_screen->win,
-                     HostX.gc, host_screen->ximg,
-                     sx, sy, dx, dy, width, height, False);
+        xcb_image_shm_put(HostX.conn, host_screen->win,
+                          HostX.gc, host_screen->ximg,
+                          host_screen->shminfo,
+                          sx, sy, dx, dy, width, height, False);
     }
     else {
-        XPutImage(HostX.dpy, host_screen->win, HostX.gc, host_screen->ximg,
-                  sx, sy, dx, dy, width, height);
+        xcb_image_put(HostX.conn, host_screen->win, HostX.gc, host_screen->ximg,
+                      dx, dy, 0);
     }
 
-    XSync(HostX.dpy, False);
+    xcb_aux_sync(HostX.conn);
 }
 
 static void
@@ -821,6 +885,9 @@ hostx_paint_debug_rect(struct EphyrHostScreen *host_screen,
                        int x, int y, int width, int height)
 {
     struct timespec tspec;
+    xcb_rectangle_t rect = { .x = x, .y = y, .width = width, .height = height };
+    xcb_void_cookie_t cookie;
+    xcb_generic_error_t *e;
 
     tspec.tv_sec = HostX.damage_debug_msec / (1000000);
     tspec.tv_nsec = (HostX.damage_debug_msec % 1000000) * 1000;
@@ -830,8 +897,10 @@ hostx_paint_debug_rect(struct EphyrHostScreen *host_screen,
 
     /* fprintf(stderr, "Xephyr updating: %i+%i %ix%i\n", x, y, width, height); */
 
-    XFillRectangle(HostX.dpy, host_screen->win, HostX.gc, x, y, width, height);
-    XSync(HostX.dpy, False);
+    cookie = xcb_poly_fill_rectangle_checked(HostX.conn, host_screen->win,
+                                             HostX.gc, 1, &rect);
+    e = xcb_request_check(HostX.conn, cookie);
+    free(e);
 
     /* nanosleep seems to work better than usleep for me... */
     nanosleep(&tspec, NULL);
@@ -1023,6 +1092,18 @@ hostx_get_display(void)
     return HostX.dpy;
 }
 
+xcb_connection_t *
+hostx_get_xcbconn(void)
+{
+    return HostX.conn;
+}
+
+int
+hostx_get_screen(void)
+{
+    return DefaultScreen(HostX.dpy);
+}
+
 int
 hostx_get_window(int a_screen_number)
 {
@@ -1036,19 +1117,24 @@ hostx_get_window(int a_screen_number)
 int
 hostx_get_window_attributes(int a_window, EphyrHostWindowAttributes * a_attrs)
 {
-    XWindowAttributes attrs;
-
-    memset(&attrs, 0, sizeof(attrs));
-
-    if (!XGetWindowAttributes(hostx_get_display(), a_window, &attrs)) {
-        return FALSE;
-    }
-    a_attrs->x = attrs.x;
-    a_attrs->y = attrs.y;
-    a_attrs->width = attrs.width;
-    a_attrs->height = attrs.height;
-    if (attrs.visual)
-        a_attrs->visualid = attrs.visual->visualid;
+    xcb_get_geometry_cookie_t geom_cookie;
+    xcb_get_window_attributes_cookie_t attr_cookie;
+    xcb_get_geometry_reply_t *geom_reply;
+    xcb_get_window_attributes_reply_t *attr_reply;
+
+    geom_cookie = xcb_get_geometry(HostX.conn, a_window);
+    attr_cookie = xcb_get_window_attributes(HostX.conn, a_window);
+    geom_reply = xcb_get_geometry_reply(HostX.conn, geom_cookie, NULL);
+    attr_reply = xcb_get_window_attributes_reply(HostX.conn, attr_cookie, NULL);
+
+    a_attrs->x = geom_reply->x;
+    a_attrs->y = geom_reply->y;
+    a_attrs->width = geom_reply->width;
+    a_attrs->height = geom_reply->height;
+    a_attrs->visualid = attr_reply->visual;
+
+    free(geom_reply);
+    free(attr_reply);
     return TRUE;
 }
 
@@ -1120,54 +1206,56 @@ hostx_create_window(int a_screen_number,
                     int a_visual_id, int *a_host_peer /*out parameter */ )
 {
     Bool is_ok = FALSE;
-    Display *dpy = hostx_get_display();
-    XVisualInfo *visual_info = NULL, visual_info_templ;
-    int visual_mask = VisualIDMask;
-    Window win = None;
-    int nb_visuals = 0, winmask = 0;
-    XSetWindowAttributes attrs;
-
-    EPHYR_RETURN_VAL_IF_FAIL(dpy && a_geometry, FALSE);
+    xcb_window_t win;
+    int winmask = 0;
+    uint32_t attrs[2];
+    xcb_screen_t *screen = xcb_aux_get_screen(HostX.conn, hostx_get_screen());
+    xcb_visualtype_t *visual;
+    int depth = 0;
+    EPHYR_RETURN_VAL_IF_FAIL(screen && a_geometry, FALSE);
 
     EPHYR_LOG("enter\n");
 
-    /*get visual */
-    memset(&visual_info, 0, sizeof(visual_info));
-    visual_info_templ.visualid = a_visual_id;
-    visual_info = XGetVisualInfo(dpy, visual_mask,
-                                 &visual_info_templ, &nb_visuals);
-    if (!visual_info) {
-        EPHYR_LOG_ERROR("argh, could not find a remote visual with id:%d\n",
-                        a_visual_id);
-        goto out;
-    }
-    memset(&attrs, 0, sizeof(attrs));
-    attrs.colormap = XCreateColormap(dpy,
-                                     RootWindow(dpy,
-                                                visual_info->screen),
-                                     visual_info->visual, AllocNone);
-    attrs.event_mask = ButtonPressMask
-        | ButtonReleaseMask
-        | PointerMotionMask | KeyPressMask | KeyReleaseMask | ExposureMask;
-    winmask = CWColormap | CWEventMask;
-
-    win = XCreateWindow(dpy, hostx_get_window(a_screen_number),
-                        a_geometry->x, a_geometry->y,
-                        a_geometry->width, a_geometry->height, 0,
-                        visual_info->depth, CopyFromParent,
-                        visual_info->visual, winmask, &attrs);
-    if (win == None) {
-        EPHYR_LOG_ERROR("failed to create peer window\n");
+    visual = xcb_aux_find_visual_by_id(screen, a_visual_id);
+    if (!visual) {
+        EPHYR_LOG_ERROR ("argh, could not find a remote visual with id:%d\n",
+                         a_visual_id);
         goto out;
     }
-    if (HostX.screens[a_screen_number].peer_win == None) {
+    depth = xcb_aux_get_depth_of_visual(screen, a_visual_id);
+
+    winmask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
+    attrs[0] = XCB_EVENT_MASK_BUTTON_PRESS
+              |XCB_EVENT_MASK_BUTTON_RELEASE
+              |XCB_EVENT_MASK_POINTER_MOTION
+              |XCB_EVENT_MASK_KEY_PRESS
+              |XCB_EVENT_MASK_KEY_RELEASE
+              |XCB_EVENT_MASK_EXPOSURE;
+    attrs[1] = xcb_generate_id(HostX.conn);
+    xcb_create_colormap(HostX.conn,
+                        XCB_COLORMAP_ALLOC_NONE,
+                        attrs[1],
+                        hostx_get_window(a_screen_number),
+                        a_visual_id);
+
+    win = xcb_generate_id(HostX.conn);
+    xcb_create_window(HostX.conn,
+                      depth,
+                      win,
+                      hostx_get_window (a_screen_number),
+                      a_geometry->x, a_geometry->y,
+                      a_geometry->width, a_geometry->height, 0,
+                      XCB_WINDOW_CLASS_COPY_FROM_PARENT,
+                      a_visual_id, winmask, attrs);
+
+    if (HostX.screens[a_screen_number].peer_win == XCB_NONE) {
         HostX.screens[a_screen_number].peer_win = win;
     }
     else {
         EPHYR_LOG_ERROR("multiple peer windows created for same screen\n");
     }
-    XFlush(dpy);
-    XMapWindow(dpy, win);
+    xcb_flush(HostX.conn);
+    xcb_map_window(HostX.conn, win);
     *a_host_peer = win;
     is_ok = TRUE;
  out:
@@ -1178,26 +1266,31 @@ hostx_create_window(int a_screen_number,
 int
 hostx_destroy_window(int a_win)
 {
-    Display *dpy = hostx_get_display();
-
-    EPHYR_RETURN_VAL_IF_FAIL(dpy, FALSE);
-    XDestroyWindow(dpy, a_win);
-    XFlush(dpy);
+    xcb_destroy_window(HostX.conn, a_win);
+    xcb_flush(HostX.conn);
     return TRUE;
 }
 
 int
 hostx_set_window_geometry(int a_win, EphyrBox * a_geo)
 {
-    Display *dpy = hostx_get_display();
+    uint32_t mask = XCB_CONFIG_WINDOW_X
+                  | XCB_CONFIG_WINDOW_Y
+                  | XCB_CONFIG_WINDOW_WIDTH
+                  | XCB_CONFIG_WINDOW_HEIGHT;
+    uint32_t values[4];
 
-    EPHYR_RETURN_VAL_IF_FAIL(dpy && a_geo, FALSE);
+    EPHYR_RETURN_VAL_IF_FAIL(a_geo, FALSE);
 
     EPHYR_LOG("enter. x,y,w,h:(%d,%d,%d,%d)\n",
               a_geo->x, a_geo->y, a_geo->width, a_geo->height);
 
-    XMoveWindow(dpy, a_win, a_geo->x, a_geo->y);
-    XResizeWindow(dpy, a_win, a_geo->width, a_geo->height);
+    values[0] = a_geo->x;
+    values[1] = a_geo->y;
+    values[2] = a_geo->width;
+    values[3] = a_geo->height;
+    xcb_configure_window(HostX.conn, a_win, mask, values);
+
     EPHYR_LOG("leave\n");
     return TRUE;
 }
@@ -1271,13 +1364,10 @@ hostx_set_window_clipping_rectangles(int a_window,
 int
 hostx_has_xshape(void)
 {
-    int event_base = 0, error_base = 0;
-    Display *dpy = hostx_get_display();
+    const xcb_query_extension_reply_t *rep;
 
-    if (!XShapeQueryExtension(dpy, &event_base, &error_base)) {
-        return FALSE;
-    }
-    return TRUE;
+    rep = xcb_get_extension_data(HostX.conn, &xcb_shape_id);
+    return rep && rep->present;
 }
 
 #ifdef XF86DRI
@@ -1296,7 +1386,6 @@ hostx_allocate_resource_id_peer(int a_local_resource_id,
 {
     int i = 0;
     ResourcePair *peer = NULL;
-    Display *dpy = hostx_get_display();
 
     /*
      * first make sure a resource peer
@@ -1321,7 +1410,7 @@ hostx_allocate_resource_id_peer(int a_local_resource_id,
             }
         }
         if (peer) {
-            peer->remote_id = XAllocID(dpy);
+            peer->remote_id = xcb_generate_id(HostX.conn);
             peer->local_id = a_local_resource_id;
             peer->is_valid = TRUE;
         }
@@ -1356,28 +1445,21 @@ hostx_get_resource_id_peer(int a_local_resource_id, int *a_remote_resource_id)
 int
 hostx_has_dri(void)
 {
-    int event_base = 0, error_base = 0;
-    Display *dpy = hostx_get_display();
+    const xcb_query_extension_reply_t *dri;
 
-    if (!dpy)
-        return FALSE;
+    dri = xcb_get_extension_data(HostX.conn, &xcb_xf86dri_id);
 
-    if (!XF86DRIQueryExtension(dpy, &event_base, &error_base)) {
-        return FALSE;
-    }
-    return TRUE;
+    return dri && dri->present;
 }
 
 int
 hostx_has_glx(void)
 {
-    Display *dpy = hostx_get_display();
-    int event_base = 0, error_base = 0;
+    const xcb_query_extension_reply_t *glx;
 
-    if (!glXQueryExtension(dpy, &event_base, &error_base)) {
-        return FALSE;
-    }
-    return TRUE;
+    glx = xcb_get_extension_data(HostX.conn, &xcb_glx_id);
+
+    return glx && glx->present;
 }
 
 #endif                          /* XF86DRI */
diff --git a/hw/kdrive/ephyr/hostx.h b/hw/kdrive/ephyr/hostx.h
index f165606..c065f2d 100644
--- a/hw/kdrive/ephyr/hostx.h
+++ b/hw/kdrive/ephyr/hostx.h
@@ -28,6 +28,7 @@
 
 #include <X11/X.h>
 #include <X11/Xmd.h>
+#include <xcb/xcb.h>
 
 #define EPHYR_WANT_DEBUG 0
 
@@ -206,6 +207,12 @@ int
 
 void *hostx_get_display(void);
 
+xcb_connection_t *
+hostx_get_xcbconn(void);
+
+int
+hostx_get_screen(void);
+
 int
  hostx_get_window(int a_screen_number);
 
-- 
1.8.4.rc3



More information about the xorg-devel mailing list