main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  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. #include "KeyboardMapperWidget.h"
  27. #include <LibCore/ArgsParser.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/Application.h>
  30. #include <LibGUI/FilePicker.h>
  31. #include <LibGUI/Icon.h>
  32. #include <LibGUI/Menu.h>
  33. #include <LibGUI/MenuBar.h>
  34. int main(int argc, char** argv)
  35. {
  36. const char* path = nullptr;
  37. Core::ArgsParser args_parser;
  38. args_parser.add_positional_argument(path, "Keyboard character mapping file.", "file", Core::ArgsParser::Required::No);
  39. args_parser.parse(argc, argv);
  40. if (pledge("stdio thread rpath accept cpath wpath recvfd sendfd unix fattr", nullptr) < 0) {
  41. perror("pledge");
  42. return 1;
  43. }
  44. auto app = GUI::Application::construct(argc, argv);
  45. if (pledge("stdio thread rpath accept cpath wpath recvfd sendfd", nullptr) < 0) {
  46. perror("pledge");
  47. return 1;
  48. }
  49. auto app_icon = GUI::Icon::default_icon("app-keyboard-mapper");
  50. auto window = GUI::Window::construct();
  51. window->set_title("Keyboard Mapper");
  52. window->set_icon(app_icon.bitmap_for_size(16));
  53. window->set_main_widget<KeyboardMapperWidget>();
  54. window->resize(775, 315);
  55. window->set_resizable(false);
  56. window->show();
  57. auto keyboard_mapper_widget = (KeyboardMapperWidget*)window->main_widget();
  58. if (path != nullptr) {
  59. keyboard_mapper_widget->load_from_file(path);
  60. } else {
  61. keyboard_mapper_widget->load_from_system();
  62. }
  63. // Actions
  64. auto open_action = GUI::CommonActions::make_open_action(
  65. [&](auto&) {
  66. Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open");
  67. if (path.has_value()) {
  68. keyboard_mapper_widget->load_from_file(path.value());
  69. }
  70. });
  71. auto save_action = GUI::CommonActions::make_save_action(
  72. [&](auto&) {
  73. keyboard_mapper_widget->save();
  74. });
  75. auto save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  76. String name = "Unnamed";
  77. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "json");
  78. if (!save_path.has_value())
  79. return;
  80. keyboard_mapper_widget->save_to_file(save_path.value());
  81. });
  82. auto quit_action = GUI::CommonActions::make_quit_action(
  83. [&](auto&) {
  84. app->quit();
  85. });
  86. // Menu
  87. auto menubar = GUI::MenuBar::construct();
  88. auto& app_menu = menubar->add_menu("Keyboard Mapper");
  89. app_menu.add_action(open_action);
  90. app_menu.add_action(save_action);
  91. app_menu.add_action(save_as_action);
  92. app_menu.add_separator();
  93. app_menu.add_action(quit_action);
  94. auto& help_menu = menubar->add_menu("Help");
  95. help_menu.add_action(GUI::CommonActions::make_about_action("Keyboard Mapper", app_icon, window));
  96. app->set_menubar(move(menubar));
  97. return app->exec();
  98. }