From 87eafe20ae632df5ea0e42c2031ead5ef7f3bf3a Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Mon, 22 Sep 2025 21:44:58 +1000 Subject: [PATCH] OpenXR: Add support for XR_KHR_generic_controller --- ...penxr_khr_generic_controller_extension.cpp | 79 +++++++++++++++++++ .../openxr_khr_generic_controller_extension.h | 47 +++++++++++ modules/openxr/register_types.cpp | 2 + 3 files changed, 128 insertions(+) create mode 100644 modules/openxr/extensions/openxr_khr_generic_controller_extension.cpp create mode 100644 modules/openxr/extensions/openxr_khr_generic_controller_extension.h diff --git a/modules/openxr/extensions/openxr_khr_generic_controller_extension.cpp b/modules/openxr/extensions/openxr_khr_generic_controller_extension.cpp new file mode 100644 index 0000000000..e8560ae9ce --- /dev/null +++ b/modules/openxr/extensions/openxr_khr_generic_controller_extension.cpp @@ -0,0 +1,79 @@ +/**************************************************************************/ +/* openxr_khr_generic_controller_extension.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_khr_generic_controller_extension.h" + +#include "../action_map/openxr_interaction_profile_metadata.h" +#include "../openxr_api.h" + +HashMap OpenXRKHRGenericController::get_requested_extensions(XrVersion p_version) { + HashMap request_extensions; + + request_extensions[XR_KHR_GENERIC_CONTROLLER_EXTENSION_NAME] = &generic_controller_ext; + + return request_extensions; +} + +bool OpenXRKHRGenericController::is_available() const { + return generic_controller_ext; +} + +void OpenXRKHRGenericController::on_register_metadata() { + OpenXRInteractionProfileMetadata *openxr_metadata = OpenXRInteractionProfileMetadata::get_singleton(); + ERR_FAIL_NULL(openxr_metadata); + + { // KHR Generic controller + const String profile_path = "/interaction_profiles/khr/generic_controller"; + openxr_metadata->register_interaction_profile("Generic controller", profile_path, XR_KHR_GENERIC_CONTROLLER_EXTENSION_NAME); + for (const String user_path : { "/user/hand/left", "/user/hand/right" }) { + openxr_metadata->register_io_path(profile_path, "Primary click", user_path, user_path + "/input/primary/click", "", OpenXRAction::OPENXR_ACTION_BOOL); + openxr_metadata->register_io_path(profile_path, "Secondary click", user_path, user_path + "/input/secondary/click", "", OpenXRAction::OPENXR_ACTION_BOOL); + + openxr_metadata->register_io_path(profile_path, "Thumbstick", user_path, user_path + "/input/thumbstick", "", OpenXRAction::OPENXR_ACTION_VECTOR2); + openxr_metadata->register_io_path(profile_path, "Thumbstick click", user_path, user_path + "/input/thumbstick/click", "", OpenXRAction::OPENXR_ACTION_BOOL); + openxr_metadata->register_io_path(profile_path, "Thumbstick Dpad Up", user_path, user_path + "/input/thumbstick/dpad_up", XR_EXT_DPAD_BINDING_EXTENSION_NAME, OpenXRAction::OPENXR_ACTION_BOOL); + openxr_metadata->register_io_path(profile_path, "Thumbstick Dpad Down", user_path, user_path + "/input/thumbstick/dpad_down", XR_EXT_DPAD_BINDING_EXTENSION_NAME, OpenXRAction::OPENXR_ACTION_BOOL); + openxr_metadata->register_io_path(profile_path, "Thumbstick Dpad Left", user_path, user_path + "/input/thumbstick/dpad_left", XR_EXT_DPAD_BINDING_EXTENSION_NAME, OpenXRAction::OPENXR_ACTION_BOOL); + openxr_metadata->register_io_path(profile_path, "Thumbstick Dpad Right", user_path, user_path + "/input/thumbstick/dpad_right", XR_EXT_DPAD_BINDING_EXTENSION_NAME, OpenXRAction::OPENXR_ACTION_BOOL); + + openxr_metadata->register_io_path(profile_path, "Squeeze", user_path, user_path + "/input/squeeze/value", "", OpenXRAction::OPENXR_ACTION_FLOAT); + openxr_metadata->register_io_path(profile_path, "Trigger", user_path, user_path + "/input/trigger/value", "", OpenXRAction::OPENXR_ACTION_FLOAT); + + openxr_metadata->register_io_path(profile_path, "Grip pose", user_path, user_path + "/input/grip/pose", "", OpenXRAction::OPENXR_ACTION_POSE); + openxr_metadata->register_io_path(profile_path, "Grip surface pose", user_path, user_path + "/input/grip_surface/pose", XR_EXT_PALM_POSE_EXTENSION_NAME "," XR_KHR_MAINTENANCE1_EXTENSION_NAME "," XR_OPENXR_1_1_NAME, OpenXRAction::OPENXR_ACTION_POSE); + openxr_metadata->register_io_path(profile_path, "Aim pose", user_path, user_path + "/input/aim/pose", "", OpenXRAction::OPENXR_ACTION_POSE); + + openxr_metadata->register_io_path(profile_path, "Pinch pose", user_path, user_path + "/input/pinch_ext/pose", XR_EXT_HAND_INTERACTION_EXTENSION_NAME, OpenXRAction::OPENXR_ACTION_POSE); + openxr_metadata->register_io_path(profile_path, "Poke pose", user_path, user_path + "/input/poke_ext/pose", XR_EXT_HAND_INTERACTION_EXTENSION_NAME, OpenXRAction::OPENXR_ACTION_POSE); + + openxr_metadata->register_io_path(profile_path, "Haptic output", user_path, user_path + "/output/haptic", "", OpenXRAction::OPENXR_ACTION_HAPTIC); + } + } +} diff --git a/modules/openxr/extensions/openxr_khr_generic_controller_extension.h b/modules/openxr/extensions/openxr_khr_generic_controller_extension.h new file mode 100644 index 0000000000..4249f903b3 --- /dev/null +++ b/modules/openxr/extensions/openxr_khr_generic_controller_extension.h @@ -0,0 +1,47 @@ +/**************************************************************************/ +/* openxr_khr_generic_controller_extension.h */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include "openxr_extension_wrapper.h" + +class OpenXRKHRGenericController : public OpenXRExtensionWrapper { + GDCLASS(OpenXRKHRGenericController, OpenXRExtensionWrapper); + +public: + virtual HashMap get_requested_extensions(XrVersion p_version) override; + + bool is_available() const; + + virtual void on_register_metadata() override; + +private: + bool generic_controller_ext = false; +}; diff --git a/modules/openxr/register_types.cpp b/modules/openxr/register_types.cpp index 283a9ffa8f..7b0d338abf 100644 --- a/modules/openxr/register_types.cpp +++ b/modules/openxr/register_types.cpp @@ -71,6 +71,7 @@ #include "extensions/openxr_htc_controller_extension.h" #include "extensions/openxr_htc_vive_tracker_extension.h" #include "extensions/openxr_huawei_controller_extension.h" +#include "extensions/openxr_khr_generic_controller_extension.h" #include "extensions/openxr_local_floor_extension.h" #include "extensions/openxr_meta_controller_extension.h" #include "extensions/openxr_ml2_controller_extension.h" @@ -178,6 +179,7 @@ void initialize_openxr_module(ModuleInitializationLevel p_level) { OpenXRAPI::register_extension_wrapper(memnew(OpenXRVisibilityMaskExtension)); OpenXRAPI::register_extension_wrapper(memnew(OpenXRPerformanceSettingsExtension)); OpenXRAPI::register_extension_wrapper(memnew(OpenXRValveControllerExtension)); + OpenXRAPI::register_extension_wrapper(memnew(OpenXRKHRGenericController)); // Futures extension has to be registered as a singleton so extensions can access it. OpenXRFutureExtension *future_extension = memnew(OpenXRFutureExtension);