SDL_sensor.c 14 KB

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