Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
913 lines (735 loc) · 23 KB

SDL_syshaptic.c

File metadata and controls

913 lines (735 loc) · 23 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
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"
Jul 3, 2008
Jul 3, 2008
24
#ifdef SDL_HAPTIC_LINUX
25
26
27
#include "SDL_haptic.h"
#include "../SDL_syshaptic.h"
Jul 2, 2008
Jul 2, 2008
28
29
30
#include "SDL_joystick.h"
#include "../../joystick/SDL_sysjoystick.h" /* For the real SDL_Joystick */
#include "../../joystick/linux/SDL_sysjoystick_c.h" /* For joystick hwdata */
31
32
#include <unistd.h> /* close */
Aug 12, 2008
Aug 12, 2008
33
34
#include <linux/input.h> /* Force feedback linux stuff. */
#include <fcntl.h> /* O_RDWR */
Jul 10, 2008
Jul 10, 2008
35
#include <limits.h> /* INT_MAX */
Aug 12, 2008
Aug 12, 2008
36
37
#include <errno.h> /* errno, strerror */
#include <math.h> /* atan2 */
Jul 9, 2008
Jul 9, 2008
38
Aug 12, 2008
Aug 12, 2008
39
/* Just in case. */
Jul 9, 2008
Jul 9, 2008
40
41
42
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
Aug 12, 2008
Aug 12, 2008
45
#define MAX_HAPTICS 32 /* It's doubtful someone has more then 32 evdev */
Jun 1, 2008
Jun 1, 2008
48
49
50
/*
* List of available haptic devices.
*/
51
52
static struct
{
Aug 12, 2008
Aug 12, 2008
53
54
char *fname; /* Dev path name (like /dev/input/event1) */
SDL_Haptic *haptic; /* Assosciated haptic. */
55
56
} SDL_hapticlist[MAX_HAPTICS];
Jun 1, 2008
Jun 1, 2008
57
58
59
60
/*
* Haptic system hardware data.
*/
61
62
struct haptic_hwdata
{
Aug 12, 2008
Aug 12, 2008
63
int fd; /* File descriptor of the device. */
Jul 17, 2008
Jul 17, 2008
64
char *fname; /* Points to the name in SDL_hapticlist. */
65
66
67
};
Jun 30, 2008
Jun 30, 2008
68
69
70
71
72
/*
* Haptic system effect data.
*/
struct haptic_hweffect
{
Jul 9, 2008
Jul 9, 2008
73
struct ff_effect effect; /* The linux kernel effect structure. */
Jun 30, 2008
Jun 30, 2008
74
75
76
};
Jun 1, 2008
Jun 1, 2008
77
78
79
80
81
#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);
Jun 1, 2008
Jun 1, 2008
82
83
84
85
/*
* Test whether a device has haptic properties.
* Returns available properties or 0 if there are none.
*/
86
87
88
89
90
91
static int
EV_IsHaptic(int fd)
{
unsigned int ret;
unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
Jul 10, 2008
Jul 10, 2008
92
/* Ask device for what it has. */
93
ret = 0;
Jul 10, 2008
Jul 10, 2008
94
if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features) < 0) {
Jul 10, 2008
Jul 10, 2008
95
SDL_SetError("Haptic: Unable to get device's features: %s", strerror(errno));
Jul 2, 2008
Jul 2, 2008
96
97
return -1;
}
Jul 10, 2008
Jul 10, 2008
99
/* Convert supported features to SDL_HAPTIC platform-neutral features. */
100
EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
Aug 6, 2008
Aug 6, 2008
101
102
103
104
105
EV_TEST(FF_SINE, SDL_HAPTIC_SINE);
EV_TEST(FF_SQUARE, SDL_HAPTIC_SQUARE);
EV_TEST(FF_TRIANGLE, SDL_HAPTIC_TRIANGLE);
EV_TEST(FF_SAW_UP, SDL_HAPTIC_SAWTOOTHUP);
EV_TEST(FF_SAW_DOWN, SDL_HAPTIC_SAWTOOTHDOWN);
106
107
108
109
110
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);
Jul 1, 2008
Jul 1, 2008
111
EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM);
112
113
114
EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN);
EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
Jul 10, 2008
Jul 10, 2008
115
/* Return what it supports. */
116
117
118
return ret;
}
Jul 10, 2008
Jul 10, 2008
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Tests whether a device is a mouse or not.
*/
static int
EV_IsMouse(int fd)
{
unsigned long argp[40];
/* Ask for supported features. */
if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(argp)), argp) < 0) {
return -1;
}
/* Currently we only test for BTN_MOUSE which can give fake positives. */
if (test_bit(BTN_MOUSE,argp) != 0) {
return 1;
}
return 0;
}
Jul 6, 2008
Jul 6, 2008
141
142
143
/*
* Initializes the haptic subsystem by finding available devices.
*/
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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;
Jul 10, 2008
Jul 10, 2008
158
159
160
161
/*
* Limit amount of checks to MAX_HAPTICS since we may or may not have
* permission to some or all devices.
*/
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
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 */
Jul 2, 2008
Jul 2, 2008
191
if (EV_IsHaptic(fd) > 0) {
192
193
194
195
196
197
198
199
200
201
202
203
SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
SDL_hapticlist[numhaptics].haptic = NULL;
dev_nums[numhaptics] = sb.st_rdev;
++numhaptics;
}
close(fd);
}
return numhaptics;
}
Jul 17, 2008
Jul 17, 2008
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Gets the name from a file descriptor.
*/
static const char *
SDL_SYS_HapticNameFromFD(int fd)
{
static char namebuf[128];
/* We use the evdev name ioctl. */
if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
return NULL;
}
return namebuf;
}
Jun 1, 2008
Jun 1, 2008
221
222
223
/*
* Return the name of a haptic device, does not need to be opened.
*/
224
225
226
227
const char *
SDL_SYS_HapticName(int index)
{
int fd;
Jul 17, 2008
Jul 17, 2008
228
const char *name;
Jul 10, 2008
Jul 10, 2008
230
/* Open the haptic device. */
231
232
name = NULL;
fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
Jul 10, 2008
Jul 10, 2008
233
234
if (fd >= 0) {
Jul 10, 2008
Jul 10, 2008
235
Jul 17, 2008
Jul 17, 2008
236
237
name = SDL_SYS_HapticNameFromFD(fd);
if (name==NULL) {
Jul 10, 2008
Jul 10, 2008
238
/* No name found, return device character device */
239
240
241
242
243
244
245
246
247
name = SDL_hapticlist[index].fname;
}
}
close(fd);
return name;
}
Jun 1, 2008
Jun 1, 2008
248
/*
Jul 2, 2008
Jul 2, 2008
249
* Opens the haptic device from the file descriptor.
Jun 1, 2008
Jun 1, 2008
250
*/
Jul 2, 2008
Jul 2, 2008
251
252
static int
SDL_SYS_HapticOpenFromFD(SDL_Haptic * haptic, int fd)
Jul 17, 2008
Jul 17, 2008
254
255
const char *name;
Jun 23, 2008
Jun 23, 2008
256
257
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)
Jun 30, 2008
Jun 30, 2008
258
SDL_malloc(sizeof(*haptic->hwdata));
Jun 23, 2008
Jun 23, 2008
259
260
if (haptic->hwdata == NULL) {
SDL_OutOfMemory();
Jun 30, 2008
Jun 30, 2008
261
goto open_err;
Jun 23, 2008
Jun 23, 2008
262
263
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
Jul 10, 2008
Jul 10, 2008
264
Jul 17, 2008
Jul 17, 2008
265
/* Set the data. */
Jun 23, 2008
Jun 23, 2008
266
haptic->hwdata->fd = fd;
Jun 30, 2008
Jun 30, 2008
267
haptic->supported = EV_IsHaptic(fd);
Jul 17, 2008
Jul 17, 2008
268
haptic->naxes = 2; /* Hardcoded for now, not sure if it's possible to find out. */
Jun 30, 2008
Jun 30, 2008
270
271
/* Set the effects */
if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
Jul 10, 2008
Jul 10, 2008
272
SDL_SetError("Haptic: Unable to query device memory: %s", strerror(errno));
Jun 30, 2008
Jun 30, 2008
273
274
goto open_err;
}
Jul 10, 2008
Jul 10, 2008
275
haptic->nplaying = haptic->neffects; /* Linux makes no distinction. */
Jun 30, 2008
Jun 30, 2008
276
277
278
279
280
281
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (haptic->effects == NULL) {
SDL_OutOfMemory();
goto open_err;
}
Jul 1, 2008
Jul 1, 2008
282
283
284
/* Clear the memory */
SDL_memset(haptic->effects, 0,
sizeof(struct haptic_effect) * haptic->neffects);
Jun 30, 2008
Jun 30, 2008
285
286
return 0;
Jun 30, 2008
Jun 30, 2008
287
288
289
290
291
292
293
294
295
/* Error handling */
open_err:
close(fd);
if (haptic->hwdata != NULL) {
free(haptic->hwdata);
haptic->hwdata = NULL;
}
return -1;
296
297
298
}
Jul 2, 2008
Jul 2, 2008
299
300
301
302
303
304
305
/*
* Opens a haptic device for usage.
*/
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
int fd;
Jul 17, 2008
Jul 17, 2008
306
int ret;
Jul 2, 2008
Jul 2, 2008
307
308
309
310
/* Open the character device */
fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
if (fd < 0) {
Jul 10, 2008
Jul 10, 2008
311
SDL_SetError("Haptic: Unable to open %s: %s",
Jul 3, 2008
Jul 3, 2008
312
SDL_hapticlist[haptic->index], strerror(errno));
Jul 2, 2008
Jul 2, 2008
313
314
return -1;
}
Jul 17, 2008
Jul 17, 2008
315
316
317
318
319
320
321
322
323
324
/* Try to create the haptic. */
ret = SDL_SYS_HapticOpenFromFD(haptic,fd); /* Already closes on error. */
if (ret < 0) {
return -1;
}
/* Set the fname. */
haptic->hwdata->fname = SDL_hapticlist[haptic->index].fname;
return 0;
Jul 10, 2008
Jul 10, 2008
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
}
/*
* Opens a haptic device from first mouse it finds for usage.
*/
int
SDL_SYS_HapticMouse(void)
{
int fd;
int i;
for (i=0; i<SDL_numhaptics; i++) {
/* Open the device. */
fd = open(SDL_hapticlist[i].fname, O_RDWR, 0);
if (fd < 0) {
Jul 10, 2008
Jul 10, 2008
342
SDL_SetError("Haptic: Unable to open %s: %s",
Jul 10, 2008
Jul 10, 2008
343
344
345
346
SDL_hapticlist[i], strerror(errno));
return -1;
}
Aug 12, 2008
Aug 12, 2008
347
/* Is it a mouse? */
Jul 10, 2008
Jul 10, 2008
348
349
350
351
352
353
354
355
356
357
if (EV_IsMouse(fd)) {
close(fd);
return i;
}
close(fd);
}
return -1;
}
Jul 2, 2008
Jul 2, 2008
358
359
Jul 2, 2008
Jul 2, 2008
360
361
362
363
364
365
/*
* Checks to see if a joystick has haptic features.
*/
int
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
{
Jul 2, 2008
Jul 2, 2008
366
367
368
369
370
371
372
373
374
375
return EV_IsHaptic(joystick->hwdata->fd);
}
/*
* Checks to see if the haptic device and joystick and in reality the same.
*/
int
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
Jul 17, 2008
Jul 17, 2008
376
377
378
/* We are assuming linux is using evdev which should trump the old
* joystick methods. */
if (SDL_strcmp(joystick->hwdata->fname,haptic->hwdata->fname)==0) {
Jul 2, 2008
Jul 2, 2008
379
return 1;
Jul 2, 2008
Jul 2, 2008
380
}
Jul 2, 2008
Jul 2, 2008
381
382
383
384
385
386
387
388
389
390
return 0;
}
/*
* Opens a SDL_Haptic from a SDL_Joystick.
*/
int
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
Jul 17, 2008
Jul 17, 2008
391
int i;
Jul 2, 2008
Jul 2, 2008
392
int fd;
Jul 17, 2008
Jul 17, 2008
393
394
395
396
397
398
399
400
401
402
403
int ret;
/* Find the joystick in the haptic list. */
for (i=0; i<MAX_HAPTICS; i++) {
if (SDL_hapticlist[i].fname != NULL) {
if (SDL_strcmp(SDL_hapticlist[i].fname, joystick->hwdata->fname)==0) {
haptic->index = i;
}
}
}
Jul 2, 2008
Jul 2, 2008
404
fd = open(joystick->hwdata->fname, O_RDWR, 0);
Jul 17, 2008
Jul 17, 2008
405
406
407
408
409
410
411
ret = SDL_SYS_HapticOpenFromFD(haptic,fd); /* Already closes on error. */
if (ret < 0) {
return -1;
}
haptic->hwdata->fname = SDL_hapticlist[haptic->index].fname;
return 0;
Jul 2, 2008
Jul 2, 2008
412
413
414
}
Jun 1, 2008
Jun 1, 2008
415
416
417
418
419
420
/*
* Closes the haptic device.
*/
void
SDL_SYS_HapticClose(SDL_Haptic * haptic)
{
Jun 23, 2008
Jun 23, 2008
421
422
if (haptic->hwdata) {
Jul 31, 2008
Jul 31, 2008
423
424
425
426
427
/* Free effects. */
SDL_free(haptic->effects);
haptic->effects = NULL;
haptic->neffects = 0;
Jun 23, 2008
Jun 23, 2008
428
429
430
431
/* Clean up */
close(haptic->hwdata->fd);
/* Free */
Jul 31, 2008
Jul 31, 2008
432
433
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;
Jun 23, 2008
Jun 23, 2008
434
}
Jul 17, 2008
Jul 17, 2008
435
436
437
/* Clear the rest. */
SDL_memset(haptic, 0, sizeof(SDL_Haptic));
Jun 1, 2008
Jun 1, 2008
438
439
440
}
Jun 30, 2008
Jun 30, 2008
441
442
443
/*
* Clean up after system specific haptic stuff
*/
Jun 1, 2008
Jun 1, 2008
444
445
446
447
448
449
void
SDL_SYS_HapticQuit(void)
{
int i;
for (i=0; SDL_hapticlist[i].fname != NULL; i++) {
Jul 18, 2008
Jul 18, 2008
450
451
452
/* Opened and not closed haptics are leaked, this is on purpose.
* Close your haptic devices after usage. */
Jun 1, 2008
Jun 1, 2008
453
454
455
456
457
SDL_free(SDL_hapticlist[i].fname);
}
SDL_hapticlist[0].fname = NULL;
}
Jul 30, 2008
Jul 30, 2008
458
459
460
461
462
463
464
465
466
467
468
/*
* Converts an SDL button to a ff_trigger button.
*/
static Uint16
SDL_SYS_ToButton( Uint16 button )
{
Uint16 ff_button;
ff_button = 0;
Aug 12, 2008
Aug 12, 2008
469
470
471
472
/*
* Not sure what the proper syntax is because this actually isn't implemented
* in the current kernel from what I've seen (2.6.26).
*/
Jul 30, 2008
Jul 30, 2008
473
474
475
476
477
478
479
480
if (button != 0) {
ff_button = BTN_GAMEPAD + button - 1;
}
return ff_button;
}
Jul 6, 2008
Jul 6, 2008
481
482
483
484
485
486
487
/*
* Returns the ff_effect usable direction from a SDL_HapticDirection.
*/
static Uint16
SDL_SYS_ToDirection( SDL_HapticDirection * dir )
{
Uint32 tmp;
Jul 9, 2008
Jul 9, 2008
488
float f; /* Ideally we'd use fixed point math instead of floats... */
Jul 6, 2008
Jul 6, 2008
489
490
491
switch (dir->type) {
case SDL_HAPTIC_POLAR:
Jul 7, 2008
Jul 7, 2008
492
493
/* Linux directions are inverted. */
tmp = (((18000 + dir->dir[0]) % 36000) * 0xFFFF) / 36000;
Jul 6, 2008
Jul 6, 2008
494
return (Uint16) tmp;
Jul 17, 2008
Jul 17, 2008
495
Jul 6, 2008
Jul 6, 2008
496
case SDL_HAPTIC_CARTESIAN:
Jul 9, 2008
Jul 9, 2008
497
/* We must invert "x" and "y" to go clockwise. */
Jul 9, 2008
Jul 9, 2008
498
499
500
501
f = atan2(dir->dir[0], dir->dir[1]);
tmp = (int)(f*18000./M_PI) % 36000;
tmp = (tmp * 0xFFFF) / 36000;
return (Uint16) tmp;
Jul 6, 2008
Jul 6, 2008
502
Jul 17, 2008
Jul 17, 2008
503
504
505
506
507
case SDL_HAPTIC_SPHERICAL:
tmp = (36000 - dir->dir[0]) + 27000; /* Convert to polars */
tmp = (((18000 + tmp) % 36000) * 0xFFFF) / 36000;
return (Uint16) tmp;
Jul 6, 2008
Jul 6, 2008
508
default:
Jul 17, 2008
Jul 17, 2008
509
SDL_SetError("Haptic: Unsupported direction type.");
Aug 6, 2008
Aug 6, 2008
510
return (Uint16)-1;
Jul 6, 2008
Jul 6, 2008
511
512
513
514
515
}
return 0;
}
Jul 30, 2008
Jul 30, 2008
516
Jul 1, 2008
Jul 1, 2008
517
#define CLAMP(x) (((x) > 32767) ? 32767 : x)
Jun 30, 2008
Jun 30, 2008
518
519
/*
* Initializes the linux effect struct from a haptic_effect.
Jul 1, 2008
Jul 1, 2008
520
* Values above 32767 (for unsigned) are unspecified so we must clamp.
Jun 30, 2008
Jun 30, 2008
521
522
*/
static int
Jul 1, 2008
Jul 1, 2008
523
SDL_SYS_ToFFEffect( struct ff_effect * dest, SDL_HapticEffect * src )
Jun 30, 2008
Jun 30, 2008
524
{
Jul 18, 2008
Jul 18, 2008
525
Uint32 tmp;
Jul 1, 2008
Jul 1, 2008
526
527
SDL_HapticConstant *constant;
SDL_HapticPeriodic *periodic;
Jul 1, 2008
Jul 1, 2008
528
529
SDL_HapticCondition *condition;
SDL_HapticRamp *ramp;
Jul 1, 2008
Jul 1, 2008
530
531
/* Clear up */
Jun 30, 2008
Jun 30, 2008
532
SDL_memset(dest, 0, sizeof(struct ff_effect));
Jul 1, 2008
Jul 1, 2008
533
534
switch (src->type) {
Jun 30, 2008
Jun 30, 2008
535
case SDL_HAPTIC_CONSTANT:
Jul 1, 2008
Jul 1, 2008
536
537
538
constant = &src->constant;
/* Header */
Jun 30, 2008
Jun 30, 2008
539
dest->type = FF_CONSTANT;
Jul 6, 2008
Jul 6, 2008
540
dest->direction = SDL_SYS_ToDirection(&constant->direction);
Aug 6, 2008
Aug 6, 2008
541
if (dest->direction == (Uint16)-1) return -1;
Jul 1, 2008
Jul 1, 2008
542
543
/* Replay */
Jul 20, 2008
Jul 20, 2008
544
545
dest->replay.length = (constant->length == SDL_HAPTIC_INFINITY) ?
0 : CLAMP(constant->length);
Jul 1, 2008
Jul 1, 2008
546
dest->replay.delay = CLAMP(constant->delay);
Jul 1, 2008
Jul 1, 2008
547
548
/* Trigger */
Jul 30, 2008
Jul 30, 2008
549
dest->trigger.button = SDL_SYS_ToButton(constant->button);
Jul 1, 2008
Jul 1, 2008
550
dest->trigger.interval = CLAMP(constant->interval);
Jul 1, 2008
Jul 1, 2008
551
552
553
554
555
/* Constant */
dest->u.constant.level = constant->level;
/* Envelope */
Jul 1, 2008
Jul 1, 2008
556
557
558
559
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);
Jul 1, 2008
Jul 1, 2008
560
561
562
break;
Jul 1, 2008
Jul 1, 2008
563
564
565
566
567
case SDL_HAPTIC_SINE:
case SDL_HAPTIC_SQUARE:
case SDL_HAPTIC_TRIANGLE:
case SDL_HAPTIC_SAWTOOTHUP:
case SDL_HAPTIC_SAWTOOTHDOWN:
Jul 1, 2008
Jul 1, 2008
568
569
570
571
periodic = &src->periodic;
/* Header */
dest->type = FF_PERIODIC;
Jul 6, 2008
Jul 6, 2008
572
dest->direction = SDL_SYS_ToDirection(&periodic->direction);
Aug 6, 2008
Aug 6, 2008
573
if (dest->direction == (Uint16)-1) return -1;
Jul 1, 2008
Jul 1, 2008
574
575
/* Replay */
Jul 20, 2008
Jul 20, 2008
576
577
dest->replay.length = (periodic->length == SDL_HAPTIC_INFINITY) ?
0 : CLAMP(periodic->length);
Jul 1, 2008
Jul 1, 2008
578
dest->replay.delay = CLAMP(periodic->delay);
Jul 1, 2008
Jul 1, 2008
579
580
/* Trigger */
Jul 30, 2008
Jul 30, 2008
581
dest->trigger.button = SDL_SYS_ToButton(periodic->button);
Jul 1, 2008
Jul 1, 2008
582
dest->trigger.interval = CLAMP(periodic->interval);
Jul 1, 2008
Jul 1, 2008
583
Jul 1, 2008
Jul 1, 2008
584
585
586
587
588
589
590
591
592
593
594
/* Periodic */
if (periodic->type == SDL_HAPTIC_SINE)
dest->u.periodic.waveform = FF_SINE;
else if (periodic->type == SDL_HAPTIC_SQUARE)
dest->u.periodic.waveform = FF_SQUARE;
else if (periodic->type == SDL_HAPTIC_TRIANGLE)
dest->u.periodic.waveform = FF_TRIANGLE;
else if (periodic->type == SDL_HAPTIC_SAWTOOTHUP)
dest->u.periodic.waveform = FF_SAW_UP;
else if (periodic->type == SDL_HAPTIC_SAWTOOTHDOWN)
dest->u.periodic.waveform = FF_SAW_DOWN;
Jul 1, 2008
Jul 1, 2008
595
dest->u.periodic.period = CLAMP(periodic->period);
Jul 1, 2008
Jul 1, 2008
596
597
dest->u.periodic.magnitude = periodic->magnitude;
dest->u.periodic.offset = periodic->offset;
Jul 18, 2008
Jul 18, 2008
598
599
600
/* Phase is calculated based of offset from period and then clamped. */
tmp = ((periodic->phase % 36000) * dest->u.periodic.period) / 36000;
dest->u.periodic.phase = CLAMP(tmp);
Jul 1, 2008
Jul 1, 2008
601
602
/* Envelope */
Jul 1, 2008
Jul 1, 2008
603
604
605
606
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);
Jun 30, 2008
Jun 30, 2008
607
608
609
break;
Jul 1, 2008
Jul 1, 2008
610
611
612
613
614
615
616
case SDL_HAPTIC_SPRING:
case SDL_HAPTIC_DAMPER:
case SDL_HAPTIC_INERTIA:
case SDL_HAPTIC_FRICTION:
condition = &src->condition;
/* Header */
Jul 7, 2008
Jul 7, 2008
617
if (condition->type == SDL_HAPTIC_SPRING)
Jul 1, 2008
Jul 1, 2008
618
dest->type = FF_SPRING;
Jul 7, 2008
Jul 7, 2008
619
else if (condition->type == SDL_HAPTIC_DAMPER)
Jul 1, 2008
Jul 1, 2008
620
dest->type = FF_DAMPER;
Jul 7, 2008
Jul 7, 2008
621
else if (condition->type == SDL_HAPTIC_INERTIA)
Jul 1, 2008
Jul 1, 2008
622
dest->type = FF_INERTIA;
Jul 7, 2008
Jul 7, 2008
623
else if (condition->type == SDL_HAPTIC_FRICTION)
Jul 1, 2008
Jul 1, 2008
624
dest->type = FF_FRICTION;
Jul 6, 2008
Jul 6, 2008
625
dest->direction = 0; /* Handled by the condition-specifics. */
Jul 1, 2008
Jul 1, 2008
626
627
/* Replay */
Jul 20, 2008
Jul 20, 2008
628
629
dest->replay.length = (condition->length == SDL_HAPTIC_INFINITY) ?
0 : CLAMP(condition->length);
Jul 1, 2008
Jul 1, 2008
630
631
632
dest->replay.delay = CLAMP(condition->delay);
/* Trigger */
Jul 30, 2008
Jul 30, 2008
633
dest->trigger.button = SDL_SYS_ToButton(condition->button);
Jul 1, 2008
Jul 1, 2008
634
635
dest->trigger.interval = CLAMP(condition->interval);
Jul 6, 2008
Jul 6, 2008
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/* Condition */
/* X axis */
dest->u.condition[0].right_saturation = CLAMP(condition->right_sat[0]);
dest->u.condition[0].left_saturation = CLAMP(condition->left_sat[0]);
dest->u.condition[0].right_coeff = condition->right_coeff[0];
dest->u.condition[0].left_coeff = condition->left_coeff[0];
dest->u.condition[0].deadband = CLAMP(condition->deadband[0]);
dest->u.condition[0].center = condition->center[0];
/* Y axis */
dest->u.condition[1].right_saturation = CLAMP(condition->right_sat[1]);
dest->u.condition[1].left_saturation = CLAMP(condition->left_sat[1]);
dest->u.condition[1].right_coeff = condition->right_coeff[1];
dest->u.condition[1].left_coeff = condition->left_coeff[1];
dest->u.condition[1].deadband = CLAMP(condition->deadband[1]);
dest->u.condition[1].center = condition->center[1];
Jul 1, 2008
Jul 1, 2008
651
Aug 12, 2008
Aug 12, 2008
652
653
654
655
/*
* There is no envelope in the linux force feedback api for conditions.
*/
Jul 1, 2008
Jul 1, 2008
656
657
658
659
660
661
662
break;
case SDL_HAPTIC_RAMP:
ramp = &src->ramp;
/* Header */
dest->type = FF_RAMP;
Jul 6, 2008
Jul 6, 2008
663
dest->direction = SDL_SYS_ToDirection(&ramp->direction);
Aug 6, 2008
Aug 6, 2008
664
if (dest->direction == (Uint16)-1) return -1;
Jul 1, 2008
Jul 1, 2008
665
666
/* Replay */
Jul 20, 2008
Jul 20, 2008
667
668
dest->replay.length = (ramp->length == SDL_HAPTIC_INFINITY) ?
0 : CLAMP(ramp->length);
Jul 1, 2008
Jul 1, 2008
669
670
671
dest->replay.delay = CLAMP(ramp->delay);
/* Trigger */
Jul 30, 2008
Jul 30, 2008
672
dest->trigger.button = SDL_SYS_ToButton(ramp->button);
Jul 1, 2008
Jul 1, 2008
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
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;
Jun 30, 2008
Jun 30, 2008
688
default:
Jul 10, 2008
Jul 10, 2008
689
SDL_SetError("Haptic: Unknown effect type.");
Jun 30, 2008
Jun 30, 2008
690
691
692
693
694
695
return -1;
}
return 0;
}
Jul 1, 2008
Jul 1, 2008
696
Jun 30, 2008
Jun 30, 2008
697
698
699
700
/*
* Creates a new haptic effect.
*/
int
Jul 1, 2008
Jul 1, 2008
701
702
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
SDL_HapticEffect * base)
Jun 30, 2008
Jun 30, 2008
703
{
Jul 1, 2008
Jul 1, 2008
704
705
struct ff_effect * linux_effect;
Jun 30, 2008
Jun 30, 2008
706
707
708
709
710
711
712
713
714
/* 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 */
Jul 1, 2008
Jul 1, 2008
715
716
linux_effect = &effect->hweffect->effect;
if (SDL_SYS_ToFFEffect( linux_effect, base ) != 0) {
Jul 1, 2008
Jul 1, 2008
717
goto new_effect_err;
Jun 30, 2008
Jun 30, 2008
718
}
Jul 1, 2008
Jul 1, 2008
719
linux_effect->id = -1; /* Have the kernel give it an id */
Jun 30, 2008
Jun 30, 2008
720
721
/* Upload the effect */
Jul 1, 2008
Jul 1, 2008
722
if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
Jul 10, 2008
Jul 10, 2008
723
SDL_SetError("Haptic: Error uploading effect to the device: %s", strerror(errno));
Jul 1, 2008
Jul 1, 2008
724
goto new_effect_err;
Jun 30, 2008
Jun 30, 2008
725
726
727
}
return 0;
Jul 1, 2008
Jul 1, 2008
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
new_effect_err:
free(effect->hweffect);
effect->hweffect = NULL;
return -1;
}
/*
* Updates an effect.
*
* Note: Dynamically updating the direction can in some cases force
* the effect to restart and run once.
*/
int SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
struct haptic_effect * effect, SDL_HapticEffect * data)
{
struct ff_effect linux_effect;
/* Create the new effect */
if (SDL_SYS_ToFFEffect( &linux_effect, data ) != 0) {
return -1;
}
linux_effect.id = effect->hweffect->effect.id;
/* See if it can be uploaded. */
if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
Jul 10, 2008
Jul 10, 2008
755
SDL_SetError("Haptic: Error updating the effect: %s", strerror(errno));
Jul 1, 2008
Jul 1, 2008
756
757
758
759
760
761
762
return -1;
}
/* Copy the new effect into memory. */
SDL_memcpy( &effect->hweffect->effect, &linux_effect, sizeof(struct ff_effect) );
return effect->hweffect->effect.id;
Jun 30, 2008
Jun 30, 2008
763
764
765
766
767
768
769
}
/*
* Runs an effect.
*/
int
Jul 15, 2008
Jul 15, 2008
770
771
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
Uint32 iterations)
Jun 30, 2008
Jun 30, 2008
772
773
774
775
776
777
{
struct input_event run;
/* Prepare to run the effect */
run.type = EV_FF;
run.code = effect->hweffect->effect.id;
Jul 20, 2008
Jul 20, 2008
778
/* We don't actually have infinity here, so we just do INT_MAX which is pretty damn close. */
Jul 15, 2008
Jul 15, 2008
779
run.value = (iterations > INT_MAX) ? INT_MAX : iterations;
Jun 30, 2008
Jun 30, 2008
780
781
if (write(haptic->hwdata->fd, (const void*) &run, sizeof(run)) < 0) {
Jul 10, 2008
Jul 10, 2008
782
SDL_SetError("Haptic: Unable to run the effect: %s", strerror(errno));
Jun 30, 2008
Jun 30, 2008
783
784
785
786
787
788
789
return -1;
}
return 0;
}
Jul 1, 2008
Jul 1, 2008
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
* 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) {
Jul 10, 2008
Jul 10, 2008
803
SDL_SetError("Haptic: Unable to stop the effect: %s", strerror(errno));
Jul 1, 2008
Jul 1, 2008
804
805
806
807
808
809
810
return -1;
}
return 0;
}
Jun 30, 2008
Jun 30, 2008
811
/*
Jul 1, 2008
Jul 1, 2008
812
* Frees the effect.
Jun 30, 2008
Jun 30, 2008
813
814
815
816
817
*/
void
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
{
if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
Jul 10, 2008
Jul 10, 2008
818
SDL_SetError("Haptic: Error removing the effect from the device: %s",
Jul 3, 2008
Jul 3, 2008
819
strerror(errno));
Jun 30, 2008
Jun 30, 2008
820
821
822
}
SDL_free(effect->hweffect);
effect->hweffect = NULL;
Jun 30, 2008
Jun 30, 2008
823
824
}
Jun 1, 2008
Jun 1, 2008
825
Jul 3, 2008
Jul 3, 2008
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
* Gets the status of a haptic effect.
*/
int
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect * effect)
{
#if 0 /* Not supported atm. */
struct input_event ie;
ie.type = EV_FF;
ie.type = EV_FF_STATUS;
ie.code = effect->hweffect->effect.id;
if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
Jul 10, 2008
Jul 10, 2008
840
SDL_SetError("Haptic: Error getting device status.");
Jul 3, 2008
Jul 3, 2008
841
842
843
844
845
846
847
848
849
850
return -1;
}
return 0;
#endif
return -1;
}
Jul 1, 2008
Jul 1, 2008
851
852
853
854
855
856
857
858
859
860
861
862
/*
* 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;
Jul 1, 2008
Jul 1, 2008
863
if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
Jul 10, 2008
Jul 10, 2008
864
SDL_SetError("Haptic: Error setting gain: %s", strerror(errno));
Jul 1, 2008
Jul 1, 2008
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
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) {
Jul 10, 2008
Jul 10, 2008
885
SDL_SetError("Haptic: Error setting autocenter: %s", strerror(errno));
Jul 1, 2008
Jul 1, 2008
886
return -1;
Jul 1, 2008
Jul 1, 2008
887
888
}
Jul 1, 2008
Jul 1, 2008
889
return 0;
Jul 1, 2008
Jul 1, 2008
890
891
892
}
Aug 24, 2008
Aug 24, 2008
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/*
* Pausing is not supported atm by linux.
*/
int
SDL_SYS_HapticPause(SDL_Haptic * haptic)
{
return -1;
}
/*
* Unpausing is not supported atm by linux.
*/
int
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
{
return -1;
}
913
#endif /* SDL_HAPTIC_LINUX */