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

Julien Cristau jcristau at debian.org
Wed Oct 20 10:11:43 PDT 2010


Reviewed-by: Mikhail Gusarov <dottedmag at dottedmag.net>
Signed-off-by: Julien Cristau <jcristau at debian.org>
---
 configure.ac            |    4 +-
 hw/kdrive/ephyr/hostx.c |  654 ++++++++++++++++++++++++++---------------------
 hw/kdrive/ephyr/hostx.h |    7 +
 3 files changed, 366 insertions(+), 299 deletions(-)

diff --git a/configure.ac b/configure.ac
index baa0b5c..c91b716 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2061,12 +2061,12 @@ if test "$KDRIVE" = yes; then
        AC_DEFINE(KDRIVE_MOUSE, 1, [Enable KDrive mouse driver])
     fi
 
-    XEPHYR_REQUIRED_LIBS="x11 $LIBXEXT xau xdmcp"
+    XEPHYR_REQUIRED_LIBS="x11 $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
 
     PKG_CHECK_MODULES(XEPHYR, $XEPHYR_REQUIRED_LIBS, [xephyr="yes"], [xephyr="no"])
diff --git a/hw/kdrive/ephyr/hostx.c b/hw/kdrive/ephyr/hostx.c
index 2ebeca9..62e67e4 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>
+#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 .
@@ -79,11 +81,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 */
@@ -93,10 +95,11 @@ 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;
@@ -127,11 +130,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 *
@@ -149,28 +147,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 )
 {
@@ -229,7 +205,12 @@ hostx_set_win_title (EphyrScreenInfo screen, char *extra_text)
     return;
 
     if (ephyrTitle) {
-      XStoreName(HostX.dpy, host_screen->win, ephyrTitle);
+        xcb_set_wm_name(HostX.conn,
+                        host_screen->win,
+                        XCB_ATOM_STRING,
+                        strlen(ephyrTitle),
+                        ephyrTitle);
+
     } else {
 #define BUF_LEN 256
       char buf[BUF_LEN+1];
@@ -240,7 +221,11 @@ hostx_set_win_title (EphyrScreenInfo screen, char *extra_text)
 		host_screen->mynum,
 		(extra_text != NULL) ? extra_text : "");
 
-      XStoreName (HostX.dpy, host_screen->win, buf);
+      xcb_set_wm_name(HostX.conn,
+                      host_screen->win,
+                      XCB_ATOM_STRING,
+                      strlen(buf),
+                      buf);
     }
 }
 
@@ -283,24 +268,33 @@ 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;
 
-  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, NULL);
+  atom_WINDOW_STATE = reply->atom;
+  free(reply);
+  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);
 }
 
 
@@ -334,21 +328,25 @@ hostx_set_title (char *title)
 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;
+  char                 *tmpstr;
+  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;
 
   EPHYR_DBG("mark");
 
@@ -358,65 +356,72 @@ hostx_init (void)
       exit(1);
     }
 
+  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)
+      if (host_screen->win_pre_existing != XCB_WINDOW_NONE)
         {
-          Status            result;
-          XWindowAttributes prewin_attr;
+          xcb_get_geometry_reply_t *prewin_geom;
+	  xcb_get_geometry_cookie_t cookie;
+	  xcb_generic_error_t *e = NULL;
 
           /* 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);
 
-          hostx_errors_trap();
-
-          result = XGetWindowAttributes (HostX.dpy,
-                                         host_screen->win_pre_existing,
-                                         &prewin_attr);
-
-
-          if (hostx_errors_untrap() || !result)
+          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);
+          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)");
@@ -429,53 +434,82 @@ hostx_init (void)
               hostx_set_fullscreen_hint();
             }
 
-          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);
-
-            }
+	  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) {
+		  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);
-
-  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);
+  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);
+  }
+  {
+      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 ())
     {
       /* 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;
@@ -483,30 +517,31 @@ hostx_init (void)
   else
     {
       /* Really really check we have shm - better way ?*/
-      XShmSegmentInfo shminfo;
+      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;
+      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 (e) {
+          fprintf(stderr, "\nXephyr unable to use SHM XImages\n");
+          HostX.have_shm = False;
+          free(e);
+      }
 
-        if (hostx_errors_untrap())
-          {
-            fprintf(stderr, "\nXephyr unable to use SHM XImages\n");
-            HostX.have_shm = False;
-          }
-
-        shmdt(shminfo.shmaddr);
-        shmctl(shminfo.shmid, IPC_RMID, 0);
-}
+      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 */
 
@@ -642,7 +677,6 @@ hostx_screen_init (EphyrScreenInfo screen,
 {
   int         bitmap_pad;
   Bool        shm_success = False;
-  XSizeHints *size_hints;
 
   struct EphyrHostScreen *host_screen = host_screen_from_screen_info (screen);
   if (!host_screen)
@@ -662,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);
 	}
@@ -672,35 +706,43 @@ 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;
 	}
     }
