-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpad_translator.cpp
353 lines (309 loc) · 9.06 KB
/
pad_translator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* author : Shuichi TAKANO
* since : Sat Sep 09 2023 14:41:26
*/
#include "pad_translator.h"
#include <algorithm>
#include <cstdio>
#include "serializer.h"
#include "debug.h"
int getLevel(PadConfig::AnalogPos p)
{
switch (p)
{
default:
case PadConfig::AnalogPos::H:
return 255;
case PadConfig::AnalogPos::MID:
return 128;
case PadConfig::AnalogPos::L:
return 0;
}
}
bool testHat(int hat, PadConfig::HatPos hatPos)
{
static constexpr int L = 1 << static_cast<int>(PadConfig::HatPos::LEFT);
static constexpr int R = 1 << static_cast<int>(PadConfig::HatPos::RIGHT);
static constexpr int U = 1 << static_cast<int>(PadConfig::HatPos::UP);
static constexpr int D = 1 << static_cast<int>(PadConfig::HatPos::DOWN);
static constexpr int table[] = {
U,
U | R,
R,
D | R,
D,
L | D,
L,
L | U,
};
if (hat >= 0 && hat < 8)
{
return table[hat] & (1 << static_cast<int>(hatPos));
}
return false;
}
PadConfig::AnalogPos getAnalogPos(int v)
{
if (v < 85)
{
return PadConfig::AnalogPos::L;
}
if (v > 170)
{
return PadConfig::AnalogPos::H;
}
return PadConfig::AnalogPos::MID;
}
bool PadConfig::Unit::testAnalog(uint8_t v) const
{
int lv0 = getLevel(analogOff);
int lv1 = getLevel(analogOn);
int d0 = lv0 - v;
int d1 = lv1 - v;
return d0 * d0 > d1 * d1;
}
void PadConfig::Unit::dump() const
{
constexpr char typeTable[] = {'-', 'B', 'A', 'H'};
constexpr char apTable[] = {'H', 'M', 'L'};
constexpr char hatTable[] = {'L', 'R', 'U', 'D'};
DPRINT(("%c[%d]:A%c,%c:H%c: %d\n",
typeTable[(int)type],
number,
apTable[(int)analogOn],
apTable[(int)analogOff],
hatTable[(int)hatPos],
index));
}
bool PadConfig::convertButton(int i, const uint32_t *buttons, int nButtons,
const int *analogs, int nAnalogs, int hat) const
{
if (static_cast<size_t>(i) >= buttons_.size())
{
return false;
}
const auto &cfg = buttons_[i];
switch (cfg.type)
{
case Type::BUTTON:
return buttons[cfg.number >> 5] & (1u << (cfg.number & 31));
case Type::ANALOG:
return cfg.testAnalog(analogs[cfg.number]);
case Type::HAT:
return testHat(hat, cfg.hatPos);
default:
break;
}
return false;
}
int8_t PadConfig::convertAnalog(int i, const uint32_t *buttons, int nButtons,
const int *analogs, int nAnalogs, int hat) const
{
// todo
return 0;
}
PadConfig::PadConfig(int vid, int pid, int outPortOfs,
const std::vector<Unit> &buttons,
const std::vector<Unit> &analogs)
: buttons_(buttons), analogs_(analogs), vid_(vid), pid_(pid), outPortOfs_(outPortOfs)
{
}
void PadConfig::dump() const
{
if (!buttons_.empty())
{
DPRINT(("buttons\n"));
for (auto &v : buttons_)
{
v.dump();
}
}
if (!analogs_.empty())
{
DPRINT(("analogs\n"));
for (auto &v : analogs_)
{
v.dump();
}
}
}
void PadConfig::serialize(Serializer &s) const
{
s.append16u(vid_);
s.append16u(pid_);
s.append8u(outPortOfs_);
auto writeUnit = [&](const Unit &v)
{
s.append8u(static_cast<uint8_t>(v.type));
s.append8u(v.number);
s.append8u(static_cast<uint8_t>(v.analogOn));
s.append8u(static_cast<uint8_t>(v.analogOff));
s.append8u(static_cast<uint8_t>(v.hatPos));
s.append8u(v.index);
s.append8u(v.subIndex);
s.append8u(v.inPortOfs);
};
s.append16u(buttons_.size());
for (auto &v : buttons_)
{
writeUnit(v);
}
s.append8u(analogs_.size());
for (auto &v : analogs_)
{
writeUnit(v);
}
}
PadConfig::PadConfig(Deserializer &s)
{
vid_ = s.peek16u();
pid_ = s.peek16u();
outPortOfs_ = s.peek8u();
auto readUnit = [&]()
{
Unit v;
v.type = static_cast<Type>(s.peek8u());
v.number = s.peek8u();
v.analogOn = static_cast<AnalogPos>(s.peek8u());
v.analogOff = static_cast<AnalogPos>(s.peek8u());
v.hatPos = static_cast<HatPos>(s.peek8u());
v.index = s.peek8u();
v.subIndex = s.peek8u();
v.inPortOfs = s.peek8u();
return v;
};
auto n = s.peek16u();
buttons_.reserve(n);
for (auto i = 0u; i < n; ++i)
{
buttons_.push_back(readUnit());
}
n = s.peek8u();
analogs_.reserve(n);
for (auto i = 0u; i < n; ++i)
{
analogs_.push_back(readUnit());
}
}
/////////
/////////
PadTranslator::PadTranslator()
{
/*
// default pad configs
using T = PadConfig::Type;
using AP = PadConfig::AnalogPos;
using HP = PadConfig::HatPos;
// CMD LRUD ABCDEF Coin Start
std::vector<PadConfig> defaultConfigs = {
// BUFFALO BGC-FC801
{0x411, 0xc6, {
{T::BUTTON, 4, {}, {}, {}, 0}, // CMD
{T::ANALOG, 0, AP::L, AP::MID, {}, 1}, // LEFT
{T::ANALOG, 0, AP::H, AP::MID, {}, 2}, // RIGHT
{T::ANALOG, 1, AP::L, AP::MID, {}, 3}, // UP
{T::ANALOG, 1, AP::H, AP::MID, {}, 4}, // DOWN
{T::BUTTON, 0, {}, {}, {}, 5}, // A
{T::BUTTON, 1, {}, {}, {}, 6}, // B
{T::BUTTON, 2, {}, {}, {}, 7}, // C
{T::BUTTON, 3, {}, {}, {}, 8}, // D
{T::BUTTON, 5, {}, {}, {}, 9}, // E
{T::NA, 0, {}, {}, {}, 10}, // F
{T::BUTTON, 6, {}, {}, {}, 11}, // COIN
{T::BUTTON, 7, {}, {}, {}, 12}, // START
},
{{}}},
// DualShock4
{0x54c, 0x9cc, {
{T::BUTTON, 2, {}, {}, {}, 0}, // CMD
{T::HAT, 0, {}, {}, HP::LEFT, 1}, // LEFT
{T::HAT, 0, {}, {}, HP::RIGHT, 2}, // RIGHT
{T::HAT, 0, {}, {}, HP::UP, 3}, // UP
{T::HAT, 0, {}, {}, HP::DOWN, 4}, // DOWN
{T::BUTTON, 0, {}, {}, {}, 5}, // A
{T::BUTTON, 1, {}, {}, {}, 6}, // B
{T::BUTTON, 2, {}, {}, {}, 7}, // C
{T::BUTTON, 3, {}, {}, {}, 8}, // D
{T::BUTTON, 4, {}, {}, {}, 9}, // E
{T::BUTTON, 5, {}, {}, {}, 10}, // F
{T::BUTTON, 8, {}, {}, {}, 11}, // COIN
{T::BUTTON, 9, {}, {}, {}, 12}, // START
},
{{}}},
};
configs_ = std::move(defaultConfigs);
sort();
*/
}
void PadTranslator::reset()
{
configs_.clear();
}
PadConfig *PadTranslator::_find(int vid, int pid, int portOfs)
{
PadConfig::DeviceID id{vid, pid, portOfs};
auto p = std::partition_point(configs_.begin(), configs_.end(),
[&](const PadConfig &v)
{ return v.getDeviceID() < id; });
if (p != configs_.end() && p->getDeviceID() == id)
{
return &*p;
}
return nullptr;
}
const PadConfig *PadTranslator::find(int vid, int pid, int portOfs) const
{
auto p = const_cast<PadTranslator *>(this)->_find(vid, pid, portOfs);
return p ? p : &defaultConfig_;
}
void PadTranslator::sort()
{
std::sort(configs_.begin(), configs_.end(), [&](auto &a, auto &b)
{ return a.getDeviceID() < b.getDeviceID(); });
}
void PadTranslator::append(PadConfig &&cnf)
{
cnf.dump();
if (auto *p = _find(cnf.getVID(), cnf.getPID(), cnf.getOutPortOfs()))
{
DPRINT(("replace config %04x, %04x, %d\n", cnf.getVID(), cnf.getPID(), cnf.getOutPortOfs()));
*p = std::move(cnf);
}
else
{
DPRINT(("new config %04x, %04x, %d\n", cnf.getVID(), cnf.getPID(), cnf.getOutPortOfs()));
configs_.push_back(std::move(cnf));
sort();
}
}
void PadTranslator::serialize(Serializer &s) const
{
s.append(configs_.size());
int n = 0;
for (auto &v : configs_)
{
if (s.exceedLimit())
{
s.append8u(0);
}
s.append8u(1); // enabled
v.serialize(s);
++n;
}
DPRINT(("store %d/%d configs.\n", n, configs_.size()));
}
void PadTranslator::deserialize(Deserializer &s)
{
configs_.clear();
auto nn = s.peek();
DPRINT(("%d configs...\n", nn));
int n = 0;
while (n < nn && s.peek8u())
{
PadConfig cnf(s);
configs_.push_back(std::move(cnf));
++n;
}
DPRINT(("load %d/%d configs.\n", n, nn));
sort();
}