1 |
/* |
2 |
* PSEmu plugin for SDL joysticks |
3 |
* |
4 |
* Written in 2003 by Christian Bauer |
5 |
*/ |
6 |
|
7 |
#include <SDL/SDL.h> |
8 |
#include <string.h> |
9 |
|
10 |
typedef char HWND; |
11 |
#include "psemu.h" |
12 |
|
13 |
static SDL_Joystick *js1 = NULL, *js2 = NULL; |
14 |
static int num_buttons1 = 0, num_buttons2 = 0; |
15 |
static int num_axes1 = 0, num_axes2 = 0; |
16 |
|
17 |
// Maps SDL button number to PSX button bit number |
18 |
static int button_map[16] = { |
19 |
12, // Triangle |
20 |
13, // Circle |
21 |
14, // Cross |
22 |
15, // Square |
23 |
8, // L2 |
24 |
9, // R2 |
25 |
10, // L1 |
26 |
11, // R1 |
27 |
0, // Select |
28 |
1, // Left analog |
29 |
2, // Right analog |
30 |
3, // Start |
31 |
4, // Up |
32 |
5, // Right |
33 |
6, // Down |
34 |
7 // Left |
35 |
}; |
36 |
|
37 |
char *PSEgetLibName(void) |
38 |
{ |
39 |
return "Cebix' SDL Joystick Plugin"; |
40 |
} |
41 |
|
42 |
unsigned long PSEgetLibType(void) |
43 |
{ |
44 |
return PSE_LT_PAD; |
45 |
} |
46 |
|
47 |
unsigned long PSEgetLibVersion(void) |
48 |
{ |
49 |
return (1 << 16) | (VERSION << 8) | BUILD; |
50 |
} |
51 |
|
52 |
long PADinit(long flags) |
53 |
{ |
54 |
if (SDL_Init(SDL_INIT_JOYSTICK) < 0) |
55 |
return PSE_INIT_ERR_NOHARDWARE; |
56 |
if (SDL_NumJoysticks() < 1) |
57 |
return PSE_INIT_ERR_NOHARDWARE; |
58 |
SDL_JoystickEventState(SDL_IGNORE); |
59 |
|
60 |
return PSE_INIT_ERR_SUCCESS; |
61 |
} |
62 |
|
63 |
void PADshutdown(void) |
64 |
{ |
65 |
SDL_Quit(); |
66 |
} |
67 |
|
68 |
long PADopen(unsigned long *disp) |
69 |
{ |
70 |
js1 = SDL_JoystickOpen(0); |
71 |
if (js1) { |
72 |
num_axes1 = SDL_JoystickNumAxes(js1); |
73 |
num_buttons1 = SDL_JoystickNumButtons(js1); |
74 |
} |
75 |
js2 = SDL_JoystickOpen(1); |
76 |
if (js2) { |
77 |
num_axes2 = SDL_JoystickNumAxes(js2); |
78 |
num_buttons2 = SDL_JoystickNumButtons(js2); |
79 |
} |
80 |
return (js1 || js2) ? PSE_PAD_ERR_SUCCESS : PSE_PAD_ERR_FAILURE; |
81 |
} |
82 |
|
83 |
long PADclose(void) |
84 |
{ |
85 |
if (js1) { |
86 |
SDL_JoystickClose(js1); |
87 |
js1 = NULL; |
88 |
} |
89 |
if (js2) { |
90 |
SDL_JoystickClose(js2); |
91 |
js2 = NULL; |
92 |
} |
93 |
return PSE_PAD_ERR_SUCCESS; |
94 |
} |
95 |
|
96 |
long PADquery(void) |
97 |
{ |
98 |
return PSE_PAD_USE_PORT1 | PSE_PAD_USE_PORT2; |
99 |
} |
100 |
|
101 |
static read_pad(SDL_Joystick *js, int num_buttons, int num_axes, PadDataS *pad) |
102 |
{ |
103 |
Uint16 buttons = 0; |
104 |
unsigned i; |
105 |
|
106 |
memset(pad, 0, sizeof(PadDataS)); |
107 |
pad->controllerType = PSE_PAD_TYPE_ANALOGPAD; |
108 |
|
109 |
for (i=0; i<num_buttons; i++) { |
110 |
if (SDL_JoystickGetButton(js, i) == SDL_PRESSED) |
111 |
buttons |= (1 << button_map[i]); |
112 |
} |
113 |
pad->buttonStatus = ~buttons; |
114 |
|
115 |
if (num_axes >= 2) { |
116 |
pad->leftJoyX = SDL_JoystickGetAxis(js, 0) / 256 + 128; |
117 |
pad->leftJoyY = SDL_JoystickGetAxis(js, 1) / 256 + 128; |
118 |
} else |
119 |
pad->leftJoyX = pad->leftJoyY = 128; |
120 |
|
121 |
if (num_axes >= 4) { |
122 |
pad->rightJoyX = SDL_JoystickGetAxis(js, 2) / 256 + 128; |
123 |
pad->rightJoyY = SDL_JoystickGetAxis(js, 3) / 256 + 128; |
124 |
} else |
125 |
pad->rightJoyX = pad->rightJoyY = 128; |
126 |
|
127 |
pad->moveX = pad->moveY = 0; |
128 |
} |
129 |
|
130 |
long PADreadPort1(PadDataS *pad) |
131 |
{ |
132 |
if (js1) { |
133 |
SDL_JoystickUpdate(); |
134 |
read_pad(js1, num_buttons1, num_axes1, pad); |
135 |
} else { |
136 |
pad->controllerType = PSE_PAD_TYPE_ANALOGPAD; |
137 |
pad->buttonStatus = 0xffff; |
138 |
} |
139 |
return PSE_PAD_ERR_SUCCESS; |
140 |
} |
141 |
|
142 |
long PADreadPort2(PadDataS *pad) |
143 |
{ |
144 |
if (js2) { |
145 |
SDL_JoystickUpdate(); |
146 |
read_pad(js2, num_buttons2, num_axes2, pad); |
147 |
} else { |
148 |
pad->controllerType = PSE_PAD_TYPE_ANALOGPAD; |
149 |
pad->buttonStatus = 0xffff; |
150 |
} |
151 |
return PSE_PAD_ERR_SUCCESS; |
152 |
} |
153 |
|
154 |
long PADtest(void) |
155 |
{ |
156 |
return PSE_ERR_SUCCESS; |
157 |
} |
158 |
|
159 |
long PADconfigure(void) |
160 |
{ |
161 |
return PSE_ERR_SUCCESS; |
162 |
} |
163 |
|
164 |
void PADabout(void) |
165 |
{ |
166 |
} |