From 76f0a74b0c52a3ddfb1bc2bd7cf63ae536e6c4f3 Mon Sep 17 00:00:00 2001 From: Jami Kettunen Date: Mon, 30 Dec 2019 14:06:18 +0200 Subject: [PATCH] Keymap: Add ability to load keymap files by name --- Userland/keymap.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Userland/keymap.cpp b/Userland/keymap.cpp index 119c58387f..edbc999116 100644 --- a/Userland/keymap.cpp +++ b/Userland/keymap.cpp @@ -40,10 +40,30 @@ char* read_map(const JsonObject& json, const String& name) return map; } -int read_map_from_file(String filename) +RefPtr open_keymap_file(String& filename) { auto file = CFile::construct(filename); - if (!file->open(CIODevice::ReadOnly)) { + if (file->open(CIODevice::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 = CFile::construct(filename); + if (file->open(CIODevice::ReadOnly)) + return file; + } + + return file; +} + +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; } @@ -61,7 +81,7 @@ int read_map_from_file(String filename) int main(int argc, char** argv) { if (argc != 2) { - fprintf(stderr, "usage: keymap \n"); + fprintf(stderr, "usage: keymap \n"); return 0; }