tools/how-to-type: add --keysym for how to type a keysym

Previously, could only check how to type a Unicode codepoint, but
searching for a keysym directly is also occasionally useful.

Signed-off-by: Ran Benita <ran@unusedvar.com>
master
Ran Benita 2021-07-31 22:03:33 +03:00
parent 5419e57736
commit f8c430cf71
2 changed files with 32 additions and 14 deletions

View File

@ -24,6 +24,7 @@
#include "config.h"
#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
@ -35,9 +36,9 @@
static void
usage(const char *argv0, FILE *fp)
{
fprintf(fp, "Usage: %s [--rules <rules>] [--model <model>] "
fprintf(fp, "Usage: %s [--keysym] [--rules <rules>] [--model <model>] "
"[--layout <layout>] [--variant <variant>] [--options <options>]"
" <unicode codepoint>\n", argv0);
" <unicode codepoint/keysym>\n", argv0);
}
int
@ -48,6 +49,7 @@ main(int argc, char *argv[])
const char *layout_ = NULL;
const char *variant = NULL;
const char *options = NULL;
bool keysym_mode = false;
int err = EXIT_FAILURE;
struct xkb_context *ctx = NULL;
char *endp;
@ -60,6 +62,7 @@ main(int argc, char *argv[])
xkb_keycode_t min_keycode, max_keycode;
xkb_mod_index_t num_mods;
enum options {
OPT_KEYSYM,
OPT_RULES,
OPT_MODEL,
OPT_LAYOUT,
@ -68,6 +71,7 @@ main(int argc, char *argv[])
};
static struct option opts[] = {
{"help", no_argument, 0, 'h'},
{"keysym", no_argument, 0, OPT_KEYSYM},
{"rules", required_argument, 0, OPT_RULES},
{"model", required_argument, 0, OPT_MODEL},
{"layout", required_argument, 0, OPT_LAYOUT},
@ -85,6 +89,9 @@ main(int argc, char *argv[])
break;
switch (opt) {
case OPT_KEYSYM:
keysym_mode = true;
break;
case OPT_RULES:
rules = optarg;
break;
@ -113,18 +120,26 @@ main(int argc, char *argv[])
exit(EXIT_INVALID_USAGE);
}
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;
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;
keysym = xkb_utf32_to_keysym(codepoint);
if (keysym == XKB_KEY_NoSymbol) {
fprintf(stderr, "Failed to convert codepoint to keysym\n");
goto err;
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));

View File

@ -9,7 +9,7 @@
.Sh SYNOPSIS
.Nm
.Op options
.Ar codepoint
.Ar codepoint/keysym
.
.Sh DESCRIPTION
.Nm
@ -17,6 +17,9 @@ prints the key combinations (keycode + modifiers) in the keymap's layouts which
would produce the given Unicode codepoint.
.
.Bl -tag -width Ds
.It Fl \-keysym
Treat the argument as a keysym, not a Unicode codepoint
.
.It Fl \-rules Ar rules
The XKB ruleset
.