Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
274 lines (233 loc) · 7.69 KB

SDL_sysjoystick.c

File metadata and controls

274 lines (233 loc) · 7.69 KB
 
Mar 17, 2013
Mar 17, 2013
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 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.
*/
/* This is the system specific header for the SDL joystick API */
#include <pspctrl.h>
#include <pspkernel.h>
May 18, 2013
May 18, 2013
26
#include <stdio.h> /* For the definition of NULL */
Mar 17, 2013
Mar 17, 2013
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdlib.h>
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
#include "SDL_events.h"
#include "SDL_error.h"
#include "SDL_thread.h"
#include "SDL_mutex.h"
#include "SDL_timer.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[] = {
May 18, 2013
May 18, 2013
44
45
46
47
PSP_CTRL_TRIANGLE, PSP_CTRL_CIRCLE, PSP_CTRL_CROSS, PSP_CTRL_SQUARE,
PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER,
PSP_CTRL_DOWN, PSP_CTRL_LEFT, PSP_CTRL_UP, PSP_CTRL_RIGHT,
PSP_CTRL_SELECT, PSP_CTRL_START, PSP_CTRL_HOME, PSP_CTRL_HOLD };
Mar 17, 2013
Mar 17, 2013
48
49
50
51
52
53
54
55
static int analog_map[256]; /* Map analog inputs to -32768 -> 32767 */
typedef struct
{
int x;
int y;
} point;
May 18, 2013
May 18, 2013
56
/* 4 points define the bezier-curve. */
Mar 17, 2013
Mar 17, 2013
57
58
59
60
61
static point a = { 0, 0 };
static point b = { 50, 0 };
static point c = { 78, 32767 };
static point d = { 128, 32767 };
May 18, 2013
May 18, 2013
62
/* simple linear interpolation between two points */
Mar 17, 2013
Mar 17, 2013
63
64
static __inline__ void lerp (point *dest, point *a, point *b, float t)
{
May 18, 2013
May 18, 2013
65
66
dest->x = a->x + (b->x - a->x)*t;
dest->y = a->y + (b->y - a->y)*t;
Mar 17, 2013
Mar 17, 2013
67
68
}
May 18, 2013
May 18, 2013
69
/* evaluate a point on a bezier-curve. t goes from 0 to 1.0 */
Mar 17, 2013
Mar 17, 2013
70
71
static int calc_bezier_y(float t)
{
May 18, 2013
May 18, 2013
72
73
74
75
76
77
78
79
point ab, bc, cd, abbc, bccd, dest;
lerp (&ab, &a, &b, t); /* point between a and b */
lerp (&bc, &b, &c, t); /* point between b and c */
lerp (&cd, &c, &d, t); /* point between c and d */
lerp (&abbc, &ab, &bc, t); /* point between ab and bc */
lerp (&bccd, &bc, &cd, t); /* point between bc and cd */
lerp (&dest, &abbc, &bccd, t); /* point on the bezier-curve */
return dest.y;
Mar 17, 2013
Mar 17, 2013
80
81
82
83
84
85
86
}
/*
* Collect pad data about once per frame
*/
int JoystickUpdate(void *data)
{
May 18, 2013
May 18, 2013
87
88
89
90
91
92
93
94
while (running) {
SDL_SemWait(pad_sem);
sceCtrlPeekBufferPositive(&pad, 1);
SDL_SemPost(pad_sem);
/* Delay 1/60th of a second */
sceKernelDelayThread(1000000 / 60);
}
return 0;
Mar 17, 2013
Mar 17, 2013
95
96
97
98
99
100
101
102
103
104
105
}
/* Function to scan the system for joysticks.
* This function should set SDL_numjoysticks to the number of available
* joysticks. Joystick 0 should be the system default joystick.
* It should return number of joysticks, or -1 on an unrecoverable fatal error.
*/
int SDL_SYS_JoystickInit(void)
{
May 18, 2013
May 18, 2013
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
int i;
/* SDL_numjoysticks = 1; */
/* Setup input */
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_CreateThread(JoystickUpdate, "JoySitckThread",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++)
{
float t = (float)i/127.0f;
analog_map[i+128] = calc_bezier_y(t);
analog_map[127-i] = -1 * analog_map[i+128];
}
return 1;
Mar 17, 2013
Mar 17, 2013
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
}
int SDL_SYS_NumJoysticks()
{
return 1;
}
void SDL_SYS_JoystickDetect()
{
}
SDL_bool SDL_SYS_JoystickNeedsPolling()
{
return SDL_FALSE;
}
/* Function to get the device-dependent name of a joystick */
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
return "PSP builtin joypad";
}
/* Function to perform the mapping from device index to the instance id for this index */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
return device_index;
}
/* Function to get the device-dependent name of a joystick */
const char *SDL_SYS_JoystickName(int index)
{
May 18, 2013
May 18, 2013
164
165
if (index == 0)
return "PSP controller";
Mar 17, 2013
Mar 17, 2013
166
May 18, 2013
May 18, 2013
167
168
SDL_SetError("No joystick available with that index");
return(NULL);
Mar 17, 2013
Mar 17, 2013
169
170
171
172
173
174
175
176
177
}
/* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick.
This should fill the nbuttons and naxes fields of the joystick structure.
It returns 0, or -1 if there is an error.
*/
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
May 18, 2013
May 18, 2013
178
179
180
joystick->nbuttons = 14;
joystick->naxes = 2;
joystick->nhats = 0;
Mar 17, 2013
Mar 17, 2013
181
May 18, 2013
May 18, 2013
182
return 0;
Mar 17, 2013
Mar 17, 2013
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
}
/* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return SDL_TRUE;
}
/* 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.
*/
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
May 18, 2013
May 18, 2013
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
int i;
enum PspCtrlButtons buttons;
enum PspCtrlButtons changed;
unsigned char x, y;
static enum PspCtrlButtons old_buttons = 0;
static unsigned char old_x = 0, old_y = 0;
SDL_SemWait(pad_sem);
buttons = pad.Buttons;
x = pad.Lx;
y = pad.Ly;
SDL_SemPost(pad_sem);
/* Axes */
if(old_x != x) {
SDL_PrivateJoystickAxis(joystick, 0, analog_map[x]);
old_x = x;
}
if(old_y != y) {
SDL_PrivateJoystickAxis(joystick, 1, analog_map[y]);
old_y = y;
}
/* Buttons */
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]) {
SDL_PrivateJoystickButton(
joystick, i,
(buttons & button_map[i]) ?
SDL_PRESSED : SDL_RELEASED);
}
}
}
sceKernelDelayThread(0);
Mar 17, 2013
Mar 17, 2013
236
237
238
239
240
}
/* Function to close a joystick after use */
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
{
May 18, 2013
May 18, 2013
241
/* Do nothing. */
Mar 17, 2013
Mar 17, 2013
242
243
244
245
246
}
/* Function to perform any system-specific joystick related cleanup */
void SDL_SYS_JoystickQuit(void)
{
May 18, 2013
May 18, 2013
247
248
249
250
/* Cleanup Threads and Semaphore. */
running = 0;
SDL_WaitThread(thread, NULL);
SDL_DestroySemaphore(pad_sem);
Mar 17, 2013
Mar 17, 2013
251
252
253
254
255
}
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{
SDL_JoystickGUID guid;
May 18, 2013
May 18, 2013
256
/* the GUID is just the first 16 chars of the name for now */
Mar 17, 2013
Mar 17, 2013
257
258
259
260
261
262
263
264
265
const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid;
}
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{
SDL_JoystickGUID guid;
May 18, 2013
May 18, 2013
266
/* the GUID is just the first 16 chars of the name for now */
Mar 17, 2013
Mar 17, 2013
267
268
269
270
271
272
273
274
const char *name = joystick->name;
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid;
}
/* vim: ts=4 sw=4
*/