[Xcb] [PATCH 3/5] Drop Xlib native code, use x11-xcb

Julien Danjou julien at danjou.info
Mon May 24 10:01:34 PDT 2010


Signed-off-by: Julien Danjou <julien at danjou.info>
---
 configure.in         |    8 +-
 libsn/sn-common.c    |  205 ++++++++++++++------------------------
 libsn/sn-internals.h |   27 ++----
 libsn/sn-launchee.c  |    2 +-
 libsn/sn-launchee.h  |    2 +-
 libsn/sn-xmessages.c |  272 ++++++++++++++++----------------------------------
 libsn/sn-xutils.c    |   52 +++-------
 libsn/sn-xutils.h    |    4 +-
 8 files changed, 190 insertions(+), 382 deletions(-)

diff --git a/configure.in b/configure.in
index 7ca0ca2..b5d3666 100644
--- a/configure.in
+++ b/configure.in
@@ -84,11 +84,11 @@ PKG_CHECK_MODULES([xcb_aux], [xcb-aux],,
     [AC_MSG_ERROR([Cannot find xcb-aux])])
 PKG_CHECK_MODULES([xcb_event], [xcb-event],,
     [AC_MSG_ERROR([Cannot find xcb-event])])
-PKG_CHECK_MODULES([xcb_atom], [xcb-atom],,
-    [AC_MSG_ERROR([Cannot find xcb-atom])])
+PKG_CHECK_MODULES([x11_xcb], [x11-xcb],,
+    [AC_MSG_ERROR([Cannot find x11-xcb])])
 
-LIBSN_CFLAGS="$X_CFLAGS $xcb_CFLAGS $xcb_aux_CFLAGS $xcb_event_CFLAGS $xcb_atom_CFLAGS"
-LIBSN_LIBS=" $xcb_LIBS $xcb_aux_LIBS $xcb_event_LIBS $xcb_atom_LIBS $X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"
+LIBSN_CFLAGS="$xcb_CFLAGS $xcb_aux_CFLAGS $xcb_event_CFLAGS $x11_xcb_CFLAGS"
+LIBSN_LIBS="$xcb_LIBS $xcb_aux_LIBS $x11_xcb_LIBS"
 AC_SUBST(LIBSN_CFLAGS)
 AC_SUBST(LIBSN_LIBS)
 
diff --git a/libsn/sn-common.c b/libsn/sn-common.c
index 3771f1f..4d96924 100644
--- a/libsn/sn-common.c
+++ b/libsn/sn-common.c
@@ -29,28 +29,20 @@
 
 #include <xcb/xcb.h>
 #include <xcb/xcb_aux.h>
