openxr_select_runtime.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "core/io/dir_access.h"
  32. #include "core/os/os.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. void OpenXRSelectRuntime::_update_items() {
  36. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  37. OS *os = OS::get_singleton();
  38. Dictionary runtimes = EDITOR_GET("xr/openxr/runtime_paths");
  39. int current_runtime = 0;
  40. int index = 0;
  41. String current_path = os->get_environment("XR_RUNTIME_JSON");
  42. // Parse the user's home folder.
  43. String home_folder = os->get_environment("HOME");
  44. if (home_folder.is_empty()) {
  45. home_folder = os->get_environment("HOMEDRIVE") + os->get_environment("HOMEPATH");
  46. }
  47. clear();
  48. add_item("Default", -1);
  49. set_item_metadata(index, "");
  50. index++;
  51. Array keys = runtimes.keys();
  52. for (int i = 0; i < keys.size(); i++) {
  53. String key = keys[i];
  54. String path = runtimes[key];
  55. String adj_path = path.replace("~", home_folder);
  56. if (da->file_exists(adj_path)) {
  57. add_item(key, index);
  58. set_item_metadata(index, adj_path);
  59. if (current_path == adj_path) {
  60. current_runtime = index;
  61. }
  62. index++;
  63. }
  64. }
  65. select(current_runtime);
  66. }
  67. void OpenXRSelectRuntime::_item_selected(int p_which) {
  68. OS *os = OS::get_singleton();
  69. if (p_which == 0) {
  70. // Return to default runtime
  71. os->set_environment("XR_RUNTIME_JSON", "");
  72. } else {
  73. // Select the runtime we want
  74. String runtime_path = get_item_metadata(p_which);
  75. os->set_environment("XR_RUNTIME_JSON", runtime_path);
  76. }
  77. }
  78. void OpenXRSelectRuntime::_notification(int p_notification) {
  79. switch (p_notification) {
  80. case NOTIFICATION_ENTER_TREE: {
  81. // Update dropdown
  82. _update_items();
  83. // Connect signal
  84. connect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_item_selected));
  85. } break;
  86. case NOTIFICATION_EXIT_TREE: {
  87. // Disconnect signal
  88. disconnect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_item_selected));
  89. } break;
  90. }
  91. }
  92. OpenXRSelectRuntime::OpenXRSelectRuntime() {
  93. Dictionary default_runtimes;
  94. // Add known common runtimes by default.
  95. #ifdef WINDOWS_ENABLED
  96. default_runtimes["Meta"] = "C:\\Program Files\\Oculus\\Support\\oculus-runtime\\oculus_openxr_64.json";
  97. default_runtimes["SteamVR"] = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SteamVR\\steamxr_win64.json";
  98. default_runtimes["Varjo"] = "C:\\Program Files\\Varjo\\varjo-openxr\\VarjoOpenXR.json";
  99. default_runtimes["WMR"] = "C:\\WINDOWS\\system32\\MixedRealityRuntime.json";
  100. #endif
  101. #ifdef LINUXBSD_ENABLED
  102. default_runtimes["Monado"] = "/usr/share/openxr/1/openxr_monado.json";
  103. default_runtimes["SteamVR"] = "~/.steam/steam/steamapps/common/SteamVR/steamxr_linux64.json";
  104. #endif
  105. // TODO: Move to editor_settings.cpp
  106. EDITOR_DEF_RST("xr/openxr/runtime_paths", default_runtimes);
  107. set_flat(true);
  108. set_theme_type_variation("TopBarOptionButton");
  109. set_fit_to_longest_item(false);
  110. set_focus_mode(Control::FOCUS_NONE);
  111. set_tooltip_text(TTR("Choose an XR runtime."));
  112. }