This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_syshaptic.c
576 lines (462 loc) · 14.1 KB
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
/*
SDL - Simple DirectMedia Layer
Copyright (C) 2008 Edgar Simo
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#ifdef SDL_JOYSTICK_LINUX
#include "SDL_haptic.h"
#include "../SDL_haptic_c.h"
#include "../SDL_syshaptic.h"
#include <unistd.h> /* close */
#include <linux/input.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <string.h>
#define MAX_HAPTICS 32
43
44
45
/*
* List of available haptic devices.
*/
46
47
48
49
50
51
static struct
{
char *fname;
SDL_Haptic *haptic;
} SDL_hapticlist[MAX_HAPTICS];
52
53
54
55
/*
* Haptic system hardware data.
*/
56
57
58
59
60
61
struct haptic_hwdata
{
int fd;
};
62
63
64
65
66
/*
* Haptic system effect data.
*/
struct haptic_hweffect
{
67
struct ff_effect effect;
68
69
70
};
71
72
73
74
75
#define test_bit(nr, addr) \
(((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
#define EV_TEST(ev,f) \
if (test_bit((ev), features)) ret |= (f);
76
77
78
79
/*
* Test whether a device has haptic properties.
* Returns available properties or 0 if there are none.
*/
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
static int
EV_IsHaptic(int fd)
{
unsigned int ret;
unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
ret = 0;
ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features);
EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
EV_TEST(FF_PERIODIC, SDL_HAPTIC_PERIODIC);
EV_TEST(FF_RAMP, SDL_HAPTIC_RAMP);
EV_TEST(FF_SPRING, SDL_HAPTIC_SPRING);
EV_TEST(FF_FRICTION, SDL_HAPTIC_FRICTION);
EV_TEST(FF_DAMPER, SDL_HAPTIC_DAMPER);
EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA);
97
EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM);
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
EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN);
EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
return ret;
}
int
SDL_SYS_HapticInit(void)
{
const char joydev_pattern[] = "/dev/input/event%d";
dev_t dev_nums[MAX_HAPTICS];
char path[PATH_MAX];
struct stat sb;
int fd;
int i, j, k;
int duplicate;
int numhaptics;
numhaptics = 0;
i = 0;
for (j = 0; j < MAX_HAPTICS; ++j) {
snprintf(path, PATH_MAX, joydev_pattern, i++);
/* check to see if file exists */
if (stat(path,&sb) != 0)
break;
/* check for duplicates */
duplicate = 0;
for (k = 0; (k < numhaptics) && !duplicate; ++k) {
if (sb.st_rdev == dev_nums[k]) {
duplicate = 1;
}
}
if (duplicate) {
continue;
}
/* try to open */
fd = open(path, O_RDWR, 0);
if (fd < 0) continue;
#ifdef DEBUG_INPUT_EVENTS
printf("Checking %s\n", path);
#endif
/* see if it works */
if (EV_IsHaptic(fd)!=0) {
SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
SDL_hapticlist[numhaptics].haptic = NULL;
dev_nums[numhaptics] = sb.st_rdev;
++numhaptics;
}
close(fd);
}
return numhaptics;
}
160
161
162
/*
* Return the name of a haptic device, does not need to be opened.
*/
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const char *
SDL_SYS_HapticName(int index)
{
int fd;
static char namebuf[128];
char *name;
name = NULL;
fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
if (fd >= 0) {
if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
name = SDL_hapticlist[index].fname;
}
else {
name = namebuf;
}
}
close(fd);
return name;
}
186
187
188
/*
* Opens a haptic device for usage.
*/
189
190
191
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
192
int i;
193
194
int fd;
195
/* Open the character device */
196
197
198
fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
if (fd < 0) {
SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
199
return -1;
200
201
}
202
203
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)
204
SDL_malloc(sizeof(*haptic->hwdata));
205
206
if (haptic->hwdata == NULL) {
SDL_OutOfMemory();
207
goto open_err;
208
209
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
210
/* Set the data */
211
haptic->hwdata->fd = fd;
212
haptic->supported = EV_IsHaptic(fd);
213
214
215
216
217
218
219
220
221
222
223
224
/* Set the effects */
if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
SDL_SetError("Unable to query haptic device memory.");
goto open_err;
}
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (haptic->effects == NULL) {
SDL_OutOfMemory();
goto open_err;
}
225
226
227
/* Clear the memory */
SDL_memset(haptic->effects, 0,
sizeof(struct haptic_effect) * haptic->neffects);
228
229
return 0;
230
231
232
233
234
235
236
237
238
/* Error handling */
open_err:
close(fd);
if (haptic->hwdata != NULL) {
free(haptic->hwdata);
haptic->hwdata = NULL;
}
return -1;
239
240
241
}
242
243
244
245
246
247
/*
* Closes the haptic device.
*/
void
SDL_SYS_HapticClose(SDL_Haptic * haptic)
{
248
249
250
251
252
253
254
255
if (haptic->hwdata) {
/* Clean up */
close(haptic->hwdata->fd);
/* Free */
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;
256
257
SDL_free(haptic->effects);
haptic->neffects = 0;
258
}
259
260
261
}
262
263
264
/*
* Clean up after system specific haptic stuff
*/
265
266
267
268
269
270
271
272
273
274
275
void
SDL_SYS_HapticQuit(void)
{
int i;
for (i=0; SDL_hapticlist[i].fname != NULL; i++) {
SDL_free(SDL_hapticlist[i].fname);
}
SDL_hapticlist[0].fname = NULL;
}
276
#define CLAMP(x) (((x) > 32767) ? 32767 : x)
277
278
/*
* Initializes the linux effect struct from a haptic_effect.
279
* Values above 32767 (for unsigned) are unspecified so we must clamp.
280
281
*/
static int
282
SDL_SYS_ToFFEffect( struct ff_effect * dest, SDL_HapticEffect * src )
283
{
284
int i;
285
286
SDL_HapticConstant *constant;
SDL_HapticPeriodic *periodic;
287
288
SDL_HapticCondition *condition;
SDL_HapticRamp *ramp;
289
290
/* Clear up */
291
SDL_memset(dest, 0, sizeof(struct ff_effect));
292
293
switch (src->type) {
294
case SDL_HAPTIC_CONSTANT:
295
296
297
constant = &src->constant;
/* Header */
298
dest->type = FF_CONSTANT;
299
dest->direction = CLAMP(constant->direction);
300
301
/* Replay */
302
303
dest->replay.length = CLAMP(constant->length);
dest->replay.delay = CLAMP(constant->delay);
304
305
/* Trigger */
306
307
dest->trigger.button = CLAMP(constant->button);
dest->trigger.interval = CLAMP(constant->interval);
308
309
310
311
312
/* Constant */
dest->u.constant.level = constant->level;
/* Envelope */
313
314
315
316
dest->u.constant.envelope.attack_length = CLAMP(constant->attack_length);
dest->u.constant.envelope.attack_level = CLAMP(constant->attack_level);
dest->u.constant.envelope.fade_length = CLAMP(constant->fade_length);
dest->u.constant.envelope.fade_level = CLAMP(constant->fade_level);
317
318
319
320
321
322
323
324
break;
case SDL_HAPTIC_PERIODIC:
periodic = &src->periodic;
/* Header */
dest->type = FF_PERIODIC;
325
dest->direction = CLAMP(periodic->direction);
326
327
/* Replay */
328
329
dest->replay.length = CLAMP(periodic->length);
dest->replay.delay = CLAMP(periodic->delay);
330
331
/* Trigger */
332
333
dest->trigger.button = CLAMP(periodic->button);
dest->trigger.interval = CLAMP(periodic->interval);
334
335
/* Constant */
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
switch (periodic->waveform) {
case SDL_WAVEFORM_SINE:
dest->u.periodic.waveform = FF_SINE;
break;
case SDL_WAVEFORM_SQUARE:
dest->u.periodic.waveform = FF_SQUARE;
break;
case SDL_WAVEFORM_TRIANGLE:
dest->u.periodic.waveform = FF_TRIANGLE;
break;
case SDL_WAVEFORM_SAWTOOTHUP:
dest->u.periodic.waveform = FF_SAW_UP;
break;
case SDL_WAVEFORM_SAWTOOTHDOWN:
dest->u.periodic.waveform = FF_SAW_DOWN;
break;
default:
SDL_SetError("Unknown waveform.");
return -1;
}
dest->u.periodic.period = CLAMP(periodic->period);
358
359
dest->u.periodic.magnitude = periodic->magnitude;
dest->u.periodic.offset = periodic->offset;
360
dest->u.periodic.phase = CLAMP(periodic->phase);
361
362
/* Envelope */
363
364
365
366
dest->u.periodic.envelope.attack_length = CLAMP(periodic->attack_length);
dest->u.periodic.envelope.attack_level = CLAMP(periodic->attack_level);
dest->u.periodic.envelope.fade_length = CLAMP(periodic->fade_length);
dest->u.periodic.envelope.fade_level = CLAMP(periodic->fade_level);
367
368
369
break;
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
case SDL_HAPTIC_SPRING:
case SDL_HAPTIC_DAMPER:
case SDL_HAPTIC_INERTIA:
case SDL_HAPTIC_FRICTION:
condition = &src->condition;
/* Header */
if (dest->type == SDL_HAPTIC_SPRING)
dest->type = FF_SPRING;
else if (dest->type == SDL_HAPTIC_DAMPER)
dest->type = FF_DAMPER;
else if (dest->type == SDL_HAPTIC_INERTIA)
dest->type = FF_INERTIA;
else if (dest->type == SDL_HAPTIC_FRICTION)
dest->type = FF_FRICTION;
dest->direction = CLAMP(condition->direction);
/* Replay */
dest->replay.length = CLAMP(condition->length);
dest->replay.delay = CLAMP(condition->delay);
/* Trigger */
dest->trigger.button = CLAMP(condition->button);
dest->trigger.interval = CLAMP(condition->interval);
/* Condition - TODO handle axes */
dest->u.condition[0].right_saturation = CLAMP(condition->right_sat);
dest->u.condition[0].left_saturation = CLAMP(condition->left_sat);
dest->u.condition[0].right_coeff = condition->right_coeff;
dest->u.condition[0].left_coeff = condition->left_coeff;
dest->u.condition[0].deadband = CLAMP(condition->deadband);
dest->u.condition[0].center = condition->center;
break;
case SDL_HAPTIC_RAMP:
ramp = &src->ramp;
/* Header */
dest->type = FF_RAMP;
dest->direction = CLAMP(ramp->direction);
/* Replay */
dest->replay.length = CLAMP(ramp->length);
dest->replay.delay = CLAMP(ramp->delay);
/* Trigger */
dest->trigger.button = CLAMP(ramp->button);
dest->trigger.interval = CLAMP(ramp->interval);
/* Ramp */
dest->u.ramp.start_level = ramp->start;
dest->u.ramp.end_level = ramp->end;
/* Envelope */
dest->u.ramp.envelope.attack_length = CLAMP(ramp->attack_length);
dest->u.ramp.envelope.attack_level = CLAMP(ramp->attack_level);
dest->u.ramp.envelope.fade_length = CLAMP(ramp->fade_length);
dest->u.ramp.envelope.fade_level = CLAMP(ramp->fade_level);
break;
433
434
435
436
437
438
439
440
default:
SDL_SetError("Unknown haptic effect type.");
return -1;
}
return 0;
}
441
442
443
444
445
/*
* Creates a new haptic effect.
*/
int
446
447
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
SDL_HapticEffect * base)
448
{
449
450
struct ff_effect * linux_effect;
451
452
453
454
455
456
457
458
459
/* Allocate the hardware effect */
effect->hweffect = (struct haptic_hweffect *)
SDL_malloc(sizeof(struct haptic_hweffect));
if (effect->hweffect == NULL) {
SDL_OutOfMemory();
return -1;
}
/* Prepare the ff_effect */
460
461
linux_effect = &effect->hweffect->effect;
if (SDL_SYS_ToFFEffect( linux_effect, base ) != 0) {
462
463
return -1;
}
464
linux_effect->id = -1; /* Have the kernel give it an id */
465
466
/* Upload the effect */
467
if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
468
SDL_SetError("Error uploading effect to the haptic device.");
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
return -1;
}
return 0;
}
/*
* Runs an effect.
*/
int
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
{
struct input_event run;
/* Prepare to run the effect */
run.type = EV_FF;
run.code = effect->hweffect->effect.id;
run.value = 1;
if (write(haptic->hwdata->fd, (const void*) &run, sizeof(run)) < 0) {
SDL_SetError("Unable to run the haptic effect.");
return -1;
}
return 0;
}
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* Stops an effect.
*/
int
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
{
struct input_event stop;
stop.type = EV_FF;
stop.code = effect->hweffect->effect.id;
stop.value = 0;
if (write(haptic->hwdata->fd, (const void*) &stop, sizeof(stop)) < 0) {
SDL_SetError("Unable to stop the haptic effect.");
return -1;
}
return 0;
}
519
520
521
522
523
524
525
526
527
528
529
/*
* Frees the effect
*/
void
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
{
if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
SDL_SetError("Error removing the effect from the haptic device.");
}
SDL_free(effect->hweffect);
effect->hweffect = NULL;
530
531
}
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* Sets the gain.
*/
int
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
{
struct input_event ie;
ie.type = EV_FF;
ie.code = FF_GAIN;
ie.value = (0xFFFFUL * gain) / 100;
545
if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
546
SDL_SetError("Error setting gain.");
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
return -1;
}
return 0;
}
/*
* Sets the autocentering.
*/
int
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
{
struct input_event ie;
ie.type = EV_FF;
ie.code = FF_AUTOCENTER;
ie.value = (0xFFFFUL * autocenter) / 100;
if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
SDL_SetError("Error setting autocenter.");
return -1;
569
570
}
571
return 0;
572
573
574
}
575
576
#endif /* SDL_HAPTIC_LINUX */