Atlas - SDL_sysjoystick.c

Home / ext / SDL / src / joystick / ps2 Lines: 1 | Size: 12550 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 struct JoyInfo *info = &joyInfo[device_index]; 262 263 if (!info->opened) { 264 if (padPortOpen(info->port, info->slot, (void *)info->padBuf) > 0) { 265 info->opened = 1; 266 } else { 267 return false; 268 } 269 } 270 PS2_InitializePad(info->port, info->slot); 271 272 joystick->nbuttons = PS2_BUTTONS; 273 joystick->naxes = PS2_TOTAL_AXIS; 274 joystick->nhats = 0; 275 276 SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true); 277 278 return true; 279} 280 281// Rumble functionality 282static bool PS2_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) 283{ 284 char actAlign[6]; 285 int res; 286 int index = (int)(joystick->instance_id - 1); 287 struct JoyInfo *info = &joyInfo[index]; 288 289 if (!rumble_status(index)) { 290 return false; 291 } 292 293 // Initial value 294 actAlign[0] = low_frequency_rumble >> 8; // Enable small engine 295 actAlign[1] = high_frequency_rumble >> 8; // Enable big engine 296 actAlign[2] = 0xff; 297 actAlign[3] = 0xff; 298 actAlign[4] = 0xff; 299 actAlign[5] = 0xff; 300 301 res = padSetActDirect(info->port, info->slot, actAlign); 302 return (res == 1); 303} 304 305// Rumble functionality 306static bool PS2_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left, Uint16 right) 307{ 308 return SDL_Unsupported(); 309} 310 311// LED functionality 312static bool PS2_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue) 313{ 314 return SDL_Unsupported(); 315} 316 317// General effects 318static bool PS2_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size) 319{ 320 return SDL_Unsupported(); 321} 322 323// Sensor functionality 324static bool PS2_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled) 325{ 326 return SDL_Unsupported(); 327} 328 329/* Function to update the state of a joystick - called as a device poll. 330 * This function shouldn't update the joystick structure directly, 331 * but instead should call SDL_PrivateJoystick*() to deliver events 332 * and update joystick device state. 333 */ 334static void PS2_JoystickUpdate(SDL_Joystick *joystick) 335{ 336 uint8_t i; 337 uint8_t previous_axis, current_axis; 338 uint16_t mask, previous, current; 339 struct padButtonStatus buttons; 340 uint8_t all_axis[PS2_TOTAL_AXIS]; 341 int index = (int)(joystick->instance_id - 1); 342 struct JoyInfo *info = &joyInfo[index]; 343 int state = padGetState(info->port, info->slot); 344 Uint64 timestamp = SDL_GetTicksNS(); 345 346 if (state != PAD_STATE_DISCONN && state != PAD_STATE_EXECCMD && state != PAD_STATE_ERROR) { 347 int ret = padRead(info->port, info->slot, &buttons); // port, slot, buttons 348 if (ret != 0) { 349 // Buttons 350 int32_t pressed_buttons = 0xffff ^ buttons.btns; 351 ; 352 if (info->btns != pressed_buttons) { 353 for (i = 0; i < PS2_BUTTONS; i++) { 354 mask = (1 << i); 355 previous = info->btns & mask; 356 current = pressed_buttons & mask; 357 if (previous != current) { 358 SDL_SendJoystickButton(timestamp, joystick, i, (current != 0)); 359 } 360 } 361 } 362 info->btns = pressed_buttons; 363 364 // Analog 365 all_axis[0] = buttons.ljoy_h; 366 all_axis[1] = buttons.ljoy_v; 367 all_axis[2] = buttons.rjoy_h; 368 all_axis[3] = buttons.rjoy_v; 369 370 for (i = 0; i < PS2_TOTAL_AXIS; i++) { 371 previous_axis = info->analog_state[i]; 372 current_axis = all_axis[i]; 373 if (previous_axis != current_axis) { 374 SDL_SendJoystickAxis(timestamp, joystick, i, convert_u8_to_s16(current_axis)); 375 } 376 377 info->analog_state[i] = current_axis; 378 } 379 } 380 } 381} 382 383// Function to close a joystick after use 384static void PS2_JoystickClose(SDL_Joystick *joystick) 385{ 386 int index = (int)(joystick->instance_id - 1); 387 struct JoyInfo *info = &joyInfo[index]; 388 padPortClose(info->port, info->slot); 389 info->opened = 0; 390} 391 392// Function to perform any system-specific joystick related cleanup 393static void PS2_JoystickQuit(void) 394{ 395 deinit_joystick_driver(true); 396} 397 398static bool PS2_GetGamepadMapping(int device_index, SDL_GamepadMapping *out) 399{ 400 return false; 401} 402 403SDL_JoystickDriver SDL_PS2_JoystickDriver = { 404 PS2_JoystickInit, 405 PS2_JoystickGetCount, 406 PS2_JoystickDetect, 407 PS2_JoystickIsDevicePresent, 408 PS2_JoystickGetDeviceName, 409 PS2_JoystickGetDevicePath, 410 PS2_JoystickGetDeviceSteamVirtualGamepadSlot, 411 PS2_JoystickGetDevicePlayerIndex, 412 PS2_JoystickSetDevicePlayerIndex, 413 PS2_JoystickGetDeviceGUID, 414 PS2_JoystickGetDeviceInstanceID, 415 PS2_JoystickOpen, 416 PS2_JoystickRumble, 417 PS2_JoystickRumbleTriggers, 418 PS2_JoystickSetLED, 419 PS2_JoystickSendEffect, 420 PS2_JoystickSetSensorsEnabled, 421 PS2_JoystickUpdate, 422 PS2_JoystickClose, 423 PS2_JoystickQuit, 424 PS2_GetGamepadMapping, 425}; 426 427#endif // SDL_JOYSTICK_PS2 428
[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.