xrt: Move device assignment code to u_device

This commit is contained in:
Christoph Haag 2020-11-06 16:34:29 +01:00
parent 2855616e32
commit c60b7412e5
4 changed files with 82 additions and 38 deletions

View file

@ -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;
}
}
}

View file

@ -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

View file

@ -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?

View file

@ -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