SDL_sensor.c 15 KB

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