N3DS: Refactor joystick module to avoid globals.
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
diff --git a/src/joystick/n3ds/SDL_sysjoystick.c b/src/joystick/n3ds/SDL_sysjoystick.c
index 1b98462..6525d51 100644
--- a/src/joystick/n3ds/SDL_sysjoystick.c
+++ b/src/joystick/n3ds/SDL_sysjoystick.c
@@ -45,18 +45,10 @@
*/
#define CORRECTION_FACTOR_Y -CORRECTION_FACTOR_X
-typedef struct N3DSJoystickState
-{
- u32 kDown;
- u32 kUp;
- circlePosition circlePos;
- circlePosition cStickPos;
-} N3DSJoystickState;
-
-SDL_FORCE_INLINE void UpdateAxis(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
-SDL_FORCE_INLINE void UpdateButtons(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
-
-static N3DSJoystickState current_state;
+SDL_FORCE_INLINE void UpdateN3DSPressedButtons(SDL_Joystick *joystick);
+SDL_FORCE_INLINE void UpdateN3DSReleasedButtons(SDL_Joystick *joystick);
+SDL_FORCE_INLINE void UpdateN3DSCircle(SDL_Joystick *joystick);
+SDL_FORCE_INLINE void UpdateN3DSCStick(SDL_Joystick *joystick);
static int
N3DS_JoystickInit(void)
@@ -110,64 +102,82 @@ N3DS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
static void
N3DS_JoystickUpdate(SDL_Joystick *joystick)
{
- N3DSJoystickState previous_state = current_state;
+ UpdateN3DSPressedButtons(joystick);
+ UpdateN3DSReleasedButtons(joystick);
+ UpdateN3DSCircle(joystick);
+ UpdateN3DSCStick(joystick);
+}
+
+SDL_FORCE_INLINE void
+UpdateN3DSPressedButtons(SDL_Joystick *joystick)
+{
+ static u32 previous_state = 0;
+ u32 updated_down;
+ u32 current_state = hidKeysDown();
+ updated_down = previous_state ^ current_state;
+ if (updated_down) {
+ for (Uint8 i = 0; i < joystick->nbuttons; i++) {
+ if (current_state & BIT(i) & updated_down) {
+ SDL_PrivateJoystickButton(joystick, i, SDL_PRESSED);
+ }
+ }
+ }
+ previous_state = current_state;
+}
- UpdateAxis(joystick, &previous_state);
- UpdateButtons(joystick, &previous_state);
+SDL_FORCE_INLINE void
+UpdateN3DSReleasedButtons(SDL_Joystick *joystick)
+{
+ static u32 previous_state = 0;
+ u32 updated_up;
+ u32 current_state = hidKeysUp();
+ updated_up = previous_state ^ current_state;
+ if (updated_up) {
+ for (Uint8 i = 0; i < joystick->nbuttons; i++) {
+ if (current_state & BIT(i) & updated_up) {
+ SDL_PrivateJoystickButton(joystick, i, SDL_RELEASED);
+ }
+ }
+ }
+ previous_state = current_state;
}
SDL_FORCE_INLINE void
-UpdateAxis(SDL_Joystick *joystick, N3DSJoystickState *previous_state)
+UpdateN3DSCircle(SDL_Joystick *joystick)
{
- hidCircleRead(¤t_state.circlePos);
- if (previous_state->circlePos.dx != current_state.circlePos.dx) {
+ static circlePosition previous_state = { 0, 0 };
+ circlePosition current_state;
+ hidCircleRead(¤t_state);
+ if (previous_state.dx != current_state.dx) {
SDL_PrivateJoystickAxis(joystick,
0,
- current_state.circlePos.dx * CORRECTION_FACTOR_X);
+ current_state.dx * CORRECTION_FACTOR_X);
}
- if (previous_state->circlePos.dy != current_state.circlePos.dy) {
+ if (previous_state.dy != current_state.dy) {
SDL_PrivateJoystickAxis(joystick,
1,
- current_state.circlePos.dy * CORRECTION_FACTOR_Y);
+ current_state.dy * CORRECTION_FACTOR_Y);
}
+ previous_state = current_state;
+}
- hidCstickRead(¤t_state.cStickPos);
- if (previous_state->cStickPos.dx != current_state.cStickPos.dx) {
+SDL_FORCE_INLINE void
+UpdateN3DSCStick(SDL_Joystick *joystick)
+{
+ static circlePosition previous_state = { 0, 0 };
+ circlePosition current_state;
+ hidCstickRead(¤t_state);
+ if (previous_state.dx != current_state.dx) {
SDL_PrivateJoystickAxis(joystick,
2,
- current_state.cStickPos.dx * CORRECTION_FACTOR_X);
+ current_state.dx * CORRECTION_FACTOR_X);
}
- if (previous_state->cStickPos.dy != current_state.cStickPos.dy) {
+ if (previous_state.dy != current_state.dy) {
SDL_PrivateJoystickAxis(joystick,
3,
- current_state.cStickPos.dy * CORRECTION_FACTOR_Y);
- }
-}
-
-SDL_FORCE_INLINE void
-UpdateButtons(SDL_Joystick *joystick, N3DSJoystickState *previous_state)
-{
- u32 updated_down, updated_up;
-
- current_state.kDown = hidKeysDown();
- updated_down = previous_state->kDown ^ current_state.kDown;
- if (updated_down) {
- for (Uint8 i = 0; i < joystick->nbuttons; i++) {
- if (current_state.kDown & BIT(i) & updated_down) {
- SDL_PrivateJoystickButton(joystick, i, SDL_PRESSED);
- }
- }
- }
-
- current_state.kUp = hidKeysUp();
- updated_up = previous_state->kUp ^ current_state.kUp;
- if (updated_up) {
- for (Uint8 i = 0; i < joystick->nbuttons; i++) {
- if (current_state.kUp & BIT(i) & updated_up) {
- SDL_PrivateJoystickButton(joystick, i, SDL_RELEASED);
- }
- }
+ current_state.dy * CORRECTION_FACTOR_Y);
}
+ previous_state = current_state;
}
static void