SDL_sensor.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. /* This is the sensor API for Simple DirectMedia Layer */
  20. #include "SDL_syssensor.h"
  21. #ifndef SDL_EVENTS_DISABLED
  22. #include "../events/SDL_events_c.h"
  23. #endif
  24. #include "../joystick/SDL_gamepad_c.h"
  25. static SDL_SensorDriver *SDL_sensor_drivers[] = {
  26. #ifdef SDL_SENSOR_ANDROID
  27. &SDL_ANDROID_SensorDriver,
  28. #endif
  29. #ifdef SDL_SENSOR_COREMOTION
  30. &SDL_COREMOTION_SensorDriver,
  31. #endif
  32. #ifdef SDL_SENSOR_WINDOWS
  33. &SDL_WINDOWS_SensorDriver,
  34. #endif
  35. #ifdef SDL_SENSOR_VITA
  36. &SDL_VITA_SensorDriver,
  37. #endif
  38. #ifdef SDL_SENSOR_N3DS
  39. &SDL_N3DS_SensorDriver,
  40. #endif
  41. #if defined(SDL_SENSOR_DUMMY) || defined(SDL_SENSOR_DISABLED)
  42. &SDL_DUMMY_SensorDriver
  43. #endif
  44. };
  45. #ifndef SDL_THREAD_SAFETY_ANALYSIS
  46. static
  47. #endif
  48. SDL_Mutex *SDL_sensor_lock = NULL; /* This needs to support recursive locks */
  49. static SDL_AtomicInt SDL_sensor_lock_pending;
  50. static int SDL_sensors_locked;
  51. static SDL_bool SDL_sensors_initialized;
  52. static SDL_Sensor *SDL_sensors SDL_GUARDED_BY(SDL_sensor_lock) = NULL;
  53. static SDL_AtomicInt SDL_last_sensor_instance_id SDL_GUARDED_BY(SDL_sensor_lock);
  54. static char SDL_sensor_magic;
  55. #define CHECK_SENSOR_MAGIC(sensor, retval) \
  56. if (!sensor || sensor->magic != &SDL_sensor_magic) { \
  57. SDL_InvalidParamError("sensor"); \
  58. SDL_UnlockSensors(); \
  59. return retval; \
  60. }
  61. SDL_bool SDL_SensorsInitialized(void)
  62. {
  63. return SDL_sensors_initialized;
  64. }
  65. void SDL_LockSensors(void)
  66. {
  67. (void)SDL_AtomicIncRef(&SDL_sensor_lock_pending);
  68. SDL_LockMutex(SDL_sensor_lock);
  69. (void)SDL_AtomicDecRef(&SDL_sensor_lock_pending);
  70. ++SDL_sensors_locked;
  71. }
  72. void SDL_UnlockSensors(void)
  73. {
  74. SDL_bool last_unlock = SDL_FALSE;
  75. --SDL_sensors_locked;
  76. if (!SDL_sensors_initialized) {
  77. /* NOTE: There's a small window here where another thread could lock the mutex after we've checked for pending locks */
  78. if (!SDL_sensors_locked && SDL_AtomicGet(&SDL_sensor_lock_pending) == 0) {
  79. last_unlock = SDL_TRUE;
  80. }
  81. }
  82. /* The last unlock after sensors are uninitialized will cleanup the mutex,
  83. * allowing applications to lock sensors while reinitializing the system.
  84. */
  85. if (last_unlock) {
  86. SDL_Mutex *sensor_lock = SDL_sensor_lock;
  87. SDL_LockMutex(sensor_lock);
  88. {
  89. SDL_UnlockMutex(SDL_sensor_lock);
  90. SDL_sensor_lock = NULL;
  91. }
  92. SDL_UnlockMutex(sensor_lock);
  93. SDL_DestroyMutex(sensor_lock);
  94. } else {
  95. SDL_UnlockMutex(SDL_sensor_lock);
  96. }
  97. }
  98. SDL_bool SDL_SensorsLocked(void)
  99. {
  100. return (SDL_sensors_locked > 0) ? SDL_TRUE : SDL_FALSE;
  101. }
  102. void SDL_AssertSensorsLocked(void)
  103. {
  104. SDL_assert(SDL_SensorsLocked());
  105. }
  106. int SDL_InitSensors(void)
  107. {
  108. int i, status;
  109. /* Create the sensor list lock */
  110. if (SDL_sensor_lock == NULL) {
  111. SDL_sensor_lock = SDL_CreateMutex();
  112. }
  113. #ifndef SDL_EVENTS_DISABLED
  114. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
  115. return -1;
  116. }
  117. #endif /* !SDL_EVENTS_DISABLED */
  118. SDL_LockSensors();
  119. SDL_sensors_initialized = SDL_TRUE;
  120. status = -1;
  121. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  122. if (SDL_sensor_drivers[i]->Init() >= 0) {
  123. status = 0;
  124. }
  125. }
  126. SDL_UnlockSensors();
  127. if (status < 0) {
  128. SDL_QuitSensors();
  129. }
  130. return status;
  131. }
  132. SDL_bool SDL_SensorsOpened(void)
  133. {
  134. SDL_bool opened;
  135. SDL_LockSensors();
  136. {
  137. if (SDL_sensors != NULL) {
  138. opened = SDL_TRUE;
  139. } else {
  140. opened = SDL_FALSE;
  141. }
  142. }
  143. SDL_UnlockSensors();
  144. return opened;
  145. }
  146. SDL_SensorID *SDL_GetSensors(int *count)
  147. {
  148. int i, num_sensors, device_index;
  149. int sensor_index = 0, total_sensors = 0;
  150. SDL_SensorID *sensors;
  151. SDL_LockSensors();
  152. {
  153. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  154. total_sensors += SDL_sensor_drivers[i]->GetCount();
  155. }
  156. sensors = (SDL_SensorID *)SDL_malloc((total_sensors + 1) * sizeof(*sensors));
  157. if (sensors) {
  158. if (count) {
  159. *count = total_sensors;
  160. }
  161. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  162. num_sensors = SDL_sensor_drivers[i]->GetCount();
  163. for (device_index = 0; device_index < num_sensors; ++device_index) {
  164. SDL_assert(sensor_index < total_sensors);
  165. sensors[sensor_index] = SDL_sensor_drivers[i]->GetDeviceInstanceID(device_index);
  166. SDL_assert(sensors[sensor_index] > 0);
  167. ++sensor_index;
  168. }
  169. }
  170. SDL_assert(sensor_index == total_sensors);
  171. sensors[sensor_index] = 0;
  172. } else {
  173. if (count) {
  174. *count = 0;
  175. }
  176. SDL_OutOfMemory();
  177. }
  178. }
  179. SDL_UnlockSensors();
  180. return sensors;
  181. }
  182. /*
  183. * Return the next available sensor instance ID
  184. * This may be called by drivers from multiple threads, unprotected by any locks
  185. */
  186. SDL_SensorID SDL_GetNextSensorInstanceID(void)
  187. {
  188. return SDL_AtomicIncRef(&SDL_last_sensor_instance_id) + 1;
  189. }
  190. /*
  191. * Get the driver and device index for a sensor instance ID
  192. * This should be called while the sensor lock is held, to prevent another thread from updating the list
  193. */
  194. static SDL_bool SDL_GetDriverAndSensorIndex(SDL_SensorID instance_id, SDL_SensorDriver **driver, int *driver_index)
  195. {
  196. int i, num_sensors, device_index;
  197. if (instance_id > 0) {
  198. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  199. num_sensors = SDL_sensor_drivers[i]->GetCount();
  200. for (device_index = 0; device_index < num_sensors; ++device_index) {
  201. SDL_SensorID sensor_id = SDL_sensor_drivers[i]->GetDeviceInstanceID(device_index);
  202. if (sensor_id == instance_id) {
  203. *driver = SDL_sensor_drivers[i];
  204. *driver_index = device_index;
  205. return SDL_TRUE;
  206. }
  207. }
  208. }
  209. }
  210. SDL_SetError("Sensor %" SDL_PRIs32 " not found", instance_id);
  211. return SDL_FALSE;
  212. }
  213. /*
  214. * Get the implementation dependent name of a sensor
  215. */
  216. const char *SDL_GetSensorInstanceName(SDL_SensorID instance_id)
  217. {
  218. SDL_SensorDriver *driver;
  219. int device_index;
  220. const char *name = NULL;
  221. SDL_LockSensors();
  222. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  223. name = driver->GetDeviceName(device_index);
  224. }
  225. SDL_UnlockSensors();
  226. /* FIXME: Really we should reference count this name so it doesn't go away after unlock */
  227. return name;
  228. }
  229. SDL_SensorType SDL_GetSensorInstanceType(SDL_SensorID instance_id)
  230. {
  231. SDL_SensorDriver *driver;
  232. int device_index;
  233. SDL_SensorType type = SDL_SENSOR_INVALID;
  234. SDL_LockSensors();
  235. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  236. type = driver->GetDeviceType(device_index);
  237. }
  238. SDL_UnlockSensors();
  239. return type;
  240. }
  241. int SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id)
  242. {
  243. SDL_SensorDriver *driver;
  244. int device_index;
  245. int type = -1;
  246. SDL_LockSensors();
  247. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  248. type = driver->GetDeviceNonPortableType(device_index);
  249. }
  250. SDL_UnlockSensors();
  251. return type;
  252. }
  253. /*
  254. * Open a sensor for use - the index passed as an argument refers to
  255. * the N'th sensor on the system. This index is the value which will
  256. * identify this sensor in future sensor events.
  257. *
  258. * This function returns a sensor identifier, or NULL if an error occurred.
  259. */
  260. SDL_Sensor *SDL_OpenSensor(SDL_SensorID instance_id)
  261. {
  262. SDL_SensorDriver *driver;
  263. int device_index;
  264. SDL_Sensor *sensor;
  265. SDL_Sensor *sensorlist;
  266. const char *sensorname = NULL;
  267. SDL_LockSensors();
  268. if (!SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  269. SDL_UnlockSensors();
  270. return NULL;
  271. }
  272. sensorlist = SDL_sensors;
  273. /* If the sensor is already open, return it
  274. * it is important that we have a single sensor * for each instance id
  275. */
  276. while (sensorlist) {
  277. if (instance_id == sensorlist->instance_id) {
  278. sensor = sensorlist;
  279. ++sensor->ref_count;
  280. SDL_UnlockSensors();
  281. return sensor;
  282. }
  283. sensorlist = sensorlist->next;
  284. }
  285. /* Create and initialize the sensor */
  286. sensor = (SDL_Sensor *)SDL_calloc(sizeof(*sensor), 1);
  287. if (sensor == NULL) {
  288. SDL_OutOfMemory();
  289. SDL_UnlockSensors();
  290. return NULL;
  291. }
  292. sensor->driver = driver;
  293. sensor->instance_id = instance_id;
  294. sensor->type = driver->GetDeviceType(device_index);
  295. sensor->non_portable_type = driver->GetDeviceNonPortableType(device_index);
  296. if (driver->Open(sensor, device_index) < 0) {
  297. SDL_free(sensor);
  298. SDL_UnlockSensors();
  299. return NULL;
  300. }
  301. sensorname = driver->GetDeviceName(device_index);
  302. if (sensorname) {
  303. sensor->name = SDL_strdup(sensorname);
  304. } else {
  305. sensor->name = NULL;
  306. }
  307. /* Add sensor to list */
  308. ++sensor->ref_count;
  309. /* Link the sensor in the list */
  310. sensor->next = SDL_sensors;
  311. SDL_sensors = sensor;
  312. driver->Update(sensor);
  313. SDL_UnlockSensors();
  314. return sensor;
  315. }
  316. /*
  317. * Find the SDL_Sensor that owns this instance id
  318. */
  319. SDL_Sensor *SDL_GetSensorFromInstanceID(SDL_SensorID instance_id)
  320. {
  321. SDL_Sensor *sensor;
  322. SDL_LockSensors();
  323. for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
  324. if (sensor->instance_id == instance_id) {
  325. break;
  326. }
  327. }
  328. SDL_UnlockSensors();
  329. return sensor;
  330. }
  331. /*
  332. * Get the friendly name of this sensor
  333. */
  334. const char *SDL_GetSensorName(SDL_Sensor *sensor)
  335. {
  336. const char *retval;
  337. SDL_LockSensors();
  338. {
  339. CHECK_SENSOR_MAGIC(sensor, NULL);
  340. retval = sensor->name;
  341. }
  342. SDL_UnlockSensors();
  343. return retval;
  344. }
  345. /*
  346. * Get the type of this sensor
  347. */
  348. SDL_SensorType SDL_GetSensorType(SDL_Sensor *sensor)
  349. {
  350. SDL_SensorType retval;
  351. SDL_LockSensors();
  352. {
  353. CHECK_SENSOR_MAGIC(sensor, SDL_SENSOR_INVALID);
  354. retval = sensor->type;
  355. }
  356. SDL_UnlockSensors();
  357. return retval;
  358. }
  359. /*
  360. * Get the platform dependent type of this sensor
  361. */
  362. int SDL_GetSensorNonPortableType(SDL_Sensor *sensor)
  363. {
  364. int retval;
  365. SDL_LockSensors();
  366. {
  367. CHECK_SENSOR_MAGIC(sensor, -1);
  368. retval = sensor->non_portable_type;
  369. }
  370. SDL_UnlockSensors();
  371. return retval;
  372. }
  373. /*
  374. * Get the instance id for this opened sensor
  375. */
  376. SDL_SensorID SDL_GetSensorInstanceID(SDL_Sensor *sensor)
  377. {
  378. SDL_SensorID retval;
  379. SDL_LockSensors();
  380. {
  381. CHECK_SENSOR_MAGIC(sensor, 0);
  382. retval = sensor->instance_id;
  383. }
  384. SDL_UnlockSensors();
  385. return retval;
  386. }
  387. /*
  388. * Get the current state of this sensor
  389. */
  390. int SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values)
  391. {
  392. SDL_LockSensors();
  393. {
  394. CHECK_SENSOR_MAGIC(sensor, -1);
  395. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  396. SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
  397. }
  398. SDL_UnlockSensors();
  399. return 0;
  400. }
  401. /*
  402. * Close a sensor previously opened with SDL_OpenSensor()
  403. */
  404. void SDL_CloseSensor(SDL_Sensor *sensor)
  405. {
  406. SDL_Sensor *sensorlist;
  407. SDL_Sensor *sensorlistprev;
  408. SDL_LockSensors();
  409. {
  410. CHECK_SENSOR_MAGIC(sensor,);
  411. /* First decrement ref count */
  412. if (--sensor->ref_count > 0) {
  413. SDL_UnlockSensors();
  414. return;
  415. }
  416. sensor->driver->Close(sensor);
  417. sensor->hwdata = NULL;
  418. sensorlist = SDL_sensors;
  419. sensorlistprev = NULL;
  420. while (sensorlist) {
  421. if (sensor == sensorlist) {
  422. if (sensorlistprev) {
  423. /* unlink this entry */
  424. sensorlistprev->next = sensorlist->next;
  425. } else {
  426. SDL_sensors = sensor->next;
  427. }
  428. break;
  429. }
  430. sensorlistprev = sensorlist;
  431. sensorlist = sensorlist->next;
  432. }
  433. /* Free the data associated with this sensor */
  434. SDL_free(sensor->name);
  435. SDL_free(sensor);
  436. }
  437. SDL_UnlockSensors();
  438. }
  439. void SDL_QuitSensors(void)
  440. {
  441. int i;
  442. SDL_LockSensors();
  443. /* Stop the event polling */
  444. while (SDL_sensors) {
  445. SDL_sensors->ref_count = 1;
  446. SDL_CloseSensor(SDL_sensors);
  447. }
  448. /* Quit the sensor setup */
  449. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  450. SDL_sensor_drivers[i]->Quit();
  451. }
  452. #ifndef SDL_EVENTS_DISABLED
  453. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  454. #endif
  455. SDL_sensors_initialized = SDL_FALSE;
  456. SDL_UnlockSensors();
  457. }
  458. /* These are global for SDL_syssensor.c and SDL_events.c */
  459. int SDL_SendSensorUpdate(Uint64 timestamp, SDL_Sensor *sensor, Uint64 sensor_timestamp, float *data, int num_values)
  460. {
  461. int posted;
  462. SDL_AssertSensorsLocked();
  463. /* Allow duplicate events, for things like steps and heartbeats */
  464. /* Update internal sensor state */
  465. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  466. SDL_memcpy(sensor->data, data, num_values * sizeof(*data));
  467. /* Post the event, if desired */
  468. posted = 0;
  469. #ifndef SDL_EVENTS_DISABLED
  470. if (SDL_EventEnabled(SDL_EVENT_SENSOR_UPDATE)) {
  471. SDL_Event event;
  472. event.type = SDL_EVENT_SENSOR_UPDATE;
  473. event.common.timestamp = timestamp;
  474. event.sensor.which = sensor->instance_id;
  475. num_values = SDL_min(num_values, SDL_arraysize(event.sensor.data));
  476. SDL_memset(event.sensor.data, 0, sizeof(event.sensor.data));
  477. SDL_memcpy(event.sensor.data, data, num_values * sizeof(*data));
  478. event.sensor.sensor_timestamp = sensor_timestamp;
  479. posted = SDL_PushEvent(&event) == 1;
  480. }
  481. #endif /* !SDL_EVENTS_DISABLED */
  482. SDL_GamepadSensorWatcher(timestamp, sensor->instance_id, sensor_timestamp, data, num_values);
  483. return posted;
  484. }
  485. void SDL_UpdateSensor(SDL_Sensor *sensor)
  486. {
  487. SDL_LockSensors();
  488. {
  489. CHECK_SENSOR_MAGIC(sensor,);
  490. sensor->driver->Update(sensor);
  491. }
  492. SDL_UnlockSensors();
  493. }
  494. void SDL_UpdateSensors(void)
  495. {
  496. int i;
  497. SDL_Sensor *sensor;
  498. if (!SDL_WasInit(SDL_INIT_SENSOR)) {
  499. return;
  500. }
  501. SDL_LockSensors();
  502. for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
  503. sensor->driver->Update(sensor);
  504. }
  505. /* this needs to happen AFTER walking the sensor list above, so that any
  506. dangling hardware data from removed devices can be free'd
  507. */
  508. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  509. SDL_sensor_drivers[i]->Detect();
  510. }
  511. SDL_UnlockSensors();
  512. }