2020-06-18 21:00:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-18 21:00:19 +00:00
|
|
|
*/
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2022-01-19 15:20:42 +00:00
|
|
|
#include <AK/Vector.h>
|
2020-06-18 20:19:57 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-05-09 13:56:03 +00:00
|
|
|
#include <LibCore/ConfigFile.h>
|
2021-11-23 14:38:53 +00:00
|
|
|
#include <LibCore/System.h>
|
2020-06-18 20:19:57 +00:00
|
|
|
#include <LibKeyboard/CharacterMap.h>
|
2021-11-23 14:38:53 +00:00
|
|
|
#include <LibMain/Main.h>
|
2020-06-18 20:19:57 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
int set_keymap(ByteString const& keymap);
|
2022-01-19 15:20:42 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
int set_keymap(ByteString const& keymap)
|
2022-01-19 15:20:42 +00:00
|
|
|
{
|
|
|
|
auto character_map = Keyboard::CharacterMap::load_from_file(keymap);
|
|
|
|
if (character_map.is_error()) {
|
|
|
|
warnln("Cannot read keymap {}", keymap);
|
|
|
|
warnln("Hint: Must be a keymap name (e.g. 'en-us')");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = character_map.value().set_system_map();
|
|
|
|
if (rc != 0) {
|
|
|
|
perror("setkeymap");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:38:53 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-06-18 20:19:57 +00:00
|
|
|
{
|
2021-11-27 22:26:34 +00:00
|
|
|
TRY(Core::System::pledge("stdio setkeymap getkeymap rpath wpath cpath"));
|
2021-11-23 14:38:53 +00:00
|
|
|
TRY(Core::System::unveil("/res/keymaps", "r"));
|
|
|
|
TRY(Core::System::unveil("/etc/Keyboard.ini", "rwc"));
|
2021-05-09 13:56:03 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString mapping;
|
|
|
|
ByteString mappings;
|
2020-06-18 20:19:57 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2022-01-19 15:20:42 +00:00
|
|
|
args_parser.add_option(mapping, "The mapping to be used", "set-keymap", 'm', "keymap");
|
|
|
|
args_parser.add_option(mappings, "Comma separated list of enabled mappings", "set-keymaps", 's', "keymaps");
|
2021-11-23 14:38:53 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-06-18 20:19:57 +00:00
|
|
|
|
2021-11-23 14:38:53 +00:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-01-30 21:01:34 +00:00
|
|
|
|
2022-01-19 15:20:42 +00:00
|
|
|
if (mapping.is_empty() && mappings.is_empty()) {
|
2021-11-23 14:38:53 +00:00
|
|
|
auto keymap = TRY(Keyboard::CharacterMap::fetch_system_map());
|
|
|
|
outln("{}", keymap.character_map_name());
|
2021-01-30 21:01:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-06 13:33:42 +00:00
|
|
|
auto mapper_config = TRY(Core::ConfigFile::open("/etc/Keyboard.ini", Core::ConfigFile::AllowWriting::Yes));
|
2021-01-30 21:41:29 +00:00
|
|
|
|
2022-01-19 15:20:42 +00:00
|
|
|
if (!mappings.is_empty()) {
|
|
|
|
auto mappings_vector = mappings.split(',');
|
|
|
|
|
|
|
|
if (mappings_vector.is_empty()) {
|
|
|
|
warnln("Keymaps list should not be empty");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all specified keymaps are loadable
|
|
|
|
for (auto& keymap_name : mappings_vector) {
|
2023-02-09 18:26:53 +00:00
|
|
|
if (auto keymap = Keyboard::CharacterMap::load_from_file(keymap_name); keymap.is_error()) {
|
2022-01-19 15:20:42 +00:00
|
|
|
warnln("Cannot load keymap {}: {}({})", keymap_name, keymap.error().string_literal(), keymap.error().code());
|
2023-02-09 18:26:53 +00:00
|
|
|
return keymap.release_error();
|
2022-01-19 15:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
auto keymaps = ByteString::join(',', mappings_vector);
|
2022-01-19 15:20:42 +00:00
|
|
|
mapper_config->write_entry("Mapping", "Keymaps", keymaps);
|
2022-02-06 14:26:33 +00:00
|
|
|
TRY(mapper_config->sync());
|
2021-01-30 21:41:29 +00:00
|
|
|
}
|
2020-06-18 20:19:57 +00:00
|
|
|
|
2022-01-19 15:20:42 +00:00
|
|
|
auto keymaps = mapper_config->read_entry("Mapping", "Keymaps");
|
|
|
|
auto keymaps_vector = keymaps.split(',');
|
2021-05-09 13:56:03 +00:00
|
|
|
|
2022-01-19 15:20:42 +00:00
|
|
|
if (!mapping.is_empty()) {
|
|
|
|
if (keymaps_vector.is_empty()) {
|
|
|
|
warnln("No keymaps configured - writing default configurations (en-us)");
|
|
|
|
mapper_config->write_entry("Mapping", "Keymaps", "en-us");
|
2022-02-06 14:26:33 +00:00
|
|
|
TRY(mapper_config->sync());
|
2022-01-19 15:20:42 +00:00
|
|
|
keymaps_vector.append("en-us");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!keymaps_vector.find(mapping).is_end()) {
|
2022-05-19 22:18:01 +00:00
|
|
|
int rc = set_keymap(mapping);
|
|
|
|
if (rc == 0)
|
2022-01-19 15:20:42 +00:00
|
|
|
return rc;
|
|
|
|
} else {
|
|
|
|
warnln("Keymap '{}' is not in list of configured keymaps ({})", mapping, keymaps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 22:18:01 +00:00
|
|
|
return set_keymap(keymaps_vector.first());
|
2020-06-18 20:19:57 +00:00
|
|
|
}
|