openxr_select_runtime.cpp 5.1 KB

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