Removed build dependency on xproto.
[platform/upstream/libxkbcommon.git] / tools / interactive-evdev.c
index 6b7c8a1..2dece75 100644 (file)
@@ -59,6 +59,14 @@ static bool report_state_changes;
 static bool with_compose;
 static enum xkb_consumed_mode consumed_mode = XKB_CONSUMED_MODE_XKB;
 
+#ifdef ENABLE_PRIVATE_APIS
+#define DEFAULT_PRINT_FIELDS (PRINT_ALL_FIELDS & ~PRINT_MODMAPS)
+#else
+#define DEFAULT_PRINT_FIELDS PRINT_ALL_FIELDS
+#endif
+print_state_fields_mask_t print_fields = DEFAULT_PRINT_FIELDS;
+
+#define DEFAULT_INCLUDE_PATH_PLACEHOLDER "__defaults__"
 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
 
 static bool
@@ -270,9 +278,12 @@ process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
         xkb_compose_state_feed(kbd->compose_state, keysym);
     }
 
-    if (value != KEY_STATE_RELEASE)
-        tools_print_keycode_state(kbd->state, kbd->compose_state, keycode,
-                                  consumed_mode);
+    if (value != KEY_STATE_RELEASE) {
+        tools_print_keycode_state(
+            kbd->state, kbd->compose_state, keycode,
+            consumed_mode, print_fields
+        );
+    }
 
     if (with_compose) {
         status = xkb_compose_state_get_status(kbd->compose_state);
@@ -313,10 +324,10 @@ read_keyboard(struct keyboard *kbd)
 static int
 loop(struct keyboard *kbds)
 {
+    int ret = -1;
     struct keyboard *kbd;
     nfds_t nfds, i;
     struct pollfd *fds = NULL;
-    int ret;
 
     for (kbd = kbds, nfds = 0; kbd; kbd = kbd->next, nfds++) {}
     fds = calloc(nfds, sizeof(*fds));
@@ -365,16 +376,23 @@ sigintr_handler(int signum)
 static void
 usage(FILE *fp, char *progname)
 {
-        fprintf(fp, "Usage: %s [--rules=<rules>] [--model=<model>] "
-                "[--layout=<layout>] [--variant=<variant>] [--options=<options>]\n",
+        fprintf(fp, "Usage: %s [--include=<path>] [--include-defaults] "
+                "[--rules=<rules>] [--model=<model>] [--layout=<layout>] "
+                "[--variant=<variant>] [--options=<options>]\n",
                 progname);
         fprintf(fp, "      or: %s --keymap <path to keymap file>\n",
                 progname);
         fprintf(fp, "For both:\n"
+#ifdef ENABLE_PRIVATE_APIS
+                        "          --print-modmaps (print real & virtual key modmaps)\n"
+#endif
+                        "          --short (do not print layout nor Unicode keysym translation)\n"
                         "          --report-state-changes (report changes to the state)\n"
                         "          --enable-compose (enable Compose)\n"
                         "          --consumed-mode={xkb|gtk} (select the consumed modifiers mode, default: xkb)\n"
                         "          --without-x11-offset (don't add X11 keycode offset)\n"
+                    "Other:\n"
+                        "          --help (display this help and exit)\n"
         );
 }
 
@@ -386,6 +404,8 @@ main(int argc, char *argv[])
     struct xkb_context *ctx = NULL;
     struct xkb_keymap *keymap = NULL;
     struct xkb_compose_table *compose_table = NULL;
+    const char *includes[64];
+    size_t num_includes = 0;
     const char *rules = NULL;
     const char *model = NULL;
     const char *layout = NULL;
@@ -395,6 +415,8 @@ main(int argc, char *argv[])
     const char *locale;
     struct sigaction act;
     enum options {
+        OPT_INCLUDE,
+        OPT_INCLUDE_DEFAULTS,
         OPT_RULES,
         OPT_MODEL,
         OPT_LAYOUT,
@@ -404,10 +426,16 @@ main(int argc, char *argv[])
         OPT_WITHOUT_X11_OFFSET,
         OPT_CONSUMED_MODE,
         OPT_COMPOSE,
+        OPT_SHORT,
         OPT_REPORT_STATE,
+#ifdef ENABLE_PRIVATE_APIS
+        OPT_PRINT_MODMAPS,
+#endif
     };
     static struct option opts[] = {
         {"help",                 no_argument,            0, 'h'},
+        {"include",              required_argument,      0, OPT_INCLUDE},
+        {"include-defaults",     no_argument,            0, OPT_INCLUDE_DEFAULTS},
         {"rules",                required_argument,      0, OPT_RULES},
         {"model",                required_argument,      0, OPT_MODEL},
         {"layout",               required_argument,      0, OPT_LAYOUT},
@@ -416,8 +444,12 @@ main(int argc, char *argv[])
         {"keymap",               required_argument,      0, OPT_KEYMAP},
         {"consumed-mode",        required_argument,      0, OPT_CONSUMED_MODE},
         {"enable-compose",       no_argument,            0, OPT_COMPOSE},
+        {"short",                no_argument,            0, OPT_SHORT},
         {"report-state-changes", no_argument,            0, OPT_REPORT_STATE},
         {"without-x11-offset",   no_argument,            0, OPT_WITHOUT_X11_OFFSET},
+#ifdef ENABLE_PRIVATE_APIS
+        {"print-modmaps",        no_argument,            0, OPT_PRINT_MODMAPS},
+#endif
         {0, 0, 0, 0},
     };
 
@@ -432,6 +464,20 @@ main(int argc, char *argv[])
             break;
 
         switch (opt) {
+        case OPT_INCLUDE:
+            if (num_includes >= ARRAY_SIZE(includes)) {
+                fprintf(stderr, "error: too many includes\n");
+                exit(EXIT_INVALID_USAGE);
+            }
+            includes[num_includes++] = optarg;
+            break;
+        case OPT_INCLUDE_DEFAULTS:
+            if (num_includes >= ARRAY_SIZE(includes)) {
+                fprintf(stderr, "error: too many includes\n");
+                exit(EXIT_INVALID_USAGE);
+            }
+            includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
+            break;
         case OPT_RULES:
             rules = optarg;
             break;
@@ -459,6 +505,9 @@ main(int argc, char *argv[])
         case OPT_COMPOSE:
             with_compose = true;
             break;
+        case OPT_SHORT:
+            print_fields &= ~PRINT_VERBOSE_FIELDS;
+            break;
         case OPT_CONSUMED_MODE:
             if (strcmp(optarg, "gtk") == 0) {
                 consumed_mode = XKB_CONSUMED_MODE_GTK;
@@ -470,6 +519,11 @@ main(int argc, char *argv[])
                 return EXIT_INVALID_USAGE;
             }
             break;
+#ifdef ENABLE_PRIVATE_APIS
+        case OPT_PRINT_MODMAPS:
+            print_fields |= PRINT_MODMAPS;
+            break;
+#endif
         case 'h':
             usage(stdout, argv[0]);
             return EXIT_SUCCESS;
@@ -479,12 +533,23 @@ main(int argc, char *argv[])
         }
     }
 
-    ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+    ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
     if (!ctx) {
         fprintf(stderr, "Couldn't create xkb context\n");
         goto out;
     }
 
+    if (num_includes == 0)
+        includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
+
+    for (size_t i = 0; i < num_includes; i++) {
+        const char *include = includes[i];
+        if (strcmp(include, DEFAULT_INCLUDE_PATH_PLACEHOLDER) == 0)
+            xkb_context_include_path_append_default(ctx);
+        else
+            xkb_context_include_path_append(ctx, include);
+    }
+
     if (keymap_path) {
         FILE *file = fopen(keymap_path, "rb");
         if (!file) {
@@ -540,6 +605,15 @@ main(int argc, char *argv[])
         goto out;
     }
 
+#ifdef ENABLE_PRIVATE_APIS
+    if (print_fields & PRINT_MODMAPS) {
+        print_keys_modmaps(keymap);
+        putchar('\n');
+        print_keymap_modmaps(keymap);
+        putchar('\n');
+    }
+#endif
+
     act.sa_handler = sigintr_handler;
     sigemptyset(&act.sa_mask);
     act.sa_flags = 0;