Debian Codegraph @

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