Skip to content

Commit 21fabab

Browse files
committed
Support reading available OpenXR runtimes from Windows registry
1 parent 68410ac commit 21fabab

File tree

2 files changed

+114
-6
lines changed

2 files changed

+114
-6
lines changed

modules/openxr/editor/openxr_select_runtime.cpp

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@
3030

3131
#include "openxr_select_runtime.h"
3232

33+
#ifdef WINDOWS_ENABLED
34+
#define WIN32_LEAN_AND_MEAN
35+
#include <windows.h>
36+
#endif
37+
3338
#include "core/io/dir_access.h"
39+
#include "core/io/json.h"
3440
#include "core/os/os.h"
3541
#include "editor/settings/editor_settings.h"
3642

43+
constexpr char GENERIC_PREFIX[] = "Unknown OpenXR Runtime";
44+
3745
void OpenXRSelectRuntime::_update_items() {
3846
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
3947
OS *os = OS::get_singleton();
@@ -102,23 +110,119 @@ void OpenXRSelectRuntime::_notification(int p_notification) {
102110
}
103111
}
104112

105-
OpenXRSelectRuntime::OpenXRSelectRuntime() {
113+
String OpenXRSelectRuntime::_try_and_get_runtime_name(const String &p_config_file) {
114+
if constexpr (!GD_IS_CLASS_ENABLED(JSON)) {
115+
return "";
116+
}
117+
118+
// Attempt to get a valid runtime name from the json file
119+
String file_contents = FileAccess::get_file_as_string(p_config_file);
120+
Dictionary root_node = JSON::parse_string(file_contents);
121+
if (!root_node.has("runtime")) {
122+
return "";
123+
}
124+
Dictionary api_layer = root_node["runtime"];
125+
if (!api_layer.has("name") || api_layer["name"].get_type() != Variant::STRING) {
126+
return "";
127+
}
128+
return api_layer["name"];
129+
}
130+
131+
void OpenXRSelectRuntime::_add_runtime(Dictionary &r_runtimes, const String &p_config_file) {
132+
if (r_runtimes.values().has(p_config_file)) {
133+
// config file already in the list of runtimes, do not add a duplicate
134+
return;
135+
}
136+
137+
String runtime_name = _try_and_get_runtime_name(p_config_file);
138+
if (runtime_name.is_empty()) {
139+
runtime_name = GENERIC_PREFIX;
140+
}
141+
142+
if (r_runtimes.keys().has(runtime_name)) {
143+
// Highly unlikely, performance is not critical
144+
unsigned int index = 1;
145+
while (r_runtimes.keys().has(runtime_name + " " + uitos(index))) {
146+
index++;
147+
}
148+
runtime_name = runtime_name + " " + uitos(index);
149+
}
150+
r_runtimes[runtime_name] = p_config_file;
151+
}
152+
153+
Dictionary OpenXRSelectRuntime::_enumerate_runtimes() {
106154
Dictionary default_runtimes;
107155

108-
// Add known common runtimes by default.
109-
#ifdef WINDOWS_ENABLED
156+
#if defined(WINDOWS_ENABLED)
157+
// Add known common runtimes in case they are not populated in registry
110158
default_runtimes["Meta"] = "C:\\Program Files\\Oculus\\Support\\oculus-runtime\\oculus_openxr_64.json";
111159
default_runtimes["SteamVR"] = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SteamVR\\steamxr_win64.json";
112160
default_runtimes["Varjo"] = "C:\\Program Files\\Varjo\\varjo-openxr\\VarjoOpenXR.json";
113161
default_runtimes["WMR"] = "C:\\WINDOWS\\system32\\MixedRealityRuntime.json";
114-
#endif
115-
#ifdef LINUXBSD_ENABLED
162+
163+
// Hard code openxr version 1.
164+
LPCWSTR runtimes_key = L"SOFTWARE\\Khronos\\OpenXR\\1\\AvailableRuntimes";
165+
HKEY hkey = nullptr;
166+
LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, runtimes_key, 0, KEY_READ | KEY_QUERY_VALUE, &hkey);
167+
if (result != ERROR_SUCCESS) {
168+
return default_runtimes;
169+
}
170+
171+
DWORD max_value_len, value_count;
172+
result = RegQueryInfoKeyW(
173+
hkey, // hKey
174+
nullptr, // lpClass
175+
nullptr, // lpcchClass
176+
nullptr, // lpReserved
177+
nullptr, // lpcSubKeys
178+
nullptr, // lpcbMaxSubKeyLen
179+
nullptr, // lpcbMaxClassLen
180+
&value_count, // lpcValues
181+
&max_value_len, // lpcbMaxValueNameLen
182+
nullptr, // lpcbMaxValueLen
183+
nullptr, // lpcbSecurityDescriptor
184+
nullptr // lpftLastWriteTime
185+
);
186+
if (result != ERROR_SUCCESS) {
187+
return default_runtimes;
188+
}
189+
190+
Char16String value_name;
191+
value_name.resize_uninitialized(max_value_len + 1);
192+
DWORD value_len, value_type;
193+
194+
for (DWORD i = 0; i < value_count; i++) {
195+
value_len = max_value_len + 1;
196+
result = RegEnumValueW(
197+
hkey, // hKey
198+
i, // dwIndex
199+
(LPWSTR)value_name.get_data(), // lpValueName
200+
&value_len, // lpcchValueName
201+
nullptr, // lpReserved
202+
&value_type, // lpType
203+
nullptr, // lpData
204+
nullptr // lpcbData
205+
);
206+
if (result != ERROR_SUCCESS || value_type != REG_DWORD) {
207+
continue;
208+
}
209+
210+
_add_runtime(default_runtimes, String::utf16((const char16_t *)value_name.get_data()));
211+
}
212+
213+
// Cleanup, close the key we opened
214+
RegCloseKey(hkey);
215+
216+
#elif defined(LINUXBSD_ENABLED)
116217
default_runtimes["Monado"] = "/usr/share/openxr/1/openxr_monado.json";
117218
default_runtimes["SteamVR"] = "~/.steam/steam/steamapps/common/SteamVR/steamxr_linux64.json";
118219
#endif
220+
return default_runtimes;
221+
}
119222

223+
OpenXRSelectRuntime::OpenXRSelectRuntime() {
120224
// TODO: Move to editor_settings.cpp
121-
EDITOR_DEF_RST("xr/openxr/runtime_paths", default_runtimes);
225+
EDITOR_DEF_RST("xr/openxr/runtime_paths", _enumerate_runtimes());
122226

123227
set_flat(true);
124228
set_theme_type_variation("TopBarOptionButton");

modules/openxr/editor/openxr_select_runtime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ class OpenXRSelectRuntime : public OptionButton {
4444
private:
4545
void _update_items();
4646
void _on_item_selected(int p_which);
47+
48+
Dictionary _enumerate_runtimes();
49+
String _try_and_get_runtime_name(const String &p_config_file);
50+
void _add_runtime(Dictionary &r_runtimes, const String &p_config_file);
4751
};

0 commit comments

Comments
 (0)