The open source OpenXR runtime

u/builder: Add new u_builder helper

This does some of the heavy lifting when it comes to managing the system
devices and the space overseer. The same code was replicated in basically
all of the builders, this lets us avoid that.

+183
+77
src/xrt/auxiliary/util/u_builders.c
··· 13 13 14 14 #include "util/u_debug.h" 15 15 #include "util/u_builders.h" 16 + #include "util/u_system_helpers.h" 16 17 #include "util/u_space_overseer.h" 17 18 18 19 ··· 180 181 181 182 *out_xso = (struct xrt_space_overseer *)uso; 182 183 } 184 + 185 + xrt_result_t 186 + u_builder_roles_helper_open_system(struct xrt_builder *xb, 187 + cJSON *config, 188 + struct xrt_prober *xp, 189 + struct xrt_system_devices **out_xsysd, 190 + struct xrt_space_overseer **out_xso, 191 + u_builder_open_system_fn fn) 192 + { 193 + struct u_builder_roles_helper ubrh = XRT_STRUCT_INIT; 194 + xrt_result_t xret; 195 + 196 + // Use the static system devices helper, no dynamic roles. 197 + struct u_system_devices_static *usysds = u_system_devices_static_allocate(); 198 + struct xrt_tracking_origin *origin = &usysds->base.origin; 199 + struct xrt_system_devices *xsysd = &usysds->base.base; 200 + struct xrt_frame_context *xfctx = &usysds->base.xfctx; 201 + 202 + xret = fn( // 203 + xb, // xb 204 + config, // config 205 + xp, // xp 206 + origin, // origin 207 + xsysd, // xsysd 208 + xfctx, // xfctx 209 + &ubrh); // ubrh 210 + if (xret != XRT_SUCCESS) { 211 + xrt_system_devices_destroy(&xsysd); 212 + return xret; 213 + } 214 + 215 + /* 216 + * Assign to role(s). 217 + */ 218 + 219 + xsysd->static_roles.head = ubrh.head; 220 + 221 + u_system_devices_static_finalize( // 222 + usysds, // usysds 223 + ubrh.left, // left 224 + ubrh.right); // right 225 + 226 + 227 + /* 228 + * Create the space overseer. 229 + */ 230 + 231 + *out_xsysd = xsysd; 232 + u_builder_create_space_overseer_legacy( // 233 + ubrh.head, // head 234 + ubrh.left, // left 235 + ubrh.right, // right 236 + xsysd->xdevs, // xdevs 237 + xsysd->xdev_count, // xdev_count 238 + out_xso); // out_xso 239 + 240 + return XRT_SUCCESS; 241 + } 242 + 243 + xrt_result_t 244 + u_builder_open_system_static_roles(struct xrt_builder *xb, 245 + cJSON *config, 246 + struct xrt_prober *xp, 247 + struct xrt_system_devices **out_xsysd, 248 + struct xrt_space_overseer **out_xso) 249 + { 250 + struct u_builder *ub = (struct u_builder *)xb; 251 + 252 + return u_builder_roles_helper_open_system( // 253 + xb, // 254 + config, // 255 + xp, // 256 + out_xsysd, // 257 + out_xso, // 258 + ub->open_system_static_roles); // 259 + }
+106
src/xrt/auxiliary/util/u_builders.h
··· 18 18 #endif 19 19 20 20 struct xrt_prober_device; 21 + struct u_builder_roles_helper; 22 + 21 23 22 24 /*! 23 25 * Max return of the number @ref xrt_prober_device. ··· 27 29 #define U_BUILDER_SEARCH_MAX (16) // 16 Vive trackers 28 30 29 31 /*! 32 + * Argument to @ref u_builder_roles_helper_open_system and implemented by 33 + * @ref u_builder::open_system_static_roles function. 34 + * 35 + * A builder implement this function is free to focus on only creating the 36 + * devices and assigning them initial roles. With this implementation details 37 + * of the @ref xrt_system_devices and @ref xrt_space_overseer is taken care of 38 + * by the caller of this function. 39 + * 40 + * @ingroup aux_util 41 + */ 42 + typedef xrt_result_t (*u_builder_open_system_fn)(struct xrt_builder *xb, 43 + cJSON *config, 44 + struct xrt_prober *xp, 45 + struct xrt_tracking_origin *origin, 46 + struct xrt_system_devices *xsysd, 47 + struct xrt_frame_context *xfctx, 48 + struct u_builder_roles_helper *ubrh); 49 + 50 + /*! 30 51 * A filter to match the against. 31 52 * 32 53 * @ingroup aux_util ··· 53 74 }; 54 75 55 76 /*! 77 + * This small helper struct is for @ref u_builder_roles_helper_open_system, 78 + * lets a builder focus on opening devices rather then dealing with Monado 79 + * structs like @ref xrt_system_devices and the like. 80 + * 81 + * @ingroup aux_util 82 + */ 83 + struct u_builder_roles_helper 84 + { 85 + struct xrt_device *head; 86 + struct xrt_device *left; 87 + struct xrt_device *right; 88 + 89 + struct 90 + { 91 + struct xrt_device *left; 92 + struct xrt_device *right; 93 + } hand_tracking; 94 + }; 95 + 96 + /*! 97 + * This helper struct makes it easier to implement the builder interface, but it 98 + * also comes with a set of integration that may not be what all builders want. 99 + * See the below functions for more information. 100 + * 101 + * * @ref u_builder_open_system_static_roles 102 + * * @ref u_builder_roles_helper_open_system 103 + * 104 + * @ingroup aux_util 105 + */ 106 + struct u_builder 107 + { 108 + //! Base for this struct. 109 + struct xrt_builder base; 110 + 111 + /*! 112 + * @copydoc u_builder_open_system_fn 113 + */ 114 + u_builder_open_system_fn open_system_static_roles; 115 + }; 116 + 117 + 118 + /* 119 + * 120 + * Functions. 121 + * 122 + */ 123 + 124 + /*! 56 125 * Find the first @ref xrt_prober_device in the prober list. 57 126 * 58 127 * @ingroup aux_util ··· 104 173 struct xrt_device **xdevs, 105 174 uint32_t xdev_count, 106 175 struct xrt_space_overseer **out_xso); 176 + 177 + /*! 178 + * Helper to create a system devices that has static roles and a appropriate 179 + * space overseer. Currently uses the functions below to create a full system 180 + * with the help of @p fn argument. But this might change in the future. 181 + * 182 + * * @ref u_system_devices_static_allocate 183 + * * @ref u_system_devices_static_finalize 184 + * * @ref u_builder_create_space_overseer_legacy 185 + * 186 + * @ingroup aux_util 187 + */ 188 + xrt_result_t 189 + u_builder_roles_helper_open_system(struct xrt_builder *xb, 190 + cJSON *config, 191 + struct xrt_prober *xp, 192 + struct xrt_system_devices **out_xsysd, 193 + struct xrt_space_overseer **out_xso, 194 + u_builder_open_system_fn fn); 195 + 196 + /*! 197 + * Implementation for xrt_builder::open_system to be used with @ref u_builder. 198 + * Uses @ref u_builder_roles_helper_open_system internally, a builder that uses 199 + * the @ref u_builder should use this function for xrt_builder::open_system. 200 + * 201 + * When using this function the builder must have @ref u_builder and implement 202 + * the @ref u_builder::open_static_roles function, see documentation for 203 + * @ref u_builder_open_system_fn about requirements. 204 + * 205 + * @ingroup aux_util 206 + */ 207 + xrt_result_t 208 + u_builder_open_system_static_roles(struct xrt_builder *xb, 209 + cJSON *config, 210 + struct xrt_prober *xp, 211 + struct xrt_system_devices **out_xsysd, 212 + struct xrt_space_overseer **out_xso); 107 213 108 214 109 215 #ifdef __cplusplus