1
0

SDL_sensor.c 15 KB

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