Commit f8c430cf7157bb45aeeb3dacce69730bad3052a4

Ran Benita 2021-07-31T22:03:33

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>

diff --git a/tools/how-to-type.c b/tools/how-to-type.c
index 0a6f79e..91ebd00 100644
--- a/tools/how-to-type.c
+++ b/tools/how-to-type.c
@@ -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));
diff --git a/tools/xkbcli-how-to-type.1 b/tools/xkbcli-how-to-type.1
index 5211d0f..d5002ae 100644
--- a/tools/xkbcli-how-to-type.1
+++ b/tools/xkbcli-how-to-type.1
@@ -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
 .