forked from mirror/dolphin-emu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControllerEmu.h
688 lines (568 loc) · 15.5 KB
/
ControllerEmu.h
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#ifndef _CONTROLLEREMU_H_
#define _CONTROLLEREMU_H_
// windows crap
#define NOMINMAX
#include <cmath>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include "GCPadStatus.h"
#include "ControllerInterface/ControllerInterface.h"
#include "IniFile.h"
#include "MathUtil.h"
#include "Timer.h"
#define sign(x) ((x)?(x)<0?-1:1:0)
enum
{
GROUP_TYPE_OTHER,
GROUP_TYPE_OPTIONS,
GROUP_TYPE_STICK,
GROUP_TYPE_MIXED_TRIGGERS,
GROUP_TYPE_BUTTONS,
GROUP_TYPE_FORCE,
GROUP_TYPE_EXTENSION,
GROUP_TYPE_TILT,
GROUP_TYPE_CURSOR,
GROUP_TYPE_TRIGGERS,
GROUP_TYPE_UDPWII,
GROUP_TYPE_SLIDER,
};
enum
{
SETTING_BACKGROUND_INPUT,
SETTING_SIDEWAYS_WIIMOTE,
SETTING_UPRIGHT_WIIMOTE,
SETTING_MOTIONPLUS,
SETTING_MOTIONPLUS_FAST,
SETTING_IR_HIDE,
};
enum
{
AS_LEFT,
AS_RIGHT,
AS_UP,
AS_DOWN,
AS_MODIFIER,
};
enum
{
AS_RADIUS,
AS_DEADZONE,
AS_SQUARE,
};
enum
{
B_THRESHOLD,
};
enum
{
B_RANGE,
B_THRESHOLD_R,
};
enum
{
F_FORWARD,
F_BACKWARD,
F_LEFT,
F_RIGHT,
F_UP,
F_DOWN,
};
enum
{
F_RANGE,
F_DEADZONE,
};
enum
{
T_FORWARD,
T_BACKWARD,
T_LEFT,
T_RIGHT,
T_UP,
T_DOWN,
};
enum
{
T_FAST = 6,
T_ACC_RANGE,
T_GYRO_RANGE_1,
T_GYRO_RANGE_2,
};
enum
{
T_MODIFIER = 6,
};
enum
{
T_ACC_RANGE_S,
T_GYRO_RANGE,
T_GYRO_SETTLE,
T_DEADZONE,
T_CIRCLESTICK,
T_ANGLE,
};
enum
{
T_RANGE,
T_DEADZONE_N,
T_CIRCLESTICK_N,
T_ANGLE_N,
};
enum
{
C_UP,
C_DOWN,
C_LEFT,
C_RIGHT,
C_FORWARD,
C_BACKWARD,
C_RANGE_MODIFIER,
C_HIDE,
C_SHOW,
};
enum
{
C_RANGE,
C_CENTER,
C_WIDTH,
C_HEIGHT,
};
const char * const named_directions[] =
{
"Up",
"Down",
"Left",
"Right"
};
class ControllerEmu
{
public:
class ControlGroup
{
public:
class Control
{
protected:
Control(ControllerInterface::ControlReference* const _ref, const char * const _name)
: control_ref(_ref), name(_name){}
public:
virtual ~Control();
ControllerInterface::ControlReference* const control_ref;
const char * const name;
};
class Input : public Control
{
public:
Input(const char * const _name)
: Control(new ControllerInterface::InputReference, _name) {}
};
class Output : public Control
{
public:
Output(const char * const _name)
: Control(new ControllerInterface::OutputReference, _name) {}
};
class Setting
{
public:
Setting(const char* const _name, const ControlState def_value
, const unsigned int _low = 0, const unsigned int _high = 100)
: name(_name)
, value(def_value)
, default_value(def_value)
, low(_low)
, high(_high)
, lastState(false)
{
control = new Input("");
}
void GetState();
const char* const name;
ControlState value;
const ControlState default_value;
const unsigned int low, high;
Control* control;
bool lastState;
};
ControlGroup(const char* const _name, const unsigned int _type = GROUP_TYPE_OTHER) : name(_name), type(_type) {}
virtual ~ControlGroup();
virtual void LoadConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" );
virtual void SaveConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" );
const char* const name;
const unsigned int type;
std::vector< Control* > controls;
std::vector< Setting* > settings;
};
class AnalogStick : public ControlGroup
{
public:
template <typename C>
void GetState(C* const x, C* const y, const unsigned int base, const unsigned int range)
{
// this is all a mess
ControlState yy = controls[0]->control_ref->State() - controls[1]->control_ref->State();
ControlState xx = controls[3]->control_ref->State() - controls[2]->control_ref->State();
ControlState radius = settings[AS_RADIUS]->value;
ControlState deadzone = settings[AS_DEADZONE]->value;
ControlState square = settings[AS_SQUARE]->value;
ControlState m = controls[AS_MODIFIER]->control_ref->State();
// modifier code
if (m)
{
yy = (fabsf(yy)>deadzone) * sign(yy) * (m + deadzone/2);
xx = (fabsf(xx)>deadzone) * sign(xx) * (m + deadzone/2);
}
// deadzone / square stick code
if (radius != 1 || deadzone || square)
{
// this section might be all wrong, but its working good enough, I think
ControlState ang = atan2(yy, xx);
ControlState ang_sin = sin(ang);
ControlState ang_cos = cos(ang);
// the amt a full square stick would have at current angle
ControlState square_full = std::min(ang_sin ? 1/fabsf(ang_sin) : 2, ang_cos ? 1/fabsf(ang_cos) : 2);
// the amt a full stick would have that was ( user setting squareness) at current angle
// I think this is more like a pointed circle rather than a rounded square like it should be
ControlState stick_full = (1 + (square_full - 1) * square);
ControlState dist = sqrt(xx*xx + yy*yy);
// dead zone code
dist = std::max(0.0f, dist - deadzone * stick_full);
dist /= (1 - deadzone);
// square stick code
ControlState amt = dist / stick_full;
dist -= ((square_full - 1) * amt * square);
// radius
dist *= radius;
yy = std::max(-1.0f, std::min(1.0f, ang_sin * dist));
xx = std::max(-1.0f, std::min(1.0f, ang_cos * dist));
}
*y = C(yy * range + base);
*x = C(xx * range + base);
}
AnalogStick(const char* const _name);
};
class Buttons : public ControlGroup
{
public:
Buttons(const char* const _name, bool has_range = false);
bool HasRange() { return m_has_range; }
template <typename C>
void GetState(C* const buttons, const C* bitmasks)
{
std::vector<Control*>::iterator
i = controls.begin(),
e = controls.end();
for (; i!=e; ++i, ++bitmasks)
{
if ((*i)->control_ref->State() > settings[m_has_range ? B_THRESHOLD_R : B_THRESHOLD]->value) // threshold
*buttons |= *bitmasks;
}
}
void GetState(ControlState* buttons)
{
auto range = settings[B_RANGE]->value;
std::vector<Control*>::iterator
i = controls.begin(),
e = controls.end();
for (; i!=e; ++i)
{
if ((*i)->control_ref->State() > settings[m_has_range ? B_THRESHOLD_R : B_THRESHOLD]->value)
*buttons++ = (sign((*i)->control_ref->State()) * range);
else
*buttons++ = 0.f;
}
}
private:
bool m_has_range;
};
class MixedTriggers : public ControlGroup
{
public:
template <typename C, typename S>
void GetState(C* const digital, const C* bitmasks, S* analog, const unsigned int range)
{
const unsigned int trig_count = ((unsigned int) (controls.size() / 2));
for (unsigned int i=0; i<trig_count; ++i,++bitmasks,++analog)
{
if (controls[i]->control_ref->State() > settings[0]->value) //threshold
{
*analog = range;
*digital |= *bitmasks;
}
else
{
*analog = S(controls[i+trig_count]->control_ref->State() * range);
}
}
}
MixedTriggers(const char* const _name);
};
class Triggers : public ControlGroup
{
public:
template <typename S>
void GetState(S* analog, const unsigned int range)
{
const unsigned int trig_count = ((unsigned int) (controls.size()));
const ControlState deadzone = settings[0]->value;
for (unsigned int i=0; i<trig_count; ++i,++analog)
*analog = S(std::max(controls[i]->control_ref->State() - deadzone, 0.0f) / (1 - deadzone) * range);
}
Triggers(const char* const _name);
};
class Slider : public ControlGroup
{
public:
template <typename S>
void GetState(S* const slider, const unsigned int range, const unsigned int base = 0)
{
const float deadzone = settings[0]->value;
const float state = controls[1]->control_ref->State() - controls[0]->control_ref->State();
if (fabsf(state) > deadzone)
*slider = (S)((state - (deadzone * sign(state))) / (1 - deadzone) * range + base);
else
*slider = 0;
}
Slider(const char* const _name);
};
class Force : public ControlGroup
{
public:
Force(const char* const _name);
template <typename C, typename R>
void GetState(C* axis, const u8 base, const R range, bool step = true)
{
const float master_range = settings[F_RANGE]->value;
const float deadzone = settings[F_DEADZONE]->value;
for (unsigned int i = 0; i < 3; i++)
{
float dz = 0;
const float state = controls[i*2 + 1]->control_ref->State() - controls[i*2]->control_ref->State();
if (fabsf(state) > deadzone)
dz = ((state - (deadzone * sign(state))) / (1 - deadzone));
if (step)
{
if (state > m_swing[i])
m_swing[i] = std::min(m_swing[i] + 0.1f, state);
else if (state < m_swing[i])
m_swing[i] = std::max(m_swing[i] - 0.1f, state);
}
// deceleration switch
m_state[i] = (abs(m_swing[i]) >= 0.7
? -2 * sign(state) + m_swing[i] * 2
: m_swing[i]) * sign(state);
*axis++ = (C)(m_state[i] * range * master_range + base);
}
}
private:
float m_swing[3], m_state[3];
};
class Tilt : public ControlGroup
{
public:
Tilt(const char* const _name, bool gyro = false);
bool HasGyro() { return m_has_gyro; }
template <typename C, typename R>
void GetState(C* const pitch, C* const roll, C* const yaw, const bool gyro = false, const unsigned int base = 0.0, const R range = 1.0, const bool step = true)
{
ControlState state;
ControlState r_acc = controls[m_has_gyro ? T_ACC_RANGE : T_RANGE]->control_ref->State();
auto const r_acc_s = settings[m_has_gyro ? T_ACC_RANGE_S : T_RANGE]->value;
auto const deadzone = settings[m_has_gyro ? T_DEADZONE : T_DEADZONE_N]->value;
auto const circle = settings[m_has_gyro ? T_CIRCLESTICK : T_CIRCLESTICK_N]->value;
auto const angle = settings[m_has_gyro ? T_ANGLE : T_ANGLE_N]->value / 1.8f;
ControlState settle = 0
, r_gyro_1 = 1.0
, r_gyro_2 = 1.0
, r_gyro_s = 1.0;
if (m_has_gyro)
{
r_gyro_1 = controls[T_GYRO_RANGE_1]->control_ref->State();
r_gyro_2 = controls[T_GYRO_RANGE_2]->control_ref->State();
r_gyro_s = settings[T_GYRO_RANGE]->value;
settle = settings[T_GYRO_SETTLE]->value;
}
for (unsigned int i = 0; i < 3; i++)
{
state = controls[i*2 + 1]->control_ref->State() - controls[i*2]->control_ref->State();
// deadzone
state *= fabsf(state)>deadzone
? (fabsf(state)-deadzone)/(1.0-deadzone)
: 0.0;
if (step)
{
if (state == m_acc[i])
m_settle[i]++;
else
m_settle[i] = 0;
// step towards state
if (state > m_acc[i])
m_acc[i] = std::min(m_acc[i] + 0.1f, state);
else if (state < m_acc[i])
m_acc[i] = std::max(m_acc[i] - 0.1f, state);
// step gyro towards 0
if (m_settle[i] > settle * 100.0)
{
if (0 > m_gyro[i])
m_gyro[i] = std::min(m_gyro[i] + 0.05f, 0.0f);
else if (0 < m_gyro[i])
m_gyro[i] = std::max(m_gyro[i] - 0.05f, 0.0f);
}
// step gyro towards state
else
{
if (m_acc[i] > m_gyro[i])
m_gyro[i] = std::min(m_gyro[i] + 0.05f, m_acc[i]);
else if (m_acc[i] < m_gyro[i])
m_gyro[i] = std::max(m_gyro[i] - 0.05f, m_acc[i]);
}
}
}
ControlState y = m_acc[0];
ControlState x = m_acc[1];
// circle stick
if (circle)
{
ControlState ang = atan2(y, x);
ControlState ang_sin = sin(ang);
ControlState ang_cos = cos(ang);
ControlState rad = sqrt(x*x + y*y);
// the amt a full square stick would have at current angle
ControlState square_full = std::min(ang_sin ? 1/fabsf(ang_sin) : 2, ang_cos ? 1/fabsf(ang_cos) : 2);
// the amt a full stick would have that was (user setting circular) at current angle
// I think this is more like a pointed circle rather than a rounded square like it should be
ControlState stick_full = (square_full * (1 - circle)) + (circle);
// dead zone
rad = std::max(0.0f, rad - deadzone * stick_full);
rad /= (1.0 - deadzone);
// circle stick
ControlState amt = rad / stick_full;
rad += (square_full - 1) * amt * circle;
y = ang_sin * rad;
x = ang_cos * rad;
}
m_acc[0] = y;
m_acc[1] = x;
if (gyro)
{
if (!r_gyro_1) r_gyro_1 = 1.0;
if (!r_gyro_2) r_gyro_2 = 1.0;
*pitch = C(m_gyro[0] * angle * range * r_gyro_1 * r_gyro_2 * r_gyro_s + base);
*roll = C(m_gyro[1] * angle * range * r_gyro_1 * r_gyro_2 * r_gyro_s + base);
*yaw = C(m_gyro[2] * angle * range * r_gyro_1 * r_gyro_2 * r_gyro_s + base);
}
else
{
if (!r_acc) r_acc = 1.0;
*pitch = C(m_acc[0] * angle * range * r_acc * r_acc_s + base);
*roll = C(m_acc[1] * angle * range * r_acc * r_acc_s + base);
*yaw = C(m_acc[2] * angle * range * r_acc * r_acc_s + base);
}
}
private:
float m_acc[3], m_gyro[3];
int m_settle[3];
bool m_has_gyro;
};
class Cursor : public ControlGroup
{
public:
Cursor(const char* const _name);
template <typename C, typename R>
void GetState(C* const x, C* const y, C* const z, const R range = 1.0, const bool adjusted = true, const bool relative = false, const bool step = false)
{
std::lock_guard<std::mutex> lk(m_mutex);
// smooth frames
const u8 NUM_FRAMES = 10;
// absolute conversion range
const float ABS_RANGE = .2,
// raw input range
RAW_RANGE = .1;
ControlState r = controls[C_RANGE_MODIFIER]->control_ref->State();
if (!r)
r = 1.0;
for (unsigned int i=0; i<3; i++)
{
bool is_relative = controls[i*2 + 1]->control_ref->IsRelative() || controls[i*2]->control_ref->IsRelative();
ControlState state = controls[i*2 + 1]->control_ref->State() - controls[i*2]->control_ref->State();
// change sign
if (i == 0)
state = -state;
// update absolute position
if (is_relative)
{
// moving average smooth input
m_list[i].push_back(state);
float fsum = 0; int j = 0;
if (m_list[i].size() > 0)
{
for (std::list<float>::reverse_iterator it = m_list[i].rbegin(); relative ? it != m_list[i].rend() : j < 1; it++, j++)
fsum += *it;
state = fsum / float(m_list[i].size());
}
if (m_list[i].size() >= NUM_FRAMES)
m_list[i].pop_front();
// update position
m_absolute[i] += state * range * r * ABS_RANGE;
m_absolute[i] = MathUtil::Trim(m_absolute[i], -1, 1);
m_state[i] = m_absolute[i];
}
else
{
m_absolute[i] = MathUtil::Trim(state, -1, 1);
}
if (relative)
{
m_state[i] = (m_absolute[i] - m_last[i]) * RAW_RANGE;
if (step)
m_last[i] = m_absolute[i];
}
else
{
m_state[i] = m_absolute[i];
}
}
*y = m_state[0];
*x = m_state[1];
*z = m_state[2];
// adjust absolute cursor
if (adjusted && !relative)
{
*x *= (settings[C_WIDTH]->value * range * 2.0);
*y *= (settings[C_HEIGHT]->value * range * 2.0);
*y += (settings[C_CENTER]->value - 0.5f);
}
}
float m_state[3], m_absolute[3], m_last[3];
std::list<float> m_list[3];
std::mutex m_mutex;
};
class Extension : public ControlGroup
{
public:
Extension(const char* const _name)
: ControlGroup(_name, GROUP_TYPE_EXTENSION)
, switch_extension(0)
, active_extension(0) {}
~Extension();
void GetState(u8* const data, const bool focus = true);
std::vector<ControllerEmu*> attachments;
int switch_extension;
int active_extension;
};
virtual ~ControllerEmu();
virtual std::string GetName() const = 0;
virtual void LoadDefaults(const ControllerInterface& ciface);
virtual void LoadConfig(IniFile::Section *sec, const std::string& base = "");
virtual void SaveConfig(IniFile::Section *sec, const std::string& base = "");
void UpdateDefaultDevice();
void UpdateReferences(ControllerInterface& devi);
std::vector< ControlGroup* > groups;
ControllerInterface::DeviceQualifier default_device;
};
#endif