SDL  2.0
SDL_sysjoystick.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2019 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 #if SDL_JOYSTICK_PSP
24 
25 /* This is the PSP implementation of the SDL joystick API */
26 #include <pspctrl.h>
27 #include <pspkernel.h>
28 
29 #include <stdio.h> /* For the definition of NULL */
30 #include <stdlib.h>
31 
32 #include "../SDL_sysjoystick.h"
33 #include "../SDL_joystick_c.h"
34 
35 #include "SDL_events.h"
36 #include "SDL_error.h"
37 #include "SDL_mutex.h"
38 #include "SDL_timer.h"
39 #include "../../thread/SDL_systhread.h"
40 
41 /* Current pad state */
42 static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 };
43 static SDL_sem *pad_sem = NULL;
44 static SDL_Thread *thread = NULL;
45 static int running = 0;
46 static const enum PspCtrlButtons button_map[] = {
47  PSP_CTRL_TRIANGLE, PSP_CTRL_CIRCLE, PSP_CTRL_CROSS, PSP_CTRL_SQUARE,
48  PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER,
49  PSP_CTRL_DOWN, PSP_CTRL_LEFT, PSP_CTRL_UP, PSP_CTRL_RIGHT,
50  PSP_CTRL_SELECT, PSP_CTRL_START, PSP_CTRL_HOME, PSP_CTRL_HOLD };
51 static int analog_map[256]; /* Map analog inputs to -32768 -> 32767 */
52 
53 typedef struct
54 {
55  int x;
56  int y;
57 } point;
58 
59 /* 4 points define the bezier-curve. */
60 static point a = { 0, 0 };
61 static point b = { 50, 0 };
62 static point c = { 78, 32767 };
63 static point d = { 128, 32767 };
64 
65 /* simple linear interpolation between two points */
66 static SDL_INLINE void lerp (point *dest, point *a, point *b, float t)
67 {
68  dest->x = a->x + (b->x - a->x)*t;
69  dest->y = a->y + (b->y - a->y)*t;
70 }
71 
72 /* evaluate a point on a bezier-curve. t goes from 0 to 1.0 */
73 static int calc_bezier_y(float t)
74 {
75  point ab, bc, cd, abbc, bccd, dest;
76  lerp (&ab, &a, &b, t); /* point between a and b */
77  lerp (&bc, &b, &c, t); /* point between b and c */
78  lerp (&cd, &c, &d, t); /* point between c and d */
79  lerp (&abbc, &ab, &bc, t); /* point between ab and bc */
80  lerp (&bccd, &bc, &cd, t); /* point between bc and cd */
81  lerp (&dest, &abbc, &bccd, t); /* point on the bezier-curve */
82  return dest.y;
83 }
84 
85 /*
86  * Collect pad data about once per frame
87  */
88 int JoystickUpdate(void *data)
89 {
90  while (running) {
91  SDL_SemWait(pad_sem);
92  sceCtrlPeekBufferPositive(&pad, 1);
93  SDL_SemPost(pad_sem);
94  /* Delay 1/60th of a second */
95  sceKernelDelayThread(1000000 / 60);
96  }
97  return 0;
98 }
99 
100 
101 
102 /* Function to scan the system for joysticks.
103  * Joystick 0 should be the system default joystick.
104  * It should return number of joysticks, or -1 on an unrecoverable fatal error.
105  */
106 int SDL_SYS_JoystickInit(void)
107 {
108  int i;
109 
110  /* Setup input */
111  sceCtrlSetSamplingCycle(0);
112  sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
113 
114  /* Start thread to read data */
115  if((pad_sem = SDL_CreateSemaphore(1)) == NULL) {
116  return SDL_SetError("Can't create input semaphore");
117  }
118  running = 1;
119  if((thread = SDL_CreateThreadInternal(JoystickUpdate, "JoystickThread", 4096, NULL)) == NULL) {
120  return SDL_SetError("Can't create input thread");
121  }
122 
123  /* Create an accurate map from analog inputs (0 to 255)
124  to SDL joystick positions (-32768 to 32767) */
125  for (i = 0; i < 128; i++)
126  {
127  float t = (float)i/127.0f;
128  analog_map[i+128] = calc_bezier_y(t);
129  analog_map[127-i] = -1 * analog_map[i+128];
130  }
131 
132  return 1;
133 }
134 
135 int SDL_SYS_NumJoysticks(void)
136 {
137  return 1;
138 }
139 
140 void SDL_SYS_JoystickDetect(void)
141 {
142 }
143 
144 /* Function to get the device-dependent name of a joystick */
145 const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
146 {
147  return "PSP builtin joypad";
148 }
149 
150 /* Function to perform the mapping from device index to the instance id for this index */
151 SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
152 {
153  return device_index;
154 }
155 
156 /* Function to get the device-dependent name of a joystick */
157 const char *SDL_SYS_JoystickName(int index)
158 {
159  if (index == 0)
160  return "PSP controller";
161 
162  SDL_SetError("No joystick available with that index");
163  return(NULL);
164 }
165 
166 /* Function to open a joystick for use.
167  The joystick to open is specified by the device index.
168  This should fill the nbuttons and naxes fields of the joystick structure.
169  It returns 0, or -1 if there is an error.
170  */
171 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
172 {
173  joystick->nbuttons = 14;
174  joystick->naxes = 2;
175  joystick->nhats = 0;
176 
177  return 0;
178 }
179 
180 /* Function to update the state of a joystick - called as a device poll.
181  * This function shouldn't update the joystick structure directly,
182  * but instead should call SDL_PrivateJoystick*() to deliver events
183  * and update joystick device state.
184  */
185 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
186 {
187  int i;
188  enum PspCtrlButtons buttons;
189  enum PspCtrlButtons changed;
190  unsigned char x, y;
191  static enum PspCtrlButtons old_buttons = 0;
192  static unsigned char old_x = 0, old_y = 0;
193 
194  SDL_SemWait(pad_sem);
195  buttons = pad.Buttons;
196  x = pad.Lx;
197  y = pad.Ly;
198  SDL_SemPost(pad_sem);
199 
200  /* Axes */
201  if(old_x != x) {
202  SDL_PrivateJoystickAxis(joystick, 0, analog_map[x]);
203  old_x = x;
204  }
205  if(old_y != y) {
206  SDL_PrivateJoystickAxis(joystick, 1, analog_map[y]);
207  old_y = y;
208  }
209 
210  /* Buttons */
211  changed = old_buttons ^ buttons;
212  old_buttons = buttons;
213  if(changed) {
214  for(i=0; i<sizeof(button_map)/sizeof(button_map[0]); i++) {
215  if(changed & button_map[i]) {
217  joystick, i,
218  (buttons & button_map[i]) ?
220  }
221  }
222  }
223 
224  sceKernelDelayThread(0);
225 }
226 
227 /* Function to close a joystick after use */
228 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
229 {
230 }
231 
232 /* Function to perform any system-specific joystick related cleanup */
233 void SDL_SYS_JoystickQuit(void)
234 {
235  /* Cleanup Threads and Semaphore. */
236  running = 0;
237  SDL_WaitThread(thread, NULL);
238  SDL_DestroySemaphore(pad_sem);
239 }
240 
241 SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
242 {
243  SDL_JoystickGUID guid;
244  /* the GUID is just the first 16 chars of the name for now */
245  const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
246  SDL_zero( guid );
247  SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
248  return guid;
249 }
250 
251 SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
252 {
253  SDL_JoystickGUID guid;
254  /* the GUID is just the first 16 chars of the name for now */
255  const char *name = joystick->name;
256  SDL_zero( guid );
257  SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
258  return guid;
259 }
260 
261 #endif /* SDL_JOYSTICK_PSP */
262 
263 /* vim: ts=4 sw=4
264  */
SDL_zero
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
SDL_events.h
SDL_CreateSemaphore
#define SDL_CreateSemaphore
Definition: SDL_dynapi_overrides.h:264
NULL
#define NULL
Definition: begin_code.h:167
SDL_timer.h
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109
SDL_error.h
index
GLuint index
Definition: SDL_opengl_glext.h:660
SDL_Thread
Definition: SDL_thread_c.h:54
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
SDL_RELEASED
#define SDL_RELEASED
Definition: SDL_events.h:49
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093
SDL_SemPost
#define SDL_SemPost
Definition: SDL_dynapi_overrides.h:269
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1973
SDL_PrivateJoystickAxis
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:833
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1573
SDL_CreateThreadInternal
SDL_Thread * SDL_CreateThreadInternal(int(*fn)(void *), const char *name, const size_t stacksize, void *data)
Definition: SDL_thread.c:429
SDL_INLINE
#define SDL_INLINE
Definition: begin_code.h:134
f
GLfloat f
Definition: SDL_opengl_glext.h:1870
t
GLdouble GLdouble t
Definition: SDL_opengl.h:2070
SDL_mutex.h
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_PrivateJoystickButton
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
Definition: SDL_joystick.c:966
SDL_min
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1573
SDL_SemWait
#define SDL_SemWait
Definition: SDL_dynapi_overrides.h:266
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_DestroySemaphore
#define SDL_DestroySemaphore
Definition: SDL_dynapi_overrides.h:265
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:393
SDL_JoystickGUID
Definition: SDL_joystick.h:70
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_WaitThread
#define SDL_WaitThread
Definition: SDL_dynapi_overrides.h:478
d
const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char const char const SDL_SCANF_FORMAT_STRING char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 ** d
Definition: SDL_dynapi_procs.h:117
SDL_JoystickID
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:81