+#include <xcb/xcb_event.h>
+
+#include <X11/Xlib-xcb.h>
 
 struct SnDisplay
 {
   int refcount;
-  enum SnDisplayType type;
-  union
-  {
-      struct
-      {
-          Display *xdisplay;
-          Screen **screens;
-          SnDisplayErrorTrapPush push_trap_func;
-          SnDisplayErrorTrapPop  pop_trap_func;
-      } xlib;
-      struct
-      {
-          xcb_connection_t *xconnection;
-          xcb_screen_t **screens;
-          SnXcbDisplayErrorTrapPush push_trap_func;
-          SnXcbDisplayErrorTrapPop  pop_trap_func;
-      } xcb;
-  } x;
+  Display *xdisplay;
+  xcb_connection_t *xconnection;
+  xcb_screen_t **screens;
+  SnDisplayErrorTrapPush push_trap_func;
+  SnDisplayErrorTrapPop  pop_trap_func;
+  SnXcbDisplayErrorTrapPush xcb_push_trap_func;
+  SnXcbDisplayErrorTrapPop  xcb_pop_trap_func;
   int n_screens;
   SnList *xmessage_funcs;
   SnList *pending_messages;
@@ -80,22 +72,12 @@ sn_display_new (Display                *xdisplay,
                 SnDisplayErrorTrapPush  push_trap_func,
                 SnDisplayErrorTrapPop   pop_trap_func)
 {
-  SnDisplay *display;
-  int i;
-  
-  display = sn_new0 (SnDisplay, 1);
+  SnDisplay *display = sn_xcb_display_new(XGetXCBConnection(xdisplay),
+                                          NULL, NULL);
 
-  display->type = SN_DISPLAY_TYPE_XLIB;
-  display->x.xlib.xdisplay = xdisplay;
-  display->n_screens = ScreenCount (xdisplay);
-  display->x.xlib.screens = sn_new (Screen*, display->n_screens);
-  display->refcount = 1;
-
-  display->x.xlib.push_trap_func = push_trap_func;
-  display->x.xlib.pop_trap_func = pop_trap_func;
-
-  for (i = 0; i < display->n_screens; ++i)
-    display->x.xlib.screens[i] = ScreenOfDisplay (display->x.xlib.xdisplay, i);
+  display->xdisplay = xdisplay;
+  display->push_trap_func = push_trap_func;
+  display->pop_trap_func = pop_trap_func;
 
   return display;
 }
@@ -128,17 +110,16 @@ sn_xcb_display_new (xcb_connection_t          *xconnection,
 
   display = sn_new0 (SnDisplay, 1);
 
-  display->type = SN_DISPLAY_TYPE_XCB;
-  display->x.xcb.xconnection = xconnection;
+  display->xconnection = xconnection;
   display->n_screens = xcb_setup_roots_length (xcb_get_setup (xconnection));
-  display->x.xcb.screens = sn_new (xcb_screen_t*, display->n_screens);
+  display->screens = sn_new (xcb_screen_t*, display->n_screens);
   display->refcount = 1;
 
-  display->x.xcb.push_trap_func = push_trap_func;
-  display->x.xcb.pop_trap_func = pop_trap_func;
+  display->xcb_push_trap_func = push_trap_func;
+  display->xcb_pop_trap_func = pop_trap_func;
   
   for (i = 0; i < display->n_screens; ++i)
-    display->x.xcb.screens[i] = xcb_aux_get_screen(xconnection, i);
+    display->screens[i] = xcb_aux_get_screen(xconnection, i);
 
   return display;
 }
@@ -172,15 +153,7 @@ sn_display_unref (SnDisplay *display)
         sn_list_free (display->xmessage_funcs);
       if (display->pending_messages)
         sn_list_free (display->pending_messages);
-      switch (display->type)
-      {
-       case SN_DISPLAY_TYPE_XCB:
-        sn_free (display->x.xcb.screens);
-        break;
-       case SN_DISPLAY_TYPE_XLIB:
-        sn_free (display->x.xlib.screens);
-        break;
-      }
+      sn_free (display->screens);
       sn_free (display);
     }
 }
@@ -198,9 +171,7 @@ sn_display_unref (SnDisplay *display)
 Display*
 sn_display_get_x_display (SnDisplay *display)
 {
-  if(display->type == SN_DISPLAY_TYPE_XLIB)
-      return display->x.xlib.xdisplay;
-  return NULL;
+  return display->xdisplay;
 }
 
 /**
@@ -216,9 +187,7 @@ sn_display_get_x_display (SnDisplay *display)
 xcb_connection_t*
 sn_display_get_x_connection(SnDisplay *display)
 {
-  if(display->type == SN_DISPLAY_TYPE_XCB)
-      return display->x.xcb.xconnection;
-  return NULL;
+  return display->xconnection;
 }
 
 /**
@@ -232,14 +201,7 @@ sn_display_get_x_connection(SnDisplay *display)
 void *
 sn_internal_display_get_id (SnDisplay *display)
 {
-  switch (display->type)
-  {
-   case SN_DISPLAY_TYPE_XLIB:
-    return display->x.xlib.xdisplay;
-   case SN_DISPLAY_TYPE_XCB:
-    return display->x.xcb.xconnection;
-  }
-  return NULL;
+  return display->xconnection;
 }
 
 /**
@@ -252,14 +214,14 @@ sn_internal_display_get_id (SnDisplay *display)
  *
  * Return value: X screen or %NULL
  **/
-Screen*
+xcb_screen_t*
 sn_internal_display_get_x_screen (SnDisplay *display,
                                   int        number)
 {
-  if (display->type != SN_DISPLAY_TYPE_XLIB || number < 0 || number >= display->n_screens)
+  if (number < 0 || number >= display->n_screens)
     return NULL;
   else
-    return display->x.xlib.screens[number];
+    return display->screens[number];
 }
 
 /**
@@ -272,56 +234,16 @@ sn_internal_display_get_x_screen (SnDisplay *display,
  *
  * Return value: X root window or %NULL
  **/
