feat: use d-bus for server objects

This commit is contained in:
Nova
2024-06-25 16:17:44 -04:00
parent 5be25da46f
commit ebaf113d94
4 changed files with 136 additions and 107 deletions

View File

@@ -1,28 +0,0 @@
use crate::{
core::client::INTERNAL_CLIENT,
nodes::{
spatial::{Spatial, EXPORTED_SPATIALS},
Node,
},
};
use glam::Mat4;
use std::sync::Arc;
use zbus::{interface, Connection};
pub struct HMD;
impl HMD {
pub async fn create(connection: &Connection) -> Arc<Spatial> {
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false);
EXPORTED_SPATIALS.lock().insert(0, node);
connection.object_server().at("/hmd", Self).await.unwrap();
spatial
}
}
#[interface(name = "org.stardustxr.SpatialRef")]
impl HMD {
#[zbus(property)]
pub fn uid(&self) -> u64 {
0
}
}

View File

@@ -1,33 +1,47 @@
use crate::nodes::spatial::Spatial;
#![allow(unused)]
use crate::{
core::client::INTERNAL_CLIENT,
nodes::{
fields::{Field, Shape, EXPORTED_FIELDS},
spatial::{Spatial, EXPORTED_SPATIALS},
Node,
},
};
use glam::{vec3, Mat4};
use input::{
eye_pointer::EyePointer, mouse_pointer::MousePointer, sk_controller::SkController,
sk_hand::SkHand,
};
use play_space::PlaySpace;
use std::sync::Arc;
use stereokit_rust::{
sk::{DisplayMode, MainThreadToken, Sk},
system::{Handed, Input, World},
util::Device,
};
use zbus::{interface, Connection};
pub mod hmd;
pub mod input;
pub mod play_space;
pub struct ServerObjects {
hmd: Arc<Spatial>,
play_space: Option<Arc<Spatial>>,
mouse_pointer: Option<MousePointer>,
hands: Option<(SkHand, SkHand)>,
controllers: Option<(SkController, SkController)>,
eye_pointer: Option<EyePointer>,
play_space: Option<PlaySpace>,
}
impl ServerObjects {
pub fn new(intentional_flatscreen: bool, sk: &Sk, hmd: Arc<Spatial>) -> ServerObjects {
pub fn new(
intentional_flatscreen: bool,
sk: &Sk,
hmd: Arc<Spatial>,
play_space: Option<Arc<Spatial>>,
) -> ServerObjects {
ServerObjects {
hmd,
play_space,
mouse_pointer: intentional_flatscreen
.then(MousePointer::new)
.transpose()
@@ -51,7 +65,6 @@ impl ServerObjects {
.then(EyePointer::new)
.transpose()
.unwrap(),
play_space: World::has_bounds().then(|| PlaySpace::new().ok()).flatten(),
}
}
@@ -64,6 +77,14 @@ impl ServerObjects {
hmd_pose.position.into(),
));
if let Some(play_space) = self.play_space.as_ref() {
let pose = World::get_bounds_pose();
play_space.set_local_transform(Mat4::from_rotation_translation(
pose.orientation.into(),
pose.position.into(),
));
}
if let Some(mouse_pointer) = self.mouse_pointer.as_mut() {
mouse_pointer.update();
}
@@ -78,8 +99,52 @@ impl ServerObjects {
if let Some(eye_pointer) = self.eye_pointer.as_ref() {
eye_pointer.update();
}
if let Some(play_space) = self.play_space.as_ref() {
play_space.update();
}
}
}
pub struct SpatialRef(u64);
impl SpatialRef {
pub async fn create(connection: &Connection, path: &str) -> Arc<Node> {
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
Spatial::add_to(&node, None, Mat4::IDENTITY, false);
let uid: u64 = rand::random();
EXPORTED_SPATIALS.lock().insert(uid, node.clone());
connection
.object_server()
.at(path, Self(uid))
.await
.unwrap();
node
}
}
#[interface(name = "org.stardustxr.SpatialRef")]
impl SpatialRef {
#[zbus(property)]
fn uid(&self) -> u64 {
self.0
}
}
pub struct FieldRef(u64);
impl FieldRef {
pub async fn create(connection: &Connection, path: &str, shape: Shape) -> Arc<Node> {
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
Spatial::add_to(&node, None, Mat4::IDENTITY, false);
Field::add_to(&node, shape).unwrap();
let uid: u64 = rand::random();
EXPORTED_FIELDS.lock().insert(uid, node.clone());
connection
.object_server()
.at(path, Self(uid))
.await
.unwrap();
node
}
}
#[interface(name = "org.stardustxr.FieldRef")]
impl FieldRef {
#[zbus(property)]
fn uid(&self) -> u64 {
self.0
}
}

View File

@@ -1,70 +1,26 @@
use std::sync::Arc;
use color_eyre::eyre::Result;
use glam::Mat4;
use mint::Vector2;
use serde::{Deserialize, Serialize};
use stardust_xr::values::Datamap;
use stereokit_rust::system::World;
use zbus::{interface, Connection, ObjectServer};
use crate::{
core::client::INTERNAL_CLIENT,
nodes::{
data::PulseReceiver,
fields::{Field, Shape},
spatial::Spatial,
Node,
},
};
#[derive(Debug, Deserialize, Serialize)]
struct PlaySpaceMap {
play_space: (),
size: Vector2<f32>,
}
impl Default for PlaySpaceMap {
fn default() -> Self {
Self {
play_space: (),
size: [0.0; 2].into(),
}
pub struct PlaySpaceBounds;
impl PlaySpaceBounds {
pub async fn create(connection: &Connection) {
connection
.object_server()
.at("/org/stardustxr/PlaySpace", Self)
.await
.unwrap();
}
}
pub struct PlaySpace {
_node: Arc<Node>,
spatial: Arc<Spatial>,
field: Arc<Field>,
_pulse_rx: Arc<PulseReceiver>,
}
impl PlaySpace {
pub fn new() -> Result<Self> {
let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?;
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false);
Field::add_to(&node, Shape::Box([0.0; 3].into()))?;
let field = node.get_aspect::<Field>()?.clone();
let pulse_rx = PulseReceiver::add_to(
&node,
field.clone(),
Datamap::from_typed(PlaySpaceMap::default())?,
)?;
Ok(PlaySpace {
_node: node,
spatial,
field,
_pulse_rx: pulse_rx,
})
}
pub fn update(&self) {
let pose = World::get_bounds_pose();
self.spatial
.set_local_transform(Mat4::from_rotation_translation(
pose.orientation.into(),
pose.position.into(),
));
*self.field.shape.lock() =
Shape::Box([World::get_bounds_size().x, 0.0, World::get_bounds_size().y].into());
#[interface(name = "org.stardustxr.PlaySpace")]
impl PlaySpaceBounds {
#[zbus(property)]
fn bounds(&self) -> Vec<(f64, f64)> {
let bounds = World::get_bounds_size();
vec![
((bounds.x).into(), (bounds.y).into()),
((bounds.x).into(), (-bounds.y).into()),
((-bounds.x).into(), (-bounds.y).into()),
((-bounds.x).into(), (bounds.y).into()),
]
}
}