@@ -711,32 +753,37 @@ hostx_screen_init (EphyrScreenInfo screen,
 
       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);
     }
 
-  XResizeWindow (HostX.dpy, host_screen->win, width, height);
-
-  /* Ask the WM to keep our size static */
-  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);
-
-  XMapWindow(HostX.dpy, host_screen->win);
-
-  XSync(HostX.dpy, False);
+  {
+    uint32_t cw[2] = { width, height };
+    xcb_configure_window(HostX.conn, host_screen->win,
+                         XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
+                         cw);
+  }
+  {
+    /* 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_SIZE_HINT_P_MIN_SIZE | XCB_SIZE_HINT_P_MAX_SIZE;
+    xcb_set_wm_normal_hints(HostX.conn, host_screen->win, &size_hints);
+  }
+
+  xcb_map_window(HostX.conn, host_screen->win);
+
+  xcb_aux_sync(HostX.conn);
 
   host_screen->win_width  = width;
   host_screen->win_height = height;
@@ -814,13 +861,13 @@ 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:
@@ -831,17 +878,18 @@ 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
@@ -850,6 +898,9 @@ hostx_paint_debug_rect (struct EphyrHostScreen *host_screen,
                         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;
@@ -859,8 +910,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);
@@ -869,18 +922,22 @@ hostx_paint_debug_rect (struct EphyrHostScreen *host_screen,
 void
 hostx_load_keymap(void)
 {
-  XID             *keymap;
+  xcb_keysym_t *keymap;
   int              host_width, min_keycode, max_keycode, width;
   int              i,j;
+  xcb_get_keyboard_mapping_cookie_t cookie;
+  xcb_get_keyboard_mapping_reply_t *reply;
 
   XDisplayKeycodes (HostX.dpy, &min_keycode, &max_keycode);
 
   EPHYR_DBG ("min: %d, max: %d", min_keycode, max_keycode);
 
-  keymap = XGetKeyboardMapping (HostX.dpy,
-			        min_keycode,
-			        max_keycode - min_keycode + 1,
-			        &host_width);
+  cookie = xcb_get_keyboard_mapping(HostX.conn,
+                                    min_keycode,
+                                    max_keycode - min_keycode + 1);
+  reply = xcb_get_keyboard_mapping_reply(HostX.conn, cookie, NULL);
+  host_width = reply->keysyms_per_keycode;
+  keymap = xcb_get_keyboard_mapping_keysyms(reply);
 
   /* Try and copy the hosts keymap into our keymap to avoid loads
    * of messing around.
@@ -906,7 +963,7 @@ hostx_load_keymap(void)
   ephyrKeySyms.maxKeyCode  = max_keycode;
   ephyrKeySyms.mapWidth    = width;
 
-  XFree(keymap);
+  free(reply);
 }
 
 static struct EphyrHostScreen *
@@ -1065,6 +1122,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)
 {
@@ -1078,21 +1147,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 ;
 }
 
@@ -1171,60 +1243,57 @@ hostx_create_window (int a_screen_number,
                      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) {
+    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 ;
-    }
-    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") ;
-        goto out ;
+                         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) ;
-    *a_host_peer = win ;
-    is_ok = TRUE ;
+    xcb_flush(HostX.conn);
+    xcb_map_window(HostX.conn, win);
+    *a_host_peer = win;
+    is_ok = TRUE;
 out:
     EPHYR_LOG ("leave\n") ;
     return is_ok ;
@@ -1233,27 +1302,32 @@ out:
 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 ();
-
-    EPHYR_RETURN_VAL_IF_FAIL (dpy && a_geo, FALSE) ;
+    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 (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;
 }
@@ -1331,14 +1405,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 () ;
-    if (!XShapeQueryExtension (dpy,
-                               &event_base,
-                               &error_base)) {
-        return FALSE ;
-    }
-    return TRUE;
+    const xcb_query_extension_reply_t *rep;
+    
+    rep = xcb_get_extension_data(HostX.conn, &xcb_shape_id);
+    return rep && rep->present;
 }
 
 #ifdef XF86DRI
@@ -1358,7 +1428,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
@@ -1383,7 +1452,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 ;
         }
@@ -1418,30 +1487,21 @@ hostx_get_resource_id_peer (int a_local_resource_id,
 int
 hostx_has_dri (void)
 {
-    int event_base=0, error_base=0 ;
-    Display *dpy=hostx_get_display () ;
-
-    if (!dpy)
-        return FALSE ;
-
-    if (!XF86DRIQueryExtension (dpy,
-                                &event_base,
-                                &error_base)) {
-        return FALSE ;
-    }
-    return TRUE ;
+    const xcb_query_extension_reply_t *dri;
+    
+    dri = xcb_get_extension_data(HostX.conn, &xcb_xf86dri_id);
+    
+    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 e65e0c9..8694f9a 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
 
@@ -213,6 +214,12 @@ hostx_get_event (EphyrHostXEvent *ev);
 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.7.1



More information about the xorg-devel mailing list