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
|
/*
* jstest.c Version 1.2
*
* Copyright (c) 1996-1999 Vojtech Pavlik
*
* Sponsored by SuSE
*/
/*
* This program can be used to test all the features of the Linux
* joystick API, including non-blocking and select() access, as
* well as version 0.x compatibility mode. It is also intended to
* serve as an example implementation for those who wish to learn
* how to write their own joystick using applications.
*/
/*
* 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 <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <linux/input.h>
#include <linux/joystick.h>
#include "axbtnmap.h"
char *axis_names[ABS_MAX + 1] = {
"X", "Y", "Z", "Rx", "Ry", "Rz", "Throttle", "Rudder",
"Wheel", "Gas", "Brake", "?", "?", "?", "?", "?",
"Hat0X", "Hat0Y", "Hat1X", "Hat1Y", "Hat2X", "Hat2Y", "Hat3X", "Hat3Y",
"?", "?", "?", "?", "?", "?", "?",
};
char *button_names[KEY_MAX - BTN_MISC + 1] = {
"Btn0", "Btn1", "Btn2", "Btn3", "Btn4", "Btn5", "Btn6", "Btn7", "Btn8", "Btn9", "?", "?", "?", "?", "?", "?",
"LeftBtn", "RightBtn", "MiddleBtn", "SideBtn", "ExtraBtn", "ForwardBtn", "BackBtn", "TaskBtn", "?", "?", "?", "?", "?", "?", "?", "?",
"Trigger", "ThumbBtn", "ThumbBtn2", "TopBtn", "TopBtn2", "PinkieBtn", "BaseBtn", "BaseBtn2", "BaseBtn3", "BaseBtn4", "BaseBtn5", "BaseBtn6", "BtnDead",
"BtnA", "BtnB", "BtnC", "BtnX", "BtnY", "BtnZ", "BtnTL", "BtnTR", "BtnTL2", "BtnTR2", "BtnSelect", "BtnStart", "BtnMode", "BtnThumbL", "BtnThumbR", "?",
"?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
"WheelBtn", "Gear up",
};
#define NAME_LENGTH 128
int main (int argc, char **argv)
{
int fd, i;
unsigned char axes = 2;
unsigned char buttons = 2;
int version = 0x000800;
char name[NAME_LENGTH] = "Unknown";
uint16_t btnmap[BTNMAP_SIZE];
uint8_t axmap[AXMAP_SIZE];
int btnmapok = 1;
if (argc < 2 || argc > 3 || !strcmp("--help", argv[1])) {
puts("");
puts("Usage: jstest [<mode>] <device>");
puts("");
puts("Modes:");
puts(" --normal One-line mode showing immediate status");
puts(" --old Same as --normal, using 0.x interface");
puts(" --event Prints events as they come in");
puts(" --nonblock Same as --event, in nonblocking mode");
puts(" --select Same as --event, using select() call");
puts("");
return 1;
}
if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
perror("jstest");
return 1;
}
ioctl(fd, JSIOCGVERSION, &version);
ioctl(fd, JSIOCGAXES, &axes);
ioctl(fd, JSIOCGBUTTONS, &buttons);
ioctl(fd, JSIOCGNAME(NAME_LENGTH), name);
getaxmap(fd, axmap);
getbtnmap(fd, btnmap);
printf("Driver version is %d.%d.%d.\n",
version >> 16, (version >> 8) & 0xff, version & 0xff);
/* Determine whether the button map is usable. */
for (i = 0; btnmapok && i < buttons; i++) {
if (btnmap[i] < BTN_MISC || btnmap[i] > KEY_MAX) {
btnmapok = 0;
break;
}
}
if (!btnmapok) {
/* btnmap out of range for names. Don't print any. */
puts("jstest is not fully compatible with your kernel. Unable to retrieve button map!");
printf("Joystick (%s) has %d axes ", name, axes);
printf("and %d buttons.\n", buttons);
} else {
printf("Joystick (%s) has %d axes (", name, axes);
for (i = 0; i < axes; i++)
printf("%s%s", i > 0 ? ", " : "", axis_names[axmap[i]]);
puts(")");
printf("and %d buttons (", buttons);
for (i = 0; i < buttons; i++) {
printf("%s%s", i > 0 ? ", " : "", button_names[btnmap[i] - BTN_MISC]);
}
puts(").");
}
printf("Testing ... (interrupt to exit)\n");
/*
* Old (0.x) interface.
*/
if ((argc == 2 && version < 0x010000) || !strcmp("--old", argv[1])) {
struct JS_DATA_TYPE js;
while (1) {
if (read(fd, &js, JS_RETURN) != JS_RETURN) {
perror("\njstest: error reading");
return 1;
}
printf("Axes: X:%3d Y:%3d Buttons: A:%s B:%s\r",
js.x, js.y, (js.buttons & 1) ? "on " : "off", (js.buttons & 2) ? "on " : "off");
fflush(stdout);
usleep(10000);
}
}
/*
* Event interface, single line readout.
*/
if (argc == 2 || !strcmp("--normal", argv[1])) {
int *axis;
char *button;
int i;
struct js_event js;
axis = calloc(axes, sizeof(int));
button = calloc(buttons, sizeof(char));
while (1) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
switch(js.type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
button[js.number] = js.value;
break;
case JS_EVENT_AXIS:
axis[js.number] = js.value;
break;
}
printf("\r");
if (axes) {
printf("Axes: ");
for (i = 0; i < axes; i++)
printf("%2d:%6d ", i, axis[i]);
}
if (buttons) {
printf("Buttons: ");
for (i = 0; i < buttons; i++)
printf("%2d:%s ", i, button[i] ? "on " : "off");
}
fflush(stdout);
}
}
/*
* Event interface, events being printed.
*/
if (!strcmp("--event", argv[1])) {
struct js_event js;
while (1) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
fflush(stdout);
}
}
/*
* Reading in nonblocking mode.
*/
if (!strcmp("--nonblock", argv[1])) {
struct js_event js;
fcntl(fd, F_SETFL, O_NONBLOCK);
while (1) {
while (read(fd, &js, sizeof(struct js_event)) == sizeof(struct js_event)) {
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
}
if (errno != EAGAIN) {
perror("\njstest: error reading");
return 1;
}
usleep(10000);
}
}
/*
* Using select() on joystick fd.
*/
if (!strcmp("--select", argv[1])) {
struct js_event js;
fd_set set;
while (1) {
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&set);
FD_SET(fd, &set);
if (select(fd+1, &set, NULL, NULL, &tv)) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
}
}
}
printf("jstest: unknown mode: %s\n", argv[1]);
return -1;
}
|