-Window
+xcb_window_t
 sn_internal_display_get_root_window (SnDisplay *display,
                                      int       number)
 {
     if (number >= 0 && number < display->n_screens)
-      switch (display->type)
-      {
-       case SN_DISPLAY_TYPE_XLIB:
-        return RootWindow (display->x.xlib.xdisplay, number);
-       case SN_DISPLAY_TYPE_XCB:
-        return display->x.xcb.screens[number]->root;
-      }
+        return display->screens[number]->root;
     return None;
 }
 
 /**
- * sn_internal_display_get_type
- * @display: an #SnDisplay
- *
- *
- *
- * Return value: X connection type
- */
-enum SnDisplayType
-sn_internal_display_get_type (SnDisplay *display)
-{
-    return display->type;
-}
-
-/**
- * sn_internal_display_get_x_screen:
- * @display: an #SnDisplay
- * @number: screen number to get
- * 
- * Gets a screen by number; if the screen number
- * does not exist, returns %NULL.
- * 
- * Return value: X screen or %NULL
- **/
-xcb_screen_t*
-sn_internal_display_get_xcb_screen (SnDisplay *display,
-                                    int        number)
-{
-  if (display->type != SN_DISPLAY_TYPE_XCB || number < 0 || number >= display->n_screens)
-    return NULL;
-  else
-    return display->x.xcb.screens[number];
-}
-
-/**
  * sn_internal_display_get_screen_number:
  * @display an #SnDisplay
  *
@@ -362,9 +284,19 @@ sn_display_process_event (SnDisplay *display,
   if (sn_internal_monitor_process_event (display))
     retval = TRUE;
 
-  if (sn_internal_xmessage_process_event (display, xevent))
-    retval = TRUE;
-  
+  switch(xevent->xany.type)
+  {
+    case ClientMessage:
+      if (sn_internal_xmessage_process_client_message (display,
+                                                       xevent->xclient.window,
+                                                       xevent->xclient.message_type,
+                                                       xevent->xclient.data.b))
+          retval = TRUE;
+      break;
+    default:
+      break;
+  }
+
   return retval;
 }
 
@@ -395,8 +327,21 @@ sn_xcb_display_process_event (SnDisplay           *display,
   if (sn_internal_monitor_process_event (display))
     retval = TRUE;
 
-  if (sn_xcb_internal_xmessage_process_event (display, xevent))
-    retval = TRUE;
+  switch(XCB_EVENT_RESPONSE_TYPE(xevent))
+  {
+    case XCB_CLIENT_MESSAGE:
+      {
+        xcb_client_message_event_t *ev = (xcb_client_message_event_t *) xevent;
+        if (sn_internal_xmessage_process_client_message (display,
+                                                         ev->window,
+                                                         ev->type,
+                                                         (const char *) ev->data.data8))
+          retval = TRUE;
+      }
+      break;
+    default:
+      break;
+  }
 
   return retval;
 }
@@ -410,16 +355,16 @@ sn_xcb_display_process_event (SnDisplay           *display,
 void
 sn_display_error_trap_push (SnDisplay *display)
 {
-  switch (display->type)
+  /* SnDisplay has been created for Xlib */
+  if (display->xdisplay)
+  {
+    if (display->push_trap_func)
+      (* display->push_trap_func) (display, display->xdisplay);
+  }
+  else
   {
-   case SN_DISPLAY_TYPE_XCB:
-    if (display->x.xcb.push_trap_func)
-      (* display->x.xcb.push_trap_func) (display, display->x.xcb.xconnection);
-    break;
-   case SN_DISPLAY_TYPE_XLIB:
-    if (display->x.xlib.push_trap_func)
-      (* display->x.xlib.push_trap_func) (display, display->x.xlib.xdisplay);
-    break;
+    if (display->xcb_push_trap_func)
+      (* display->xcb_push_trap_func) (display, display->xconnection);
   }
 }
 
