SDL 2.0
SDL_syshaptic.c
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "../../SDL_internal.h"
22
23#ifdef SDL_HAPTIC_ANDROID
24
25#include "SDL_assert.h"
26#include "SDL_timer.h"
27#include "SDL_syshaptic_c.h"
28#include "../SDL_syshaptic.h"
29#include "SDL_haptic.h"
30#include "../../core/android/SDL_android.h"
31#include "SDL_joystick.h"
32#include "../../joystick/SDL_sysjoystick.h" /* For the real SDL_Joystick */
33#include "../../joystick/android/SDL_sysjoystick_c.h" /* For joystick hwdata */
34
35
36typedef struct SDL_hapticlist_item
37{
38 int device_id;
39 char *name;
40 SDL_Haptic *haptic;
43
45static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
46static int numhaptics = 0;
47
48
49int
51{
52 /* Support for device connect/disconnect is API >= 16 only,
53 * so we poll every three seconds
54 * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
55 */
56 static Uint32 timeout = 0;
58 timeout = SDL_GetTicks() + 3000;
60 }
61 return (numhaptics);
62}
63
64int
66{
67 return (numhaptics);
68}
69
71HapticByOrder(int index)
72{
74 if ((index < 0) || (index >= numhaptics)) {
75 return NULL;
76 }
77 while (index > 0) {
78 SDL_assert(item != NULL);
79 --index;
80 item = item->next;
81 }
82 return item;
83}
84
86HapticByDevId (int device_id)
87{
89 for (item = SDL_hapticlist; item != NULL; item = item->next) {
90 if (device_id == item->device_id) {
91 /*SDL_Log("=+=+=+=+=+= HapticByDevId id [%d]", device_id);*/
92 return item;
93 }
94 }
95 return NULL;
96}
97
98const char *
100{
101 SDL_hapticlist_item *item = HapticByOrder(index);
102 if (item == NULL ) {
103 SDL_SetError("No such device");
104 return NULL;
105 }
106 return item->name;
107}
108
109
110static SDL_hapticlist_item *
111OpenHaptic(SDL_Haptic *haptic, SDL_hapticlist_item *item)
112{
113 if (item == NULL ) {
114 SDL_SetError("No such device");
115 return NULL;
116 }
117 if (item->haptic != NULL) {
118 SDL_SetError("Haptic already opened");
119 return NULL;
120 }
121
122 haptic->hwdata = (struct haptic_hwdata *)item;
123 item->haptic = haptic;
124
125 haptic->supported = SDL_HAPTIC_LEFTRIGHT;
126 haptic->neffects = 1;
127 haptic->nplaying = haptic->neffects;
128 haptic->effects = (struct haptic_effect *)SDL_malloc (sizeof (struct haptic_effect) * haptic->neffects);
129 if (haptic->effects == NULL) {
131 return NULL;
132 }
133 SDL_memset(haptic->effects, 0, sizeof (struct haptic_effect) * haptic->neffects);
134 return item;
135}
136
137static SDL_hapticlist_item *
138OpenHapticByOrder(SDL_Haptic *haptic, int index)
139{
140 return OpenHaptic (haptic, HapticByOrder(index));
141}
142
143static SDL_hapticlist_item *
144OpenHapticByDevId(SDL_Haptic *haptic, int device_id)
145{
146 return OpenHaptic (haptic, HapticByDevId(device_id));
147}
148
149int
150SDL_SYS_HapticOpen(SDL_Haptic *haptic)
151{
152 return (OpenHapticByOrder(haptic, haptic->index) == NULL ? -1 : 0);
153}
154
155
156int
158{
159 return 0;
160}
161
162
163int
164SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
165{
167 item = HapticByDevId(((joystick_hwdata *)joystick->hwdata)->device_id);
168 return (item != NULL) ? 1 : 0;
169}
170
171
172int
173SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
174{
175 return (OpenHapticByDevId(haptic, ((joystick_hwdata *)joystick->hwdata)->device_id) == NULL ? -1 : 0);
176}
177
178
179int
180SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
181{
182 return (((SDL_hapticlist_item *)haptic->hwdata)->device_id == ((joystick_hwdata *)joystick->hwdata)->device_id ? 1 : 0);
183}
184
185
186void
187SDL_SYS_HapticClose(SDL_Haptic * haptic)
188{
189 ((SDL_hapticlist_item *)haptic->hwdata)->haptic = NULL;
190 haptic->hwdata = NULL;
191 return;
192}
193
194
195void
197{
198/* We don't have any way to scan for joysticks (and their vibrators) at init, so don't wipe the list
199 * of joysticks here in case this is a reinit.
200 */
201#if 0
204
205 for (item = SDL_hapticlist; item; item = next) {
206 next = item->next;
207 SDL_free(item);
208 }
209
210 SDL_hapticlist = SDL_hapticlist_tail = NULL;
211 numhaptics = 0;
212 return;
213#endif
214}
215
216
217int
219 struct haptic_effect *effect, SDL_HapticEffect * base)
220{
221 return 0;
222}
223
224
225int
227 struct haptic_effect *effect,
229{
230 return 0;
231}
232
233
234int
237{
238 float large = effect->effect.leftright.large_magnitude / 32767.0f;
239 float small = effect->effect.leftright.small_magnitude / 32767.0f;
240
241 float total = (large * 0.6f) + (small * 0.4f);
242
243 Android_JNI_HapticRun (((SDL_hapticlist_item *)haptic->hwdata)->device_id, total, effect->effect.leftright.length);
244 return 0;
245}
246
247
248int
250{
251 Android_JNI_HapticStop (((SDL_hapticlist_item *)haptic->hwdata)->device_id);
252 return 0;
253}
254
255
256void
258{
259 return;
260}
261
262
263int
265 struct haptic_effect *effect)
266{
267 return 0;
268}
269
270
271int
272SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
273{
274 return 0;
275}
276
277
278int
279SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
280{
281 return 0;
282}
283
284int
285SDL_SYS_HapticPause(SDL_Haptic * haptic)
286{
287 return 0;
288}
289
290int
291SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
292{
293 return 0;
294}
295
296int
297SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
298{
299 return 0;
300}
301
302
303
304int
305Android_AddHaptic(int device_id, const char *name)
306{
308 item = (SDL_hapticlist_item *) SDL_calloc(1, sizeof (SDL_hapticlist_item));
309 if (item == NULL) {
310 return -1;
311 }
312
313 item->device_id = device_id;
314 item->name = SDL_strdup (name);
315 if (item->name == NULL) {
316 SDL_free (item);
317 return -1;
318 }
319
320 if (SDL_hapticlist_tail == NULL) {
321 SDL_hapticlist = SDL_hapticlist_tail = item;
322 } else {
323 SDL_hapticlist_tail->next = item;
324 SDL_hapticlist_tail = item;
325 }
326
327 ++numhaptics;
328 return numhaptics;
329}
330
331int
332Android_RemoveHaptic(int device_id)
333{
336
337 for (item = SDL_hapticlist; item != NULL; item = item->next) {
338 /* found it, remove it. */
339 if (device_id == item->device_id) {
340 const int retval = item->haptic ? item->haptic->index : -1;
341
342 if (prev != NULL) {
343 prev->next = item->next;
344 } else {
345 SDL_assert(SDL_hapticlist == item);
346 SDL_hapticlist = item->next;
347 }
348 if (item == SDL_hapticlist_tail) {
349 SDL_hapticlist_tail = prev;
350 }
351
352 /* Need to decrement the haptic count */
353 --numhaptics;
354 /* !!! TODO: Send a haptic remove event? */
355
356 SDL_free(item->name);
357 SDL_free(item);
358 return retval;
359 }
360 prev = item;
361 }
362 return -1;
363}
364
365
366#endif /* SDL_HAPTIC_ANDROID */
367
368/* vi: set ts=4 sw=4 expandtab: */
void Android_JNI_HapticRun(int device_id, float intensity, int length)
void Android_JNI_PollHapticDevices(void)
void Android_JNI_HapticStop(int device_id)
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define SDL_SetError
#define SDL_memset
#define SDL_malloc
#define SDL_free
#define SDL_strdup
#define SDL_calloc
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
The SDL haptic subsystem allows you to control haptic (force feedback) devices.
#define SDL_HAPTIC_LEFTRIGHT
Left/Right effect supported.
Definition: SDL_haptic.h:183
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
GLuint index
GLbitfield GLuint64 timeout
GLuint const GLchar * name
uint32_t Uint32
Definition: SDL_stdinc.h:203
void SDL_SYS_HapticClose(SDL_Haptic *haptic)
int SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
int SDL_SYS_HapticPause(SDL_Haptic *haptic)
int SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain)
void SDL_SYS_HapticQuit(void)
int SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
int SDL_SYS_HapticMouse(void)
int SDL_SYS_HapticUnpause(SDL_Haptic *haptic)
int SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *data)
int SDL_SYS_HapticStopAll(SDL_Haptic *haptic)
int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *base)
int SDL_SYS_NumHaptics(void)
const char * SDL_SYS_HapticName(int index)
void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
int SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, Uint32 iterations)
int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
int SDL_SYS_HapticOpen(SDL_Haptic *haptic)
int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect)
int SDL_SYS_HapticInit(void)
int SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
#define SDL_TICKS_PASSED(A, B)
Compare SDL ticks values, and return true if A has passed B.
Definition: SDL_timer.h:56
SDL_hapticlist_item * SDL_hapticlist
#define NULL
Definition: begin_code.h:167
struct SDL_hapticlist_item * next
SDL_HapticEffect effect
Definition: SDL_syshaptic.h:32
SDL_bool retval
static SDL_Haptic * haptic
Definition: testhaptic.c:25
static int iterations
Definition: testsprite2.c:45
The generic template for any haptic effect.
Definition: SDL_haptic.h:801
SDL_HapticLeftRight leftright
Definition: SDL_haptic.h:808