1use std::ffi::{c_char, c_void};
2use std::ptr;
3use std::rc::Rc;
4
5use qaros_sys::{self as sys, QarApi, QarResult, defaults};
6
7use crate::error::{Result, check};
8use crate::session::{CallbackState, SessionInner, Subscription, query_owned_handles};
9use crate::strings;
10use crate::types::{AppVolumeId, AppVolumeLifetime, PeerId, Pose};
11
12#[derive(Clone, Copy, Debug, PartialEq)]
14pub struct AppVolumeSize {
15 pub width: f32,
16 pub length: f32,
17 pub height: f32,
18}
19
20impl Default for AppVolumeSize {
21 fn default() -> Self {
23 Self::from_raw(defaults::qar_app_volume_size_default())
24 }
25}
26
27impl AppVolumeSize {
28 fn to_raw(self) -> sys::QarAppVolumeSize {
29 sys::QarAppVolumeSize {
30 width_meters: self.width,
31 length_meters: self.length,
32 height_meters: self.height,
33 }
34 }
35
36 fn from_raw(raw: sys::QarAppVolumeSize) -> Self {
37 Self {
38 width: raw.width_meters,
39 length: raw.length_meters,
40 height: raw.height_meters,
41 }
42 }
43}
44
45#[derive(Clone, Debug)]
47pub struct AppVolumeConfig {
48 pub common_name: String,
51 pub display_name: Option<String>,
52 pub pose: Pose,
54 pub size: AppVolumeSize,
55 pub initial_peers: Vec<PeerId>,
56 pub app_pose: Pose,
58 pub app_scale: f32,
60}
61
62impl AppVolumeConfig {
63 pub fn new(common_name: impl Into<String>) -> Self {
65 Self {
66 common_name: common_name.into(),
67 display_name: None,
68 pose: Pose::default(),
69 size: AppVolumeSize::default(),
70 initial_peers: Vec::new(),
71 app_pose: Pose::default(),
72 app_scale: 1.0,
73 }
74 }
75}
76
77#[derive(Clone, Debug, PartialEq)]
79pub struct AppVolumeInfo {
80 pub id: AppVolumeId,
81 pub display_name: String,
82 pub pose: Pose,
83 pub size: AppVolumeSize,
84 pub lifetime: AppVolumeLifetime,
85 pub app_pose: Pose,
86 pub app_scale: f32,
87}
88
89#[derive(Debug)]
91pub struct AppVolumes<'session> {
92 session: &'session Rc<SessionInner>,
93}
94
95macro_rules! volume_setter {
96 ($(#[$documentation:meta])* $method:ident, $function:ident, $value_type:ty, |$value:ident| $raw_value:expr, $pass:ident) => {
97 $(#[$documentation])*
98 pub fn $method(&self, volume: AppVolumeId, $value: $value_type) -> Result<()> {
99 let api = self.session.api();
100 let volume_id = volume.to_raw();
101 let raw_value = $raw_value;
102 let result = unsafe {
104 (api.$function)(self.session.handle.as_ptr(), &raw const volume_id, volume_setter!(@pass $pass raw_value))
105 };
106 check(api, stringify!($function), result)
107 }
108 };
109 (@pass by_pointer $raw_value:ident) => { &raw const $raw_value };
110 (@pass by_value $raw_value:ident) => { $raw_value };
111}
112
113macro_rules! volume_latest_getter {
114 ($(#[$documentation:meta])* $method:ident, $function:ident, $value_type:ty, $initial:expr, |$raw:ident| $convert:expr) => {
115 $(#[$documentation])*
116 pub fn $method(&self, volume: AppVolumeId) -> Result<$value_type> {
117 let api = self.session.api();
118 let volume_id = volume.to_raw();
119 let mut $raw = $initial;
120 let result = unsafe {
122 (api.$function)(self.session.handle.as_ptr(), &raw const volume_id, &raw mut $raw)
123 };
124 check(api, stringify!($function), result)?;
125 Ok($convert)
126 }
127 };
128}
129
130impl<'session> AppVolumes<'session> {
131 pub(crate) fn new(session: &'session Rc<SessionInner>) -> Self {
132 Self { session }
133 }
134
135 pub fn get_or_create(&self, config: &AppVolumeConfig) -> Result<AppVolumeId> {
138 let common_name = strings::to_c_string(&config.common_name, "common name")?;
139 let display_name =
140 strings::optional_c_string(config.display_name.as_deref(), "display name")?;
141 let initial_peers: Vec<sys::QarPeerId> = config
142 .initial_peers
143 .iter()
144 .map(|peer| peer.to_raw())
145 .collect();
146 let mut initial_peer_pointers: Vec<*const sys::QarPeerId> =
147 initial_peers.iter().map(ptr::from_ref).collect();
148
149 let mut init = defaults::qar_app_volume_init_default();
150 init.common_name = common_name.as_ptr();
151 init.display_name = strings::pointer_or_null(display_name.as_ref());
152 init.pose = config.pose.to_raw();
153 init.size = config.size.to_raw();
154 if !initial_peer_pointers.is_empty() {
155 init.initial_peers = initial_peer_pointers.as_mut_ptr();
156 init.initial_peer_count = initial_peer_pointers.len();
157 }
158 init.app_pose = config.app_pose.to_raw();
159 init.app_scale = config.app_scale;
160
161 let api = self.session.api();
162 let mut volume_id = defaults::qar_app_volume_id_default();
163 let result = unsafe {
165 (api.qar_impl_app_volumes_get_or_create)(
166 self.session.handle.as_ptr(),
167 &raw const init,
168 &raw mut volume_id,
169 )
170 };
171 check(api, "qar_app_volumes_get_or_create", result)?;
172 Ok(AppVolumeId::from_raw(volume_id))
173 }
174
175 volume_setter!(
176 set_pose,
178 qar_impl_app_volumes_change_pose,
179 Pose,
180 |pose| pose.to_raw(),
181 by_pointer
182 );
183 volume_setter!(
184 set_size,
186 qar_impl_app_volumes_change_size,
187 AppVolumeSize,
188 |size| size.to_raw(),
189 by_pointer
190 );
191 volume_setter!(
192 set_app_pose,
194 qar_impl_app_volumes_change_app_pose,
195 Pose,
196 |app_pose| app_pose.to_raw(),
197 by_pointer
198 );
199 volume_setter!(
200 set_app_scale,
202 qar_impl_app_volumes_change_app_scale,
203 f32,
204 |app_scale| app_scale,
205 by_value
206 );
207
208 pub fn set_display_name(&self, volume: AppVolumeId, display_name: &str) -> Result<()> {
210 let api = self.session.api();
211 let volume_id = volume.to_raw();
212 let display_name = strings::to_c_string(display_name, "display name")?;
213 let result = unsafe {
215 (api.qar_impl_app_volumes_change_display_name)(
216 self.session.handle.as_ptr(),
217 &raw const volume_id,
218 display_name.as_ptr(),
219 )
220 };
221 check(api, "qar_app_volumes_change_display_name", result)
222 }
223
224 pub fn close(&self, volume: AppVolumeId) -> Result<()> {
226 let api = self.session.api();
227 let volume_id = volume.to_raw();
228 let result = unsafe {
230 (api.qar_impl_app_volumes_close_volume)(
231 self.session.handle.as_ptr(),
232 &raw const volume_id,
233 )
234 };
235 check(api, "qar_app_volumes_close_volume", result)
236 }
237
238 volume_latest_getter!(
239 latest_pose,
241 qar_impl_app_volume_get_latest_pose,
242 Pose,
243 defaults::qar_pose_default(),
244 |raw| Pose::from_raw(raw)
245 );
246 volume_latest_getter!(
247 latest_size,
249 qar_impl_app_volume_get_latest_size,
250 AppVolumeSize,
251 defaults::qar_app_volume_size_default(),
252 |raw| AppVolumeSize::from_raw(raw)
253 );
254 volume_latest_getter!(
255 latest_app_pose,
257 qar_impl_app_volume_get_latest_app_pose,
258 Pose,
259 defaults::qar_pose_default(),
260 |raw| Pose::from_raw(raw)
261 );
262 volume_latest_getter!(
263 latest_app_scale,
265 qar_impl_app_volume_get_latest_app_scale,
266 f32,
267 1.0_f32,
268 |raw| raw
269 );
270
271 pub fn list(&self) -> Result<Vec<AppVolumeInfo>> {
273 let api = self.session.api();
274 let session = self.session.handle.as_ptr();
275 query_owned_handles(
276 api,
277 |count| unsafe { (api.qar_impl_query_app_volumes_count)(session, count) },
279 |handles, capacity, written| unsafe {
281 (api.qar_impl_query_app_volumes)(session, handles, capacity, written)
282 },
283 api.qar_impl_app_volume_handle_destroy,
284 "qar_query_app_volumes",
285 |handle| read_app_volume_info(api, handle),
286 )
287 }
288
289 pub fn subscribe(
292 &self,
293 callback: impl Fn(AppVolumeInfo) + Send + Sync + 'static,
294 ) -> Result<Subscription> {
295 let session = self.session.handle.as_ptr();
296 self.session.subscribe(
297 Box::new(callback),
298 "qar_app_volumes_subscribe_updates",
299 |api, user_state, token| {
300 unsafe {
303 (api.qar_impl_app_volumes_subscribe_updates)(
304 session,
305 Some(app_volume_update_trampoline),
306 user_state,
307 token,
308 )
309 }
310 },
311 )
312 }
313}
314
315fn read_app_volume_info(api: &QarApi, handle: *mut sys::QarAppVolume) -> Result<AppVolumeInfo> {
316 let mut volume_id = defaults::qar_app_volume_id_default();
317 let mut pose = defaults::qar_pose_default();
318 let mut size = defaults::qar_app_volume_size_default();
319 let mut lifetime = sys::QarAppVolumeLifetimeStatus::QAR_APP_VOLUME_ACTIVE;
320 let mut app_pose = defaults::qar_pose_default();
321 let mut app_scale = 1.0_f32;
322 let results: [(&'static str, QarResult); 6] = unsafe {
324 [
325 (
326 "qar_app_volume_get_id",
327 (api.qar_impl_app_volume_get_id)(handle, &raw mut volume_id),
328 ),
329 (
330 "qar_app_volume_get_pose",
331 (api.qar_impl_app_volume_get_pose)(handle, &raw mut pose),
332 ),
333 (
334 "qar_app_volume_get_size",
335 (api.qar_impl_app_volume_get_size)(handle, &raw mut size),
336 ),
337 (
338 "qar_app_volume_get_lifetime_status",
339 (api.qar_impl_app_volume_get_lifetime_status)(handle, &raw mut lifetime),
340 ),
341 (
342 "qar_app_volume_get_app_pose",
343 (api.qar_impl_app_volume_get_app_pose)(handle, &raw mut app_pose),
344 ),
345 (
346 "qar_app_volume_get_app_scale",
347 (api.qar_impl_app_volume_get_app_scale)(handle, &raw mut app_scale),
348 ),
349 ]
350 };
351 for (function, result) in results {
352 check(api, function, result)?;
353 }
354 let display_name = strings::read_string(|buffer: *mut c_char, size: usize| {
355 let result = unsafe { (api.qar_impl_app_volume_get_display_name)(handle, buffer, size) };
357 check(api, "qar_app_volume_get_display_name", result)
358 })?;
359 Ok(AppVolumeInfo {
360 id: AppVolumeId::from_raw(volume_id),
361 display_name,
362 pose: Pose::from_raw(pose),
363 size: AppVolumeSize::from_raw(size),
364 lifetime: AppVolumeLifetime::from_raw(lifetime),
365 app_pose: Pose::from_raw(app_pose),
366 app_scale,
367 })
368}
369
370unsafe extern "C" fn app_volume_update_trampoline(
371 handle: *mut sys::QarAppVolume,
372 user_state: *mut c_void,
373) {
374 let state = unsafe { CallbackState::<AppVolumeInfo>::from_user_state(user_state) };
376 if handle.is_null() {
377 return;
378 }
379 match read_app_volume_info(state.api(), handle) {
381 Ok(volume) => state.deliver(volume, "app volume update"),
382 Err(error) => log::warn!("reading an app volume update: {error}"),
383 }
384}