Debian Codegraph @

src/joystick.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 <assert.h>
20 #include <math.h>
21 #include <algorithm>
22 #include <iostream>
23 #include <stdexcept>
24 #include <stdint.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <sstream>
32 #include <unistd.h>
33 #include <linux/joystick.h>
34 #include <glibmm.h>
35
36 #include "evdev_helper.hpp"
37 #include "joystick.hpp"
38
39 Joystick::Joystick(const std::string& filename_)
40 : filename(filename_)
41 {
42 if ((fd = open(filename.c_str(), O_RDONLY)) < 0)
43 {
44 std::ostringstream str;
45 str << filename << ": " << strerror(errno);
46 throw std::runtime_error(str.str());
47 }
48 else
49 {
50 // ok
51 uint8_t num_axis = 0;
52 uint8_t num_button = 0;
53 ioctl(fd, JSIOCGAXES, &num_axis);
54 ioctl(fd, JSIOCGBUTTONS, &num_button);
55 axis_count = num_axis;
56 button_count = num_button;
57
58 // Get Name
59 char name_c_str[1024];
60 if (ioctl(fd, JSIOCGNAME(sizeof(name_c_str)), name_c_str) < 0)
61 {
62 std::ostringstream str;
63 str << filename << ": " << strerror(errno);
64 throw std::runtime_error(str.str());
65 }
66 else
67 {
68 orig_name = name_c_str;
69 try {
70 name = Glib::convert_with_fallback(name_c_str, "UTF-8", "ISO-8859-1");
71 } catch(Glib::ConvertError& err) {
72 std::cout << err.what() << std::endl;
73 }
74 }
75
76 axis_state.resize(axis_count);
77 }
78
79 orig_calibration_data = get_calibration();
80
81 connection = Glib::signal_io().connect(sigc::mem_fun(this, &Joystick::on_in), fd,
82 Glib::IO_IN | Glib::IO_HUP | Glib::IO_ERR);
83 }
84
85 Joystick::~Joystick()
86 {
87 connection.disconnect();
88 close(fd);
89 }
90
91 bool
92 Joystick::on_in(Glib::IOCondition cond)
93 {
94 if (cond & Glib::IO_IN)
95 {
96 update();
97 }
98
99 if (cond & Glib::IO_HUP)
100 {
101 std::cout << filename << ": joystick got disconnected" << std::endl;
102 connection.disconnect();
103 }
104
105 if (cond & Glib::IO_ERR)
106 {
107 std::cout << filename << ": error ocured while reading from joystick" << std::endl;
108 connection.disconnect();
109 }
110
111 return true;
112 }
113
114 void
115 Joystick::update()
116 {
117 struct js_event event;
118
119 ssize_t len = read(fd, &event, sizeof(event));
120
121 if (len < 0)
122 {
123 std::ostringstream str;
124 str << filename << ": " << strerror(errno);
125 throw std::runtime_error(str.str());
126 }
127 else if (len == sizeof(event))
128 { // ok
129 if (event.type & JS_EVENT_AXIS)
130 {
131 //std::cout << "Axis: " << (int)event.number << " -> " << (int)event.value << std::endl;
132 axis_state[event.number] = event.value;
133 axis_move(event.number, event.value);
134 }
135 else if (event.type & JS_EVENT_BUTTON)
136 {
137 //std::cout << "Button: " << (int)event.number << " -> " << (int)event.value << std::endl;
138 button_move(event.number, event.value);
139 }
140 }
141 else
142 {
143 throw std::runtime_error("Joystick::update(): unknown read error");
144 }
145 }
146
147 std::vector<JoystickDescription>
148 Joystick::get_joysticks()
149 {
150 std::vector<JoystickDescription> joysticks;
151
152 for(int i = 0; i < 32; ++i)
153 {
154 try
155 {
156 std::ostringstream str;
157 str << "/dev/input/js" << i;
158
159 if (Glib::file_test(str.str(), Glib::FILE_TEST_EXISTS))
160 {
161 Joystick joystick(str.str());
162
163 joysticks.push_back(JoystickDescription(joystick.get_filename(),
164 joystick.get_name(),
165 joystick.get_axis_count(),
166 joystick.get_button_count()));
167 }
168 }
169 catch(std::exception& err)
170 {
171 // ok
172 }
173 }
174
175 return joysticks;
176 }
177
178 Joystick::CalibrationData corr2cal(const struct js_corr& corr_)
179 {
180 struct js_corr corr = corr_;
181
182 Joystick::CalibrationData data;
183
184 if (corr.type)
185 {
186 data.calibrate = true;
187 data.invert = (corr.coef[2] < 0 && corr.coef[3] < 0);
188 data.center_min = corr.coef[0];
189 data.center_max = corr.coef[1];
190
191 if (data.invert)
192 {
193 corr.coef[2] = -corr.coef[2];
194 corr.coef[3] = -corr.coef[3];
195 }
196
197 // Need to use double and rint(), since calculation doesn't end
198 // up on clean integer positions (i.e. 0.9999 can happen)
199 data.range_min = rint(data.center_min - ((32767.0 * 16384) / corr.coef[2]));
200 data.range_max = rint((32767.0 * 16384) / corr.coef[3] + data.center_max);
201 }
202 else
203 {
204 data.calibrate = false;
205 data.invert = false;
206 data.center_min = 0;
207 data.center_max = 0;
208 data.range_min = 0;
209 data.range_max = 0;
210 }
211
212 return data;
213 }
214
215 std::vector<Joystick::CalibrationData>
216 Joystick::get_calibration()
217 {
218 std::vector<struct js_corr> corr(get_axis_count());
219
220 if (ioctl(fd, JSIOCGCORR, &*corr.begin()) < 0)
221 {
222 std::ostringstream str;
223 str << filename << ": " << strerror(errno);
224 throw std::runtime_error(str.str());
225 }
226 else
227 {
228 std::vector<CalibrationData> data;
229 std::transform(corr.begin(), corr.end(), std::back_inserter(data), corr2cal);
230 return data;
231 }
232 }
233
234 struct js_corr cal2corr(const Joystick::CalibrationData& data)
235 {
236 struct js_corr corr;
237
238 if (data.calibrate &&
239 (data.center_min - data.range_min) != 0 &&
240 (data.range_max - data.center_max) != 0)
241 {
242 corr.type = 1;
243 corr.prec = 0;
244 corr.coef[0] = data.center_min;
245 corr.coef[1] = data.center_max;
246
247 corr.coef[2] = (32767 * 16384) / (data.center_min - data.range_min);
248 corr.coef[3] = (32767 * 16384) / (data.range_max - data.center_max);
249
250 if (data.invert)
251 {
252 corr.coef[2] = -corr.coef[2];
253 corr.coef[3] = -corr.coef[3];
254 }
255 }
256 else
257 {
258 corr.type = 0;
259 corr.prec = 0;
260 memset(corr.coef, 0, sizeof(corr.coef));
261 }
262
263 return corr;
264 }
265
266 void
267 Joystick::set_calibration(const std::vector<CalibrationData>& data)
268 {
269 std::vector<struct js_corr> corr;
270
271 std::transform(data.begin(), data.end(), std::back_inserter(corr), cal2corr);
272
273 if (ioctl(fd, JSIOCSCORR, &*corr.begin()) < 0)
274 {
275 std::ostringstream str;
276 str << filename << ": " << strerror(errno);
277 throw std::runtime_error(str.str());
278 }
279 }
280
281 void
282 Joystick::clear_calibration()
283 {
284 std::vector<CalibrationData> data;
285
286 for(int i = 0; i < get_axis_count(); ++i)
287 {
288 CalibrationData cal;
289
290 cal.calibrate = false;
291 cal.invert = false;
292 cal.center_min = 0;
293 cal.center_max = 0;
294 cal.range_min = 0;
295 cal.range_max = 0;
296
297 data.push_back(cal);
298 }
299
300 set_calibration(data);
301 }
302
303 void
304 Joystick::reset_calibration()
305 {
306 set_calibration(orig_calibration_data);
307 }
308
309 std::vector<int>
310 Joystick::get_button_mapping()
311 {
312 uint16_t btnmap[KEY_MAX - BTN_MISC + 1];
313 if (ioctl(fd, JSIOCGBTNMAP, btnmap) < 0)
314 {
315 std::ostringstream str;
316 str << filename << ": " << strerror(errno);
317 throw std::runtime_error(str.str());
318 }
319 else
320 {
321 std::vector<int> mapping;
322 std::copy(btnmap, btnmap + button_count, std::back_inserter(mapping));
323 return mapping;
324 }
325 }
326
327 std::vector<int>
328 Joystick::get_axis_mapping()
329 {
330 uint8_t axismap[ABS_MAX + 1];
331 if (ioctl(fd, JSIOCGAXMAP, axismap) < 0)
332 {
333 std::ostringstream str;
334 str << filename << ": " << strerror(errno);
335 throw std::runtime_error(str.str());
336 }
337 else
338 {
339 std::vector<int> mapping;
340 std::copy(axismap, axismap + axis_count, std::back_inserter(mapping));
341 return mapping;
342 }
343 }
344
345 void
346 Joystick::set_button_mapping(const std::vector<int>& mapping)
347 {
348 assert((int)mapping.size() == button_count);
349
350 uint16_t btnmap[KEY_MAX - BTN_MISC + 1];
351 memset(btnmap, 0, sizeof(btnmap));
352 std::copy(mapping.begin(), mapping.end(), btnmap);
353
354 if (0)
355 for(int i = 0; i < button_count; ++i)
356 {
357 std::cout << i << " -> " << btnmap[i] << std::endl;
358 }
359
360 if (ioctl(fd, JSIOCSBTNMAP, btnmap) < 0)
361 {
362 std::ostringstream str;
363 str << filename << ": " << strerror(errno);
364 throw std::runtime_error(str.str());
365 }
366 }
367
368 int
369 Joystick::get_axis_state(int id)
370 {
371 if (id >= 0 && id < (int)axis_state.size())
372 return axis_state[id];
373 else
374 return 0;
375 }
376
377 void
378 Joystick::set_axis_mapping(const std::vector<int>& mapping)
379 {
380 assert((int)mapping.size() == axis_count);
381
382 uint8_t axismap[ABS_MAX + 1];
383
384 std::copy(mapping.begin(), mapping.end(), axismap);
385
386 if (ioctl(fd, JSIOCSAXMAP, axismap) < 0)
387 {
388 std::ostringstream str;
389 str << filename << ": " << strerror(errno);
390 throw std::runtime_error(str.str());
391 }
392 }
393
394 void
395 Joystick::correct_calibration(const std::vector<int>& mapping_old, const std::vector<int>& mapping_new)
396 {
397 int axes[ABS_MAX + 1]; // axes[name] -> old_idx
398 for(std::vector<int>::const_iterator i = mapping_old.begin(); i != mapping_old.end(); ++i)
399 {
400 axes[*i] = i - mapping_old.begin();
401 }
402
403 std::vector<CalibrationData> callib_old = get_calibration();
404 std::vector<CalibrationData> callib_new;
405 for(std::vector<int>::const_iterator i = mapping_new.begin(); i != mapping_new.end(); ++i)
406 {
407 callib_new.push_back(callib_old[axes[*i]]);
408 }
409
410 set_calibration(callib_new);
411 }
412
413 std::string
414 Joystick::get_evdev() const
415 {
416 // See /usr/share/doc/linux-doc-2.6.28/devices.txt.gz
417 for(int i = 0; i < 32; ++i)
418 {
419 std::ostringstream out;
420 out << "/dev/input/event" << i;
421
422 int evdev_fd;
423 if ((evdev_fd = open(out.str().c_str(), O_RDONLY)) < 0)
424 {
425 // ignore
426 }
427 else
428 {
429 char evdev_name[256];
430 if (ioctl(evdev_fd, EVIOCGNAME(sizeof(evdev_name)), evdev_name) < 0)
431 {
432 std::cout << out.str() << ": " << strerror(errno) << std::endl;
433 }
434 else
435 {
436 if (orig_name == evdev_name)
437 {
438 // Found a device that matches, so return it
439 close(evdev_fd);
440 return out.str();
441 }
442 }
443
444 close(evdev_fd);
445 }
446 }
447
448 throw std::runtime_error("couldn't find evdev for " + filename);
449 }
450
451 #ifdef __TEST__
452
453 // g++ -D__TEST__ joystick.cpp evdev_helper.cpp xml_writer.cpp xml_reader.cpp -o joystick-test `pkg-config --cflags --libs gtkmm-2.4 sigc++-2.0`
454
455 int main(int argc, char** argv)
456 {
457 for(int i = 1; i < argc; ++i)
458 {
459 Joystick joystick(argv[i]);
460
461 std::cout << "Filename: '" << joystick.get_filename() << "'\n";
462 std::cout << "Name: '" << joystick.get_name() << "'\n";
463 std::cout << "Axis: " << joystick.get_axis_count() << "\n";
464 std::cout << "Button: " << joystick.get_button_count() << "\n";
465 std::cout << "Evdev: '" << joystick.get_evdev() << "'\n";
466 }
467 return 0;
468 }
469 #endif
470
471 /* EOF */