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
|
// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "InputCommon/ControllerEmu.h"
#include <memory>
#include "Common/Common.h"
#include "VideoCommon/OnScreenDisplay.h"
// This should be called before calling GetState() or State() on a control reference
// to prevent a race condition.
// This is a recursive mutex because UpdateReferences is recursive.
static std::recursive_mutex s_get_state_mutex;
std::unique_lock<std::recursive_mutex> ControllerEmu::GetStateLock()
{
std::unique_lock<std::recursive_mutex> lock(s_get_state_mutex);
return lock;
}
void ControllerEmu::UpdateReferences(ControllerInterface& devi)
{
auto lock = ControllerEmu::GetStateLock();
for (auto& ctrlGroup : groups)
{
for (auto& control : ctrlGroup->controls)
devi.UpdateReference(control->control_ref.get(), default_device);
// extension
if (ctrlGroup->type == GROUP_TYPE_EXTENSION)
{
for (auto& attachment : ((Extension*)ctrlGroup.get())->attachments)
attachment->UpdateReferences(devi);
}
}
}
void ControllerEmu::UpdateDefaultDevice()
{
for (auto& ctrlGroup : groups)
{
// extension
if (ctrlGroup->type == GROUP_TYPE_EXTENSION)
{
for (auto& ai : ((Extension*)ctrlGroup.get())->attachments)
{
ai->default_device = default_device;
ai->UpdateDefaultDevice();
}
}
}
}
void ControllerEmu::ControlGroup::LoadConfig(IniFile::Section* sec, const std::string& defdev,
const std::string& base)
{
std::string group(base + name + "/");
// settings
for (auto& s : numeric_settings)
{
if (s->m_type == SettingType::VIRTUAL)
continue;
sec->Get(group + s->m_name, &s->m_value, s->m_default_value * 100);
s->m_value /= 100;
}
for (auto& s : boolean_settings)
{
if (s->m_type == SettingType::VIRTUAL)
continue;
sec->Get(group + s->m_name, &s->m_value, s->m_default_value);
}
for (auto& c : controls)
{
// control expression
sec->Get(group + c->name, &c->control_ref->expression, "");
// range
sec->Get(group + c->name + "/Range", &c->control_ref->range, 100.0);
c->control_ref->range /= 100;
}
// extensions
if (type == GROUP_TYPE_EXTENSION)
{
Extension* const ext = (Extension*)this;
ext->switch_extension = 0;
unsigned int n = 0;
std::string extname;
sec->Get(base + name, &extname, "");
for (auto& ai : ext->attachments)
{
ai->default_device.FromString(defdev);
ai->LoadConfig(sec, base + ai->GetName() + "/");
if (ai->GetName() == extname)
ext->switch_extension = n;
n++;
}
}
}
void ControllerEmu::LoadConfig(IniFile::Section* sec, const std::string& base)
{
std::string defdev = default_device.ToString();
if (base.empty())
{
sec->Get(base + "Device", &defdev, "");
default_device.FromString(defdev);
}
for (auto& cg : groups)
cg->LoadConfig(sec, defdev, base);
}
void ControllerEmu::ControlGroup::SaveConfig(IniFile::Section* sec, const std::string& defdev,
const std::string& base)
{
std::string group(base + name + "/");
for (auto& s : numeric_settings)
{
if (s->m_type == SettingType::VIRTUAL)
continue;
sec->Set(group + s->m_name, s->m_value * 100.0, s->m_default_value * 100.0);
}
for (auto& s : boolean_settings)
{
if (s->m_type == SettingType::VIRTUAL)
continue;
sec->Set(group + s->m_name, s->m_value, s->m_default_value);
}
for (auto& c : controls)
{
// control expression
sec->Set(group + c->name, c->control_ref->expression, "");
// range
sec->Set(group + c->name + "/Range", c->control_ref->range * 100.0, 100.0);
}
// extensions
if (type == GROUP_TYPE_EXTENSION)
{
Extension* const ext = (Extension*)this;
sec->Set(base + name, ext->attachments[ext->switch_extension]->GetName(), "None");
for (auto& ai : ext->attachments)
ai->SaveConfig(sec, base + ai->GetName() + "/");
}
}
void ControllerEmu::SaveConfig(IniFile::Section* sec, const std::string& base)
{
const std::string defdev = default_device.ToString();
if (base.empty())
sec->Set(/*std::string(" ") +*/ base + "Device", defdev, "");
for (auto& ctrlGroup : groups)
ctrlGroup->SaveConfig(sec, defdev, base);
}
void ControllerEmu::ControlGroup::SetControlExpression(int index, const std::string& expression)
{
controls.at(index)->control_ref->expression = expression;
}
ControllerEmu::AnalogStick::AnalogStick(const char* const _name, ControlState default_radius)
: AnalogStick(_name, _name, GROUP_TYPE_STICK)
{
}
ControllerEmu::AnalogStick::AnalogStick(const char* const _name, const char* const _ui_name,
ControlState default_radius)
: ControlGroup(_name, _ui_name, GROUP_TYPE_STICK)
{
for (auto& named_direction : named_directions)
controls.emplace_back(std::make_unique<Input>(named_direction));
controls.emplace_back(std::make_unique<Input>(_trans("Modifier")));
numeric_settings.emplace_back(
std::make_unique<NumericSetting>(_trans("Radius"), default_radius, 0, 100));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
}
ControllerEmu::Buttons::Buttons(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_BUTTONS)
{
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Threshold"), 0.5));
}
ControllerEmu::ModifySettingsButton::ModifySettingsButton(std::string button_name)
: Buttons(std::move(button_name))
{
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Threshold"), 0.5));
}
void ControllerEmu::ModifySettingsButton::AddInput(std::string button_name, bool toggle)
{
controls.emplace_back(new ControlGroup::Input(std::move(button_name)));
threshold_exceeded.emplace_back(false);
associated_settings.emplace_back(false);
associated_settings_toggle.emplace_back(toggle);
}
void ControllerEmu::ModifySettingsButton::GetState()
{
for (size_t i = 0; i < controls.size(); ++i)
{
ControlState state = controls[i]->control_ref->State();
if (!associated_settings_toggle[i])
{
// not toggled
associated_settings[i] = state > numeric_settings[0]->GetValue();
}
else
{
// toggle (loading savestates does not en-/disable toggle)
// after we passed the threshold, we en-/disable. but after that, we don't change it
// anymore
if (!threshold_exceeded[i] && state > numeric_settings[0]->GetValue())
{
associated_settings[i] = !associated_settings[i];
if (associated_settings[i])
OSD::AddMessage(controls[i]->name + ": " + _trans("on"));
else
OSD::AddMessage(controls[i]->name + ": " + _trans("off"));
threshold_exceeded[i] = true;
}
if (state < numeric_settings[0]->GetValue())
threshold_exceeded[i] = false;
}
}
}
ControllerEmu::MixedTriggers::MixedTriggers(const std::string& _name)
: ControlGroup(_name, GROUP_TYPE_MIXED_TRIGGERS)
{
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Threshold"), 0.9));
}
ControllerEmu::Triggers::Triggers(const std::string& _name)
: ControlGroup(_name, GROUP_TYPE_TRIGGERS)
{
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
}
ControllerEmu::Slider::Slider(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_SLIDER)
{
controls.emplace_back(std::make_unique<Input>("Left"));
controls.emplace_back(std::make_unique<Input>("Right"));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
}
ControllerEmu::Force::Force(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_FORCE)
{
memset(m_swing, 0, sizeof(m_swing));
controls.emplace_back(std::make_unique<Input>(_trans("Up")));
controls.emplace_back(std::make_unique<Input>(_trans("Down")));
controls.emplace_back(std::make_unique<Input>(_trans("Left")));
controls.emplace_back(std::make_unique<Input>(_trans("Right")));
controls.emplace_back(std::make_unique<Input>(_trans("Forward")));
controls.emplace_back(std::make_unique<Input>(_trans("Backward")));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
}
ControllerEmu::Tilt::Tilt(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_TILT)
{
memset(m_tilt, 0, sizeof(m_tilt));
controls.emplace_back(std::make_unique<Input>("Forward"));
controls.emplace_back(std::make_unique<Input>("Backward"));
controls.emplace_back(std::make_unique<Input>("Left"));
controls.emplace_back(std::make_unique<Input>("Right"));
controls.emplace_back(std::make_unique<Input>(_trans("Modifier")));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Circle Stick"), 0));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Angle"), 0.9, 0, 180));
}
ControllerEmu::Cursor::Cursor(const std::string& _name)
: ControlGroup(_name, GROUP_TYPE_CURSOR), m_z(0)
{
for (auto& named_direction : named_directions)
controls.emplace_back(std::make_unique<Input>(named_direction));
controls.emplace_back(std::make_unique<Input>("Forward"));
controls.emplace_back(std::make_unique<Input>("Backward"));
controls.emplace_back(std::make_unique<Input>(_trans("Hide")));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Center"), 0.5));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Width"), 0.5));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Height"), 0.5));
}
void ControllerEmu::LoadDefaults(const ControllerInterface& ciface)
{
// load an empty inifile section, clears everything
IniFile::Section sec;
LoadConfig(&sec);
const std::string& default_device_string = g_controller_interface.GetDefaultDeviceString();
if (!default_device_string.empty())
{
default_device.FromString(default_device_string);
UpdateDefaultDevice();
}
}
|