diff --git a/src/xrt/auxiliary/util/u_device.c b/src/xrt/auxiliary/util/u_device.c index 5a5dd5bcc..c10d1ebfc 100644 --- a/src/xrt/auxiliary/util/u_device.c +++ b/src/xrt/auxiliary/util/u_device.c @@ -294,3 +294,68 @@ u_device_free(struct xrt_device *xdev) free(xdev); } + +/* + * move the assigned xdev from hand to other_hand if: + * - controller of type "any hand" is assigned to hand + * - other_hand is unassiged + */ +static void +try_move_assignment(struct xrt_device **xdevs, int *hand, int *other_hand) +{ + if (*hand != XRT_DEVICE_ROLE_UNASSIGNED && + xdevs[*hand]->device_type == XRT_DEVICE_TYPE_ANY_HAND_CONTROLLER && + *other_hand == XRT_DEVICE_ROLE_UNASSIGNED) { + + *other_hand = *hand; + *hand = XRT_DEVICE_ROLE_UNASSIGNED; + } +} + +void +u_device_assign_xdev_roles(struct xrt_device **xdevs, + size_t num_xdevs, + int *head, + int *left, + int *right) +{ + *head = XRT_DEVICE_ROLE_UNASSIGNED; + *left = XRT_DEVICE_ROLE_UNASSIGNED; + *right = XRT_DEVICE_ROLE_UNASSIGNED; + + for (size_t i = 0; i < num_xdevs; i++) { + if (xdevs[i] == NULL) { + continue; + } + + switch (xdevs[i]->device_type) { + case XRT_DEVICE_TYPE_HMD: + if (*head == XRT_DEVICE_ROLE_UNASSIGNED) { + *head = i; + } + break; + case XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER: + try_move_assignment(xdevs, left, right); + if (*left == XRT_DEVICE_ROLE_UNASSIGNED) { + *left = i; + } + break; + case XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER: + try_move_assignment(xdevs, right, left); + if (*right == XRT_DEVICE_ROLE_UNASSIGNED) { + *right = i; + } + break; + case XRT_DEVICE_TYPE_ANY_HAND_CONTROLLER: + if (*left == XRT_DEVICE_ROLE_UNASSIGNED) { + *left = i; + } else if (*right == XRT_DEVICE_ROLE_UNASSIGNED) { + *right = i; + } else { + //! @todo: do something with unassigend devices? + } + break; + default: break; + } + } +} diff --git a/src/xrt/auxiliary/util/u_device.h b/src/xrt/auxiliary/util/u_device.h index 9d5b26e63..c3bec4ba3 100644 --- a/src/xrt/auxiliary/util/u_device.h +++ b/src/xrt/auxiliary/util/u_device.h @@ -98,6 +98,20 @@ void u_device_free(struct xrt_device *xdev); +#define XRT_DEVICE_ROLE_UNASSIGNED (-1) + +/*! + * Helper function to assign head, left hand and right hand roles. + * + * @ingroup aux_util + */ +void +u_device_assign_xdev_roles(struct xrt_device **xdevs, + size_t num_xdevs, + int *head, + int *left, + int *right); + #ifdef __cplusplus } #endif diff --git a/src/xrt/state_trackers/oxr/oxr_instance.c b/src/xrt/state_trackers/oxr/oxr_instance.c index 905bf6280..d33fd4c44 100644 --- a/src/xrt/state_trackers/oxr/oxr_instance.c +++ b/src/xrt/state_trackers/oxr/oxr_instance.c @@ -114,41 +114,6 @@ cache_path(struct oxr_logger *log, #define NUM_XDEVS 16 -static void -assign_xdev_roles(struct oxr_instance *inst) -{ - struct oxr_system *sys = &inst->system; - for (size_t i = 0; i < NUM_XDEVS; i++) { - if (sys->xdevs[i] == NULL) { - continue; - } - - if (sys->xdevs[i]->device_type == XRT_DEVICE_TYPE_HMD) { - sys->role.head = i; - } else if (sys->xdevs[i]->device_type == - XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER) { - if (sys->role.left == XRT_DEVICE_ROLE_UNASSIGNED) { - sys->role.left = i; - } - } else if (sys->xdevs[i]->device_type == - XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER) { - if (sys->role.right == XRT_DEVICE_ROLE_UNASSIGNED) { - sys->role.right = i; - } - } else if (sys->xdevs[i]->device_type == - XRT_DEVICE_TYPE_ANY_HAND_CONTROLLER) { - if (sys->role.left == XRT_DEVICE_ROLE_UNASSIGNED) { - sys->role.left = i; - } else if (sys->role.right == - XRT_DEVICE_ROLE_UNASSIGNED) { - sys->role.right = i; - } else { - //! @todo: do something with unassigend devices? - } - } - } -} - static inline size_t min_size_t(size_t a, size_t b) { @@ -277,7 +242,8 @@ oxr_instance_create(struct oxr_logger *log, oxr_xdev_destroy(&xdevs[i]); } - assign_xdev_roles(inst); + u_device_assign_xdev_roles(xdevs, NUM_XDEVS, &sys->role.head, + &sys->role.left, &sys->role.right); // Did we find any HMD // @todo Headless with only controllers? diff --git a/src/xrt/state_trackers/oxr/oxr_objects.h b/src/xrt/state_trackers/oxr/oxr_objects.h index b5e1b1cbb..70b6d401b 100644 --- a/src/xrt/state_trackers/oxr/oxr_objects.h +++ b/src/xrt/state_trackers/oxr/oxr_objects.h @@ -20,6 +20,7 @@ #include "util/u_index_fifo.h" #include "util/u_hashset.h" #include "util/u_hashmap.h" +#include "util/u_device.h" #include "oxr_extension_support.h" #include "oxr_subaction.h" @@ -1089,8 +1090,6 @@ struct oxr_handle_base oxr_handle_destroyer destroy; }; -#define XRT_DEVICE_ROLE_UNASSIGNED (-1) - /*! * Single or multiple devices grouped together to form a system that sessions * can be created from. Might need to open devices in order to get all