Add return value the xkb_keysym_get_name
authorRan Benita <ran234@gmail.com>
Sun, 23 Sep 2012 14:57:16 +0000 (16:57 +0200)
committerDaniel Stone <daniel@fooishbar.org>
Sun, 23 Sep 2012 23:13:32 +0000 (09:13 +1000)
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>
src/keysym.c
xkbcommon/xkbcommon.h

index da1d219..d659354 100644 (file)
@@ -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
index 7a97a21..a26744c 100644 (file)
@@ -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);