aboutsummaryrefslogtreecommitdiff
path: root/utils/winmm_drv/config/regconfig.c
blob: 70874ab151f6726eda21afeb9ad843928763c4e8 (plain)
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
#include <stdlib.h>
#include <stdio.h>

#include <windef.h>
#include <winbase.h>
#include <winuser.h>

#include "regconfig.h"


#define TOTAL_BYTES_READ    1024
#define OFFSET_BYTES 1024

static BOOL createRegistryKey(HKEY hKeyParent, const PWCHAR subkey)
{
    DWORD dwDisposition; //It verify new key is created or open existing key
    HKEY  hKey;
    DWORD ret;

    ret = RegCreateKeyExW(hKeyParent, subkey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
    if(ret != ERROR_SUCCESS)
    {
        return FALSE;
    }

    RegCloseKey(hKey); //close the key
    return TRUE;
}

static BOOL writeStringToRegistry(HKEY hKeyParent, const PWCHAR subkey, const PWCHAR valueName, PWCHAR strData)
{
    DWORD ret;
    HKEY hKey;

    //Check if the registry exists
    ret = RegOpenKeyExW(hKeyParent, subkey, 0, KEY_WRITE, &hKey);
    if(ret == ERROR_SUCCESS)
    {
        ret = RegSetValueExW(hKey, valueName, 0, REG_SZ, (LPBYTE)(strData), wcslen(strData) * sizeof(wchar_t));
        if(ret != ERROR_SUCCESS)
        {
            RegCloseKey(hKey);
            return FALSE;
        }

        RegCloseKey(hKey);
        return TRUE;
    }
    return FALSE;
}

static BOOL readStringFromRegistry(HKEY hKeyParent, const PWCHAR subkey, const PWCHAR valueName, PWCHAR *readData)
{
    HKEY hKey;
    DWORD len = TOTAL_BYTES_READ;
    DWORD readDataLen = len;
    PWCHAR readBuffer = (PWCHAR )malloc(sizeof(PWCHAR)* len);

    if (readBuffer == NULL)
        return FALSE;

    //Check if the registry exists
    DWORD ret = RegOpenKeyExW(hKeyParent, subkey, 0, KEY_READ, &hKey);

    if(ret == ERROR_SUCCESS)
    {
        ret = RegQueryValueExW(hKey, valueName, NULL, NULL, (BYTE*)readBuffer, &readDataLen);

        while(ret == ERROR_MORE_DATA)
        {
            // Get a buffer that is big enough.
            len += OFFSET_BYTES;
            readBuffer = (PWCHAR)realloc(readBuffer, len);
            readDataLen = len;
            ret = RegQueryValueExW(hKey, valueName, NULL, NULL, (BYTE*)readBuffer, &readDataLen);
        }
        if (ret != ERROR_SUCCESS)
        {
            RegCloseKey(hKey);
            return FALSE;
        }

        *readData = readBuffer;
        RegCloseKey(hKey);
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

static BOOL readIntFromRegistry(HKEY hKeyParent, const PWCHAR subkey, const PWCHAR valueName, int *readData)
{
    WCHAR *buf = NULL;
    BOOL ret;
    ret = readStringFromRegistry(hKeyParent, subkey, valueName, &buf);
    if(ret && readData)
    {
        *readData = _wtoi(buf);
    }
    if(buf)
        free(buf);
    return ret;
}

static BOOL writeIntToRegistry(HKEY hKeyParent, const PWCHAR subkey, const PWCHAR valueName, int intData)
{
    WCHAR buf[20];
    BOOL ret;

    ZeroMemory(buf, 20);
    _snwprintf(buf, 20, L"%d", intData);

    ret = writeStringToRegistry(hKeyParent, subkey, valueName, buf);
    return ret;
}




void setupDefault(DriverSettings *setup)
{
    setup->useExternalBank = 0;
    setup->bankId = 68;
    ZeroMemory(setup->bankPath, MAX_PATH * sizeof(WCHAR));
    setup->emulatorId = 0;

    setup->flagDeepTremolo = BST_INDETERMINATE;
    setup->flagDeepVibrato = BST_INDETERMINATE;

    setup->flagSoftPanning = BST_CHECKED;
    setup->flagScaleModulators = BST_UNCHECKED;
    setup->flagFullBrightness = BST_UNCHECKED;

    setup->volumeModel = 0;
    setup->numChips = 4;
    setup->num4ops = -1;
}



static const PWCHAR s_regPath       = L"SOFTWARE\\Wohlstand\\libADLMIDI";

void loadSetup(DriverSettings *setup)
{
    int iVal;
    WCHAR *sVal = NULL;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"useExternalBank", &iVal))
        setup->useExternalBank = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"bankId", &iVal))
        setup->bankId = iVal;

    if(readStringFromRegistry(HKEY_CURRENT_USER, s_regPath, L"bankPath", &sVal))
        wcsncpy(setup->bankPath, sVal, MAX_PATH);
    if(sVal)
        free(sVal);
    sVal = NULL;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"emulatorId", &iVal))
        setup->emulatorId = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"flagDeepTremolo", &iVal))
        setup->flagDeepTremolo = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"flagDeepVibrato", &iVal))
        setup->flagDeepVibrato = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"flagSoftPanning", &iVal))
        setup->flagSoftPanning = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"flagScaleModulators", &iVal))
        setup->flagScaleModulators = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"flagFullBrightness", &iVal))
        setup->flagFullBrightness = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"volumeModel", &iVal))
        setup->volumeModel = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"numChips", &iVal))
        setup->numChips = iVal;

    if(readIntFromRegistry(HKEY_CURRENT_USER, s_regPath, L"num4ops", &iVal))
        setup->num4ops = iVal;
}

