Path: blob/master/modules/openxr/editor/openxr_select_runtime.cpp
21117 views
/**************************************************************************/1/* openxr_select_runtime.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "openxr_select_runtime.h"3132#ifdef WINDOWS_ENABLED33#define WIN32_LEAN_AND_MEAN34#include <windows.h>35#endif3637#include "core/io/dir_access.h"38#include "core/io/json.h"39#include "core/os/os.h"40#include "editor/settings/editor_settings.h"4142constexpr char GENERIC_PREFIX[] = "Unknown OpenXR Runtime";4344void OpenXRSelectRuntime::_update_items() {45Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);46OS *os = OS::get_singleton();47Dictionary runtimes = EDITOR_GET("xr/openxr/runtime_paths");4849int current_runtime = 0;50int index = 0;51String current_path = os->get_environment("XR_RUNTIME_JSON");5253// Parse the user's home folder.54String home_folder = os->get_environment("HOME");55if (home_folder.is_empty()) {56home_folder = os->get_environment("HOMEDRIVE") + os->get_environment("HOMEPATH");57}5859clear();60add_item("Default", -1);61set_item_metadata(index, "");62index++;6364for (const KeyValue<Variant, Variant> &kv : runtimes) {65const String &key = kv.key;66const String &path = kv.value;67String adj_path = path.replace("~", home_folder);6869if (da->file_exists(adj_path)) {70add_item(key, index);71set_item_metadata(index, adj_path);7273if (current_path == adj_path) {74current_runtime = index;75}76index++;77}78}7980select(current_runtime);81}8283void OpenXRSelectRuntime::_on_item_selected(int p_which) {84OS *os = OS::get_singleton();8586if (p_which == 0) {87// Return to default runtime88os->set_environment("XR_RUNTIME_JSON", "");89} else {90// Select the runtime we want91String runtime_path = get_item_metadata(p_which);92os->set_environment("XR_RUNTIME_JSON", runtime_path);93}94}9596void OpenXRSelectRuntime::_notification(int p_notification) {97switch (p_notification) {98case NOTIFICATION_ENTER_TREE: {99// Update dropdown100_update_items();101102// Connect signal103connect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_on_item_selected));104} break;105case NOTIFICATION_EXIT_TREE: {106// Disconnect signal107disconnect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_on_item_selected));108} break;109}110}111112String OpenXRSelectRuntime::_try_and_get_runtime_name(const String &p_config_file) {113if constexpr (!GD_IS_CLASS_ENABLED(JSON)) {114return "";115}116117// Attempt to get a valid runtime name from the json file118String file_contents = FileAccess::get_file_as_string(p_config_file);119Dictionary root_node = JSON::parse_string(file_contents);120if (!root_node.has("runtime")) {121return "";122}123Dictionary api_layer = root_node["runtime"];124if (!api_layer.has("name") || api_layer["name"].get_type() != Variant::STRING) {125return "";126}127return api_layer["name"];128}129130void OpenXRSelectRuntime::_add_runtime(Dictionary &r_runtimes, const String &p_config_file) {131if (r_runtimes.values().has(p_config_file)) {132// config file already in the list of runtimes, do not add a duplicate133return;134}135136String runtime_name = _try_and_get_runtime_name(p_config_file);137if (runtime_name.is_empty()) {138runtime_name = GENERIC_PREFIX;139}140141if (r_runtimes.keys().has(runtime_name)) {142// Highly unlikely, performance is not critical143unsigned int index = 1;144while (r_runtimes.keys().has(runtime_name + " " + uitos(index))) {145index++;146}147runtime_name = runtime_name + " " + uitos(index);148}149r_runtimes[runtime_name] = p_config_file;150}151152Dictionary OpenXRSelectRuntime::_enumerate_runtimes() {153Dictionary default_runtimes;154155#if defined(WINDOWS_ENABLED)156// Add known common runtimes in case they are not populated in registry157default_runtimes["Meta"] = "C:\\Program Files\\Oculus\\Support\\oculus-runtime\\oculus_openxr_64.json";158default_runtimes["SteamVR"] = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SteamVR\\steamxr_win64.json";159default_runtimes["Varjo"] = "C:\\Program Files\\Varjo\\varjo-openxr\\VarjoOpenXR.json";160default_runtimes["WMR"] = "C:\\WINDOWS\\system32\\MixedRealityRuntime.json";161162// Hard code openxr version 1.163LPCWSTR runtimes_key = L"SOFTWARE\\Khronos\\OpenXR\\1\\AvailableRuntimes";164HKEY hkey = nullptr;165LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, runtimes_key, 0, KEY_READ | KEY_QUERY_VALUE, &hkey);166if (result != ERROR_SUCCESS) {167return default_runtimes;168}169170DWORD max_value_len, value_count;171result = RegQueryInfoKeyW(172hkey, // hKey173nullptr, // lpClass174nullptr, // lpcchClass175nullptr, // lpReserved176nullptr, // lpcSubKeys177nullptr, // lpcbMaxSubKeyLen178nullptr, // lpcbMaxClassLen179&value_count, // lpcValues180&max_value_len, // lpcbMaxValueNameLen181nullptr, // lpcbMaxValueLen182nullptr, // lpcbSecurityDescriptor183nullptr // lpftLastWriteTime184);185if (result != ERROR_SUCCESS) {186RegCloseKey(hkey);187return default_runtimes;188}189190Char16String value_name;191value_name.resize_uninitialized(max_value_len + 1);192DWORD value_len, value_type;193194for (DWORD i = 0; i < value_count; i++) {195value_len = max_value_len + 1;196result = RegEnumValueW(197hkey, // hKey198i, // dwIndex199(LPWSTR)value_name.get_data(), // lpValueName200&value_len, // lpcchValueName201nullptr, // lpReserved202&value_type, // lpType203nullptr, // lpData204nullptr // lpcbData205);206if (result != ERROR_SUCCESS || value_type != REG_DWORD) {207continue;208}209210_add_runtime(default_runtimes, String::utf16((const char16_t *)value_name.get_data()));211}212213// Cleanup, close the key we opened214RegCloseKey(hkey);215216#elif defined(LINUXBSD_ENABLED)217default_runtimes["Monado"] = "/usr/share/openxr/1/openxr_monado.json";218default_runtimes["SteamVR"] = "~/.steam/steam/steamapps/common/SteamVR/steamxr_linux64.json";219#endif220return default_runtimes;221}222223OpenXRSelectRuntime::OpenXRSelectRuntime() {224// TODO: Move to editor_settings.cpp225EDITOR_DEF_RST("xr/openxr/runtime_paths", _enumerate_runtimes());226227set_flat(true);228set_theme_type_variation("TopBarOptionButton");229set_fit_to_longest_item(false);230set_focus_mode(Control::FOCUS_NONE);231set_tooltip_text(TTR("Choose an XR runtime."));232}233234235