SDL_androidsensor.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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_config.h"
  19. #ifdef SDL_SENSOR_ANDROID
  20. /* This is the system specific header for the SDL sensor API */
  21. #include <android/sensor.h>
  22. #include "SDL_error.h"
  23. #include "SDL_sensor.h"
  24. #include "SDL_androidsensor.h"
  25. #include "../SDL_syssensor.h"
  26. #include "../SDL_sensor_c.h"
  27. //#include "../../core/android/SDL_android.h"
  28. #ifndef LOOPER_ID_USER
  29. #define LOOPER_ID_USER 3
  30. #endif
  31. typedef struct
  32. {
  33. ASensorRef asensor;
  34. SDL_SensorID instance_id;
  35. } SDL_AndroidSensor;
  36. static ASensorManager* SDL_sensor_manager;
  37. static ALooper* SDL_sensor_looper;
  38. static SDL_AndroidSensor *SDL_sensors;
  39. static int SDL_sensors_count;
  40. static int
  41. SDL_ANDROID_SensorInit(void)
  42. {
  43. int i, sensors_count;
  44. ASensorList sensors;
  45. SDL_sensor_manager = ASensorManager_getInstance();
  46. if (!SDL_sensor_manager) {
  47. return SDL_SetError("Couldn't create sensor manager");
  48. }
  49. SDL_sensor_looper = ALooper_forThread();
  50. if (!SDL_sensor_looper) {
  51. SDL_sensor_looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
  52. if (!SDL_sensor_looper) {
  53. return SDL_SetError("Couldn't create sensor event loop");
  54. }
  55. }
  56. /* FIXME: Is the sensor list dynamic? */
  57. sensors_count = ASensorManager_getSensorList(SDL_sensor_manager, &sensors);
  58. if (sensors_count > 0) {
  59. SDL_sensors = (SDL_AndroidSensor *)SDL_calloc(sensors_count, sizeof(*SDL_sensors));
  60. if (!SDL_sensors) {
  61. return SDL_OutOfMemory();
  62. }
  63. for (i = 0; i < sensors_count; ++i) {
  64. SDL_sensors[i].asensor = sensors[i];
  65. SDL_sensors[i].instance_id = SDL_GetNextSensorInstanceID();
  66. }
  67. SDL_sensors_count = sensors_count;
  68. }
  69. return 0;
  70. }
  71. static int
  72. SDL_ANDROID_SensorGetCount(void)
  73. {
  74. return SDL_sensors_count;
  75. }
  76. static void
  77. SDL_ANDROID_SensorDetect(void)
  78. {
  79. }
  80. static const char *
  81. SDL_ANDROID_SensorGetDeviceName(int device_index)
  82. {
  83. return ASensor_getName(SDL_sensors[device_index].asensor);
  84. }
  85. static SDL_SensorType
  86. SDL_ANDROID_SensorGetDeviceType(int device_index)
  87. {
  88. switch (ASensor_getType(SDL_sensors[device_index].asensor)) {
  89. case 0x00000001:
  90. return SDL_SENSOR_ACCEL;
  91. case 0x00000004:
  92. return SDL_SENSOR_GYRO;
  93. default:
  94. return SDL_SENSOR_UNKNOWN;
  95. }
  96. }
  97. static int
  98. SDL_ANDROID_SensorGetDeviceNonPortableType(int device_index)
  99. {
  100. return ASensor_getType(SDL_sensors[device_index].asensor);
  101. }
  102. static SDL_SensorID
  103. SDL_ANDROID_SensorGetDeviceInstanceID(int device_index)
  104. {
  105. return SDL_sensors[device_index].instance_id;
  106. }
  107. static int
  108. SDL_ANDROID_SensorOpen(SDL_Sensor *sensor, int device_index)
  109. {
  110. struct sensor_hwdata *hwdata;
  111. hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata));
  112. if (hwdata == NULL) {
  113. return SDL_OutOfMemory();
  114. }
  115. hwdata->asensor = SDL_sensors[device_index].asensor;
  116. hwdata->eventqueue = ASensorManager_createEventQueue(SDL_sensor_manager, SDL_sensor_looper, LOOPER_ID_USER, NULL, NULL);
  117. if (!hwdata->eventqueue) {
  118. SDL_free(hwdata);
  119. return SDL_SetError("Couldn't create sensor event queue");
  120. }
  121. if (ASensorEventQueue_enableSensor(hwdata->eventqueue, hwdata->asensor) < 0) {
  122. ASensorManager_destroyEventQueue(SDL_sensor_manager, hwdata->eventqueue);
  123. SDL_free(hwdata);
  124. return SDL_SetError("Couldn't enable sensor");
  125. }
  126. /* FIXME: What rate should we set for this sensor? 60 FPS? Let's try the default rate for now... */
  127. sensor->hwdata = hwdata;
  128. return 0;
  129. }
  130. static void
  131. SDL_ANDROID_SensorUpdate(SDL_Sensor *sensor)
  132. {
  133. int events;
  134. ASensorEvent event;
  135. struct android_poll_source* source;
  136. if (ALooper_pollAll(0, NULL, &events, (void**)&source) == LOOPER_ID_USER) {
  137. SDL_zero(event);
  138. while (ASensorEventQueue_getEvents(sensor->hwdata->eventqueue, &event, 1) > 0) {
  139. SDL_PrivateSensorUpdate(sensor, event.data, SDL_arraysize(event.data));
  140. }
  141. }
  142. }
  143. static void
  144. SDL_ANDROID_SensorClose(SDL_Sensor *sensor)
  145. {
  146. if (sensor->hwdata) {
  147. ASensorEventQueue_disableSensor(sensor->hwdata->eventqueue, sensor->hwdata->asensor);
  148. ASensorManager_destroyEventQueue(SDL_sensor_manager, sensor->hwdata->eventqueue);
  149. SDL_free(sensor->hwdata);
  150. sensor->hwdata = NULL;
  151. }
  152. }
  153. static void
  154. SDL_ANDROID_SensorQuit(void)
  155. {
  156. if (SDL_sensors) {
  157. SDL_free(SDL_sensors);
  158. SDL_sensors = NULL;
  159. SDL_sensors_count = 0;
  160. }
  161. }
  162. SDL_SensorDriver SDL_ANDROID_SensorDriver =
  163. {
  164. SDL_ANDROID_SensorInit,
  165. SDL_ANDROID_SensorGetCount,
  166. SDL_ANDROID_SensorDetect,
  167. SDL_ANDROID_SensorGetDeviceName,
  168. SDL_ANDROID_SensorGetDeviceType,
  169. SDL_ANDROID_SensorGetDeviceNonPortableType,
  170. SDL_ANDROID_SensorGetDeviceInstanceID,
  171. SDL_ANDROID_SensorOpen,
  172. SDL_ANDROID_SensorUpdate,
  173. SDL_ANDROID_SensorClose,
  174. SDL_ANDROID_SensorQuit,
  175. };
  176. #endif /* SDL_SENSOR_ANDROID */
  177. /* vi: set ts=4 sw=4 expandtab: */