keymap.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/ArgsParser.h>
  7. #include <LibCore/ConfigFile.h>
  8. #include <LibCore/System.h>
  9. #include <LibKeyboard/CharacterMap.h>
  10. #include <LibMain/Main.h>
  11. #include <stdio.h>
  12. ErrorOr<int> serenity_main(Main::Arguments arguments)
  13. {
  14. TRY(Core::System::pledge("stdio setkeymap getkeymap rpath wpath cpath"));
  15. TRY(Core::System::unveil("/res/keymaps", "r"));
  16. TRY(Core::System::unveil("/etc/Keyboard.ini", "rwc"));
  17. const char* path = nullptr;
  18. Core::ArgsParser args_parser;
  19. args_parser.add_positional_argument(path, "The mapping file to be used", "file", Core::ArgsParser::Required::No);
  20. args_parser.parse(arguments);
  21. if (path && path[0] == '/')
  22. TRY(Core::System::unveil(path, "r"));
  23. TRY(Core::System::unveil(nullptr, nullptr));
  24. if (!path) {
  25. auto keymap = TRY(Keyboard::CharacterMap::fetch_system_map());
  26. outln("{}", keymap.character_map_name());
  27. return 0;
  28. }
  29. auto character_map = Keyboard::CharacterMap::load_from_file(path);
  30. if (character_map.is_error()) {
  31. warnln("Cannot read keymap {}", path);
  32. warnln("Hint: Must be either a keymap name (e.g. 'en-us') or a full path.");
  33. return 1;
  34. }
  35. int rc = character_map.value().set_system_map();
  36. if (rc != 0) {
  37. perror("setkeymap");
  38. return rc;
  39. }
  40. auto mapper_config(Core::ConfigFile::open("/etc/Keyboard.ini", Core::ConfigFile::AllowWriting::Yes));
  41. mapper_config->write_entry("Mapping", "Keymap", path);
  42. mapper_config->sync();
  43. return rc;
  44. }