keysyms: Add min and max assigned keysyms internal API

Currently there is no direct way to know the minimum and maximum keysym
values that are assigned, i.e. that have an explicit name or are
Unicode keysyms.

Introduce the new following internal API:
- XKB_KEYSYM_MIN_ASSIGNED
- XKB_KEYSYM_MAX_ASSIGNED
- XKB_KEYSYM_MIN_EXPLICIT
- XKB_KEYSYM_MAX_EXPLICIT
- XKB_KEYSYM_COUNT_EXPLICIT

Also add a bunch of tests to ensure consistant keysyms bounds.
master
Pierre Le Marre 2023-12-14 09:13:35 +01:00 committed by Wismill
parent 4a92f61b5c
commit 82f138c633
5 changed files with 205 additions and 0 deletions

89
scripts/update-headers.py Executable file
View File

@ -0,0 +1,89 @@
#!/usr/bin/env python3
import argparse
from pathlib import Path
import re
import sys
from typing import Any
import jinja2
KEYSYM_PATTERN = re.compile(
r"^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s"
)
def load_keysyms(path: Path) -> dict[str, int]:
# Load the keysyms header
keysym_min = sys.maxsize
keysym_max = 0
min_unicode_keysym = 0x01000100
max_unicode_keysym = 0x0110FFFF
keysyms = set()
with path.open("rt", encoding="utf-8") as fd:
for line in fd:
if m := KEYSYM_PATTERN.match(line):
value = int(m.group("value"), 16)
keysyms.add(value)
keysym_min = min(keysym_min, value)
keysym_max = max(keysym_max, value)
return {
"XKB_KEYSYM_MIN_ASSIGNED": min(keysym_min, min_unicode_keysym),
"XKB_KEYSYM_MAX_ASSIGNED": max(keysym_max, max_unicode_keysym),
"XKB_KEYSYM_MIN_EXPLICIT": keysym_min,
"XKB_KEYSYM_MAX_EXPLICIT": keysym_max,
"XKB_KEYSYM_COUNT_EXPLICIT": len(keysyms),
}
def generate(
env: jinja2.Environment,
data: dict[str, Any],
root: Path,
file: Path,
):
"""Generate a file from its Jinja2 template"""
template_path = file.with_suffix(f"{file.suffix}.jinja")
template = env.get_template(str(template_path))
path = root / file
with path.open("wt", encoding="utf-8") as fd:
fd.writelines(template.generate(**data))
# Root of the project
ROOT = Path(__file__).parent.parent
# Parse commands
parser = argparse.ArgumentParser(
description="Generate C header files related to keysyms bounds"
)
parser.add_argument(
"--root",
type=Path,
default=ROOT,
help="Path to the root of the project (default: %(default)s)",
)
args = parser.parse_args()
# Configure Jinja
template_loader = jinja2.FileSystemLoader(args.root, encoding="utf-8")
jinja_env = jinja2.Environment(
loader=template_loader,
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True,
)
jinja_env.filters["keysym"] = lambda ks: f"0x{ks:0>8x}"
# Load keysyms
keysyms_data = load_keysyms(args.root / "include/xkbcommon/xkbcommon-keysyms.h")
# Generate the files
generate(
jinja_env,
keysyms_data,
args.root,
Path("src/keysym.h"),
)

View File

@ -4,3 +4,4 @@
export LC_CTYPE=C export LC_CTYPE=C
scripts/makeheader > include/xkbcommon/xkbcommon-keysyms.h scripts/makeheader > include/xkbcommon/xkbcommon-keysyms.h
scripts/makekeys include/xkbcommon/xkbcommon-keysyms.h > src/ks_tables.h scripts/makekeys include/xkbcommon/xkbcommon-keysyms.h > src/ks_tables.h
scripts/update-headers

View File

@ -57,6 +57,16 @@
*/ */
/** Minimum keysym value */ /** Minimum keysym value */
#define XKB_KEYSYM_MIN 0x00000000 #define XKB_KEYSYM_MIN 0x00000000
/** Minimum keysym value assigned */
#define XKB_KEYSYM_MIN_ASSIGNED ((xkb_keysym_t)0x00000000)
/** Maximum keysym value assigned */
#define XKB_KEYSYM_MAX_ASSIGNED 0x1008ffb8
/** Minimum keysym value with explicit name */
#define XKB_KEYSYM_MIN_EXPLICIT 0x00000000
/** Maximum keysym value with explicit name */
#define XKB_KEYSYM_MAX_EXPLICIT 0x1008ffb8
/** Count of keysym value with explicit name */
#define XKB_KEYSYM_COUNT_EXPLICIT 2442
/** Offset to use when converting a Unicode code point to a keysym */ /** Offset to use when converting a Unicode code point to a keysym */
#define XKB_KEYSYM_UNICODE_OFFSET 0x01000000 #define XKB_KEYSYM_UNICODE_OFFSET 0x01000000
/** Minimum Unicode keysym. NOTE: code points in 0..0xff cannot be converted. */ /** Minimum Unicode keysym. NOTE: code points in 0..0xff cannot be converted. */

