From 3403a91213bc6e33145f88686adfbb458144e71c Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Tue, 31 Jan 2023 16:24:46 +0100 Subject: [PATCH] input: provide keycode-by-name lookup The new utility returns a key code for a passed name and is implemented by linear search, which is slow but sufficient in situations like config updates. Issue #4748 --- repos/os/include/input/keycodes.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/repos/os/include/input/keycodes.h b/repos/os/include/input/keycodes.h index 39773b72c5..c61962cf99 100644 --- a/repos/os/include/input/keycodes.h +++ b/repos/os/include/input/keycodes.h @@ -17,6 +17,8 @@ #ifndef _INCLUDE__INPUT__KEYCODES_H_ #define _INCLUDE__INPUT__KEYCODES_H_ +#include + /* * C++ provides no reflection of the names of enum values. So we use the * preprocessor to generate the bodies of both the enum type 'Keycode' and @@ -481,6 +483,21 @@ namespace Input { } return "KEY_UNKNOWN"; } + + typedef Genode::String<22> Key_name; + + /** + * Return key code for name (linear search) + */ + Keycode key_code(Key_name name) + { + for (unsigned i = 0; i < KEY_MAX; ++i) { + Keycode const code = Keycode(i); + if (name == key_name(code)) + return code; + } + return KEY_UNKNOWN; + } }