Skip to content

Commit

Permalink
Explicitly load for current process or base config
Browse files Browse the repository at this point in the history
refs #20 - add 'tablet mode' executable that's independent of OpenXR
  • Loading branch information
fredemmott committed Jan 7, 2023
1 parent ec545a0 commit cc622cd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/APILayer/APILayer_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ XrResult __declspec(dllexport) XRAPI_CALL
return XR_ERROR_INITIALIZATION_FAILED;
}

HandTrackedCockpitClicking::Config::Load();
HandTrackedCockpitClicking::Config::LoadForCurrentProcess();

// TODO: check version fields etc in loaderInfo

Expand Down
2 changes: 1 addition & 1 deletion src/SettingsApp/MainWindow.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static constexpr wchar_t gAPILayerSubkey[]

namespace winrt::HTCCSettings::implementation {
MainWindow::MainWindow() {
HTCC::Config::Load();
HTCC::Config::LoadBaseConfig();

InitializeComponent();
ExtendsContentIntoTitleBar(true);
Expand Down
125 changes: 67 additions & 58 deletions src/lib/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ HandTrackedCockpitClicking_DWORD_SETTINGS
HandTrackedCockpitClicking_STRING_SETTINGS
#undef IT

static const std::wstring SubKey {
static const std::wstring BaseSubKey {
L"SOFTWARE\\Fred Emmott\\HandTrackedCockpitClicking"};

static std::wstring AppOverrideSubKey() {
Expand All @@ -53,94 +53,103 @@ static std::wstring AppOverrideSubKey() {

sCache = std::format(
L"{}\\AppOverrides\\{}",
SubKey,
BaseSubKey,
std::filesystem::path(std::wstring_view {buf, bufLen})
.filename()
.wstring());
return sCache;
}

template <class T>
static void LoadDWord(T& value, const wchar_t* valueName) {
for (const auto& subKey: {AppOverrideSubKey(), SubKey}) {
DWORD data {};
DWORD dataSize = sizeof(data);
const auto result = RegGetValueW(
HKEY_LOCAL_MACHINE,
subKey.c_str(),
valueName,
RRF_RT_DWORD,
nullptr,
&data,
&dataSize);
if (result == ERROR_SUCCESS) {
value = static_cast<T>(data);
return;
}
static void
LoadDWord(const std::wstring& subKey, T& value, const wchar_t* valueName) {
DWORD data {};
DWORD dataSize = sizeof(data);
const auto result = RegGetValueW(
HKEY_LOCAL_MACHINE,
subKey.c_str(),
valueName,
RRF_RT_DWORD,
nullptr,
&data,
&dataSize);
if (result == ERROR_SUCCESS) {
value = static_cast<T>(data);
}
}

static bool LoadString(std::string& value, const wchar_t* valueName) {
for (const auto& subKey: {AppOverrideSubKey(), SubKey}) {
DWORD dataSize = 0;
const auto sizeResult = RegGetValueW(
HKEY_LOCAL_MACHINE,
subKey.c_str(),
valueName,
RRF_RT_REG_SZ,
nullptr,
nullptr,
&dataSize);
if (sizeResult != ERROR_SUCCESS && sizeResult != ERROR_MORE_DATA) {
continue;
}

std::vector<wchar_t> buffer(dataSize / sizeof(wchar_t), L'\0');
const auto dataResult = RegGetValueW(
HKEY_LOCAL_MACHINE,
subKey.c_str(),
valueName,
RRF_RT_REG_SZ,
nullptr,
buffer.data(),
&dataSize);

if (dataResult == ERROR_SUCCESS) {
value = winrt::to_string(buffer.data());
return true;
}
static bool LoadString(
const std::wstring& subKey,
std::string& value,
const wchar_t* valueName) {
DWORD dataSize = 0;
const auto sizeResult = RegGetValueW(
HKEY_LOCAL_MACHINE,
subKey.c_str(),
valueName,
RRF_RT_REG_SZ,
nullptr,
nullptr,
&dataSize);
if (sizeResult != ERROR_SUCCESS && sizeResult != ERROR_MORE_DATA) {
return false;
}

std::vector<wchar_t> buffer(dataSize / sizeof(wchar_t), L'\0');
const auto dataResult = RegGetValueW(
HKEY_LOCAL_MACHINE,
subKey.c_str(),
valueName,
RRF_RT_REG_SZ,
nullptr,
buffer.data(),
&dataSize);

if (dataResult == ERROR_SUCCESS) {
value = winrt::to_string(buffer.data());
return true;
}
return false;
}

static void LoadFloat(float& value, const wchar_t* valueName) {
static void
LoadFloat(const std::wstring& subKey, float& value, const wchar_t* valueName) {
std::string buffer;
if (LoadString(buffer, valueName)) {
if (LoadString(subKey, buffer, valueName)) {
value = std::atof(buffer.data());
}
}

void Load() {
DebugPrint(L"Loading settings from HKLM\\{}", SubKey);
DebugPrint(L"Loading app overrides from HKLM\\{}", AppOverrideSubKey());

#define IT(native_type, name, default) LoadDWord(Config::name, L#name);
static void Load(const std::wstring& subKey) {
#define IT(native_type, name, default) LoadDWord(subKey, Config::name, L#name);
HandTrackedCockpitClicking_DWORD_SETTINGS
#undef IT
#define IT(name, default) LoadFloat(Config::name, L#name);
#define IT(name, default) LoadFloat(subKey, Config::name, L#name);
HandTrackedCockpitClicking_FLOAT_SETTINGS
#undef IT
#define IT(name, default) LoadString(Config::name, L#name);
#define IT(name, default) LoadString(subKey, Config::name, L#name);
HandTrackedCockpitClicking_STRING_SETTINGS
#undef IT
}

void LoadBaseConfig() {
DebugPrint(L"Loading settings from HKLM\\{}", BaseSubKey);
Load(BaseSubKey);
}

void LoadForCurrentProcess() {
LoadBaseConfig();
const auto subKey = AppOverrideSubKey();
DebugPrint(L"Loading app overrides from HKLM\\{}", subKey);
Load(subKey);
}

template <class T>
static void SaveDWord(const wchar_t* valueName, T value) {
auto data = static_cast<DWORD>(value);
const auto result = RegSetKeyValueW(
HKEY_LOCAL_MACHINE,
SubKey.c_str(),
BaseSubKey.c_str(),
valueName,
REG_DWORD,
&data,
Expand All @@ -155,7 +164,7 @@ static void SaveString(const wchar_t* valueName, std::string_view value) {
const std::wstring buffer {winrt::to_hstring(value)};
const auto result = RegSetKeyValueW(
HKEY_LOCAL_MACHINE,
SubKey.c_str(),
BaseSubKey.c_str(),
valueName,
REG_SZ,
buffer.data(),
Expand Down
3 changes: 2 additions & 1 deletion src/lib/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ HandTrackedCockpitClicking_DWORD_SETTINGS
== HandTrackingOrientation::RayCastWithReprojection;
}

void Load();
void LoadBaseConfig();
void LoadForCurrentProcess();

}// namespace HandTrackedCockpitClicking::Config

0 comments on commit cc622cd

Please sign in to comment.