diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/joystick/psp/SDL_sysjoystick.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/joystick/psp/SDL_sysjoystick.c | 277 |
1 files changed, 277 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/joystick/psp/SDL_sysjoystick.c b/contrib/SDL-3.2.8/src/joystick/psp/SDL_sysjoystick.c new file mode 100644 index 0000000..8a0154c --- /dev/null +++ b/contrib/SDL-3.2.8/src/joystick/psp/SDL_sysjoystick.c | |||
| @@ -0,0 +1,277 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_JOYSTICK_PSP | ||
| 24 | |||
| 25 | // This is the PSP implementation of the SDL joystick API | ||
| 26 | #include <pspctrl.h> | ||
| 27 | |||
| 28 | #include <stdio.h> // For the definition of NULL | ||
| 29 | #include <stdlib.h> | ||
| 30 | |||
| 31 | #include "../SDL_sysjoystick.h" | ||
| 32 | #include "../SDL_joystick_c.h" | ||
| 33 | |||
| 34 | // Current pad state | ||
| 35 | static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 }; | ||
| 36 | static const enum PspCtrlButtons button_map[] = { | ||
| 37 | PSP_CTRL_TRIANGLE, PSP_CTRL_CIRCLE, PSP_CTRL_CROSS, PSP_CTRL_SQUARE, | ||
| 38 | PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER, | ||
| 39 | PSP_CTRL_DOWN, PSP_CTRL_LEFT, PSP_CTRL_UP, PSP_CTRL_RIGHT, | ||
| 40 | PSP_CTRL_SELECT, PSP_CTRL_START, PSP_CTRL_HOME, PSP_CTRL_HOLD | ||
| 41 | }; | ||
| 42 | static int analog_map[256]; // Map analog inputs to -32768 -> 32767 | ||
| 43 | |||
| 44 | // 4 points define the bezier-curve. | ||
| 45 | static SDL_Point a = { 0, 0 }; | ||
| 46 | static SDL_Point b = { 50, 0 }; | ||
| 47 | static SDL_Point c = { 78, 32767 }; | ||
| 48 | static SDL_Point d = { 128, 32767 }; | ||
| 49 | |||
| 50 | // simple linear interpolation between two points | ||
| 51 | static SDL_INLINE void lerp(SDL_Point *dest, const SDL_Point *pt_a, const SDL_Point *pt_b, float t) | ||
| 52 | { | ||
| 53 | dest->x = pt_a->x + (int)((pt_b->x - pt_a->x) * t); | ||
| 54 | dest->y = pt_a->y + (int)((pt_b->y - pt_a->y) * t); | ||
| 55 | } | ||
| 56 | |||
| 57 | // evaluate a point on a bezier-curve. t goes from 0 to 1.0 | ||
| 58 | static int calc_bezier_y(float t) | ||
| 59 | { | ||
| 60 | SDL_Point ab, bc, cd, abbc, bccd, dest; | ||
| 61 | lerp(&ab, &a, &b, t); // point between a and b | ||
| 62 | lerp(&bc, &b, &c, t); // point between b and c | ||
| 63 | lerp(&cd, &c, &d, t); // point between c and d | ||
| 64 | lerp(&abbc, &ab, &bc, t); // point between ab and bc | ||
| 65 | lerp(&bccd, &bc, &cd, t); // point between bc and cd | ||
| 66 | lerp(&dest, &abbc, &bccd, t); // point on the bezier-curve | ||
| 67 | return dest.y; | ||
| 68 | } | ||
| 69 | |||
| 70 | /* Function to scan the system for joysticks. | ||
| 71 | * Joystick 0 should be the system default joystick. | ||
| 72 | * It should return number of joysticks, or -1 on an unrecoverable fatal error. | ||
| 73 | */ | ||
| 74 | static bool PSP_JoystickInit(void) | ||
| 75 | { | ||
| 76 | int i; | ||
| 77 | |||
| 78 | // Setup input | ||
| 79 | sceCtrlSetSamplingCycle(0); | ||
| 80 | sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG); | ||
| 81 | |||
| 82 | /* Create an accurate map from analog inputs (0 to 255) | ||
| 83 | to SDL joystick positions (-32768 to 32767) */ | ||
| 84 | for (i = 0; i < 128; i++) { | ||
| 85 | float t = (float)i / 127.0f; | ||
| 86 | analog_map[i + 128] = calc_bezier_y(t); | ||
| 87 | analog_map[127 - i] = -1 * analog_map[i + 128]; | ||
| 88 | } | ||
| 89 | |||
| 90 | SDL_PrivateJoystickAdded(1); | ||
| 91 | |||
| 92 | return 1; | ||
| 93 | } | ||
| 94 | |||
| 95 | static int PSP_JoystickGetCount(void) | ||
| 96 | { | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | static void PSP_JoystickDetect(void) | ||
| 101 | { | ||
| 102 | } | ||
| 103 | |||
| 104 | static bool PSP_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name) | ||
| 105 | { | ||
| 106 | // We don't override any other drivers | ||
| 107 | return false; | ||
| 108 | } | ||
| 109 | |||
| 110 | // Function to get the device-dependent name of a joystick | ||
| 111 | static const char *PSP_JoystickGetDeviceName(int device_index) | ||
| 112 | { | ||
| 113 | if (device_index == 0) { | ||
| 114 | return "PSP builtin joypad"; | ||
| 115 | } | ||
| 116 | |||
| 117 | SDL_SetError("No joystick available with that index"); | ||
| 118 | return NULL; | ||
| 119 | } | ||
| 120 | |||
| 121 | static const char *PSP_JoystickGetDevicePath(int index) | ||
| 122 | { | ||
| 123 | return NULL; | ||
| 124 | } | ||
| 125 | |||
| 126 | static int PSP_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index) | ||
| 127 | { | ||
| 128 | return -1; | ||
| 129 | } | ||
| 130 | |||
| 131 | static int PSP_JoystickGetDevicePlayerIndex(int device_index) | ||
| 132 | { | ||
| 133 | return -1; | ||
| 134 | } | ||
| 135 | |||
| 136 | static void PSP_JoystickSetDevicePlayerIndex(int device_index, int player_index) | ||
| 137 | { | ||
| 138 | } | ||
| 139 | |||
| 140 | static SDL_GUID PSP_JoystickGetDeviceGUID(int device_index) | ||
| 141 | { | ||
| 142 | // the GUID is just the name for now | ||
| 143 | const char *name = PSP_JoystickGetDeviceName(device_index); | ||
| 144 | return SDL_CreateJoystickGUIDForName(name); | ||
| 145 | } | ||
| 146 | |||
| 147 | // Function to perform the mapping from device index to the instance id for this index | ||
| 148 | static SDL_JoystickID PSP_JoystickGetDeviceInstanceID(int device_index) | ||
| 149 | { | ||
| 150 | return device_index + 1; | ||
| 151 | } | ||
| 152 | |||
| 153 | /* Function to open a joystick for use. | ||
| 154 | The joystick to open is specified by the device index. | ||
| 155 | This should fill the nbuttons and naxes fields of the joystick structure. | ||
| 156 | It returns 0, or -1 if there is an error. | ||
| 157 | */ | ||
| 158 | static bool PSP_JoystickOpen(SDL_Joystick *joystick, int device_index) | ||
| 159 | { | ||
| 160 | joystick->nbuttons = SDL_arraysize(button_map); | ||
| 161 | joystick->naxes = 2; | ||
| 162 | joystick->nhats = 0; | ||
| 163 | |||
| 164 | return true; | ||
| 165 | } | ||
| 166 | |||
| 167 | static bool PSP_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) | ||
| 168 | { | ||
| 169 | return SDL_Unsupported(); | ||
| 170 | } | ||
| 171 | |||
| 172 | static bool PSP_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble) | ||
| 173 | { | ||
| 174 | return SDL_Unsupported(); | ||
| 175 | } | ||
| 176 | |||
| 177 | static bool PSP_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue) | ||
| 178 | { | ||
| 179 | return SDL_Unsupported(); | ||
| 180 | } | ||
| 181 | |||
| 182 | static bool PSP_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size) | ||
| 183 | { | ||
| 184 | return SDL_Unsupported(); | ||
| 185 | } | ||
| 186 | |||
| 187 | static bool PSP_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled) | ||
| 188 | { | ||
| 189 | return SDL_Unsupported(); | ||
| 190 | } | ||
| 191 | |||
| 192 | /* Function to update the state of a joystick - called as a device poll. | ||
| 193 | * This function shouldn't update the joystick structure directly, | ||
| 194 | * but instead should call SDL_PrivateJoystick*() to deliver events | ||
| 195 | * and update joystick device state. | ||
| 196 | */ | ||
| 197 | static void PSP_JoystickUpdate(SDL_Joystick *joystick) | ||
| 198 | { | ||
| 199 | int i; | ||
| 200 | enum PspCtrlButtons buttons; | ||
| 201 | enum PspCtrlButtons changed; | ||
| 202 | unsigned char x, y; | ||
| 203 | static enum PspCtrlButtons old_buttons = 0; | ||
| 204 | static unsigned char old_x = 0, old_y = 0; | ||
| 205 | Uint64 timestamp = SDL_GetTicksNS(); | ||
| 206 | |||
| 207 | if (sceCtrlPeekBufferPositive(&pad, 1) <= 0) { | ||
| 208 | return; | ||
| 209 | } | ||
| 210 | buttons = pad.Buttons; | ||
| 211 | x = pad.Lx; | ||
| 212 | y = pad.Ly; | ||
| 213 | |||
| 214 | // Axes | ||
| 215 | if (old_x != x) { | ||
| 216 | SDL_SendJoystickAxis(timestamp, joystick, 0, analog_map[x]); | ||
| 217 | old_x = x; | ||
| 218 | } | ||
| 219 | if (old_y != y) { | ||
| 220 | SDL_SendJoystickAxis(timestamp, joystick, 1, analog_map[y]); | ||
| 221 | old_y = y; | ||
| 222 | } | ||
| 223 | |||
| 224 | // Buttons | ||
| 225 | changed = old_buttons ^ buttons; | ||
| 226 | old_buttons = buttons; | ||
| 227 | if (changed) { | ||
| 228 | for (i = 0; i < SDL_arraysize(button_map); i++) { | ||
| 229 | if (changed & button_map[i]) { | ||
| 230 | bool down = ((buttons & button_map[i]) != 0); | ||
| 231 | SDL_SendJoystickButton(timestamp, | ||
| 232 | joystick, i, down); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | // Function to close a joystick after use | ||
| 239 | static void PSP_JoystickClose(SDL_Joystick *joystick) | ||
| 240 | { | ||
| 241 | } | ||
| 242 | |||
| 243 | // Function to perform any system-specific joystick related cleanup | ||
| 244 | static void PSP_JoystickQuit(void) | ||
| 245 | { | ||
| 246 | } | ||
| 247 | |||
| 248 | static bool PSP_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out) | ||
| 249 | { | ||
| 250 | return false; | ||
| 251 | } | ||
| 252 | |||
| 253 | SDL_JoystickDriver SDL_PSP_JoystickDriver = { | ||
| 254 | PSP_JoystickInit, | ||
| 255 | PSP_JoystickGetCount, | ||
| 256 | PSP_JoystickDetect, | ||
| 257 | PSP_JoystickIsDevicePresent, | ||
| 258 | PSP_JoystickGetDeviceName, | ||
| 259 | PSP_JoystickGetDevicePath, | ||
| 260 | PSP_JoystickGetDeviceSteamVirtualGamepadSlot, | ||
| 261 | PSP_JoystickGetDevicePlayerIndex, | ||
| 262 | PSP_JoystickSetDevicePlayerIndex, | ||
| 263 | PSP_JoystickGetDeviceGUID, | ||
| 264 | PSP_JoystickGetDeviceInstanceID, | ||
| 265 | PSP_JoystickOpen, | ||
| 266 | PSP_JoystickRumble, | ||
| 267 | PSP_JoystickRumbleTriggers, | ||
| 268 | PSP_JoystickSetLED, | ||
| 269 | PSP_JoystickSendEffect, | ||
| 270 | PSP_JoystickSetSensorsEnabled, | ||
| 271 | PSP_JoystickUpdate, | ||
| 272 | PSP_JoystickClose, | ||
| 273 | PSP_JoystickQuit, | ||
| 274 | PSP_JoystickGetGamepadMapping | ||
| 275 | }; | ||
| 276 | |||
| 277 | #endif // SDL_JOYSTICK_PSP | ||
