Skip to content

Commit

Permalink
Keymap: Add ability to load keymap files by name
Browse files Browse the repository at this point in the history
  • Loading branch information
JamiKettunen authored and awesomekling committed Dec 30, 2019
1 parent 0b3f1e7 commit 76f0a74
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions Userland/keymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,30 @@ char* read_map(const JsonObject& json, const String& name)
return map;
}

int read_map_from_file(String filename)
RefPtr<CFile> 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;
}
Expand All @@ -61,7 +81,7 @@ int read_map_from_file(String filename)
int main(int argc, char** argv)
{
if (argc != 2) {
fprintf(stderr, "usage: keymap <file>\n");
fprintf(stderr, "usage: keymap <name|file>\n");
return 0;
}

Expand Down

0 comments on commit 76f0a74

Please sign in to comment.