| 1 | // SPDX-FileCopyrightText: 2009 Ingo Ruhnke <[email protected]> |
| 2 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | |
| 4 | #include <iostream> |
| 5 | #include <assert.h> |
| 6 | #include <gtkmm/spinbutton.h> |
| 7 | #include <gtkmm/stock.h> |
| 8 | |
| 9 | #include "joystick.hpp" |
| 10 | #include "calibrate_maximum_dialog.hpp" |
| 11 | #include "joystick_calibration_widget.hpp" |
| 12 | |
| 13 | JoystickCalibrationWidget::JoystickCalibrationWidget(Joystick& joystick) |
| 14 | : Gtk::Dialog("Calibration: " + joystick.get_name()), |
| 15 | joystick(joystick), |
| 16 | label("The <i>center</i> values are the minimum and the maximum values of the deadzone.\n" |
| 17 | "The <i>min</i> and <i>max</i> values refer to the outer values. You have to unplug\n" |
| 18 | "your joystick or reboot to reset the values to their original default.\n" |
| 19 | "\n" |
| 20 | "To run the calibration wizard, press the <i>Calibrate</i> button."), |
| 21 | axis_frame("Axes"), |
| 22 | axis_table(joystick.get_axis_count() + 1, 5), |
| 23 | buttonbox(Gtk::BUTTONBOX_SPREAD), |
| 24 | calibration_button("Start Calibration") |
| 25 | { |
| 26 | set_border_width(5); |
| 27 | axis_frame.set_border_width(5); |
| 28 | axis_table.set_border_width(5); |
| 29 | |
| 30 | label.set_use_markup(true); |
| 31 | label.set_line_wrap(); |
| 32 | get_vbox()->pack_start(label, Gtk::PACK_SHRINK); |
| 33 | |
| 34 | calibration_button.signal_clicked().connect(sigc::mem_fun(this, &JoystickCalibrationWidget::on_calibrate)); |
| 35 | |
| 36 | buttonbox.set_border_width(5); |
| 37 | buttonbox.add(calibration_button); |
| 38 | get_vbox()->pack_start(buttonbox, Gtk::PACK_SHRINK); |
| 39 | |
| 40 | axis_table.attach(*Gtk::manage(new Gtk::Label("Axes")), 0, 1, 0, 1); |
| 41 | |
| 42 | axis_table.attach(*Gtk::manage(new Gtk::Label("CenterMin")), 1, 2, 0, 1); |
| 43 | axis_table.attach(*Gtk::manage(new Gtk::Label("CenterMax")), 2, 3, 0, 1); |
| 44 | |
| 45 | axis_table.attach(*Gtk::manage(new Gtk::Label("RangeMin")), 3, 4, 0, 1); |
| 46 | axis_table.attach(*Gtk::manage(new Gtk::Label("RangeMax")), 4, 5, 0, 1); |
| 47 | |
| 48 | axis_table.attach(*Gtk::manage(new Gtk::Label("Invert")), 5, 6, 0, 1); |
| 49 | |
| 50 | axis_table.set_col_spacing(2, 8); |
| 51 | for(int i = 0; i < joystick.get_axis_count(); ++i) |
| 52 | { |
| 53 | CalibrationData data; |
| 54 | |
| 55 | Gtk::SpinButton& center_min = *Gtk::manage(new Gtk::SpinButton((data.center_min = Gtk::Adjustment::create(0, -32768, 32767)))); |
| 56 | Gtk::SpinButton& center_max = *Gtk::manage(new Gtk::SpinButton((data.center_max = Gtk::Adjustment::create(0, -32768, 32767)))); |
| 57 | Gtk::SpinButton& range_min = *Gtk::manage(new Gtk::SpinButton((data.range_min = Gtk::Adjustment::create(0, -32768, 32767)))); |
| 58 | Gtk::SpinButton& range_max = *Gtk::manage(new Gtk::SpinButton((data.range_max = Gtk::Adjustment::create(0, -32768, 32767)))); |
| 59 | Gtk::CheckButton& invert = *(data.invert = Gtk::manage(new Gtk::CheckButton())); |
| 60 | |
| 61 | center_min.signal_value_changed().connect(sigc::mem_fun(this, &JoystickCalibrationWidget::on_apply)); |
| 62 | center_max.signal_value_changed().connect(sigc::mem_fun(this, &JoystickCalibrationWidget::on_apply)); |
| 63 | range_min.signal_value_changed().connect(sigc::mem_fun(this, &JoystickCalibrationWidget::on_apply)); |
| 64 | range_max.signal_value_changed().connect(sigc::mem_fun(this, &JoystickCalibrationWidget::on_apply)); |
| 65 | invert.signal_clicked().connect(sigc::mem_fun(this, &JoystickCalibrationWidget::on_apply)); |
| 66 | |
| 67 | center_min.set_tooltip_text("The minimal value of the dead zone"); |
| 68 | center_max.set_tooltip_text("The maximum value of the dead zone"); |
| 69 | range_min.set_tooltip_text("The minimal position reachable"); |
| 70 | range_max.set_tooltip_text("The maximum position reachable"); |
| 71 | |
| 72 | calibration_data.push_back(data); |
| 73 | |
| 74 | std::ostringstream str; |
| 75 | str << i; |
| 76 | axis_table.attach(*Gtk::manage(new Gtk::Label(str.str())), 0, 1, i+1, i+2); |
| 77 | |
| 78 | axis_table.attach(center_min, 1, 2, i+1, i+2); |
| 79 | axis_table.attach(center_max, 2, 3, i+1, i+2); |
| 80 | |
| 81 | axis_table.attach(range_min, 3, 4, i+1, i+2); |
| 82 | axis_table.attach(range_max, 4, 5, i+1, i+2); |
| 83 | |
| 84 | axis_table.attach(invert, 5, 6, i+1, i+2, Gtk::SHRINK, Gtk::SHRINK); |
| 85 | } |
| 86 | |
| 87 | add_button(Gtk::Stock::REVERT_TO_SAVED, 2); |
| 88 | add_button("Raw Events", 1); |
| 89 | Gtk::Widget* close_button = add_button(Gtk::Stock::CLOSE, 0); |
| 90 | |
| 91 | scroll.add(axis_table); |
| 92 | scroll.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); |
| 93 | scroll.set_size_request(-1, 300); |
| 94 | axis_frame.add(scroll); |
| 95 | |
| 96 | get_vbox()->pack_start(axis_frame, Gtk::PACK_EXPAND_WIDGET); |
| 97 | |
| 98 | signal_response().connect(sigc::mem_fun(this, &JoystickCalibrationWidget::on_response)); |
| 99 | |
| 100 | close_button->grab_focus(); |
| 101 | |
| 102 | update_with(joystick.get_calibration()); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | JoystickCalibrationWidget::on_clear() |
| 107 | { |
| 108 | joystick.clear_calibration(); |
| 109 | update_with(joystick.get_calibration()); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | JoystickCalibrationWidget::update_with(const std::vector<Joystick::CalibrationData>& data) |
| 114 | { |
| 115 | assert(data.size() == calibration_data.size()); |
| 116 | |
| 117 | for(int i = 0; i < (int)data.size(); ++i) |
| 118 | { |
| 119 | calibration_data[i].invert->set_active(data[i].invert); |
| 120 | calibration_data[i].center_min->set_value(data[i].center_min); |
| 121 | calibration_data[i].center_max->set_value(data[i].center_max); |
| 122 | calibration_data[i].range_min->set_value(data[i].range_min); |
| 123 | calibration_data[i].range_max->set_value(data[i].range_max); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | JoystickCalibrationWidget::on_apply() |
| 129 | { |
| 130 | std::vector<Joystick::CalibrationData> data(calibration_data.size()); |
| 131 | |
| 132 | for(int i = 0; i < (int)data.size(); ++i) |
| 133 | { |
| 134 | data[i].calibrate = true; |
| 135 | data[i].invert = calibration_data[i].invert->get_active(); |
| 136 | data[i].center_min = calibration_data[i].center_min->get_value(); |
| 137 | data[i].center_max = calibration_data[i].center_max->get_value(); |
| 138 | data[i].range_min = calibration_data[i].range_min->get_value(); |
| 139 | data[i].range_max = calibration_data[i].range_max->get_value(); |
| 140 | } |
| 141 | |
| 142 | joystick.set_calibration(data); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | JoystickCalibrationWidget::on_calibrate() |
| 147 | { |
| 148 | CalibrateMaximumDialog dialog(joystick); |
| 149 | dialog.show_all(); |
| 150 | dialog.run(); |
| 151 | update_with(joystick.get_calibration()); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | JoystickCalibrationWidget::on_response(int i) |
| 156 | { |
| 157 | if (i == 0) |
| 158 | { |
| 159 | hide(); |
| 160 | } |
| 161 | else if (i == 1) |
| 162 | { |
| 163 | on_clear(); |
| 164 | } |
| 165 | else if (i == 2) |
| 166 | { |
| 167 | joystick.reset_calibration(); |
| 168 | update_with(joystick.get_calibration()); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* EOF */ |