Serenity Operating System
at hosted 134 lines 4.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/JsonObject.h> 28#include <LibCore/File.h> 29#include <stdio.h> 30 31#include <Kernel/Syscall.h> 32#include <AK/Optional.h> 33#include <AK/StdLibExtras.h> 34#include <AK/kmalloc.h> 35#include <stdio.h> 36#include <string.h> 37#include <sys/stat.h> 38#include <unistd.h> 39 40char* read_map(const JsonObject& json, const String& name) 41{ 42 if (!json.has(name)) 43 return nullptr; 44 45 char* map = new char[0x80](); 46 auto map_arr = json.get(name).as_array(); 47 48 for (int i = 0; i < map_arr.size(); i++) { 49 auto key_value = map_arr.at(i).as_string(); 50 char character = 0; 51 if (key_value.length() == 0) { 52 ; 53 } else if (key_value.length() == 1) { 54 character = key_value.characters()[0]; 55 } else if (key_value.length() == 4) { 56 // FIXME: Replace this workaround with "\u001B" in the keymap files 57 // after these kind of escape sequences are implemented in JsonParser. 58 if (key_value == "0x1B") { 59 character = 0x1B; 60 } 61 } else { 62 fprintf(stderr, "Unknown character in %s[%u] = %s.\n", name.characters(), i, key_value.characters()); 63 ASSERT_NOT_REACHED(); 64 } 65 66 map[i] = character; 67 } 68 69 return map; 70} 71 72RefPtr<Core::File> open_keymap_file(String& filename) 73{ 74 auto file = Core::File::construct(filename); 75 if (file->open(Core::IODevice::ReadOnly)) 76 return file; 77 78 if (!filename.ends_with(".json")) { 79 StringBuilder full_path; 80 full_path.append("/res/keymaps/"); 81 full_path.append(filename); 82 full_path.append(".json"); 83 filename = full_path.to_string(); 84 file = Core::File::construct(filename); 85 if (file->open(Core::IODevice::ReadOnly)) 86 return file; 87 } 88 89 return file; 90} 91 92int read_map_from_file(String& filename) 93{ 94 auto file = open_keymap_file(filename); 95 if (!file->is_open()) { 96 fprintf(stderr, "Failed to open %s: %s\n", filename.characters(), file->error_string()); 97 return 1; 98 } 99 100 auto file_contents = file->read_all(); 101 auto json = JsonValue::from_string(file_contents).as_object(); 102 103 char* map = read_map(json, "map"); 104 char* shift_map = read_map(json, "shift_map"); 105 char* alt_map = read_map(json, "alt_map"); 106 char* altgr_map = read_map(json, "altgr_map"); 107 108 if (!altgr_map) { 109 // AltGr map was not found, using Alt map as fallback. 110 altgr_map = alt_map; 111 } 112 113 Syscall::SC_setkeymap_params params { map, shift_map, alt_map, altgr_map }; 114 return syscall(SC_setkeymap, &params); 115} 116 117int main(int argc, char** argv) 118{ 119 if (argc != 2) { 120 fprintf(stderr, "usage: keymap <name|file>\n"); 121 return 0; 122 } 123 124 String filename = argv[1]; 125 int ret_val = read_map_from_file(filename); 126 127 if (ret_val == -EPERM) 128 fprintf(stderr, "Permission denied.\n"); 129 130 if (ret_val == 0) 131 fprintf(stderr, "New keymap loaded from \"%s\".\n", filename.characters()); 132 133 return ret_val; 134}