[Xcb] [util] patch 1: xcb atom

Vincent Torri vtorri at univ-evry.fr
Sat Mar 8 22:03:38 PST 2008


Hey,

in order to make the util patch async and with correct namespace, here is 
a patch for the atom package, first patch of a serie.

Modifications:

  * add xcb_atom in front of each function
  * make the get_atom_name function async

May I commit ?

Vincent
-------------- next part --------------
diff --git a/atom/atoms.gperf.m4 b/atom/atoms.gperf.m4
index ec733de..c8261ea 100644
--- a/atom/atoms.gperf.m4
+++ b/atom/atoms.gperf.m4
@@ -33,7 +33,7 @@ define(`DO', `	OFFSET,define(`OFFSET', eval(OFFSET+1+len($1)))')dnl
 include(atomlist.m4)`'dnl
 };
 
-xcb_atom_t intern_atom_predefined(uint16_t name_len, const char *name)
+xcb_atom_t xcb_atom_intern_atom_predefined(uint16_t name_len, const char *name)
 {
 	const struct atom_map *value = in_word_set(name, name_len);
 	xcb_atom_t ret = XCB_NONE;
@@ -42,11 +42,11 @@ xcb_atom_t intern_atom_predefined(uint16_t name_len, const char *name)
 	return ret;
 }
 
-intern_atom_fast_cookie_t intern_atom_fast(xcb_connection_t *c, uint8_t only_if_exists, uint16_t name_len, const char *name)
+xcb_atom_intern_atom_fast_cookie_t xcb_atom_intern_atom_fast(xcb_connection_t *c, uint8_t only_if_exists, uint16_t name_len, const char *name)
 {
-	intern_atom_fast_cookie_t cookie;
+	xcb_atom_intern_atom_fast_cookie_t cookie;
 
-	if((cookie.u.atom = intern_atom_predefined(name_len, name)) != XCB_NONE)
+	if((cookie.u.atom = xcb_atom_intern_atom_predefined(name_len, name)) != XCB_NONE)
 	{
 		cookie.tag = TAG_VALUE;
 		return cookie;
@@ -57,7 +57,22 @@ intern_atom_fast_cookie_t intern_atom_fast(xcb_connection_t *c, uint8_t only_if_
 	return cookie;
 }
 
-xcb_atom_t intern_atom_fast_reply(xcb_connection_t *c, intern_atom_fast_cookie_t cookie, xcb_generic_error_t **e)
+xcb_atom_intern_atom_fast_cookie_t xcb_atom_intern_atom_fast_unchecked(xcb_connection_t *c, uint8_t only_if_exists, uint16_t name_len, const char *name)
+{
+	xcb_atom_intern_atom_fast_cookie_t cookie;
+
+	if((cookie.u.atom = xcb_atom_intern_atom_predefined(name_len, name)) != XCB_NONE)
+	{
+		cookie.tag = TAG_VALUE;
+		return cookie;
+	}
+
+	cookie.tag = TAG_COOKIE;
+	cookie.u.cookie = xcb_intern_atom(c, only_if_exists, name_len, name);
+	return cookie;
+}
+
+xcb_atom_t xcb_atom_intern_atom_fast_reply(xcb_connection_t *c, xcb_atom_intern_atom_fast_cookie_t cookie, xcb_generic_error_t **e)
 {
 	switch(cookie.tag)
 	{
@@ -80,36 +95,71 @@ xcb_atom_t intern_atom_fast_reply(xcb_connection_t *c, intern_atom_fast_cookie_t
 	return cookie.u.atom;
 }
 
-const char *get_atom_name_predefined(xcb_atom_t atom)
+const char *xcb_atom_get_atom_name_predefined(xcb_atom_t atom)
 {
 	if(atom <= 0 || atom > (sizeof(atom_name_offsets) / sizeof(*atom_name_offsets)))
 		return 0;
 	return atom_names + atom_name_offsets[atom - 1];
 }
 
-int get_atom_name(xcb_connection_t *c, xcb_atom_t atom, const char **namep, int *lengthp)
+xcb_atom_get_atom_name_cookie_t xcb_atom_get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
 {
-	static char buf[100];
-	const char *name = get_atom_name_predefined(atom);
-	int namelen;
-	xcb_get_atom_name_cookie_t atomc;
-	xcb_get_atom_name_reply_t *atomr;
+	const char *name = xcb_atom_get_atom_name_predefined(atom);
+	xcb_atom_get_atom_name_cookie_t cookie;
+
 	if(name)
 	{
-		*namep = name;
-		*lengthp = strlen(name);
-		return 1;
+		cookie.u.name = strdup(name);
+		cookie.tag = TAG_VALUE;
+		return cookie;
 	}
-	atomc = xcb_get_atom_name(c, atom);
-	atomr = xcb_get_atom_name_reply(c, atomc, 0);
-	if(!atomr)
-		return 0;
-	namelen = xcb_get_atom_name_name_length(atomr);
-	if(namelen > sizeof(buf))
-		namelen = sizeof(buf);
-	*lengthp = namelen;
-	memcpy(buf, xcb_get_atom_name_name(atomr), namelen);
-	*namep = buf;
-	free(atomr);
-	return 1;
+
+	cookie.tag = TAG_COOKIE;
+	cookie.u.cookie = xcb_get_atom_name(c, atom);
+	return cookie;
+}
+
+xcb_atom_get_atom_name_cookie_t xcb_atom_get_atom_name_unchecked(xcb_connection_t *c, xcb_atom_t atom)
+{
+	const char *name = xcb_atom_get_atom_name_predefined(atom);
+	xcb_atom_get_atom_name_cookie_t cookie;
+
+	if(name)
+	{
+		cookie.u.name = strdup(name);
+		cookie.tag = TAG_VALUE;
+		return cookie;
+	}
+
+	cookie.tag = TAG_COOKIE;
+	cookie.u.cookie = xcb_get_atom_name_unchecked(c, atom);
+	return cookie;
+}
+
+char *xcb_atom_get_atom_name_reply(xcb_connection_t *c, xcb_atom_get_atom_name_cookie_t cookie, xcb_generic_error_t **e)
+{
+	switch(cookie.tag)
+	{
+		xcb_get_atom_name_reply_t *reply;
+	case TAG_VALUE:
+		if(e)
+			*e = 0;
+		break;
+	case TAG_COOKIE:
+		reply = xcb_get_atom_name_reply(c, cookie.u.cookie, e);
+		if(reply)
+		{
+			int length;
+
+			length = xcb_get_atom_name_name_length(reply);
+			cookie.u.name = (char *)malloc (sizeof (char) * (length + 1));
+			memcpy(cookie.u.name, xcb_get_atom_name_name(reply), length);
+			cookie.u.name[length] = '\0';
+			free(reply);
+		}
+		else
+			cookie.u.name = NULL;
+		break;
+	}
+	return cookie.u.name;
 }
diff --git a/atom/xcb_atom.h.m4 b/atom/xcb_atom.h.m4
index 6ea81d3..918671e 100644
--- a/atom/xcb_atom.h.m4
+++ b/atom/xcb_atom.h.m4
@@ -11,14 +11,25 @@ typedef struct {
 		xcb_intern_atom_cookie_t cookie;
 		xcb_atom_t atom;
 	} u;
-} intern_atom_fast_cookie_t;
+} xcb_atom_intern_atom_fast_cookie_t;
 
-xcb_atom_t intern_atom_predefined(uint16_t name_len, const char *name);
-intern_atom_fast_cookie_t intern_atom_fast(xcb_connection_t *c, uint8_t only_if_exists, uint16_t name_len, const char *name);
-xcb_atom_t intern_atom_fast_reply(xcb_connection_t *c, intern_atom_fast_cookie_t cookie, xcb_generic_error_t **e);
+xcb_atom_t xcb_atom_intern_atom_predefined(uint16_t name_len, const char *name);
+xcb_atom_intern_atom_fast_cookie_t xcb_atom_intern_atom_fast(xcb_connection_t *c, uint8_t only_if_exists, uint16_t name_len, const char *name);
+xcb_atom_intern_atom_fast_cookie_t xcb_atom_intern_atom_fast_unchecked(xcb_connection_t *c, uint8_t only_if_exists, uint16_t name_len, const char *name);
+xcb_atom_t xcb_atom_intern_atom_fast_reply(xcb_connection_t *c, xcb_atom_intern_atom_fast_cookie_t cookie, xcb_generic_error_t **e);
 
-const char *get_atom_name_predefined(xcb_atom_t atom);
-int get_atom_name(xcb_connection_t *c, xcb_atom_t atom, const char **namep, int *lengthp);
+typedef struct {
+	enum tag_t tag;
+	union {
+		xcb_get_atom_name_cookie_t cookie;
+		char *name;
+	} u;
+} xcb_atom_get_atom_name_cookie_t;
+
+const char *xcb_atom_get_atom_name_predefined(xcb_atom_t atom);
+xcb_atom_get_atom_name_cookie_t xcb_atom_get_atom_name(xcb_connection_t *c, xcb_atom_t atom);
+xcb_atom_get_atom_name_cookie_t xcb_atom_get_atom_name_unchecked(xcb_connection_t *c, xcb_atom_t atom);
+char *xcb_atom_get_atom_name_reply(xcb_connection_t *c, xcb_atom_get_atom_name_cookie_t cookie, xcb_generic_error_t **e);
 
 define(`DO', `extern const xcb_atom_t $1;')dnl
 include(atomlist.m4)`'dnl
diff --git a/icccm/icccm.c b/icccm/icccm.c
index cf22a25..07183a4 100644
--- a/icccm/icccm.c
+++ b/icccm/icccm.c
@@ -868,11 +868,11 @@ xcb_set_wm_protocols_checked (xcb_connection_t *c,
                               uint32_t          list_len,
                               xcb_atom_t       *list)
 {
-	intern_atom_fast_cookie_t proto;
+	xcb_atom_intern_atom_fast_cookie_t proto;
 	xcb_atom_t WM_PROTOCOLS;
 
-	proto = intern_atom_fast(c, 0, sizeof("WM_PROTOCOLS") - 1, "WM_PROTOCOLS");
-	WM_PROTOCOLS = intern_atom_fast_reply(c, proto, 0);
+	proto = xcb_atom_intern_atom_fast(c, 0, sizeof("WM_PROTOCOLS") - 1, "WM_PROTOCOLS");
+	WM_PROTOCOLS = xcb_atom_intern_atom_fast_reply(c, proto, 0);
 
 	xcb_change_property_checked(c, XCB_PROP_MODE_REPLACE, window, WM_PROTOCOLS, ATOM, 32, list_len, list);
 }
@@ -883,11 +883,11 @@ xcb_set_wm_protocols (xcb_connection_t *c,
                       uint32_t          list_len,
                       xcb_atom_t       *list)
 {
-	intern_atom_fast_cookie_t proto;
+	xcb_atom_intern_atom_fast_cookie_t proto;
 	xcb_atom_t WM_PROTOCOLS;
 
-	proto = intern_atom_fast(c, 0, sizeof("WM_PROTOCOLS") - 1, "WM_PROTOCOLS");
-	WM_PROTOCOLS = intern_atom_fast_reply(c, proto, 0);
+	proto = xcb_atom_intern_atom_fast(c, 0, sizeof("WM_PROTOCOLS") - 1, "WM_PROTOCOLS");
+	WM_PROTOCOLS = xcb_atom_intern_atom_fast_reply(c, proto, 0);
 
 	xcb_change_property(c, XCB_PROP_MODE_REPLACE, window, WM_PROTOCOLS, ATOM, 32, list_len, list);
 }
@@ -902,12 +902,12 @@ xcb_get_wm_protocols (xcb_connection_t *c,
 	xcb_get_property_reply_t   *rep;
 	xcb_atom_t              property;
 
-	property = intern_atom_fast_reply(c,
-                                          intern_atom_fast(c,
-                                                           0,
-                                                           strlen("WM_PROTOCOLS"),
-                                                           "WM_PROTOCOLS"),
-                                          NULL);
+	property = xcb_atom_intern_atom_fast_reply(c,
+                                                   xcb_atom_intern_atom_fast(c,
+                                                                             0,
+                                                                             strlen("WM_PROTOCOLS"),
+                                                                             "WM_PROTOCOLS"),
+                                                   NULL);
 	cookie = xcb_get_property(c, 0, window,
 				property, ATOM, 0, 1000000L);
 	rep = xcb_get_property_reply(c, cookie, 0);


More information about the Xcb mailing list