SDL_syshaptic.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_HAPTIC_IOKIT
  20. #include "../SDL_syshaptic.h"
  21. #include "../../joystick/SDL_sysjoystick.h" // For the real SDL_Joystick
  22. #include "../../joystick/darwin/SDL_iokitjoystick_c.h" // For joystick hwdata
  23. #include "SDL_syshaptic_c.h"
  24. #include <IOKit/IOKitLib.h>
  25. #include <IOKit/hid/IOHIDKeys.h>
  26. #include <IOKit/hid/IOHIDUsageTables.h>
  27. #include <ForceFeedback/ForceFeedback.h>
  28. #include <ForceFeedback/ForceFeedbackConstants.h>
  29. #ifndef IO_OBJECT_NULL
  30. #define IO_OBJECT_NULL ((io_service_t)0)
  31. #endif
  32. /*
  33. * List of available haptic devices.
  34. */
  35. typedef struct SDL_hapticlist_item
  36. {
  37. SDL_HapticID instance_id;
  38. char name[256]; // Name of the device.
  39. io_service_t dev; // Node we use to create the device.
  40. SDL_Haptic *haptic; // Haptic currently associated with it.
  41. // Usage pages for determining if it's a mouse or not.
  42. long usage;
  43. long usagePage;
  44. struct SDL_hapticlist_item *next;
  45. } SDL_hapticlist_item;
  46. /*
  47. * Haptic system hardware data.
  48. */
  49. struct haptic_hwdata
  50. {
  51. FFDeviceObjectReference device; // Hardware device.
  52. UInt8 axes[3];
  53. };
  54. /*
  55. * Haptic system effect data.
  56. */
  57. struct haptic_hweffect
  58. {
  59. FFEffectObjectReference ref; // Reference.
  60. struct FFEFFECT effect; // Hardware effect.
  61. };
  62. /*
  63. * Prototypes.
  64. */
  65. static void SDL_SYS_HapticFreeFFEFFECT(FFEFFECT *effect, int type);
  66. static bool HIDGetDeviceProduct(io_service_t dev, char *name);
  67. static SDL_hapticlist_item *SDL_hapticlist = NULL;
  68. static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
  69. static int numhaptics = -1;
  70. /*
  71. * Like strerror but for force feedback errors.
  72. */
  73. static const char *FFStrError(unsigned int err)
  74. {
  75. switch (err) {
  76. case FFERR_DEVICEFULL:
  77. return "device full";
  78. // This should be valid, but for some reason isn't defined...
  79. /* case FFERR_DEVICENOTREG:
  80. return "device not registered"; */
  81. case FFERR_DEVICEPAUSED:
  82. return "device paused";
  83. case FFERR_DEVICERELEASED:
  84. return "device released";
  85. case FFERR_EFFECTPLAYING:
  86. return "effect playing";
  87. case FFERR_EFFECTTYPEMISMATCH:
  88. return "effect type mismatch";
  89. case FFERR_EFFECTTYPENOTSUPPORTED:
  90. return "effect type not supported";
  91. case FFERR_GENERIC:
  92. return "undetermined error";
  93. case FFERR_HASEFFECTS:
  94. return "device has effects";
  95. case FFERR_INCOMPLETEEFFECT:
  96. return "incomplete effect";
  97. case FFERR_INTERNAL:
  98. return "internal fault";
  99. case FFERR_INVALIDDOWNLOADID:
  100. return "invalid download id";
  101. case FFERR_INVALIDPARAM:
  102. return "invalid parameter";
  103. case FFERR_MOREDATA:
  104. return "more data";
  105. case FFERR_NOINTERFACE:
  106. return "interface not supported";
  107. case FFERR_NOTDOWNLOADED:
  108. return "effect is not downloaded";
  109. case FFERR_NOTINITIALIZED:
  110. return "object has not been initialized";
  111. case FFERR_OUTOFMEMORY:
  112. return "out of memory";
  113. case FFERR_UNPLUGGED:
  114. return "device is unplugged";
  115. case FFERR_UNSUPPORTED:
  116. return "function call unsupported";
  117. case FFERR_UNSUPPORTEDAXIS:
  118. return "axis unsupported";
  119. default:
  120. return "unknown error";
  121. }
  122. }
  123. /*
  124. * Initializes the haptic subsystem.
  125. */
  126. bool SDL_SYS_HapticInit(void)
  127. {
  128. IOReturn result;
  129. io_iterator_t iter;
  130. CFDictionaryRef match;
  131. io_service_t device;
  132. if (numhaptics != -1) {
  133. return SDL_SetError("Haptic subsystem already initialized!");
  134. }
  135. numhaptics = 0;
  136. // Get HID devices.
  137. match = IOServiceMatching(kIOHIDDeviceKey);
  138. if (!match) {
  139. return SDL_SetError("Haptic: Failed to get IOServiceMatching.");
  140. }
  141. // Now search I/O Registry for matching devices.
  142. result = IOServiceGetMatchingServices(kIOMainPortDefault, match, &iter);
  143. if (result != kIOReturnSuccess) {
  144. return SDL_SetError("Haptic: Couldn't create a HID object iterator.");
  145. }
  146. // IOServiceGetMatchingServices consumes dictionary.
  147. if (!IOIteratorIsValid(iter)) { // No iterator.
  148. return true;
  149. }
  150. while ((device = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
  151. MacHaptic_MaybeAddDevice(device);
  152. // always release as the AddDevice will retain IF it's a forcefeedback device
  153. IOObjectRelease(device);
  154. }
  155. IOObjectRelease(iter);
  156. return true;
  157. }
  158. int SDL_SYS_NumHaptics(void)
  159. {
  160. return numhaptics;
  161. }
  162. static SDL_hapticlist_item *HapticByDevIndex(int device_index)
  163. {
  164. SDL_hapticlist_item *item = SDL_hapticlist;
  165. if ((device_index < 0) || (device_index >= numhaptics)) {
  166. return NULL;
  167. }
  168. while (device_index > 0) {
  169. SDL_assert(item != NULL);
  170. --device_index;
  171. item = item->next;
  172. }
  173. return item;
  174. }
  175. static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id)
  176. {
  177. SDL_hapticlist_item *item;
  178. for (item = SDL_hapticlist; item; item = item->next) {
  179. if (instance_id == item->instance_id) {
  180. return item;
  181. }
  182. }
  183. return NULL;
  184. }
  185. bool MacHaptic_MaybeAddDevice(io_object_t device)
  186. {
  187. IOReturn result;
  188. CFMutableDictionaryRef hidProperties;
  189. CFTypeRef refCF;
  190. SDL_hapticlist_item *item;
  191. if (numhaptics == -1) {
  192. return false; // not initialized. We'll pick these up on enumeration if we init later.
  193. }
  194. // Check for force feedback.
  195. if (FFIsForceFeedback(device) != FF_OK) {
  196. return false;
  197. }
  198. // Make sure we don't already have it
  199. for (item = SDL_hapticlist; item; item = item->next) {
  200. if (IOObjectIsEqualTo((io_object_t)item->dev, device)) {
  201. // Already added
  202. return false;
  203. }
  204. }
  205. item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
  206. if (!item) {
  207. return SDL_SetError("Could not allocate haptic storage");
  208. }
  209. item->instance_id = SDL_GetNextObjectID();
  210. // retain it as we are going to keep it around a while
  211. IOObjectRetain(device);
  212. // Set basic device data.
  213. HIDGetDeviceProduct(device, item->name);
  214. item->dev = device;
  215. // Set usage pages.
  216. hidProperties = 0;
  217. refCF = 0;
  218. result = IORegistryEntryCreateCFProperties(device,
  219. &hidProperties,
  220. kCFAllocatorDefault,
  221. kNilOptions);
  222. if ((result == KERN_SUCCESS) && hidProperties) {
  223. refCF = CFDictionaryGetValue(hidProperties,
  224. CFSTR(kIOHIDPrimaryUsagePageKey));
  225. if (refCF) {
  226. if (!CFNumberGetValue(refCF, kCFNumberLongType, &item->usagePage)) {
  227. SDL_SetError("Haptic: Receiving device's usage page.");
  228. }
  229. refCF = CFDictionaryGetValue(hidProperties,
  230. CFSTR(kIOHIDPrimaryUsageKey));
  231. if (refCF) {
  232. if (!CFNumberGetValue(refCF, kCFNumberLongType, &item->usage)) {
  233. SDL_SetError("Haptic: Receiving device's usage.");
  234. }
  235. }
  236. }
  237. CFRelease(hidProperties);
  238. }
  239. if (!SDL_hapticlist_tail) {
  240. SDL_hapticlist = SDL_hapticlist_tail = item;
  241. } else {
  242. SDL_hapticlist_tail->next = item;
  243. SDL_hapticlist_tail = item;
  244. }
  245. // Device has been added.
  246. ++numhaptics;
  247. return true;
  248. }
  249. bool MacHaptic_MaybeRemoveDevice(io_object_t device)
  250. {
  251. SDL_hapticlist_item *item;
  252. SDL_hapticlist_item *prev = NULL;
  253. if (numhaptics == -1) {
  254. return false; // not initialized. ignore this.
  255. }
  256. for (item = SDL_hapticlist; item; item = item->next) {
  257. // found it, remove it.
  258. if (IOObjectIsEqualTo((io_object_t)item->dev, device)) {
  259. bool result = item->haptic ? true : false;
  260. if (prev) {
  261. prev->next = item->next;
  262. } else {
  263. SDL_assert(SDL_hapticlist == item);
  264. SDL_hapticlist = item->next;
  265. }
  266. if (item == SDL_hapticlist_tail) {
  267. SDL_hapticlist_tail = prev;
  268. }
  269. // Need to decrement the haptic count
  270. --numhaptics;
  271. // !!! TODO: Send a haptic remove event?
  272. IOObjectRelease(item->dev);
  273. SDL_free(item);
  274. return result;
  275. }
  276. prev = item;
  277. }
  278. return false;
  279. }
  280. SDL_HapticID SDL_SYS_HapticInstanceID(int index)
  281. {
  282. SDL_hapticlist_item *item;
  283. item = HapticByDevIndex(index);
  284. if (item) {
  285. return item->instance_id;
  286. }
  287. return 0;
  288. }
  289. /*
  290. * Return the name of a haptic device, does not need to be opened.
  291. */
  292. const char *SDL_SYS_HapticName(int index)
  293. {
  294. SDL_hapticlist_item *item;
  295. item = HapticByDevIndex(index);
  296. if (item) {
  297. return item->name;
  298. }
  299. return NULL;
  300. }
  301. /*
  302. * Gets the device's product name.
  303. */
  304. static bool HIDGetDeviceProduct(io_service_t dev, char *name)
  305. {
  306. CFMutableDictionaryRef hidProperties, usbProperties;
  307. io_registry_entry_t parent1, parent2;
  308. kern_return_t ret;
  309. hidProperties = usbProperties = 0;
  310. ret = IORegistryEntryCreateCFProperties(dev, &hidProperties,
  311. kCFAllocatorDefault, kNilOptions);
  312. if ((ret != KERN_SUCCESS) || !hidProperties) {
  313. return SDL_SetError("Haptic: Unable to create CFProperties.");
  314. }
  315. /* macOS currently is not mirroring all USB properties to HID page so need to look at USB device page also
  316. * get dictionary for USB properties: step up two levels and get CF dictionary for USB properties
  317. */
  318. if ((KERN_SUCCESS ==
  319. IORegistryEntryGetParentEntry(dev, kIOServicePlane, &parent1)) &&
  320. (KERN_SUCCESS ==
  321. IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2)) &&
  322. (KERN_SUCCESS ==
  323. IORegistryEntryCreateCFProperties(parent2, &usbProperties,
  324. kCFAllocatorDefault,
  325. kNilOptions))) {
  326. if (usbProperties) {
  327. CFTypeRef refCF = 0;
  328. /* get device info
  329. * try hid dictionary first, if fail then go to USB dictionary
  330. */
  331. // Get product name
  332. refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
  333. if (!refCF) {
  334. refCF = CFDictionaryGetValue(usbProperties,
  335. CFSTR("USB Product Name"));
  336. }
  337. if (refCF) {
  338. if (!CFStringGetCString(refCF, name, 256,
  339. CFStringGetSystemEncoding())) {
  340. return SDL_SetError("Haptic: CFStringGetCString error retrieving pDevice->product.");
  341. }
  342. }
  343. CFRelease(usbProperties);
  344. } else {
  345. return SDL_SetError("Haptic: IORegistryEntryCreateCFProperties failed to create usbProperties.");
  346. }
  347. // Release stuff.
  348. if (kIOReturnSuccess != IOObjectRelease(parent2)) {
  349. SDL_SetError("Haptic: IOObjectRelease error with parent2.");
  350. }
  351. if (kIOReturnSuccess != IOObjectRelease(parent1)) {
  352. SDL_SetError("Haptic: IOObjectRelease error with parent1.");
  353. }
  354. } else {
  355. return SDL_SetError("Haptic: Error getting registry entries.");
  356. }
  357. return true;
  358. }
  359. #define FF_TEST(ff, s) \
  360. if (features.supportedEffects & (ff)) \
  361. supported |= (s)
  362. /*
  363. * Gets supported features.
  364. */
  365. static bool GetSupportedFeatures(SDL_Haptic *haptic)
  366. {
  367. HRESULT ret;
  368. FFDeviceObjectReference device;
  369. FFCAPABILITIES features;
  370. unsigned int supported;
  371. Uint32 val;
  372. device = haptic->hwdata->device;
  373. ret = FFDeviceGetForceFeedbackCapabilities(device, &features);
  374. if (ret != FF_OK) {
  375. return SDL_SetError("Haptic: Unable to get device's supported features.");
  376. }
  377. supported = 0;
  378. // Get maximum effects.
  379. haptic->neffects = features.storageCapacity;
  380. haptic->nplaying = features.playbackCapacity;
  381. // Test for effects.
  382. FF_TEST(FFCAP_ET_CONSTANTFORCE, SDL_HAPTIC_CONSTANT);
  383. FF_TEST(FFCAP_ET_RAMPFORCE, SDL_HAPTIC_RAMP);
  384. FF_TEST(FFCAP_ET_SQUARE, SDL_HAPTIC_SQUARE);
  385. FF_TEST(FFCAP_ET_SINE, SDL_HAPTIC_SINE);
  386. FF_TEST(FFCAP_ET_TRIANGLE, SDL_HAPTIC_TRIANGLE);
  387. FF_TEST(FFCAP_ET_SAWTOOTHUP, SDL_HAPTIC_SAWTOOTHUP);
  388. FF_TEST(FFCAP_ET_SAWTOOTHDOWN, SDL_HAPTIC_SAWTOOTHDOWN);
  389. FF_TEST(FFCAP_ET_SPRING, SDL_HAPTIC_SPRING);
  390. FF_TEST(FFCAP_ET_DAMPER, SDL_HAPTIC_DAMPER);
  391. FF_TEST(FFCAP_ET_INERTIA, SDL_HAPTIC_INERTIA);
  392. FF_TEST(FFCAP_ET_FRICTION, SDL_HAPTIC_FRICTION);
  393. FF_TEST(FFCAP_ET_CUSTOMFORCE, SDL_HAPTIC_CUSTOM);
  394. // Check if supports gain.
  395. ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_FFGAIN,
  396. &val, sizeof(val));
  397. if (ret == FF_OK) {
  398. supported |= SDL_HAPTIC_GAIN;
  399. } else if (ret != FFERR_UNSUPPORTED) {
  400. return SDL_SetError("Haptic: Unable to get if device supports gain: %s.",
  401. FFStrError(ret));
  402. }
  403. // Checks if supports autocenter.
  404. ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_AUTOCENTER,
  405. &val, sizeof(val));
  406. if (ret == FF_OK) {
  407. supported |= SDL_HAPTIC_AUTOCENTER;
  408. } else if (ret != FFERR_UNSUPPORTED) {
  409. return SDL_SetError("Haptic: Unable to get if device supports autocenter: %s.",
  410. FFStrError(ret));
  411. }
  412. // Check for axes, we have an artificial limit on axes
  413. haptic->naxes = ((features.numFfAxes) > 3) ? 3 : features.numFfAxes;
  414. // Actually store the axes we want to use
  415. SDL_memcpy(haptic->hwdata->axes, features.ffAxes,
  416. haptic->naxes * sizeof(Uint8));
  417. // Always supported features.
  418. supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE;
  419. haptic->supported = supported;
  420. return true;
  421. }
  422. /*
  423. * Opens the haptic device from the file descriptor.
  424. */
  425. static bool SDL_SYS_HapticOpenFromService(SDL_Haptic *haptic, io_service_t service)
  426. {
  427. HRESULT ret;
  428. // Allocate the hwdata
  429. haptic->hwdata = (struct haptic_hwdata *) SDL_calloc(1, sizeof(*haptic->hwdata));
  430. if (!haptic->hwdata) {
  431. goto creat_err;
  432. }
  433. // Open the device
  434. ret = FFCreateDevice(service, &haptic->hwdata->device);
  435. if (ret != FF_OK) {
  436. SDL_SetError("Haptic: Unable to create device from service: %s.", FFStrError(ret));
  437. goto creat_err;
  438. }
  439. // Get supported features.
  440. if (!GetSupportedFeatures(haptic)) {
  441. goto open_err;
  442. }
  443. // Reset and then enable actuators.
  444. ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device,
  445. FFSFFC_RESET);
  446. if (ret != FF_OK) {
  447. SDL_SetError("Haptic: Unable to reset device: %s.", FFStrError(ret));
  448. goto open_err;
  449. }
  450. ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device,
  451. FFSFFC_SETACTUATORSON);
  452. if (ret != FF_OK) {
  453. SDL_SetError("Haptic: Unable to enable actuators: %s.",
  454. FFStrError(ret));
  455. goto open_err;
  456. }
  457. // Allocate effects memory.
  458. haptic->effects = (struct haptic_effect *)
  459. SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
  460. if (!haptic->effects) {
  461. goto open_err;
  462. }
  463. // Clear the memory
  464. SDL_memset(haptic->effects, 0,
  465. sizeof(struct haptic_effect) * haptic->neffects);
  466. return true;
  467. // Error handling
  468. open_err:
  469. FFReleaseDevice(haptic->hwdata->device);
  470. creat_err:
  471. if (haptic->hwdata) {
  472. SDL_free(haptic->hwdata);
  473. haptic->hwdata = NULL;
  474. }
  475. return false;
  476. }
  477. /*
  478. * Opens a haptic device for usage.
  479. */
  480. bool SDL_SYS_HapticOpen(SDL_Haptic *haptic)
  481. {
  482. SDL_hapticlist_item *item;
  483. item = HapticByInstanceID(haptic->instance_id);
  484. return SDL_SYS_HapticOpenFromService(haptic, item->dev);
  485. }
  486. /*
  487. * Opens a haptic device from first mouse it finds for usage.
  488. */
  489. int SDL_SYS_HapticMouse(void)
  490. {
  491. int device_index = 0;
  492. SDL_hapticlist_item *item;
  493. for (item = SDL_hapticlist; item; item = item->next) {
  494. if ((item->usagePage == kHIDPage_GenericDesktop) &&
  495. (item->usage == kHIDUsage_GD_Mouse)) {
  496. return device_index;
  497. }
  498. ++device_index;
  499. }
  500. return 0;
  501. }
  502. /*
  503. * Checks to see if a joystick has haptic features.
  504. */
  505. bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
  506. {
  507. #ifdef SDL_JOYSTICK_IOKIT
  508. if (joystick->driver != &SDL_DARWIN_JoystickDriver) {
  509. return false;
  510. }
  511. if (joystick->hwdata->ffservice != 0) {
  512. return true;
  513. }
  514. #endif
  515. return false;
  516. }
  517. /*
  518. * Checks to see if the haptic device and joystick are in reality the same.
  519. */
  520. bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
  521. {
  522. #ifdef SDL_JOYSTICK_IOKIT
  523. if (joystick->driver != &SDL_DARWIN_JoystickDriver) {
  524. return false;
  525. }
  526. if (IOObjectIsEqualTo((io_object_t)((size_t)haptic->hwdata->device),
  527. joystick->hwdata->ffservice)) {
  528. return true;
  529. }
  530. #endif
  531. return false;
  532. }
  533. /*
  534. * Opens a SDL_Haptic from a SDL_Joystick.
  535. */
  536. bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
  537. {
  538. #ifdef SDL_JOYSTICK_IOKIT
  539. SDL_hapticlist_item *item;
  540. if (joystick->driver != &SDL_DARWIN_JoystickDriver) {
  541. return false;
  542. }
  543. for (item = SDL_hapticlist; item; item = item->next) {
  544. if (IOObjectIsEqualTo((io_object_t)item->dev,
  545. joystick->hwdata->ffservice)) {
  546. haptic->instance_id = item->instance_id;
  547. break;
  548. }
  549. }
  550. if (joystick->name) {
  551. haptic->name = SDL_strdup(joystick->name);
  552. }
  553. return SDL_SYS_HapticOpenFromService(haptic, joystick->hwdata->ffservice);
  554. #else
  555. return false;
  556. #endif
  557. }
  558. /*
  559. * Closes the haptic device.
  560. */
  561. void SDL_SYS_HapticClose(SDL_Haptic *haptic)
  562. {
  563. if (haptic->hwdata) {
  564. // Free Effects.
  565. SDL_free(haptic->effects);
  566. haptic->effects = NULL;
  567. haptic->neffects = 0;
  568. // Clean up
  569. FFReleaseDevice(haptic->hwdata->device);
  570. // Free
  571. SDL_free(haptic->hwdata);
  572. haptic->hwdata = NULL;
  573. }
  574. }
  575. /*
  576. * Clean up after system specific haptic stuff
  577. */
  578. void SDL_SYS_HapticQuit(void)
  579. {
  580. SDL_hapticlist_item *item;
  581. SDL_hapticlist_item *next = NULL;
  582. for (item = SDL_hapticlist; item; item = next) {
  583. next = item->next;
  584. /* Opened and not closed haptics are leaked, this is on purpose.
  585. * Close your haptic devices after usage. */
  586. // Free the io_service_t
  587. IOObjectRelease(item->dev);
  588. SDL_free(item);
  589. }
  590. numhaptics = -1;
  591. SDL_hapticlist = NULL;
  592. SDL_hapticlist_tail = NULL;
  593. }
  594. /*
  595. * Converts an SDL trigger button to an FFEFFECT trigger button.
  596. */
  597. static DWORD FFGetTriggerButton(Uint16 button)
  598. {
  599. DWORD dwTriggerButton;
  600. dwTriggerButton = FFEB_NOTRIGGER;
  601. if (button != 0) {
  602. dwTriggerButton = FFJOFS_BUTTON(button - 1);
  603. }
  604. return dwTriggerButton;
  605. }
  606. /*
  607. * Sets the direction.
  608. */
  609. static bool SDL_SYS_SetDirection(FFEFFECT *effect, const SDL_HapticDirection *dir, int naxes)
  610. {
  611. LONG *rglDir;
  612. // Handle no axes a part.
  613. if (naxes == 0) {
  614. effect->dwFlags |= FFEFF_SPHERICAL; // Set as default.
  615. effect->rglDirection = NULL;
  616. return true;
  617. }
  618. // Has axes.
  619. rglDir = SDL_malloc(sizeof(LONG) * naxes);
  620. if (!rglDir) {
  621. return false;
  622. }
  623. SDL_memset(rglDir, 0, sizeof(LONG) * naxes);
  624. effect->rglDirection = rglDir;
  625. switch (dir->type) {
  626. case SDL_HAPTIC_POLAR:
  627. effect->dwFlags |= FFEFF_POLAR;
  628. rglDir[0] = dir->dir[0];
  629. return true;
  630. case SDL_HAPTIC_CARTESIAN:
  631. effect->dwFlags |= FFEFF_CARTESIAN;
  632. rglDir[0] = dir->dir[0];
  633. if (naxes > 1) {
  634. rglDir[1] = dir->dir[1];
  635. }
  636. if (naxes > 2) {
  637. rglDir[2] = dir->dir[2];
  638. }
  639. return true;
  640. case SDL_HAPTIC_SPHERICAL:
  641. effect->dwFlags |= FFEFF_SPHERICAL;
  642. rglDir[0] = dir->dir[0];
  643. if (naxes > 1) {
  644. rglDir[1] = dir->dir[1];
  645. }
  646. if (naxes > 2) {
  647. rglDir[2] = dir->dir[2];
  648. }
  649. return true;
  650. case SDL_HAPTIC_STEERING_AXIS:
  651. effect->dwFlags |= FFEFF_CARTESIAN;
  652. rglDir[0] = 0;
  653. return true;
  654. default:
  655. return SDL_SetError("Haptic: Unknown direction type.");
  656. }
  657. }
  658. // Clamps and converts.
  659. #define CCONVERT(x) (((x) > 0x7FFF) ? 10000 : ((x)*10000) / 0x7FFF)
  660. // Just converts.
  661. #define CONVERT(x) (((x)*10000) / 0x7FFF)
  662. /*
  663. * Creates the FFEFFECT from a SDL_HapticEffect.
  664. */
  665. static bool SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, const SDL_HapticEffect *src)
  666. {
  667. int i;
  668. FFCONSTANTFORCE *constant = NULL;
  669. FFPERIODIC *periodic = NULL;
  670. FFCONDITION *condition = NULL; // Actually an array of conditions - one per axis.
  671. FFRAMPFORCE *ramp = NULL;
  672. FFCUSTOMFORCE *custom = NULL;
  673. FFENVELOPE *envelope = NULL;
  674. const SDL_HapticConstant *hap_constant = NULL;
  675. const SDL_HapticPeriodic *hap_periodic = NULL;
  676. const SDL_HapticCondition *hap_condition = NULL;
  677. const SDL_HapticRamp *hap_ramp = NULL;
  678. const SDL_HapticCustom *hap_custom = NULL;
  679. DWORD *axes = NULL;
  680. // Set global stuff.
  681. SDL_memset(dest, 0, sizeof(FFEFFECT));
  682. dest->dwSize = sizeof(FFEFFECT); // Set the structure size.
  683. dest->dwSamplePeriod = 0; // Not used by us.
  684. dest->dwGain = 10000; // Gain is set globally, not locally.
  685. dest->dwFlags = FFEFF_OBJECTOFFSETS; // Seems obligatory.
  686. // Envelope.
  687. envelope = SDL_calloc(1, sizeof(FFENVELOPE));
  688. if (!envelope) {
  689. return false;
  690. }
  691. dest->lpEnvelope = envelope;
  692. envelope->dwSize = sizeof(FFENVELOPE); // Always should be this.
  693. // Axes.
  694. if (src->constant.direction.type == SDL_HAPTIC_STEERING_AXIS) {
  695. dest->cAxes = 1;
  696. } else {
  697. dest->cAxes = haptic->naxes;
  698. }
  699. if (dest->cAxes > 0) {
  700. axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
  701. if (!axes) {
  702. return false;
  703. }
  704. axes[0] = haptic->hwdata->axes[0]; // Always at least one axis.
  705. if (dest->cAxes > 1) {
  706. axes[1] = haptic->hwdata->axes[1];
  707. }
  708. if (dest->cAxes > 2) {
  709. axes[2] = haptic->hwdata->axes[2];
  710. }
  711. dest->rgdwAxes = axes;
  712. }
  713. // The big type handling switch, even bigger then Linux's version.
  714. switch (src->type) {
  715. case SDL_HAPTIC_CONSTANT:
  716. hap_constant = &src->constant;
  717. constant = SDL_calloc(1, sizeof(FFCONSTANTFORCE));
  718. if (!constant) {
  719. return false;
  720. }
  721. // Specifics
  722. constant->lMagnitude = CONVERT(hap_constant->level);
  723. dest->cbTypeSpecificParams = sizeof(FFCONSTANTFORCE);
  724. dest->lpvTypeSpecificParams = constant;
  725. // Generics
  726. dest->dwDuration = hap_constant->length * 1000; // In microseconds.
  727. dest->dwTriggerButton = FFGetTriggerButton(hap_constant->button);
  728. dest->dwTriggerRepeatInterval = hap_constant->interval;
  729. dest->dwStartDelay = hap_constant->delay * 1000; // In microseconds.
  730. // Direction.
  731. if (!SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes)) {
  732. return false;
  733. }
  734. // Envelope
  735. if ((hap_constant->attack_length == 0) && (hap_constant->fade_length == 0)) {
  736. SDL_free(envelope);
  737. dest->lpEnvelope = NULL;
  738. } else {
  739. envelope->dwAttackLevel = CCONVERT(hap_constant->attack_level);
  740. envelope->dwAttackTime = hap_constant->attack_length * 1000;
  741. envelope->dwFadeLevel = CCONVERT(hap_constant->fade_level);
  742. envelope->dwFadeTime = hap_constant->fade_length * 1000;
  743. }
  744. break;
  745. case SDL_HAPTIC_SINE:
  746. case SDL_HAPTIC_SQUARE:
  747. case SDL_HAPTIC_TRIANGLE:
  748. case SDL_HAPTIC_SAWTOOTHUP:
  749. case SDL_HAPTIC_SAWTOOTHDOWN:
  750. hap_periodic = &src->periodic;
  751. periodic = SDL_calloc(1, sizeof(FFPERIODIC));
  752. if (!periodic) {
  753. return false;
  754. }
  755. // Specifics
  756. periodic->dwMagnitude = CONVERT(SDL_abs(hap_periodic->magnitude));
  757. periodic->lOffset = CONVERT(hap_periodic->offset);
  758. periodic->dwPhase =
  759. (hap_periodic->phase + (hap_periodic->magnitude < 0 ? 18000 : 0)) % 36000;
  760. periodic->dwPeriod = hap_periodic->period * 1000;
  761. dest->cbTypeSpecificParams = sizeof(FFPERIODIC);
  762. dest->lpvTypeSpecificParams = periodic;
  763. // Generics
  764. dest->dwDuration = hap_periodic->length * 1000; // In microseconds.
  765. dest->dwTriggerButton = FFGetTriggerButton(hap_periodic->button);
  766. dest->dwTriggerRepeatInterval = hap_periodic->interval;
  767. dest->dwStartDelay = hap_periodic->delay * 1000; // In microseconds.
  768. // Direction.
  769. if (!SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes)) {
  770. return false;
  771. }
  772. // Envelope
  773. if ((hap_periodic->attack_length == 0) && (hap_periodic->fade_length == 0)) {
  774. SDL_free(envelope);
  775. dest->lpEnvelope = NULL;
  776. } else {
  777. envelope->dwAttackLevel = CCONVERT(hap_periodic->attack_level);
  778. envelope->dwAttackTime = hap_periodic->attack_length * 1000;
  779. envelope->dwFadeLevel = CCONVERT(hap_periodic->fade_level);
  780. envelope->dwFadeTime = hap_periodic->fade_length * 1000;
  781. }
  782. break;
  783. case SDL_HAPTIC_SPRING:
  784. case SDL_HAPTIC_DAMPER:
  785. case SDL_HAPTIC_INERTIA:
  786. case SDL_HAPTIC_FRICTION:
  787. hap_condition = &src->condition;
  788. if (dest->cAxes > 0) {
  789. condition = SDL_calloc(dest->cAxes, sizeof(FFCONDITION));
  790. if (!condition) {
  791. return false;
  792. }
  793. // Specifics
  794. for (i = 0; i < dest->cAxes; i++) {
  795. condition[i].lOffset = CONVERT(hap_condition->center[i]);
  796. condition[i].lPositiveCoefficient =
  797. CONVERT(hap_condition->right_coeff[i]);
  798. condition[i].lNegativeCoefficient =
  799. CONVERT(hap_condition->left_coeff[i]);
  800. condition[i].dwPositiveSaturation =
  801. CCONVERT(hap_condition->right_sat[i] / 2);
  802. condition[i].dwNegativeSaturation =
  803. CCONVERT(hap_condition->left_sat[i] / 2);
  804. condition[i].lDeadBand = CCONVERT(hap_condition->deadband[i] / 2);
  805. }
  806. }
  807. dest->cbTypeSpecificParams = sizeof(FFCONDITION) * dest->cAxes;
  808. dest->lpvTypeSpecificParams = condition;
  809. // Generics
  810. dest->dwDuration = hap_condition->length * 1000; // In microseconds.
  811. dest->dwTriggerButton = FFGetTriggerButton(hap_condition->button);
  812. dest->dwTriggerRepeatInterval = hap_condition->interval;
  813. dest->dwStartDelay = hap_condition->delay * 1000; // In microseconds.
  814. // Direction.
  815. if (!SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes)) {
  816. return false;
  817. }
  818. // Envelope - Not actually supported by most CONDITION implementations.
  819. SDL_free(dest->lpEnvelope);
  820. dest->lpEnvelope = NULL;
  821. break;
  822. case SDL_HAPTIC_RAMP:
  823. hap_ramp = &src->ramp;
  824. ramp = SDL_calloc(1, sizeof(FFRAMPFORCE));
  825. if (!ramp) {
  826. return false;
  827. }
  828. // Specifics
  829. ramp->lStart = CONVERT(hap_ramp->start);
  830. ramp->lEnd = CONVERT(hap_ramp->end);
  831. dest->cbTypeSpecificParams = sizeof(FFRAMPFORCE);
  832. dest->lpvTypeSpecificParams = ramp;
  833. // Generics
  834. dest->dwDuration = hap_ramp->length * 1000; // In microseconds.
  835. dest->dwTriggerButton = FFGetTriggerButton(hap_ramp->button);
  836. dest->dwTriggerRepeatInterval = hap_ramp->interval;
  837. dest->dwStartDelay = hap_ramp->delay * 1000; // In microseconds.
  838. // Direction.
  839. if (!SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes)) {
  840. return false;
  841. }
  842. // Envelope
  843. if ((hap_ramp->attack_length == 0) && (hap_ramp->fade_length == 0)) {
  844. SDL_free(envelope);
  845. dest->lpEnvelope = NULL;
  846. } else {
  847. envelope->dwAttackLevel = CCONVERT(hap_ramp->attack_level);
  848. envelope->dwAttackTime = hap_ramp->attack_length * 1000;
  849. envelope->dwFadeLevel = CCONVERT(hap_ramp->fade_level);
  850. envelope->dwFadeTime = hap_ramp->fade_length * 1000;
  851. }
  852. break;
  853. case SDL_HAPTIC_CUSTOM:
  854. hap_custom = &src->custom;
  855. custom = SDL_calloc(1, sizeof(FFCUSTOMFORCE));
  856. if (!custom) {
  857. return false;
  858. }
  859. // Specifics
  860. custom->cChannels = hap_custom->channels;
  861. custom->dwSamplePeriod = hap_custom->period * 1000;
  862. custom->cSamples = hap_custom->samples;
  863. custom->rglForceData =
  864. SDL_malloc(sizeof(LONG) * custom->cSamples * custom->cChannels);
  865. for (i = 0; i < hap_custom->samples * hap_custom->channels; i++) { // Copy data.
  866. custom->rglForceData[i] = CCONVERT(hap_custom->data[i]);
  867. }
  868. dest->cbTypeSpecificParams = sizeof(FFCUSTOMFORCE);
  869. dest->lpvTypeSpecificParams = custom;
  870. // Generics
  871. dest->dwDuration = hap_custom->length * 1000; // In microseconds.
  872. dest->dwTriggerButton = FFGetTriggerButton(hap_custom->button);
  873. dest->dwTriggerRepeatInterval = hap_custom->interval;
  874. dest->dwStartDelay = hap_custom->delay * 1000; // In microseconds.
  875. // Direction.
  876. if (!SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes)) {
  877. return false;
  878. }
  879. // Envelope
  880. if ((hap_custom->attack_length == 0) && (hap_custom->fade_length == 0)) {
  881. SDL_free(envelope);
  882. dest->lpEnvelope = NULL;
  883. } else {
  884. envelope->dwAttackLevel = CCONVERT(hap_custom->attack_level);
  885. envelope->dwAttackTime = hap_custom->attack_length * 1000;
  886. envelope->dwFadeLevel = CCONVERT(hap_custom->fade_level);
  887. envelope->dwFadeTime = hap_custom->fade_length * 1000;
  888. }
  889. break;
  890. default:
  891. return SDL_SetError("Haptic: Unknown effect type.");
  892. }
  893. return true;
  894. }
  895. /*
  896. * Frees an FFEFFECT allocated by SDL_SYS_ToFFEFFECT.
  897. */
  898. static void SDL_SYS_HapticFreeFFEFFECT(FFEFFECT *effect, int type)
  899. {
  900. FFCUSTOMFORCE *custom;
  901. SDL_free(effect->lpEnvelope);
  902. effect->lpEnvelope = NULL;
  903. SDL_free(effect->rgdwAxes);
  904. effect->rgdwAxes = NULL;
  905. if (effect->lpvTypeSpecificParams) {
  906. if (type == SDL_HAPTIC_CUSTOM) { // Must free the custom data.
  907. custom = (FFCUSTOMFORCE *)effect->lpvTypeSpecificParams;
  908. SDL_free(custom->rglForceData);
  909. custom->rglForceData = NULL;
  910. }
  911. SDL_free(effect->lpvTypeSpecificParams);
  912. effect->lpvTypeSpecificParams = NULL;
  913. }
  914. SDL_free(effect->rglDirection);
  915. effect->rglDirection = NULL;
  916. }
  917. /*
  918. * Gets the effect type from the generic SDL haptic effect wrapper.
  919. */
  920. CFUUIDRef
  921. SDL_SYS_HapticEffectType(Uint16 type)
  922. {
  923. switch (type) {
  924. case SDL_HAPTIC_CONSTANT:
  925. return kFFEffectType_ConstantForce_ID;
  926. case SDL_HAPTIC_RAMP:
  927. return kFFEffectType_RampForce_ID;
  928. case SDL_HAPTIC_SQUARE:
  929. return kFFEffectType_Square_ID;
  930. case SDL_HAPTIC_SINE:
  931. return kFFEffectType_Sine_ID;
  932. case SDL_HAPTIC_TRIANGLE:
  933. return kFFEffectType_Triangle_ID;
  934. case SDL_HAPTIC_SAWTOOTHUP:
  935. return kFFEffectType_SawtoothUp_ID;
  936. case SDL_HAPTIC_SAWTOOTHDOWN:
  937. return kFFEffectType_SawtoothDown_ID;
  938. case SDL_HAPTIC_SPRING:
  939. return kFFEffectType_Spring_ID;
  940. case SDL_HAPTIC_DAMPER:
  941. return kFFEffectType_Damper_ID;
  942. case SDL_HAPTIC_INERTIA:
  943. return kFFEffectType_Inertia_ID;
  944. case SDL_HAPTIC_FRICTION:
  945. return kFFEffectType_Friction_ID;
  946. case SDL_HAPTIC_CUSTOM:
  947. return kFFEffectType_CustomForce_ID;
  948. default:
  949. SDL_SetError("Haptic: Unknown effect type.");
  950. return NULL;
  951. }
  952. }
  953. /*
  954. * Creates a new haptic effect.
  955. */
  956. bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
  957. const SDL_HapticEffect *base)
  958. {
  959. HRESULT ret;
  960. CFUUIDRef type;
  961. // Alloc the effect.
  962. effect->hweffect = (struct haptic_hweffect *)
  963. SDL_calloc(1, sizeof(struct haptic_hweffect));
  964. if (!effect->hweffect) {
  965. goto err_hweffect;
  966. }
  967. // Get the type.
  968. type = SDL_SYS_HapticEffectType(base->type);
  969. if (!type) {
  970. goto err_hweffect;
  971. }
  972. // Get the effect.
  973. if (!SDL_SYS_ToFFEFFECT(haptic, &effect->hweffect->effect, base)) {
  974. goto err_effectdone;
  975. }
  976. // Create the actual effect.
  977. ret = FFDeviceCreateEffect(haptic->hwdata->device, type,
  978. &effect->hweffect->effect,
  979. &effect->hweffect->ref);
  980. if (ret != FF_OK) {
  981. SDL_SetError("Haptic: Unable to create effect: %s.", FFStrError(ret));
  982. goto err_effectdone;
  983. }
  984. return true;
  985. err_effectdone:
  986. SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, base->type);
  987. err_hweffect:
  988. SDL_free(effect->hweffect);
  989. effect->hweffect = NULL;
  990. return false;
  991. }
  992. /*
  993. * Updates an effect.
  994. */
  995. bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic,
  996. struct haptic_effect *effect,
  997. const SDL_HapticEffect *data)
  998. {
  999. HRESULT ret;
  1000. FFEffectParameterFlag flags;
  1001. FFEFFECT temp;
  1002. // Get the effect.
  1003. SDL_memset(&temp, 0, sizeof(FFEFFECT));
  1004. if (!SDL_SYS_ToFFEFFECT(haptic, &temp, data)) {
  1005. goto err_update;
  1006. }
  1007. /* Set the flags. Might be worthwhile to diff temp with loaded effect and
  1008. * only change those parameters. */
  1009. flags = FFEP_DIRECTION |
  1010. FFEP_DURATION |
  1011. FFEP_ENVELOPE |
  1012. FFEP_STARTDELAY |
  1013. FFEP_TRIGGERBUTTON |
  1014. FFEP_TRIGGERREPEATINTERVAL | FFEP_TYPESPECIFICPARAMS;
  1015. // Create the actual effect.
  1016. ret = FFEffectSetParameters(effect->hweffect->ref, &temp, flags);
  1017. if (ret != FF_OK) {
  1018. SDL_SetError("Haptic: Unable to update effect: %s.", FFStrError(ret));
  1019. goto err_update;
  1020. }
  1021. // Copy it over.
  1022. SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, data->type);
  1023. SDL_memcpy(&effect->hweffect->effect, &temp, sizeof(FFEFFECT));
  1024. return true;
  1025. err_update:
  1026. SDL_SYS_HapticFreeFFEFFECT(&temp, data->type);
  1027. return false;
  1028. }
  1029. /*
  1030. * Runs an effect.
  1031. */
  1032. bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
  1033. Uint32 iterations)
  1034. {
  1035. HRESULT ret;
  1036. Uint32 iter;
  1037. // Check if it's infinite.
  1038. if (iterations == SDL_HAPTIC_INFINITY) {
  1039. iter = FF_INFINITE;
  1040. } else {
  1041. iter = iterations;
  1042. }
  1043. // Run the effect.
  1044. ret = FFEffectStart(effect->hweffect->ref, iter, 0);
  1045. if (ret != FF_OK) {
  1046. return SDL_SetError("Haptic: Unable to run the effect: %s.",
  1047. FFStrError(ret));
  1048. }
  1049. return true;
  1050. }
  1051. /*
  1052. * Stops an effect.
  1053. */
  1054. bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
  1055. {
  1056. HRESULT ret;
  1057. ret = FFEffectStop(effect->hweffect->ref);
  1058. if (ret != FF_OK) {
  1059. return SDL_SetError("Haptic: Unable to stop the effect: %s.",
  1060. FFStrError(ret));
  1061. }
  1062. return true;
  1063. }
  1064. /*
  1065. * Frees the effect.
  1066. */
  1067. void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
  1068. {
  1069. HRESULT ret;
  1070. ret = FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref);
  1071. if (ret != FF_OK) {
  1072. SDL_SetError("Haptic: Error removing the effect from the device: %s.",
  1073. FFStrError(ret));
  1074. }
  1075. SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect,
  1076. effect->effect.type);
  1077. SDL_free(effect->hweffect);
  1078. effect->hweffect = NULL;
  1079. }
  1080. /*
  1081. * Gets the status of a haptic effect.
  1082. */
  1083. int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic,
  1084. struct haptic_effect *effect)
  1085. {
  1086. HRESULT ret;
  1087. FFEffectStatusFlag status;
  1088. ret = FFEffectGetEffectStatus(effect->hweffect->ref, &status);
  1089. if (ret != FF_OK) {
  1090. SDL_SetError("Haptic: Unable to get effect status: %s.", FFStrError(ret));
  1091. return -1;
  1092. }
  1093. if (status == 0) {
  1094. return 0;
  1095. }
  1096. return 1; // Assume it's playing or emulated.
  1097. }
  1098. /*
  1099. * Sets the gain.
  1100. */
  1101. bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain)
  1102. {
  1103. HRESULT ret;
  1104. Uint32 val;
  1105. val = gain * 100; // macOS uses 0 to 10,000
  1106. ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
  1107. FFPROP_FFGAIN, &val);
  1108. if (ret != FF_OK) {
  1109. return SDL_SetError("Haptic: Error setting gain: %s.", FFStrError(ret));
  1110. }
  1111. return true;
  1112. }
  1113. /*
  1114. * Sets the autocentering.
  1115. */
  1116. bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
  1117. {
  1118. HRESULT ret;
  1119. Uint32 val;
  1120. // macOS only has 0 (off) and 1 (on)
  1121. if (autocenter == 0) {
  1122. val = 0;
  1123. } else {
  1124. val = 1;
  1125. }
  1126. ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
  1127. FFPROP_AUTOCENTER, &val);
  1128. if (ret != FF_OK) {
  1129. return SDL_SetError("Haptic: Error setting autocenter: %s.",
  1130. FFStrError(ret));
  1131. }
  1132. return true;
  1133. }
  1134. /*
  1135. * Pauses the device.
  1136. */
  1137. bool SDL_SYS_HapticPause(SDL_Haptic *haptic)
  1138. {
  1139. HRESULT ret;
  1140. ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device,
  1141. FFSFFC_PAUSE);
  1142. if (ret != FF_OK) {
  1143. return SDL_SetError("Haptic: Error pausing device: %s.", FFStrError(ret));
  1144. }
  1145. return true;
  1146. }
  1147. /*
  1148. * Unpauses the device.
  1149. */
  1150. bool SDL_SYS_HapticResume(SDL_Haptic *haptic)
  1151. {
  1152. HRESULT ret;
  1153. ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device,
  1154. FFSFFC_CONTINUE);
  1155. if (ret != FF_OK) {
  1156. return SDL_SetError("Haptic: Error resuming device: %s.", FFStrError(ret));
  1157. }
  1158. return true;
  1159. }
  1160. /*
  1161. * Stops all currently playing effects.
  1162. */
  1163. bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic)
  1164. {
  1165. HRESULT ret;
  1166. ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device,
  1167. FFSFFC_STOPALL);
  1168. if (ret != FF_OK) {
  1169. return SDL_SetError("Haptic: Error stopping device: %s.", FFStrError(ret));
  1170. }
  1171. return true;
  1172. }
  1173. #endif // SDL_HAPTIC_IOKIT