| 1 | /**************************************************************************/ |
| 2 | /* register_types.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 "register_types.h" |
| 32 | |
| 33 | #include "action_map/openxr_action.h" |
| 34 | #include "action_map/openxr_action_map.h" |
| 35 | #include "action_map/openxr_action_set.h" |
| 36 | #include "action_map/openxr_interaction_profile.h" |
| 37 | #include "action_map/openxr_interaction_profile_metadata.h" |
| 38 | #include "openxr_interface.h" |
| 39 | |
| 40 | #include "extensions/openxr_extension_wrapper_extension.h" |
| 41 | |
| 42 | #include "scene/openxr_hand.h" |
| 43 | |
| 44 | #include "extensions/openxr_composition_layer_depth_extension.h" |
| 45 | #include "extensions/openxr_fb_display_refresh_rate_extension.h" |
| 46 | #include "extensions/openxr_fb_passthrough_extension_wrapper.h" |
| 47 | #include "extensions/openxr_hand_tracking_extension.h" |
| 48 | #include "extensions/openxr_htc_controller_extension.h" |
| 49 | #include "extensions/openxr_htc_vive_tracker_extension.h" |
| 50 | #include "extensions/openxr_huawei_controller_extension.h" |
| 51 | #include "extensions/openxr_ml2_controller_extension.h" |
| 52 | #include "extensions/openxr_palm_pose_extension.h" |
| 53 | #include "extensions/openxr_pico_controller_extension.h" |
| 54 | #include "extensions/openxr_wmr_controller_extension.h" |
| 55 | |
| 56 | #ifdef TOOLS_ENABLED |
| 57 | #include "editor/openxr_editor_plugin.h" |
| 58 | #endif |
| 59 | |
| 60 | #ifdef ANDROID_ENABLED |
| 61 | #include "extensions/openxr_android_extension.h" |
| 62 | #endif |
| 63 | |
| 64 | #include "core/config/project_settings.h" |
| 65 | #include "main/main.h" |
| 66 | |
| 67 | #ifdef TOOLS_ENABLED |
| 68 | #include "editor/editor_node.h" |
| 69 | #endif |
| 70 | |
| 71 | static OpenXRAPI *openxr_api = nullptr; |
| 72 | static OpenXRInteractionProfileMetadata *openxr_interaction_profile_metadata = nullptr; |
| 73 | static Ref<OpenXRInterface> openxr_interface; |
| 74 | |
| 75 | #ifdef TOOLS_ENABLED |
| 76 | static void _editor_init() { |
| 77 | if (OpenXRAPI::openxr_is_enabled(false)) { |
| 78 | // Only add our OpenXR action map editor if OpenXR is enabled for our project |
| 79 | |
| 80 | if (openxr_interaction_profile_metadata == nullptr) { |
| 81 | // If we didn't initialize our actionmap metadata at startup, we initialize it now. |
| 82 | openxr_interaction_profile_metadata = memnew(OpenXRInteractionProfileMetadata); |
| 83 | ERR_FAIL_NULL(openxr_interaction_profile_metadata); |
| 84 | } |
| 85 | |
| 86 | OpenXREditorPlugin *openxr_plugin = memnew(OpenXREditorPlugin()); |
| 87 | EditorNode::get_singleton()->add_editor_plugin(openxr_plugin); |
| 88 | } |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | void initialize_openxr_module(ModuleInitializationLevel p_level) { |
| 93 | if (p_level == MODULE_INITIALIZATION_LEVEL_CORE) { |
| 94 | GDREGISTER_CLASS(OpenXRExtensionWrapperExtension); |
| 95 | GDREGISTER_CLASS(OpenXRAPIExtension); |
| 96 | } |
| 97 | |
| 98 | if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) { |
| 99 | if (OpenXRAPI::openxr_is_enabled(false)) { |
| 100 | // Always register our extension wrappers even if we don't initialize OpenXR. |
| 101 | // Some of these wrappers will add functionality to our editor. |
| 102 | #ifdef ANDROID_ENABLED |
| 103 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRAndroidExtension)); |
| 104 | #endif |
| 105 | |
| 106 | // register our other extensions |
| 107 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRPalmPoseExtension)); |
| 108 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRPicoControllerExtension)); |
| 109 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRCompositionLayerDepthExtension)); |
| 110 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCControllerExtension)); |
| 111 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRHTCViveTrackerExtension)); |
| 112 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRHuaweiControllerExtension)); |
| 113 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRHandTrackingExtension)); |
| 114 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRFbPassthroughExtensionWrapper)); |
| 115 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRDisplayRefreshRateExtension)); |
| 116 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRWMRControllerExtension)); |
| 117 | OpenXRAPI::register_extension_wrapper(memnew(OpenXRML2ControllerExtension)); |
| 118 | } |
| 119 | |
| 120 | if (OpenXRAPI::openxr_is_enabled()) { |
| 121 | openxr_interaction_profile_metadata = memnew(OpenXRInteractionProfileMetadata); |
| 122 | ERR_FAIL_NULL(openxr_interaction_profile_metadata); |
| 123 | openxr_api = memnew(OpenXRAPI); |
| 124 | ERR_FAIL_NULL(openxr_api); |
| 125 | |
| 126 | if (!openxr_api->initialize(Main::get_rendering_driver_name())) { |
| 127 | const char *init_error_message = |
| 128 | "OpenXR was requested but failed to start.\n" |
| 129 | "Please check if your HMD is connected.\n" |
| 130 | "When using Windows MR please note that WMR only has DirectX support, make sure SteamVR is your default OpenXR runtime.\n" |
| 131 | "Godot will start in normal mode.\n" ; |
| 132 | |
| 133 | WARN_PRINT(init_error_message); |
| 134 | |
| 135 | bool init_show_startup_alert = GLOBAL_GET("xr/openxr/startup_alert" ); |
| 136 | if (init_show_startup_alert) { |
| 137 | OS::get_singleton()->alert(init_error_message); |
| 138 | } |
| 139 | |
| 140 | memdelete(openxr_api); |
| 141 | openxr_api = nullptr; |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { |
| 148 | GDREGISTER_CLASS(OpenXRInterface); |
| 149 | |
| 150 | GDREGISTER_CLASS(OpenXRAction); |
| 151 | GDREGISTER_CLASS(OpenXRActionSet); |
| 152 | GDREGISTER_CLASS(OpenXRActionMap); |
| 153 | GDREGISTER_CLASS(OpenXRInteractionProfileMetadata); |
| 154 | GDREGISTER_CLASS(OpenXRIPBinding); |
| 155 | GDREGISTER_CLASS(OpenXRInteractionProfile); |
| 156 | |
| 157 | GDREGISTER_CLASS(OpenXRHand); |
| 158 | |
| 159 | XRServer *xr_server = XRServer::get_singleton(); |
| 160 | if (xr_server) { |
| 161 | openxr_interface.instantiate(); |
| 162 | xr_server->add_interface(openxr_interface); |
| 163 | |
| 164 | if (openxr_interface->initialize_on_startup()) { |
| 165 | openxr_interface->initialize(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #ifdef TOOLS_ENABLED |
| 170 | EditorNode::add_init_callback(_editor_init); |
| 171 | #endif |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void uninitialize_openxr_module(ModuleInitializationLevel p_level) { |
| 176 | if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (openxr_interface.is_valid()) { |
| 181 | // uninitialize just in case |
| 182 | if (openxr_interface->is_initialized()) { |
| 183 | openxr_interface->uninitialize(); |
| 184 | } |
| 185 | |
| 186 | // unregister our interface from the XR server |
| 187 | XRServer *xr_server = XRServer::get_singleton(); |
| 188 | if (xr_server) { |
| 189 | if (xr_server->get_primary_interface() == openxr_interface) { |
| 190 | xr_server->set_primary_interface(Ref<XRInterface>()); |
| 191 | } |
| 192 | xr_server->remove_interface(openxr_interface); |
| 193 | } |
| 194 | |
| 195 | // and release |
| 196 | openxr_interface.unref(); |
| 197 | } |
| 198 | |
| 199 | if (openxr_api) { |
| 200 | openxr_api->finish(); |
| 201 | |
| 202 | memdelete(openxr_api); |
| 203 | openxr_api = nullptr; |
| 204 | } |
| 205 | |
| 206 | if (openxr_interaction_profile_metadata) { |
| 207 | memdelete(openxr_interaction_profile_metadata); |
| 208 | openxr_interaction_profile_metadata = nullptr; |
| 209 | } |
| 210 | |
| 211 | // cleanup our extension wrappers |
| 212 | OpenXRAPI::cleanup_extension_wrappers(); |
| 213 | } |
| 214 | |