-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathmcmitm_config.cpp
More file actions
159 lines (137 loc) · 6.33 KB
/
mcmitm_config.cpp
File metadata and controls
159 lines (137 loc) · 6.33 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
/*
* Copyright (c) 2020-2024 ndeadly
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "mcmitm_config.hpp"
namespace ams::mitm {
namespace {
constexpr const char config_file_location[] = "sdmc:/config/MissionControl/missioncontrol.ini";
constinit SetLanguage g_system_language;
constinit MissionControlConfig g_global_config = {
.general = {
.enable_rumble = true,
.enable_motion = true
},
.misc = {
.analog_trigger_activation_threshold = 50,
.dualshock3_led_mode = 0,
.dualshock4_polling_rate = 8,
.dualshock4_lightbar_brightness = 5,
.dualsense_lightbar_brightness = 5,
.dualsense_enable_player_leds = true,
.dualsense_vibration_intensity = 4,
.enable_dualsense_mute_button = true,
.swap_touchpad_button = false
}
};
void ParseBoolean(const char *value, bool *out) {
if (strcasecmp(value, "true") == 0)
*out = true;
else if (strcasecmp(value, "false") == 0)
*out = false;
}
void ParseInt(const char *value, int *out, int min=INT_MIN, int max=INT_MAX) {
int tmp = std::strtol(value, nullptr, 10);
if ((tmp >= min) && (tmp <= max)) {
*out = tmp;
}
}
void ParseBluetoothAddress(const char *value, bluetooth::Address *out) {
// Check length of address string is correct
if (std::strlen(value) != 3*sizeof(bluetooth::Address) - 1) {
return;
}
// Parse bluetooth mac address
char buf[2 + 1];
bluetooth::Address address = {};
for (u32 i = 0; i < sizeof(bluetooth::Address); ++i) {
// Convert hex pair to number
std::memcpy(buf, &value[i*3], 2);
address.address[i] = static_cast<u8>(std::strtoul(buf, nullptr, 16));
// Check for colon separator
if ((i < sizeof(bluetooth::Address) - 1) && (value[i*3 + 2] != ':')) {
return;
}
}
*out = address;
}
int ConfigIniHandler(void *user, const char *section, const char *name, const char *value) {
auto config = reinterpret_cast<MissionControlConfig *>(user);
if (strcasecmp(section, "general") == 0) {
if (strcasecmp(name, "enable_rumble") == 0) {
ParseBoolean(value, &config->general.enable_rumble);
} else if (strcasecmp(name, "enable_motion") == 0) {
ParseBoolean(value, &config->general.enable_motion);
}
} else if (strcasecmp(section, "bluetooth") == 0) {
if (strcasecmp(name, "host_name") == 0) {
std::strncpy(config->bluetooth.host_name, value, sizeof(config->bluetooth.host_name));
} else if (strcasecmp(name, "host_address") == 0) {
ParseBluetoothAddress(value, &config->bluetooth.host_address);
}
} else if (strcasecmp(section, "misc") == 0) {
if (strcasecmp(name, "analog_trigger_activation_threshold") == 0) {
ParseInt(value, &config->misc.analog_trigger_activation_threshold, 0, 100);
} else if (strcasecmp(name, "dualshock3_led_mode") == 0) {
ParseInt(value, &config->misc.dualshock3_led_mode, 0, 2);
} else if (strcasecmp(name, "dualshock4_polling_rate") == 0) {
ParseInt(value, &config->misc.dualshock4_polling_rate, 0, 16);
} else if (strcasecmp(name, "dualshock4_lightbar_brightness") == 0) {
ParseInt(value, &config->misc.dualshock4_lightbar_brightness, 0, 9);
} else if (strcasecmp(name, "dualsense_lightbar_brightness") == 0) {
ParseInt(value, &config->misc.dualsense_lightbar_brightness, 0, 9);
} else if (strcasecmp(name, "dualsense_enable_player_leds") == 0) {
ParseBoolean(value, &config->misc.dualsense_enable_player_leds);
} else if (strcasecmp(name, "dualsense_vibration_intensity") == 0) {
ParseInt(value, &config->misc.dualsense_vibration_intensity, 1, 8);
} else if (strcasecmp(name, "enable_dualsense_mute_button") == 0) {
ParseBoolean(value, &config->misc.enable_dualsense_mute_button);
} else if (strcasecmp(name, "swap_touchpad_button") == 0) {
ParseBoolean(value, &config->misc.swap_touchpad_button);
}
} else {
return 0;
}
return 1;
}
void ParseIniConfiguration() {
fs::FileHandle file;
{
if (R_FAILED(fs::OpenFile(std::addressof(file), config_file_location, fs::OpenMode_Read))) {
return;
}
}
ON_SCOPE_EXIT { fs::CloseFile(file); };
util::ini::ParseFile(file, &g_global_config, ConfigIniHandler);
}
void ReadSystemLanguage() {
R_ABORT_UNLESS(setInitialize());
ON_SCOPE_EXIT { setExit(); };
u64 language_code = 0;
R_ABORT_UNLESS(setGetSystemLanguage(&language_code));
R_ABORT_UNLESS(setMakeLanguage(language_code, &g_system_language));
}
}
void LoadConfiguration() {
ParseIniConfiguration();
ReadSystemLanguage();
}
MissionControlConfig *GetGlobalConfig() {
return &g_global_config;
}
SetLanguage GetSystemLanguage() {
return g_system_language;
}
}