ladybird/Userland/keymap.cpp

99 lines
2.6 KiB
C++
Raw Normal View History

2019-11-23 10:47:50 +03:00
#include <AK/JsonObject.h>
#include <LibCore/CFile.h>
#include <stdio.h>
#include <Kernel/Syscall.h>
#include <AK/Optional.h>
#include <AK/StdLibExtras.h>
#include <AK/kmalloc.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
char* read_map(const JsonObject& json, const String& name)
{
char* map = new char[80];
auto map_arr = json.get(name).as_array();
2019-12-30 03:53:02 +03:00
for (int i = 0; i < map_arr.size(); i++) {
2019-11-23 10:47:50 +03:00
auto key_value = map_arr.at(i).as_string();
char character = 0;
2019-12-30 03:53:02 +03:00
if (key_value.length() == 0) {
2019-11-23 10:47:50 +03:00
;
2019-12-30 03:53:02 +03:00
} else if (key_value.length() == 1) {
2019-11-23 10:47:50 +03:00
character = key_value.characters()[0];
2019-12-30 03:53:02 +03:00
} else if (key_value.length() == 4) {
if (key_value == "0x08") {
2019-11-23 10:47:50 +03:00
character = 0x08;
2019-12-30 03:53:02 +03:00
} else if (key_value == "0x33") {
2019-11-23 10:47:50 +03:00
character = 0x33;
}
} else {
2019-12-30 03:53:02 +03:00
fprintf(stderr, "Unknown character in %s[%u] = %s.\n", name.characters(), i, key_value.characters());
2019-11-23 10:47:50 +03:00
ASSERT_NOT_REACHED();
}
2019-12-30 03:53:02 +03:00
2019-11-23 10:47:50 +03:00
map[i] = character;
}
return map;
}
RefPtr<CFile> open_keymap_file(String& filename)
2019-12-30 03:53:02 +03:00
{
2019-11-23 10:47:50 +03:00
auto file = CFile::construct(filename);
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()) {
2019-11-23 10:47:50 +03:00
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();
2019-12-30 03:53:02 +03:00
2019-11-23 10:47:50 +03:00
char* map = read_map(json, "map");
char* shift_map = read_map(json, "shift_map");
char* alt_map = read_map(json, "alt_map");
return syscall(SC_setkeymap, map, shift_map, alt_map);
}
int main(int argc, char** argv)
2019-12-30 03:53:02 +03:00
{
2019-11-23 10:47:50 +03:00
if (argc != 2) {
fprintf(stderr, "usage: keymap <name|file>\n");
2019-11-23 10:47:50 +03:00
return 0;
}
String filename = argv[1];
int ret_val = read_map_from_file(filename);
2019-12-30 03:53:02 +03:00
if (ret_val == -EPERM)
2019-11-23 10:47:50 +03:00
fprintf(stderr, "Permission denied.\n");
2019-12-30 03:53:02 +03:00
if (ret_val == 0)
2019-11-23 10:47:50 +03:00
fprintf(stderr, "New keymap loaded from \"%s\".\n", filename.characters());
return ret_val;
2019-12-30 03:53:02 +03:00
}