openxr_select_runtime.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "openxr_select_runtime.h"
  31. #ifdef WINDOWS_ENABLED
  32. #define WIN32_LEAN_AND_MEAN
  33. #include <windows.h>
  34. #endif
  35. #include "core/io/dir_access.h"
  36. #include "core/io/json.h"
  37. #include "core/os/os.h"
  38. #include "editor/settings/editor_settings.h"
  39. constexpr char GENERIC_PREFIX[] = "Unknown OpenXR Runtime";
  40. void OpenXRSelectRuntime::_update_items() {
  41. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  42. OS *os = OS::get_singleton();
  43. Dictionary runtimes = EDITOR_GET("xr/openxr/runtime_paths");
  44. int current_runtime = 0;
  45. int index = 0;
  46. String current_path = os->get_environment("XR_RUNTIME_JSON");
  47. // Parse the user's home folder.
  48. String home_folder = os->get_environment("HOME");
  49. if (home_folder.is_empty()) {
  50. home_folder = os->get_environment("HOMEDRIVE") + os->get_environment("HOMEPATH");
  51. }
  52. clear();
  53. add_item("Default", -1);
  54. set_item_metadata(index, "");
  55. index++;
  56. for (const KeyValue<Variant, Variant> &kv : runtimes) {
  57. const String &key = kv.key;
  58. const String &path = kv.value;
  59. String adj_path = path.replace("~", home_folder);
  60. if (da->file_exists(adj_path)) {
  61. add_item(key, index);
  62. set_item_metadata(index, adj_path);
  63. if (current_path == adj_path) {
  64. current_runtime = index;
  65. }
  66. index++;
  67. }
  68. }
  69. select(current_runtime);
  70. }
  71. void OpenXRSelectRuntime::_on_item_selected(int p_which) {
  72. OS *os = OS::get_singleton();
  73. if (p_which == 0) {
  74. // Return to default runtime
  75. os->set_environment("XR_RUNTIME_JSON", "");
  76. } else {
  77. // Select the runtime we want
  78. String runtime_path = get_item_metadata(p_which);
  79. os->set_environment("XR_RUNTIME_JSON", runtime_path);
  80. }
  81. }
  82. void OpenXRSelectRuntime::_notification(int p_notification) {
  83. switch (p_notification) {
  84. case NOTIFICATION_ENTER_TREE: {
  85. // Update dropdown
  86. _update_items();
  87. // Connect signal
  88. connect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_on_item_selected));
  89. } break;
  90. case NOTIFICATION_EXIT_TREE: {
  91. // Disconnect signal
  92. disconnect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_on_item_selected));
  93. } break;
  94. }
  95. }
  96. String OpenXRSelectRuntime::_try_and_get_runtime_name(const String &p_config_file) {
  97. if constexpr (!GD_IS_CLASS_ENABLED(JSON)) {
  98. return "";
  99. }
  100. // Attempt to get a valid runtime name from the json file
  101. String file_contents = FileAccess::get_file_as_string(p_config_file);
  102. Dictionary root_node = JSON::parse_string(file_contents);
  103. if (!root_node.has("runtime")) {
  104. return "";
  105. }
  106. Dictionary api_layer = root_node["runtime"];
  107. if (!api_layer.has("name") || api_layer["name"].get_type() != Variant::STRING) {
  108. return "";
  109. }
  110. return api_layer["name"];
  111. }
  112. void OpenXRSelectRuntime::_add_runtime(Dictionary &r_runtimes, const String &p_config_file) {
  113. if (r_runtimes.values().has(p_config_file)) {
  114. // config file already in the list of runtimes, do not add a duplicate
  115. return;
  116. }
  117. String runtime_name = _try_and_get_runtime_name(p_config_file);
  118. if (runtime_name.is_empty()) {
  119. runtime_name = GENERIC_PREFIX;
  120. }
  121. if (r_runtimes.keys().has(runtime_name)) {
  122. // Highly unlikely, performance is not critical
  123. unsigned int index = 1;
  124. while (r_runtimes.keys().has(runtime_name + " " + uitos(index))) {
  125. index++;
  126. }
  127. runtime_name = runtime_name + " " + uitos(index);
  128. }
  129. r_runtimes[runtime_name] = p_config_file;
  130. }
  131. Dictionary OpenXRSelectRuntime::_enumerate_runtimes() {
  132. Dictionary default_runtimes;
  133. #if defined(WINDOWS_ENABLED)
  134. // Add known common runtimes in case they are not populated in registry
  135. default_runtimes["Meta"] = "C:\\Program Files\\Oculus\\Support\\oculus-runtime\\oculus_openxr_64.json";
  136. default_runtimes["SteamVR"] = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SteamVR\\steamxr_win64.json";
  137. default_runtimes["Varjo"] = "C:\\Program Files\\Varjo\\varjo-openxr\\VarjoOpenXR.json";
  138. default_runtimes["WMR"] = "C:\\WINDOWS\\system32\\MixedRealityRuntime.json";
  139. // Hard code openxr version 1.
  140. LPCWSTR runtimes_key = L"SOFTWARE\\Khronos\\OpenXR\\1\\AvailableRuntimes";
  141. HKEY hkey = nullptr;
  142. LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, runtimes_key, 0, KEY_READ | KEY_QUERY_VALUE, &hkey);
  143. if (result != ERROR_SUCCESS) {
  144. return default_runtimes;
  145. }
  146. DWORD max_value_len, value_count;
  147. result = RegQueryInfoKeyW(
  148. hkey, // hKey
  149. nullptr, // lpClass
  150. nullptr, // lpcchClass
  151. nullptr, // lpReserved
  152. nullptr, // lpcSubKeys
  153. nullptr, // lpcbMaxSubKeyLen
  154. nullptr, // lpcbMaxClassLen
  155. &value_count, // lpcValues
  156. &max_value_len, // lpcbMaxValueNameLen
  157. nullptr, // lpcbMaxValueLen
  158. nullptr, // lpcbSecurityDescriptor
  159. nullptr // lpftLastWriteTime
  160. );
  161. if (result != ERROR_SUCCESS) {
  162. RegCloseKey(hkey);
  163. return default_runtimes;
  164. }
  165. Char16String value_name;
  166. value_name.resize_uninitialized(max_value_len + 1);
  167. DWORD value_len, value_type;
  168. for (DWORD i = 0; i < value_count; i++) {
  169. value_len = max_value_len + 1;
  170. result = RegEnumValueW(
  171. hkey, // hKey
  172. i, // dwIndex
  173. (LPWSTR)value_name.get_data(), // lpValueName
  174. &value_len, // lpcchValueName
  175. nullptr, // lpReserved
  176. &value_type, // lpType
  177. nullptr, // lpData
  178. nullptr // lpcbData
  179. );
  180. if (result != ERROR_SUCCESS || value_type != REG_DWORD) {
  181. continue;
  182. }
  183. _add_runtime(default_runtimes, String::utf16((const char16_t *)value_name.get_data()));
  184. }
  185. // Cleanup, close the key we opened
  186. RegCloseKey(hkey);
  187. #elif defined(LINUXBSD_ENABLED)
  188. default_runtimes["Monado"] = "/usr/share/openxr/1/openxr_monado.json";
  189. default_runtimes["SteamVR"] = "~/.steam/steam/steamapps/common/SteamVR/steamxr_linux64.json";
  190. #endif
  191. return default_runtimes;
  192. }
  193. OpenXRSelectRuntime::OpenXRSelectRuntime() {
  194. // TODO: Move to editor_settings.cpp
  195. EDITOR_DEF_RST("xr/openxr/runtime_paths", _enumerate_runtimes());
  196. set_flat(true);
  197. set_theme_type_variation("TopBarOptionButton");
  198. set_fit_to_longest_item(false);
  199. set_focus_mode(Control::FOCUS_NONE);
  200. set_tooltip_text(TTR("Choose an XR runtime."));
  201. }