forked from janoc/libndofdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndofdev.c
307 lines (257 loc) · 9.79 KB
/
ndofdev.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
/*
*
* Copyright (c) 2008, Jan Ciger (jan.ciger (at) gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The name of its contributors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Jan Ciger ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Jan Ciger BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Drop-in replacement for the 3D Connexion SDK library not available on Linux
*
* Uses native Linux input (evdev) device for SpaceNavigator, otherwise SDL for
* regular joysticks.
*
* Evdev could be used for joysticks as well, but higher level logic would have to be
* re-implemented (calibration, filtering, etc.) - SDL includes it already.
*
* Release 0.6
*/
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <SDL/SDL.h>
#include "ndofdev_external.h"
// Hack - we need to store the descriptor
// of the SpaceNavigator in order to be able to
// turn its LED off in the ndof_cleanup() function :(
static int spacenav_fd = -1;
const int SPACE_NAVIG_THRESHOLD = 20; // minimum change threshold
typedef struct
{
// native HID interface or SDL?
int USE_SDL;
// SpaceNavigator
int fd; // file descriptor of the device
long int *axes; // last state
long int *buttons;
// SDL joysticks
SDL_Joystick *j;
} LinJoystickPrivate;
NDOF_Device *ndof_create()
{
NDOF_Device *d = (NDOF_Device *) calloc(1, sizeof(NDOF_Device));
return d;
}
int ndof_init_first(NDOF_Device *in_out_dev, void *param)
{
// try to find 3DConnexion SpaceNavigator first
int i = 0;
int fd = -1;
char *fname = NULL;
struct input_id ID;
fname = (char *)malloc(1000 * sizeof(char));
while (i < 32)
{
sprintf(fname, "/dev/input/event%d", i++);
fd = open(fname, O_RDWR | O_NONBLOCK);
if (fd > 0)
{
ioctl(fd, EVIOCGID, &ID); // get device ID
if ( // For a nice list see http://spacemice.org/index.php?title=Dev
((ID.vendor == 0x046d) && // Logitech's Vendor ID, used by 3DConnexion until they got their own.
(
(ID.product == 0xc603) || // SpaceMouse (untested)
(ID.product == 0xc605) || // CADMan (untested)
(ID.product == 0xc606) || // SpaceMouse Classic (untested)
(ID.product == 0xc621) || // SpaceBall 5000
(ID.product == 0xc623) || // SpaceTraveler (untested)
(ID.product == 0xc625) || // SpacePilot (untested)
(ID.product == 0xc626) || // SpaceNavigators
(ID.product == 0xc627) || // SpaceExplorer (untested)
(ID.product == 0xc628) || // SpaceNavigator for Notebooks (untested)
(ID.product == 0xc629) || // SpacePilot Pro (untested)
(ID.product == 0xc62b) || // SpaceMousePro
0
)
) ||
((ID.vendor == 0x256F) && // 3Dconnexion's Vendor ID
(
(ID.product == 0xc62E) || // SpaceMouse Wireless (cable) (untested)
(ID.product == 0xc62F) || // SpaceMouse Wireless (receiver) (untested)
(ID.product == 0xc631) || // Spacemouse Wireless (untested)
(ID.product == 0xc632) || // SpacemousePro Wireless (untested)
0
)
))
{
// printf("Using device: %s\n", fname);
break;
} else {
close(fd);
fd = -1;
}
}
}
if(fd > 0)
{
// We have SpaceNavigator, use it
spacenav_fd = fd;
unsigned int axes_count = 6; // default to sane values for these devices
unsigned int N_BUTTONS = 32;
unsigned char evtype_mask[(EV_MAX + 7) / 8];
// Get the actual number of axes for this device.
if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof evtype_mask), evtype_mask) >= 0)
{
axes_count = 0;
unsigned int index = 0;
for (; index < REL_CNT; ++index)
{
unsigned int idx = index / 8;
unsigned int bit = index % 8;
axes_count += (evtype_mask[idx] & (1 << bit)) > 0;
}
} else {
perror("Failed to obtain the number of axes for device:\n");
}
in_out_dev->axes_count = axes_count;
in_out_dev->btn_count = N_BUTTONS;
in_out_dev->absolute = 0;
in_out_dev->valid = 1;
in_out_dev->axes_max = 512;
in_out_dev->axes_min = -512;
ioctl(fd, EVIOCGNAME(255), &in_out_dev->product); // copy name of the device
// private data
LinJoystickPrivate *priv = (LinJoystickPrivate *) malloc (sizeof(LinJoystickPrivate));
priv->fd = fd;
priv->axes = (long int *) calloc(axes_count, sizeof(long int));
priv->buttons = (long int *) calloc(N_BUTTONS, sizeof(long int));
priv->USE_SDL = 0;
priv->j = NULL;
in_out_dev->private_data = priv;
// turn the LED on
struct input_event led_ev;
led_ev.type = EV_LED;
led_ev.code = LED_MISC;
led_ev.value = 1;
if(write(spacenav_fd, &led_ev, sizeof(struct input_event)) < 0)
perror("Failed to write LED_ON command:\n");
return 0;
} else {
// SpaceNavigator not found, use SDL Joystick
SDL_Joystick *j = SDL_JoystickOpen(0);
if(j)
{
in_out_dev->axes_count = SDL_JoystickNumAxes(j);
in_out_dev->btn_count = SDL_JoystickNumButtons(j);
in_out_dev->absolute = 0; // always relative on Linux
in_out_dev->valid = 1;
in_out_dev->axes_max = 32767;
in_out_dev->axes_min = -32767;
strncpy(in_out_dev->product, SDL_JoystickName(0), 255);
// private data
LinJoystickPrivate *priv = (LinJoystickPrivate *) malloc (sizeof(LinJoystickPrivate));
priv->j = j;
priv->fd = -1;
priv->USE_SDL = 1;
in_out_dev->private_data = priv;
return 0;
} else {
// no joysticks found
return -1;
}
}
}
void ndof_libcleanup()
{
if(spacenav_fd > 0)
{
// turn the LED off
struct input_event led_ev;
led_ev.type = EV_LED;
led_ev.code = LED_MISC;
led_ev.value = 0;
if(write(spacenav_fd, &led_ev, sizeof(struct input_event)) < 0)
perror("Failed to write LED_OFF command:\n");
}
// FIXME: needs to cleanup the memory
}
int ndof_libinit(NDOF_DeviceAddCallback in_add_cb,
NDOF_DeviceRemovalCallback in_removal_cb,
void *platform_specific)
{
// Initialize the joystick subsystem
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
return 0;
}
void ndof_update(NDOF_Device *in_dev)
{
int i;
LinJoystickPrivate *priv = (LinJoystickPrivate *) in_dev->private_data;
assert(priv != NULL);
if(priv->USE_SDL)
{
SDL_JoystickUpdate();
SDL_Joystick *j = priv->j;
for(i = 0; i < in_dev->axes_count; i++)
{
in_dev->axes[i] = (int) (SDL_JoystickGetAxis(j, i));
}
for(i = 0; i < in_dev->btn_count; i++)
{
in_dev->buttons[i] = SDL_JoystickGetButton(j, i);
}
} else {
// update SpaceNavigator
struct input_event ev;
while(read(priv->fd, &ev, sizeof(struct input_event)) > 0)
{
switch (ev.type)
{
case EV_KEY:
// printf("Key %d pressed %d.\n", ev.code, ev.value);
priv->buttons[ev.code & 0xff] = ev.value;
break;
case EV_REL:
case EV_ABS: // 2.6.35 and up, maybe earlier kernels too send EV_ABS instead of EV_REL
// printf("%d %g\n", ev.code, ev.value);
// clean up small values
priv->axes[ev.code] = abs(ev.value) > SPACE_NAVIG_THRESHOLD ? ev.value : 0;
break;
default:
break;
}
}
memcpy(in_dev->axes, priv->axes, in_dev->axes_count * sizeof(long int));
memcpy(in_dev->buttons, priv->buttons, in_dev->btn_count * sizeof(long int));
}
}
void ndof_dump(NDOF_Device *dev)
{
}
void ndof_dump_list()
{
}