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
master
Pierre Le Marre 2023-12-14 08:19:16 +01:00 committed by Wismill
parent 31ebe0037b
commit 4a92f61b5c
4 changed files with 26 additions and 18 deletions

View File

@ -40,6 +40,7 @@
#include "xkbcommon/xkbcommon.h" #include "xkbcommon/xkbcommon.h"
#include "utils.h" #include "utils.h"
#include "utf8.h" #include "utf8.h"
#include "keysym.h"
#define NO_KEYSYM_UNICODE_CONVERSION 0 #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 * ways. However, changing this after a couple of decades probably won't
* go well, so it stays as it is. * go well, so it stays as it is.
*/ */
if (0x01000000 <= keysym && keysym <= 0x0110ffff) if (XKB_KEYSYM_UNICODE_OFFSET <= keysym && keysym <= XKB_KEYSYM_UNICODE_MAX)
return keysym - 0x01000000; return keysym - XKB_KEYSYM_UNICODE_OFFSET;
/* search main table */ /* search main table */
return bin_search(keysymtab, ARRAY_SIZE(keysymtab) - 1, keysym); return bin_search(keysymtab, ARRAY_SIZE(keysymtab) - 1, keysym);
@ -920,7 +921,7 @@ xkb_utf32_to_keysym(uint32_t ucs)
return keysymtab[i].keysym; return keysymtab[i].keysym;
/* Use direct encoding if everything else fails */ /* Use direct encoding if everything else fails */
return ucs | 0x01000000; return ucs | XKB_KEYSYM_UNICODE_OFFSET;
} }
/* /*

View File

@ -82,7 +82,7 @@ xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size)
} }
/* Unnamed Unicode codepoint. */ /* 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; const int width = (ks & 0xff0000UL) ? 8 : 4;
return snprintf(buffer, size, "U%0*lX", width, ks & 0xffffffUL); 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; return (xkb_keysym_t) val;
if (val > 0x10ffff) if (val > 0x10ffff)
return XKB_KEY_NoSymbol; 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'))) { else if (name[0] == '0' && (name[1] == 'x' || (icase && name[1] == 'X'))) {
if (!parse_keysym_hex(&name[2], &val)) 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 */ /* Unicode keysym */
if ((sym & 0xff000000) == 0x01000000) { if ((sym & 0xff000000) == XKB_KEYSYM_UNICODE_OFFSET) {
UCSConvertCase((sym & 0x00ffffff), lower, upper); UCSConvertCase((sym & 0x00ffffff), lower, upper);
*upper |= 0x01000000; *upper |= XKB_KEYSYM_UNICODE_OFFSET;
*lower |= 0x01000000; *lower |= XKB_KEYSYM_UNICODE_OFFSET;
return; return;
} }

View File

