Repository navigation
Expand file tree
/
Copy pathXidiConfigReader.cpp
More file actions
446 lines (391 loc) · 18.2 KB
/
Copy pathXidiConfigReader.cpp
File metadata and controls
446 lines (391 loc) · 18.2 KB
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
/***************************************************************************************************
* Xidi
* DirectInput interface for XInput controllers.
***************************************************************************************************
* Authored by Samuel Grossman
* Copyright (c) 2016-2026
***********************************************************************************************//**
* @file XidiConfigReader.cpp
* Implementation of Xidi-specific configuration reading functionality.
**************************************************************************************************/
#include "XidiConfigReader.h"
#include <map>
#include <mutex>
#include <optional>
#include <string_view>
#include <Infra/Core/Configuration.h>
#include <Infra/Core/Strings.h>
#include <Infra/Core/TemporaryBuffer.h>
#include "ApiWindows.h"
#include "Strings.h"
#ifndef XIDI_SKIP_MAPPERS
#include "ElementMapper.h"
#include "Mapper.h"
#include "MapperBuilder.h"
#include "MapperParser.h"
#include "VirtualControllerTypes.h"
#endif
namespace Xidi
{
using namespace ::Infra::Configuration;
#ifndef XIDI_SKIP_MAPPERS
/// Default name for a custom mapper whose name is not specified.
static constexpr std::wstring_view kDefaultCustomMapperName = L"Custom";
/// Enumerates the possible operations to be performed on a custom mapper blueprint.
enum class EBlueprintOperation
{
/// Indicates an error.
Error,
/// Set an element mapper for a controller element.
SetElementMapper,
/// Set a force feedback actuator configuration for a physical force feedback actuator.
SetForceFeedbackActuator,
/// Set the blueprint template.
SetTemplate,
};
#endif
/// Holds the layout of the Xidi configuration file that is known statically.
static TConfigurationFileLayout configurationFileLayout = {
ConfigurationFileLayoutSection(
Infra::Configuration::kSectionNameGlobal,
{ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingPlugin, EValueType::StringMultiValue),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingControllerBackend, EValueType::String)}),
ConfigurationFileLayoutSection(
Strings::kStrConfigurationSectionLog,
{
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingLogEnabled, EValueType::Boolean),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingLogLevel, EValueType::Integer),
}),
ConfigurationFileLayoutSection(
Strings::kStrConfigurationSectionMapper,
{
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingMapperType, EValueType::String),
}),
ConfigurationFileLayoutSection(
Strings::kStrConfigurationSectionProperties,
{
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingPropertiesForceFeedbackEffectStrengthPercent,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingPropertiesMouseSpeedScalingFactorPercent,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesUseBuiltinProperties,
EValueType::Boolean),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesCircleToSquarePercentStickLeft,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesCircleToSquarePercentStickRight,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesDeadzonePercentStickLeft,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesDeadzonePercentStickRight,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesDeadzonePercentTriggerLT,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesDeadzonePercentTriggerRT,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesSaturationPercentStickLeft,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesSaturationPercentStickRight,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesSaturationPercentTriggerLT,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsPropertiesSaturationPercentTriggerRT,
EValueType::Integer),
}),
ConfigurationFileLayoutSection(
Strings::kStrConfigurationSectionWorkarounds,
{
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingWorkaroundsActiveVirtualControllerMask,
EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingWorkaroundsPollReturnCode, EValueType::Integer),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsWorkaroundsIgnoreEnumObjectsCallbackReturnCode,
EValueType::Boolean),
ConfigurationFileLayoutNameAndValueType(
Strings::kStrConfigurationSettingsWorkaroundsUseShortVirtualControllerNames,
EValueType::Boolean),
}),
};
#ifndef XIDI_SKIP_MAPPERS
/// Determines the operation that should be performed on a mapper blueprint based on the name of a
/// configuration setting.
/// @param [in] name Configuration setting name.
/// @return Operation to perform.
static EBlueprintOperation BlueprintOperationFromName(std::wstring_view name)
{
/// Map of supported configuration setting names to associated blueprint operations.
static const std::map<std::wstring_view, EBlueprintOperation> kBlueprintOperationsMap = {
{Strings::kStrConfigurationSettingCustomMapperTemplate, EBlueprintOperation::SetTemplate}};
// If the configuration setting name identifies a valid controller element, then the value
// should be parsed for an element mapper to be assigned to that controller element.
if (true == Controller::MapperParser::IsControllerElementStringValid(name))
return EBlueprintOperation::SetElementMapper;
// If the configuration setting name identifies a valid force feedback actuator, then the value
// should be parsed for a configuration to be assigned to that actuator.
if (true == Controller::MapperParser::IsForceFeedbackActuatorStringValid(name))
return EBlueprintOperation::SetForceFeedbackActuator;
// If the configuration setting name is known and contained within the map above, simply return
// it.
const auto blueprintOperationIter = kBlueprintOperationsMap.find(name);
if (kBlueprintOperationsMap.cend() != blueprintOperationIter)
return blueprintOperationIter->second;
return EBlueprintOperation::Error;
}
#endif
/// Checks if the specified section name could correspond with a section that defines a custom
/// mapper. This is simply based on whether or not the section name begins with the right prefix.
/// @param [in] section Section name to check.
/// @return `true` if the section name begins with the custom mapper section name prefix, `false`
/// otherwise.
static inline bool IsCustomMapperSectionName(std::wstring_view section)
{
return (section.starts_with(Strings::kStrConfigurationSectionCustomMapperPrefix));
}
#ifndef XIDI_SKIP_MAPPERS
/// Extracts the name of a custom mapper from a section name and performs no error checks
/// whatsoever.
/// @param [in] section Section name from which to extract the custom mapper name.
/// @return Custom mapper name.
static inline std::wstring_view QuickExtractCustomMapperName(std::wstring_view section)
{
if (section.length() == Strings::kStrConfigurationSectionCustomMapperPrefix.length())
return kDefaultCustomMapperName;
return section.substr(1 + Strings::kStrConfigurationSectionCustomMapperPrefix.length());
}
/// Extracts the name of a custom mapper from a section name with the appropriate prefix.
/// @param [in] section Section name from which to extract the custom mapper name.
/// @return Custom mapper name, if it was successfully extracted from the section name.
static std::optional<std::wstring_view> ExtractCustomMapperName(std::wstring_view section)
{
if (false == IsCustomMapperSectionName(section)) return std::nullopt;
// Section name contains just the prefix, in which case it identifies the default name.
if (section.length() == Strings::kStrConfigurationSectionCustomMapperPrefix.length())
return kDefaultCustomMapperName;
// Separator character needs to be present otherwise the section name is invalid.
if (Strings::kCharConfigurationSectionCustomMapperSeparator !=
section[Strings::kStrConfigurationSectionCustomMapperPrefix.length()])
return std::nullopt;
std::wstring_view customMapperName = QuickExtractCustomMapperName(section);
// It is explicitly forbidden to specify a custom mapper name equal to the default.
if (customMapperName == kDefaultCustomMapperName) return std::nullopt;
return customMapperName;
}
#endif
Action XidiConfigReader::ActionForSection(std::wstring_view section)
{
#ifndef XIDI_SKIP_MAPPERS
if ((nullptr != customMapperBuilder) && (true == IsCustomMapperSectionName(section)))
{
std::optional<std::wstring_view> customMapperName = ExtractCustomMapperName(section);
if (false == customMapperName.has_value()) return Action::Error();
if (false == customMapperBuilder->CreateBlueprint(*customMapperName))
{
return Action::ErrorWithMessage(Infra::Strings::Format(
L"%.*s: A mapper with this name already exists.",
static_cast<unsigned int>(customMapperName->length()),
customMapperName->data()));
}
return Action::Process();
}
#else
if (true == IsCustomMapperSectionName(section)) return Action::Skip();
#endif
if (0 != configurationFileLayout.count(section)) return Action::Process();
return Action::Error();
}
Action XidiConfigReader::ActionForValue(
std::wstring_view section, std::wstring_view name, TIntegerView value)
{
#ifndef XIDI_SKIP_MAPPERS
if (Strings::kStrConfigurationSectionProperties == section)
{
if (Strings::kStrConfigurationSettingPropertiesMouseSpeedScalingFactorPercent == name)
{
// Mouse speed scaling factor percent must not be negative but it is allowed to exceed 100,
// which would simply mean that the mouse speed should be scaled up rather than down.
if (value < 0) return Action::Error();
}
else if (name.starts_with(XIDI_CONFIG_PROPERTIES_PREFIX_DEADZONE_PERCENT))
{
// Deadzone percentages must be in the range of 0 to 45 inclusive.
// This ensures they make semantic sense and cannot cross the minimum possible saturation
// percentage.
if ((value < 0) || (value > 45))
return Action::Error();
else
return Action::Process();
}
else if (name.starts_with(XIDI_CONFIG_PROPERTIES_PREFIX_SATURATION_PERCENT))
{
// Saturation percentages must be in the range of 55 to 100 inclusive.
// This ensures they make semantic sense and cannot cross the maximum possible deadzone
// percentage.
if ((value < 55) || (value > 100))
return Action::Error();
else
return Action::Process();
}
else if (name.contains(L"Percent"))
{
// All other percentages must be between 0 and 100 inclusive.
if ((value < 0) || (value > 100))
return Action::Error();
else
return Action::Process();
}
}
#endif
if (value >= 0) return Action::Process();
return Action::Error();
}
Action XidiConfigReader::ActionForValue(
std::wstring_view section, std::wstring_view name, TBooleanView value)
{
return Action::Process();
}
Action XidiConfigReader::ActionForValue(
std::wstring_view section, std::wstring_view name, TStringView value)
{
#ifndef XIDI_SKIP_MAPPERS
if ((nullptr != customMapperBuilder) && (true == IsCustomMapperSectionName(section)))
{
std::wstring_view customMapperName = QuickExtractCustomMapperName(section);
switch (BlueprintOperationFromName(name))
{
case EBlueprintOperation::SetElementMapper:
{
Xidi::Controller::MapperParser::ElementMapperOrError maybeElementMapper =
Controller::MapperParser::ElementMapperFromString(value);
if (false == maybeElementMapper.HasValue())
{
return Action::ErrorWithMessage(Infra::Strings::Format(
L"%.*s: Failed to parse element mapper: %s.",
static_cast<unsigned int>(name.length()),
name.data(),
maybeElementMapper.Error().c_str()));
customMapperBuilder->InvalidateBlueprint(customMapperName);
}
if (false ==
customMapperBuilder->SetBlueprintElementMapper(
customMapperName, name, std::move(maybeElementMapper.Value())))
{
return Action::ErrorWithMessage(Infra::Strings::Format(
L"%.*s: Internal error: Successfully parsed element mapper but failed to set it on the blueprint.",
static_cast<unsigned int>(name.length()),
name.data()));
customMapperBuilder->InvalidateBlueprint(customMapperName);
}
break;
}
case EBlueprintOperation::SetForceFeedbackActuator:
{
Xidi::Controller::MapperParser::ForceFeedbackActuatorOrError maybeForceFeedbackActuator =
Controller::MapperParser::ForceFeedbackActuatorFromString(value);
if (false == maybeForceFeedbackActuator.HasValue())
{
return Action::ErrorWithMessage(Infra::Strings::Format(
L"%.*s: Failed to parse force feedback actuator: %s.",
static_cast<unsigned int>(name.length()),
name.data(),
maybeForceFeedbackActuator.Error().c_str()));
customMapperBuilder->InvalidateBlueprint(customMapperName);
}
if (false ==
customMapperBuilder->SetBlueprintForceFeedbackActuator(
customMapperName, name, maybeForceFeedbackActuator.Value()))
{
return Action::ErrorWithMessage(Infra::Strings::Format(
L"%s.*: Internal error: Successfully parsed force feedback actuator but failed to set it on the blueprint.",
static_cast<unsigned int>(name.length()),
name.data()));
customMapperBuilder->InvalidateBlueprint(customMapperName);
}
break;
}
case EBlueprintOperation::SetTemplate:
{
if (false == customMapperBuilder->SetBlueprintTemplate(customMapperName, value))
{
return Action::ErrorWithMessage(Infra::Strings::Format(
L"Internal error: Failed to set template for %.*s to %.*s.",
static_cast<unsigned int>(customMapperName.length()),
customMapperName.data(),
static_cast<unsigned int>(value.length()),
value.data()));
customMapperBuilder->InvalidateBlueprint(customMapperName);
}
break;
}
default:
return Action::Error();
}
// Custom mapper configuration settings are processed using the mapper builder.
// They do not need to be inserted into the configuration data structure.
return Action::Skip();
}
#endif
return Action::Process();
}
void XidiConfigReader::BeginRead(void)
{
static std::once_flag initFlag;
std::call_once(
initFlag,
[]() -> void
{
// Create the per-controller mapper settings types and submit them to the configuration
// file layout. These are gernerated dynamically based on the number of controllers the
// system supports.
for (Controller::TControllerIdentifier i = 0; i < Controller::kVirtualControllerMaxCount;
++i)
configurationFileLayout[Strings::kStrConfigurationSectionMapper]
[Strings::MapperTypeConfigurationNameString(i)] =
EValueType::String;
});
}
void XidiConfigReader::EndRead(void)
{
#ifndef XIDI_SKIP_MAPPERS
customMapperBuilder = nullptr;
#endif
}
EValueType XidiConfigReader::TypeForValue(std::wstring_view section, std::wstring_view name)
{
#ifndef XIDI_SKIP_MAPPERS
if ((nullptr != customMapperBuilder) && (true == IsCustomMapperSectionName(section)))
{
// All custom mapper operations use strings as input.
// As long as an operation can be located for the specified configuration setting name the
// result is a string, otherwise it is an error.
if (EBlueprintOperation::Error == BlueprintOperationFromName(name))
return EValueType::Error;
else
return EValueType::String;
}
#endif
auto sectionLayout = configurationFileLayout.find(section);
if (configurationFileLayout.end() == sectionLayout) return EValueType::Error;
auto settingInfo = sectionLayout->second.find(name);
if (sectionLayout->second.end() == settingInfo) return EValueType::Error;
return settingInfo->second;
}
} // namespace Xidi