1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/***************************************************************************
* Copyright (C) 2003 by Martin Koller *
* m.koller@surfeu.at *
* This file is part of the TDE Control Center Module for Joysticks *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "joydevice.h"
#include <klocale.h>
#include <kdebug.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <errno.h>
#include <math.h>
//--------------------------------------------------------------
JoyDevice::JoyDevice(const TQString &devicefile)
: devName(devicefile), joyFd(-1), buttons(0), axes(0),
amin(0), amax(0), corr(0), origCorr(0)
{
}
//--------------------------------------------------------------
TQString JoyDevice::errText(ErrorCode code) const
{
switch ( code )
{
case SUCCESS: return "";
case OPEN_FAILED:
{
return i18n("The given device %1 could not be opened: %2")
.arg(devName).arg(strerror(errno));
}
case NO_JOYSTICK:
{
return i18n("The given device %1 is not a joystick.").arg(devName);
}
case ERR_GET_VERSION:
{
return i18n("Could not get kernel driver version for joystick device %1: %2")
.arg(devName).arg(strerror(errno));
}
case WRONG_VERSION:
{
int version = 0;
int fd = ::open(devName.latin1(), O_RDONLY);
if ( fd != -1 )
{
::ioctl(fd, JSIOCGVERSION, &version);
::close(fd);
}
return i18n("The current running kernel driver version (%1.%2.%3) is not the one this module was compiled for (%4.%5.%6).")
.arg(version >> 16).arg((version >> 8) & 0xFF).arg(version & 0xFF)
.arg(JS_VERSION >> 16).arg((JS_VERSION >> 8) & 0xFF).arg(JS_VERSION & 0xFF);
}
case ERR_GET_BUTTONS:
{
return i18n("Could not get number of buttons for joystick device %1: %2")
.arg(devName).arg(strerror(errno));
}
case ERR_GET_AXES:
{
return i18n("Could not get number of axes for joystick device %1: %2")
.arg(devName).arg(strerror(errno));
}
case ERR_GET_CORR:
{
return i18n("Could not get calibration values for joystick device %1: %2")
.arg(devName).arg(strerror(errno));
}
case ERR_RESTORE_CORR:
{
return i18n("Could not restore calibration values for joystick device %1: %2")
.arg(devName).arg(strerror(errno));
}
case ERR_INIT_CAL:
{
return i18n("Could not initialize calibration values for joystick device %1: %2")
.arg(devName).arg(strerror(errno));
}
case ERR_APPLY_CAL:
{
return i18n("Could not apply calibration values for joystick device %1: %2")
.arg(devName).arg(strerror(errno));
}
default: return i18n("internal error - code %1 unknown").arg(int(code));
}
}
//--------------------------------------------------------------
JoyDevice::ErrorCode JoyDevice::open()
{
if ( joyFd != -1 ) return JoyDevice::SUCCESS; // already open
int fd = ::open(devName.latin1(), O_RDONLY);
if ( fd == -1 )
return JoyDevice::OPEN_FAILED;
// we could open the devicefile, now check if a joystick is attached
char name[128];
if ( ::ioctl(fd, JSIOCGNAME(sizeof(name)), &name) == -1 )
{
::close(fd);
return JoyDevice::NO_JOYSTICK;
}
// check the kernel driver version
int version;
if ( ::ioctl(fd, JSIOCGVERSION, &version) == -1 )
{
::close(fd);
return JoyDevice::ERR_GET_VERSION;
}
if ( version != JS_VERSION )
{
::close(fd);
return JoyDevice::WRONG_VERSION;
}
char bt = 0, ax = 0;
if ( ::ioctl(fd, JSIOCGBUTTONS, &bt) == -1 )
{
::close(fd);
return JoyDevice::ERR_GET_BUTTONS;
}
if ( ::ioctl(fd, JSIOCGAXES, &ax) == -1 )
{
::close(fd);
return JoyDevice::ERR_GET_AXES;
}
struct js_corr *oldCorr = new struct js_corr[ax];
if ( ::ioctl(fd, JSIOCGCORR, oldCorr) == -1 )
{
::close(fd);
delete [] oldCorr;
return JoyDevice::ERR_GET_CORR;
}
descr = name;
joyFd = fd;
axes = ax;
buttons = bt;
origCorr = oldCorr;
corr = new struct js_corr[axes];
amin = new int[axes];
amax = new int[axes];
int i;
for (i = 0; i < axes; i++)
resetMinMax(i);
return JoyDevice::SUCCESS;
}
//--------------------------------------------------------------
void JoyDevice::close()
{
if ( joyFd == -1 ) return;
::close(joyFd);
joyFd = -1;
descr = "";
delete [] amin;
delete [] amax;
amin = 0;
amax = 0;
delete [] corr;
corr = 0;
delete [] origCorr;
origCorr = 0;
}
//--------------------------------------------------------------
int JoyDevice::axisMin(int axis) const
{
if ( (axis < 0) || (axis >= axes) ) return 0;
return amin[axis];
}
//--------------------------------------------------------------
int JoyDevice::axisMax(int axis) const
{
if ( (axis < 0) || (axis >= axes) ) return 0;
return amax[axis];
}
//--------------------------------------------------------------
JoyDevice::ErrorCode JoyDevice::initCalibration()
{
if ( joyFd == -1 ) return JoyDevice::ERR_INIT_CAL;
int i;
// Reset all current correction values
for (i = 0; i < axes; i++)
{
corr[i].type = JS_CORR_NONE;
corr[i].prec = 0;
}
if ( ::ioctl(joyFd, JSIOCSCORR, corr) == -1 )
return JoyDevice::ERR_INIT_CAL;
for (i = 0; i < axes; i++)
corr[i].type = JS_CORR_BROKEN;
return JoyDevice::SUCCESS;
}
//--------------------------------------------------------------
JoyDevice::ErrorCode JoyDevice::applyCalibration()
{
if ( joyFd == -1 ) return JoyDevice::ERR_APPLY_CAL;
if ( ::ioctl(joyFd, JSIOCSCORR, corr) == -1 )
return JoyDevice::ERR_APPLY_CAL;
return JoyDevice::SUCCESS;
}
//--------------------------------------------------------------
void JoyDevice::resetMinMax(int axis, int value)
{
amin[axis] = value;
amax[axis] = value;
}
//--------------------------------------------------------------
void JoyDevice::calcPrecision()
{
if ( !corr ) return;
int i;
for (i = 0; i < axes; i++)
{
corr[i].prec = amax[i] - amin[i];
kdDebug() << "Precision for axis: " << i << ": " << corr[i].prec << endl;
}
}
//--------------------------------------------------------------
JoyDevice::ErrorCode JoyDevice::restoreCorr()
{
if ( joyFd == -1 ) return JoyDevice::SUCCESS;
if ( ::ioctl(joyFd, JSIOCSCORR, origCorr) == -1 )
return JoyDevice::ERR_RESTORE_CORR;
else
return JoyDevice::SUCCESS;
}
//--------------------------------------------------------------
JoyDevice::~JoyDevice()
{
close();
}
//--------------------------------------------------------------
bool JoyDevice::getEvent(JoyDevice::EventType &type, int &number, int &value)
{
number = value = 0;
int ret;
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(joyFd, &readSet);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100000;
ret = ::select(joyFd + 1, &readSet, 0, 0, &timeout);
if ( ret == 1 ) // got an event from the joystick
{
struct js_event e;
if ( ::read(joyFd, &e, sizeof(struct js_event)) == sizeof(struct js_event) )
{
if ( e.type & JS_EVENT_BUTTON )
{
type = JoyDevice::BUTTON;
value = e.value;
number = e.number;
return true;
}
if ( e.type & JS_EVENT_AXIS )
{
type = JoyDevice::AXIS;
value = e.value;
number = e.number;
// store min, max values
if ( e.value < amin[number] ) amin[number] = e.value;
if ( e.value > amax[number] ) amax[number] = e.value;
return true;
}
}
}
return false; // no event
}
//--------------------------------------------------------------
void JoyDevice::calcCorrection(int axis, int *min, int *center, int *max)
{
const int MIN = 0;
const int MAX = 1;
double a, b, c, d;
a = center[MIN]; // inputs.cmin[1];
b = center[MAX]; // inputs.cmax[1];
c = 32767.0 / (center[MIN] - min[MAX]); // (inputs.cmin[1] - inputs.cmax[0]);
d = 32767.0 / (max[MIN] - center[MAX]); // (inputs.cmin[2] - inputs.cmax[1]);
corr[axis].coef[0] = (int)rint(a);
corr[axis].coef[1] = (int)rint(b);
corr[axis].coef[2] = (int)rint(c*16384.0);
corr[axis].coef[3] = (int)rint(d*16384.0);
kdDebug() << "min min: " << min[0] << " max: " << min[1] << endl;
kdDebug() << "max min: " << max[0] << " max: " << max[1] << endl;
kdDebug() << "Correction values for axis: " << axis << ": "
<< corr[axis].coef[0] << ", "
<< corr[axis].coef[1] << ", "
<< corr[axis].coef[2] << ", "
<< corr[axis].coef[3] << endl;
}
//--------------------------------------------------------------
|