Debian Codegraph @

modules/openxr/editor/openxr_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* openxr_editor_plugin.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
31 #include "openxr_editor_plugin.h"
32
33 #include "../action_map/openxr_action_map.h"
34 #include "../openxr_api.h"
35
36 #include "editor/docks/editor_dock_manager.h"
37 #include "editor/editor_node.h"
38 #include "platform/android/export/export_plugin.h"
39
40 #include <openxr/openxr.h>
41
42 ////////////////////////////////////////////////////////////////////////////
43 // OpenXRExportPlugin
44
45 bool OpenXRExportPlugin::supports_platform(const Ref<EditorExportPlatform> &p_export_platform) const {
46 return p_export_platform->is_class(EditorExportPlatformAndroid::get_class_static());
47 }
48
49 bool OpenXRExportPlugin::is_openxr_mode() const {
50 // Check if OpenXR is enabled using `EditorExportPlatform::get_project_settings()` because that'll
51 // take into account the feature tags on the specific export preset that is being exported.
52 bool openxr_enabled = (bool)get_export_platform()->get_project_setting(get_export_preset(), "xr/openxr/enabled");
53 int xr_mode_index = get_option("xr_features/xr_mode");
54
55 return openxr_enabled && xr_mode_index == XR_MODE_OPENXR;
56 }
57
58 String OpenXRExportPlugin::_get_export_option_warning(const Ref<EditorExportPlatform> &p_export_platform, const String &p_option_name) const {
59 if (!supports_platform(p_export_platform)) {
60 return String();
61 }
62
63 bool gradle_build = get_option("gradle_build/use_gradle_build");
64 if (is_openxr_mode() && !gradle_build) {
65 return "\"Use Gradle Build\" must be enabled when xr_mode is set to \"OpenXR\".";
66 }
67
68 return String();
69 }
70
71 PackedStringArray OpenXRExportPlugin::get_android_dependencies(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
72 PackedStringArray ret;
73
74 if (!supports_platform(p_export_platform)) {
75 return ret;
76 }
77
78 if (is_openxr_mode()) {
79 // Loader is always identified by the full API version even if we're initializing for OpenXR 1.0.
80 int major = XR_VERSION_MAJOR(XR_CURRENT_API_VERSION);
81 int minor = XR_VERSION_MINOR(XR_CURRENT_API_VERSION);
82 int patch = XR_VERSION_PATCH(XR_CURRENT_API_VERSION);
83 String openxr_loader = "org.khronos.openxr:openxr_loader_for_android:" + String::num_int64(major) + "." + String::num_int64(minor) + "." + String::num_int64(patch);
84
85 ret.push_back(openxr_loader);
86 }
87
88 return ret;
89 }
90
91 PackedStringArray OpenXRExportPlugin::_get_export_features(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
92 PackedStringArray features;
93
94 if (!supports_platform(p_export_platform) || !is_openxr_mode()) {
95 return features;
96 }
97
98 // Placeholder for now
99
100 return features;
101 }
102
103 String OpenXRExportPlugin::get_android_manifest_element_contents(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
104 String contents;
105
106 if (!supports_platform(p_export_platform) || !is_openxr_mode()) {
107 return contents;
108 }
109
110 contents += R"n(
111 <uses-permission android:name="org.khronos.openxr.permission.OPENXR" />
112 <uses-permission android:name="org.khronos.openxr.permission.OPENXR_SYSTEM" />
113
114 <queries>
115 <provider android:authorities="org.khronos.openxr.runtime_broker;org.khronos.openxr.system_runtime_broker" />
116
117 <intent>
118 <action android:name="org.khronos.openxr.OpenXRRuntimeService" />
119 </intent>
120 <intent>
121 <action android:name="org.khronos.openxr.OpenXRApiLayerService" />
122 </intent>
123
124 </queries>
125 )n";
126
127 #ifndef DISABLE_DEPRECATED
128 // This logic addresses the issue from https://github.com/GodotVR/godot_openxr_vendors/issues/429.
129 // The issue is caused by this plugin and the vendors plugin adding the same `uses-feature` tag to the generated
130 // manifest, causing a duplicate error at build time.
131 // In order to maintain backward compatibility, we fix the issue by disabling the addition of the `uses-feature`
132 // tag from this plugin when specific conditions are met.
133 bool meta_plugin_enabled = get_option("xr_features/enable_meta_plugin");
134 int meta_boundary_mode = get_option("meta_xr_features/boundary_mode");
135 if (!meta_plugin_enabled || meta_boundary_mode != 1 /* BOUNDARY_DISABLED_VALUE */) {
136 #endif // DISABLE_DEPRECATED
137 contents += " <uses-feature android:name=\"android.hardware.vr.headtracking\" android:required=\"false\" android:version=\"1\" />\n";
138 #ifndef DISABLE_DEPRECATED
139 }
140 #endif // DISABLE_DEPRECATED
141
142 return contents;
143 }
144
145 String OpenXRExportPlugin::get_android_manifest_activity_element_contents(const Ref<EditorExportPlatform> &p_export_platform, bool p_debug) const {
146 String contents;
147
148 if (!supports_platform(p_export_platform) || !is_openxr_mode()) {
149 return contents;
150 }
151
152 contents += R"n(
153 <intent-filter>
154 <action android:name="android.intent.action.MAIN" />
155
156 <category android:name="android.intent.category.DEFAULT" />
157
158 <category android:name="org.khronos.openxr.intent.category.IMMERSIVE_HMD" />
159 </intent-filter>
160 )n";
161
162 return contents;
163 }
164
165 ////////////////////////////////////////////////////////////////////////////
166 // OpenXREditorPlugin
167
168 void OpenXREditorPlugin::edit(Object *p_node) {
169 if (action_map_editor && Object::cast_to<OpenXRActionMap>(p_node)) {
170 String path = Object::cast_to<OpenXRActionMap>(p_node)->get_path();
171 if (path.is_resource_file()) {
172 action_map_editor->open_action_map(path);
173 }
174 }
175 }
176
177 bool OpenXREditorPlugin::handles(Object *p_node) const {
178 if (action_map_editor) {
179 return (Object::cast_to<OpenXRActionMap>(p_node) != nullptr);
180 }
181 return false;
182 }
183
184 void OpenXREditorPlugin::make_visible(bool p_visible) {
185 }
186
187 OpenXREditorPlugin::OpenXREditorPlugin() {
188 // Only add our OpenXR action map editor if OpenXR is enabled for the whole project.
189 if (OpenXRAPI::openxr_is_enabled(false)) {
190 action_map_editor = memnew(OpenXRActionMapEditor);
191 EditorDockManager::get_singleton()->add_dock(action_map_editor);
192
193 binding_modifier_inspector_plugin = Ref<EditorInspectorPluginBindingModifier>(memnew(EditorInspectorPluginBindingModifier));
194 EditorInspector::add_inspector_plugin(binding_modifier_inspector_plugin);
195
196 #ifndef ANDROID_ENABLED
197 select_runtime = memnew(OpenXRSelectRuntime);
198 add_control_to_container(CONTAINER_TOOLBAR, select_runtime);
199 #endif
200 }
201 }
202
203 void OpenXREditorPlugin::_notification(int p_what) {
204 switch (p_what) {
205 case NOTIFICATION_ENTER_TREE: {
206 // Initialize our export plugin
207 openxr_export_plugin.instantiate();
208 add_export_plugin(openxr_export_plugin);
209 } break;
210 case NOTIFICATION_EXIT_TREE: {
211 // Clean up our export plugin
212 remove_export_plugin(openxr_export_plugin);
213
214 openxr_export_plugin.unref();
215 }
216 }
217 }