@ -56,7 +56,13 @@
* check min keysym when previously there was no reason to. * check min keysym when previously there was no reason to.
*/ */
/** Minimum keysym value */ /** 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 bool
xkb_keysym_is_lower(xkb_keysym_t keysym); xkb_keysym_is_lower(xkb_keysym_t keysym);

View File

@ -166,9 +166,10 @@ main(void)
assert(test_string("U009f", XKB_KEY_NoSymbol)); assert(test_string("U009f", XKB_KEY_NoSymbol));
assert(test_string("U00a0", 0x00000a0)); assert(test_string("U00a0", 0x00000a0));
assert(test_string("U00ff", 0x00000ff)); assert(test_string("U00ff", 0x00000ff));
assert(test_string("U0100", XKB_KEYSYM_UNICODE_MIN));
assert(test_string("U4567", 0x1004567)); assert(test_string("U4567", 0x1004567));
assert(test_string("U1F4A9", 0x0101F4A9)); 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)); assert(test_string("U110000", XKB_KEY_NoSymbol));
/* Unicode: test syntax */ /* Unicode: test syntax */
assert(test_string("U00004567", 0x1004567)); /* OK: 8 digits */ assert(test_string("U00004567", 0x1004567)); /* OK: 8 digits */
@ -189,8 +190,8 @@ main(void)
assert(test_string("0x1", 0x00000001)); assert(test_string("0x1", 0x00000001));
assert(test_string("0x01234567", 0x01234567)); assert(test_string("0x01234567", 0x01234567));
assert(test_string("0x09abcdef", 0x09abcdef)); assert(test_string("0x09abcdef", 0x09abcdef));
assert(test_string("0x01000100", 0x01000100)); /* Min Unicode. */ assert(test_string("0x01000100", XKB_KEYSYM_UNICODE_MIN)); /* Min Unicode. */
assert(test_string("0x0110ffff", 0x0110ffff)); /* Max 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(STRINGIFY2(XKB_KEYSYM_MAX), XKB_KEYSYM_MAX)); /* Max keysym. */
assert(test_string("0x20000000", XKB_KEY_NoSymbol)); assert(test_string("0x20000000", XKB_KEY_NoSymbol));
assert(test_string("0xffffffff", XKB_KEY_NoSymbol)); assert(test_string("0xffffffff", XKB_KEY_NoSymbol));
@ -215,16 +216,16 @@ main(void)
assert(test_keysym(0x1008FF56, "XF86Close")); assert(test_keysym(0x1008FF56, "XF86Close"));
assert(test_keysym(0x0, "NoSymbol")); assert(test_keysym(0x0, "NoSymbol"));
assert(test_keysym(0x1008FE20, "XF86Ungrab")); assert(test_keysym(0x1008FE20, "XF86Ungrab"));
assert(test_keysym(0x01000000, "0x01000000")); assert(test_keysym(XKB_KEYSYM_UNICODE_OFFSET, "0x01000000"));
/* Min Unicode */ /* Min Unicode */
assert(test_keysym(0x01000100, "U0100")); assert(test_keysym(XKB_KEYSYM_UNICODE_MIN, "U0100"));
assert(test_keysym(0x01001234, "U1234")); assert(test_keysym(0x01001234, "U1234"));
/* 16-bit unicode padded to width 4. */ /* 16-bit unicode padded to width 4. */
assert(test_keysym(0x010002DE, "U02DE")); assert(test_keysym(0x010002DE, "U02DE"));
/* 32-bit unicode padded to width 8. */ /* 32-bit unicode padded to width 8. */
assert(test_keysym(0x0101F4A9, "U0001F4A9")); assert(test_keysym(0x0101F4A9, "U0001F4A9"));
/* Max Unicode */ /* Max Unicode */
assert(test_keysym(0x0110ffff, "U0010FFFF")); assert(test_keysym(XKB_KEYSYM_UNICODE_MAX, "U0010FFFF"));
/* Max Unicode + 1 */ /* Max Unicode + 1 */
assert(test_keysym(0x01110000, "0x01110000")); assert(test_keysym(0x01110000, "0x01110000"));
/* Min keysym. */ /* Min keysym. */
@ -293,14 +294,14 @@ main(void)
assert(test_utf8(XKB_KEY_KP_Subtract, "-")); assert(test_utf8(XKB_KEY_KP_Subtract, "-"));
/* Unicode keysyms */ /* 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(0x1000001, "\x01")); /* Currently accepted, but not intended (< 0x100100) */
assert(test_utf8(0x1000020, " ")); /* 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(0x100007f, "\x7f")); /* Currently accepted, but not intended (< 0x100100) */
assert(test_utf8(0x10000a0, "\xc2\xa0")); /* 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(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(0x0100d800, NULL) == 0); // Unicode surrogates
assert(test_utf8(0x0100dfff, NULL) == 0); // Unicode surrogates assert(test_utf8(0x0100dfff, NULL) == 0); // Unicode surrogates
assert(test_utf8(0x1110000, NULL) == 0); assert(test_utf8(0x1110000, NULL) == 0);