@@ -432,16 +377,16 @@ sn_display_error_trap_push (SnDisplay *display)
 void
 sn_display_error_trap_pop  (SnDisplay *display)
 {
-  switch (display->type)
+  /* SnDisplay has been created for Xlib */
+  if (display->xdisplay)
+  {
+    if (display->pop_trap_func)
+      (* display->pop_trap_func) (display, display->xdisplay);
+  }
+  else
   {
-   case SN_DISPLAY_TYPE_XCB:
-    if (display->x.xcb.pop_trap_func)
-      (* display->x.xcb.pop_trap_func) (display, display->x.xcb.xconnection);
-    break;
-   case SN_DISPLAY_TYPE_XLIB:
-    if (display->x.xlib.pop_trap_func)
-      (* display->x.xlib.pop_trap_func) (display, display->x.xlib.xdisplay);
-    break;
+    if (display->xcb_pop_trap_func)
+      (* display->xcb_pop_trap_func) (display, display->xconnection);
   }
 }
 
diff --git a/libsn/sn-internals.h b/libsn/sn-internals.h
index 1f7c792..3b37ec3 100644
--- a/libsn/sn-internals.h
+++ b/libsn/sn-internals.h
@@ -49,27 +49,17 @@ SN_BEGIN_DECLS
 #define NULL ((void*) 0)
 #endif
 
-enum SnDisplayType
-{
-    SN_DISPLAY_TYPE_XLIB,
-    SN_DISPLAY_TYPE_XCB
-};
-
 /* --- From sn-common.c --- */
