diff --git a/Userland/CMakeLists.txt b/Userland/CMakeLists.txt index 5ea7c15358e..797900c1a29 100644 --- a/Userland/CMakeLists.txt +++ b/Userland/CMakeLists.txt @@ -14,6 +14,7 @@ target_link_libraries(disasm LibX86) target_link_libraries(functrace LibDebug LibX86) target_link_libraries(html LibWeb) target_link_libraries(ht LibWeb) +target_link_libraries(keymap LibKeyboard) target_link_libraries(lspci LibPCIDB) target_link_libraries(man LibMarkdown) target_link_libraries(md LibMarkdown) diff --git a/Userland/keymap.cpp b/Userland/keymap.cpp index 72630c35df7..6c2eefeefdf 100644 --- a/Userland/keymap.cpp +++ b/Userland/keymap.cpp @@ -24,98 +24,9 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include +#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static Vector read_map(const JsonObject& json, const String& name) -{ - if (!json.has(name)) - return {}; - - Vector map; - map.resize(0x80); - auto map_arr = json.get(name).as_array(); - - for (int i = 0; i < map_arr.size(); i++) { - auto key_value = map_arr.at(i).as_string(); - char character = 0; - if (key_value.length() == 0) { - ; - } else if (key_value.length() == 1) { - character = key_value.characters()[0]; - } else if (key_value.length() == 4) { - // FIXME: Replace this workaround with "\u001B" in the keymap files - // after these kind of escape sequences are implemented in JsonParser. - if (key_value == "0x1B") { - character = 0x1B; - } - } else { - fprintf(stderr, "Unknown character in %s[%u] = %s.\n", name.characters(), i, key_value.characters()); - ASSERT_NOT_REACHED(); - } - - map[i] = character; - } - - return map; -} - -static RefPtr open_keymap_file(String& filename) -{ - auto file = Core::File::construct(filename); - if (file->open(Core::IODevice::ReadOnly)) - return file; - - if (!filename.ends_with(".json")) { - StringBuilder full_path; - full_path.append("/res/keymaps/"); - full_path.append(filename); - full_path.append(".json"); - filename = full_path.to_string(); - file = Core::File::construct(filename); - if (file->open(Core::IODevice::ReadOnly)) - return file; - } - - return file; -} - -static int read_map_from_file(String& filename) -{ - auto file = open_keymap_file(filename); - if (!file->is_open()) { - fprintf(stderr, "Failed to open %s: %s\n", filename.characters(), file->error_string()); - return 1; - } - - auto file_contents = file->read_all(); - auto json = JsonValue::from_string(file_contents).as_object(); - - auto map = read_map(json, "map"); - auto shift_map = read_map(json, "shift_map"); - auto alt_map = read_map(json, "alt_map"); - auto altgr_map = read_map(json, "altgr_map"); - - if (altgr_map.is_empty()) { - // AltGr map was not found, using Alt map as fallback. - altgr_map = alt_map; - } - - Syscall::SC_setkeymap_params params { map.data(), shift_map.data(), alt_map.data(), altgr_map.data() }; - return syscall(SC_setkeymap, ¶ms); -} - int main(int argc, char** argv) { if (argc != 2) { @@ -124,7 +35,9 @@ int main(int argc, char** argv) } String filename = argv[1]; - int ret_val = read_map_from_file(filename); + + Keyboard::CharacterMap character_map(filename); + int ret_val = character_map.set_system_map(); if (ret_val == -EPERM) fprintf(stderr, "Permission denied.\n");