tools: add ability to print the KcCGST components for rmlvo-to-keymap
[platform/upstream/libxkbcommon.git] / tools / rmlvo-to-keymap.c
index 40ef52f..77e350f 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
+#include "xkbcomp/xkbcomp-priv.h"
+#include "xkbcomp/rules.h"
 #include "xkbcommon/xkbcommon.h"
 
+static bool verbose = false;
+static enum output_format {
+    FORMAT_KEYMAP,
+    FORMAT_KCCGST,
+} output_format = FORMAT_KEYMAP;
+
 static void
 usage(char **argv)
 {
@@ -43,6 +51,8 @@ usage(char **argv)
            "Options:\n"
            " --verbose\n"
            "    Enable verbose debugging output\n"
+           " --kccgst\n"
+           "    Print a keymap which only includes the KcCGST component names instead of the full keymap\n"
            "\n"
            "XKB-specific options:\n"
            " --rules <rules>\n"
@@ -63,10 +73,11 @@ usage(char **argv)
 }
 
 static bool
-parse_options(int argc, char **argv, bool *verbose, struct xkb_rule_names *names)
+parse_options(int argc, char **argv, struct xkb_rule_names *names)
 {
     enum options {
         OPT_VERBOSE,
+        OPT_KCCGST,
         OPT_RULES,
         OPT_MODEL,
         OPT_LAYOUT,
@@ -76,6 +87,7 @@ parse_options(int argc, char **argv, bool *verbose, struct xkb_rule_names *names
     static struct option opts[] = {
         {"help",        no_argument,            0, 'h'},
         {"verbose",     no_argument,            0, OPT_VERBOSE},
+        {"kccgst",      no_argument,            0, OPT_KCCGST},
         {"rules",       required_argument,      0, OPT_RULES},
         {"model",       required_argument,      0, OPT_MODEL},
         {"layout",      required_argument,      0, OPT_LAYOUT},
@@ -96,7 +108,10 @@ parse_options(int argc, char **argv, bool *verbose, struct xkb_rule_names *names
             usage(argv);
             exit(0);
         case OPT_VERBOSE:
-            *verbose = true;
+            verbose = true;
+            break;
+        case OPT_KCCGST:
+            output_format = FORMAT_KCCGST;
             break;
         case OPT_RULES:
             names->rules = optarg;
@@ -123,11 +138,48 @@ parse_options(int argc, char **argv, bool *verbose, struct xkb_rule_names *names
     return true;
 }
 
+static bool
+print_kccgst(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
+{
+        struct xkb_component_names kccgst;
+
+        if (!xkb_components_from_rules(ctx, rmlvo, &kccgst))
+            return false;
+
+        printf("xkb_keymap {\n"
+               "  xkb_keycodes { include \"%s\" };\n"
+               "  xkb_types { include \"%s\" };\n"
+               "  xkb_compat { include \"%s\" };\n"
+               "  xkb_symbols { include \"%s\" };\n"
+               "};\n",
+               kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
+        free(kccgst.keycodes);
+        free(kccgst.types);
+        free(kccgst.compat);
+        free(kccgst.symbols);
+
+        return true;
+}
+
+static bool
+print_keymap(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
+{
+    struct xkb_keymap *keymap;
+
+    keymap = xkb_keymap_new_from_names(ctx, rmlvo, XKB_KEYMAP_COMPILE_NO_FLAGS);
+    if (keymap == NULL)
+        return false;
+
+    printf("%s\n", xkb_keymap_get_as_string(keymap,
+                                            XKB_KEYMAP_FORMAT_TEXT_V1));
+    xkb_keymap_unref(keymap);
+    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,
@@ -135,15 +187,14 @@ main(int argc, char **argv)
         .variant = NULL,
         .options = NULL,
     };
-    int rc;
-    bool verbose = false;
+    int rc = 1;
 
     if (argc <= 1) {
         usage(argv);
         return 1;
     }
 
-    if (!parse_options(argc, argv, &verbose, &names))
+    if (!parse_options(argc, argv, &names))
         return 1;
 
     ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
@@ -154,16 +205,15 @@ main(int argc, char **argv)
         xkb_context_set_log_verbosity(ctx, 10);
     }
 
+    xkb_context_sanitize_rule_names(ctx, &names);
     xkb_context_include_path_append_default(ctx);
 
-    keymap = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
-    rc = (keymap == NULL);
-
-    if (rc == 0)
-        printf("%s\n", xkb_keymap_get_as_string(keymap,
-                                                XKB_KEYMAP_FORMAT_TEXT_V1));
+    if (output_format == FORMAT_KEYMAP) {
+        rc = print_keymap(ctx, &names) ? EXIT_SUCCESS : EXIT_FAILURE;
+    } else if (output_format == FORMAT_KCCGST) {
+        rc = print_kccgst(ctx, &names) ? EXIT_SUCCESS : EXIT_FAILURE;
+    }
 
-    xkb_keymap_unref(keymap);
     xkb_context_unref(ctx);
 
     return rc;