| 1 | // SPDX-FileCopyrightText: 2009 Ingo Ruhnke <[email protected]> |
| 2 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | |
| 4 | #include <iostream> |
| 5 | #include <cstring> |
| 6 | #include <cstdlib> |
| 7 | |
| 8 | #include <gtkmm.h> |
| 9 | |
| 10 | #include "joystick_config_files.hpp" |
| 11 | #include "joystick_test_widget.hpp" |
| 12 | #include "main.hpp" |
| 13 | |
| 14 | Main* Main::current_ = 0; |
| 15 | bool m_verbose = false; |
| 16 | |
| 17 | #ifndef JSTEST_GTK_DATADIR |
| 18 | # define JSTEST_GTK_DATADIR "data/" |
| 19 | #endif |
| 20 | |
| 21 | #ifndef JSTEST_GTK_VERSION |
| 22 | # define JSTEST_GTK_VERSION "unknown" |
| 23 | #endif |
| 24 | |
| 25 | Main::Main() : |
| 26 | Gtk::Application("com.gmail.grumbel.jstest-gtk", Gio::APPLICATION_HANDLES_OPEN), |
| 27 | datadir(JSTEST_GTK_DATADIR), |
| 28 | m_simple_ui(false) |
| 29 | { |
| 30 | current_ = this; |
| 31 | } |
| 32 | |
| 33 | Main::~Main() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | int |
| 38 | Main::run(int argc, char** argv) |
| 39 | { |
| 40 | std::string device_file; |
| 41 | |
| 42 | for (int i = 1; i < argc; ++i) |
| 43 | { |
| 44 | if (strcmp("--help", argv[i]) == 0 || strcmp("-h", argv[i]) == 0) |
| 45 | { |
| 46 | std::cout << "Usage: " << argv[0] << " [OPTIONS]... [DEVICE]\n" |
| 47 | << "A graphical joystick tester.\n" |
| 48 | << "\n" |
| 49 | << "Options:\n" |
| 50 | << " -h, --help Display this help and exit\n" |
| 51 | << " -v, --version Display version information and exit\n" |
| 52 | << " --simple Hide graphical representation of axis\n" |
| 53 | << " --verbose Print useful extra information\n" |
| 54 | << " --datadir DIR Load application data from DIR\n" |
| 55 | << "\n" |
| 56 | << "Report bugs to Ingo Ruhnke <[email protected]>.\n"; |
| 57 | return 0; |
| 58 | } |
| 59 | else if (strcmp("--version", argv[i]) == 0 || strcmp("-v", argv[i]) == 0) |
| 60 | { |
| 61 | std::cout << "jstest-gtk " << JSTEST_GTK_VERSION << std::endl; |
| 62 | return 0; |
| 63 | } |
| 64 | else if (strcmp("--simple", argv[i]) == 0) |
| 65 | { |
| 66 | m_simple_ui = true; |
| 67 | } |
| 68 | else if (strcmp("--verbose", argv[i]) == 0) |
| 69 | { |
| 70 | m_verbose = true; |
| 71 | } |
| 72 | else if (strcmp("--datadir", argv[i]) == 0) |
| 73 | { |
| 74 | i += 1; |
| 75 | if (i >= argc) |
| 76 | { |
| 77 | std::cout << "Error: " << argv[0] << ": argument to --datadir is missing" << std::endl; |
| 78 | return EXIT_FAILURE; |
| 79 | } |
| 80 | datadir = argv[i]; |
| 81 | if (!datadir.empty() && datadir.back() != '/') |
| 82 | datadir += '/'; |
| 83 | } |
| 84 | else if (argv[i][0] == '-') |
| 85 | { |
| 86 | std::cout << "Error: " << argv[0] << ": unrecognized option '" << argv[i] << "'" << std::endl; |
| 87 | return EXIT_FAILURE; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | if (!device_file.empty()) |
| 92 | { |
| 93 | std::cout << "Error: " << argv[0] << ": multiple device files given, only one allowed: " << argv[i] << std::endl; |
| 94 | return EXIT_FAILURE; |
| 95 | } |
| 96 | device_file = argv[i]; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | joystick_configs = load_all_configs(datadir + "mappings"); |
| 101 | |
| 102 | try |
| 103 | { |
| 104 | JoystickTestWidget window(m_simple_ui, device_file); |
| 105 | window.show_all(); |
| 106 | return Gtk::Application::run(window); |
| 107 | } |
| 108 | catch (std::exception& err) |
| 109 | { |
| 110 | std::cout << "Error: " << err.what() << std::endl; |
| 111 | return EXIT_FAILURE; |
| 112 | } |
| 113 | catch (Glib::Exception& err) |
| 114 | { |
| 115 | std::cout << "Error: " << err.what() << std::endl; |
| 116 | return EXIT_FAILURE; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | int main(int argc, char** argv) |
| 121 | { |
| 122 | try |
| 123 | { |
| 124 | Glib::RefPtr<Main> app = Main::create(); |
| 125 | return app->run(argc, argv); |
| 126 | } |
| 127 | catch (std::exception& err) |
| 128 | { |
| 129 | std::cout << "Error: " << err.what() << std::endl; |
| 130 | return EXIT_FAILURE; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | Glib::RefPtr<Main> Main::create() |
| 135 | { |
| 136 | return Glib::RefPtr<Main>(new Main); |
| 137 | } |
| 138 | |
| 139 | /* EOF */ |