-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.cpp
More file actions
238 lines (199 loc) · 6.57 KB
/
Copy pathConfigManager.cpp
File metadata and controls
238 lines (199 loc) · 6.57 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
#include "ConfigManager.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <shlobj.h>
namespace fs = std::filesystem;
namespace {
int ParseMicrophoneVolumePercent(const std::string& jsonText) {
std::size_t keyPosition = jsonText.find("\"microphoneVolume\"");
if (keyPosition == std::string::npos) {
return 100;
}
std::size_t colonPosition = jsonText.find(':', keyPosition);
if (colonPosition == std::string::npos) {
return 100;
}
std::size_t numberStart = jsonText.find_first_of("-0123456789", colonPosition + 1);
if (numberStart == std::string::npos) {
return 100;
}
std::size_t numberEnd = jsonText.find_first_not_of("0123456789", numberStart + 1);
std::string numberToken = jsonText.substr(numberStart, numberEnd - numberStart);
try {
return std::stoi(numberToken);
} catch (...) {
return 100;
}
}
bool ParseEnabled(const std::string& jsonText) {
std::size_t keyPosition = jsonText.find("\"enabled\"");
if (keyPosition == std::string::npos) {
return true;
}
std::size_t colonPosition = jsonText.find(':', keyPosition);
if (colonPosition == std::string::npos) {
return true;
}
std::size_t valueStart = jsonText.find_first_not_of(" \t\r\n", colonPosition + 1);
if (valueStart == std::string::npos) {
return true;
}
if (jsonText.compare(valueStart, 4, "true") == 0) {
return true;
}
if (jsonText.compare(valueStart, 5, "false") == 0) {
return false;
}
return true;
}
}
ConfigManager::ConfigManager() {
PWSTR path = NULL;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Profile, 0, NULL, &path))) {
m_configPath = std::wstring(path) + L"\\.MicGainControl.json";
CoTaskMemFree(path);
} else {
// Fallback to local directory if for some reason SHGetKnownFolderPath fails
wchar_t exePath[MAX_PATH];
GetModuleFileNameW(NULL, exePath, MAX_PATH);
m_configPath = fs::path(exePath).parent_path() / L".MicGainControl.json";
}
m_stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
ConfigManager::~ConfigManager() {
StopWatching();
if (m_stopEvent) CloseHandle(m_stopEvent);
}
bool ConfigManager::Load() {
if (!fs::exists(m_configPath)) {
Save();
return true;
}
try {
std::ifstream fileStream(m_configPath);
std::string jsonText((std::istreambuf_iterator<char>(fileStream)), std::istreambuf_iterator<char>());
int microphoneVolumePercent = ParseMicrophoneVolumePercent(jsonText);
if (microphoneVolumePercent < 0) {
microphoneVolumePercent = 0;
}
if (microphoneVolumePercent > 100) {
microphoneVolumePercent = 100;
}
m_config.microphoneVolume = static_cast<float>(microphoneVolumePercent) / 100.0f;
m_config.enabled = ParseEnabled(jsonText);
return true;
} catch (...) {
return false;
}
}
void ConfigManager::Save() {
try {
int microphoneVolumePercent = static_cast<int>(m_config.microphoneVolume * 100.0f);
if (microphoneVolumePercent < 0) {
microphoneVolumePercent = 0;
}
if (microphoneVolumePercent > 100) {
microphoneVolumePercent = 100;
}
std::ofstream fileStream(m_configPath);
fileStream << "{\n";
fileStream << " \"microphoneVolume\": " << microphoneVolumePercent << ",\n";
fileStream << " \"enabled\": " << (m_config.enabled ? "true" : "false") << "\n";
fileStream << "}\n";
} catch (...) {
}
}
const Config& ConfigManager::GetConfig() const {
return m_config;
}
void ConfigManager::SetEnabled(bool enabled) {
m_config.enabled = enabled;
Save();
}
void ConfigManager::SetMicrophoneVolume(float volume) {
if (volume < 0.0f) {
volume = 0.0f;
}
if (volume > 1.0f) {
volume = 1.0f;
}
m_config.microphoneVolume = volume;
Save();
}
void ConfigManager::SetCallback(ConfigChangedCallback callback) {
m_callback = callback;
}
void ConfigManager::StartWatching() {
if (m_running) return;
m_running = true;
ResetEvent(m_stopEvent);
m_watchThread = std::thread(&ConfigManager::WatchThread, this);
}
void ConfigManager::StopWatching() {
if (!m_running) return;
m_running = false;
SetEvent(m_stopEvent);
if (m_watchThread.joinable()) {
m_watchThread.join();
}
}
void ConfigManager::WatchThread() {
fs::path dir = fs::path(m_configPath).parent_path();
HANDLE hDir = CreateFileW(
dir.c_str(),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL
);
if (hDir == INVALID_HANDLE_VALUE) return;
OVERLAPPED overlapped = {0};
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
alignas(DWORD) uint8_t buffer[1024];
while (m_running) {
DWORD bytesReturned;
if (ReadDirectoryChangesW(
hDir, buffer, sizeof(buffer), FALSE,
FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_FILE_NAME,
&bytesReturned, &overlapped, NULL
)) {
HANDLE handles[] = { m_stopEvent, overlapped.hEvent };
DWORD wait = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
if (wait == WAIT_OBJECT_0) break; // Stop event
if (wait == WAIT_OBJECT_0 + 1) {
// Change detected
PFILE_NOTIFY_INFORMATION pNotify;
DWORD offset = 0;
bool relevantChange = false;
do {
pNotify = (PFILE_NOTIFY_INFORMATION)&buffer[offset];
std::wstring fileName(pNotify->FileName, pNotify->FileNameLength / sizeof(wchar_t));
if (fileName == fs::path(m_configPath).filename().wstring()) {
relevantChange = true;
}
offset += pNotify->NextEntryOffset;
} while (pNotify->NextEntryOffset != 0);
if (relevantChange) {
// Debounce a bit to let the file be fully written
Sleep(100);
OnFileChanged();
}
ResetEvent(overlapped.hEvent);
}
} else {
break;
}
}
CloseHandle(overlapped.hEvent);
CloseHandle(hDir);
}
void ConfigManager::OnFileChanged() {
if (Load()) {
if (m_callback) {
m_callback(m_config);
}
}
}