PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
input.c
1#include <stdio.h>
2#include <malloc.h>
3#include <libpad.h>
4#ifdef _XINPUT
5#include <libmtap.h>
6#endif
7
8#include <input.h>
9
10pad_t *pad_open(unsigned int port, unsigned int slot, unsigned int mode, unsigned int lock)
11{
12
13 pad_t *pad = (pad_t*)malloc(sizeof(pad_t));
14
15 pad->buffer = (char*)memalign(64,256);
16 pad->buttons = (struct padButtonStatus*)malloc(sizeof(struct padButtonStatus));
17 pad->actuator = NULL;
18
19 // there are only two ports
20 if (port > 1)
21 {
22 free(pad->buttons);
23 free(pad);
24 return NULL;
25 }
26
27#ifdef _XINPUT
28 if (mtapGetConnection(port))
29 {
30 // there are only four slots
31 if (slot > 3)
32 {
33 free(pad->buttons);
34 free(pad);
35 return NULL;
36 }
37 }
38 else
39 {
40#endif
41 // there's only one slot
42 if (slot)
43 {
44 free(pad->buttons);
45 free(pad);
46 return NULL;
47 }
48#ifdef _XINPUT
49 }
50#endif
51
52 pad->port = port;
53#ifdef _XINPUT
54 pad->slot = slot;
55#else
56 pad->slot = 0;
57#endif
58
59 // Placeholders
60 pad->type = PAD_TYPE_DIGITAL;
61
62 pad->state = 0;
63 pad->last_state = -1;
64 pad->exec_cmd = 0;
65 pad->num_modes = -1;
66 pad->sensitivity = 0;
67
68 // These get verified by pad_set_mode()
69 pad->mode = mode;
70 pad->lock = lock;
71
72 padPortOpen(pad->port,pad->slot,pad->buffer);
73
74 pad_wait(pad);
75
76 pad_set_mode(pad,pad->mode,pad->lock);
77
78 return pad;
79
80}
81
82void pad_close(pad_t *pad)
83{
84
85 if (pad == NULL)
86 {
87 return;
88 }
89
90 if (pad->actuator != NULL)
91 {
92 free(pad->actuator);
93 }
94
95 padPortClose(pad->port,pad->slot);
96
97 free(pad->buttons);
98 free(pad->buffer);
99 free(pad);
100
101}
102
104{
105
106 pad->state = padGetState(pad->port, pad->slot);
107
108 return pad->state;
109
110}
111#ifdef DEBUG
112void pad_print_state(pad_t* pad)
113{
114
115 char stateString[16];
116
117 padStateInt2String(pad->state,stateString);
118 printf("Pad(%d,%d) state is: %s\n",pad->port,pad->slot,stateString);
119
120}
121#endif
122void pad_wait(pad_t* pad)
123{
124
125 pad_get_state(pad);
126
127 if (pad->state != pad->last_state)
128 {
129#ifdef DEBUG
130 pad_print_state(pad);
131#endif
132 pad->last_state = pad->state;
133
134 }
135
136 if (pad->state == PAD_STATE_STABLE) return;
137 if (pad->state == PAD_STATE_ERROR) return;
138 if (pad->state == PAD_STATE_DISCONN) return;
139 if (pad->state == PAD_STATE_FINDCTP1) return;
140
141 return pad_wait(pad);
142
143}
144
146{
147
148 pad->num_modes = padInfoMode(pad->port,pad->slot,PAD_MODETABLE,-1);
149
150 return pad->num_modes;
151
152}
153#ifdef DEBUG
154void pad_print_supported_modes(pad_t *pad)
155{
156
157 int modes = pad_get_num_modes(pad);
158
159 printf("The device has %d modes\n", modes);
160
161 if (modes > 0)
162 {
163 int i;
164
165 printf("( ");
166
167 for (i = 0; i < modes; i++)
168 {
169 printf("%d ", padInfoMode(0, 0, PAD_MODETABLE, i));
170 }
171
172 printf(")\n");
173
174 }
175
176}
177#endif
178int pad_has_type(pad_t *pad, int type)
179{
180
181 int i = 0;
182
183 if (pad->num_modes < 0)
184 {
186 }
187
188 if ((pad->num_modes == 0) && (type == PAD_TYPE_DIGITAL))
189 {
190 return 1;
191 }
192
193 for (i = 0; i < pad->num_modes; i++)
194 {
195 if (padInfoMode(pad->port,pad->slot,PAD_MODETABLE,i) == type)
196 {
197 return 1;
198 }
199
200 }
201
202 return 0;
203
204}
205
207{
208
209 pad->type = padInfoMode(pad->port,pad->slot,PAD_MODECURID,0);
210
211 return pad->type;
212
213}
214
215void pad_set_mode(pad_t *pad, int mode, int lock)
216{
217
218 int status = 0;
219
220 if (lock == PAD_MMODE_LOCK)
221 {
222 pad->lock = lock;
223 }
224 else
225 {
226 pad->lock = PAD_MMODE_UNLOCK;
227 }
228
229 if ((mode == PAD_MMODE_DUALSHOCK) || (mode == PAD_TYPE_DUALSHOCK))
230 {
231 if (pad_has_type(pad,PAD_TYPE_DUALSHOCK))
232 {
233 status = padSetMainMode(pad->port,pad->slot,pad->mode,pad->lock);
234 }
235
236 if (status)
237 {
238 pad->mode = PAD_MMODE_DUALSHOCK;
239 }
240 }
241 else
242 {
243 if (pad_has_type(pad,PAD_TYPE_DIGITAL))
244 {
245 status = padSetMainMode(pad->port,pad->slot,pad->mode,pad->lock);
246 }
247
248 if (status)
249 {
250 pad->mode = PAD_MMODE_DIGITAL;
251 }
252 }
253
254 if (status)
255 {
256 pad_wait(pad);
257 }
258
259}
260
261void pad_set_sensitivity(pad_t *pad, int enable)
262{
263
264 if(padInfoPressMode(pad->port,pad->slot))
265 {
266 pad_wait(pad);
267 pad->sensitivity = 1;
268 }
269 else
270 {
271 pad_wait(pad);
272 pad->sensitivity = 0;
273 return;
274 }
275
276 if (enable)
277 {
278 padEnterPressMode(pad->port,pad->slot);
279 }
280 else
281 {
282 padExitPressMode(pad->port,pad->slot);
283 }
284
285
286}
287
289{
290
291 if (pad->actuator == NULL)
292 {
293#ifdef _XINPUT
294 if (padInfoAct(pad->port, pad->slot, -1, 0))
295#else
296 if (padInfoMode(pad->port,pad->slot,PAD_MODECUREXID,0))
297#endif
298 {
299 pad_wait(pad);
300 pad->actuator = (actuator_t*)malloc(sizeof(actuator_t));
301 }
302 else
303 {
304 pad_wait(pad);
305 return;
306 }
307 }
308
309 pad->actuator->small = 0;
310 pad->actuator->large = 0x00;
311
312 pad->actuator->status[0] = 0;
313 pad->actuator->status[1] = 1;
314 pad->actuator->status[2] = 0xFF;
315 pad->actuator->status[3] = 0xFF;
316 pad->actuator->status[4] = 0xFF;
317 pad->actuator->status[5] = 0xFF;
318
319
320 pad_wait(pad);
321 padSetActAlign(pad->port,pad->slot,pad->actuator->status);
322 pad_wait(pad);
323
324}
325
326void pad_set_actuators(pad_t *pad, int small, unsigned char large)
327{
328
329 if (!small)
330 {
331 pad->actuator->small = small;
332 }
333 else
334 {
335 pad->actuator->small = 0x01;
336 }
337
338 pad->actuator->large = large;
339
340 pad->actuator->status[0] = pad->actuator->small;
341 pad->actuator->status[1] = pad->actuator->large;
342
343 padSetActDirect(pad->port,pad->slot,pad->actuator->status);
344
345}
void pad_close(pad_t *pad)
Definition input.c:82
int pad_has_type(pad_t *pad, int type)
Definition input.c:178
void pad_wait(pad_t *pad)
Definition input.c:122
int pad_get_state(pad_t *pad)
Definition input.c:103
int pad_get_type(pad_t *pad)
Definition input.c:206
pad_t * pad_open(unsigned int port, unsigned int slot, unsigned int mode, unsigned int lock)
Definition input.c:10
void pad_set_actuators(pad_t *pad, int small, unsigned char large)
Definition input.c:326
void pad_set_sensitivity(pad_t *pad, int enable)
Definition input.c:261
void pad_init_actuators(pad_t *pad)
Definition input.c:288
void pad_set_mode(pad_t *pad, int mode, int lock)
Definition input.c:215
int pad_get_num_modes(pad_t *pad)
Definition input.c:145
Definition input.h:40
int mtapGetConnection(int port)
Definition libmtap.c:87
int padPortOpen(int port, int slot, void *padArea)
Definition libpad.c:394
int padSetActAlign(int port, int slot, const char act_align[6])
Definition libpad.c:779
int padInfoMode(int port, int slot, int infoMode, int index)
Definition libpad.c:590
int padExitPressMode(int port, int slot)
Definition libpad.c:698
int padSetActDirect(int port, int slot, char act_align[6])
Definition libpad.c:802
int padPortClose(int port, int slot)
Definition libpad.c:451
int padGetState(int port, int slot)
Definition libpad.c:484
unsigned char padInfoAct(int port, int slot, int word, int byte)
Definition libpad.c:740
int padInfoPressMode(int port, int slot)
Definition libpad.c:677
int padSetMainMode(int port, int slot, int mode, int lock)
Definition libpad.c:658
int padEnterPressMode(int port, int slot)
Definition libpad.c:692