Debian Codegraph @

src/main.cpp

raw
1 /*
2 ** jstest-gtk - A graphical joystick tester
3 ** Copyright (C) 2009 Ingo Ruhnke <[email protected]>
4 **
5 ** This program is free software: you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation, either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <iostream>
20 #include <gtkmm.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include "joystick_test_widget.hpp"
28 #include "joystick_list_widget.hpp"
29 #include "joystick_map_widget.hpp"
30 #include "joystick_calibration_widget.hpp"
31 #include "joystick.hpp"
32 #include "main.hpp"
33
34 Main* Main::current_ = 0;
35
36 JoystickGui::JoystickGui(std::unique_ptr<Joystick> joystick, bool simple_ui, Gtk::Window* parent) :
37 m_joystick(std::move(joystick)),
38 m_test_widget(),
39 m_mapping_widget(),
40 m_calibration_widget()
41 {
42 m_test_widget = std::unique_ptr<JoystickTestWidget>(new JoystickTestWidget(*this, *m_joystick, simple_ui));
43 if (parent) {
44 m_test_widget->set_transient_for(*parent);
45 }
46
47 m_test_widget->show_all();
48 }
49
50 void
51 JoystickGui::show_calibration_dialog()
52 {
53 if (m_calibration_widget)
54 {
55 m_calibration_widget->present();
56 }
57 else
58 {
59 m_calibration_widget.reset(new JoystickCalibrationWidget(*m_joystick));
60 m_calibration_widget->signal_hide().connect([this] { m_calibration_widget.reset(); });
61 m_calibration_widget->set_transient_for(*m_test_widget);
62 m_calibration_widget->show_all();
63 }
64 }
65
66 void
67 JoystickGui::show_mapping_dialog()
68 {
69 if (m_mapping_widget)
70 {
71 m_mapping_widget->present();
72 }
73 else
74 {
75 m_mapping_widget.reset(new JoystickMapWidget(*m_joystick));
76 m_mapping_widget->signal_hide().connect([this] { m_calibration_widget.reset(); });
77 m_mapping_widget->set_transient_for(*m_test_widget);
78 m_mapping_widget->show_all();
79 }
80 }
81
82
83 Main::Main() :
84 Gtk::Application("com.gmail.grumbel.jstest-gtk", Gio::APPLICATION_HANDLES_OPEN),
85 datadir("data/"),
86 m_simple_ui(false)
87 {
88 current_ = this;
89 }
90
91 Main::~Main()
92 {
93 }
94
95 JoystickTestWidget*
96 Main::show_device_property_dialog(const std::string& filename, Gtk::Window* parent)
97 {
98 auto it = m_joystick_guis.find(filename);
99 if (it != m_joystick_guis.end())
100 {
101 JoystickTestWidget* widget = it->second->get_test_widget();
102 widget->present();
103 return widget;
104 }
105 else
106 {
107 std::unique_ptr<Joystick> joystick(new Joystick(filename));
108 std::unique_ptr<JoystickGui> gui(new JoystickGui(std::move(joystick), m_simple_ui, parent));
109
110 JoystickTestWidget* widget = gui->get_test_widget();
111 m_joystick_guis[filename] = std::move(gui);
112 widget->signal_hide().connect([this, filename]{
113 m_joystick_guis.erase(filename);
114 });
115
116 return widget;
117 }
118 }
119
120 int
121 Main::run(int argc, char** argv)
122 {
123 typedef std::vector<std::string> DeviceFiles;
124 DeviceFiles device_files;
125
126 for(int i = 1; i < argc; ++i)
127 {
128 if (strcmp("--help", argv[i]) == 0 ||
129 strcmp("-h", argv[i]) == 0)
130 {
131 std::cout << "Usage: " << argv[0] << " [OPTIONS]... [DEVICE]\n"
132 << "A graphical joystick tester.\n"
133 << "\n"
134 << "Options:\n"
135 << " -h, --help Display this help and exit\n"
136 << " -v, --version Display version information and exit\n"
137 << " --simple Hide graphical representation of axis\n"
138 << " --datadir DIR Load application data from DIR\n"
139 << "\n"
140 << "Report bugs to Ingo Ruhnke <[email protected]>.\n";
141 return 0;
142 }
143 else if (strcmp("--version", argv[i]) == 0 ||
144 strcmp("-v", argv[i]) == 0)
145 {
146 std::cout << "jstest-gtk 0.1.1" << std::endl;
147 return 0;
148 }
149 else if (strcmp("--simple", argv[i]) == 0)
150 {
151 m_simple_ui = true;
152 }
153 else if (strcmp("--datadir", argv[i]) == 0)
154 {
155 i += 1;
156 if (i >= argc)
157 {
158 std::cout << "Error: " << argv[0] << ": argument to --datadir is missing" << std::endl;
159 return EXIT_FAILURE;
160 }
161 else
162 {
163 datadir = argv[i];
164 }
165 }
166 else if (argv[i][0] == '-')
167 {
168 std::cout << "Error: " << argv[0] << ": unrecognized option '" << argv[i] << "'" << std::endl;
169 return EXIT_FAILURE;
170 }
171 else
172 {
173 if (!device_files.empty())
174 {
175 std::cout << "Error: " << argv[0] << ": multiple device files given, only one allowed: " << argv[i] << std::endl;
176 return EXIT_FAILURE;
177 }
178
179 device_files.push_back(argv[i]);
180 }
181 }
182
183 try
184 {
185 if (device_files.empty())
186 {
187 JoystickListWidget list_dialog;
188 list_dialog.show_all();
189 return Gtk::Application::run(list_dialog);
190 }
191 else
192 {
193 auto dialog = show_device_property_dialog(*device_files.begin());
194 return Gtk::Application::run(*dialog);
195 }
196 }
197 catch(std::exception& err)
198 {
199 std::cout << "Error: " << err.what() << std::endl;
200 return EXIT_FAILURE;
201 }
202 catch(Glib::Exception& err)
203 {
204 std::cout << "Error: " << err.what() << std::endl;
205 return EXIT_FAILURE;
206 }
207
208 return 0;
209 }
210
211 int main(int argc, char** argv)
212 {
213 try
214 {
215 Glib::RefPtr<Main> app = Main::create();
216
217 return app->run(argc, argv);
218 }
219 catch(std::exception& err)
220 {
221 std::cout << "Error: " << err.what() << std::endl;
222 return EXIT_FAILURE;
223 }
224 }
225
226 Glib::RefPtr<Main> Main::create()
227 {
228 return Glib::RefPtr<Main>(new Main);
229 }
230
231 /* EOF */