Commit 5d31b9e3e706df2256295d5c92b74935d1e9fbcd

Ran Benita 2012-09-23T16:57:16

Add return value the xkb_keysym_get_name This is useful to see whether the function was successful and whether truncation occurred. It just changes void -> int so shouldn't break API or ABI. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/keysym.c b/src/keysym.c
index da1d219..d659354 100644
--- a/src/keysym.c
+++ b/src/keysym.c
@@ -52,7 +52,7 @@
 #include "ks_tables.h"
 #include "keysym.h"
 
-XKB_EXPORT void
+XKB_EXPORT int
 xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size)
 {
     int i, n, h, idx;
@@ -61,7 +61,7 @@ xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size)
 
     if ((ks & ((unsigned long) ~0x1fffffff)) != 0) {
         snprintf(buffer, size, "Invalid");
-        return;
+        return -1;
     }
 
     /* Try to find it in our hash table. */
@@ -79,8 +79,7 @@ xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size)
 
             if ((entry[0] == val1) && (entry[1] == val2) &&
                 (entry[2] == val3) && (entry[3] == val4)) {
-                snprintf(buffer, size, "%s", entry + 4);
-                return;
+                return snprintf(buffer, size, "%s", entry + 4);
             }
 
             if (!--n)
@@ -94,10 +93,10 @@ xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size)
 
     if (ks >= 0x01000100 && ks <= 0x0110ffff)
         /* Unnamed Unicode codepoint. */
-        snprintf(buffer, size, "U%lx", ks & 0xffffffUL);
-    else
-        /* Unnamed, non-Unicode, symbol (shouldn't generally happen). */
-        snprintf(buffer, size, "0x%08x", ks);
+        return snprintf(buffer, size, "U%lx", ks & 0xffffffUL);
+
+    /* Unnamed, non-Unicode, symbol (shouldn't generally happen). */
+    return snprintf(buffer, size, "0x%08x", ks);
 }
 
 XKB_EXPORT xkb_keysym_t
diff --git a/xkbcommon/xkbcommon.h b/xkbcommon/xkbcommon.h
index 7a97a21..a26744c 100644
--- a/xkbcommon/xkbcommon.h
+++ b/xkbcommon/xkbcommon.h
@@ -204,6 +204,12 @@ struct xkb_rule_names {
  *
  * @warning If the buffer passed is too small, the string is truncated
  * (though still NUL-terminated); a size of at least 32 bytes is recommended.
+ *
+ * @returns The number of bytes in the name, excluding the NUL byte, if
+ * keysym is valid.  Otherwise, -1 is returned.
+ *
+ * @remark You may check if truncation has occured by comparing the return
+ * value with the length of buffer, similarly to the snprintf(3) function.
  */
 int
 xkb_keysym_get_name(xkb_keysym_t keysym, char *buffer, size_t size);