Commit b733e38c3ab8493d8572d7459f6225b6b48587ec

Sam Lantinga 2022-06-29T17:25:20

Merge commit '7fd46ec581d45ec3fea3fcfe226bbc2ae824fc7c' into main

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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b82f735..1f42cf5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2539,7 +2539,7 @@ elseif(PSP)
   endif(NOT SDL2_DISABLE_SDL2MAIN)
 
 elseif(PS2)
-  list(APPEND EXTRA_CFLAGS "-DPS2" "-D__PS2__" "-I${PS2SDK}/ports/include")
+  list(APPEND EXTRA_CFLAGS "-DPS2" "-D__PS2__" "-I$ENV{PS2SDK}/ports/include")
 
   file(GLOB PS2_MAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/ps2/*.c)
   set(SDLMAIN_SOURCES ${SDLMAIN_SOURCES} ${PS2_MAIN_SOURCES})
@@ -2550,6 +2550,12 @@ elseif(PS2)
     list(APPEND SOURCE_FILES ${PS2_FILESYSTEM_SOURCES})
     set(HAVE_SDL_FILESYSTEM TRUE)
   endif()
+  if(SDL_JOYSTICK)
+    set(SDL_JOYSTICK_PS2 1)
+    file(GLOB PS2_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/ps2/*.c)
+    list(APPEND SOURCE_FILES ${PS2_JOYSTICK_SOURCES})
+    set(HAVE_SDL_JOYSTICK TRUE)
+  endif()
   if(SDL_THREADS)
     set(SDL_THREAD_PS2 1)
     file(GLOB PS2_THREAD_SOURCES ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_systls.c ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_sysmutex.c ${SDL2_SOURCE_DIR}/src/thread/ps2/*.c)
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 9cc18ab..129e808 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -348,6 +348,7 @@
 #cmakedefine SDL_JOYSTICK_VIRTUAL @SDL_JOYSTICK_VIRTUAL@
 #cmakedefine SDL_JOYSTICK_VITA @SDL_JOYSTICK_VITA@
 #cmakedefine SDL_JOYSTICK_PSP @SDL_JOYSTICK_PSP@
+#cmakedefine SDL_JOYSTICK_PS2 @SDL_JOYSTICK_PS2@
 #cmakedefine SDL_HAPTIC_DUMMY @SDL_HAPTIC_DUMMY@
 #cmakedefine SDL_HAPTIC_LINUX @SDL_HAPTIC_LINUX@
 #cmakedefine SDL_HAPTIC_IOKIT @SDL_HAPTIC_IOKIT@
diff --git a/src/joystick/SDL_gamecontrollerdb.h b/src/joystick/SDL_gamecontrollerdb.h
index 9266c51..aa9d357 100644
--- a/src/joystick/SDL_gamecontrollerdb.h
+++ b/src/joystick/SDL_gamecontrollerdb.h
@@ -914,6 +914,9 @@ static const char *s_ControllerMappings [] =
 #if defined(SDL_JOYSTICK_PSP)
     "505350206275696c74696e206a6f7970,PSP builtin joypad,a:b2,b:b1,back:b10,dpdown:b6,dpleft:b7,dpright:b9,dpup:b8,leftshoulder:b4,leftx:a0,lefty:a1,rightshoulder:b5,rightx:a2,righty:a3,start:b11,x:b3,y:b0,",
 #endif
+#if defined(SDL_JOYSTICK_PS2)
+    "50533220436f6e74726f6c6c65720000,PS2 Controller,a:b14,b:b13,back:b0,dpdown:b6,dpleft:b7,dpright:b5,dpup:b4,leftshoulder:b10,leftx:a0,lefty:a1,rightshoulder:b9,rightx:a2,righty:a3,start:b3,x:b15,y:b12,leftstick:b1,rightstick:b2,",
+#endif
     "hidapi,*,a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,",
     NULL
 };
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 32f03ff..bfb2046 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -89,6 +89,9 @@ static SDL_JoystickDriver *SDL_joystick_drivers[] = {
 #ifdef SDL_JOYSTICK_OS2
     &SDL_OS2_JoystickDriver,
 #endif
+#ifdef SDL_JOYSTICK_PS2
+    &SDL_PS2_JoystickDriver,
+#endif
 #ifdef SDL_JOYSTICK_PSP
     &SDL_PSP_JoystickDriver,
 #endif
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index 6fd82bd..4215f4a 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -155,7 +155,7 @@ typedef struct _SDL_JoystickDriver
     /* Function to get the player index of a joystick */
     int (*GetDevicePlayerIndex)(int device_index);
 
-    /* Function to get the player index of a joystick */
+    /* Function to set the player index of a joystick */
     void (*SetDevicePlayerIndex)(int device_index, int player_index);
 
     /* Function to return the stable GUID for a plugged in device */
@@ -226,6 +226,7 @@ extern SDL_JoystickDriver SDL_WGI_JoystickDriver;
 extern SDL_JoystickDriver SDL_WINDOWS_JoystickDriver;
 extern SDL_JoystickDriver SDL_WINMM_JoystickDriver;
 extern SDL_JoystickDriver SDL_OS2_JoystickDriver;
+extern SDL_JoystickDriver SDL_PS2_JoystickDriver;
 extern SDL_JoystickDriver SDL_PSP_JoystickDriver;
 extern SDL_JoystickDriver SDL_VITA_JoystickDriver;
 
diff --git a/src/joystick/ps2/SDL_sysjoystick.c b/src/joystick/ps2/SDL_sysjoystick.c
new file mode 100644
index 0000000..d30b3c1
--- /dev/null
+++ b/src/joystick/ps2/SDL_sysjoystick.c
@@ -0,0 +1,348 @@
+/*
+  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"
+
+#if SDL_JOYSTICK_PS2
+
+/* This is the PS2 implementation of the SDL joystick API */
+#include <libmtap.h>
+#include <libpad.h>
+#include <ps2_joystick_driver.h>
+
+#include <stdio.h>      /* For the definition of NULL */
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include "../SDL_sysjoystick.h"
+#include "../SDL_joystick_c.h"
+
+#include "SDL_events.h"
+#include "SDL_error.h"
+
+#define PS2_MAX_PORT 2 /* each ps2 has 2 ports */
+#define PS2_MAX_SLOT 4 /* maximum - 4 slots in one multitap */
+#define MAX_CONTROLLERS (PS2_MAX_PORT * PS2_MAX_SLOT)
+#define PS2_ANALOG_STICKS 2
+#define PS2_ANALOG_AXIS 2
+#define PS2_BUTTONS 16
+#define PS2_TOTAL_AXIS (PS2_ANALOG_STICKS * PS2_ANALOG_AXIS)
+
+struct JoyInfo {
+    uint8_t padBuf[256];
+    uint16_t btns;
+    uint8_t analog_state[PS2_TOTAL_AXIS];
+    uint8_t port;
+    uint8_t slot;
+    int8_t rumble_ready;
+} __attribute__ ((aligned (64)));
+
+static uint8_t enabled_pads = 0;
+static struct JoyInfo joyInfo[MAX_CONTROLLERS];
+
+static inline int16_t convert_u8_to_s16(uint8_t val)
+{
+   if (val == 0)
+      return -0x7fff;
+   return val * 0x0101 - 0x8000;
+}
+
+static inline uint8_t rumble_status(uint8_t index) 
+{
+    char actAlign[6];
+    int res;
+    struct JoyInfo *info = &joyInfo[index];
+
+    if (info->rumble_ready == 0) {
+        actAlign[0] = 0;
+        actAlign[1] = 1;
+        actAlign[2] = 0xff;
+        actAlign[3] = 0xff;
+        actAlign[4] = 0xff;
+        actAlign[5] = 0xff;
+        
+        res = padSetActAlign(info->port, info->slot, actAlign);
+        info->rumble_ready = res <= 0 ? -1 : 1;
+    }
+
+    return info->rumble_ready == 1;
+}
+
+/* Function to scan the system for joysticks.
+*  Joystick 0 should be the system default joystick.
+*  This function should return 0, or -1 on an unrecoverable error.
+*/
+static int PS2_JoystickInit(void)
+{
+    uint32_t port = 0;
+    uint32_t slot = 0;
+
+    if(init_joystick_driver(true) < 0)
+        return -1;
+
+   for (port = 0; port < PS2_MAX_PORT; port++)
+        mtapPortOpen(port);
+    /* it can fail - we dont care, we will check it more strictly when padPortOpen */
+
+    for (slot = 0; slot < PS2_MAX_SLOT; slot++) {
+        for (port = 0; port < PS2_MAX_PORT; port++) {
+            /* 2 main controller ports acts the same with and without multitap
+            Port 0,0 -> Connector 1 - the same as Port 0
+            Port 1,0 -> Connector 2 - the same as Port 1
+            Port 0,1 -> Connector 3
+            Port 1,1 -> Connector 4
+            Port 0,2 -> Connector 5
+            Port 1,2 -> Connector 6
+            Port 0,3 -> Connector 7
+            Port 1,3 -> Connector 8
+            */
+
+            struct JoyInfo *info = &joyInfo[enabled_pads];
+            if(padPortOpen(port, slot, (void *)info->padBuf) > 0) {
+                info->port = (uint8_t)port;
+                info->slot = (uint8_t)slot;
+                enabled_pads++;
+            }
+        }
+    }
+
+    return enabled_pads > 0 ? 0 : -1;
+}
+
+/* Function to return the number of joystick devices plugged in right now */
+static int PS2_JoystickGetCount()
+{
+    return (int)enabled_pads;
+}
+
+/* Function to cause any queued joystick insertions to be processed */
+static void PS2_JoystickDetect()
+{
+}
+
+/* Function to get the device-dependent name of a joystick */
+static const char *PS2_JoystickGetDeviceName(int index)
+{
+    if (index >= 0 && index < enabled_pads)
+        return "PS2 Controller";
+
+    SDL_SetError("No joystick available with that index");
+    return NULL;
+}
+
+/* Function to get the device-dependent path of a joystick */
+static const char *PS2_JoystickGetDevicePath(int index)
+{
+    return NULL;
+}
+
+/* Function to get the player index of a joystick */
+static int PS2_JoystickGetDevicePlayerIndex(int device_index)
+{
+    return -1;
+}
+
+/* Function to set the player index of a joystick */
+static void PS2_JoystickSetDevicePlayerIndex(int device_index, int player_index)
+{
+}
+
+/* Function to return the stable GUID for a plugged in device */
+static SDL_JoystickGUID PS2_JoystickGetDeviceGUID( int device_index )
+{
+    SDL_JoystickGUID guid;
+    /* the GUID is just the first 16 chars of the name for now */
+    const char *name = PS2_JoystickGetDeviceName(device_index);
+    SDL_zero(guid);
+    SDL_memcpy(&guid, name, SDL_min(sizeof(guid), SDL_strlen(name)));
+    return guid;
+}
+
+/* Function to get the current instance id of the joystick located at device_index */
+static SDL_JoystickID PS2_JoystickGetDeviceInstanceID(int device_index)
+{
+    return device_index;
+}
+
+/*  Function to open a joystick for use.
+    The joystick to open is specified by the device index.
+    This should fill the nbuttons and naxes fields of the joystick structure.
+    It returns 0, or -1 if there is an error.
+*/
+static int PS2_JoystickOpen(SDL_Joystick *joystick, int device_index)
+{
+    joystick->nbuttons = PS2_BUTTONS;
+    joystick->naxes = PS2_TOTAL_AXIS;
+    joystick->nhats = 0;
+    joystick->instance_id = device_index;
+    
+    return 0;
+}
+
+/* Rumble functionality */
+static int PS2_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
+{
+    char actAlign[6];
+    int res;
+    int index = joystick->instance_id;
+    struct JoyInfo *info = &joyInfo[index];
+
+    if (!rumble_status(index)) {
+        return -1;
+    }
+
+    // Initial value
+    actAlign[0] = low_frequency_rumble >> 8;   // Enable small engine
+    actAlign[1] = high_frequency_rumble >> 8;   // Enable big engine
+    actAlign[2] = 0xff;
+    actAlign[3] = 0xff;
+    actAlign[4] = 0xff;
+    actAlign[5] = 0xff;
+    
+    res = padSetActDirect(info->port, info->slot, actAlign);
+    return res == 1 ? 0 : -1;
+}
+
+/* Rumble functionality */
+static int PS2_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left, Uint16 right)
+{
+    return -1;
+}
+
+/* Capability detection */
+static Uint32 PS2_JoystickGetCapabilities(SDL_Joystick *joystick)
+{
+    return  SDL_JOYCAP_RUMBLE;
+}
+
+/* LED functionality */
+static int PS2_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
+{
+    return -1;
+}
+
+/* General effects */
+static int PS2_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
+{
+    return -1;
+}
+
+/* Sensor functionality */
+static int PS2_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
+{
+    return -1;
+}
+
+/*  Function to update the state of a joystick - called as a device poll.
+*   This function shouldn't update the joystick structure directly,
+*   but instead should call SDL_PrivateJoystick*() to deliver events
+*   and update joystick device state.
+*/
+static void PS2_JoystickUpdate(SDL_Joystick *joystick)
+{
+    uint8_t i;
+    uint8_t previous_axis, current_axis;
+    uint16_t mask, previous, current;
+    struct padButtonStatus buttons;
+    uint8_t all_axis[PS2_TOTAL_AXIS];
+    int index = joystick->instance_id;
+    struct JoyInfo *info = &joyInfo[index];
+    int state = padGetState(info->port, info->slot);
+
+    if (state != PAD_STATE_DISCONN || state != PAD_STATE_EXECCMD || state != PAD_STATE_ERROR) {
+        int ret = padRead(info->port, info->slot, &buttons); /* port, slot, buttons */
+        if (ret != 0) {
+            /* Buttons */
+            int32_t pressed_buttons = 0xffff ^ buttons.btns;;
+            if (info->btns != pressed_buttons) {
+                for (i = 0; i < PS2_BUTTONS; i++) {
+                    mask = (1 << i);
+                    previous = info->btns & mask;
+                    current = pressed_buttons & mask;
+                    if (previous != current)
+                        SDL_PrivateJoystickButton(joystick, i, current ? SDL_PRESSED : SDL_RELEASED);
+                }
+            }
+            info->btns = pressed_buttons;
+
+            /* Analog */
+            all_axis[0] = buttons.ljoy_h;
+            all_axis[1] = buttons.ljoy_v;
+            all_axis[2] = buttons.rjoy_h;
+            all_axis[3] = buttons.rjoy_v;
+
+            for (i = 0; i < PS2_TOTAL_AXIS; i++) {
+                previous_axis = info->analog_state[i];
+                current_axis = all_axis[i];
+                if (previous_axis != current_axis)
+                    SDL_PrivateJoystickAxis(joystick, i, convert_u8_to_s16(current_axis));
+                
+                info->analog_state[i] = current_axis;
+            }
+        }
+    }
+}
+
+/* Function to close a joystick after use */
+static void PS2_JoystickClose(SDL_Joystick *joystick)
+{
+    int index = joystick->instance_id;
+    struct JoyInfo *info = &joyInfo[index];
+    padPortClose(info->port, info->slot);
+}
+
+/* Function to perform any system-specific joystick related cleanup */
+static void PS2_JoystickQuit(void)
+{
+    deinit_joystick_driver(true);
+}
+
+static SDL_bool PS2_GetGamepadMapping(int device_index, SDL_GamepadMapping * out)
+{
+    return SDL_FALSE;
+}
+
+SDL_JoystickDriver SDL_PS2_JoystickDriver =
+{
+    PS2_JoystickInit,
+    PS2_JoystickGetCount,
+    PS2_JoystickDetect,
+    PS2_JoystickGetDeviceName,
+    PS2_JoystickGetDevicePath,
+    PS2_JoystickGetDevicePlayerIndex,
+    PS2_JoystickSetDevicePlayerIndex,
+    PS2_JoystickGetDeviceGUID,
+    PS2_JoystickGetDeviceInstanceID,
+    PS2_JoystickOpen,
+    PS2_JoystickRumble,
+    PS2_JoystickRumbleTriggers,
+    PS2_JoystickGetCapabilities,
+    PS2_JoystickSetLED,
+    PS2_JoystickSendEffect,
+    PS2_JoystickSetSensorsEnabled,
+    PS2_JoystickUpdate,
+    PS2_JoystickClose,
+    PS2_JoystickQuit,
+    PS2_GetGamepadMapping,
+};
+
+#endif /* SDL_JOYSTICK_PS2 */
+
+/* vi: set ts=4 sw=4 expandtab: */