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 
This commit is contained in:
Christian Helmuth 2023-01-31 16:24:46 +01:00
parent e8424e19fa
commit 3403a91213

@ -17,6 +17,8 @@
#ifndef _INCLUDE__INPUT__KEYCODES_H_
#define _INCLUDE__INPUT__KEYCODES_H_
#include <util/string.h>
/*
* 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;
}
}