Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/editor/openxr_select_runtime.cpp
21117 views
1
/**************************************************************************/
2
/* openxr_select_runtime.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "openxr_select_runtime.h"
32
33
#ifdef WINDOWS_ENABLED
34
#define WIN32_LEAN_AND_MEAN
35
#include <windows.h>
36
#endif
37
38
#include "core/io/dir_access.h"
39
#include "core/io/json.h"
40
#include "core/os/os.h"
41
#include "editor/settings/editor_settings.h"
42
43
constexpr char GENERIC_PREFIX[] = "Unknown OpenXR Runtime";
44
45
void OpenXRSelectRuntime::_update_items() {
46
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
47
OS *os = OS::get_singleton();
48
Dictionary runtimes = EDITOR_GET("xr/openxr/runtime_paths");
49
50
int current_runtime = 0;
51
int index = 0;
52
String current_path = os->get_environment("XR_RUNTIME_JSON");
53
54
// Parse the user's home folder.
55
String home_folder = os->get_environment("HOME");
56
if (home_folder.is_empty()) {
57
home_folder = os->get_environment("HOMEDRIVE") + os->get_environment("HOMEPATH");
58
}
59
60
clear();
61
add_item("Default", -1);
62
set_item_metadata(index, "");
63
index++;
64
65
for (const KeyValue<Variant, Variant> &kv : runtimes) {
66
const String &key = kv.key;
67
const String &path = kv.value;
68
String adj_path = path.replace("~", home_folder);
69
70
if (da->file_exists(adj_path)) {
71
add_item(key, index);
72
set_item_metadata(index, adj_path);
73
74
if (current_path == adj_path) {
75
current_runtime = index;
76
}
77
index++;
78
}
79
}
80
81
select(current_runtime);
82
}
83
84
void OpenXRSelectRuntime::_on_item_selected(int p_which) {
85
OS *os = OS::get_singleton();
86
87
if (p_which == 0) {
88
// Return to default runtime
89
os->set_environment("XR_RUNTIME_JSON", "");
90
} else {
91
// Select the runtime we want
92
String runtime_path = get_item_metadata(p_which);
93
os->set_environment("XR_RUNTIME_JSON", runtime_path);
94
}
95
}
96
97
void OpenXRSelectRuntime::_notification(int p_notification) {
98
switch (p_notification) {
99
case NOTIFICATION_ENTER_TREE: {
100
// Update dropdown
101
_update_items();
102
103
// Connect signal
104
connect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_on_item_selected));
105
} break;
106
case NOTIFICATION_EXIT_TREE: {
107
// Disconnect signal
108
disconnect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_on_item_selected));
109
} break;
110
}
111
}
112
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() {
154
Dictionary default_runtimes;
155
156
#if defined(WINDOWS_ENABLED)
157
// Add known common runtimes in case they are not populated in registry
158
default_runtimes["Meta"] = "C:\\Program Files\\Oculus\\Support\\oculus-runtime\\oculus_openxr_64.json";
159
default_runtimes["SteamVR"] = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SteamVR\\steamxr_win64.json";
160
default_runtimes["Varjo"] = "C:\\Program Files\\Varjo\\varjo-openxr\\VarjoOpenXR.json";
161
default_runtimes["WMR"] = "C:\\WINDOWS\\system32\\MixedRealityRuntime.json";
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
RegCloseKey(hkey);
188
return default_runtimes;
189
}
190
191
Char16String value_name;
192
value_name.resize_uninitialized(max_value_len + 1);
193
DWORD value_len, value_type;
194
195
for (DWORD i = 0; i < value_count; i++) {
196
value_len = max_value_len + 1;
197
result = RegEnumValueW(
198
hkey, // hKey
199
i, // dwIndex
200
(LPWSTR)value_name.get_data(), // lpValueName
201
&value_len, // lpcchValueName
202
nullptr, // lpReserved
203
&value_type, // lpType
204
nullptr, // lpData
205
nullptr // lpcbData
206
);
207
if (result != ERROR_SUCCESS || value_type != REG_DWORD) {
208
continue;
209
}
210
211
_add_runtime(default_runtimes, String::utf16((const char16_t *)value_name.get_data()));
212
}
213
214
// Cleanup, close the key we opened
215
RegCloseKey(hkey);
216
217
#elif defined(LINUXBSD_ENABLED)
218
default_runtimes["Monado"] = "/usr/share/openxr/1/openxr_monado.json";
219
default_runtimes["SteamVR"] = "~/.steam/steam/steamapps/common/SteamVR/steamxr_linux64.json";
220
#endif
221
return default_runtimes;
222
}
223
224
OpenXRSelectRuntime::OpenXRSelectRuntime() {
225
// TODO: Move to editor_settings.cpp
226
EDITOR_DEF_RST("xr/openxr/runtime_paths", _enumerate_runtimes());
227
228
set_flat(true);
229
set_theme_type_variation("TopBarOptionButton");
230
set_fit_to_longest_item(false);
231
set_focus_mode(Control::FOCUS_NONE);
232
set_tooltip_text(TTR("Choose an XR runtime."));
233
}
234
235