LibKeyboard+keymap: Support querying the keymap via commandline

This commit is contained in:
Ben Wiederhake 2021-01-30 22:01:34 +01:00 committed by Andreas Kling
parent a2c21a55e1
commit dd4e670f72
Notes: sideshowbarker 2024-07-18 22:40:27 +09:00
4 changed files with 52 additions and 7 deletions

View file

@ -407,7 +407,7 @@ void KeyboardDevice::set_maps(const Keyboard::CharacterMapData& character_map_da
{
m_character_map.set_character_map_data(character_map_data);
m_character_map.set_character_map_name(character_map_name);
dbgln("New Character map '{}' passing to client.", character_map_name);
dbgln("New Character map '{}' passed in by client.", character_map_name);
}
}

View file

@ -33,17 +33,23 @@
namespace Keyboard {
CharacterMap::CharacterMap(const String& file_name)
CharacterMap::CharacterMap(const String& map_name)
{
#ifdef KERNEL
m_character_map_data = default_character_map;
#else
auto result = CharacterMapFile::load_from_file(file_name);
auto result = CharacterMapFile::load_from_file(map_name);
ASSERT(result.has_value());
m_character_map_data = result.value();
#endif
m_character_map_name = file_name;
m_character_map_name = map_name;
}
CharacterMap::CharacterMap(const String& map_name, const CharacterMapData& map_data)
: m_character_map_data(map_data)
, m_character_map_name(map_name)
{
}
#ifndef KERNEL
@ -54,6 +60,26 @@ int CharacterMap::set_system_map()
return syscall(SC_setkeymap, &params);
}
Result<CharacterMap, OSError> CharacterMap::fetch_system_map()
{
CharacterMapData map_data;
char keymap_name[50 + 1] = { 0 };
Syscall::SC_getkeymap_params params {
map_data.map, map_data.shift_map,
map_data.alt_map,
map_data.altgr_map,
map_data.shift_altgr_map,
{ keymap_name, sizeof(keymap_name) }
};
int rc = syscall(SC_getkeymap, &params);
if (rc < 0) {
return OSError(-rc);
}
return CharacterMap { keymap_name, map_data };
}
#endif
u32 CharacterMap::get_char(KeyEvent event)

View file

@ -26,6 +26,10 @@
#pragma once
#ifndef KERNEL
# include <AK/OSError.h>
# include <AK/Result.h>
#endif
#include <AK/String.h>
#include <Kernel/API/KeyCode.h>
#include <LibKeyboard/CharacterMapData.h>
@ -35,10 +39,12 @@ namespace Keyboard {
class CharacterMap {
public:
CharacterMap(const String& file_name);
CharacterMap(const String& map_name);
CharacterMap(const String& map_name, const CharacterMapData& map_data);
#ifndef KERNEL
int set_system_map();
static Result<CharacterMap, OSError> fetch_system_map();
#endif
u32 get_char(KeyEvent);

View file

@ -49,13 +49,26 @@ int main(int argc, char** argv)
const char* path = nullptr;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(path, "The mapping file to be used", "file");
args_parser.add_positional_argument(path, "The mapping file to be used", "file", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
dbgln("path is at {:p}, and contains {}", path, path);
if (!path) {
auto keymap = Keyboard::CharacterMap::fetch_system_map();
if (keymap.is_error()) {
warnln("getkeymap: {}", keymap.error());
return 1;
}
outln("{}", keymap.value().character_map_name());
return 0;
}
Keyboard::CharacterMap character_map(path);
int rc = character_map.set_system_map();
if (rc != 0)
fprintf(stderr, "%s\n", strerror(-rc));
perror("setkeymap");
return rc;
}