-
Notifications
You must be signed in to change notification settings - Fork 1
/
vrpn_3DConnexion.C
357 lines (315 loc) · 13.5 KB
/
vrpn_3DConnexion.C
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
// vrpn_3DConnexion.C: VRPN driver for 3DConnexion
// Space Navigator, Space Traveler, Space Explorer, Space Mouse, Spaceball 5000
#include <string.h> // for memset
#include "vrpn_3DConnexion.h"
#include "vrpn_BaseClass.h" // for ::vrpn_TEXT_WARNING
// There is a non-HID Linux-based driver for this device that has a capability
// not implemented in the HID interface. It uses the input.h interface.
#if defined(linux) && !defined(VRPN_USE_HID)
#define VRPN_USING_3DCONNEXION_EVENT_IFACE
#include <linux/input.h>
#include <stdlib.h> // for malloc, free, etc
#include <unistd.h> // for write, etc
#endif
typedef struct input_devinfo {
vrpn_uint16 bustype;
vrpn_uint16 vendor;
vrpn_uint16 product;
vrpn_uint16 version;
} XXX_should_have_been_in_system_includes;
// USB vendor and product IDs for the models we support
static const vrpn_uint16 vrpn_3DCONNEXION_VENDOR = 0x046d; //1133; // 3Dconnexion is made by Logitech
static const vrpn_uint16 vrpn_SPACEMOUSEWIRELESS_VENDOR = 9583; // Made by a different vendor...
static const vrpn_uint16 vrpn_3DCONNEXION_TRAVELER = 50723;
static const vrpn_uint16 vrpn_3DCONNEXION_NAVIGATOR = 50726;
static const vrpn_uint16 vrpn_3DCONNEXION_NAVIGATOR_FOR_NOTEBOOKS = 0xc628; // 50728;
static const vrpn_uint16 vrpn_3DCONNEXION_SPACEEXPLORER = 0xc627; // 50727
static const vrpn_uint16 vrpn_3DCONNEXION_SPACEMOUSE = 50691;
static const vrpn_uint16 vrpn_3DCONNEXION_SPACEMOUSEPRO = 50731;
static const vrpn_uint16 vrpn_3DCONNEXION_SPACEMOUSEWIRELESS = 50735;
static const vrpn_uint16 vrpn_3DCONNEXION_SPACEBALL5000 = 0xc621; // 50721;
static const vrpn_uint16 vrpn_3DCONNEXION_SPACEPILOT = 0xc625;
vrpn_3DConnexion::vrpn_3DConnexion(vrpn_HidAcceptor *filter, unsigned num_buttons,
const char *name, vrpn_Connection *c,
vrpn_uint16 vendor, vrpn_uint16 product)
: vrpn_Button_Filter(name, c)
, vrpn_Analog(name, c)
#if defined(VRPN_USE_HID)
, vrpn_HidInterface(filter, vendor, product)
#endif
, _filter(filter)
{
vrpn_Analog::num_channel = 6;
vrpn_Button::num_buttons = num_buttons;
// Initialize the state of all the analogs and buttons
memset(buttons, 0, sizeof(buttons));
memset(lastbuttons, 0, sizeof(lastbuttons));
memset(channel, 0, sizeof(channel));
memset(last, 0, sizeof(last));
// There is a non-HID Linux-based driver for this device that has a capability
// not implemented in the HID interface. It is implemented using the Event
// interface.
#if defined(VRPN_USING_3DCONNEXION_EVENT_IFACE)
// Use the Event interface to open devices looking for the one
// we want. Call the acceptor with all the devices we find
// until we get one that we want.
fd = -1;
FILE *f;
int i = 0;
// try to autodetect the device
char *fname = (char *)malloc(1000*sizeof(char));
while(i < 256) {
sprintf(fname, "/dev/input/event%d", i++);
f = fopen(fname, "r+b");
if(f) {
// We got an active device. Fill in its values and see if it
// is acceptable to the filter.
struct input_devinfo ID;
ioctl(fileno(f), EVIOCGID, &ID);
vrpn_HIDDEVINFO info;
info.product = ID.product;
info.vendor = ID.vendor;
if (_filter->accept(info)) {
fd = fileno(f);
set_led(1);
break;
} else {
fclose(f);
f = NULL;
}
}
}
if(!f) {
perror("Could not open the device");
exit(1);
}
fclose(f);
free(fname);
// turn the LED on
set_led(1);
#endif
}
vrpn_3DConnexion::~vrpn_3DConnexion()
{
#if defined(VRPN_USING_3DCONNEXION_EVENT_IFACE)
set_led(0);
#endif
delete _filter;
}
#if defined(VRPN_USE_HID)
void vrpn_3DConnexion::on_data_received(size_t bytes, vrpn_uint8 *buffer)
{
decodePacket(bytes, buffer);
}
#endif
void vrpn_3DConnexion::mainloop()
{
#if defined(VRPN_USE_HID)
// Full reports are 7 bytes long.
// XXX If we get a 2-byte report mixed in, then something is going to get
// truncated.
update();
#elif defined(VRPN_USING_3DCONNEXION_EVENT_IFACE)
struct timeval zerotime;
fd_set fdset;
struct input_event ev;
int i;
zerotime.tv_sec = 0;
zerotime.tv_usec = 0;
FD_ZERO(&fdset); /* clear fdset */
FD_SET(fd, &fdset); /* include fd in fdset */
int moreData = 0;
do {
vrpn_noint_select(fd + 1, &fdset, NULL, NULL, &zerotime);
moreData = 0;
if (FD_ISSET(fd, &fdset)) {
moreData = 1;
if (vrpn_noint_block_read(fd, reinterpret_cast<char*>(&ev), sizeof(struct input_event)) != sizeof(struct input_event)) {
send_text_message("Error reading from vrpn_3DConnexion", vrpn_Analog::timestamp, vrpn_TEXT_ERROR);
if (d_connection) { d_connection->send_pending_reports(); }
return;
}
switch (ev.type) {
case EV_KEY: // button movement
vrpn_gettimeofday((timeval *)&this->vrpn_Button::timestamp, NULL);
buttons[ev.code & 0x0ff] = ev.value;
break;
case EV_REL: // axis movement
case EV_ABS: // new kernels send more logical _ABS instead of _REL
vrpn_gettimeofday((timeval *)&this->vrpn_Analog::timestamp, NULL);
// Convert from short to int to avoid a short/double conversion
// bug in GCC 3.2.
i = ev.value;
channel[ev.code] = static_cast<double>(i)/400.0;
break;
default:
break;
}
}
report_changes();
} while (moreData == 1);
#endif
server_mainloop();
vrpn_gettimeofday(&_timestamp, NULL);
}
void vrpn_3DConnexion::report_changes(vrpn_uint32 class_of_service)
{
vrpn_Analog::timestamp = _timestamp;
vrpn_Button::timestamp = _timestamp;
vrpn_Analog::report_changes(class_of_service);
vrpn_Button::report_changes();
}
void vrpn_3DConnexion::report(vrpn_uint32 class_of_service)
{
vrpn_Analog::timestamp = _timestamp;
vrpn_Button::timestamp = _timestamp;
vrpn_Analog::report(class_of_service);
vrpn_Button::report_changes();
}
#if defined(linux) && !defined(VRPN_USE_HID)
int vrpn_3DConnexion::set_led(int led_state)
{
struct input_event event;
int ret;
event.type = EV_LED;
event.code = LED_MISC;
event.value = led_state;
ret = write(fd, &event, sizeof(struct input_event));
if (ret < 0) {
perror ("setting led state failed");
}
return ret < static_cast<int>(sizeof(struct input_event));
}
#endif
#if defined(VRPN_USE_HID)
void vrpn_3DConnexion::decodePacket(size_t bytes, vrpn_uint8 *buffer)
{
// Force 'small' buffers (ie button under linux - 3 bytes - and apple - 2 bytes - into 7 bytes
// so we get through the report loop once. XXX Problem: this is skipping 7 bytes per report
// regardless of how many bytes were in the report. This is going to get us into trouble for
// multi-report packets. Instead, we should go until we've parsed all characters and add the
// number of characters parsed each time rather than a constant 7 reports.
if(bytes<7) bytes=7;
if (bytes > 7) {
fprintf(stderr, "vrpn_3DConnexion::decodePacket(): Long packet (%d bytes), may mis-parse\n",
static_cast<int>(bytes));
}
// Decode all full reports.
// Full reports for all of the pro devices are 7 bytes long (the first
// byte is the report type, because this device has multiple ones the
// HIDAPI library leaves it in the report).
for (size_t i = 0; i < bytes / 7; i++) {
vrpn_uint8 *report = buffer + (i * 7);
// There are three types of reports. Parse whichever type
// this is.
char report_type = report[0];
vrpn_uint8 *bufptr = &report[1];
const float scale = 1.0f/400.0f;
switch (report_type) {
// Report types 1 and 2 come one after the other. Each seems
// to change when the puck is moved. It looks like each pair
// of values records a signed value for one channel; report
// type 1 is translation and report type 2 is rotation.
// The minimum and maximum values seem to vary somewhat.
// They all seem to be able to get over 400, so we scale
// by 400 and then clamp to (-1..1).
// The first byte is the low-order byte and the second is the
// high-order byte.
case 1:
channel[0] = vrpn_unbuffer_from_little_endian<vrpn_int16>(bufptr) * scale;
if (channel[0] < -1.0) { channel[0] = -1.0; }
if (channel[0] > 1.0) { channel[0] = 1.0; }
channel[1] = vrpn_unbuffer_from_little_endian<vrpn_int16>(bufptr) * scale;
if (channel[1] < -1.0) { channel[1] = -1.0; }
if (channel[1] > 1.0) { channel[1] = 1.0; }
channel[2] = vrpn_unbuffer_from_little_endian<vrpn_int16>(bufptr) * scale;
if (channel[2] < -1.0) { channel[2] = -1.0; }
if (channel[2] > 1.0) { channel[2] = 1.0; }
break;
case 2:
channel[3] = vrpn_unbuffer_from_little_endian<vrpn_int16>(bufptr) * scale;
if (channel[3] < -1.0) { channel[3] = -1.0; }
if (channel[3] > 1.0) { channel[3] = 1.0; }
channel[4] = vrpn_unbuffer_from_little_endian<vrpn_int16>(bufptr) * scale;
if (channel[4] < -1.0) { channel[4] = -1.0; }
if (channel[4] > 1.0) { channel[4] = 1.0; }
channel[5] = vrpn_unbuffer_from_little_endian<vrpn_int16>(bufptr) * scale;
if (channel[5] < -1.0) { channel[5] = -1.0; }
if (channel[5] > 1.0) { channel[5] = 1.0; }
break;
case 3: { // Button report
int btn;
// Button reports are encoded as bits in the first 2 bytes
// after the type. There can be more than one byte if there
// are more than 8 buttons such as on SpaceExplorer or SpaceBall5000.
// If 8 or less, we don't look at 2nd byte.
// SpaceExplorer buttons are (for example):
// Name Number
// 1 0
// 2 1
// T 2
// L 3
// R 4
// F 5
// ESC 6
// ALT 7
// SHIFT 8
// CTRL 9
// FIT 10
// PANEL 11
// + 12
// - 13
// 2D 14
for (btn = 0; btn < vrpn_Button::num_buttons; btn++) {
vrpn_uint8 *location, mask;
location = report + 1 + (btn / 8);
mask = 1 << (btn % 8);
buttons[btn] = ( (*location) & mask) != 0;
}
break;
}
default:
vrpn_gettimeofday(&_timestamp, NULL);
send_text_message("Unknown report type", _timestamp, vrpn_TEXT_WARNING);
}
// Report this event before parsing the next.
report_changes();
}
}
#endif
vrpn_3DConnexion_Navigator::vrpn_3DConnexion_Navigator(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_NAVIGATOR), 2, name, c, vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_NAVIGATOR)
{
}
vrpn_3DConnexion_Navigator_for_Notebooks::vrpn_3DConnexion_Navigator_for_Notebooks(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_NAVIGATOR_FOR_NOTEBOOKS), 2, name, c, vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_NAVIGATOR_FOR_NOTEBOOKS)
{
}
vrpn_3DConnexion_Traveler::vrpn_3DConnexion_Traveler(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_TRAVELER), 8, name, c, vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_TRAVELER)
{
}
vrpn_3DConnexion_SpaceMouse::vrpn_3DConnexion_SpaceMouse(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEMOUSE), 11, name, c, vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEMOUSE)
{
}
vrpn_3DConnexion_SpaceMousePro::vrpn_3DConnexion_SpaceMousePro(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEMOUSEPRO), 27, name, c, vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEMOUSEPRO)
{ // 15 physical buttons are numbered: 0-2, 4-5, 8, 12-15, 22-26
}
vrpn_3DConnexion_SpaceMouseWireless::vrpn_3DConnexion_SpaceMouseWireless(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_SPACEMOUSEWIRELESS_VENDOR, vrpn_3DCONNEXION_SPACEMOUSEWIRELESS), 2, name, c, vrpn_SPACEMOUSEWIRELESS_VENDOR, vrpn_3DCONNEXION_SPACEMOUSEWIRELESS)
{
}
vrpn_3DConnexion_SpaceExplorer::vrpn_3DConnexion_SpaceExplorer(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEEXPLORER), 15, name, c, vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEEXPLORER)
{
}
vrpn_3DConnexion_SpaceBall5000::vrpn_3DConnexion_SpaceBall5000(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEBALL5000), 12, name, c, vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEBALL5000)
{
}
vrpn_3DConnexion_SpacePilot::vrpn_3DConnexion_SpacePilot(const char *name, vrpn_Connection *c)
: vrpn_3DConnexion(_filter = new vrpn_HidProductAcceptor(vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEPILOT), 21, name, c, vrpn_3DCONNEXION_VENDOR, vrpn_3DCONNEXION_SPACEPILOT)
{
}