89
src/keysym.h.jinja Normal file
View File

@ -0,0 +1,89 @@
/*
* Copyright 1985, 1987, 1990, 1998 The Open Group
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors or their
* institutions shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization from the authors.
*/
/*
* Copyright © 2009 Dan Nicholson
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef KEYSYM_H
#define KEYSYM_H
/*
* NOTE: this is not defined in xkbcommon.h, because if we did, it may add
* overhead for library user: when handling keysyms they would also need to
* check min keysym when previously there was no reason to.
*/
/** Minimum keysym value */
#define XKB_KEYSYM_MIN 0x00000000
/** Minimum keysym value assigned */
#define XKB_KEYSYM_MIN_ASSIGNED ((xkb_keysym_t){{ XKB_KEYSYM_MIN_ASSIGNED | keysym }})
/** Maximum keysym value assigned */
#define XKB_KEYSYM_MAX_ASSIGNED {{ XKB_KEYSYM_MAX_ASSIGNED | keysym }}
/** Minimum keysym value with explicit name */
#define XKB_KEYSYM_MIN_EXPLICIT {{ XKB_KEYSYM_MIN_EXPLICIT | keysym }}
/** Maximum keysym value with explicit name */
#define XKB_KEYSYM_MAX_EXPLICIT {{ XKB_KEYSYM_MAX_EXPLICIT | keysym }}
/** Count of keysym value with explicit name */
#define XKB_KEYSYM_COUNT_EXPLICIT {{ XKB_KEYSYM_COUNT_EXPLICIT }}
/** 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);
bool
xkb_keysym_is_upper(xkb_keysym_t keysym);
bool
xkb_keysym_is_keypad(xkb_keysym_t keysym);
bool
xkb_keysym_is_modifier(xkb_keysym_t keysym);
#endif

View File

@ -131,6 +131,22 @@ test_utf32_to_keysym(uint32_t ucs, xkb_keysym_t expected)
int int
main(void) main(void)
{ {
/* Bounds */
assert(XKB_KEYSYM_MIN == 0);
assert(XKB_KEYSYM_MIN < XKB_KEYSYM_MAX);
assert(XKB_KEYSYM_MAX <= UINT32_MAX); /* Ensure it fits in xkb_keysym_t */
assert(XKB_KEYSYM_MAX <= INT32_MAX); /* Ensure correct cast to int32_t */
assert(XKB_KEYSYM_MIN_ASSIGNED == XKB_KEYSYM_MIN);
assert(XKB_KEYSYM_MIN_ASSIGNED < XKB_KEYSYM_MAX_ASSIGNED);
assert(XKB_KEYSYM_MAX_ASSIGNED <= XKB_KEYSYM_MAX);
assert(XKB_KEYSYM_MIN_EXPLICIT == XKB_KEYSYM_MIN_ASSIGNED);
assert(XKB_KEYSYM_MIN_EXPLICIT < XKB_KEYSYM_MAX_EXPLICIT);
assert(XKB_KEYSYM_MAX_EXPLICIT <= XKB_KEYSYM_MAX_ASSIGNED);
assert(XKB_KEYSYM_COUNT_EXPLICIT <= XKB_KEYSYM_MAX_EXPLICIT - XKB_KEYSYM_MIN_EXPLICIT + 1);
assert(XKB_KEYSYM_UNICODE_MIN >= XKB_KEYSYM_MIN_EXPLICIT);
assert(XKB_KEYSYM_UNICODE_MIN < XKB_KEYSYM_UNICODE_MAX);
assert(XKB_KEYSYM_UNICODE_MAX <= XKB_KEYSYM_MAX_EXPLICIT);
/* Named keysyms */ /* Named keysyms */
assert(test_string("NoSymbol", XKB_KEY_NoSymbol)); assert(test_string("NoSymbol", XKB_KEY_NoSymbol));
assert(test_string("Undo", 0xFF65)); assert(test_string("Undo", 0xFF65));