N3DS: Use SDL_Sensor instead of Joystick sensors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
diff --git a/CMakeLists.txt b/CMakeLists.txt
index dd131af..badb861 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2783,6 +2783,13 @@ elseif(N3DS)
set(HAVE_SDL_TIMERS TRUE)
endif()
+ if(SDL_SENSOR)
+ set(SDL_SENSOR_N3DS 1)
+ file(GLOB N3DS_SENSOR_SOURCES ${SDL2_SOURCE_DIR}/src/sensor/n3ds/*.c)
+ list(APPEND SOURCE_FILES ${N3DS_SENSOR_SOURCES})
+ set(HAVE_SDL_SENSORS TRUE)
+ endif()
+
if(SDL_VIDEO)
set(SDL_VIDEO_DRIVER_N3DS 1)
file(GLOB N3DS_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/n3ds/*.c)
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 6dc89a4..6daeadf 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -365,6 +365,7 @@
#cmakedefine SDL_SENSOR_WINDOWS @SDL_SENSOR_WINDOWS@
#cmakedefine SDL_SENSOR_DUMMY @SDL_SENSOR_DUMMY@
#cmakedefine SDL_SENSOR_VITA @SDL_SENSOR_VITA@
+#cmakedefine SDL_SENSOR_N3DS @SDL_SENSOR_N3DS@
/* Enable various shared object loading systems */
#cmakedefine SDL_LOADSO_DLOPEN @SDL_LOADSO_DLOPEN@
diff --git a/src/joystick/n3ds/SDL_sysjoystick.c b/src/joystick/n3ds/SDL_sysjoystick.c
index 3f2d5a2..1b98462 100644
--- a/src/joystick/n3ds/SDL_sysjoystick.c
+++ b/src/joystick/n3ds/SDL_sysjoystick.c
@@ -51,23 +51,17 @@ typedef struct N3DSJoystickState
u32 kUp;
circlePosition circlePos;
circlePosition cStickPos;
- accelVector acceleration;
- angularRate rate;
} N3DSJoystickState;
SDL_FORCE_INLINE void UpdateAxis(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
SDL_FORCE_INLINE void UpdateButtons(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
-SDL_FORCE_INLINE void UpdateSensors(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
static N3DSJoystickState current_state;
-static SDL_bool sensors_enabled = SDL_FALSE;
static int
N3DS_JoystickInit(void)
{
hidInit();
- HIDUSER_EnableAccelerometer();
- HIDUSER_EnableGyroscope();
return 0;
}
@@ -104,17 +98,13 @@ N3DS_JoystickOpen(SDL_Joystick *joystick, int device_index)
joystick->nhats = 0;
joystick->instance_id = device_index;
- SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 0.0f);
- SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 0.0f);
-
return 0;
}
static int
N3DS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{
- sensors_enabled = enabled;
- return 0;
+ return SDL_Unsupported();
}
static void
@@ -124,10 +114,6 @@ N3DS_JoystickUpdate(SDL_Joystick *joystick)
UpdateAxis(joystick, &previous_state);
UpdateButtons(joystick, &previous_state);
-
- if (sensors_enabled) {
- UpdateSensors(joystick, &previous_state);
- }
}
SDL_FORCE_INLINE void
@@ -184,30 +170,6 @@ UpdateButtons(SDL_Joystick *joystick, N3DSJoystickState *previous_state)
}
}
-SDL_FORCE_INLINE void
-UpdateSensors(SDL_Joystick *joystick, N3DSJoystickState *previous_state)
-{
- float data[3];
-
- hidAccelRead(¤t_state.acceleration);
- if (SDL_memcmp(&previous_state->acceleration, ¤t_state.acceleration, sizeof(accelVector)) != 0) {
- SDL_memcpy(&previous_state->acceleration, ¤t_state.acceleration, sizeof(accelVector));
- data[0] = (float) current_state.acceleration.x * SDL_STANDARD_GRAVITY;
- data[1] = (float) current_state.acceleration.y * SDL_STANDARD_GRAVITY;
- data[2] = (float) current_state.acceleration.z * SDL_STANDARD_GRAVITY;
- SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_ACCEL, data, sizeof data);
- }
-
- hidGyroRead(¤t_state.rate);
- if (SDL_memcmp(&previous_state->rate, ¤t_state.rate, sizeof(angularRate)) != 0) {
- SDL_memcpy(&previous_state->rate, ¤t_state.rate, sizeof(angularRate));
- data[0] = (float) current_state.rate.y;
- data[1] = (float) current_state.rate.z;
- data[2] = (float) current_state.rate.x;
- SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_GYRO, data, sizeof data);
- }
-}
-
static void
N3DS_JoystickClose(SDL_Joystick *joystick)
{
@@ -216,8 +178,6 @@ N3DS_JoystickClose(SDL_Joystick *joystick)
static void
N3DS_JoystickQuit(void)
{
- HIDUSER_DisableGyroscope();
- HIDUSER_DisableAccelerometer();
hidExit();
}
diff --git a/src/sensor/SDL_sensor.c b/src/sensor/SDL_sensor.c
index ba86528..6db4c63 100644
--- a/src/sensor/SDL_sensor.c
+++ b/src/sensor/SDL_sensor.c
@@ -41,12 +41,15 @@ static SDL_SensorDriver *SDL_sensor_drivers[] = {
#ifdef SDL_SENSOR_WINDOWS
&SDL_WINDOWS_SensorDriver,
#endif
+#ifdef SDL_SENSOR_VITA
+ &SDL_VITA_SensorDriver,
+#endif
+#ifdef SDL_SENSOR_N3DS
+ &SDL_N3DS_SensorDriver,
+#endif
#if defined(SDL_SENSOR_DUMMY) || defined(SDL_SENSOR_DISABLED)
&SDL_DUMMY_SensorDriver
#endif
-#if defined(SDL_SENSOR_VITA)
- &SDL_VITA_SensorDriver
-#endif
};
static SDL_Sensor *SDL_sensors = NULL;
static SDL_bool SDL_updating_sensor = SDL_FALSE;
diff --git a/src/sensor/SDL_syssensor.h b/src/sensor/SDL_syssensor.h
index 6e601a2..30d4400 100644
--- a/src/sensor/SDL_syssensor.h
+++ b/src/sensor/SDL_syssensor.h
@@ -102,6 +102,7 @@ extern SDL_SensorDriver SDL_COREMOTION_SensorDriver;
extern SDL_SensorDriver SDL_WINDOWS_SensorDriver;
extern SDL_SensorDriver SDL_DUMMY_SensorDriver;
extern SDL_SensorDriver SDL_VITA_SensorDriver;
+extern SDL_SensorDriver SDL_N3DS_SensorDriver;
#endif /* SDL_syssensor_h_ */
diff --git a/src/sensor/n3ds/SDL_n3dssensor.c b/src/sensor/n3ds/SDL_n3dssensor.c
new file mode 100644
index 0000000..7204fcc
--- /dev/null
+++ b/src/sensor/n3ds/SDL_n3dssensor.c
@@ -0,0 +1,218 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "../../SDL_internal.h"
+
+#ifdef SDL_SENSOR_N3DS
+
+#include <3ds.h>
+
+#include "../SDL_syssensor.h"
+
+/* 1 accelerometer and 1 gyroscope */
+#define N3DS_SENSOR_COUNT 2
+
+typedef struct
+{
+ SDL_SensorType type;
+ SDL_SensorID instance_id;
+} SDL_N3DSSensor;
+
+static SDL_N3DSSensor N3DS_sensors[N3DS_SENSOR_COUNT];
+
+SDL_FORCE_INLINE int InitN3DSServices(void);
+SDL_FORCE_INLINE void UpdateN3DSAccelerometer(SDL_Sensor *sensor);
+SDL_FORCE_INLINE void UpdateN3DSGyroscope(SDL_Sensor *sensor);
+
+SDL_FORCE_INLINE SDL_bool
+IsDeviceIndexValid(int device_index)
+{
+ return device_index >= 0 && device_index < N3DS_SENSOR_COUNT;
+}
+
+static int
+N3DS_SensorInit(void)
+{
+ if (InitN3DSServices() < 0) {
+ return SDL_SetError("Failed to initialise N3DS services");
+ }
+
+ N3DS_sensors[0].type = SDL_SENSOR_ACCEL;
+ N3DS_sensors[0].instance_id = SDL_GetNextSensorInstanceID();
+ N3DS_sensors[1].type = SDL_SENSOR_GYRO;
+ N3DS_sensors[1].instance_id = SDL_GetNextSensorInstanceID();
+ return 0;
+}
+
+SDL_FORCE_INLINE int
+InitN3DSServices(void)
+{
+ if (R_FAILED(hidInit())) {
+ return -1;
+ }
+
+ if (R_FAILED(HIDUSER_EnableAccelerometer())) {
+ return -1;
+ }
+
+ if (R_FAILED(HIDUSER_EnableGyroscope())) {
+ return -1;
+ }
+ return 0;
+}
+
+static int
+N3DS_SensorGetCount(void)
+{
+ return N3DS_SENSOR_COUNT;
+}
+
+static void
+N3DS_SensorDetect(void)
+{
+}
+
+static const char *
+N3DS_SensorGetDeviceName(int device_index)
+{
+ if (IsDeviceIndexValid(device_index)) {
+ switch (N3DS_sensors[device_index].type) {
+ case SDL_SENSOR_ACCEL:
+ return "Accelerometer";
+ case SDL_SENSOR_GYRO:
+ return "Gyroscope";
+ default:
+ return "Unknown";
+ }
+ }
+
+ return NULL;
+}
+
+static SDL_SensorType
+N3DS_SensorGetDeviceType(int device_index)
+{
+ if (IsDeviceIndexValid(device_index)) {
+ return N3DS_sensors[device_index].type;
+ }
+ return SDL_SENSOR_INVALID;
+}
+
+static int
+N3DS_SensorGetDeviceNonPortableType(int device_index)
+{
+ return (int) N3DS_SensorGetDeviceType(device_index);
+}
+
+static SDL_SensorID
+N3DS_SensorGetDeviceInstanceID(int device_index)
+{
+ if (IsDeviceIndexValid(device_index)) {
+ return N3DS_sensors[device_index].instance_id;
+ }
+ return -1;
+}
+
+static int
+N3DS_SensorOpen(SDL_Sensor *sensor, int device_index)
+{
+ return 0;
+}
+
+static void
+N3DS_SensorUpdate(SDL_Sensor *sensor)
+{
+ switch (sensor->type) {
+ case SDL_SENSOR_ACCEL:
+ UpdateN3DSAccelerometer(sensor);
+ break;
+ case SDL_SENSOR_GYRO:
+ UpdateN3DSGyroscope(sensor);
+ break;
+ default:
+ break;
+ }
+}
+
+SDL_FORCE_INLINE void
+UpdateN3DSAccelerometer(SDL_Sensor *sensor)
+{
+ static accelVector previous_state = { 0, 0, 0 };
+ accelVector current_state;
+ float data[3];
+
+ hidAccelRead(¤t_state);
+ if (SDL_memcmp(&previous_state, ¤t_state, sizeof(accelVector)) != 0) {
+ SDL_memcpy(&previous_state, ¤t_state, sizeof(accelVector));
+ data[0] = (float) current_state.x * SDL_STANDARD_GRAVITY;
+ data[1] = (float) current_state.y * SDL_STANDARD_GRAVITY;
+ data[2] = (float) current_state.z * SDL_STANDARD_GRAVITY;
+ SDL_PrivateSensorUpdate(sensor, data, sizeof data);
+ }
+}
+
+SDL_FORCE_INLINE void
+UpdateN3DSGyroscope(SDL_Sensor *sensor)
+{
+ static angularRate previous_state = { 0, 0, 0 };
+ angularRate current_state;
+ float data[3];
+
+ hidGyroRead(¤t_state);
+ if (SDL_memcmp(&previous_state, ¤t_state, sizeof(angularRate)) != 0) {
+ SDL_memcpy(&previous_state, ¤t_state, sizeof(angularRate));
+ data[0] = (float) current_state.x;
+ data[1] = (float) current_state.y;
+ data[2] = (float) current_state.z;
+ SDL_PrivateSensorUpdate(sensor, data, sizeof data);
+ }
+}
+
+static void
+N3DS_SensorClose(SDL_Sensor *sensor)
+{
+}
+
+static void
+N3DS_SensorQuit(void)
+{
+ HIDUSER_DisableGyroscope();
+ HIDUSER_DisableAccelerometer();
+ hidExit();
+}
+
+SDL_SensorDriver SDL_N3DS_SensorDriver = {
+ N3DS_SensorInit,
+ N3DS_SensorGetCount,
+ N3DS_SensorDetect,
+ N3DS_SensorGetDeviceName,
+ N3DS_SensorGetDeviceType,
+ N3DS_SensorGetDeviceNonPortableType,
+ N3DS_SensorGetDeviceInstanceID,
+ N3DS_SensorOpen,
+ N3DS_SensorUpdate,
+ N3DS_SensorClose,
+ N3DS_SensorQuit,
+};
+
+#endif /* SDL_SENSOR_N3DS */
+
+/* vi: set ts=4 sw=4 expandtab: */