From 0e3408d4d603ab3d1945818ed5875eaf5b4c6b4b Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 30 Jan 2021 22:41:29 +0100 Subject: LibKeyboard: Don't assert on failure --- Kernel/Devices/KeyboardDevice.h | 2 +- Userland/Libraries/LibKeyboard/CharacterMap.cpp | 20 +++++++++----------- Userland/Libraries/LibKeyboard/CharacterMap.h | 2 +- Userland/Utilities/keymap.cpp | 13 ++++++++++--- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Kernel/Devices/KeyboardDevice.h b/Kernel/Devices/KeyboardDevice.h index d358f70bf3..11e3b4d65e 100644 --- a/Kernel/Devices/KeyboardDevice.h +++ b/Kernel/Devices/KeyboardDevice.h @@ -105,7 +105,7 @@ private: bool m_has_e0_prefix { false }; EntropySource m_entropy_source; - Keyboard::CharacterMap m_character_map = Keyboard::CharacterMap("en"); + Keyboard::CharacterMap m_character_map = Keyboard::CharacterMap("en", Keyboard::default_character_map); }; class KeyboardClient { diff --git a/Userland/Libraries/LibKeyboard/CharacterMap.cpp b/Userland/Libraries/LibKeyboard/CharacterMap.cpp index 3ea6604df2..059c6da20f 100644 --- a/Userland/Libraries/LibKeyboard/CharacterMap.cpp +++ b/Userland/Libraries/LibKeyboard/CharacterMap.cpp @@ -27,24 +27,22 @@ #include "CharacterMap.h" #include #include -#ifndef KERNEL -# include -#endif +#include namespace Keyboard { -CharacterMap::CharacterMap(const String& map_name) +#ifndef KERNEL +// The Kernel explicitly and exclusively links only this file into it. +// Thus, we cannot even include a reference to the symbol `CharacterMapFile::load_from_file`. +Optional CharacterMap::load_from_file(const String& map_name) { -#ifdef KERNEL - m_character_map_data = default_character_map; -#else auto result = CharacterMapFile::load_from_file(map_name); - ASSERT(result.has_value()); + if (!result.has_value()) + return {}; - m_character_map_data = result.value(); -#endif - m_character_map_name = map_name; + return CharacterMap(map_name, result.value()); } +#endif CharacterMap::CharacterMap(const String& map_name, const CharacterMapData& map_data) : m_character_map_data(map_data) diff --git a/Userland/Libraries/LibKeyboard/CharacterMap.h b/Userland/Libraries/LibKeyboard/CharacterMap.h index 848b32a7fa..5a45c34fdb 100644 --- a/Userland/Libraries/LibKeyboard/CharacterMap.h +++ b/Userland/Libraries/LibKeyboard/CharacterMap.h @@ -39,8 +39,8 @@ namespace Keyboard { class CharacterMap { public: - CharacterMap(const String& map_name); CharacterMap(const String& map_name, const CharacterMapData& map_data); + static Optional load_from_file(const String& file_name); #ifndef KERNEL int set_system_map(); diff --git a/Userland/Utilities/keymap.cpp b/Userland/Utilities/keymap.cpp index f32d27f2c3..47c4b00623 100644 --- a/Userland/Utilities/keymap.cpp +++ b/Userland/Utilities/keymap.cpp @@ -65,10 +65,17 @@ int main(int argc, char** argv) return 0; } - Keyboard::CharacterMap character_map(path); - int rc = character_map.set_system_map(); - if (rc != 0) + auto character_map = Keyboard::CharacterMap::load_from_file(path); + if (!character_map.has_value()) { + warnln("Cannot read keymap {}", path); + warnln("Hint: Must be either a keymap name (e.g. 'en') or a full path."); + return 1; + } + + int rc = character_map.value().set_system_map(); + if (rc != 0) { perror("setkeymap"); + } return rc; } -- cgit v1.2.3