Atlas - SDL_sysjoystick.c

Home / ext / SDL / src / joystick / ps2 Lines: 1 | Size: 12549 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 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_PS2 24 25// This is the PS2 implementation of the SDL joystick API 26#include <libmtap.h> 27#include <libpad.h> 28#include <ps2_joystick_driver.h> 29 30#include <stdio.h> // For the definition of NULL 31#include <stdlib.h> 32 33#include "../SDL_sysjoystick.h" 34#include "../SDL_joystick_c.h" 35 36#define PS2_MAX_PORT 2 // each ps2 has 2 ports 37#define PS2_MAX_SLOT 4 // maximum - 4 slots in one multitap 38#define MAX_CONTROLLERS (PS2_MAX_PORT * PS2_MAX_SLOT) 39#define PS2_ANALOG_STICKS 2 40#define PS2_ANALOG_AXIS 2 41#define PS2_BUTTONS 16 42#define PS2_TOTAL_AXIS (PS2_ANALOG_STICKS * PS2_ANALOG_AXIS) 43 44struct JoyInfo 45{ 46 uint8_t padBuf[256]; 47 uint16_t btns; 48 uint8_t analog_state[PS2_TOTAL_AXIS]; 49 uint8_t port; 50 uint8_t slot; 51 int8_t rumble_ready; 52 int8_t opened; 53} __attribute__((aligned(64))); 54 55static uint8_t enabled_pads = 0; 56static struct JoyInfo joyInfo[MAX_CONTROLLERS]; 57 58static inline int16_t convert_u8_to_s16(uint8_t val) 59{ 60 if (val == 0) { 61 return -0x7fff; 62 } 63 return val * 0x0101 - 0x8000; 64} 65 66static inline uint8_t rumble_status(uint8_t index) 67{ 68 char actAlign[6]; 69 int res; 70 struct JoyInfo *info = &joyInfo[index]; 71 72 if (info->rumble_ready == 0) { 73 actAlign[0] = 0; 74 actAlign[1] = 1; 75 actAlign[2] = 0xff; 76 actAlign[3] = 0xff; 77 actAlign[4] = 0xff; 78 actAlign[5] = 0xff; 79 80 res = padSetActAlign(info->port, info->slot, actAlign); 81 info->rumble_ready = res <= 0 ? -1 : 1; 82 } 83 84 return info->rumble_ready == 1; 85} 86 87// Function to scan the system for joysticks. 88static bool PS2_JoystickInit(void) 89{ 90 uint32_t port = 0; 91 uint32_t slot = 0; 92 93 if (init_joystick_driver(true) < 0) { 94 return false; 95 } 96 97 for (port = 0; port < PS2_MAX_PORT; port++) { 98 mtapPortOpen(port); 99 } 100 // it can fail - we dont care, we will check it more strictly when padPortOpen 101 102 for (slot = 0; slot < PS2_MAX_SLOT; slot++) { 103 for (port = 0; port < PS2_MAX_PORT; port++) { 104 /* 2 main controller ports acts the same with and without multitap 105 Port 0,0 -> Connector 1 - the same as Port 0 106 Port 1,0 -> Connector 2 - the same as Port 1 107 Port 0,1 -> Connector 3 108 Port 1,1 -> Connector 4 109 Port 0,2 -> Connector 5 110 Port 1,2 -> Connector 6 111 Port 0,3 -> Connector 7 112 Port 1,3 -> Connector 8 113 */ 114 115 struct JoyInfo *info = &joyInfo[enabled_pads]; 116 if (padPortOpen(port, slot, (void *)info->padBuf) > 0) { 117 info->port = (uint8_t)port; 118 info->slot = (uint8_t)slot; 119 info->opened = 1; 120 enabled_pads++; 121 SDL_PrivateJoystickAdded(enabled_pads); 122 } 123 } 124 } 125 126 return (enabled_pads > 0); 127} 128 129// Function to return the number of joystick devices plugged in right now 130static int PS2_JoystickGetCount(void) 131{ 132 return (int)enabled_pads; 133} 134 135// Function to cause any queued joystick insertions to be processed 136static void PS2_JoystickDetect(void) 137{ 138} 139 140static bool PS2_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name) 141{ 142 // We don't override any other drivers 143 return false; 144} 145 146// Function to get the device-dependent name of a joystick 147static const char *PS2_JoystickGetDeviceName(int index) 148{ 149 if (index >= 0 && index < enabled_pads) { 150 return "PS2 Controller"; 151 } 152 153 SDL_SetError("No joystick available with that index"); 154 return NULL; 155} 156 157// Function to get the device-dependent path of a joystick 158static const char *PS2_JoystickGetDevicePath(int index) 159{ 160 return NULL; 161} 162 163// Function to get the Steam virtual gamepad slot of a joystick 164static int PS2_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index) 165{ 166 return -1; 167} 168 169// Function to get the player index of a joystick 170static int PS2_JoystickGetDevicePlayerIndex(int device_index) 171{ 172 return -1; 173} 174 175// Function to set the player index of a joystick 176static void PS2_JoystickSetDevicePlayerIndex(int device_index, int player_index) 177{ 178} 179 180// Function to return the stable GUID for a plugged in device 181static SDL_GUID PS2_JoystickGetDeviceGUID(int device_index) 182{ 183 // the GUID is just the name for now 184 const char *name = PS2_JoystickGetDeviceName(device_index); 185 return SDL_CreateJoystickGUIDForName(name); 186} 187 188// Function to get the current instance id of the joystick located at device_index 189static SDL_JoystickID PS2_JoystickGetDeviceInstanceID(int device_index) 190{ 191 return device_index + 1; 192} 193 194static void PS2_WaitPadReady(int port, int slot) 195{ 196 int state = padGetState(port, slot); 197 while ((state != PAD_STATE_STABLE) && (state != PAD_STATE_FINDCTP1)) { 198 SDL_Delay(1); 199 state = padGetState(port, slot); 200 } 201} 202 203static void PS2_InitializePad(int port, int slot) 204{ 205 int modes; 206 int i; 207 char actAlign[6]; 208 209 PS2_WaitPadReady(port, slot); 210 211 // How many different modes can this device operate in? 212 modes = padInfoMode(port, slot, PAD_MODETABLE, -1); 213 214 // Verify that the controller has a DUAL SHOCK mode 215 for (i = 0; i < modes; i++) { 216 if (padInfoMode(port, slot, PAD_MODETABLE, i) == PAD_TYPE_DUALSHOCK) { 217 break; 218 } 219 } 220 if (i >= modes) { 221 // This is no Dual Shock controller 222 return; 223 } 224 225 // If ExId != 0x0 => This controller has actuator engines 226 // This check should always pass if the Dual Shock test above passed 227 if (!padInfoMode(port, slot, PAD_MODECUREXID, 0)) { 228 // This is no Dual Shock controller?? 229 return; 230 } 231 232 // When using MMODE_LOCK, user cant change mode with Select button 233 padSetMainMode(port, slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK); 234 235 PS2_WaitPadReady(port, slot); 236 padEnterPressMode(port, slot); 237 238 PS2_WaitPadReady(port, slot); 239 if (padInfoAct(port, slot, -1, 0)) { 240 actAlign[0] = 0; // Enable small engine 241 actAlign[1] = 1; // Enable big engine 242 actAlign[2] = 0xff; 243 actAlign[3] = 0xff; 244 actAlign[4] = 0xff; 245 actAlign[5] = 0xff; 246 247 PS2_WaitPadReady(port, slot); 248 padSetActAlign(port, slot, actAlign); 249 } 250 251 PS2_WaitPadReady(port, slot); 252} 253 254/* Function to open a joystick for use. 255 The joystick to open is specified by the device index. 256 This should fill the nbuttons and naxes fields of the joystick structure. 257 It returns 0, or -1 if there is an error. 258*/ 259static bool PS2_JoystickOpen(SDL_Joystick *joystick, int device_index) 260{ 261 int index = joystick->instance_id; 262 struct JoyInfo *info = &joyInfo[index]; 263 264 if (!info->opened) { 265 if (padPortOpen(info->port, info->slot, (void *)info->padBuf) > 0) { 266 info->opened = 1; 267 } else { 268 return false; 269 } 270 } 271 PS2_InitializePad(info->port, info->slot); 272 273 joystick->nbuttons = PS2_BUTTONS; 274 joystick->naxes = PS2_TOTAL_AXIS; 275 joystick->nhats = 0; 276 277 SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true); 278 279 return true; 280} 281 282// Rumble functionality 283static bool PS2_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) 284{ 285 char actAlign[6]; 286 int res; 287 int index = joystick->instance_id; 288 struct JoyInfo *info = &joyInfo[index]; 289 290 if (!rumble_status(index)) { 291 return false; 292 } 293 294 // Initial value 295 actAlign[0] = low_frequency_rumble >> 8; // Enable small engine 296 actAlign[1] = high_frequency_rumble >> 8; // Enable big engine 297 actAlign[2] = 0xff; 298 actAlign[3] = 0xff; 299 actAlign[4] = 0xff; 300 actAlign[5] = 0xff; 301 302 res = padSetActDirect(info->port, info->slot, actAlign); 303 return (res == 1); 304} 305 306// Rumble functionality 307static bool PS2_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left, Uint16 right) 308{ 309 return SDL_Unsupported(); 310} 311 312// LED functionality 313static bool PS2_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue) 314{ 315 return SDL_Unsupported(); 316} 317 318// General effects 319static bool PS2_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size) 320{ 321 return SDL_Unsupported(); 322} 323 324// Sensor functionality 325static bool PS2_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled) 326{ 327 return SDL_Unsupported(); 328} 329 330/* Function to update the state of a joystick - called as a device poll. 331 * This function shouldn't update the joystick structure directly, 332 * but instead should call SDL_PrivateJoystick*() to deliver events 333 * and update joystick device state. 334 */ 335static void PS2_JoystickUpdate(SDL_Joystick *joystick) 336{ 337 uint8_t i; 338 uint8_t previous_axis, current_axis; 339 uint16_t mask, previous, current; 340 struct padButtonStatus buttons; 341 uint8_t all_axis[PS2_TOTAL_AXIS]; 342 int index = joystick->instance_id; 343 struct JoyInfo *info = &joyInfo[index]; 344 int state = padGetState(info->port, info->slot); 345 Uint64 timestamp = SDL_GetTicksNS(); 346 347 if (state != PAD_STATE_DISCONN && state != PAD_STATE_EXECCMD && state != PAD_STATE_ERROR) { 348 int ret = padRead(info->port, info->slot, &buttons); // port, slot, buttons 349 if (ret != 0) { 350 // Buttons 351 int32_t pressed_buttons = 0xffff ^ buttons.btns; 352 ; 353 if (info->btns != pressed_buttons) { 354 for (i = 0; i < PS2_BUTTONS; i++) { 355 mask = (1 << i); 356 previous = info->btns & mask; 357 current = pressed_buttons & mask; 358 if (previous != current) { 359 SDL_SendJoystickButton(timestamp, joystick, i, (current != 0)); 360 } 361 } 362 } 363 info->btns = pressed_buttons; 364 365 // Analog 366 all_axis[0] = buttons.ljoy_h; 367 all_axis[1] = buttons.ljoy_v; 368 all_axis[2] = buttons.rjoy_h; 369 all_axis[3] = buttons.rjoy_v; 370 371 for (i = 0; i < PS2_TOTAL_AXIS; i++) { 372 previous_axis = info->analog_state[i]; 373 current_axis = all_axis[i]; 374 if (previous_axis != current_axis) { 375 SDL_SendJoystickAxis(timestamp, joystick, i, convert_u8_to_s16(current_axis)); 376 } 377 378 info->analog_state[i] = current_axis; 379 } 380 } 381 } 382} 383 384// Function to close a joystick after use 385static void PS2_JoystickClose(SDL_Joystick *joystick) 386{ 387 int index = joystick->instance_id; 388 struct JoyInfo *info = &joyInfo[index]; 389 padPortClose(info->port, info->slot); 390 info->opened = 0; 391} 392 393// Function to perform any system-specific joystick related cleanup 394static void PS2_JoystickQuit(void) 395{ 396 deinit_joystick_driver(true); 397} 398 399static bool PS2_GetGamepadMapping(int device_index, SDL_GamepadMapping *out) 400{ 401 return false; 402} 403 404SDL_JoystickDriver SDL_PS2_JoystickDriver = { 405 PS2_JoystickInit, 406 PS2_JoystickGetCount, 407 PS2_JoystickDetect, 408 PS2_JoystickIsDevicePresent, 409 PS2_JoystickGetDeviceName, 410 PS2_JoystickGetDevicePath, 411 PS2_JoystickGetDeviceSteamVirtualGamepadSlot, 412 PS2_JoystickGetDevicePlayerIndex, 413 PS2_JoystickSetDevicePlayerIndex, 414 PS2_JoystickGetDeviceGUID, 415 PS2_JoystickGetDeviceInstanceID, 416 PS2_JoystickOpen, 417 PS2_JoystickRumble, 418 PS2_JoystickRumbleTriggers, 419 PS2_JoystickSetLED, 420 PS2_JoystickSendEffect, 421 PS2_JoystickSetSensorsEnabled, 422 PS2_JoystickUpdate, 423 PS2_JoystickClose, 424 PS2_JoystickQuit, 425 PS2_GetGamepadMapping, 426}; 427 428#endif // SDL_JOYSTICK_PS2 429
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.