21
21
#include "SDL_internal.h"
22
22
23
23
#include "SDL_syshaptic.h"
24
+ #ifdef SDL_JOYSTICK_HIDAPI
25
+ #include "SDL_hidapihaptic.h"
26
+ #endif //SDL_JOYSTICK_HIDAPI
24
27
#include "SDL_haptic_c.h"
25
28
#include "../joystick/SDL_joystick_c.h" // For SDL_IsJoystickValid
26
29
@@ -34,7 +37,17 @@ static SDL_Haptic *SDL_haptics = NULL;
34
37
35
38
bool SDL_InitHaptics (void )
36
39
{
37
- return SDL_SYS_HapticInit ();
40
+ if (!SDL_SYS_HapticInit ()) {
41
+ return false;
42
+ }
43
+ #ifdef SDL_JOYSTICK_HIDAPI
44
+ if (!SDL_HIDAPI_HapticInit ()) {
45
+ SDL_SYS_HapticQuit ();
46
+ return false;
47
+ }
48
+ #endif //SDL_JOYSTICK_HIDAPI
49
+
50
+ return true;
38
51
}
39
52
40
53
static bool SDL_GetHapticIndex (SDL_HapticID instance_id , int * driver_index )
@@ -127,7 +140,6 @@ SDL_Haptic *SDL_OpenHaptic(SDL_HapticID instance_id)
127
140
}
128
141
129
142
// Initialize the haptic device
130
- SDL_SetObjectValid (haptic , SDL_OBJECT_TYPE_HAPTIC , true);
131
143
haptic -> instance_id = instance_id ;
132
144
haptic -> rumble_id = -1 ;
133
145
if (!SDL_SYS_HapticOpen (haptic )) {
@@ -156,6 +168,7 @@ SDL_Haptic *SDL_OpenHaptic(SDL_HapticID instance_id)
156
168
SDL_SetHapticAutocenter (haptic , 0 );
157
169
}
158
170
171
+ SDL_SetObjectValid (haptic , SDL_OBJECT_TYPE_HAPTIC , true);
159
172
return haptic ;
160
173
}
161
174
@@ -216,7 +229,11 @@ bool SDL_IsJoystickHaptic(SDL_Joystick *joystick)
216
229
// Must be a valid joystick
217
230
if (SDL_IsJoystickValid (joystick ) &&
218
231
!SDL_IsGamepad (SDL_GetJoystickID (joystick ))) {
232
+ #ifdef SDL_JOYSTICK_HIDAPI
233
+ result = SDL_SYS_JoystickIsHaptic (joystick ) || SDL_HIDAPI_JoystickIsHaptic (joystick );
234
+ #else //SDL_JOYSTICK_HIDAPI
219
235
result = SDL_SYS_JoystickIsHaptic (joystick );
236
+ #endif //SDL_JOYSTICK_HIDAPI
220
237
}
221
238
}
222
239
SDL_UnlockJoysticks ();
@@ -231,16 +248,8 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
231
248
232
249
SDL_LockJoysticks ();
233
250
{
234
- // Must be a valid joystick
235
- if (!SDL_IsJoystickValid (joystick )) {
236
- SDL_SetError ("Haptic: Joystick isn't valid." );
237
- SDL_UnlockJoysticks ();
238
- return NULL ;
239
- }
240
-
241
- // Joystick must be haptic
242
- if (SDL_IsGamepad (SDL_GetJoystickID (joystick )) ||
243
- !SDL_SYS_JoystickIsHaptic (joystick )) {
251
+ // Joystick must be valid and haptic
252
+ if (!SDL_IsJoystickHaptic (joystick )) {
244
253
SDL_SetError ("Haptic: Joystick isn't a haptic device." );
245
254
SDL_UnlockJoysticks ();
246
255
return NULL ;
@@ -249,7 +258,11 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
249
258
hapticlist = SDL_haptics ;
250
259
// Check to see if joystick's haptic is already open
251
260
while (hapticlist ) {
261
+ #ifdef SDL_JOYSTICK_HIDAPI
262
+ if (SDL_SYS_JoystickSameHaptic (hapticlist , joystick ) || SDL_HIDAPI_JoystickSameHaptic (hapticlist , joystick )) {
263
+ #else
252
264
if (SDL_SYS_JoystickSameHaptic (hapticlist , joystick )) {
265
+ #endif //SDL_JOYSTICK_HIDAPI
253
266
haptic = hapticlist ;
254
267
++ haptic -> ref_count ;
255
268
SDL_UnlockJoysticks ();
@@ -269,6 +282,16 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
269
282
* This function should fill in the instance ID and name.
270
283
*/
271
284
haptic -> rumble_id = -1 ;
285
+ #ifdef SDL_JOYSTICK_HIDAPI
286
+ if (SDL_HIDAPI_JoystickIsHaptic (joystick )) {
287
+ if (!SDL_HIDAPI_HapticOpenFromJoystick (haptic , joystick )) {
288
+ SDL_SetError ("Haptic: SDL_HIDAPI_HapticOpenFromJoystick failed." );
289
+ SDL_free (haptic );
290
+ SDL_UnlockJoysticks ();
291
+ return NULL ;
292
+ }
293
+ } else
294
+ #endif //SDL_JOYSTICK_HIDAPI
272
295
if (!SDL_SYS_HapticOpenFromJoystick (haptic , joystick )) {
273
296
SDL_SetError ("Haptic: SDL_SYS_HapticOpenFromJoystick failed." );
274
297
SDL_free (haptic );
@@ -285,6 +308,7 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
285
308
haptic -> next = SDL_haptics ;
286
309
SDL_haptics = haptic ;
287
310
311
+ SDL_SetObjectValid (haptic , SDL_OBJECT_TYPE_HAPTIC , true);
288
312
return haptic ;
289
313
}
290
314
@@ -301,13 +325,20 @@ void SDL_CloseHaptic(SDL_Haptic *haptic)
301
325
return ;
302
326
}
303
327
304
- // Close it, properly removing effects if needed
305
- for (i = 0 ; i < haptic -> neffects ; i ++ ) {
306
- if (haptic -> effects [i ].hweffect != NULL ) {
307
- SDL_DestroyHapticEffect (haptic , i );
328
+ #ifdef SDL_JOYSTICK_HIDAPI
329
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
330
+ SDL_HIDAPI_HapticClose (haptic );
331
+ } else
332
+ #endif //SDL_JOYSTICK_HIDAPI
333
+ {
334
+ // Close it, properly removing effects if needed
335
+ for (i = 0 ; i < haptic -> neffects ; i ++ ) {
336
+ if (haptic -> effects [i ].hweffect != NULL ) {
337
+ SDL_DestroyHapticEffect (haptic , i );
338
+ }
308
339
}
340
+ SDL_SYS_HapticClose (haptic );
309
341
}
310
- SDL_SYS_HapticClose (haptic );
311
342
SDL_SetObjectValid (haptic , SDL_OBJECT_TYPE_HAPTIC , false);
312
343
313
344
// Remove from the list
@@ -339,6 +370,7 @@ void SDL_QuitHaptics(void)
339
370
SDL_CloseHaptic (SDL_haptics );
340
371
}
341
372
373
+ SDL_HIDAPI_HapticQuit ();
342
374
SDL_SYS_HapticQuit ();
343
375
}
344
376
@@ -401,6 +433,12 @@ int SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect)
401
433
return -1 ;
402
434
}
403
435
436
+ #ifdef SDL_JOYSTICK_HIDAPI
437
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
438
+ return SDL_HIDAPI_HapticNewEffect (haptic , effect );
439
+ }
440
+ #endif //SDL_JOYSTICK_HIDAPI
441
+
404
442
// See if there's a free slot
405
443
for (i = 0 ; i < haptic -> neffects ; i ++ ) {
406
444
if (haptic -> effects [i ].hweffect == NULL ) {
@@ -433,6 +471,12 @@ bool SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, const SDL_HapticEffe
433
471
{
434
472
CHECK_HAPTIC_MAGIC (haptic , false);
435
473
474
+ #ifdef SDL_JOYSTICK_HIDAPI
475
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
476
+ return SDL_HIDAPI_HapticUpdateEffect (haptic , effect , data );
477
+ }
478
+ #endif //SDL_JOYSTICK_HIDAPI
479
+
436
480
if (!ValidEffect (haptic , effect )) {
437
481
return false;
438
482
}
@@ -460,6 +504,12 @@ bool SDL_RunHapticEffect(SDL_Haptic *haptic, int effect, Uint32 iterations)
460
504
{
461
505
CHECK_HAPTIC_MAGIC (haptic , false);
462
506
507
+ #ifdef SDL_JOYSTICK_HIDAPI
508
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
509
+ return SDL_HIDAPI_HapticRunEffect (haptic , effect , iterations );
510
+ }
511
+ #endif //SDL_JOYSTICK_HIDAPI
512
+
463
513
if (!ValidEffect (haptic , effect )) {
464
514
return false;
465
515
}
@@ -476,6 +526,12 @@ bool SDL_StopHapticEffect(SDL_Haptic *haptic, int effect)
476
526
{
477
527
CHECK_HAPTIC_MAGIC (haptic , false);
478
528
529
+ #ifdef SDL_JOYSTICK_HIDAPI
530
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
531
+ return SDL_HIDAPI_HapticStopEffect (haptic , effect );
532
+ }
533
+ #endif //SDL_JOYSTICK_HIDAPI
534
+
479
535
if (!ValidEffect (haptic , effect )) {
480
536
return false;
481
537
}
@@ -492,6 +548,12 @@ void SDL_DestroyHapticEffect(SDL_Haptic *haptic, int effect)
492
548
{
493
549
CHECK_HAPTIC_MAGIC (haptic ,);
494
550
551
+ #ifdef SDL_JOYSTICK_HIDAPI
552
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
553
+ return SDL_HIDAPI_HapticDestroyEffect (haptic , effect );
554
+ }
555
+ #endif //SDL_JOYSTICK_HIDAPI
556
+
495
557
if (!ValidEffect (haptic , effect )) {
496
558
return ;
497
559
}
@@ -508,6 +570,12 @@ bool SDL_GetHapticEffectStatus(SDL_Haptic *haptic, int effect)
508
570
{
509
571
CHECK_HAPTIC_MAGIC (haptic , false);
510
572
573
+ #ifdef SDL_JOYSTICK_HIDAPI
574
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
575
+ return SDL_HIDAPI_HapticGetEffectStatus (haptic , effect );
576
+ }
577
+ #endif //SDL_JOYSTICK_HIDAPI
578
+
511
579
if (!ValidEffect (haptic , effect )) {
512
580
return false;
513
581
}
@@ -554,6 +622,12 @@ bool SDL_SetHapticGain(SDL_Haptic *haptic, int gain)
554
622
real_gain = gain ;
555
623
}
556
624
625
+ #ifdef SDL_JOYSTICK_HIDAPI
626
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
627
+ return SDL_HIDAPI_HapticSetGain (haptic , real_gain );
628
+ }
629
+ #endif //SDL_JOYSTICK_HIDAPI
630
+
557
631
return SDL_SYS_HapticSetGain (haptic , real_gain );
558
632
}
559
633
@@ -569,6 +643,12 @@ bool SDL_SetHapticAutocenter(SDL_Haptic *haptic, int autocenter)
569
643
return SDL_SetError ("Haptic: Autocenter must be between 0 and 100." );
570
644
}
571
645
646
+ #ifdef SDL_JOYSTICK_HIDAPI
647
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
648
+ return SDL_HIDAPI_HapticSetAutocenter (haptic , autocenter );
649
+ }
650
+ #endif //SDL_JOYSTICK_HIDAPI
651
+
572
652
return SDL_SYS_HapticSetAutocenter (haptic , autocenter );
573
653
}
574
654
@@ -580,6 +660,12 @@ bool SDL_PauseHaptic(SDL_Haptic *haptic)
580
660
return SDL_SetError ("Haptic: Device does not support setting pausing." );
581
661
}
582
662
663
+ #ifdef SDL_JOYSTICK_HIDAPI
664
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
665
+ return SDL_HIDAPI_HapticPause (haptic );
666
+ }
667
+ #endif //SDL_JOYSTICK_HIDAPI
668
+
583
669
return SDL_SYS_HapticPause (haptic );
584
670
}
585
671
@@ -591,13 +677,25 @@ bool SDL_ResumeHaptic(SDL_Haptic *haptic)
591
677
return true; // Not going to be paused, so we pretend it's unpaused.
592
678
}
593
679
680
+ #ifdef SDL_JOYSTICK_HIDAPI
681
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
682
+ return SDL_HIDAPI_HapticResume (haptic );
683
+ }
684
+ #endif //SDL_JOYSTICK_HIDAPI
685
+
594
686
return SDL_SYS_HapticResume (haptic );
595
687
}
596
688
597
689
bool SDL_StopHapticEffects (SDL_Haptic * haptic )
598
690
{
599
691
CHECK_HAPTIC_MAGIC (haptic , false);
600
692
693
+ #ifdef SDL_JOYSTICK_HIDAPI
694
+ if (SDL_HIDAPI_HapticIsHidapi (haptic )) {
695
+ return SDL_HIDAPI_HapticStopAll (haptic );
696
+ }
697
+ #endif //SDL_JOYSTICK_HIDAPI
698
+
601
699
return SDL_SYS_HapticStopAll (haptic );
602
700
}
603
701
0 commit comments