Skip to content

Instantly share code, notes, and snippets.

@cas--
Last active November 17, 2024 11:26
Show Gist options
  • Save cas--/4414866e2b7a8b158c815a1000526d21 to your computer and use it in GitHub Desktop.
Save cas--/4414866e2b7a8b158c815a1000526d21 to your computer and use it in GitHub Desktop.
Test XKB Keycodes

Test Event to XKB keycode mapping

This is a simple program using libxkbcommon to verify that input event keycodes are converted to XKB key symbols. Intended to be used to debug Kodi not mapping keycodes above 255.

Prerequisites

Required build tools and libraries.

Ubuntu/Debian

sudo apt install gcc pkgconf libxkbcommon-dev

NixOS

nix-shell -p gcc libxkbcommon.dev pkg-config

Build

gcc -o xkb-test xkb-test.c $(pkg-config --cflags --libs xkbcommon)

Run

$ ./xkb-test 358
Event Keycode: 358 (0x166) -> XKB KeySymbol: XF86Info (0x10081166) 

Test with unmapped keycode:

$ ./xkb-test 400
Event Keycode: 400 (0x190) -> XKB KeySymbol: NoSymbol (0x00000000) 

Test with XKB base rules that don't support >255 keycodes.

$ XKB_DEFAULT_RULES=base ./xkb-test 358
Event Keycode: 358 (0x166) -> XKB KeySymbol: NoSymbol (0x00000000) 
#include <stdio.h>
#include <stdlib.h>
#include <xkbcommon/xkbcommon.h>
int main(int argc, char *argv[])
{
uint32_t ev_keycode;
struct xkb_context *context;
struct xkb_keymap *keymap;
struct xkb_state *state;
xkb_keycode_t xkb_keycode;
xkb_keysym_t xkb_keysym;
if (argc != 2)
{
fprintf(stderr, "Usage: %s <keycode>\n", argv[0]);
fprintf(stderr, "Event Keycode as decimal or hex (with 0x prefix)\n");
return 1;
}
// Parse event keycode from args
ev_keycode = (int)strtoul(argv[1], NULL, 0);
context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
keymap = xkb_keymap_new_from_names(
context, NULL, XKB_KEYMAP_COMPILE_NO_FLAGS);
state = xkb_state_new(keymap);
xkb_keycode = ev_keycode + 8;
xkb_keysym = xkb_state_key_get_one_sym(state, xkb_keycode);
char name[256];
xkb_keysym_get_name(xkb_keysym, name, sizeof(name));
printf("Event Keycode: %d (0x%x) -> XKB KeySymbol: %s (0x%08x) \n",
ev_keycode, ev_keycode, name, xkb_keysym);
// Cleanup
xkb_state_unref(state);
xkb_keymap_unref(keymap);
xkb_context_unref(context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment