This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_haptic.c
570 lines (469 loc) · 11.5 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
/*
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"
#include "SDL_syshaptic.h"
25
#include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
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
static Uint8 SDL_numhaptics = 0;
SDL_Haptic **SDL_haptics = NULL;
static SDL_Haptic *default_haptic = NULL;
/*
* Initializes the Haptic devices.
*/
int
SDL_HapticInit(void)
{
int arraylen;
int status;
SDL_numhaptics = 0;
status = SDL_SYS_HapticInit();
if (status >= 0) {
arraylen = (status + 1) * sizeof(*SDL_haptics);
SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
if (SDL_haptics == NULL) {
SDL_numhaptics = 0;
} else {
SDL_memset(SDL_haptics, 0, arraylen);
SDL_numhaptics = status;
}
status = 0;
}
default_haptic = NULL;
return status;
}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Checks to see if the haptic device is valid
*/
static int
ValidHaptic(SDL_Haptic ** haptic)
{
int valid;
if (*haptic == NULL) {
SDL_SetError("Haptic device hasn't been opened yet");
valid = 0;
} else {
valid = 1;
}
return valid;
}
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
/*
* Returns the number of available devices.
*/
int
SDL_NumHaptics(void)
{
return SDL_numhaptics;
}
/*
* Gets the name of a Haptic device by index.
*/
const char *
SDL_HapticName(int device_index)
{
if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
return NULL;
}
return SDL_SYS_HapticName(device_index);
}
/*
104
* Opens a Haptic device.
105
106
107
108
109
110
111
112
113
114
115
116
117
*/
SDL_Haptic *
SDL_HapticOpen(int device_index)
{
int i;
SDL_Haptic *haptic;
if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
return NULL;
}
/* If the haptic is already open, return it */
118
for (i=0; SDL_haptics[i]; i++) {
119
120
121
122
123
124
125
if (device_index == SDL_haptics[i]->index) {
haptic = SDL_haptics[i];
++haptic->ref_count;
return haptic;
}
}
126
/* Create the haptic device */
127
haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
128
129
130
if (haptic == NULL) {
SDL_OutOfMemory();
return NULL;
131
}
132
133
134
135
136
137
138
/* Initialize the haptic device */
SDL_memset(haptic, 0, (sizeof *haptic));
haptic->index = device_index;
if (SDL_SYS_HapticOpen(haptic) < 0) {
SDL_free(haptic);
return NULL;
139
}
140
141
142
143
144
145
146
/* Disable autocenter and set gain to max. */
if (haptic->supported & SDL_HAPTIC_GAIN)
SDL_HapticSetGain(haptic,100);
if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
SDL_HapticSetAutocenter(haptic,0);
147
148
/* Add haptic to list */
++haptic->ref_count;
149
for (i=0; SDL_haptics[i]; i++)
150
151
152
/* Skip to next haptic */ ;
SDL_haptics[i] = haptic;
153
154
return haptic;
}
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Returns 1 if the device has been opened.
*/
int
SDL_HapticOpened(int device_index)
{
int i, opened;
opened = 0;
for (i=0; SDL_haptics[i]; i++) {
if (SDL_haptics[i]->index == (Uint8) device_index) {
opened = 1;
break;
}
}
return opened;
}
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Returns the index to a haptic device.
*/
int
SDL_HapticIndex(SDL_Haptic * haptic)
{
if (!ValidHaptic(&haptic)) {
return -1;
}
return haptic->index;
}
190
191
192
193
194
195
196
197
/*
* Returns SDL_TRUE if joystick has haptic features.
*/
int
SDL_JoystickIsHaptic(SDL_Joystick * joystick)
{
int ret;
198
/* Must be a valid joystick */
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
if (!SDL_PrivateJoystickValid(&joystick)) {
return -1;
}
ret = SDL_SYS_JoystickIsHaptic(joystick);
if (ret > 0) return SDL_TRUE;
else if (ret == 0) return SDL_FALSE;
else return -1;
}
/*
* Opens a haptic device from a joystick.
*/
SDL_Haptic *
SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
{
217
218
219
220
int i;
SDL_Haptic *haptic;
/* Must be a valid joystick */
221
if (!SDL_PrivateJoystickValid(&joystick)) {
222
223
224
225
226
227
228
229
230
231
return NULL;
}
/* Joystick must be haptic */
if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
return NULL;
}
/* Check to see if joystick's haptic is already open */
for (i=0; SDL_haptics[i]; i++) {
232
if (SDL_SYS_JoystickSameHaptic(SDL_haptics[i],joystick)) {
233
234
235
236
237
238
239
240
241
242
243
haptic = SDL_haptics[i];
++haptic->ref_count;
return haptic;
}
}
/* Create the haptic device */
haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
if (haptic == NULL) {
SDL_OutOfMemory();
return NULL;
244
}
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Initialize the haptic device */
SDL_memset(haptic, 0, (sizeof *haptic));
if (SDL_SYS_HapticOpenFromJoystick(haptic,joystick) < 0) {
SDL_free(haptic);
return NULL;
}
/* Add haptic to list */
++haptic->ref_count;
for (i=0; SDL_haptics[i]; i++)
/* Skip to next haptic */ ;
SDL_haptics[i] = haptic;
return haptic;
260
261
262
}
263
264
265
266
267
268
/*
* Closes a SDL_Haptic device.
*/
void
SDL_HapticClose(SDL_Haptic * haptic)
{
269
270
271
272
273
274
275
276
277
278
279
280
int i;
/* Must be valid */
if (!ValidHaptic(&haptic)) {
return;
}
/* Check if it's still in use */
if (--haptic->ref_count < 0) {
return;
}
281
282
283
284
285
286
/* Close it, properly removing effects if needed */
for (i=0; i<haptic->neffects; i++) {
if (haptic->effects[i].hweffect != NULL) {
SDL_HapticDestroyEffect(haptic,i);
}
}
287
288
289
290
291
292
293
294
295
296
297
298
299
SDL_SYS_HapticClose(haptic);
/* Remove from the list */
for (i = 0; SDL_haptics[i]; ++i) {
if (haptic == SDL_haptics[i]) {
SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
(SDL_numhaptics - i) * sizeof(haptic));
break;
}
}
/* Free */
SDL_free(haptic);
300
301
302
303
304
}
/*
* Cleans up after the subsystem.
*/
305
306
307
308
309
310
311
312
313
314
315
void
SDL_HapticQuit(void)
{
SDL_numhaptics = 0;
SDL_SYS_HapticQuit();
if (SDL_haptics != NULL) {
SDL_free(SDL_haptics);
SDL_haptics = NULL;
}
}
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Returns the number of effects a haptic device has.
*/
int
SDL_HapticNumEffects(SDL_Haptic * haptic)
{
if (!ValidHaptic(&haptic)) {
return -1;
}
return haptic->neffects;
}
/*
* Returns supported effects by the device.
*/
unsigned int
334
SDL_HapticQuery(SDL_Haptic * haptic)
335
336
337
338
339
340
341
342
{
if (!ValidHaptic(&haptic)) {
return -1;
}
return haptic->supported;
}
343
344
345
/*
* Checks to see if the device can support the effect.
*/
346
347
348
349
350
351
352
353
354
355
356
int
SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
{
if (!ValidHaptic(&haptic)) {
return -1;
}
if ((haptic->supported & effect->type) != 0)
return SDL_TRUE;
return SDL_FALSE;
}
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Creates a new haptic effect.
*/
int
SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
{
int i;
/* Check for device validity. */
if (!ValidHaptic(&haptic)) {
return -1;
}
371
372
373
374
375
376
/* Check to see if effect is supported */
if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
SDL_SetError("Haptic effect not supported by haptic device.");
return -1;
}
377
378
379
380
381
/* See if there's a free slot */
for (i=0; i<haptic->neffects; i++) {
if (haptic->effects[i].hweffect == NULL) {
/* Now let the backend create the real effect */
382
if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
383
384
385
386
387
388
389
390
391
392
return -1; /* Backend failed to create effect */
}
return i;
}
}
SDL_SetError("Haptic device has no free space left.");
return -1;
}
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Checks to see if an effect is valid.
*/
static int
ValidEffect(SDL_Haptic * haptic, int effect)
{
if ((effect < 0) || (effect >= haptic->neffects)) {
SDL_SetError("Invalid haptic effect identifier.");
return 0;
}
return 1;
}
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Updates an effect.
*/
int
SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data)
{
if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
return -1;
}
/* Updates the effect */
if (SDL_SYS_HapticUpdateEffect(haptic,&haptic->effects[effect],data) < 0) {
return -1;
}
return 0;
}
425
426
427
428
429
430
/*
* Runs the haptic effect on the device.
*/
int
SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
{
431
if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
432
433
return -1;
}
434
435
436
437
438
439
440
/* Run the effect */
if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
return -1;
}
return 0;
441
442
}
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Stops the haptic effect on the device.
*/
int
SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
{
if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
return -1;
}
/* Stop the effect */
if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
return -1;
}
return 0;
}
461
462
463
464
465
466
/*
* Gets rid of a haptic effect.
*/
void
SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
{
467
if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
468
469
return;
}
470
471
472
473
474
475
476
/* Not allocated */
if (haptic->effects[effect].hweffect == NULL) {
return;
}
SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
477
478
}
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* Gets the status of a haptic effect.
*/
int
SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect)
{
if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
return -1;
}
if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
SDL_SetError("Haptic device does not support status queries.");
return -1;
}
return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
}
497
498
499
500
501
502
/*
* Sets the global gain of the device.
*/
int
SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
{
503
504
505
const char *env;
int real_gain, max_gain;
506
507
508
509
if (!ValidHaptic(&haptic)) {
return -1;
}
510
511
512
513
514
if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
SDL_SetError("Haptic device does not support setting gain.");
return -1;
}
515
516
517
518
519
if ((gain < 0) || (gain > 100)) {
SDL_SetError("Haptic gain must be between 0 and 100.");
return -1;
}
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* We use the envvar to get the maximum gain. */
env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
if (env != NULL) {
max_gain = SDL_atoi(env);
/* Check for sanity. */
if (max_gain < 0) max_gain = 0;
else if (max_gain > 100) max_gain = 100;
/* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
real_gain = (gain * max_gain) / 100;
}
else {
real_gain = gain;
}
if (SDL_SYS_HapticSetGain(haptic,real_gain) < 0) {
537
538
539
540
541
542
return -1;
}
return 0;
}
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Makes the device autocenter, 0 disables.
*/
int
SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
{
if (!ValidHaptic(&haptic)) {
return -1;
}
if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
SDL_SetError("Haptic device does not support setting autocenter.");
return -1;
}
if ((autocenter < 0) || (autocenter > 100)) {
SDL_SetError("Haptic autocenter must be between 0 and 100.");
return -1;
}
if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
return -1;
}
return 0;
}