-Screen*    sn_internal_display_get_x_screen    (SnDisplay              *display,
+xcb_screen_t* sn_internal_display_get_x_screen (SnDisplay              *display,
                                                 int                     number);
 
-Window     sn_internal_display_get_root_window (SnDisplay              *display,
-                                                int                     number);
+xcb_window_t sn_internal_display_get_root_window (SnDisplay              *display,
+                                                  int                     number);
+
 int        sn_internal_display_get_screen_number (SnDisplay *display);
 
 void*      sn_internal_display_get_id (SnDisplay *display);
 
-xcb_screen_t* sn_internal_display_get_xcb_screen (SnDisplay              *display,
-                                                  int                     number);
-
-enum SnDisplayType sn_internal_display_get_type (SnDisplay *display);
-
 void       sn_internal_display_get_xmessage_data (SnDisplay              *display,
                                                   SnList                **funcs,
                                                   SnList                **pending);
@@ -95,11 +85,10 @@ void sn_internal_append_to_string (char      **append_to,
                                    const char *append);
 
 /* --- From sn-xmessages.c --- */
-sn_bool_t sn_internal_xmessage_process_event (SnDisplay *display,
-                                              XEvent    *xevent);
-
-sn_bool_t sn_xcb_internal_xmessage_process_event (SnDisplay          *display,
-                                                  xcb_generic_event_t *xevent);
+sn_bool_t sn_internal_xmessage_process_client_message (SnDisplay  *display,
+                                                       xcb_window_t window,
+                                                       xcb_atom_t   type,
+                                                       const char  *data);
 
 SN_END_DECLS
 
diff --git a/libsn/sn-launchee.c b/libsn/sn-launchee.c
index 1c8584b..fe32338 100644
--- a/libsn/sn-launchee.c
+++ b/libsn/sn-launchee.c
@@ -217,7 +217,7 @@ sn_launchee_context_complete (SnLauncheeContext *context)
  **/
 void
 sn_launchee_context_setup_window (SnLauncheeContext *context,
-                                  Window             xwindow)
+                                  xcb_window_t       xwindow)
 {
   sn_internal_set_utf8_string (context->display,
                                xwindow,
diff --git a/libsn/sn-launchee.h b/libsn/sn-launchee.h
index a04d0ec..3f694d5 100644
--- a/libsn/sn-launchee.h
+++ b/libsn/sn-launchee.h
@@ -45,7 +45,7 @@ int                sn_launchee_context_get_id_has_timestamp (SnLauncheeContext *
 Time               sn_launchee_context_get_timestamp        (SnLauncheeContext *context);
 void               sn_launchee_context_complete             (SnLauncheeContext *context);
 void               sn_launchee_context_setup_window         (SnLauncheeContext *context,
-                                                             Window             xwindow);
+                                                             xcb_window_t       xwindow);
 
 SN_END_DECLS
 
diff --git a/libsn/sn-xmessages.c b/libsn/sn-xmessages.c
index ef18027..b1be972 100644
--- a/libsn/sn-xmessages.c
+++ b/libsn/sn-xmessages.c
@@ -34,9 +34,9 @@
 typedef struct
 {
   void          *xid;
-  Window         root;
-  Atom           type_atom;
-  Atom           type_atom_begin;
+  xcb_window_t   root;
+  xcb_atom_t     type_atom;
+  xcb_atom_t     type_atom_begin;
   char          *message_type;
   SnXmessageFunc func;
   void          *func_data;
@@ -45,8 +45,8 @@ typedef struct
 
 typedef struct
 {
-  Atom type_atom_begin;
-  Window xwindow;
+  xcb_atom_t type_atom_begin;
+  xcb_window_t xwindow;
   char *message;
   int allocated;
 } SnXmessage;
@@ -63,6 +63,14 @@ sn_internal_add_xmessage_func (SnDisplay      *display,
   SnXmessageHandler *handler;
   SnList *xmessage_funcs;
   
+  xcb_connection_t *c = sn_display_get_x_connection(display);
+
+  /* Send atom requests ASAP */
+  xcb_intern_atom_cookie_t message_type_c =
+    xcb_intern_atom(c, FALSE, strlen(message_type), message_type);
+  xcb_intern_atom_cookie_t message_type_begin_c =
+    xcb_intern_atom(c, FALSE, strlen(message_type_begin), message_type_begin);
+
   sn_internal_display_get_xmessage_data (display, &xmessage_funcs,
                                          NULL);
   
@@ -70,12 +78,12 @@ sn_internal_add_xmessage_func (SnDisplay      *display,
 
   handler->xid = sn_internal_display_get_id (display);
   handler->root = sn_internal_display_get_root_window (display, screen);
-  handler->type_atom = sn_internal_atom_get (display, message_type);
-  handler->type_atom_begin = sn_internal_atom_get (display, message_type_begin);
   handler->message_type = sn_internal_strdup (message_type);
   handler->func = func;
   handler->func_data = func_data;
   handler->free_data_func = free_data_func;
+  handler->type_atom = xcb_intern_atom_reply(c, message_type_c, NULL)->atom;
+  handler->type_atom_begin = xcb_intern_atom_reply(c, message_type_begin_c, NULL)->atom;
   
   sn_list_prepend (xmessage_funcs, handler);
 }
@@ -85,7 +93,7 @@ typedef struct
   const char *message_type;
   SnXmessageFunc func;
   void *func_data;
-  Window root;
+  xcb_window_t root;
   SnXmessageHandler *handler;
 } FindHandlerData;
 
@@ -151,147 +159,79 @@ sn_internal_broadcast_xmessage   (SnDisplay      *display,
                                   const char     *message)
 {
   if (!sn_internal_utf8_validate (message, -1))
-    {
+  {
       fprintf (stderr,
                "Attempted to send non-UTF-8 X message: %s\n",
                message);
       return;
-    }
-
-  switch (sn_internal_display_get_type (display))
-  {
-   case SN_DISPLAY_TYPE_XLIB:
-    {
-      Atom type_atom;
-      Atom type_atom_begin;
-      Window xwindow;
-      XSetWindowAttributes attrs;
-      Display *xdisplay = sn_display_get_x_display (display);
+  }
   
-      attrs.override_redirect = True;
-      attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
-
-      xwindow =
-        XCreateWindow (xdisplay,
-                       RootWindow (xdisplay, 0),
-                       -100, -100, 1, 1,
-                       0,
-                       CopyFromParent,
-                       CopyFromParent,
-                       CopyFromParent,
-                       CWOverrideRedirect | CWEventMask,
-                       &attrs);
-      type_atom = sn_internal_atom_get (display, message_type);
-      type_atom_begin = sn_internal_atom_get (display, message_type_begin);
-    
-      {
-        XEvent xevent;
-        const char *src;
-        const char *src_end;
-        char *dest;
-        char *dest_end;
-
-        xevent.xclient.type = ClientMessage;
-        xevent.xclient.message_type = type_atom_begin;
-        xevent.xclient.display = xdisplay;
-        xevent.xclient.window = xwindow;
-        xevent.xclient.format = 8;
-
-        src = message;
-        src_end = message + strlen (message) + 1; /* +1 to include nul byte */
-        
-        while (src != src_end)
-          {
-            dest = &xevent.xclient.data.b[0];
-            dest_end = dest + 20;
-
-            while (dest != dest_end &&
-                   src != src_end)
-              {
-                *dest = *src;
-                ++dest;
-                ++src;
-              }
-
-            XSendEvent (xdisplay,
-                        RootWindow (xdisplay, screen),
-                        False,
-                        PropertyChangeMask,
-                        &xevent);
-
-            xevent.xclient.message_type = type_atom;
-          }
-      }
+  xcb_connection_t *xconnection = sn_display_get_x_connection (display);
 
-      XDestroyWindow (xdisplay, xwindow);
-      XFlush (xdisplay);
-    }
-    break;
-   case SN_DISPLAY_TYPE_XCB:
-    {
-      xcb_atom_t type_atom;
-      xcb_atom_t type_atom_begin;
-      xcb_window_t xwindow;
-      uint32_t attrs[] = { 1, XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY };
-      xcb_connection_t *xconnection = sn_display_get_x_connection (display);
-      xcb_screen_t *s = sn_internal_display_get_xcb_screen (display, screen);
-
-      xwindow = xcb_generate_id(xconnection);
-      xcb_create_window(xconnection, s->root_depth, xwindow, s->root,
-                        -100, -100, 1, 1, 0, XCB_COPY_FROM_PARENT, s->root_visual,
-                        XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK,
-                        attrs);
-
-      type_atom = sn_internal_atom_get (display, message_type);
-      type_atom_begin = sn_internal_atom_get (display, message_type_begin);
+  /* Send atom requests ASAP */
+  xcb_intern_atom_cookie_t message_type_c =
+    xcb_intern_atom(xconnection, FALSE, strlen(message_type), message_type);
+  xcb_intern_atom_cookie_t message_type_begin_c =
+    xcb_intern_atom(xconnection, FALSE, strlen(message_type_begin), message_type_begin);
 
-      {
-        xcb_client_message_event_t xevent;
-        const char *src;
-        const char *src_end;
-        unsigned char *dest;
-        unsigned char *dest_end;
+  uint32_t attrs[] = { 1, XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY };
+  xcb_screen_t *s = sn_internal_display_get_x_screen (display, screen);
 
-        xevent.response_type = XCB_CLIENT_MESSAGE;
-        xevent.type = type_atom_begin;
-        xevent.window = xwindow;
-        xevent.format = 8;
+  xcb_window_t xwindow = xcb_generate_id(xconnection);
+  xcb_create_window(xconnection, s->root_depth, xwindow, s->root,
+                    -100, -100, 1, 1, 0, XCB_COPY_FROM_PARENT, s->root_visual,
+                    XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK,
+                    attrs);
 
-        src = message;
-        src_end = message + strlen (message) + 1; /* +1 to include nul byte */
+  {
+      xcb_client_message_event_t xevent;
+      const char *src;
+      const char *src_end;
+      unsigned char *dest;
+      unsigned char *dest_end;
+
+      xevent.response_type = XCB_CLIENT_MESSAGE;
+      xevent.window = xwindow;
+      xevent.format = 8;
+
+      src = message;
+      src_end = message + strlen (message) + 1; /* +1 to include nul byte */
+
+      /* Get atom reply last */
+      xcb_atom_t type_atom = xcb_intern_atom_reply(xconnection,
+                                                   message_type_c, NULL)->atom;
+      xevent.type = xcb_intern_atom_reply(xconnection,
+                                          message_type_begin_c, NULL)->atom;
+
+      while (src != src_end)
+      {
+          dest = &xevent.data.data8[0];
+          dest_end = dest + 20;
 
-        while (src != src_end)
+          while (dest != dest_end &&
+                 src != src_end)
           {
-            dest = &xevent.data.data8[0];
-            dest_end = dest + 20;
-
-            while (dest != dest_end &&
-                   src != src_end)
-              {
-                *dest = *src;
-                ++dest;
-                ++src;
-              }
+              *dest = *src;
+              ++dest;
+              ++src;
+          }
 
-            xcb_send_event (xconnection, 0, s->root, XCB_EVENT_MASK_PROPERTY_CHANGE,
-                            (char *) &xevent);
+          xcb_send_event (xconnection, 0, s->root, XCB_EVENT_MASK_PROPERTY_CHANGE,
+                          (char *) &xevent);
 
-            xevent.type = type_atom;
-          }
+          xevent.type = type_atom;
       }
-
-      xcb_destroy_window (xconnection, xwindow);
-      xcb_flush(xconnection);
-    }
-    break;
   }
+
+  xcb_destroy_window (xconnection, xwindow);
+  xcb_flush(xconnection);
 }
 
 typedef struct
 {
   void *xid;
-  Atom atom;
-  Window xwindow;
+  xcb_atom_t atom;
+  xcb_window_t xwindow;
   sn_bool_t found_handler;
 } HandlerForAtomData;
 
@@ -315,8 +255,8 @@ handler_for_atom_foreach (void *value,
 
 static sn_bool_t
 some_handler_handles_event (SnDisplay *display,
-                            Atom atom,
-                            Window win)
+                            xcb_atom_t atom,
+                            xcb_window_t win)
 {
   HandlerForAtomData hfad;
   SnList *xmessage_funcs;
@@ -339,7 +279,7 @@ some_handler_handles_event (SnDisplay *display,
 
 typedef struct
 {
-  Window window;
+  xcb_window_t window;
   SnXmessage *message;
 } FindMessageData;
 
@@ -360,7 +300,7 @@ find_message_foreach (void *value,
 }
 
 static SnXmessage*
-message_new(Atom type_atom_begin, Window win)
+message_new(xcb_atom_t type_atom_begin, xcb_window_t win)
 {
   SnXmessage *message = sn_new0 (SnXmessage, 1);
   message->type_atom_begin = type_atom_begin;
@@ -404,8 +344,8 @@ message_set_message(SnXmessage *message, const char *src)
 
 static SnXmessage*
 get_or_add_message(SnList *pending_messages,
-                   Window win,
-                   Atom type_atom_begin)
+                   xcb_window_t win,
+                   xcb_atom_t type_atom_begin)
 {
   FindMessageData fmd;
   SnXmessage *message;
@@ -431,8 +371,8 @@ get_or_add_message(SnList *pending_messages,
 
 static SnXmessage*
 add_event_to_messages (SnDisplay *display,
-                       Window win,
-                       Atom message_type,
+                       xcb_window_t win,
+                       xcb_atom_t message_type,
                        const char *data)
 {
   SnXmessage *message;
@@ -532,58 +472,20 @@ xmessage_process_message (SnDisplay *display, SnXmessage *message)
 }
 
 sn_bool_t
-sn_internal_xmessage_process_event (SnDisplay *display,
-                                    XEvent    *xevent)
+sn_internal_xmessage_process_client_message (SnDisplay   *display,
+                                             xcb_window_t window,
+                                             xcb_atom_t   type,
+                                             const char  *data)
 {
   sn_bool_t retval = FALSE;
   SnXmessage *message = NULL;
 
-  switch (xevent->xany.type)
-    {
-    case ClientMessage:
-      if (some_handler_handles_event (display,
-                                      xevent->xclient.message_type,
-                                      xevent->xclient.window))
-        {
-          retval = TRUE;
-
-          message = add_event_to_messages (display,
-                                           xevent->xclient.window,
-                                           xevent->xclient.message_type,
-                                           xevent->xclient.data.b);
-        }
-      break;
-    }
-
-  xmessage_process_message (display, message);
-
-  return retval;
-}
-
-sn_bool_t
-sn_xcb_internal_xmessage_process_event (SnDisplay           *display,
-                                        xcb_generic_event_t *xevent)
-{
-  sn_bool_t retval = FALSE;
-  SnXmessage *message = NULL;
-  xcb_client_message_event_t *ev = (xcb_client_message_event_t *) xevent;
-
-  switch (XCB_EVENT_RESPONSE_TYPE(xevent))
-    {
-    case XCB_CLIENT_MESSAGE:
-      if (some_handler_handles_event (display,
-                                      ev->type,
-                                      ev->window))
-        {
-          retval = TRUE;
+  if (some_handler_handles_event (display, type, window))
+  {
+    retval = TRUE;
 
-          message = add_event_to_messages (display,
-                                           ev->window,
-                                           ev->type,
-                                           (const char *) ev->data.data8);
-        }
-      break;
-    }
+    message = add_event_to_messages (display, window, type, data);
+  }
 
   xmessage_process_message (display, message);
 
@@ -599,7 +501,7 @@ sn_internal_append_to_string_escaped (char      **append_to,
   int len;
   char buf[2];
   const char *p;
-  
+
   buf[1] = '\0';
   len = 0;
   escaped = NULL;
diff --git a/libsn/sn-xutils.c b/libsn/sn-xutils.c
index 220f50e..03ed74c 100644
--- a/libsn/sn-xutils.c
+++ b/libsn/sn-xutils.c
@@ -24,56 +24,30 @@
 
 #include <config.h>
 #include "sn-internals.h"
-#include <X11/Xutil.h>
-#include <X11/Xatom.h>
 
 #include <xcb/xcb.h>
-#include <xcb/xcb_atom.h>
-
-Atom
-sn_internal_atom_get (SnDisplay  *display,
-                      const char *atom_name)
-{
-    switch (sn_internal_display_get_type (display))
-    {
-     case SN_DISPLAY_TYPE_XLIB:
-      return XInternAtom (sn_display_get_x_display (display),
-                          atom_name,
-                          False);
-     case SN_DISPLAY_TYPE_XCB:
-      return xcb_atom_get (sn_display_get_x_connection (display),
-                           atom_name);
-    }
-    return None;
-}
 
 void
 sn_internal_set_utf8_string (SnDisplay  *display,
-                             Window      xwindow,
+                             xcb_window_t xwindow,
                              const char *property,
                              const char *str)
 {
   sn_display_error_trap_push (display);
 
-  switch (sn_internal_display_get_type (display))
-  {
-    case SN_DISPLAY_TYPE_XLIB:
-      XChangeProperty (sn_display_get_x_display (display),
+  xcb_connection_t *c = sn_display_get_x_connection (display);
+
+  xcb_intern_atom_cookie_t atom_property_c =
+    xcb_intern_atom(c, FALSE, strlen(property), property);
+  xcb_intern_atom_cookie_t atom_utf8_string_c =
+    xcb_intern_atom(c, FALSE, sizeof("UTF8_STRING") - 1, "UTF8_STRING");
+
+  xcb_change_property (c,
+                       XCB_PROP_MODE_REPLACE,
                        xwindow,
-                       sn_internal_atom_get (display, property),
-                       sn_internal_atom_get (display, "UTF8_STRING"),
-                       8, PropModeReplace, (unsigned char*) str,
-                       strlen (str));
-      break;
-     case SN_DISPLAY_TYPE_XCB:
-      xcb_change_property (sn_display_get_x_connection (display),
-                           XCB_PROP_MODE_REPLACE,
-                           xwindow,
-                           sn_internal_atom_get (display, property),
-                           sn_internal_atom_get (display, "UTF8_STRING"),
-                           8, strlen (str), str);
-      break;
-  }
+                       xcb_intern_atom_reply(c, atom_property_c, NULL)->atom,
+                       xcb_intern_atom_reply(c, atom_utf8_string_c, NULL)->atom,
+                       8, strlen (str), str);
 
   sn_display_error_trap_pop (display);
 }
diff --git a/libsn/sn-xutils.h b/libsn/sn-xutils.h
index b28bb36..ce477f9 100644
--- a/libsn/sn-xutils.h
+++ b/libsn/sn-xutils.h
@@ -29,10 +29,8 @@
 
 SN_BEGIN_DECLS
 
-Atom sn_internal_atom_get        (SnDisplay  *display,
-                                  const char *atom_name);
 void sn_internal_set_utf8_string (SnDisplay  *display,
-                                  Window      xwindow,
+                                  xcb_window_t xwindow,
                                   const char *property,
                                   const char *str);
 
-- 
1.7.1



More information about the Xcb mailing list