summaryrefslogtreecommitdiffstats
path: root/modules/openxr/editor/openxr_select_runtime.cpp
blob: 4d95b079e22477c71ce34cee01681386e847ef97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**************************************************************************/
/*  openxr_select_runtime.cpp                                             */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "openxr_select_runtime.h"

#include "core/io/dir_access.h"
#include "core/os/os.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"

void OpenXRSelectRuntime::_update_items() {
	Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
	OS *os = OS::get_singleton();
	Dictionary runtimes = EDITOR_GET("xr/openxr/runtime_paths");

	int current_runtime = 0;
	int index = 0;
	String current_path = os->get_environment("XR_RUNTIME_JSON");

	// Parse the user's home folder.
	String home_folder = os->get_environment("HOME");
	if (home_folder.is_empty()) {
		home_folder = os->get_environment("HOMEDRIVE") + os->get_environment("HOMEPATH");
	}

	clear();
	add_item("Default", -1);
	set_item_metadata(index, "");
	index++;

	Array keys = runtimes.keys();
	for (int i = 0; i < keys.size(); i++) {
		String key = keys[i];
		String path = runtimes[key];
		String adj_path = path.replace("~", home_folder);

		if (da->file_exists(adj_path)) {
			add_item(key, index);
			set_item_metadata(index, adj_path);

			if (current_path == adj_path) {
				current_runtime = index;
			}
			index++;
		}
	}

	select(current_runtime);
}

void OpenXRSelectRuntime::_item_selected(int p_which) {
	OS *os = OS::get_singleton();

	if (p_which == 0) {
		// Return to default runtime
		os->set_environment("XR_RUNTIME_JSON", "");
	} else {
		// Select the runtime we want
		String runtime_path = get_item_metadata(p_which);
		os->set_environment("XR_RUNTIME_JSON", runtime_path);
	}
}

void OpenXRSelectRuntime::_notification(int p_notification) {
	switch (p_notification) {
		case NOTIFICATION_ENTER_TREE: {
			// Update dropdown
			_update_items();

			// Connect signal
			connect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_item_selected));
		} break;
		case NOTIFICATION_EXIT_TREE: {
			// Disconnect signal
			disconnect(SceneStringName(item_selected), callable_mp(this, &OpenXRSelectRuntime::_item_selected));
		} break;
	}
}

OpenXRSelectRuntime::OpenXRSelectRuntime() {
	Dictionary default_runtimes;

	// Add known common runtimes by default.
#ifdef WINDOWS_ENABLED
	default_runtimes["Meta"] = "C:\\Program Files\\Oculus\\Support\\oculus-runtime\\oculus_openxr_64.json";
	default_runtimes["SteamVR"] = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SteamVR\\steamxr_win64.json";
	default_runtimes["Varjo"] = "C:\\Program Files\\Varjo\\varjo-openxr\\VarjoOpenXR.json";
	default_runtimes["WMR"] = "C:\\WINDOWS\\system32\\MixedRealityRuntime.json";
#endif
#ifdef LINUXBSD_ENABLED
	default_runtimes["Monado"] = "/usr/share/openxr/1/openxr_monado.json";
	default_runtimes["SteamVR"] = "~/.steam/steam/steamapps/common/SteamVR/steamxr_linux64.json";
#endif

	EDITOR_DEF_RST("xr/openxr/runtime_paths", default_runtimes);

	set_flat(true);
	set_theme_type_variation("TopBarOptionButton");
	set_fit_to_longest_item(false);
	set_focus_mode(Control::FOCUS_NONE);
	set_tooltip_text(TTR("Choose an XR runtime."));
}