-
Notifications
You must be signed in to change notification settings - Fork 4
/
inputmapper2.cpp
504 lines (434 loc) · 12.6 KB
/
inputmapper2.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include "minir.h"
#include "libretro.h"
#include "io.h"
#include "containers.h"
#include <ctype.h>
namespace {
class inputmapper2_impl : public inputmapper2 {
public:
//function<void(unsigned int id, bool down)> callback; // held in the parent class
//each button maps to a uint32, known as 'descriptor', which uniquely describes it
//format: ttttnnnn nnnnnnnn nnnnnnll llllllll
//tttt=type
// 0000=unknown, never happens and may be used for special purposes
// 0001=keyboard
// 0010=mouse
// 0011=gamepad
// others=TODO
//n=uint18; must be exactly this to fire
//l=uint10; must be at >= this to fire (masked off to 0 when used as key to the hashmaps)
//[TODO: actually do as promised with the Ls]
//none of those descriptors may leave this file
//
//each type has their own rules for how events are mapped to descriptors
//keyboard:
// n = 00kk kkkscccc cccccc
// k=keyboard ID, 1-31, or 0 for any; if more than 31, loops back to 16
// s=is scancode flag
// c=if s is set, scancode, otherwise libretro code
//others: TODO
//
//TODO: various input sources are not buttons, the following exist and may require changing this one:
//analog/discrete
//centering/positioned/unpositioned
// button (keyboard)
// already solved
// self-centering analog (joystick)
// maybe I should define some of the bits to be some kind of minimum value? The data structures are not prepared for that...
// how will I solve issues with rounding errors? I don't want the device to fire events if it jitters across the limit.
// do I use the padding in the keydata? That should be a good solution. There's no padding on 32bit, but 32bit is dying anyways.
// events must contain the positions, I can't just turn them into booleans
// unpositioned analog (mouse, when grabbed)
// fire an event each time it moves
// merge events? probably not
// positioned analog (mouse pointer)
// fire an event for each change
// do I send delta or absolute position? probably absolute
// unpositioned discrete (mouse wheel)
// make it fire press events, but never release and never be held? Fire release instantly?
// wheels can be analog too; none are in practice, but MS docs say they can be
//maybe I should make some of the events bypass this structure entirely?
typedef uint32_t keydesc_t;
typedef uint16_t keylevel_t;
typedef uint16_t keyslot_t;
typedef keydesc_t n_keydesc_t;
typedef keyslot_t n_keyslot_t;
struct keydata {
multiint<keydesc_t> keys;//must be held for this to fire
keydesc_t primary;
keyslot_t level;//used by some event sources; 0 if the slot is unused
bool active;//whether this one is set (only valid for entries in 'mappings', not child items)
bool trigger;//whether this one is trigger-only (false: level-based)
keydata* next;//if multiple key combos are mapped to the same slot, this is non-NULL
keydata() : primary(0), level(0), active(false), next(NULL) {}
};
class kb {
kb(){} // cannot instantiate this one
private: static keydesc_t compile_key(unsigned int button, unsigned int scancode)
{
if (button) return (0<<10 | button<<0);
else return (1<<10 | scancode<<0);
}
private: static keydesc_t compile(unsigned int device, unsigned int key)
{
//the device is known to be in the correct range already
return dev_kb<<28 | (device<<11|key)<<10;
}
public: static keydesc_t compile(uint16_t device, uint16_t item, int16_t level)
{
if (device > 31) device = (device&15)|16;
return compile(device, item);
}
public: static keydesc_t global(keydesc_t desc)
{
return desc&~(31 << 21);
}
private: static unsigned int parse_keyname(const char * name, const char * * nameend)
{
size_t len;
for (len=0;isalnum(name[len]);len++) {}
if (nameend) *nameend = name+len;
if (name[0]=='x')
{
//validate the input - we require only uppercase hex, with 0x banned
for (size_t test=1;test<len;test++)
{
if (!isxdigit(name[test]) || islower(name[test])) return 0;
}
unsigned int keyid = strtoul(name+1, NULL, 16);
if (keyid >= 0x400) return 0;
return compile_key(0, keyid);
}
else
{
const char * const * keynames = inputkb::keynames();
for (unsigned int i=0;i<RETROK_LAST;i++)
{
if (keynames[i] && !strncmp(name, keynames[i], len) && !keynames[i][len])
{
return compile_key(i, 0);
}
}
return 0;
}
}
public: static keydata parse(const char * desc)
{
unsigned int kbid;
if (isdigit(*desc))
{
kbid = strtoul(desc, (char**)&desc, 10);
if (kbid<=0 || kbid>=32) return keydata();
}
else kbid = 0;
if (desc[0]!=':' || desc[1]!=':') return keydata();
desc += 2;
keydata ret;
while (true)
{
keydesc_t keyid = parse_keyname(desc, &desc);
if (keyid==0) return keydata();
keydesc_t key = compile(kbid, keyid);
if (*desc=='+')
{
ret.keys.add(key);
desc++;
}
else if (*desc=='\0' || *desc==',')
{
ret.keys.add(key);
ret.keys.sort();
ret.primary = key;
ret.level = 1;
return ret;
}
else return keydata();
}
}
};
class bydev {
bydev(){} // cannot instantiate this one
public: static keydesc_t compile(dev_t type, uint16_t device, uint16_t item, int16_t level)
{
switch (type)
{
case dev_unknown: return 0;
case dev_kb: return kb::compile(device, item, level);
case dev_mouse: return 0;
case dev_gamepad: return 0;
default: return 0;
}
}
public: static keydesc_t global(keydesc_t desc)
{
switch (desc>>28)
{
case dev_unknown: return 0;
case dev_kb: return kb::global(desc);
case dev_mouse: return 0;
case dev_gamepad: return 0;
default: return 0;
}
}
private: static keydata parse_single(const char * desc)
{
if (desc[0]=='K' && desc[1]=='B') return kb::parse(desc+2);
return keydata();
}
public: static keydata parse(const char * desc)
{
const char * next = strchr(desc, ',');
if (next)
{
keydata first = parse_single(desc);
next++;
while (isspace(*next)) next++;
keydata second = parse(next);
if (first.level && second.level)
{
first.next = malloc(sizeof(keydata));
new(first.next) keydata(second);
return first;
}
else if (first.level) return first;
else return second;
}
return parse_single(desc);
}
};
array<keydata> mappings;//the key is a slot ID
keyslot_t firstempty;//any slot such that all previous slots are used
hashmap<keydesc_t, multiint<keyslot_t> > keylist;//returns which slots are affected by this descriptor
bool keymod_valid;
hashmap<keydesc_t, multiint<keydesc_t> > keymod;//returns which descriptors are modifiers for this one
hashmap<keydesc_t, uint8_t> keyheld;
/*private*/ void keymod_regen()
{
if (keymod_valid) return;
keymod_valid = true;
for (n_keyslot_t i=0;i<mappings.len();i++)
{
keydata* key = &mappings[i];
while (key)
{
keydesc_t numkeys;
keydesc_t* keys = key->keys.get(numkeys);
for (keydesc_t j=0;j<numkeys;j++)
{
keymod.get(key->primary).add(keys[j]);
}
key = key->next;
}
}
}
/*private*/ template<typename K, typename V, typename K2, typename V2>
void multimap_remove(hashmap<K, multiint<V> >& map, K2 key, V2 val)
{
multiint<V>* items = map.get_ptr(key);
if (!items) return;
items->remove(val);
if (items->count() == 0) map.remove(key);
}
/*private*/ void keydata_add(keydata& key, keyslot_t id)
{
mappings[id] = key;
keydata* iter = &key;
while (iter)
{
n_keydesc_t numkeys;
keydesc_t* keys = iter->keys.get(numkeys);
for (n_keydesc_t i=0;i<numkeys;i++) keylist.get(keys[i]).add(id);
iter = iter->next;
}
}
/*private*/ void keydata_delete(keyslot_t id)
{
keydata* key = &mappings[id];
bool first = true;
while (key)
{
keydata* next = key->next;
keydesc_t numkeys;
keydesc_t* keys = key->keys.get(numkeys);
for (keydesc_t i=0;i<numkeys;i++) multimap_remove(keylist, keys[i], id);
//keydata_delete_single(key, id);
if (first)
{
key->level = 0;
first=false;
}
else
{
key->~keydata();
free(key);
}
key = next;
}
if (id < firstempty) firstempty = id;
}
bool register_button(unsigned int id, const char * desc, bool trigger)
{
keymod.reset();
keymod_valid = false;
keydata_delete(id);
if (desc)
{
keydata newkey = bydev::parse(desc);
if (!newkey.level) goto fail;
newkey.trigger = trigger;
keydata_add(newkey, id);
return true;
}
//fall through to fail - it's not a failure, but the results are the same.
fail:
if (id < firstempty) firstempty = id;
return false;
}
unsigned int register_group(unsigned int len)
{
while (true)
{
unsigned int n=0;
while (true)
{
if (mappings[firstempty+n].level != 0)
{
firstempty += n+1;
break;
}
n++;
if (n == len) return firstempty;
}
}
}
//enum dev_t {
// dev_unknown,
// dev_kb,
// dev_mouse,
// dev_gamepad,
//};
//enum { mb_left, mb_right, mb_middle, mb_x4, mb_x5 };
//type is an entry in the dev_ enum.
//device is which item of this type is relevant. If you have two keyboards, pressing A on both
// should give different values here. If they're distinguishable, use 1 and higher; if not, use 0.
//button is the 'common' ID for that device.
// For keyboard, it's a RETROK_* (not present here, include libretro.h). For mouse, it's the mb_* enum. For gamepads, [TODO]
//scancode is a hardware-dependent unique ID for that key. If a keyboard has two As, they will
// have different scancodes. If a key that doesn't map to any RETROK (Mute, for example), the
// common ID will be 0, and scancode will be something valid. Scancodes are still present for non-keyboards.
//down is the new state of the button. Duplicate events are fine and will be ignored.
/*private*/ n_keydesc_t num_held_in_set(multiint<keydesc_t>* set)
{
if (!set) return 0;
n_keydesc_t count = 0;
n_keydesc_t len;
keydesc_t* items = set->get(len);
for (n_keydesc_t i=0;i<len;i++)
{
if (keyheld.get_or(items[i], false)) count++; // do not just add, some elements have values outside [0,1]
}
return count;
}
/*private*/ bool slot_active_single(keydata* data, keydesc_t primary)
{
keymod_regen();
multiint<keydesc_t>* modsforthis = &data->keys;
multiint<keydesc_t>* modsfor = keymod.get_ptr(data->primary);
//we want to know if every element in modsforthis is held, but no other in modsfor
//since every element in modsforthis is in modsfor, these checks are sufficient
//they don't tell which keys have wrong state, but we're not interested either.
if (modsforthis->count() == num_held_in_set(modsforthis) &&
modsforthis->count() == num_held_in_set(modsfor) &&
(!primary || data->primary == primary))
{
return true;
}
else return false;
}
/*private*/ bool slot_active(keyslot_t slot, keydesc_t primary)
{
keydata* data = &mappings[slot];
while (data)
{
if (slot_active_single(data, primary)) return true;
data = data->next;
}
return false;
}
/*private*/ void send_event(keydesc_t trigger, bool down)
{
n_keyslot_t numslots;
keyslot_t* slots = keylist.get(trigger).get(numslots);
printf("%.8X try %i\n",trigger,numslots);
for (n_keyslot_t i=0;i<numslots;i++)
{
keydata& keydat = mappings[slots[i]];
bool down = slot_active(slots[i], keydat.trigger ? trigger : 0);
if (keydat.trigger)
{
if (down) callback(slots[i], true);
}
else
{
if (down != keydat.active) callback(slots[i], down);
keydat.active = down;
}
}
}
void event(dev_t type, uint16_t device, uint16_t item, int16_t level)
{
bool down = level;//TODO: figure out for non-keyboards
keydesc_t trigger = bydev::compile(type, device, item, level);
keydesc_t gtrigger = bydev::global(trigger);
uint8_t& state = keyheld.get(trigger);
if ((bool)state == down) return;
state = down;
if (trigger != gtrigger)
keyheld.get(gtrigger) += (down ? 1 : -1);
send_event(trigger, down);
if (trigger != gtrigger)
send_event(gtrigger, down);
}
bool query(dev_t type, unsigned int device, unsigned int button, unsigned int scancode)
{
keydesc_t trigger = bydev::compile(type, device, button, scancode);
return keyheld.get_or(trigger, false);
}
bool query_slot(unsigned int slot)
{
return slot_active(slot, 0);
}
void reset(dev_t type)
{
//TODO: this makes a bunch of keydata.active states inconsistent with keyheld
if (type == dev_unknown)
{
keyheld.reset();
}
else
{
keyheld.remove_if(bind_ptr(&inputmapper2_impl::reset_cond, (void*)(uintptr_t)type));
}
}
/*private*/ static bool reset_cond(void* type, keydesc_t key, uint8_t& value)
{
return (key>>28 == (uintptr_t)type);
}
inputmapper2_impl()
{
keymod_valid = false;
firstempty = 0;
}
};
}
inputmapper2* inputmapper2::create()
{
return new inputmapper2_impl;
}
//typedef unsigned int triggertype;
//enum {
// tr_press = 1, // The slot is now held, and wasn't before.
// tr_release = 2, // The slot is now released, and wasn't before.
// tr_primary = 4, // The primary key for this slot was pressed.
// //Only a few of the combinations (1, 2, 4, 5) are possible.
//};