void saveSetup(DriverSettings *setup)
{
    createRegistryKey(HKEY_CURRENT_USER, s_regPath);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"useExternalBank", setup->useExternalBank);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"bankId", setup->bankId);
    if(setup->bankPath[0] != L'\0')
        writeStringToRegistry(HKEY_CURRENT_USER, s_regPath, L"bankPath", setup->bankPath);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"emulatorId", setup->emulatorId);

    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"flagDeepTremolo", setup->flagDeepTremolo);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"flagDeepVibrato", setup->flagDeepVibrato);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"flagSoftPanning", setup->flagSoftPanning);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"flagScaleModulators", setup->flagScaleModulators);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"flagFullBrightness", setup->flagFullBrightness);

    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"volumeModel", setup->volumeModel);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"numChips", setup->numChips);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPath, L"num4ops", setup->num4ops);
}


static const PWCHAR s_regPathNotify = L"SOFTWARE\\Wohlstand\\libADLMIDI\\notify";

void sendSignal(int sig)
{
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPathNotify, L"command", sig);
}


#ifdef ENABLE_REG_SERVER

static HKEY   hKey = 0;
static HANDLE hEvent = 0;

void openSignalListener()
{
    LONG  errorcode;
    DWORD  dwFilter = REG_NOTIFY_CHANGE_NAME |
                      REG_NOTIFY_CHANGE_ATTRIBUTES |
                      REG_NOTIFY_CHANGE_LAST_SET |
                      REG_NOTIFY_CHANGE_SECURITY;

    createRegistryKey(HKEY_CURRENT_USER, s_regPathNotify);
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPathNotify, L"command", 0);

    errorcode = RegOpenKeyExW(HKEY_CURRENT_USER, s_regPathNotify, 0, KEY_NOTIFY, &hKey);
    if(errorcode != ERROR_SUCCESS)
        return;

    hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
    if(hEvent == NULL)
    {
        RegCloseKey(hKey);
        hKey = 0;
        return;
    }

    errorcode = RegNotifyChangeKeyValue(hKey,
                                        FALSE,
                                        dwFilter,
                                        hEvent,
                                        TRUE);
    if(errorcode != ERROR_SUCCESS)
    {
        CloseHandle(hEvent);
        hEvent = 0;
        RegCloseKey(hKey);
        hKey = 0;
        return;
    }
}

int hasReloadSetupSignal()
{
    DWORD ret;
    int cmd;

    if(hEvent == 0)
        return 0;

    ret = WaitForSingleObject(hEvent, 0);

    if(ret == WAIT_OBJECT_0)
    {
        readIntFromRegistry(HKEY_CURRENT_USER, s_regPathNotify, L"command", &cmd);
        return cmd;
    }

    return 0;
}

void resetSignal()
{
    writeIntToRegistry(HKEY_CURRENT_USER, s_regPathNotify, L"command", 0);
}

void closeSignalListener()
{
    if(hKey)
        RegCloseKey(hKey);
    hKey = 0;

    if(hEvent)
        CloseHandle(hEvent);
    hEvent = 0;
}

#endif