SDL_sensor.c 14 KB

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