diff --git a/scripts/update-headers.py b/scripts/update-headers.py new file mode 100755 index 0000000..18085e7 --- /dev/null +++ b/scripts/update-headers.py @@ -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\w+)\s+(?P0x[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"), +) diff --git a/scripts/update-keysyms b/scripts/update-keysyms index 5410474..03f5fb1 100755 --- a/scripts/update-keysyms +++ b/scripts/update-keysyms @@ -4,3 +4,4 @@ export LC_CTYPE=C scripts/makeheader > include/xkbcommon/xkbcommon-keysyms.h scripts/makekeys include/xkbcommon/xkbcommon-keysyms.h > src/ks_tables.h +scripts/update-headers diff --git a/src/keysym.h b/src/keysym.h index fd03f89..03a2c3e 100644 --- a/src/keysym.h +++ b/src/keysym.h @@ -57,6 +57,16 @@ */ /** Minimum keysym value */ #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 */ #define XKB_KEYSYM_UNICODE_OFFSET 0x01000000 /** Minimum Unicode keysym. NOTE: code points in 0..0xff cannot be converted. */ diff --git a/src/keysym.h.jinja b/src/keysym.h.jinja new file mode 100644 index 0000000..762eeb4 --- /dev/null +++ b/src/keysym.h.jinja @@ -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 diff --git a/test/keysym.c b/test/keysym.c index b266407..a2b1fce 100644 --- a/test/keysym.c +++ b/test/keysym.c @@ -131,6 +131,22 @@ test_utf32_to_keysym(uint32_t ucs, xkb_keysym_t expected) int 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 */ assert(test_string("NoSymbol", XKB_KEY_NoSymbol)); assert(test_string("Undo", 0xFF65));