Skip to content

Commit

Permalink
libkeymap: Use unicode macro more widely
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
legionus committed Feb 14, 2025
1 parent ba98008 commit 97548dd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/libkeymap/analyze.l
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/libkeymap/contextP.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include "keymap.h"

#define UNICODE_MASK 0xf000
#define U(x) ((x) ^ UNICODE_MASK)

/**
* @brief The maximum number of include levels.
*/
Expand Down
6 changes: 2 additions & 4 deletions src/libkeymap/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include "ksyms.h"
#include "modifiers.h"

#define U(x) ((x) ^ 0xf000)

/*
* ++Geert: non-PC keyboards may generate keycode zero
*
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions src/libkeymap/ksyms.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/libkeymap/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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); }
;
Expand Down

0 comments on commit 97548dd

Please sign in to comment.