Atlas - SDL_syshaptic.c
Home / ext / SDL2 / src / haptic / android Lines: 1 | Size: 8286 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2018 Sam Lantinga <[email protected]> 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; 41 struct SDL_hapticlist_item *next; 42} SDL_hapticlist_item; 43 44static SDL_hapticlist_item *SDL_hapticlist = NULL; 45static SDL_hapticlist_item *SDL_hapticlist_tail = NULL; 46static int numhaptics = 0; 47 48 49int 50SDL_SYS_HapticInit(void) 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; 57 if (SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) { 58 timeout = SDL_GetTicks() + 3000; 59 Android_JNI_PollHapticDevices(); 60 } 61 return (numhaptics); 62} 63 64int 65SDL_SYS_NumHaptics(void) 66{ 67 return (numhaptics); 68} 69 70static SDL_hapticlist_item * 71HapticByOrder(int index) 72{ 73 SDL_hapticlist_item *item = SDL_hapticlist; 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 85static SDL_hapticlist_item * 86HapticByDevId (int device_id) 87{ 88 SDL_hapticlist_item *item; 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 * 99SDL_SYS_HapticName(int index) 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) { 130 SDL_OutOfMemory(); 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 157SDL_SYS_HapticMouse(void) 158{ 159 return 0; 160} 161 162 163int 164SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick) 165{ 166 SDL_hapticlist_item *item; 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 196SDL_SYS_HapticQuit(void) 197{ 198 SDL_hapticlist_item *item = NULL; 199 SDL_hapticlist_item *next = NULL; 200 201 for (item = SDL_hapticlist; item; item = next) { 202 next = item->next; 203 SDL_free(item); 204 } 205 206 SDL_hapticlist = SDL_hapticlist_tail = NULL; 207 numhaptics = 0; 208 return; 209} 210 211 212int 213SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, 214 struct haptic_effect *effect, SDL_HapticEffect * base) 215{ 216 return 0; 217} 218 219 220int 221SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic, 222 struct haptic_effect *effect, 223 SDL_HapticEffect * data) 224{ 225 return 0; 226} 227 228 229int 230SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, 231 Uint32 iterations) 232{ 233 Android_JNI_HapticRun (((SDL_hapticlist_item *)haptic->hwdata)->device_id, effect->effect.leftright.length); 234 return 0; 235} 236 237 238int 239SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect) 240{ 241 Android_JNI_HapticStop (((SDL_hapticlist_item *)haptic->hwdata)->device_id); 242 return 0; 243} 244 245 246void 247SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect) 248{ 249 return; 250} 251 252 253int 254SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic, 255 struct haptic_effect *effect) 256{ 257 return 0; 258} 259 260 261int 262SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain) 263{ 264 return 0; 265} 266 267 268int 269SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter) 270{ 271 return 0; 272} 273 274int 275SDL_SYS_HapticPause(SDL_Haptic * haptic) 276{ 277 return 0; 278} 279 280int 281SDL_SYS_HapticUnpause(SDL_Haptic * haptic) 282{ 283 return 0; 284} 285 286int 287SDL_SYS_HapticStopAll(SDL_Haptic * haptic) 288{ 289 return 0; 290} 291 292 293 294int 295Android_AddHaptic(int device_id, const char *name) 296{ 297 SDL_hapticlist_item *item; 298 item = (SDL_hapticlist_item *) SDL_calloc(1, sizeof (SDL_hapticlist_item)); 299 if (item == NULL) { 300 return -1; 301 } 302 303 item->device_id = device_id; 304 item->name = SDL_strdup (name); 305 if (item->name == NULL) { 306 SDL_free (item); 307 return -1; 308 } 309 310 if (SDL_hapticlist_tail == NULL) { 311 SDL_hapticlist = SDL_hapticlist_tail = item; 312 } else { 313 SDL_hapticlist_tail->next = item; 314 SDL_hapticlist_tail = item; 315 } 316 317 ++numhaptics; 318 return numhaptics; 319} 320 321int 322Android_RemoveHaptic(int device_id) 323{ 324 SDL_hapticlist_item *item; 325 SDL_hapticlist_item *prev = NULL; 326 327 for (item = SDL_hapticlist; item != NULL; item = item->next) { 328 /* found it, remove it. */ 329 if (device_id == item->device_id) { 330 const int retval = item->haptic ? item->haptic->index : -1; 331 332 if (prev != NULL) { 333 prev->next = item->next; 334 } else { 335 SDL_assert(SDL_hapticlist == item); 336 SDL_hapticlist = item->next; 337 } 338 if (item == SDL_hapticlist_tail) { 339 SDL_hapticlist_tail = prev; 340 } 341 342 /* Need to decrement the haptic count */ 343 --numhaptics; 344 /* !!! TODO: Send a haptic remove event? */ 345 346 SDL_free(item->name); 347 SDL_free(item); 348 return retval; 349 } 350 prev = item; 351 } 352 return -1; 353} 354 355 356#endif /* SDL_HAPTIC_ANDROID */ 357 358/* vi: set ts=4 sw=4 expandtab: */ 359[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.