diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2020-06-22 13:07:46 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2020-06-25 10:32:08 +1000 |
commit | ec2bbe599cdbd848c8fc45f6bcf02b4e850e1163 (patch) | |
tree | 3a1c8244770d011aca13a8f8c50aa0259c0ec28e /tools | |
parent | 725a31986d1f9d26f7c91008af3d94d0cd304509 (diff) | |
download | xorg-lib-libxkbcommon-ec2bbe599cdbd848c8fc45f6bcf02b4e850e1163.tar.gz |
Move the various tools to a tools/ directory
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/how-to-type.c | 196 | ||||
-rw-r--r-- | tools/print-compiled-keymap.c | 94 | ||||
-rw-r--r-- | tools/rmlvo-to-kccgst.c | 104 | ||||
-rw-r--r-- | tools/rmlvo-to-keymap.c | 144 |
4 files changed, 538 insertions, 0 deletions
diff --git a/tools/how-to-type.c b/tools/how-to-type.c new file mode 100644 index 0000000..6fef401 --- /dev/null +++ b/tools/how-to-type.c @@ -0,0 +1,196 @@ +/* + * Copyright © 2020 Ran Benita <ran@unusedvar.com> + * + * 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. + */ + +#include "config.h" + +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> + +#include "xkbcommon/xkbcommon.h" + +#define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr)))) + +static void +usage(const char *argv0) +{ + fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] " + "[-l <layout>] [-v <variant>] [-o <options>] <unicode codepoint>\n", + argv0); + fprintf(stderr, "Pipe into `column -ts $'\\t'` for nicely aligned output.\n"); + exit(2); +} + +int +main(int argc, char *argv[]) +{ + int opt; + const char *rules = NULL; + const char *model = NULL; + const char *layout_ = NULL; + const char *variant = NULL; + const char *options = NULL; + int exit = EXIT_FAILURE; + struct xkb_context *ctx = NULL; + char *endp; + long val; + uint32_t codepoint; + xkb_keysym_t keysym; + int ret; + char name[200]; + struct xkb_keymap *keymap = NULL; + xkb_keycode_t min_keycode, max_keycode; + xkb_mod_index_t num_mods; + + while ((opt = getopt(argc, argv, "r:m:l:v:o:")) != -1) { + switch (opt) { + case 'r': + rules = optarg; + break; + case 'm': + model = optarg; + break; + case 'l': + layout_ = optarg; + break; + case 'v': + variant = optarg; + break; + case 'o': + options = optarg; + break; + default: + usage(argv[0]); + } + } + if (argc - optind != 1) { + usage(argv[0]); + } + + errno = 0; + val = strtol(argv[optind], &endp, 0); + if (errno != 0 || endp == argv[optind] || val < 0 || val > 0x10FFFF) { + usage(argv[0]); + } + codepoint = (uint32_t) val; + + keysym = xkb_utf32_to_keysym(codepoint); + if (keysym == XKB_KEY_NoSymbol) { + fprintf(stderr, "Failed to convert codepoint to keysym\n"); + goto err; + } + + ret = xkb_keysym_get_name(keysym, name, sizeof(name)); + if (ret < 0 || (size_t) ret >= sizeof(name)) { + fprintf(stderr, "Failed to get name of keysym\n"); + goto err; + } + + ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!ctx) { + fprintf(stderr, "Failed to create XKB context\n"); + goto err; + } + + struct xkb_rule_names names = { + .rules = rules, + .model = model, + .layout = layout_, + .variant = variant, + .options = options, + }; + keymap = xkb_keymap_new_from_names(ctx, &names, + XKB_KEYMAP_COMPILE_NO_FLAGS); + if (!keymap) { + fprintf(stderr, "Failed to create XKB keymap\n"); + goto err; + } + + printf("keysym: %s (%#x)\n", name, keysym); + printf("KEYCODE\tKEY NAME\tLAYOUT#\tLAYOUT NAME\tLEVEL#\tMODIFIERS\n"); + + min_keycode = xkb_keymap_min_keycode(keymap); + max_keycode = xkb_keymap_max_keycode(keymap); + num_mods = xkb_keymap_num_mods(keymap); + for (xkb_keycode_t keycode = min_keycode; keycode <= max_keycode; keycode++) { + const char *key_name; + xkb_layout_index_t num_layouts; + + key_name = xkb_keymap_key_get_name(keymap, keycode); + if (!key_name) { + continue; + } + + num_layouts = xkb_keymap_num_layouts_for_key(keymap, keycode); + for (xkb_layout_index_t layout = 0; layout < num_layouts; layout++) { + const char *layout_name; + xkb_level_index_t num_levels; + + layout_name = xkb_keymap_layout_get_name(keymap, layout); + if (!layout_name) { + layout_name = "?"; + } + + num_levels = xkb_keymap_num_levels_for_key(keymap, keycode, layout); + for (xkb_level_index_t level = 0; level < num_levels; level++) { + int num_syms; + const xkb_keysym_t *syms; + size_t num_masks; + xkb_mod_mask_t masks[100]; + + num_syms = xkb_keymap_key_get_syms_by_level( + keymap, keycode, layout, level, &syms + ); + if (num_syms != 1) { + continue; + } + if (syms[0] != keysym) { + continue; + } + + num_masks = xkb_keymap_key_get_mods_for_level( + keymap, keycode, layout, level, masks, ARRAY_SIZE(masks) + ); + for (size_t i = 0; i < num_masks; i++) { + xkb_mod_mask_t mask = masks[i]; + + printf("%u\t%s\t%u\t%s\t%u\t[ ", + keycode, key_name, layout + 1, layout_name, level + 1); + for (xkb_mod_index_t mod = 0; mod < num_mods; mod++) { + if ((mask & (1 << mod)) == 0) { + continue; + } + printf("%s ", xkb_keymap_mod_get_name(keymap, mod)); + } + printf("]\n"); + } + } + } + } + + exit = EXIT_SUCCESS; +err: + xkb_keymap_unref(keymap); + xkb_context_unref(ctx); + return exit; +} diff --git a/tools/print-compiled-keymap.c b/tools/print-compiled-keymap.c new file mode 100644 index 0000000..e595ab1 --- /dev/null +++ b/tools/print-compiled-keymap.c @@ -0,0 +1,94 @@ +/* + * Copyright © 2012 Ran Benita <ran234@gmail.com> + * + * 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. + */ + +#include "config.h" + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "xkbcommon/xkbcommon.h" + +int +main(int argc, char *argv[]) +{ + int ret = EXIT_FAILURE; + int opt; + struct xkb_context *ctx = NULL; + struct xkb_keymap *keymap = NULL; + const char *keymap_path = NULL; + FILE *file = NULL; + char *dump; + + while ((opt = getopt(argc, argv, "h")) != -1) { + switch (opt) { + case 'h': + case '?': + fprintf(stderr, "Usage: %s <path to keymap file>\n", argv[0]); + exit(EXIT_FAILURE); + } + } + + if (optind >= argc) { + fprintf(stderr, "Error: missing path to keymap file\n"); + exit(EXIT_FAILURE); + } + + keymap_path = argv[optind]; + + ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!ctx) { + fprintf(stderr, "Couldn't create xkb context\n"); + goto out; + } + + file = fopen(keymap_path, "rb"); + if (!file) { + fprintf(stderr, "Failed to open path: %s\n", keymap_path); + goto out; + } + + keymap = xkb_keymap_new_from_file(ctx, file, + XKB_KEYMAP_FORMAT_TEXT_V1, 0); + if (!keymap) { + fprintf(stderr, "Couldn't create xkb keymap\n"); + goto out; + } + + dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1); + if (!dump) { + fprintf(stderr, "Couldn't get the keymap string\n"); + goto out; + } + + fputs(dump, stdout); + + ret = EXIT_SUCCESS; + free(dump); +out: + if (file) + fclose(file); + xkb_keymap_unref(keymap); + xkb_context_unref(ctx); + return ret; +} diff --git a/tools/rmlvo-to-kccgst.c b/tools/rmlvo-to-kccgst.c new file mode 100644 index 0000000..ab9336e --- /dev/null +++ b/tools/rmlvo-to-kccgst.c @@ -0,0 +1,104 @@ +/* + * Copyright © 2012 Ran Benita <ran234@gmail.com> + * + * 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. + */ + +#include "config.h" + +#include <unistd.h> +#include <getopt.h> + +#include "xkbcomp/xkbcomp-priv.h" +#include "xkbcomp/rules.h" + +int +main(int argc, char *argv[]) +{ + struct xkb_rule_names rmlvo = { NULL }; + struct xkb_context *ctx; + struct xkb_component_names kccgst; + + static struct option opts[] = { + {"help", no_argument, 0, 'h'}, + {"rules", required_argument, 0, 'r'}, + {"model", required_argument, 0, 'm'}, + {"layout", required_argument, 0, 'l'}, + {"variant", required_argument, 0, 'v'}, + {"options", required_argument, 0, 'o'}, + {0, 0, 0, 0}, + }; + + while (1) { + int c; + int option_index = 0; + + c = getopt_long(argc, argv, "r:m:l:v:o:h", opts, &option_index); + if (c == -1) + break; + + switch (c) { + case 'r': + rmlvo.rules = optarg; + break; + case 'm': + rmlvo.model = optarg; + break; + case 'l': + rmlvo.layout = optarg; + break; + case 'v': + rmlvo.variant = optarg; + break; + case 'o': + rmlvo.options = optarg; + break; + case 'h': + case '?': + fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] " + "[-l <layout>] [-v <variant>] [-o <options>]\n", + argv[0]); + return 1; + } + } + + ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!ctx) { + fprintf(stderr, "Failed to get xkb context\n"); + return 1; + } + + xkb_context_sanitize_rule_names(ctx, &rmlvo); + + if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) + return 1; + + printf("keycodes: %s\n", kccgst.keycodes); + printf("types: %s\n", kccgst.types); + printf("compat: %s\n", kccgst.compat); + printf("symbols: %s\n", kccgst.symbols); + + free(kccgst.keycodes); + free(kccgst.types); + free(kccgst.compat); + free(kccgst.symbols); + xkb_context_unref(ctx); + return 0; +} diff --git a/tools/rmlvo-to-keymap.c b/tools/rmlvo-to-keymap.c new file mode 100644 index 0000000..3a98527 --- /dev/null +++ b/tools/rmlvo-to-keymap.c @@ -0,0 +1,144 @@ +/* + * Copyright © 2018 Red Hat, Inc. + * + * 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. + */ + +#include "config.h" + +#include <assert.h> +#include <errno.h> +#include <getopt.h> +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +#include "xkbcommon/xkbcommon.h" + +static bool print = false; + +static void +usage(char **argv) +{ + printf("Usage: %s [--print] [--rules <rules>] [--layout <layout>] [--variant <variant>] [--options <option>]\n", + argv[0]); + printf("This tool tests the compilation from RMLVO to a keymap.\n"); + printf("--print print the resulting keymap\n"); +} + +static bool +parse_options(int argc, char **argv, struct xkb_rule_names *names) +{ + enum options { + OPT_PRINT, + OPT_RULES, + OPT_MODEL, + OPT_LAYOUT, + OPT_VARIANT, + OPT_OPTION, + }; + static struct option opts[] = { + {"help", no_argument, 0, 'h'}, + {"print", no_argument, 0, OPT_PRINT}, + {"rules", required_argument, 0, OPT_RULES}, + {"model", required_argument, 0, OPT_MODEL}, + {"layout", required_argument, 0, OPT_LAYOUT}, + {"variant", required_argument, 0, OPT_VARIANT}, + {"options", required_argument, 0, OPT_OPTION}, + {0, 0, 0, 0}, + }; + + while (1) { + int c; + int option_index = 0; + c = getopt_long(argc, argv, "h", opts, &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + usage(argv); + exit(0); + case OPT_PRINT: + print = true; + break; + case OPT_RULES: + names->rules = optarg; + break; + case OPT_MODEL: + names->model = optarg; + break; + case OPT_LAYOUT: + names->layout = optarg; + break; + case OPT_VARIANT: + names->variant = optarg; + break; + case OPT_OPTION: + names->options = optarg; + break; + default: + usage(argv); + exit(1); + } + + } + + return true; +} + +int +main(int argc, char **argv) +{ + struct xkb_context *ctx; + struct xkb_keymap *keymap; + struct xkb_rule_names names = { + .rules = NULL, + .model = NULL, + .layout = NULL, + .variant = NULL, + .options = NULL, + }; + int rc; + + if (argc <= 1) { + usage(argv); + return 1; + } + + if (!parse_options(argc, argv, &names)) + return 1; + + ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + assert(ctx); + + keymap = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS); + rc = (keymap == NULL); + + if (rc == 0 && print) + printf("%s\n", xkb_keymap_get_as_string(keymap, + XKB_KEYMAP_FORMAT_TEXT_V1)); + + xkb_keymap_unref(keymap); + xkb_context_unref(ctx); + + return rc; +} |