77 lines2.2 KBC
1/*
2input_ps3.c: DualShock 3 / DualShock 4 through PSL1GHT's ioPad. Bits of
3padData.button[2..3] as NZ:P PS3's in_ps3.c (from TyrQuakeCell) reads them.
4*/
5#include <io/pad.h>
6#include "../plat.h"
7#include "../input.h"
8#include "ps3.h"
9
10#define MAX_PADS 7
11
12#define P_L2 0x0001
13#define P_R2 0x0002
14#define P_L1 0x0004
15#define P_R1 0x0008
16#define P_TRIANGLE 0x0010
17#define P_CIRCLE 0x0020
18#define P_CROSS 0x0040
19#define P_SQUARE 0x0080
20#define P_SELECT 0x0100
21#define P_L3 0x0200
22#define P_R3 0x0400
23#define P_START 0x0800
24#define P_UP 0x1000
25#define P_RIGHT 0x2000
26#define P_DOWN 0x4000
27#define P_LEFT 0x8000
28
29static const struct { unsigned pad; uint32_t btn; } map[] = {
30 {P_UP, BTN_UP}, {P_DOWN, BTN_DOWN}, {P_LEFT, BTN_LEFT}, {P_RIGHT, BTN_RIGHT},
31 {P_CROSS, BTN_CROSS}, {P_CIRCLE, BTN_CIRCLE}, {P_SQUARE, BTN_SQUARE}, {P_TRIANGLE, BTN_TRIANGLE},
32 {P_L1, BTN_L1}, {P_R1, BTN_R1}, {P_L2, BTN_L2}, {P_R2, BTN_R2},
33 {P_START, BTN_START}, {P_SELECT, BTN_SELECT}, {P_L3, BTN_L3}, {P_R3, BTN_R3},
34};
35
36// ioPadGetData only has news when something changed: keep the last
37static unsigned last_buttons[MAX_PADS];
38static unsigned char last_lx[MAX_PADS], last_ly[MAX_PADS];
39
40void ps3_input_init(void)
41{
42 plat_log("input: ioPadInit = %d", (int)ioPadInit(MAX_PADS));
43 for (int i = 0; i < MAX_PADS; i++) {
44 last_lx[i] = last_ly[i] = 128;
45 }
46}
47
48uint32_t plat_input_held(void)
49{
50 padInfo info;
51 padData data;
52 uint32_t held = 0;
53
54 if (ioPadGetInfo(&info) != 0)
55 return 0;
56 for (int p = 0; p < MAX_PADS; p++) {
57 if (!info.status[p]) {
58 last_buttons[p] = 0;
59 last_lx[p] = last_ly[p] = 128;
60 continue;
61 }
62 if (ioPadGetData(p, &data) == 0 && data.len > 0) {
63 last_buttons[p] = ((unsigned)data.button[2] << 8) | (data.button[3] & 0xff);
64 last_lx[p] = (unsigned char)data.button[6];
65 last_ly[p] = (unsigned char)data.button[7];
66 }
67 for (unsigned i = 0; i < sizeof(map) / sizeof(map[0]); i++)
68 if (last_buttons[p] & map[i].pad)
69 held |= map[i].btn;
70 // the left stick as a d-pad
71 if (last_lx[p] < 40) held |= BTN_LEFT;
72 if (last_lx[p] > 215) held |= BTN_RIGHT;
73 if (last_ly[p] < 40) held |= BTN_UP;
74 if (last_ly[p] > 215) held |= BTN_DOWN;
75 }
76 return held;
77}