1 |
cebix |
1.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 |
cebix |
1.2 |
#ifdef EPSXE |
114 |
|
|
pad->buttonStatus = ~((buttons >> 8) | (buttons << 8)); |
115 |
|
|
#else |
116 |
cebix |
1.1 |
pad->buttonStatus = ~buttons; |
117 |
cebix |
1.2 |
#endif |
118 |
cebix |
1.1 |
|
119 |
|
|
if (num_axes >= 2) { |
120 |
|
|
pad->leftJoyX = SDL_JoystickGetAxis(js, 0) / 256 + 128; |
121 |
|
|
pad->leftJoyY = SDL_JoystickGetAxis(js, 1) / 256 + 128; |
122 |
|
|
} else |
123 |
|
|
pad->leftJoyX = pad->leftJoyY = 128; |
124 |
|
|
|
125 |
|
|
if (num_axes >= 4) { |
126 |
|
|
pad->rightJoyX = SDL_JoystickGetAxis(js, 2) / 256 + 128; |
127 |
|
|
pad->rightJoyY = SDL_JoystickGetAxis(js, 3) / 256 + 128; |
128 |
|
|
} else |
129 |
|
|
pad->rightJoyX = pad->rightJoyY = 128; |
130 |
|
|
|
131 |
|
|
pad->moveX = pad->moveY = 0; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
long PADreadPort1(PadDataS *pad) |
135 |
|
|
{ |
136 |
|
|
if (js1) { |
137 |
|
|
SDL_JoystickUpdate(); |
138 |
|
|
read_pad(js1, num_buttons1, num_axes1, pad); |
139 |
|
|
} else { |
140 |
|
|
pad->controllerType = PSE_PAD_TYPE_ANALOGPAD; |
141 |
|
|
pad->buttonStatus = 0xffff; |
142 |
|
|
} |
143 |
|
|
return PSE_PAD_ERR_SUCCESS; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
long PADreadPort2(PadDataS *pad) |
147 |
|
|
{ |
148 |
|
|
if (js2) { |
149 |
|
|
SDL_JoystickUpdate(); |
150 |
|
|
read_pad(js2, num_buttons2, num_axes2, pad); |
151 |
|
|
} else { |
152 |
|
|
pad->controllerType = PSE_PAD_TYPE_ANALOGPAD; |
153 |
|
|
pad->buttonStatus = 0xffff; |
154 |
|
|
} |
155 |
|
|
return PSE_PAD_ERR_SUCCESS; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
long PADtest(void) |
159 |
|
|
{ |
160 |
|
|
return PSE_ERR_SUCCESS; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
long PADconfigure(void) |
164 |
|
|
{ |
165 |
|
|
return PSE_ERR_SUCCESS; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
void PADabout(void) |
169 |
|
|
{ |
170 |
|
|
} |