From 4a92f61b5c3b28edf855cff9398d5f00737f2218 Mon Sep 17 00:00:00 2001 From: Pierre Le Marre Date: Thu, 14 Dec 2023 08:19:16 +0100 Subject: [PATCH] keysyms: Add Unicode constants Add the following constants in order to improve the code readability: - XKB_KEYSYM_UNICODE_OFFSET - XKB_KEYSYM_UNICODE_MIN - XKB_KEYSYM_UNICODE_MAX --- src/keysym-utf.c | 7 ++++--- src/keysym.c | 10 +++++----- src/keysym.h | 8 +++++++- test/keysym.c | 19 ++++++++++--------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/keysym-utf.c b/src/keysym-utf.c index 0bb9a4f..46bf777 100644 --- a/src/keysym-utf.c +++ b/src/keysym-utf.c @@ -40,6 +40,7 @@ #include "xkbcommon/xkbcommon.h" #include "utils.h" #include "utf8.h" +#include "keysym.h" #define NO_KEYSYM_UNICODE_CONVERSION 0 @@ -886,8 +887,8 @@ xkb_keysym_to_utf32(xkb_keysym_t keysym) * ways. However, changing this after a couple of decades probably won't * go well, so it stays as it is. */ - if (0x01000000 <= keysym && keysym <= 0x0110ffff) - return keysym - 0x01000000; + if (XKB_KEYSYM_UNICODE_OFFSET <= keysym && keysym <= XKB_KEYSYM_UNICODE_MAX) + return keysym - XKB_KEYSYM_UNICODE_OFFSET; /* search main table */ return bin_search(keysymtab, ARRAY_SIZE(keysymtab) - 1, keysym); @@ -920,7 +921,7 @@ xkb_utf32_to_keysym(uint32_t ucs) return keysymtab[i].keysym; /* Use direct encoding if everything else fails */ - return ucs | 0x01000000; + return ucs | XKB_KEYSYM_UNICODE_OFFSET; } /* diff --git a/src/keysym.c b/src/keysym.c index ef811bd..192ba20 100644 --- a/src/keysym.c +++ b/src/keysym.c @@ -82,7 +82,7 @@ xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size) } /* Unnamed Unicode codepoint. */ - if (ks >= 0x01000100 && ks <= 0x0110ffff) { + if (ks >= XKB_KEYSYM_UNICODE_MIN && ks <= XKB_KEYSYM_UNICODE_MAX) { const int width = (ks & 0xff0000UL) ? 8 : 4; return snprintf(buffer, size, "U%0*lX", width, ks & 0xffffffUL); } @@ -203,7 +203,7 @@ xkb_keysym_from_name(const char *name, enum xkb_keysym_flags flags) return (xkb_keysym_t) val; if (val > 0x10ffff) return XKB_KEY_NoSymbol; - return (xkb_keysym_t) val | 0x01000000; + return (xkb_keysym_t) val | XKB_KEYSYM_UNICODE_OFFSET; } else if (name[0] == '0' && (name[1] == 'x' || (icase && name[1] == 'X'))) { if (!parse_keysym_hex(&name[2], &val)) @@ -677,10 +677,10 @@ XConvertCase(xkb_keysym_t sym, xkb_keysym_t *lower, xkb_keysym_t *upper) } /* Unicode keysym */ - if ((sym & 0xff000000) == 0x01000000) { + if ((sym & 0xff000000) == XKB_KEYSYM_UNICODE_OFFSET) { UCSConvertCase((sym & 0x00ffffff), lower, upper); - *upper |= 0x01000000; - *lower |= 0x01000000; + *upper |= XKB_KEYSYM_UNICODE_OFFSET; + *lower |= XKB_KEYSYM_UNICODE_OFFSET; return; } diff --git a/src/keysym.h b/src/keysym.h index e636746..fd03f89 100644 --- a/src/keysym.h +++ b/src/keysym.h @@ -56,7 +56,13 @@ * check min keysym when previously there was no reason to. */ /** Minimum keysym value */ -#define XKB_KEYSYM_MIN 0x00000000 +#define XKB_KEYSYM_MIN 0x00000000 +/** Offset to use when converting a Unicode code point to a keysym */ +#define XKB_KEYSYM_UNICODE_OFFSET 0x01000000 +/** Minimum Unicode keysym. NOTE: code points in 0..0xff cannot be converted. */ +#define XKB_KEYSYM_UNICODE_MIN 0x01000100 +/** Maximum Unicode keysym, correspoding to the maximum Unicode code point */ +#define XKB_KEYSYM_UNICODE_MAX 0x0110ffff bool xkb_keysym_is_lower(xkb_keysym_t keysym); diff --git a/test/keysym.c b/test/keysym.c index d2d1402..b266407 100644 --- a/test/keysym.c +++ b/test/keysym.c @@ -166,9 +166,10 @@ main(void) assert(test_string("U009f", XKB_KEY_NoSymbol)); assert(test_string("U00a0", 0x00000a0)); assert(test_string("U00ff", 0x00000ff)); + assert(test_string("U0100", XKB_KEYSYM_UNICODE_MIN)); assert(test_string("U4567", 0x1004567)); assert(test_string("U1F4A9", 0x0101F4A9)); - assert(test_string("U10FFFF", 0x110ffff)); /* Max Unicode */ + assert(test_string("U10FFFF", XKB_KEYSYM_UNICODE_MAX)); /* Max Unicode */ assert(test_string("U110000", XKB_KEY_NoSymbol)); /* Unicode: test syntax */ assert(test_string("U00004567", 0x1004567)); /* OK: 8 digits */ @@ -189,8 +190,8 @@ main(void) assert(test_string("0x1", 0x00000001)); assert(test_string("0x01234567", 0x01234567)); assert(test_string("0x09abcdef", 0x09abcdef)); - assert(test_string("0x01000100", 0x01000100)); /* Min Unicode. */ - assert(test_string("0x0110ffff", 0x0110ffff)); /* Max Unicode. */ + assert(test_string("0x01000100", XKB_KEYSYM_UNICODE_MIN)); /* Min Unicode. */ + assert(test_string("0x0110ffff", XKB_KEYSYM_UNICODE_MAX)); /* Max Unicode. */ assert(test_string(STRINGIFY2(XKB_KEYSYM_MAX), XKB_KEYSYM_MAX)); /* Max keysym. */ assert(test_string("0x20000000", XKB_KEY_NoSymbol)); assert(test_string("0xffffffff", XKB_KEY_NoSymbol)); @@ -215,16 +216,16 @@ main(void) assert(test_keysym(0x1008FF56, "XF86Close")); assert(test_keysym(0x0, "NoSymbol")); assert(test_keysym(0x1008FE20, "XF86Ungrab")); - assert(test_keysym(0x01000000, "0x01000000")); + assert(test_keysym(XKB_KEYSYM_UNICODE_OFFSET, "0x01000000")); /* Min Unicode */ - assert(test_keysym(0x01000100, "U0100")); + assert(test_keysym(XKB_KEYSYM_UNICODE_MIN, "U0100")); assert(test_keysym(0x01001234, "U1234")); /* 16-bit unicode padded to width 4. */ assert(test_keysym(0x010002DE, "U02DE")); /* 32-bit unicode padded to width 8. */ assert(test_keysym(0x0101F4A9, "U0001F4A9")); /* Max Unicode */ - assert(test_keysym(0x0110ffff, "U0010FFFF")); + assert(test_keysym(XKB_KEYSYM_UNICODE_MAX, "U0010FFFF")); /* Max Unicode + 1 */ assert(test_keysym(0x01110000, "0x01110000")); /* Min keysym. */ @@ -293,14 +294,14 @@ main(void) assert(test_utf8(XKB_KEY_KP_Subtract, "-")); /* Unicode keysyms */ - assert(test_utf8(0x1000000, NULL) == 0); /* Min Unicode codepoint */ + assert(test_utf8(XKB_KEYSYM_UNICODE_OFFSET, NULL) == 0); /* Min Unicode codepoint */ assert(test_utf8(0x1000001, "\x01")); /* Currently accepted, but not intended (< 0x100100) */ assert(test_utf8(0x1000020, " ")); /* Currently accepted, but not intended (< 0x100100) */ assert(test_utf8(0x100007f, "\x7f")); /* Currently accepted, but not intended (< 0x100100) */ assert(test_utf8(0x10000a0, "\xc2\xa0")); /* Currently accepted, but not intended (< 0x100100) */ - assert(test_utf8(0x1000100, "Ā")); /* Min Unicode keysym */ + assert(test_utf8(XKB_KEYSYM_UNICODE_MIN, "Ā")); /* Min Unicode keysym */ assert(test_utf8(0x10005d0, "א")); - assert(test_utf8(0x110ffff, "\xf4\x8f\xbf\xbf")); /* Max Unicode */ + assert(test_utf8(XKB_KEYSYM_UNICODE_MAX, "\xf4\x8f\xbf\xbf")); /* Max Unicode */ assert(test_utf8(0x0100d800, NULL) == 0); // Unicode surrogates assert(test_utf8(0x0100dfff, NULL) == 0); // Unicode surrogates assert(test_utf8(0x1110000, NULL) == 0);