2020-03-20 10:29:03 -06:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2020-07-12 20:26:07 -06:00
|
|
|
#include <getopt.h>
|
2021-07-31 13:03:33 -06:00
|
|
|
#include <stdbool.h>
|
2020-03-20 10:29:03 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2020-06-21 21:08:51 -06:00
|
|
|
#include "xkbcommon/xkbcommon.h"
|
2023-12-18 23:28:52 -07:00
|
|
|
#include "src/keysym.h"
|
2020-03-20 10:29:03 -06:00
|
|
|
|
2020-03-21 04:56:13 -06:00
|
|
|
#define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
|
|
|
|
|
2020-03-20 10:29:03 -06:00
|
|
|
static void
|
2020-07-12 20:26:07 -06:00
|
|
|
usage(const char *argv0, FILE *fp)
|
2020-03-20 10:29:03 -06:00
|
|
|
{
|
2021-07-31 13:03:33 -06:00
|
|
|
fprintf(fp, "Usage: %s [--keysym] [--rules <rules>] [--model <model>] "
|
2020-07-12 20:26:07 -06:00
|
|
|
"[--layout <layout>] [--variant <variant>] [--options <options>]"
|
2021-07-31 13:03:33 -06:00
|
|
|
" <unicode codepoint/keysym>\n", argv0);
|
2020-03-20 10:29:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char *rules = NULL;
|
|
|
|
const char *model = NULL;
|
|
|
|
const char *layout_ = NULL;
|
|
|
|
const char *variant = NULL;
|
|
|
|
const char *options = NULL;
|
2021-07-31 13:03:33 -06:00
|
|
|
bool keysym_mode = false;
|
2020-07-08 20:15:45 -06:00
|
|
|
int err = EXIT_FAILURE;
|
2020-03-21 04:56:13 -06:00
|
|
|
struct xkb_context *ctx = NULL;
|
2020-03-20 10:29:03 -06:00
|
|
|
char *endp;
|
|
|
|
long val;
|
|
|
|
uint32_t codepoint;
|
|
|
|
xkb_keysym_t keysym;
|
|
|
|
int ret;
|
2023-12-18 23:28:52 -07:00
|
|
|
char name[XKB_KEYSYM_NAME_MAX_SIZE];
|
2020-03-21 04:56:13 -06:00
|
|
|
struct xkb_keymap *keymap = NULL;
|
2020-03-20 10:29:03 -06:00
|
|
|
xkb_keycode_t min_keycode, max_keycode;
|
|
|
|
xkb_mod_index_t num_mods;
|
2020-07-12 20:26:07 -06:00
|
|
|
enum options {
|
2021-07-31 13:03:33 -06:00
|
|
|
OPT_KEYSYM,
|
2020-07-12 20:26:07 -06:00
|
|
|
OPT_RULES,
|
|
|
|
OPT_MODEL,
|
|
|
|
OPT_LAYOUT,
|
|
|
|
OPT_VARIANT,
|
|
|
|
OPT_OPTIONS,
|
|
|
|
};
|
|
|
|
static struct option opts[] = {
|
|
|
|
{"help", no_argument, 0, 'h'},
|
2021-07-31 13:03:33 -06:00
|
|
|
{"keysym", no_argument, 0, OPT_KEYSYM},
|
2020-07-12 20:26:07 -06:00
|
|
|
{"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_OPTIONS},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int opt;
|
|
|
|
int option_index = 0;
|
|
|
|
|
|
|
|
opt = getopt_long(argc, argv, "h", opts, &option_index);
|
|
|
|
if (opt == -1)
|
|
|
|
break;
|
2020-03-20 10:29:03 -06:00
|
|
|
|
|
|
|
switch (opt) {
|
2021-07-31 13:03:33 -06:00
|
|
|
case OPT_KEYSYM:
|
|
|
|
keysym_mode = true;
|
|
|
|
break;
|
2020-07-12 20:26:07 -06:00
|
|
|
case OPT_RULES:
|
2020-03-20 10:29:03 -06:00
|
|
|
rules = optarg;
|
|
|
|
break;
|
2020-07-12 20:26:07 -06:00
|
|
|
case OPT_MODEL:
|
2020-03-20 10:29:03 -06:00
|
|
|
model = optarg;
|
|
|
|
break;
|
2020-07-12 20:26:07 -06:00
|
|
|
case OPT_LAYOUT:
|
2020-03-20 10:29:03 -06:00
|
|
|
layout_ = optarg;
|
|
|
|
break;
|
2020-07-12 20:26:07 -06:00
|
|
|
case OPT_VARIANT:
|
2020-03-20 10:29:03 -06:00
|
|
|
variant = optarg;
|
|
|
|
break;
|
2020-07-12 20:26:07 -06:00
|
|
|
case OPT_OPTIONS:
|
2020-03-20 10:29:03 -06:00
|
|
|
options = optarg;
|
|
|
|
break;
|
2020-07-12 20:26:07 -06:00
|
|
|
case 'h':
|
|
|
|
usage(argv[0], stdout);
|
|
|
|
exit(EXIT_SUCCESS);
|
2020-03-20 10:29:03 -06:00
|
|
|
default:
|
2020-07-12 20:26:07 -06:00
|
|
|
usage(argv[0], stderr);
|
2020-07-08 20:15:45 -06:00
|
|
|
exit(EXIT_INVALID_USAGE);
|
2020-03-20 10:29:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc - optind != 1) {
|
2020-07-12 20:26:07 -06:00
|
|
|
usage(argv[0], stderr);
|
2020-07-08 20:15:45 -06:00
|
|
|
exit(EXIT_INVALID_USAGE);
|
2020-03-20 10:29:03 -06:00
|
|
|
}
|
|
|
|
|
2021-07-31 13:03:33 -06:00
|
|
|
if (keysym_mode) {
|
|
|
|
keysym = xkb_keysym_from_name(argv[optind], XKB_KEYSYM_NO_FLAGS);
|
|
|
|
if (keysym == XKB_KEY_NoSymbol) {
|
|
|
|
fprintf(stderr, "Failed to convert argument to keysym\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(argv[optind], &endp, 0);
|
|
|
|
if (errno != 0 || endp == argv[optind] || val < 0 || val > 0x10FFFF) {
|
|
|
|
usage(argv[0], stderr);
|
|
|
|
exit(EXIT_INVALID_USAGE);
|
|
|
|
}
|
|
|
|
codepoint = (uint32_t) val;
|
2020-03-20 10:29:03 -06:00
|
|
|
|
2021-07-31 13:03:33 -06:00
|
|
|
keysym = xkb_utf32_to_keysym(codepoint);
|
|
|
|
if (keysym == XKB_KEY_NoSymbol) {
|
|
|
|
fprintf(stderr, "Failed to convert codepoint to keysym\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2020-03-20 10:29:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = xkb_keysym_get_name(keysym, name, sizeof(name));
|
|
|
|
if (ret < 0 || (size_t) ret >= sizeof(name)) {
|
2020-03-21 04:56:13 -06:00
|
|
|
fprintf(stderr, "Failed to get name of keysym\n");
|
|
|
|
goto err;
|
2020-03-20 10:29:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
|
|
if (!ctx) {
|
|
|
|
fprintf(stderr, "Failed to create XKB context\n");
|
2020-03-21 04:56:13 -06:00
|
|
|
goto err;
|
2020-03-20 10:29:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2020-03-21 04:56:13 -06:00
|
|
|
goto err;
|
2020-03-20 10:29:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("keysym: %s (%#x)\n", name, keysym);
|
2020-09-09 01:09:13 -06:00
|
|
|
printf("%-8s %-9s %-8s %-20s %-7s %-s\n",
|
|
|
|
"KEYCODE", "KEY NAME", "LAYOUT", "LAYOUT NAME", "LEVEL#", "MODIFIERS");
|
2020-03-20 10:29:03 -06:00
|
|
|
|
|
|
|
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(
|
2020-03-21 04:56:13 -06:00
|
|
|
keymap, keycode, layout, level, masks, ARRAY_SIZE(masks)
|
2020-03-20 10:29:03 -06:00
|
|
|
);
|
|
|
|
for (size_t i = 0; i < num_masks; i++) {
|
|
|
|
xkb_mod_mask_t mask = masks[i];
|
|
|
|
|
2020-09-09 01:09:13 -06:00
|
|
|
printf("%-8u %-9s %-8u %-20s %-7u [ ",
|
2020-03-20 10:29:03 -06:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 20:15:45 -06:00
|
|
|
err = EXIT_SUCCESS;
|
2020-03-21 04:56:13 -06:00
|
|
|
err:
|
2020-03-20 10:29:03 -06:00
|
|
|
xkb_keymap_unref(keymap);
|
|
|
|
xkb_context_unref(ctx);
|
2020-07-08 20:15:45 -06:00
|
|
|
return err;
|
2020-03-20 10:29:03 -06:00
|
|
|
}
|