DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
gamepad.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 179
4
5#include "gamepad.hpp"
6#include "sce/libgraph.h"
7#include "sce/libpad.h"
8
9#include <cstdio>
10
11CGamePad GamePad;
12
13unsigned char pad_dma_buf[1024] __attribute__((aligned(64)));
14unsigned char pad_dma_buf2[1024] __attribute__((aligned(64)));
15
17 key_lock = false;
18 key_lock2 = 0;
20
21 while (sceGsSyncV(0) == 0) {
22 }
23
24 scePadInit(0);
25 unk_130[1] = 0;
26 unk_130[2] = 0;
27 unk_130[3] = 0;
28 unk_130[4] = 0;
29 int i;
30
31 for (i = 0; i < 2; i++) {
32 pad[i].input.status.phase = PAD_PHASE_QUERY;
33 pad[i].input.status.button = 0;
34 pad[i].input.status.right_y = 0;
35 pad[i].input.status.right_x = 0;
36 pad[i].input.status.left_y = 0;
37 pad[i].input.status.left_x = 0;
38
39 for (int j = 0; j < 6; j++) {
40 pad[i].actuator.vibration[j] = 0;
42 }
43
44 axis_threshold[i] = 0;
45 repeat[i].enabled = false;
46 repeat[i].active = false;
47
48 for (int j = 0; j < 32; j++) {
49 repeat[i].counter[j] = 0;
50 repeat[i].repeat_delay[j] = 0;
51 repeat[i].initial_delay[j] = 0;
52 }
53 }
54
55 if (!scePadPortOpen(0, 0, pad_dma_buf)) {
56 printf("ERROR: scePadPortOpen\n");
57 return;
58 }
59
60 sceGsSyncV(0);
61 sceGsSyncV(0);
62 sceGsSyncV(0);
63 sceGsSyncV(0);
64
65 if (!scePadPortOpen(1, 0, pad_dma_buf2)) {
66 printf("ERROR: scePadPortOpen\n");
67 return;
68 }
69
70 sceGsSyncV(0);
71 sceGsSyncV(0);
72 sceGsSyncV(0);
73 sceGsSyncV(0);
74}
75
76#define PAD_STATIC_COUNTER_1(name) static const int name = 0;
77#define PAD_STATIC_COUNTER_2(name) PAD_STATIC_COUNTER_1(name##0) PAD_STATIC_COUNTER_1(name##1)
78#define PAD_STATIC_COUNTER_4(name) PAD_STATIC_COUNTER_2(name##0) PAD_STATIC_COUNTER_2(name##1)
79#define PAD_STATIC_COUNTER_8(name) PAD_STATIC_COUNTER_4(name##0) PAD_STATIC_COUNTER_4(name##1)
80#define PAD_STATIC_COUNTER_16(name) PAD_STATIC_COUNTER_8(name##0) PAD_STATIC_COUNTER_8(name##1)
81#define PAD_STATIC_COUNTER_32(name) PAD_STATIC_COUNTER_16(name##0) PAD_STATIC_COUNTER_16(name##1)
82#define PAD_STATIC_COUNTER_64(name) PAD_STATIC_COUNTER_32(name##0) PAD_STATIC_COUNTER_32(name##1)
83#define PAD_STATIC_COUNTER_128(name) PAD_STATIC_COUNTER_64(name##0) PAD_STATIC_COUNTER_64(name##1)
84
85int pad_button_read(PAD_STATUS *status, int port, int slot) {
86 PAD_STATIC_COUNTER_128(pad_static_128_)
87 PAD_STATIC_COUNTER_16(pad_static_16_)
88 PAD_STATIC_COUNTER_4(pad_static_4_)
89 PAD_STATIC_COUNTER_1(pad_static_1_)
90 static short rpad;
91 static char init;
92#line 76
93 unsigned char data[32];
94
95 if (!init) {
96 rpad = 0;
97 init = 1;
98 }
99
100 int mode = 0;
101
102 if (!scePadRead(port, slot, data)) {
103 return 0;
104 }
105
106 if (data[0] == 0) {
107 int button = ((data[2] << 8) | data[3]) ^ 0xffff;
108 status->button = button & 0xffff;
109 status->right_x = data[4];
110 status->right_y = data[5];
111 status->left_x = data[6];
112 status->left_y = data[7];
113 rpad = button;
114 mode = data[1] >> 4;
115 }
116
117 return mode;
118}
119
120int read_pad(PAD_STATUS *status, int port, int slot) {
121 volatile int *phase = &status->phase;
122 int *state = &status->state;
123 int *mode_count = &status->extended_id;
124 int *read_result = &status->pad_mode;
125 int *previous_read_result = &status->previous_pad_mode;
126
127 *state = scePadGetState(port, slot);
128
129 if (*state == scePadStateDiscon) {
130 *phase = PAD_PHASE_QUERY;
131 }
132
133 int valid = 0;
134
135 switch (*phase) {
136 case PAD_PHASE_QUERY:
137 if (*state == scePadStateStable || *state == scePadStateFindCTP1) {
138 int terminal_id = scePadInfoMode(port, slot, InfoModeCurID, 0);
139
140 if (terminal_id != 0) {
141 *mode_count = scePadInfoMode(port, slot, InfoModeCurExID, 0);
142
143 if (*mode_count > 0) {
144 terminal_id = *mode_count;
145 }
146
147 switch (terminal_id) {
148 case PAD_TERMINAL_NEGCON:
149 *phase = PAD_PHASE_READY;
150 break;
151 case PAD_TERMINAL_KONAMI_GUN:
152 *phase = PAD_PHASE_READY;
153 break;
154 case PAD_TERMINAL_DIGITAL:
155 *phase = PAD_PHASE_ANALOG_CHECK;
156 break;
157 case PAD_TERMINAL_ANALOG_JOYSTICK:
158 *phase = PAD_PHASE_READY;
159 break;
160 case PAD_TERMINAL_NAMCO_GUN:
161 *phase = PAD_PHASE_READY;
162 break;
163 case PAD_TERMINAL_DUALSHOCK:
164 *phase = PAD_PHASE_ACTUATOR_CHECK;
165 break;
166 case PAD_TERMINAL_EX_TSURICON:
167 *phase = PAD_PHASE_READY;
168 break;
169 case PAD_TERMINAL_EX_JOGCON:
170 *phase = PAD_PHASE_READY;
171 break;
172 default:
173 *phase = PAD_PHASE_READY;
174 break;
175 }
176 }
177 }
178
179 break;
180 case PAD_PHASE_ANALOG_CHECK:
181 if (scePadInfoMode(port, slot, InfoModeCurExID, 0) == 0) {
182 *phase = PAD_PHASE_READY;
183 break;
184 }
185
186 (*phase)++;
187 // Fall through.
188 case PAD_PHASE_ANALOG_SET:
189 if (scePadSetMainMode(port, slot, 1, 3) == 1) {
190 (*phase)++;
191 }
192
193 break;
194 case PAD_PHASE_ANALOG_WAIT:
195 if (scePadGetState(port, slot) != scePadStateExecCmd) {
196 *phase = PAD_PHASE_QUERY;
197 }
198
199 break;
200 case PAD_PHASE_ACTUATOR_CHECK:
201 if (scePadInfoAct(port, slot, -1, 0) == 0) {
202 *phase = PAD_PHASE_READY;
203 }
204
205 status->actuator[0] = 0;
206 status->actuator[1] = 1;
207
208 for (int i = 2; i < 6; i++) {
209 status->actuator[i] = 255;
210 }
211
212 if (scePadSetActAlign(port, slot, status->actuator)) {
213 (*phase)++;
214 }
215
216 break;
217 case PAD_PHASE_ACTUATOR_WAIT:
218 if (scePadGetState(port, slot) != scePadStateExecCmd) {
219 *phase = PAD_PHASE_READY;
220 }
221
222 break;
223 default:
224 if (*state == scePadStateStable || *state == scePadStateFindCTP1) {
225 int result = pad_button_read(status, port, slot);
226 *read_result = result;
227
228 if (result != 0) {
229 if (*previous_read_result != 0 && *read_result != *previous_read_result) {
230 *previous_read_result = 0;
231 *phase = PAD_PHASE_QUERY;
232 } else {
233 valid = 1;
234 }
235
236 *previous_read_result = *read_result;
237 }
238 }
239
240 break;
241 }
242
243 if (!valid) {
244 status->button = 0;
245 status->left_y = 128;
246 status->left_x = 128;
247 status->right_y = 128;
248 status->right_x = 128;
249 }
250
251 if (*read_result == 4) {
252 status->left_y = 128;
253 status->left_x = 128;
254 status->right_y = 128;
255 status->right_x = 128;
256 }
257
258 return 0;
259}
260
262 static int cnt;
263 static char init;
264
265 if (!init) {
266 cnt = 0;
267 init = 1;
268 }
269
270 int value;
271 int copy_count;
272 int *destination;
273 int *source;
274 source = &pad[0].input.status.button;
275 destination = &previous_pad[0].input.status.button;
276 copy_count = 19;
277
278 do {
279 value = *source;
280 source++;
281 copy_count--;
282 *destination = value;
283 destination++;
284 } while (copy_count > 0);
285
286 read_pad(&pad[0].input.status, 0, 0);
287
288 source = &pad[1].input.status.button;
289 destination = &previous_pad[1].input.status.button;
290 copy_count = 19;
291
292 do {
293 value = *source;
294 source++;
295 copy_count--;
296 *destination = value;
297 destination++;
298 } while (copy_count > 0);
299
300 read_pad(&pad[1].input.status, 1, 0);
301
302 int i;
303
304 for (i = 0; i < 2; i++) {
305 int threshold = axis_threshold[i];
306
307 if (threshold < 0) {
308 threshold = 0;
309 }
310
311 if (threshold > 0) {
312 if (threshold < GetLX()) {
313 pad[i].input.status.button |= 0x2000;
314 }
315
316 if (GetLX() < -threshold) {
317 pad[i].input.status.button |= 0x8000;
318 }
319
320 if (threshold < GetLY()) {
321 pad[i].input.status.button |= 0x4000;
322 }
323
324 if (GetLY() < -threshold) {
325 pad[i].input.status.button |= 0x1000;
326 }
327 }
328 }
329
330 int bit = 1;
331 PAD_REPEAT *repeat_state = &repeat[0];
332
333 for (i = 0; i < 32; i++, bit <<= 1) {
334 if (repeat_state->enabled & bit) {
335 if ((pad[0].input.status.button & repeat_state->enabled) & bit) {
336 repeat_state->counter[i]++;
337
338 if (repeat_state->counter[i] >= repeat_state->initial_delay[i]) {
339 repeat_state->active |= bit;
340 }
341 } else {
342 repeat_state->counter[i] = 0;
343 repeat_state->active &= ~bit;
344 }
345
346 if (repeat_state->counter[i] >= repeat_state->repeat_delay[i] && (repeat_state->active & bit)) {
347 pad[0].input.status.button &= ~bit;
348 repeat_state->counter[i] = 0;
349 }
350 }
351 }
352
353 for (i = 0; i < 2; i++) {
354 if ((pad[i].input.status.button & PAD_UP) && (pad[i].input.status.button & PAD_DOWN)) {
355 pad[i].input.status.button &= ~(PAD_UP | PAD_DOWN);
356 }
357
358 if ((pad[i].input.status.button & PAD_RIGHT) && (pad[i].input.status.button & PAD_LEFT)) {
359 pad[i].input.status.button &= ~(PAD_RIGHT | PAD_LEFT);
360 }
361 }
362
363#ifndef PAL
364 key_lock2 = 1;
365 pad[1].input.status.button = 0;
366 pad[1].input.status.right_x = 128;
367 pad[1].input.status.right_y = 128;
368 pad[1].input.status.left_x = 128;
369 pad[1].input.status.left_y = 128;
370
371#endif
372 if (key_lock2) {
373 pad[1].input.status.button = 0;
374 pad[1].input.status.right_x = 128;
375 pad[1].input.status.right_y = 128;
376 pad[1].input.status.left_x = 128;
377 pad[1].input.status.left_y = 128;
378 }
379
380 cnt = !((bool) cnt);
381}
382
383#undef PAD_STATIC_COUNTER_128
384#undef PAD_STATIC_COUNTER_64
385#undef PAD_STATIC_COUNTER_32
386#undef PAD_STATIC_COUNTER_16
387#undef PAD_STATIC_COUNTER_8
388#undef PAD_STATIC_COUNTER_4
389#undef PAD_STATIC_COUNTER_2
390#undef PAD_STATIC_COUNTER_1
391
393 for (int i = 0; i < 2; i++) {
394 if (!vibration_enabled) {
395 pad[i].input.status.vibration[0] = 0;
396 pad[i].input.status.vibration[1] = 0;
397 }
398
399 if (pad[i].input.status.vibration_timer[0] > 0) {
401 } else {
403 pad[i].input.status.vibration[0] = 0;
404 }
405
406 if (pad[i].input.status.vibration_timer[1] > 0) {
408 } else {
410 pad[i].input.status.vibration[1] = 0;
411 }
412 }
413
414 scePadSetActDirect(0, 0, pad[0].input.status.vibration);
415}
416
417int AxisCalibration(int axis) {
418 int zero;
419 int calibrated = axis - 128;
420 zero = 0;
421
422 if (calibrated < 50 && calibrated > -50) {
423 calibrated = zero;
424 } else if (calibrated > zero) {
425 calibrated = ((calibrated - 49) << 7) / 78;
426 } else {
427 calibrated = ((calibrated + 50) << 7) / 78;
428 }
429
430 return calibrated;
431}
432
434 return AxisCalibration(pad[0].input.status.right_x);
435}
436
438 return AxisCalibration(pad[0].input.status.right_y);
439}
440
442 return AxisCalibration(pad[0].input.status.left_x);
443}
444
446 return AxisCalibration(pad[0].input.status.left_y);
447}
448
450 return AxisCalibration(pad[1].input.status.right_x);
451}
452
454 return AxisCalibration(pad[1].input.status.right_y);
455}
456
458 return AxisCalibration(pad[1].input.status.left_x);
459}
460
462 return AxisCalibration(pad[1].input.status.left_y);
463}
464
466 return pad[1].input.status.button | pad[0].input.status.button | GetRX() | GetRY() | GetLX() | GetLY() | GetRX2() | GetRY2() | GetLX2() | GetLY2();
467}
468
470 int i;
471 int bit = 1;
472 PAD_REPEAT *auto_repeat = &repeat[0];
473
474 for (i = 0; i < 32; i++, bit <<= 1) {
475 if (mask & bit) {
476 auto_repeat->enabled &= ~bit;
477 auto_repeat->active &= ~bit;
478 auto_repeat->counter[i] = 0;
479 auto_repeat->initial_delay[i] = 0;
480 auto_repeat->repeat_delay[i] = 0;
481 }
482 }
483}
484
485void CGamePad::SetAutoRepeat(int mask, int initial_delay, int repeat_delay) {
486 int i;
487 int bit = 1;
488 PAD_REPEAT *auto_repeat = &repeat[0];
489
490 for (i = 0; i < 32; i++, bit <<= 1) {
491 if (!(auto_repeat->enabled & bit) && (mask & bit)) {
492 auto_repeat->enabled |= bit;
493 auto_repeat->active &= ~bit;
494 auto_repeat->counter[i] = 0;
495 auto_repeat->initial_delay[i] = initial_delay;
496 auto_repeat->repeat_delay[i] = repeat_delay;
497 }
498 }
499}
500
501void CGamePad::KeyLock(int mask) {
502 key_lock = mask;
503}
504
505void CGamePad::KeyLock2(int mask) {
506 key_lock2 = mask;
507}
508
510 if (key_lock) {
511 return 0;
512 }
513
514 return pad[0].input.status.button;
515}
516
518 if (key_lock) {
519 return 0;
520 }
521
522 return pad[0].input.status.button & ~previous_pad[0].input.status.button;
523}
524
526 if (key_lock) {
527 return 0;
528 }
529
530 return ~pad[0].input.status.button & previous_pad[0].input.status.button;
531}
532
534 return (float) GetRX() / 128.0f;
535}
536
538 return (float) GetRY() / 128.0f;
539}
540
542 return (float) GetLX() / 128.0f;
543}
544
546 return (float) GetLY() / 128.0f;
547}
548
550 return (float) GetLX2() / 128.0f;
551}
552
554 return (float) GetLY2() / 128.0f;
555}
556
557int CGamePad::On(int mask) {
558 if (key_lock) {
559 return 0;
560 }
561
562#ifdef PAL
563 if (mask == PAD_START && On2(PAD_START)) {
564 return 1;
565 }
566
567 if (mask != PAD_START && On2(PAD_SELECT)) {
568 return 1;
569 }
570
571#endif
572 return (pad[0].input.status.button & mask) != 0;
573}
574
575int CGamePad::On2(int mask) {
576 if (key_lock) {
577 return 0;
578 }
579
580 return (pad[1].input.status.button & mask) != 0;
581}
582
583int CGamePad::Down(int mask) {
584 if (key_lock) {
585 return 0;
586 }
587
588#ifdef PAL
589 if (mask == PAD_START && On2(PAD_START)) {
590 return 1;
591 }
592
593 if (mask != PAD_START && On2(PAD_SELECT)) {
594 return 1;
595 }
596
597#endif
598 return (mask & (pad[0].input.status.button & ~previous_pad[0].input.status.button)) != 0;
599}
600
601int CGamePad::Down2(int mask) {
602 if (key_lock) {
603 return 0;
604 }
605
606 return (mask & (pad[1].input.status.button & ~previous_pad[1].input.status.button)) != 0;
607}
608
612
613void CGamePad::MenuModeOn(int threshold) {
614 axis_threshold[0] = threshold;
615}
616
618 axis_threshold[0] = 0;
619}
620
621void CGamePad::SetVibration(int motor, int intensity, int duration) {
622 if (!vibration_enabled) {
623 return;
624 }
625
626 if (motor < 0 || motor >= 2 || duration < 0 || duration > 1200) {
627 return;
628 }
629
630 pad[0].input.status.vibration_timer[motor] = duration;
631
632 if (motor == 0) {
633 intensity = intensity != 0;
634 }
635
636 pad[0].input.status.vibration[motor] = intensity;
637}
638
640 vibration_enabled = enable;
641}
642
644 SetVibration(0, 0, 0);
645 SetVibration(1, 0, 0);
646 Step();
647}
void StopVibration()
Definition gamepad.cpp:643
int GetRY()
Definition gamepad.cpp:437
float GetLXf2()
Definition gamepad.cpp:549
void KeyLock2(int mask)
Definition gamepad.cpp:505
int GetPadDown()
Definition gamepad.cpp:517
int key_lock2
Definition gamepad.hpp:131
PAD_REPEAT repeat[2]
Definition gamepad.hpp:128
void UpDate()
Definition gamepad.cpp:261
float GetLYf2()
Definition gamepad.cpp:553
int vibration_enabled
Definition gamepad.hpp:132
int GetLX()
Definition gamepad.cpp:441
int GetLY()
Definition gamepad.cpp:445
void Step()
Definition gamepad.cpp:392
int GetRX2()
Definition gamepad.cpp:449
void Init()
Definition gamepad.cpp:16
int On(int mask)
Definition gamepad.cpp:557
int AllOn()
Definition gamepad.cpp:465
void KeyLock(int mask)
Definition gamepad.cpp:501
int On2(int mask)
Definition gamepad.cpp:575
PAD_DATA pad[2]
Definition gamepad.hpp:125
int Down2(int mask)
Definition gamepad.cpp:601
float GetRYf()
Definition gamepad.cpp:537
int Down(int mask)
Definition gamepad.cpp:583
void MenuModeOff()
Definition gamepad.cpp:617
int key_lock
Definition gamepad.hpp:130
int GetPadOn()
Definition gamepad.cpp:509
int GetRX()
Definition gamepad.cpp:433
PAD_DATA previous_pad[2]
Definition gamepad.hpp:126
int GetPadUp()
Definition gamepad.cpp:525
void VibrationEnable(int enable)
Definition gamepad.cpp:639
int GetLX2()
Definition gamepad.cpp:457
void CancelAutoRepeat(int mask)
Definition gamepad.cpp:469
int GetRY2()
Definition gamepad.cpp:453
float GetLXf()
Definition gamepad.cpp:541
int GetLY2()
Definition gamepad.cpp:461
void MenuModeOn(int threshold)
Definition gamepad.cpp:613
float GetLYf()
Definition gamepad.cpp:545
void SetVibration(int motor, int intensity, int duration)
Definition gamepad.cpp:621
void SetAutoRepeat(int mask, int initial_delay, int repeat_delay)
Definition gamepad.cpp:485
int axis_threshold[2]
Definition gamepad.hpp:129
float GetRXf()
Definition gamepad.cpp:533
void AutoRepeatOff()
Definition gamepad.cpp:609
int repeat_delay[32]
Definition gamepad.hpp:116
int initial_delay[32]
Definition gamepad.hpp:115
int counter[32]
Definition gamepad.hpp:114
int right_y
Definition gamepad.hpp:68
int extended_id
Definition gamepad.hpp:72
int pad_mode
Definition gamepad.hpp:73
unsigned char actuator[6]
Definition gamepad.hpp:76
unsigned char vibration[6]
Definition gamepad.hpp:75
int right_x
Definition gamepad.hpp:69
int vibration_timer[2]
Definition gamepad.hpp:77
int previous_pad_mode
Definition gamepad.hpp:74
int vibration_timer[5]
Definition gamepad.hpp:102
struct PAD_DATA::@16 input
unsigned char vibration[6]
Definition gamepad.hpp:100
struct PAD_DATA::@17 actuator
PAD_STATUS status
Definition gamepad.hpp:92