Debian Codegraph @

src/joystick_test_widget.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2009 Ingo Ruhnke <[email protected]>
2 // SPDX-FileCopyrightText: 2025 Raphael Rosch <[email protected]>
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include <sstream>
6 #include <iostream>
7 #include <gtkmm/label.h>
8 #include <gtkmm/stock.h>
9 #include <gtkmm/dialog.h>
10 #include <glibmm/markup.h>
11 #include <glibmm/fileutils.h>
12
13 #include "main.hpp"
14 #include "joystick.hpp"
15 #include "button_widget.hpp"
16 #include "joystick_map_widget.hpp"
17 #include "joystick_calibration_widget.hpp"
18 #include "joystick_test_widget.hpp"
19 #include "joystick_config_files.hpp"
20
21 namespace {
22
23 class DeviceColumns : public Gtk::TreeModel::ColumnRecord
24 {
25 public:
26 DeviceColumns()
27 {
28 add(path);
29 add(markup);
30 }
31
32 Gtk::TreeModelColumn<Glib::ustring> path;
33 Gtk::TreeModelColumn<Glib::ustring> markup;
34 };
35
36 DeviceColumns& device_columns()
37 {
38 static DeviceColumns cols;
39 return cols;
40 }
41
42 Glib::ustring device_entry_markup(const Glib::ustring& name,
43 const Glib::ustring& path,
44 const Glib::ustring& usb_id)
45 {
46 return "<b>" + Glib::Markup::escape_text(name) + "</b>\n"
47 "Device: " + Glib::Markup::escape_text(path) + "\n"
48 "USB ID: " + Glib::Markup::escape_text(usb_id.empty() ? "unknown" : usb_id);
49 }
50
51 } // namespace
52
53 JoystickTestWidget::~JoystickTestWidget() = default;
54
55 JoystickTestWidget::JoystickTestWidget(bool simple_ui, const std::string& optional_device) :
56 Gtk::Window(),
57 m_simple_ui(simple_ui),
58 axis_frame("Axes"),
59 button_frame("Buttons"),
60 mapping_button("Mapping"),
61 calibration_button("Calibration"),
62 close_button(Gtk::Stock::CLOSE),
63 stick1_widget(128, 128),
64 stick2_widget(128, 128),
65 stick3_widget(128, 128),
66 rudder_widget(128, 32),
67 throttle_widget(32, 128),
68 left_trigger_widget(32, 128, true),
69 right_trigger_widget(32, 128, true)
70 {
71 set_title("jstest-gtk");
72 set_icon_from_file(Main::current()->get_data_directory() + "icons/generic.png");
73 set_default_size(520, 400);
74
75 axis_frame.set_border_width(5);
76 axis_table.set_border_width(5);
77 axis_table.set_spacings(5);
78 button_frame.set_border_width(5);
79 button_table.set_border_width(5);
80 button_table.set_spacings(8);
81 buttonbox.set_border_width(5);
82 stick_hbox.set_border_width(5);
83
84 m_device_store = Gtk::ListStore::create(device_columns());
85 m_device_combo.set_model(m_device_store);
86 m_device_combo.set_hexpand(true);
87 m_device_combo.set_focus_on_click(false);
88 {
89 auto* renderer = Gtk::manage(new Gtk::CellRendererText());
90 renderer->property_xalign() = 0.0;
91 // Multi-line rows in the popup and closed button
92 m_device_combo.pack_start(*renderer, true);
93 m_device_combo.add_attribute(renderer->property_markup(), device_columns().markup);
94 }
95 m_device_combo.signal_changed().connect(
96 sigc::mem_fun(this, &JoystickTestWidget::on_device_changed));
97
98 buttonbox.add(mapping_button);
99 buttonbox.add(calibration_button);
100 buttonbox.add(close_button);
101
102 test_hbox.pack_start(axis_frame, Gtk::PACK_EXPAND_WIDGET);
103 test_hbox.pack_start(button_frame, Gtk::PACK_EXPAND_WIDGET);
104
105 m_vbox.pack_start(m_device_combo, Gtk::PACK_SHRINK);
106 m_vbox.pack_start(test_hbox, Gtk::PACK_EXPAND_WIDGET);
107 m_vbox.pack_end(buttonbox, Gtk::PACK_SHRINK);
108 add(m_vbox);
109
110 axis_vbox.pack_start(stick_hbox, Gtk::PACK_SHRINK);
111 axis_vbox.add(axis_table);
112 axis_frame.add(axis_vbox);
113 button_frame.add(button_table);
114
115 calibration_button.signal_clicked().connect(sigc::mem_fun(this, &JoystickTestWidget::on_calibrate));
116 mapping_button.signal_clicked().connect(sigc::mem_fun(this, &JoystickTestWidget::on_mapping));
117 close_button.signal_clicked().connect([this]{ hide(); });
118
119 udev_monitor.reset(new UdevMonitor());
120 udev_monitor->signal_joystick_event.connect(
121 sigc::mem_fun(this, &JoystickTestWidget::on_udev_js_event));
122
123 refresh_device_list(optional_device);
124 close_button.grab_focus();
125 }
126
127 void
128 JoystickTestWidget::clear_device_ui()
129 {
130 m_axis_conn.disconnect();
131 m_button_conn.disconnect();
132
133 if (m_mapping_widget)
134 m_mapping_widget.reset();
135 if (m_calibration_widget)
136 m_calibration_widget.reset();
137
138 // Detach children from tables/hboxes (managed widgets are deleted with parents)
139 for (auto* child : axis_table.get_children())
140 axis_table.remove(*child);
141 for (auto* child : button_table.get_children())
142 button_table.remove(*child);
143 for (auto* child : stick_hbox.get_children())
144 stick_hbox.remove(*child);
145
146 axes.clear();
147 buttons.clear();
148 axis_callbacks.clear();
149 }
150
151 void
152 JoystickTestWidget::set_empty_state()
153 {
154 clear_device_ui();
155 m_joystick.reset();
156 m_connected = false;
157
158 m_updating_combo = true;
159 m_device_store->clear();
160 {
161 auto row = *m_device_store->append();
162 row[device_columns().path] = "none";
163 row[device_columns().markup] = "<i>No joystick connected</i>";
164 m_device_combo.set_active(row);
165 }
166 m_device_combo.set_sensitive(false);
167 m_updating_combo = false;
168
169 set_title("jstest-gtk");
170 mapping_button.set_sensitive(false);
171 calibration_button.set_sensitive(false);
172 }
173
174 void
175 JoystickTestWidget::build_device_ui()
176 {
177 if (!m_joystick)
178 return;
179
180 Joystick& joystick = *m_joystick;
181
182 set_title(joystick.get_name());
183
184 for (int i = 0; i < joystick.get_axis_count(); ++i)
185 {
186 std::ostringstream str;
187 str << "Axis " << i;
188 try {
189 if (!joystick.js_cfg.axes.empty() &&
190 i < static_cast<int>(joystick.js_cfg.axes.size()) &&
191 !joystick.js_cfg.axes.at(i).empty())
192 {
193 str << " [" << joystick.js_cfg.axes[i] << "] ";
194 }
195 } catch (const std::out_of_range&) {}
196 str << ": ";
197 auto* lbl = Gtk::manage(new Gtk::Label(str.str()));
198 lbl->set_xalign(0.0);
199
200 Gtk::ProgressBar& progressbar = *Gtk::manage(new Gtk::ProgressBar());
201 progressbar.set_fraction(0.5);
202
203 int x = (i / 10) * 2;
204 int y = i % 10;
205 axis_table.attach(*lbl, x, x + 1, y, y + 1, Gtk::FILL, Gtk::SHRINK);
206 axis_table.attach(progressbar, x + 1, x + 2, y, y + 1, Gtk::FILL | Gtk::EXPAND, Gtk::EXPAND);
207 axes.push_back(&progressbar);
208 }
209
210 for (int i = 0; i < joystick.get_button_count(); ++i)
211 {
212 int x = i / 10;
213 int y = i % 10;
214 std::ostringstream str;
215 str << i;
216 try {
217 if (!joystick.js_cfg.buttons.empty() &&
218 i < static_cast<int>(joystick.js_cfg.buttons.size()) &&
219 !joystick.js_cfg.buttons[i].empty())
220 {
221 str << " - " << joystick.js_cfg.buttons[i];
222 }
223 } catch (const std::out_of_range&) {}
224
225 auto* button = Gtk::manage(new ButtonWidget());
226 auto* blabel = Gtk::manage(new Gtk::Label(str.str()));
227 blabel->set_xalign(0.0);
228 blabel->set_margin_start(10);
229 blabel->set_margin_end(10);
230 button->add(*blabel);
231 button_table.attach(*button, x, x + 1, y, y + 1, Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND);
232 buttons.push_back(button);
233 }
234
235 axis_callbacks.clear();
236 for (int i = 0; i < joystick.get_axis_count(); ++i)
237 axis_callbacks.push_back(sigc::signal<void, double>());
238
239 if (m_verbose)
240 {
241 std::cout << "joystick.get_name(): " << joystick.get_name() << std::endl;
242 std::cout << "joystick.get_usb_id(): " << joystick.get_usb_id() << std::endl;
243 std::cout << "joystick.get_js_type(): " << joystick.get_js_type() << std::endl;
244 std::cout << "joystick.get_axis_count(): " << joystick.get_axis_count() << std::endl;
245 }
246
247 if (joystick.get_js_type() == "ps4-dualshock4")
248 setup_dualshock4_equiv();
249 else if (joystick.get_js_type() == "ps3-sixaxis")
250 setup_sixaxis_equiv();
251 else if (joystick.get_js_type() == "ps2-dualshock2")
252 setup_dualshock2_equiv();
253 else if (joystick.get_js_type() == "xbox360")
254 setup_xbox360_equiv();
255 else if (joystick.get_js_type() == "gamecube")
256 setup_gamecube_equiv();
257 else
258 {
259 switch (joystick.get_axis_count())
260 {
261 case 2:
262 setup_joystick_widgets(1, {0, 1}, {});
263 break;
264 case 6:
265 {
266 Gtk::Table& table = *Gtk::manage(new Gtk::Table(2, 2));
267 table.attach(stick1_widget, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
268 table.attach(rudder_widget, 0, 1, 1, 2, Gtk::SHRINK, Gtk::SHRINK);
269 table.attach(throttle_widget, 1, 2, 0, 1, Gtk::SHRINK, Gtk::SHRINK);
270 stick_hbox.pack_start(table, Gtk::PACK_EXPAND_PADDING);
271 stick_hbox.pack_start(stick3_widget, Gtk::PACK_EXPAND_PADDING);
272 axis_callbacks[0].connect(sigc::mem_fun(stick1_widget, &AxisWidget::set_x_axis));
273 axis_callbacks[1].connect(sigc::mem_fun(stick1_widget, &AxisWidget::set_y_axis));
274 axis_callbacks[2].connect(sigc::mem_fun(rudder_widget, &RudderWidget::set_pos));
275 axis_callbacks[3].connect(sigc::mem_fun(throttle_widget, &ThrottleWidget::set_pos));
276 axis_callbacks[4].connect(sigc::mem_fun(stick3_widget, &AxisWidget::set_x_axis));
277 axis_callbacks[5].connect(sigc::mem_fun(stick3_widget, &AxisWidget::set_y_axis));
278 break;
279 }
280 case 7:
281 setup_joystick_widgets(3, {0, 1, 3, 4, 5, 6}, {});
282 break;
283 case 8:
284 setup_joystick_widgets(3, {0, 1, 2, 3, 6, 7}, {4, 5});
285 break;
286 default:
287 if (m_verbose)
288 std::cout << "No graphical stick layout for this axis count." << std::endl;
289 break;
290 }
291 }
292
293 if (m_simple_ui)
294 {
295 // hide stick graphics by removing from axis_vbox if packed
296 if (stick_hbox.get_parent())
297 axis_vbox.remove(stick_hbox);
298 }
299 else if (!stick_hbox.get_parent())
300 {
301 axis_vbox.pack_start(stick_hbox, Gtk::PACK_SHRINK);
302 axis_vbox.reorder_child(stick_hbox, 0);
303 }
304
305 axis_table.show_all();
306 button_table.show_all();
307 stick_hbox.show_all();
308
309 m_axis_conn = joystick.axis_move.connect(
310 sigc::mem_fun(this, &JoystickTestWidget::axis_move));
311 m_button_conn = joystick.button_press.connect(
312 sigc::mem_fun(this, &JoystickTestWidget::button_press));
313
314 m_connected = true;
315 mapping_button.set_sensitive(true);
316 calibration_button.set_sensitive(true);
317 }
318
319 void
320 JoystickTestWidget::refresh_device_list(const std::string& prefer_path)
321 {
322 std::string previous;
323 if (m_joystick)
324 previous = m_joystick->get_filename();
325 if (!prefer_path.empty())
326 previous = prefer_path;
327
328 struct Entry { std::string path; std::string js_id; std::string name; std::string usb_id; };
329 std::vector<Entry> found;
330
331 for (int i = 0; i < 32; ++i)
332 {
333 std::string js_id = "js" + std::to_string(i);
334 std::string path = "/dev/input/" + js_id;
335 if (!Glib::file_test(path, Glib::FILE_TEST_EXISTS))
336 continue;
337 try
338 {
339 Joystick probe(path, js_id);
340 found.push_back({path, js_id, probe.get_name(), probe.get_usb_id()});
341 }
342 catch (const std::exception& err)
343 {
344 if (m_verbose)
345 std::cout << err.what() << std::endl;
346 }
347 }
348
349 if (found.empty())
350 {
351 set_empty_state();
352 return;
353 }
354
355 m_updating_combo = true;
356 m_device_combo.set_sensitive(true);
357 m_device_store->clear();
358
359 Gtk::TreeModel::iterator active_it;
360 for (const auto& e : found)
361 {
362 auto it = m_device_store->append();
363 auto row = *it;
364 row[device_columns().path] = e.path;
365 row[device_columns().markup] = device_entry_markup(e.name, e.path, e.usb_id);
366 if (!active_it)
367 active_it = it;
368 if (!previous.empty() && e.path == previous)
369 active_it = it;
370 }
371 m_device_combo.set_active(active_it);
372 m_updating_combo = false;
373
374 on_device_changed();
375 }
376
377 Glib::ustring
378 JoystickTestWidget::get_selected_device_path() const
379 {
380 auto it = m_device_combo.get_active();
381 if (!it)
382 return {};
383 return (*it)[device_columns().path];
384 }
385
386 void
387 JoystickTestWidget::on_device_changed()
388 {
389 if (m_updating_combo)
390 return;
391
392 Glib::ustring id = get_selected_device_path();
393 if (id.empty() || id == "none")
394 {
395 set_empty_state();
396 return;
397 }
398
399 clear_device_ui();
400 m_joystick.reset();
401
402 std::string js_id = get_js_dev_id_from_filename(id);
403 try
404 {
405 m_joystick.reset(new Joystick(id, js_id));
406 build_device_ui();
407 }
408 catch (const std::exception& err)
409 {
410 std::cout << err.what() << std::endl;
411 set_empty_state();
412 }
413 }
414
415 void
416 JoystickTestWidget::schedule_refresh()
417 {
418 if (m_refresh_timeout.connected())
419 return;
420 m_refresh_timeout = Glib::signal_timeout().connect(
421 sigc::mem_fun(this, &JoystickTestWidget::on_refresh_timeout), 250);
422 }
423
424 bool
425 JoystickTestWidget::on_refresh_timeout()
426 {
427 std::string prefer;
428 if (m_joystick)
429 prefer = m_joystick->get_filename();
430 refresh_device_list(prefer);
431 return false;
432 }
433
434 void
435 JoystickTestWidget::on_udev_js_event(const std::string& action, const std::string& devnode)
436 {
437 if (m_verbose)
438 std::cout << "joystick_test_widget " << action << ": " << devnode << std::endl;
439 schedule_refresh();
440 }
441
442 void
443 JoystickTestWidget::on_calibrate()
444 {
445 if (!m_joystick || !m_connected)
446 return;
447 if (m_calibration_widget)
448 {
449 m_calibration_widget->present();
450 return;
451 }
452 m_calibration_widget.reset(new JoystickCalibrationWidget(*m_joystick));
453 m_calibration_widget->signal_hide().connect([this] { m_calibration_widget.reset(); });
454 m_calibration_widget->set_transient_for(*this);
455 m_calibration_widget->show_all();
456 }
457
458 void
459 JoystickTestWidget::on_mapping()
460 {
461 if (!m_joystick || !m_connected)
462 return;
463 if (m_mapping_widget)
464 {
465 m_mapping_widget->present();
466 return;
467 }
468 m_mapping_widget.reset(new JoystickMapWidget(*m_joystick));
469 m_mapping_widget->signal_hide().connect([this] { m_mapping_widget.reset(); });
470 m_mapping_widget->set_transient_for(*this);
471 m_mapping_widget->show_all();
472 }
473
474 void
475 JoystickTestWidget::setup_joystick_widgets(const unsigned sticks, const std::vector<unsigned>& axes, const std::vector<u_int>& triggers)
476 { // eg: setup_joystick_widgets(2, {0,1,3,4}, {}); => 2 sticks, with axes 0,1 and 3,4, and no triggers
477 try {
478 if (sticks >= 1) {
479 stick_hbox.pack_start(stick1_widget, Gtk::PACK_EXPAND_PADDING);
480 axis_callbacks.at(axes.at(0)).connect(sigc::mem_fun(stick1_widget, &AxisWidget::set_x_axis));
481 axis_callbacks.at(axes.at(1)).connect(sigc::mem_fun(stick1_widget, &AxisWidget::set_y_axis));
482 }
483 if (sticks >= 2) {
484 stick_hbox.pack_start(stick2_widget, Gtk::PACK_EXPAND_PADDING);
485 axis_callbacks.at(axes.at(2)).connect(sigc::mem_fun(stick2_widget, &AxisWidget::set_x_axis));
486 axis_callbacks.at(axes.at(3)).connect(sigc::mem_fun(stick2_widget, &AxisWidget::set_y_axis));
487 }
488 if (sticks >= 3) {
489 stick_hbox.pack_start(stick3_widget, Gtk::PACK_EXPAND_PADDING);
490 axis_callbacks.at(axes.at(4)).connect(sigc::mem_fun(stick3_widget, &AxisWidget::set_x_axis));
491 axis_callbacks.at(axes.at(5)).connect(sigc::mem_fun(stick3_widget, &AxisWidget::set_y_axis));
492 }
493 }
494 catch (const std::out_of_range& e) {
495 std::cout << "joystick configuration error. Some axis data missing or out of range." << std::endl;
496 if (m_verbose)
497 std::cout << e.what() << std::endl;
498 if (m_verbose)
499 std::cout << "ERROR: axis config data" << std::endl;
500 }
501
502 try {
503 if (triggers.size() >= 1) {
504 stick_hbox.pack_start(left_trigger_widget, Gtk::PACK_EXPAND_PADDING);
505 axis_callbacks.at(triggers.at(0)).connect(sigc::mem_fun(left_trigger_widget, &ThrottleWidget::set_pos));
506 }
507 if (triggers.size() >= 2) {
508 stick_hbox.pack_start(right_trigger_widget, Gtk::PACK_EXPAND_PADDING);
509 axis_callbacks.at(triggers.at(1)).connect(sigc::mem_fun(right_trigger_widget, &ThrottleWidget::set_pos));
510 }
511 }
512 catch (const std::out_of_range& e) {
513 std::cout << "joystick configuration error. Some trigger data missing or out of range." << std::endl;
514 if (m_verbose)
515 std::cout << e.what() << std::endl;
516 if (m_verbose)
517 std::cout << "ERROR: trigger config data" << std::endl;
518 }
519 }
520
521
522 // (sticks, axes, triggers)
523 // eg: setup_joystick_widgets(2, {0,1,3,4}, {}); => 2 sticks, with axes 0,1 and 3,4, and no triggers
524 void
525 JoystickTestWidget::setup_sixaxis_equiv()
526 {
527 setup_joystick_widgets(2, {0,1,3,4}, {2,5});
528 }
529
530 void
531 JoystickTestWidget::setup_dualshock4_equiv()
532 {
533 setup_joystick_widgets(3, {0,1,3,4,6,7}, {2,5});
534 }
535
536 void
537 JoystickTestWidget::setup_dualshock2_equiv()
538 {
539 setup_joystick_widgets(2, {0,1,3,2}, {});
540 }
541
542 void
543 JoystickTestWidget::setup_xbox360_equiv()
544 {
545 setup_joystick_widgets(3, {0,1,3,4,6,7}, {2,5});
546 }
547
548 void
549 JoystickTestWidget::setup_gamecube_equiv()
550 {
551 setup_joystick_widgets(3, {0,1,5,2,6,7}, {3,4});
552 }
553
554
555 void
556 JoystickTestWidget::axis_move(int number, int value)
557 {
558 if (number < 0 || number >= static_cast<int>(axes.size()))
559 return;
560 axes.at(number)->set_fraction((value + 32767) / (double)(2*32767));
561
562 std::ostringstream str;
563 str << value;
564 axes.at(number)->set_text(str.str());
565 axis_callbacks[number](value / 32767.0);
566 }
567
568 void
569 JoystickTestWidget::button_press(int number, bool value)
570 {
571 if (number < 0 || number >= static_cast<int>(buttons.size()))
572 return;
573 buttons.at(number)->set_active(value);
574 }
575