register_types.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*************************************************************************/
  2. /* register_types.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "register_types.h"
  31. #include "main/main.h"
  32. #include "openxr_interface.h"
  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_meta_data.h"
  38. #include "scene/openxr_hand.h"
  39. static OpenXRAPI *openxr_api = nullptr;
  40. static OpenXRInteractionProfileMetaData *openxr_interaction_profile_meta_data = nullptr;
  41. static Ref<OpenXRInterface> openxr_interface;
  42. #ifdef TOOLS_ENABLED
  43. #include "editor/editor_node.h"
  44. #include "editor/openxr_editor_plugin.h"
  45. static void _editor_init() {
  46. if (OpenXRAPI::openxr_is_enabled(false)) {
  47. // Only add our OpenXR action map editor if OpenXR is enabled for our project
  48. if (openxr_interaction_profile_meta_data == nullptr) {
  49. // If we didn't initialize our actionmap meta data at startup, we initialise it now.
  50. openxr_interaction_profile_meta_data = memnew(OpenXRInteractionProfileMetaData);
  51. ERR_FAIL_NULL(openxr_interaction_profile_meta_data);
  52. }
  53. OpenXREditorPlugin *openxr_plugin = memnew(OpenXREditorPlugin());
  54. EditorNode::get_singleton()->add_editor_plugin(openxr_plugin);
  55. }
  56. }
  57. #endif
  58. void initialize_openxr_module(ModuleInitializationLevel p_level) {
  59. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  60. // For now we create our openxr device here. If we merge it with openxr_interface we'll create that here soon.
  61. if (OpenXRAPI::openxr_is_enabled()) {
  62. openxr_interaction_profile_meta_data = memnew(OpenXRInteractionProfileMetaData);
  63. ERR_FAIL_NULL(openxr_interaction_profile_meta_data);
  64. openxr_api = memnew(OpenXRAPI);
  65. ERR_FAIL_NULL(openxr_api);
  66. if (!openxr_api->initialize(Main::get_rendering_driver_name())) {
  67. memdelete(openxr_api);
  68. openxr_api = nullptr;
  69. return;
  70. }
  71. }
  72. }
  73. if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
  74. GDREGISTER_CLASS(OpenXRInterface);
  75. GDREGISTER_CLASS(OpenXRAction);
  76. GDREGISTER_CLASS(OpenXRActionSet);
  77. GDREGISTER_CLASS(OpenXRActionMap);
  78. GDREGISTER_CLASS(OpenXRInteractionProfileMetaData);
  79. GDREGISTER_CLASS(OpenXRIPBinding);
  80. GDREGISTER_CLASS(OpenXRInteractionProfile);
  81. GDREGISTER_CLASS(OpenXRHand);
  82. XRServer *xr_server = XRServer::get_singleton();
  83. if (xr_server) {
  84. openxr_interface.instantiate();
  85. xr_server->add_interface(openxr_interface);
  86. if (openxr_interface->initialize_on_startup()) {
  87. openxr_interface->initialize();
  88. }
  89. }
  90. #ifdef TOOLS_ENABLED
  91. EditorNode::add_init_callback(_editor_init);
  92. #endif
  93. }
  94. }
  95. void uninitialize_openxr_module(ModuleInitializationLevel p_level) {
  96. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  97. return;
  98. }
  99. if (openxr_interface.is_valid()) {
  100. // uninitialize just in case
  101. if (openxr_interface->is_initialized()) {
  102. openxr_interface->uninitialize();
  103. }
  104. // unregister our interface from the XR server
  105. XRServer *xr_server = XRServer::get_singleton();
  106. if (xr_server) {
  107. if (xr_server->get_primary_interface() == openxr_interface) {
  108. xr_server->set_primary_interface(Ref<XRInterface>());
  109. }
  110. xr_server->remove_interface(openxr_interface);
  111. }
  112. // and release
  113. openxr_interface.unref();
  114. }
  115. if (openxr_api) {
  116. openxr_api->finish();
  117. memdelete(openxr_api);
  118. openxr_api = nullptr;
  119. }
  120. if (openxr_interaction_profile_meta_data) {
  121. memdelete(openxr_interaction_profile_meta_data);
  122. openxr_interaction_profile_meta_data = nullptr;
  123. }
  124. }