Atlas - SDL_syshaptic.c

Home / ext / SDL / src / haptic / android Lines: 1 | Size: 7639 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 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_syshaptic_c.h" 26#include "../SDL_syshaptic.h" 27#include "../../core/android/SDL_android.h" 28 29typedef struct SDL_hapticlist_item 30{ 31 SDL_HapticID instance_id; 32 int device_id; 33 char *name; 34 SDL_Haptic *haptic; 35 struct SDL_hapticlist_item *next; 36} SDL_hapticlist_item; 37 38static SDL_hapticlist_item *SDL_hapticlist = NULL; 39static SDL_hapticlist_item *SDL_hapticlist_tail = NULL; 40static int numhaptics = 0; 41 42bool SDL_SYS_HapticInit(void) 43{ 44 Android_JNI_PollHapticDevices(); 45 46 return true; 47} 48 49int SDL_SYS_NumHaptics(void) 50{ 51 return numhaptics; 52} 53 54static SDL_hapticlist_item *HapticByOrder(int index) 55{ 56 SDL_hapticlist_item *item = SDL_hapticlist; 57 if ((index < 0) || (index >= numhaptics)) { 58 return NULL; 59 } 60 while (index > 0) { 61 SDL_assert(item != NULL); 62 --index; 63 item = item->next; 64 } 65 return item; 66} 67 68static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id) 69{ 70 SDL_hapticlist_item *item; 71 for (item = SDL_hapticlist; item; item = item->next) { 72 if (instance_id == item->instance_id) { 73 return item; 74 } 75 } 76 return NULL; 77} 78 79SDL_HapticID SDL_SYS_HapticInstanceID(int index) 80{ 81 SDL_hapticlist_item *item = HapticByOrder(index); 82 if (item) { 83 return item->instance_id; 84 } 85 return 0; 86} 87 88const char *SDL_SYS_HapticName(int index) 89{ 90 SDL_hapticlist_item *item = HapticByOrder(index); 91 if (!item) { 92 SDL_SetError("No such device"); 93 return NULL; 94 } 95 return item->name; 96} 97 98static SDL_hapticlist_item *OpenHaptic(SDL_Haptic *haptic, SDL_hapticlist_item *item) 99{ 100 if (!item) { 101 SDL_SetError("No such device"); 102 return NULL; 103 } 104 if (item->haptic) { 105 SDL_SetError("Haptic already opened"); 106 return NULL; 107 } 108 109 haptic->hwdata = (struct haptic_hwdata *)item; 110 item->haptic = haptic; 111 112 haptic->instance_id = item->instance_id; 113 if (item->name) { 114 haptic->name = SDL_strdup(item->name); 115 } 116 haptic->supported = SDL_HAPTIC_LEFTRIGHT; 117 haptic->neffects = 1; 118 haptic->nplaying = haptic->neffects; 119 haptic->effects = (struct haptic_effect *)SDL_calloc(haptic->neffects, sizeof(struct haptic_effect)); 120 if (!haptic->effects) { 121 return NULL; 122 } 123 return item; 124} 125 126static SDL_hapticlist_item *OpenHapticByInstanceID(SDL_Haptic *haptic, SDL_HapticID instance_id) 127{ 128 return OpenHaptic(haptic, HapticByInstanceID(instance_id)); 129} 130 131bool SDL_SYS_HapticOpen(SDL_Haptic *haptic) 132{ 133 return OpenHapticByInstanceID(haptic, haptic->instance_id) != NULL; 134} 135 136int SDL_SYS_HapticMouse(void) 137{ 138 return -1; 139} 140 141bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick) 142{ 143 return false; 144} 145 146bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick) 147{ 148 return SDL_Unsupported(); 149} 150 151bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick) 152{ 153 return false; 154} 155 156void SDL_SYS_HapticClose(SDL_Haptic *haptic) 157{ 158 ((SDL_hapticlist_item *)haptic->hwdata)->haptic = NULL; 159 haptic->hwdata = NULL; 160} 161 162void SDL_SYS_HapticQuit(void) 163{ 164/* We don't have any way to scan for joysticks (and their vibrators) at init, so don't wipe the list 165 * of joysticks here in case this is a reinit. 166 */ 167#if 0 168 SDL_hapticlist_item *item = NULL; 169 SDL_hapticlist_item *next = NULL; 170 171 for (item = SDL_hapticlist; item; item = next) { 172 next = item->next; 173 SDL_free(item); 174 } 175 176 SDL_hapticlist = SDL_hapticlist_tail = NULL; 177 numhaptics = 0; 178 return; 179#endif 180} 181 182bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, 183 struct haptic_effect *effect, const SDL_HapticEffect *base) 184{ 185 return true; 186} 187 188bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, 189 struct haptic_effect *effect, 190 const SDL_HapticEffect *data) 191{ 192 return true; 193} 194 195bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, 196 Uint32 iterations) 197{ 198 float large = effect->effect.leftright.large_magnitude / 32767.0f; 199 float small = effect->effect.leftright.small_magnitude / 32767.0f; 200 201 float total = (large * 0.6f) + (small * 0.4f); 202 203 Android_JNI_HapticRun(((SDL_hapticlist_item *)haptic->hwdata)->device_id, total, effect->effect.leftright.length); 204 return true; 205} 206 207bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect) 208{ 209 Android_JNI_HapticStop(((SDL_hapticlist_item *)haptic->hwdata)->device_id); 210 return true; 211} 212 213void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect) 214{ 215} 216 217int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect) 218{ 219 return 0; 220} 221 222bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain) 223{ 224 return true; 225} 226 227bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter) 228{ 229 return true; 230} 231 232bool SDL_SYS_HapticPause(SDL_Haptic *haptic) 233{ 234 return true; 235} 236 237bool SDL_SYS_HapticResume(SDL_Haptic *haptic) 238{ 239 return true; 240} 241 242bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic) 243{ 244 return true; 245} 246 247bool Android_AddHaptic(int device_id, const char *name) 248{ 249 SDL_hapticlist_item *item; 250 item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item)); 251 if (!item) { 252 return false; 253 } 254 255 item->instance_id = SDL_GetNextObjectID(); 256 item->device_id = device_id; 257 item->name = SDL_strdup(name); 258 if (!item->name) { 259 SDL_free(item); 260 return false; 261 } 262 263 if (!SDL_hapticlist_tail) { 264 SDL_hapticlist = SDL_hapticlist_tail = item; 265 } else { 266 SDL_hapticlist_tail->next = item; 267 SDL_hapticlist_tail = item; 268 } 269 270 ++numhaptics; 271 return true; 272} 273 274bool Android_RemoveHaptic(int device_id) 275{ 276 SDL_hapticlist_item *item; 277 SDL_hapticlist_item *prev = NULL; 278 279 for (item = SDL_hapticlist; item; item = item->next) { 280 // found it, remove it. 281 if (device_id == item->device_id) { 282 const bool result = item->haptic ? true : false; 283 284 if (prev) { 285 prev->next = item->next; 286 } else { 287 SDL_assert(SDL_hapticlist == item); 288 SDL_hapticlist = item->next; 289 } 290 if (item == SDL_hapticlist_tail) { 291 SDL_hapticlist_tail = prev; 292 } 293 294 // Need to decrement the haptic count 295 --numhaptics; 296 // !!! TODO: Send a haptic remove event? 297 298 SDL_free(item->name); 299 SDL_free(item); 300 return result; 301 } 302 prev = item; 303 } 304 return false; 305} 306 307#endif // SDL_HAPTIC_ANDROID 308
[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.