openxr_select_runtime.cpp 5.1 KB

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