From 97548dd77bbefaf90f2ee3af16c790083b2fc846 Mon Sep 17 00:00:00 2001 From: Alexey Gladkov Date: Fri, 14 Feb 2025 16:21:33 +0100 Subject: [PATCH] libkeymap: Use unicode macro more widely Unicode codes have a 0xf000 mask. We have a macro that repeats a macro in the kernel, but we don't use it everywhere in the library. Signed-off-by: Alexey Gladkov --- src/libkeymap/analyze.l | 2 +- src/libkeymap/contextP.h | 3 +++ src/libkeymap/dump.c | 6 ++---- src/libkeymap/ksyms.c | 24 ++++++++++++------------ src/libkeymap/parser.y | 8 ++++---- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/libkeymap/analyze.l b/src/libkeymap/analyze.l index 614077b6..c4a239df 100644 --- a/src/libkeymap/analyze.l +++ b/src/libkeymap/analyze.l @@ -401,7 +401,7 @@ To to|To|TO if (parse_int(yyextra, yytext, yytext + 1, 16, &(yylval->num)) < 0) return(ERROR); - if (yylval->num >= 0xf000) { + if (yylval->num >= UNICODE_MASK) { ERR(yyextra, _("unicode keysym out of range: %s"), yytext); return(ERROR); diff --git a/src/libkeymap/contextP.h b/src/libkeymap/contextP.h index 3e053c8a..7f9b2681 100644 --- a/src/libkeymap/contextP.h +++ b/src/libkeymap/contextP.h @@ -17,6 +17,9 @@ #include "keymap.h" +#define UNICODE_MASK 0xf000 +#define U(x) ((x) ^ UNICODE_MASK) + /** * @brief The maximum number of include levels. */ diff --git a/src/libkeymap/dump.c b/src/libkeymap/dump.c index 7daa7095..bef8e8bd 100644 --- a/src/libkeymap/dump.c +++ b/src/libkeymap/dump.c @@ -22,8 +22,6 @@ #include "ksyms.h" #include "modifiers.h" -#define U(x) ((x) ^ 0xf000) - /* * ++Geert: non-PC keyboards may generate keycode zero * @@ -291,7 +289,7 @@ void lk_dump_diacs(struct lk_ctx *ctx, FILE *fd) dumpchar(fd, ptr->base, 0); #ifdef KDGKBDIACRUC if (ctx->flags & LK_FLAG_PREFER_UNICODE) { - ksym = codetoksym(ctx, (int) ptr->result ^ 0xf000); + ksym = codetoksym(ctx, (int) U(ptr->result)); if (ksym) { fprintf(fd, " to %s\n", ksym); } else { @@ -381,7 +379,7 @@ print_keysym(struct lk_ctx *ctx, FILE *fd, int code, char numeric) if (!numeric && (p = codetoksym(ctx, code)) != NULL) fprintf(fd, "%-16s", p); else - fprintf(fd, "U+%04x ", code ^ 0xf000); + fprintf(fd, "U+%04x ", U(code)); return; } plus = 0; diff --git a/src/libkeymap/ksyms.c b/src/libkeymap/ksyms.c index 2bd2bd42..a46f5cf6 100644 --- a/src/libkeymap/ksyms.c +++ b/src/libkeymap/ksyms.c @@ -208,7 +208,7 @@ codetoksym(struct lk_ctx *ctx, int code) } else { /* Unicode keysym */ - code ^= 0xf000; + code = U(code); if (code < 0x80) return get_sym(ctx, KT_LATIN, code); @@ -311,7 +311,7 @@ int ksymtocode(struct lk_ctx *ctx, const char *s, int direction) if (p) { for (j = charsets[i].start; j < 256; j++, p++) { if (!strcmp(s, p->name)) - return (p->uni ^ 0xf000); + return U(p->uni); } } @@ -324,7 +324,7 @@ int ksymtocode(struct lk_ctx *ctx, const char *s, int direction) if (p) { for (j = charsets[i].start; j < 256; j++, p++) { if (!strcmp(s, p->name)) - return (p->uni ^ 0xf000); + return U(p->uni); } } } @@ -393,31 +393,31 @@ int convert_code(struct lk_ctx *ctx, int code, int direction) else if (!input_is_unicode && code < 0x80) /* basic ASCII is fine in every situation */ return code; - else if (input_is_unicode && (code ^ 0xf000) < 0x80) + else if (input_is_unicode && U(code) < 0x80) /* so is Unicode "Basic Latin" */ - return code ^ 0xf000; + return U(code); else if ((input_is_unicode && direction == TO_UNICODE) || (!input_is_unicode && direction == TO_8BIT)) /* no conversion necessary */ result = code; else { /* depending on direction, this will give us either an 8-bit - * K(KTYP, KVAL) or a Unicode keysym xor 0xf000 */ + * K(KTYP, KVAL) or a Unicode keysym xor UNICODE_MASK */ ksym = codetoksym(ctx, code); if (ksym) result = ksymtocode(ctx, ksym, direction); else result = code; - if (direction == TO_UNICODE && KTYP(code) == KT_LETTER && (result ^ 0xf000) < 0x100) { + if (direction == TO_UNICODE && KTYP(code) == KT_LETTER && U(result) < 0x100) { /* Unicode Latin-1 Supplement */ - result = K(KT_LETTER, result ^ 0xf000); + result = K(KT_LETTER, U(result)); } } /* if direction was TO_UNICODE from the beginning, we return the true - * Unicode value (without the 0xf000 mask) */ + * Unicode value (without the UNICODE_MASK) */ if (unicode_forced && result >= 0x1000) - return result ^ 0xf000; + return U(result); else return result; } @@ -426,10 +426,10 @@ int add_capslock(struct lk_ctx *ctx, int code) { if (KTYP(code) == KT_LATIN && (!(ctx->flags & LK_FLAG_PREFER_UNICODE) || code < 0x80)) return K(KT_LETTER, KVAL(code)); - else if ((code ^ 0xf000) < 0x100) + else if (U(code) < 0x100) /* Unicode Latin-1 Supplement */ /* a bit dirty to use KT_LETTER here, but it should work */ - return K(KT_LETTER, code ^ 0xf000); + return K(KT_LETTER, U(code)); else return convert_code(ctx, code, TO_AUTO); } diff --git a/src/libkeymap/parser.y b/src/libkeymap/parser.y index 49a7457c..225da024 100644 --- a/src/libkeymap/parser.y +++ b/src/libkeymap/parser.y @@ -291,8 +291,8 @@ compline : COMPOSE compsym compsym TO CCHAR EOL YYERROR; } ; -compsym : CCHAR { $$ = $1; } - | UNUMBER { $$ = $1 ^ 0xf000; } +compsym : CCHAR { $$ = $1; } + | UNUMBER { $$ = U($1); } ; singleline : KEYCODE NUMBER EQUALS rvalue0 EOL { @@ -393,8 +393,8 @@ rvalue1 : rvalue ; rvalue : NUMBER { $$ = convert_code(ctx, $1, TO_AUTO); } | PLUS NUMBER { $$ = add_capslock(ctx, $2); } - | UNUMBER { $$ = convert_code(ctx, $1^0xf000, TO_AUTO); } - | PLUS UNUMBER { $$ = add_capslock(ctx, $2^0xf000); } + | UNUMBER { $$ = convert_code(ctx, U($1), TO_AUTO); } + | PLUS UNUMBER { $$ = add_capslock(ctx, U($2)); } | LITERAL { $$ = $1; } | PLUS LITERAL { $$ = add_capslock(ctx, $2); } ;