spiegel-keyman/windows/src/engine/keyman32/keyboardoptions.cpp
Ross 708b6bcf68 chore(windows): rename KeyboardOptionCoretoRegistry
LoadKeyboardOptionsREGCore -> LoadKeyboardOptionsRegistrytoCore
SaveKeyboardOptionREGCore -> SaveKeyboardOptionCoretoRegistry
2023-05-04 14:16:09 +10:00

112 lines
3.9 KiB
C++

/*
Name: keyboardoptions
Copyright: Copyright (C) SIL International.
Documentation:
Description:
Create Date: 25 May 2010
Modified Date: 28 Nov 2012
Authors: mcdurdin
Related Files:
Dependencies:
Bugs:
Todo:
Notes:
History: 25 May 2010 - mcdurdin - I1632 - Keyboard Options
31 May 2010 - mcdurdin - I2397 - Assertion failure - keyboard options loading twice
28 Nov 2012 - mcdurdin - I3594 - V9.0 - Remove old SelectKeyboard code and related messages
*/
#include "pch.h"
BOOL IntLoadKeyboardOptionsCore(LPCSTR key, LPINTKEYBOARDINFO kp, km_kbp_state* const state);
void IntSaveKeyboardOptionCoretoRegistry(LPCSTR REGKey, LPINTKEYBOARDINFO kp, LPCWSTR key, LPWSTR value);
static km_kbp_cp* CloneKMKBPCP(const km_kbp_cp* cp) {
LPCWSTR buf = reinterpret_cast<LPCWSTR>(cp);
km_kbp_cp* clone = new km_kbp_cp[wcslen(buf) + 1];
wcscpy_s(reinterpret_cast<LPWSTR>(clone), wcslen(buf) + 1, buf);
return clone;
}
static km_kbp_cp* CloneKMKBPCPFromWSTR(LPWSTR buf) {
km_kbp_cp* clone = new km_kbp_cp[wcslen(buf) + 1];
wcscpy_s(reinterpret_cast<LPWSTR>(clone), wcslen(buf) + 1, buf);
return clone;
}
void SaveKeyboardOptionCoretoRegistry(LPINTKEYBOARDINFO kp, LPCWSTR key, LPWSTR value)
{
IntSaveKeyboardOptionCoretoRegistry(REGSZ_KeyboardOptions, kp, key, value);
}
void IntSaveKeyboardOptionCoretoRegistry(LPCSTR REGKey, LPINTKEYBOARDINFO kp, LPCWSTR key, LPWSTR value)
{
assert(REGKey != NULL);
assert(kp != NULL);
assert(key);
assert(value);
RegistryFullAccess r(HKEY_CURRENT_USER);
if (r.OpenKey(REGSZ_KeymanActiveKeyboards, TRUE) && r.OpenKey(kp->Name, TRUE) && r.OpenKey(REGKey, TRUE))
{
r.WriteString(key, value);
}
}
void LoadKeyboardOptionsRegistrytoCore(LPINTKEYBOARDINFO kp, km_kbp_state* const state)
{
SendDebugMessageFormat(0, sdmKeyboard, 0, "LoadKeyboardOptionsRegistrytoCore: Enter");
IntLoadKeyboardOptionsCore(REGSZ_KeyboardOptions, kp, state);
}
BOOL IntLoadKeyboardOptionsCore(LPCSTR key, LPINTKEYBOARDINFO kp, km_kbp_state* const state)
{
assert(key != NULL);
assert(kp != NULL);
// Get the list of default options to determine size of list
const km_kbp_keyboard_attrs* keyboardAttrs;
km_kbp_status err_status = km_kbp_keyboard_get_attrs(kp->lpCoreKeyboard, &keyboardAttrs);
if (err_status != KM_KBP_STATUS_OK) {
SendDebugMessageFormat(
0, sdmKeyboard, 0, "LoadKeyboardOptionsRegistrytoCore: km_kbp_keyboard_get_attrs failed with error status [%d]", err_status);
return FALSE;
}
size_t listSize = km_kbp_options_list_size(keyboardAttrs->default_options);
if (listSize == 0){
return TRUE;
}
km_kbp_option_item* keyboardOpts = new km_kbp_option_item[listSize + 1];
RegistryReadOnly r(HKEY_CURRENT_USER);
BOOL hasData = r.OpenKeyReadOnly(REGSZ_KeymanActiveKeyboards) && r.OpenKeyReadOnly(kp->Name) && r.OpenKeyReadOnly(key);
int n = 0;
for (auto kpc = keyboardAttrs->default_options; kpc->key; kpc++) {
keyboardOpts[n].scope = KM_KBP_OPT_KEYBOARD;
keyboardOpts[n].key = kpc->key;
LPCWSTR coreKey = reinterpret_cast<LPCWSTR>(kpc->key);
WCHAR val[256];
if (hasData && r.ReadString(coreKey, val, sizeof(val) / sizeof(val[0])) && val[0]) {
keyboardOpts[n].value = CloneKMKBPCPFromWSTR(val);
} else {
keyboardOpts[n].value = CloneKMKBPCP(kpc->value);
}
n++;
}
keyboardOpts[n] = KM_KBP_OPTIONS_END;
// once we have the option list we can then update the options using the public api call
err_status = km_kbp_state_options_update(state, keyboardOpts);
if (err_status != KM_KBP_STATUS_OK) {
SendDebugMessageFormat(
0, sdmKeyboard, 0, "LoadKeyboardOptionsRegistrytoCore: km_kbp_state_options_update failed with error status [%d]", err_status);
}
for (int i = 0; i < n; i++) {
delete[] keyboardOpts[i].value;
}
delete[] keyboardOpts;
return TRUE;
}