Merge pull request #5835 from fjtrujy/psp/improveJoystickDriver [PSP] Improve Joystick driver
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
diff --git a/src/joystick/psp/SDL_sysjoystick.c b/src/joystick/psp/SDL_sysjoystick.c
index a542259..df3be56 100644
--- a/src/joystick/psp/SDL_sysjoystick.c
+++ b/src/joystick/psp/SDL_sysjoystick.c
@@ -24,7 +24,6 @@
/* This is the PSP implementation of the SDL joystick API */
#include <pspctrl.h>
-#include <pspkernel.h>
#include <stdio.h> /* For the definition of NULL */
#include <stdlib.h>
@@ -34,15 +33,9 @@
#include "SDL_events.h"
#include "SDL_error.h"
-#include "SDL_mutex.h"
-#include "SDL_timer.h"
-#include "../../thread/SDL_systhread.h"
/* Current pad state */
static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 };
-static SDL_sem *pad_sem = NULL;
-static SDL_Thread *thread = NULL;
-static int running = 0;
static const enum PspCtrlButtons button_map[] = {
PSP_CTRL_TRIANGLE, PSP_CTRL_CIRCLE, PSP_CTRL_CROSS, PSP_CTRL_SQUARE,
PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER,
@@ -82,23 +75,6 @@ static int calc_bezier_y(float t)
return dest.y;
}
-/*
- * Collect pad data about once per frame
- */
-int JoystickUpdate(void *data)
-{
- while (running) {
- SDL_SemWait(pad_sem);
- sceCtrlPeekBufferPositive(&pad, 1);
- SDL_SemPost(pad_sem);
- /* Delay 1/60th of a second */
- sceKernelDelayThread(1000000 / 60);
- }
- return 0;
-}
-
-
-
/* Function to scan the system for joysticks.
* Joystick 0 should be the system default joystick.
* It should return number of joysticks, or -1 on an unrecoverable fatal error.
@@ -111,15 +87,6 @@ static int PSP_JoystickInit(void)
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
- /* Start thread to read data */
- if((pad_sem = SDL_CreateSemaphore(1)) == NULL) {
- return SDL_SetError("Can't create input semaphore");
- }
- running = 1;
- if((thread = SDL_CreateThreadInternal(JoystickUpdate, "JoystickThread", 4096, NULL)) == NULL) {
- return SDL_SetError("Can't create input thread");
- }
-
/* Create an accurate map from analog inputs (0 to 255)
to SDL joystick positions (-32768 to 32767) */
for (i = 0; i < 128; i++)
@@ -132,7 +99,7 @@ static int PSP_JoystickInit(void)
return 1;
}
-static int PSP_NumJoysticks(void)
+static int PSP_JoystickGetCount(void)
{
return 1;
}
@@ -141,21 +108,17 @@ static void PSP_JoystickDetect(void)
{
}
-#if 0
-static const char *PSP_JoystickName(int idx)
-{
- if (idx == 0) return "PSP controller";
- SDL_SetError("No joystick available with that index");
- return NULL;
-}
-#endif
-
+/* Function to get the device-dependent name of a joystick */
static const char *PSP_JoystickGetDeviceName(int device_index)
{
- return "PSP builtin joypad";
+ if (device_index == 0)
+ return "PSP builtin joypad";
+
+ SDL_SetError("No joystick available with that index");
+ return(NULL);
}
-static const char *PSP_JoystickGetDevicePath(int device_index)
+static const char *PSP_JoystickGetDevicePath(int index)
{
return NULL;
}
@@ -165,8 +128,7 @@ static int PSP_JoystickGetDevicePlayerIndex(int device_index)
return -1;
}
-static void
-PSP_JoystickSetDevicePlayerIndex(int device_index, int player_index)
+static void PSP_JoystickSetDevicePlayerIndex(int device_index, int player_index)
{
}
@@ -193,21 +155,20 @@ static SDL_JoystickID PSP_JoystickGetDeviceInstanceID(int device_index)
*/
static int PSP_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
- joystick->nbuttons = 14;
+ joystick->nbuttons = SDL_arraysize(button_map);
joystick->naxes = 2;
joystick->nhats = 0;
+ joystick->instance_id = device_index;
return 0;
}
-static int
-PSP_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
+static int PSP_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
return SDL_Unsupported();
}
-static int
-PSP_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
+static int PSP_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
{
return SDL_Unsupported();
}
@@ -217,14 +178,12 @@ static Uint32 PSP_JoystickGetCapabilities(SDL_Joystick *joystick)
return 0;
}
-static int
-PSP_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
+static int PSP_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
return SDL_Unsupported();
}
-static int
-PSP_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
+static int PSP_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
@@ -248,11 +207,10 @@ static void PSP_JoystickUpdate(SDL_Joystick *joystick)
static enum PspCtrlButtons old_buttons = 0;
static unsigned char old_x = 0, old_y = 0;
- SDL_SemWait(pad_sem);
+ sceCtrlReadBufferPositive(&pad, 1);
buttons = pad.Buttons;
x = pad.Lx;
y = pad.Ly;
- SDL_SemPost(pad_sem);
/* Axes */
if(old_x != x) {
@@ -268,8 +226,8 @@ static void PSP_JoystickUpdate(SDL_Joystick *joystick)
changed = old_buttons ^ buttons;
old_buttons = buttons;
if(changed) {
- for(i=0; i<sizeof(button_map)/sizeof(button_map[0]); i++) {
- if(changed & button_map[i]) {
+ for (i = 0; i < SDL_arraysize(button_map); i++) {
+ if (changed & button_map[i]) {
SDL_PrivateJoystickButton(
joystick, i,
(buttons & button_map[i]) ?
@@ -277,8 +235,6 @@ static void PSP_JoystickUpdate(SDL_Joystick *joystick)
}
}
}
-
- sceKernelDelayThread(0);
}
/* Function to close a joystick after use */
@@ -289,10 +245,6 @@ static void PSP_JoystickClose(SDL_Joystick *joystick)
/* Function to perform any system-specific joystick related cleanup */
static void PSP_JoystickQuit(void)
{
- /* Cleanup Threads and Semaphore. */
- running = 0;
- SDL_WaitThread(thread, NULL);
- SDL_DestroySemaphore(pad_sem);
}
static SDL_bool
@@ -304,7 +256,7 @@ PSP_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
SDL_JoystickDriver SDL_PSP_JoystickDriver =
{
PSP_JoystickInit,
- PSP_NumJoysticks,
+ PSP_JoystickGetCount,
PSP_JoystickDetect,
PSP_JoystickGetDeviceName,
PSP_JoystickGetDevicePath,