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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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);