Sensor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright (c) 2006-2022 LOVE Development Team
  3. *
  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. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Sensor.h"
  22. // SDL
  23. #include <SDL.h>
  24. #include <SDL_sensor.h>
  25. namespace love
  26. {
  27. namespace sensor
  28. {
  29. namespace sdl
  30. {
  31. Sensor::Sensor()
  32. : sensors()
  33. {
  34. if (SDL_InitSubSystem(SDL_INIT_SENSOR) < 0)
  35. throw love::Exception("Could not initialize SDL sensor subsystem (%s)", SDL_GetError());
  36. }
  37. Sensor::~Sensor()
  38. {
  39. SDL_QuitSubSystem(SDL_INIT_SENSOR);
  40. }
  41. const char *Sensor::getName() const
  42. {
  43. return "love.sensor.sdl";
  44. }
  45. bool Sensor::isAvailable(SensorType type)
  46. {
  47. for (int i = 0; i < SDL_NumSensors(); i++)
  48. {
  49. if (convert(SDL_SensorGetDeviceType(i)) == type)
  50. return true;
  51. }
  52. return false;
  53. }
  54. bool Sensor::isEnabled(SensorType type)
  55. {
  56. return sensors[type]; // nullptr is default
  57. }
  58. void Sensor::setEnabled(SensorType type, bool enable)
  59. {
  60. if (sensors[type] && !enable)
  61. {
  62. SDL_SensorClose(sensors[type]);
  63. sensors[type] = nullptr;
  64. }
  65. else if (sensors[type] == nullptr && enable)
  66. {
  67. for (int i = 0; i < SDL_NumSensors(); i++)
  68. {
  69. if (convert(SDL_SensorGetDeviceType(i)) == type)
  70. {
  71. SDL_Sensor *sensorHandle = SDL_SensorOpen(i);
  72. if (sensorHandle == nullptr)
  73. {
  74. const char *name = nullptr;
  75. getConstant(type, name);
  76. throw love::Exception("Could not open \"%s\" SDL sensor (%s)", name, SDL_GetError());
  77. }
  78. sensors[type] = sensorHandle;
  79. }
  80. }
  81. }
  82. }
  83. std::vector<float> Sensor::getData(SensorType type)
  84. {
  85. if (sensors[type] == nullptr)
  86. {
  87. const char *name = nullptr;
  88. getConstant(type, name);
  89. throw love::Exception("\"%s\" sensor is not enabled", name);
  90. }
  91. std::vector<float> values;
  92. values.resize(3);
  93. SDL_SensorGetData(sensors[type], values.data(), 3);
  94. return values;
  95. }
  96. std::vector<void*> Sensor::getHandles()
  97. {
  98. std::vector<void*> nativeSensor;
  99. for (const std::pair<SensorType, SDL_Sensor*> &data: sensors)
  100. {
  101. if (data.second)
  102. nativeSensor.push_back(data.second);
  103. }
  104. return nativeSensor;
  105. }
  106. const char *Sensor::getSensorName(SensorType type)
  107. {
  108. if (sensors[type] == nullptr)
  109. {
  110. const char *name = nullptr;
  111. getConstant(type, name);
  112. throw love::Exception("\"%s\" sensor is not enabled", name);
  113. }
  114. return SDL_SensorGetName(sensors[type]);
  115. }
  116. Sensor::SensorType Sensor::convert(SDL_SensorType type)
  117. {
  118. switch (type)
  119. {
  120. case SDL_SENSOR_ACCEL:
  121. return SENSOR_ACCELEROMETER;
  122. case SDL_SENSOR_GYRO:
  123. return SENSOR_GYROSCOPE;
  124. default:
  125. return SENSOR_MAX_ENUM;
  126. }
  127. }
  128. }
  129. }
  130. }