Commit b5079dc96dbc899d1acfb38a9aeb999b31a223ca

Pierre Le Marre 2023-09-18T12:15:06

Interactive tools: add options to hide some fields Display can be cluttered when too many fields are displayed. Add options to hide some default fields

diff --git a/tools/interactive-evdev.c b/tools/interactive-evdev.c
index 6cacae6..2f03975 100644
--- a/tools/interactive-evdev.c
+++ b/tools/interactive-evdev.c
@@ -58,6 +58,8 @@ static int evdev_offset = 8;
 static bool report_state_changes;
 static bool with_compose;
 static enum xkb_consumed_mode consumed_mode = XKB_CONSUMED_MODE_XKB;
+#define DEFAULT_PRINT_FIELDS PRINT_ALL_FIELDS
+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)
@@ -271,9 +273,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);
@@ -373,6 +378,7 @@ usage(FILE *fp, char *progname)
         fprintf(fp, "      or: %s --keymap <path to keymap file>\n",
                 progname);
         fprintf(fp, "For both:\n"
+                        "          --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"
@@ -410,6 +416,7 @@ main(int argc, char *argv[])
         OPT_WITHOUT_X11_OFFSET,
         OPT_CONSUMED_MODE,
         OPT_COMPOSE,
+        OPT_SHORT,
         OPT_REPORT_STATE,
     };
     static struct option opts[] = {
@@ -424,6 +431,7 @@ 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},
         {0, 0, 0, 0},
@@ -481,6 +489,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;
diff --git a/tools/interactive-wayland.c b/tools/interactive-wayland.c
index 7d2a351..4c11e5c 100644
--- a/tools/interactive-wayland.c
+++ b/tools/interactive-wayland.c
@@ -400,7 +400,8 @@ kbd_key(void *data, struct wl_keyboard *wl_kbd, uint32_t serial, uint32_t time,
 
     printf("%s: ", seat->name_str);
     tools_print_keycode_state(seat->state, NULL, key + EVDEV_OFFSET,
-                              XKB_CONSUMED_MODE_XKB);
+                              XKB_CONSUMED_MODE_XKB,
+                              PRINT_ALL_FIELDS);
 
     /* Exit on ESC. */
     if (xkb_state_key_get_one_sym(seat->state, key + EVDEV_OFFSET) == XKB_KEY_Escape)
diff --git a/tools/interactive-x11.c b/tools/interactive-x11.c
index eea22fa..9fe0c10 100644
--- a/tools/interactive-x11.c
+++ b/tools/interactive-x11.c
@@ -243,7 +243,8 @@ process_event(xcb_generic_event_t *gevent, struct keyboard *kbd)
         xkb_keycode_t keycode = event->detail;
 
         tools_print_keycode_state(kbd->state, NULL, keycode,
-                                  XKB_CONSUMED_MODE_XKB);
+                                  XKB_CONSUMED_MODE_XKB,
+                                  PRINT_ALL_FIELDS);
 
         /* Exit on ESC. */
         if (xkb_state_key_get_one_sym(kbd->state, keycode) == XKB_KEY_Escape)
diff --git a/tools/tools-common.c b/tools/tools-common.c
index 3403ea6..3262f61 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -65,7 +65,8 @@ void
 tools_print_keycode_state(struct xkb_state *state,
                           struct xkb_compose_state *compose_state,
                           xkb_keycode_t keycode,
-                          enum xkb_consumed_mode consumed_mode)
+                          enum xkb_consumed_mode consumed_mode,
+                          print_state_fields_mask_t fields)
 {
     struct xkb_keymap *keymap;
 
@@ -109,25 +110,29 @@ tools_print_keycode_state(struct xkb_state *state,
     }
     printf("] ");
 
-    if (status == XKB_COMPOSE_COMPOSED)
-        xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
-    else
-        xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
-    /* HACK: escape single control characters from C0 set using the
-    * Unicode codepoint convention. Ideally we would like to escape
-    * any non-printable character in the string.
-    */
-    if (!*s) {
-        printf("unicode [   ] ");
-    } else if (strlen(s) == 1 && (*s <= 0x1F || *s == 0x7F)) {
-        printf("unicode [ U+%04hX ] ", *s);
-    } else {
-        printf("unicode [ %s ] ", s);
+    if (fields & PRINT_UNICODE) {
+        if (status == XKB_COMPOSE_COMPOSED)
+            xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
+        else
+            xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
+        /* HACK: escape single control characters from C0 set using the
+        * Unicode codepoint convention. Ideally we would like to escape
+        * any non-printable character in the string.
+        */
+        if (!*s) {
+            printf("unicode [   ] ");
+        } else if (strlen(s) == 1 && (*s <= 0x1F || *s == 0x7F)) {
+            printf("unicode [ U+%04hX ] ", *s);
+        } else {
+            printf("unicode [ %s ] ", s);
+        }
     }
 
     layout = xkb_state_key_get_layout(state, keycode);
-    printf("layout [ %s (%d) ] ",
-           xkb_keymap_layout_get_name(keymap, layout), layout);
+    if (fields & PRINT_LAYOUT) {
+        printf("layout [ %s (%d) ] ",
+               xkb_keymap_layout_get_name(keymap, layout), layout);
+    }
 
     printf("level [ %d ] ",
            xkb_state_key_get_level(state, keycode, layout));
diff --git a/tools/tools-common.h b/tools/tools-common.h
index 0c16505..21ba848 100644
--- a/tools/tools-common.h
+++ b/tools/tools-common.h
@@ -36,11 +36,26 @@
 
 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
 
+/* Fields that are printed in the interactive tools. */
+enum print_state_fields {
+    PRINT_LAYOUT = (1u << 2),
+    PRINT_UNICODE = (1u << 3),
+    PRINT_ALL_FIELDS = ((PRINT_UNICODE << 1) - 1),
+    /*
+     * Fields that can be hidden with the option --short.
+     * NOTE: If this value is modified, remember to update the documentation of
+     *       the --short option in the corresponding tools.
+     */
+    PRINT_VERBOSE_FIELDS = (PRINT_LAYOUT | PRINT_UNICODE)
+};
+typedef uint32_t print_state_fields_mask_t;
+
 void
 tools_print_keycode_state(struct xkb_state *state,
                           struct xkb_compose_state *compose_state,
                           xkb_keycode_t keycode,
-                          enum xkb_consumed_mode consumed_mode);
+                          enum xkb_consumed_mode consumed_mode,
+                          print_state_fields_mask_t fields);
 
 void
 tools_print_state_changes(enum xkb_state_component changed);