From 7282684104a2fc9cfbfb76fd70ad9fd79a00f618 Mon Sep 17 00:00:00 2001 From: Nova Date: Mon, 31 Jul 2023 23:44:17 -0400 Subject: [PATCH] feat: fd passing --- Cargo.lock | 49 ++++++++++---------- Cargo.toml | 4 +- src/core/scenegraph.rs | 36 ++++++++++++--- src/core/typed_datamap.rs | 6 ++- src/nodes/audio.rs | 10 ++-- src/nodes/data.rs | 26 +++++------ src/nodes/drawable/lines.rs | 22 ++++++--- src/nodes/drawable/mod.rs | 6 +-- src/nodes/drawable/model.rs | 9 ++-- src/nodes/drawable/text.rs | 18 +++++--- src/nodes/fields/box.rs | 17 +++++-- src/nodes/fields/cylinder.rs | 13 ++++-- src/nodes/fields/mod.rs | 40 ++++++++++------ src/nodes/fields/sphere.rs | 13 ++++-- src/nodes/fields/torus.rs | 13 ++++-- src/nodes/input/mod.rs | 32 ++++++------- src/nodes/input/pointer.rs | 6 +-- src/nodes/input/tip.rs | 10 ++-- src/nodes/items/environment.rs | 20 ++++---- src/nodes/items/mod.rs | 71 ++++++++++++++++------------- src/nodes/items/panel.rs | 75 ++++++++++++++++++------------ src/nodes/mod.rs | 80 ++++++++++++++++++++++++-------- src/nodes/root.rs | 14 ++++-- src/nodes/spatial/mod.rs | 83 ++++++++++++++++++++-------------- src/nodes/spatial/zone.rs | 30 ++++++------ src/nodes/startup.rs | 31 +++++++------ src/wayland/surface.rs | 4 +- src/wayland/xdg_shell.rs | 46 ++++++++++--------- src/wayland/xwayland.rs | 9 ++-- 29 files changed, 484 insertions(+), 309 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 971019d..d37e251 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -161,7 +161,7 @@ checksum = "cc6dde6e4ed435a4c1ee4e73592f5ba9da2151af10076cc04858746af9352d09" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -310,7 +310,7 @@ checksum = "fdde5c9cd29ebd706ce1b35600920a33550e402fc998a2e53ad3b42c3c47a192" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -428,7 +428,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -471,9 +471,9 @@ dependencies = [ [[package]] name = "color-rs" -version = "0.7.1" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05d6cda18d80350d6a6d15d1f2dd6f5c88b3d61882e08801e8d68f506434b808" +checksum = "3415c18b81f66b23614db9fcccbf19d2af434e04d9a6c7ac10e49930f39d89f8" dependencies = [ "angle", "half", @@ -495,7 +495,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f76990911f2267d837d9d0ad060aa63aaad170af40904b29461734c339030d4d" dependencies = [ "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -1276,7 +1276,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -1488,7 +1488,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -1624,7 +1624,7 @@ checksum = "ec2e072ecce94ec471b13398d5402c188e76ac03cf74dd1a975161b23a3f6d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -1961,22 +1961,22 @@ checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" [[package]] name = "serde" -version = "1.0.174" +version = "1.0.180" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b88756493a5bd5e5395d53baa70b194b05764ab85b59e43e4b8f4e1192fa9b1" +checksum = "0ea67f183f058fe88a4e3ec6e2788e003840893b91bac4559cabedd00863b3ed" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.174" +version = "1.0.180" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e5c3a298c7f978e53536f95a63bdc4c4a64550582f31a0359a9afda6aede62e" +checksum = "24e744d7782b686ab3b73267ef05697159cc0e5abbed3f47f9933165e5219036" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -1998,7 +1998,7 @@ checksum = "e168eaaf71e8f9bd6037feb05190485708e019f4fd87d161b3c0a0d37daf85e5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -2097,15 +2097,16 @@ dependencies = [ [[package]] name = "stardust-xr" -version = "0.11.4" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0efaf53544cca5e641ba51928f492a8639cf62399207f8d5c1e3f917e4fc4aa1" +checksum = "5f493d5557c543404778beeba7051bc7bf537541b91cbd2b67983515675acba8" dependencies = [ "chrono", "cluFlock", "color-rs", "dirs", "mint", + "nix 0.26.2", "parking_lot 0.12.1", "rustc-hash", "serde", @@ -2218,9 +2219,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.27" +version = "2.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" +checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" dependencies = [ "proc-macro2", "quote", @@ -2263,7 +2264,7 @@ checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -2326,7 +2327,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -2451,7 +2452,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", ] [[package]] @@ -2582,7 +2583,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", "wasm-bindgen-shared", ] @@ -2604,7 +2605,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn 2.0.28", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 980bba0..3397e1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ tokio = { version = "1.27.0", features = ["rt-multi-thread", "signal"] } send_wrapper = "0.6.0" prisma = "0.1.1" xkbcommon = { version = "0.5.0", default-features = false, optional = true } -stardust-xr = "0.11.4" +stardust-xr = "0.13.0" directories = "5.0.0" serde = { version = "1.0.160", features = ["derive"] } tracing = "0.1.37" @@ -64,7 +64,7 @@ atty = "0.2.14" [dependencies.stereokit] default-features = false features = ["linux-egl"] -version = "0.16.7" +version = "0.16.9" [dependencies.smithay] # git = "https://github.com/technobaboo/smithay.git" # Until we get stereokit to understand OES samplers and external textures diff --git a/src/core/scenegraph.rs b/src/core/scenegraph.rs index 46a67e0..a8dd85b 100644 --- a/src/core/scenegraph.rs +++ b/src/core/scenegraph.rs @@ -1,9 +1,10 @@ -use crate::core::client::Client; use crate::nodes::Node; +use crate::{core::client::Client, nodes::Message}; use color_eyre::eyre::Result; use once_cell::sync::OnceCell; use stardust_xr::scenegraph; use stardust_xr::scenegraph::ScenegraphError; +use std::os::fd::OwnedFd; use std::sync::{Arc, Weak}; use tracing::{debug, debug_span, instrument}; @@ -50,12 +51,25 @@ impl Scenegraph { } impl scenegraph::Scenegraph for Scenegraph { - fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> { + fn send_signal( + &self, + path: &str, + method: &str, + data: &[u8], + fds: Vec, + ) -> Result<(), ScenegraphError> { let Some(client) = self.get_client() else {return Err(ScenegraphError::SignalNotFound)}; debug_span!("Handle signal", path, method).in_scope(|| { self.get_node(path) .ok_or(ScenegraphError::NodeNotFound)? - .send_local_signal(client, method, data) + .send_local_signal( + client, + method, + Message { + data: data.to_vec(), + fds, + }, + ) }) } fn execute_method( @@ -63,12 +77,22 @@ impl scenegraph::Scenegraph for Scenegraph { path: &str, method: &str, data: &[u8], - ) -> Result, ScenegraphError> { + fds: Vec, + ) -> Result<(Vec, Vec), ScenegraphError> { let Some(client) = self.get_client() else {return Err(ScenegraphError::MethodNotFound)}; debug_span!("Handle method", path, method).in_scope(|| { - self.get_node(path) + let message = self + .get_node(path) .ok_or(ScenegraphError::NodeNotFound)? - .execute_local_method(client, method, data) + .execute_local_method( + client, + method, + Message { + data: data.to_vec(), + fds, + }, + )?; + Ok((message.data, message.fds)) }) } } diff --git a/src/core/typed_datamap.rs b/src/core/typed_datamap.rs index 88f4ed1..4f42f90 100644 --- a/src/core/typed_datamap.rs +++ b/src/core/typed_datamap.rs @@ -10,13 +10,15 @@ use stardust_xr::schemas::{ flex::flexbuffers::{FlexbufferSerializer, Reader, ReaderError}, }; +use crate::nodes::Message; + pub struct TypedDatamap(T); impl TypedDatamap { pub fn new(data: T) -> Self { TypedDatamap(data) } - pub fn from_flex(data: &[u8]) -> Result { - let root = Reader::get_root(data)?; + pub fn from_flex(message: Message) -> Result { + let root = Reader::get_root(message.as_ref())?; T::deserialize(root).map(Self::new).map_err(|e| e.into()) } pub fn to_datamap(&mut self) -> Result { diff --git a/src/nodes/audio.rs b/src/nodes/audio.rs index cd22cfe..7bedbcd 100644 --- a/src/nodes/audio.rs +++ b/src/nodes/audio.rs @@ -1,4 +1,4 @@ -use super::Node; +use super::{Message, Node}; use crate::core::client::Client; use crate::core::destroy_queue; use crate::core::registry::Registry; @@ -83,13 +83,13 @@ impl Sound { } } - fn play_flex(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result<()> { + fn play_flex(node: &Node, _calling_client: Arc, _message: Message) -> Result<()> { let sound = node.sound.get().unwrap(); sound.play.lock().replace(()); Ok(()) } - pub fn stop_flex(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result<()> { + pub fn stop_flex(node: &Node, _calling_client: Arc, _message: Message) -> Result<()> { let sound = node.sound.get().unwrap(); sound.stop.lock().replace(()); Ok(()) @@ -108,7 +108,7 @@ pub fn create_interface(client: &Arc) -> Result<()> { node.add_to_scenegraph().map(|_| ()) } -pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn create_flex(_node: &Node, calling_client: Arc, message: Message) -> Result<()> { #[derive(Deserialize)] struct CreateSoundInfo<'a> { name: &'a str, @@ -116,7 +116,7 @@ pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Re transform: Transform, resource: ResourceID, } - let info: CreateSoundInfo = deserialize(data)?; + let info: CreateSoundInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/audio/sound", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, true); diff --git a/src/nodes/data.rs b/src/nodes/data.rs index 51fefa7..bf68058 100644 --- a/src/nodes/data.rs +++ b/src/nodes/data.rs @@ -1,7 +1,7 @@ use super::alias::AliasInfo; use super::fields::Field; use super::spatial::{parse_transform, Spatial}; -use super::{Alias, Node}; +use super::{Alias, Message, Node}; use crate::core::client::Client; use crate::core::node_collections::LifeLinkedNodeMap; use crate::core::registry::Registry; @@ -85,9 +85,9 @@ impl PulseSender { if !mask_matches(&self.mask, &receiver.mask) { return; } - let Some(tx_node) = self.node.upgrade() else { return }; - let Some(tx_client) = tx_node.get_client() else { return }; - let Some(rx_node) = receiver.node.upgrade() else { return }; + let Some(tx_node) = self.node.upgrade() else {return}; + let Some(tx_client) = tx_node.get_client() else {return}; + let Some(rx_node) = receiver.node.upgrade() else {return}; // Receiver itself let rx_alias = Alias::create( &tx_client, @@ -142,7 +142,7 @@ impl PulseSender { }; let Ok(data) = serialize(info) else {return}; - let _ = tx_node.send_remote_signal("new_receiver", &data); + let _ = tx_node.send_remote_signal("new_receiver", data); } fn handle_drop_receiver(&self, receiver: &PulseReceiver) { @@ -151,11 +151,11 @@ impl PulseSender { self.aliases.remove(&(uid.to_string() + "-field")); let Some(tx_node) = self.node.upgrade() else {return}; let Ok(data) = serialize(&uid) else {return}; - let _ = tx_node.send_remote_signal("drop_receiver", &data); + let _ = tx_node.send_remote_signal("drop_receiver", data); } - fn send_data_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { - let info: SendDataInfo = deserialize(data)?; + fn send_data_flex(node: &Node, calling_client: Arc, message: Message) -> Result<()> { + let info: SendDataInfo = deserialize(message.as_ref())?; let receiver_node = calling_client.get_node("Pulse receiver", info.uid)?; let receiver = receiver_node.get_aspect("Pulse Receiver", "pulse receiver", |n| &n.pulse_receiver)?; @@ -210,7 +210,7 @@ impl PulseReceiver { pub fn send_data(&self, uid: &str, data: Vec) -> Result<()> { if let Some(node) = self.node.upgrade() { - node.send_remote_signal("data", &serialize(SendDataInfo { uid, data })?)?; + node.send_remote_signal("data", serialize(SendDataInfo { uid, data })?)?; } Ok(()) } @@ -235,7 +235,7 @@ pub fn create_interface(client: &Arc) -> Result<()> { pub fn create_pulse_sender_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { #[derive(Deserialize)] struct CreatePulseSenderInfo<'a> { @@ -244,7 +244,7 @@ pub fn create_pulse_sender_flex( transform: Transform, mask: Vec, } - let info: CreatePulseSenderInfo = deserialize(data)?; + let info: CreatePulseSenderInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/data/sender", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); @@ -261,7 +261,7 @@ pub fn create_pulse_sender_flex( pub fn create_pulse_receiver_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { #[derive(Deserialize)] struct CreatePulseReceiverInfo<'a> { @@ -271,7 +271,7 @@ pub fn create_pulse_receiver_flex( field_path: &'a str, mask: Vec, } - let info: CreatePulseReceiverInfo = deserialize(data)?; + let info: CreatePulseReceiverInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/data/receiver", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); diff --git a/src/nodes/drawable/lines.rs b/src/nodes/drawable/lines.rs index 83284d6..ad69c80 100644 --- a/src/nodes/drawable/lines.rs +++ b/src/nodes/drawable/lines.rs @@ -2,7 +2,7 @@ use crate::{ core::{client::Client, registry::Registry}, nodes::{ spatial::{find_spatial_parent, parse_transform, Spatial}, - Node, + Message, Node, }, }; use color_eyre::eyre::{bail, ensure, Result}; @@ -97,10 +97,14 @@ impl Lines { draw_ctx.line_add_listv(points.make_contiguous()); } - pub fn set_points_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + pub fn set_points_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")}; - let mut points: Vec = deserialize(data)?; + let mut points: Vec = deserialize(message.as_ref())?; for p in &mut points { p.color[0] = p.color[0].powf(2.2); p.color[1] = p.color[1].powf(2.2); @@ -109,10 +113,14 @@ impl Lines { lines.data.lock().points = points; Ok(()) } - pub fn set_cyclic_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + pub fn set_cyclic_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")}; - lines.data.lock().cyclic = deserialize(data)?; + lines.data.lock().cyclic = deserialize(message.as_ref())?; Ok(()) } } @@ -130,7 +138,7 @@ pub fn draw_all(draw_ctx: &impl StereoKitDraw) { } } -pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn create_flex(_node: &Node, calling_client: Arc, message: Message) -> Result<()> { #[derive(Deserialize)] struct CreateTextInfo<'a> { name: &'a str, @@ -139,7 +147,7 @@ pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Re points: Vec, cyclic: bool, } - let mut info: CreateTextInfo = deserialize(data)?; + let mut info: CreateTextInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/drawable/lines", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, true); diff --git a/src/nodes/drawable/mod.rs b/src/nodes/drawable/mod.rs index 6001fe6..10eb2a2 100644 --- a/src/nodes/drawable/mod.rs +++ b/src/nodes/drawable/mod.rs @@ -9,7 +9,7 @@ use self::{ text::Text, }; -use super::Node; +use super::{Message, Node}; use crate::core::client::Client; use color_eyre::eyre::Result; use parking_lot::Mutex; @@ -56,14 +56,14 @@ pub fn draw(sk: &impl StereoKitDraw) { static QUEUED_SKYLIGHT: Mutex> = Mutex::new(None); static QUEUED_SKYTEX: Mutex> = Mutex::new(None); -fn set_sky_file_flex(_node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { +fn set_sky_file_flex(_node: &Node, _calling_client: Arc, message: Message) -> Result<()> { #[derive(Deserialize)] struct SkyFileInfo { path: PathBuf, skytex: Option, skylight: Option, } - let info: SkyFileInfo = deserialize(data)?; + let info: SkyFileInfo = deserialize(message.as_ref())?; info.path.metadata()?; if info.skytex.unwrap_or_default() { QUEUED_SKYTEX.lock().replace(info.path.clone()); diff --git a/src/nodes/drawable/model.rs b/src/nodes/drawable/model.rs index bb5d798..ef7fce5 100644 --- a/src/nodes/drawable/model.rs +++ b/src/nodes/drawable/model.rs @@ -5,6 +5,7 @@ use crate::core::registry::Registry; use crate::core::resource::ResourceID; use crate::nodes::drawable::Drawable; use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial}; +use crate::nodes::Message; use crate::SK_MULTITHREAD; use color_eyre::eyre::{bail, ensure, eyre, Result}; use glam::Mat4; @@ -213,11 +214,11 @@ impl ModelPart { fn set_material_parameter_flex( node: &Node, _calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { let Some(Drawable::ModelPart(model_part)) = node.drawable.get() else {bail!("Not a drawable??")}; - let (name, value): (String, MaterialParameter) = deserialize(data)?; + let (name, value): (String, MaterialParameter) = deserialize(message.as_ref())?; model_part .pending_material_parameters @@ -342,7 +343,7 @@ pub fn draw_all(sk: &impl StereoKitDraw) { } } -pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn create_flex(_node: &Node, calling_client: Arc, message: Message) -> Result<()> { #[derive(Deserialize)] struct CreateModelInfo<'a> { name: &'a str, @@ -350,7 +351,7 @@ pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Re transform: Transform, resource: ResourceID, } - let info: CreateModelInfo = deserialize(data)?; + let info: CreateModelInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/drawable/model", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, true); diff --git a/src/nodes/drawable/text.rs b/src/nodes/drawable/text.rs index cefd5fa..c3148ec 100644 --- a/src/nodes/drawable/text.rs +++ b/src/nodes/drawable/text.rs @@ -3,7 +3,7 @@ use crate::{ nodes::{ drawable::Drawable, spatial::{find_spatial_parent, parse_transform, Spatial}, - Node, + Message, Node, }, }; use color_eyre::eyre::{bail, ensure, eyre, Result}; @@ -151,18 +151,22 @@ impl Text { pub fn set_character_height_flex( node: &Node, _calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")}; - text.data.lock().character_height = deserialize(data)?; + text.data.lock().character_height = deserialize(message.as_ref())?; Ok(()) } - pub fn set_text_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + pub fn set_text_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")}; - text.data.lock().text = deserialize(data)?; + text.data.lock().text = deserialize(message.as_ref())?; Ok(()) } } @@ -183,7 +187,7 @@ pub fn draw_all(sk: &impl StereoKitDraw) { } } -pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn create_flex(_node: &Node, calling_client: Arc, message: Message) -> Result<()> { #[derive(Deserialize)] struct CreateTextInfo<'a> { name: &'a str, @@ -198,7 +202,7 @@ pub fn create_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Re bounds_align: TextAlign, color: [f32; 4], } - let info: CreateTextInfo = deserialize(data)?; + let info: CreateTextInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/drawable/text", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, true); diff --git a/src/nodes/fields/box.rs b/src/nodes/fields/box.rs index ffc4de7..130a4ce 100644 --- a/src/nodes/fields/box.rs +++ b/src/nodes/fields/box.rs @@ -1,6 +1,7 @@ use super::{Field, FieldTrait, Node}; use crate::core::client::Client; use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial}; +use crate::nodes::Message; use color_eyre::eyre::{ensure, Result}; use glam::{vec3, vec3a, Vec3, Vec3A}; use mint::Vector3; @@ -40,9 +41,13 @@ impl BoxField { *self.size.lock() = size.into(); } - pub fn set_size_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + pub fn set_size_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Field::Box(box_field) = node.field.get().unwrap().as_ref() else { return Ok(()) }; - box_field.set_size(deserialize(data)?); + box_field.set_size(deserialize(message.as_ref())?); Ok(()) } @@ -64,7 +69,11 @@ impl FieldTrait for BoxField { } } -pub fn create_box_field_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn create_box_field_flex( + _node: &Node, + calling_client: Arc, + message: Message, +) -> Result<()> { #[derive(Deserialize)] struct CreateFieldInfo<'a> { name: &'a str, @@ -72,7 +81,7 @@ pub fn create_box_field_flex(_node: &Node, calling_client: Arc, data: &[ transform: Transform, size: Vector3, } - let info: CreateFieldInfo = deserialize(data)?; + let info: CreateFieldInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/field", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); diff --git a/src/nodes/fields/cylinder.rs b/src/nodes/fields/cylinder.rs index 19c5943..8a0733e 100644 --- a/src/nodes/fields/cylinder.rs +++ b/src/nodes/fields/cylinder.rs @@ -1,6 +1,7 @@ use super::{Field, FieldTrait, Node}; use crate::core::client::Client; use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial}; +use crate::nodes::Message; use color_eyre::eyre::{ensure, Result}; use glam::{swizzles::*, vec2, Vec3A}; use portable_atomic::AtomicF32; @@ -43,9 +44,13 @@ impl CylinderField { self.radius.store(radius.abs(), Ordering::Relaxed); } - pub fn set_size_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + pub fn set_size_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Field::Cylinder(cylinder_field) = node.field.get().unwrap().as_ref() else { return Ok(()) }; - let (length, radius) = deserialize(data)?; + let (length, radius) = deserialize(message.as_ref())?; cylinder_field.set_size(length, radius); Ok(()) } @@ -67,7 +72,7 @@ impl FieldTrait for CylinderField { pub fn create_cylinder_field_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { #[derive(Deserialize)] struct CreateFieldInfo<'a> { @@ -77,7 +82,7 @@ pub fn create_cylinder_field_flex( length: f32, radius: f32, } - let info: CreateFieldInfo = deserialize(data)?; + let info: CreateFieldInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/field", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); diff --git a/src/nodes/fields/mod.rs b/src/nodes/fields/mod.rs index 548cf1c..f3553ee 100644 --- a/src/nodes/fields/mod.rs +++ b/src/nodes/fields/mod.rs @@ -10,7 +10,7 @@ use self::torus::{create_torus_field_flex, TorusField}; use super::alias::AliasInfo; use super::spatial::Spatial; -use super::Node; +use super::{Message, Node}; use crate::core::client::Client; use crate::nodes::spatial::find_reference_space; use color_eyre::eyre::Result; @@ -133,13 +133,17 @@ const MAX_RAY_MARCH: f32 = f32::MAX; // const MIN_RAY_LENGTH: f32 = 0_f32; const MAX_RAY_LENGTH: f32 = 1000_f32; -fn field_distance_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result> { +fn field_distance_flex( + node: &Node, + calling_client: Arc, + message: Message, +) -> Result { #[derive(Deserialize)] struct FieldInfoArgs<'a> { reference_space_path: &'a str, point: Vector3, } - let args: FieldInfoArgs = deserialize(data)?; + let args: FieldInfoArgs = deserialize(message.as_ref())?; let reference_space = find_reference_space(&calling_client, args.reference_space_path)?; let distance = node @@ -147,16 +151,20 @@ fn field_distance_flex(node: &Node, calling_client: Arc, data: &[u8]) -> .get() .unwrap() .distance(reference_space.as_ref(), args.point.into()); - Ok(serialize(distance)?) + Ok(serialize(distance)?.into()) } -fn field_normal_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result> { +fn field_normal_flex( + node: &Node, + calling_client: Arc, + message: Message, +) -> Result { #[derive(Deserialize)] struct FieldInfoArgs<'a> { reference_space_path: &'a str, point: Vector3, radius: Option, } - let args: FieldInfoArgs = deserialize(data)?; + let args: FieldInfoArgs = deserialize(message.as_ref())?; let reference_space = find_reference_space(&calling_client, args.reference_space_path)?; let normal = node.field.get().as_ref().unwrap().normal( @@ -164,20 +172,20 @@ fn field_normal_flex(node: &Node, calling_client: Arc, data: &[u8]) -> R args.point.into(), args.radius.unwrap_or(0.001), ); - Ok(serialize(mint::Vector3::from(normal))?) + Ok(serialize(mint::Vector3::from(normal))?.into()) } fn field_closest_point_flex( node: &Node, calling_client: Arc, - data: &[u8], -) -> Result> { + message: Message, +) -> Result { #[derive(Deserialize)] struct FieldInfoArgs<'a> { reference_space_path: &'a str, point: Vector3, radius: Option, } - let args: FieldInfoArgs = deserialize(data)?; + let args: FieldInfoArgs = deserialize(message.as_ref())?; let reference_space = find_reference_space(&calling_client, args.reference_space_path)?; let closest_point = node.field.get().as_ref().unwrap().closest_point( @@ -185,16 +193,20 @@ fn field_closest_point_flex( args.point.into(), args.radius.unwrap_or(0.001), ); - Ok(serialize(mint::Vector3::from(closest_point))?) + Ok(serialize(mint::Vector3::from(closest_point))?.into()) } -fn field_ray_march_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result> { +fn field_ray_march_flex( + node: &Node, + calling_client: Arc, + message: Message, +) -> Result { #[derive(Deserialize)] struct FieldInfoArgs<'a> { reference_space_path: &'a str, ray_origin: Vector3, ray_direction: Vector3, } - let args: FieldInfoArgs = deserialize(data)?; + let args: FieldInfoArgs = deserialize(message.as_ref())?; let reference_space = find_reference_space(&calling_client, args.reference_space_path)?; let ray_march_result = node.field.get().unwrap().ray_march(Ray { @@ -202,7 +214,7 @@ fn field_ray_march_flex(node: &Node, calling_client: Arc, data: &[u8]) - direction: args.ray_direction.into(), space: reference_space, }); - Ok(serialize(ray_march_result)?) + Ok(serialize(ray_march_result)?.into()) } pub enum Field { diff --git a/src/nodes/fields/sphere.rs b/src/nodes/fields/sphere.rs index 9d90dd6..1d5f15c 100644 --- a/src/nodes/fields/sphere.rs +++ b/src/nodes/fields/sphere.rs @@ -1,6 +1,7 @@ use super::{Field, FieldTrait, Node}; use crate::core::client::Client; use crate::nodes::spatial::{find_spatial_parent, Spatial}; +use crate::nodes::Message; use color_eyre::eyre::{ensure, Result}; use glam::{Mat4, Vec3A}; use mint::Vector3; @@ -39,9 +40,13 @@ impl SphereField { self.radius.store(radius, Ordering::Relaxed); } - pub fn set_radius_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + pub fn set_radius_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Field::Sphere(sphere_field) = node.field.get().unwrap().as_ref() else { return Ok(()) }; - sphere_field.set_radius(deserialize(data)?); + sphere_field.set_radius(deserialize(message.as_ref())?); Ok(()) } } @@ -64,7 +69,7 @@ impl FieldTrait for SphereField { pub fn create_sphere_field_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { #[derive(Deserialize)] struct CreateFieldInfo<'a> { @@ -73,7 +78,7 @@ pub fn create_sphere_field_flex( origin: Option>, radius: f32, } - let info: CreateFieldInfo = deserialize(data)?; + let info: CreateFieldInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/field", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = Mat4::from_translation( diff --git a/src/nodes/fields/torus.rs b/src/nodes/fields/torus.rs index d92bc0a..8277706 100644 --- a/src/nodes/fields/torus.rs +++ b/src/nodes/fields/torus.rs @@ -1,6 +1,7 @@ use super::{Field, FieldTrait, Node}; use crate::core::client::Client; use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial}; +use crate::nodes::Message; use color_eyre::eyre::{ensure, Result}; use glam::{swizzles::*, vec2, Vec3A}; use portable_atomic::AtomicF32; @@ -43,9 +44,13 @@ impl TorusField { self.radius_b.store(radius_b.abs(), Ordering::Relaxed); } - pub fn set_size_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + pub fn set_size_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Field::Torus(torus_field) = node.field.get().unwrap().as_ref() else { return Ok(()) }; - let (radius_a, radius_b) = deserialize(data)?; + let (radius_a, radius_b) = deserialize(message.as_ref())?; torus_field.set_size(radius_a, radius_b); Ok(()) @@ -67,7 +72,7 @@ impl FieldTrait for TorusField { pub fn create_torus_field_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { #[derive(Deserialize)] struct CreateFieldInfo<'a> { @@ -77,7 +82,7 @@ pub fn create_torus_field_flex( radius_a: f32, radius_b: f32, } - let info: CreateFieldInfo = deserialize(data)?; + let info: CreateFieldInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/field", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); diff --git a/src/nodes/input/mod.rs b/src/nodes/input/mod.rs index 6bce80d..885dc85 100644 --- a/src/nodes/input/mod.rs +++ b/src/nodes/input/mod.rs @@ -10,7 +10,7 @@ use super::{ alias::{Alias, AliasInfo}, fields::{find_field, Field, FIELD_ALIAS_INFO}, spatial::{find_spatial_parent, parse_transform, Spatial}, - Node, + Message, Node, }; use crate::core::{client::Client, node_collections::LifeLinkedNodeMap}; use crate::core::{node_collections::LifeLinkedNodeList, registry::Registry}; @@ -109,21 +109,21 @@ impl InputMethod { .cloned() } - fn capture_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { + fn capture_flex(node: &Node, calling_client: Arc, message: Message) -> Result<()> { let method = InputMethod::get(node)?; - let handler = InputHandler::find(&calling_client, deserialize(data)?)?; + let handler = InputHandler::find(&calling_client, deserialize(message.as_ref())?)?; method.captures.add_raw(&handler); - node.send_remote_signal("capture", data) + node.send_remote_signal("capture", message) } - fn set_datamap_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + fn set_datamap_flex(node: &Node, _calling_client: Arc, message: Message) -> Result<()> { let method = InputMethod::get(node)?; - method.datamap.lock().replace(Datamap::new(data.to_vec())?); + method.datamap.lock().replace(Datamap::new(message.data)?); Ok(()) } - fn set_handlers_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { + fn set_handlers_flex(node: &Node, calling_client: Arc, message: Message) -> Result<()> { let method = InputMethod::get(node)?; - let handler_paths: Vec<&str> = deserialize(data)?; + let handler_paths: Vec<&str> = deserialize(message.as_ref())?; let handlers: Vec> = handler_paths .into_iter() .filter_map(|p| InputHandler::find(&calling_client, p).ok()) @@ -147,9 +147,9 @@ impl InputMethod { } fn handle_new_handler(&self, handler: &InputHandler) { - let Some(method_node) = self.node.upgrade() else { return }; - let Some(method_client) = method_node.get_client() else { return }; - let Some(handler_node) = handler.node.upgrade() else { return }; + let Some(method_node) = self.node.upgrade() else {return}; + let Some(method_client) = method_node.get_client() else {return}; + let Some(handler_node) = handler.node.upgrade() else {return}; // Receiver itself let Ok(handler_alias) = Alias::create( &method_client, @@ -178,7 +178,7 @@ impl InputMethod { } let Ok(data) = serialize(&handler.uid) else {return}; - let _ = method_node.send_remote_signal("handler_created", &data); + let _ = method_node.send_remote_signal("handler_created", data); } fn handle_drop_handler(&self, handler: &InputHandler) { let uid = handler.uid.as_str(); @@ -186,7 +186,7 @@ impl InputMethod { self.handler_aliases.remove(&(uid.to_string() + "-field")); let Some(tx_node) = self.node.upgrade() else {return}; let Ok(data) = serialize(&uid) else {return}; - let _ = tx_node.send_remote_signal("handler_destroyed", &data); + let _ = tx_node.send_remote_signal("handler_destroyed", data); } } impl Drop for InputMethod { @@ -284,7 +284,7 @@ impl InputHandler { #[instrument(level = "debug", skip(self, distance_link))] fn send_input(&self, order: u32, distance_link: &DistanceLink, datamap: Datamap) { let Some(node) = self.node.upgrade() else {return}; - let _ = node.send_remote_signal("input", &distance_link.serialize(order, datamap)); + let _ = node.send_remote_signal("input", distance_link.serialize(order, datamap)); } } impl PartialEq for InputHandler { @@ -312,7 +312,7 @@ pub fn create_interface(client: &Arc) -> Result<()> { pub fn create_input_handler_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { #[derive(Deserialize)] struct CreateInputHandlerInfo<'a> { @@ -321,7 +321,7 @@ pub fn create_input_handler_flex( transform: Transform, field_path: &'a str, } - let info: CreateInputHandlerInfo = deserialize(data)?; + let info: CreateInputHandlerInfo = deserialize(message.as_ref())?; let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, true); let field = find_field(&calling_client, info.field_path)?; diff --git a/src/nodes/input/pointer.rs b/src/nodes/input/pointer.rs index 1bb7621..65878b5 100644 --- a/src/nodes/input/pointer.rs +++ b/src/nodes/input/pointer.rs @@ -3,7 +3,7 @@ use crate::core::client::Client; use crate::nodes::fields::{Field, Ray, RayMarchResult}; use crate::nodes::input::{InputMethod, InputType}; use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial}; -use crate::nodes::Node; +use crate::nodes::{Message, Node}; use glam::{vec3, Mat4}; use serde::Deserialize; use stardust_xr::schemas::flat::{Datamap, InputDataType, Pointer as FlatPointer}; @@ -63,7 +63,7 @@ impl InputSpecialization for Pointer { pub fn create_pointer_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> color_eyre::eyre::Result<()> { #[derive(Deserialize)] struct CreatePointerInfo<'a> { @@ -72,7 +72,7 @@ pub fn create_pointer_flex( transform: Transform, datamap: Option>, } - let info: CreatePointerInfo = deserialize(data)?; + let info: CreatePointerInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/input/method/pointer", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); diff --git a/src/nodes/input/tip.rs b/src/nodes/input/tip.rs index 021015e..484c61f 100644 --- a/src/nodes/input/tip.rs +++ b/src/nodes/input/tip.rs @@ -3,7 +3,7 @@ use crate::core::client::Client; use crate::nodes::fields::Field; use crate::nodes::input::{InputMethod, InputType}; use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial}; -use crate::nodes::Node; +use crate::nodes::{Message, Node}; use color_eyre::eyre::Result; use glam::{vec3a, Mat4}; use serde::Deserialize; @@ -17,9 +17,9 @@ pub struct Tip { pub radius: f32, } impl Tip { - fn set_radius(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + fn set_radius(node: &Node, _calling_client: Arc, message: Message) -> Result<()> { if let InputType::Tip(tip) = &mut *node.input_method.get().unwrap().specialization.lock() { - tip.radius = deserialize(data)?; + tip.radius = deserialize(message.as_ref())?; } Ok(()) } @@ -45,7 +45,7 @@ impl InputSpecialization for Tip { } } -pub fn create_tip_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn create_tip_flex(_node: &Node, calling_client: Arc, message: Message) -> Result<()> { #[derive(Deserialize)] struct CreateTipInfo<'a> { name: &'a str, @@ -54,7 +54,7 @@ pub fn create_tip_flex(_node: &Node, calling_client: Arc, data: &[u8]) - radius: f32, datamap: Option>, } - let info: CreateTipInfo = deserialize(data)?; + let info: CreateTipInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/input/method/tip", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); diff --git a/src/nodes/items/environment.rs b/src/nodes/items/environment.rs index 311df36..6fa411d 100644 --- a/src/nodes/items/environment.rs +++ b/src/nodes/items/environment.rs @@ -7,7 +7,7 @@ use crate::{ nodes::{ items::TypeInfo, spatial::{find_spatial_parent, parse_transform, Spatial}, - Node, + Message, Node, }, }; use color_eyre::eyre::{eyre, Result}; @@ -15,7 +15,7 @@ use lazy_static::lazy_static; use nanoid::nanoid; use serde::Deserialize; use stardust_xr::{ - schemas::flex::{deserialize, flexbuffers, serialize}, + schemas::flex::{deserialize, serialize}, values::Transform, }; use std::sync::Arc; @@ -46,22 +46,26 @@ impl EnvironmentItem { node.add_local_method("get_path", EnvironmentItem::get_path_flex); } - fn get_path_flex(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result> { + fn get_path_flex( + node: &Node, + _calling_client: Arc, + _message: Message, + ) -> Result { let ItemType::Environment(environment_item) = &node.item.get().unwrap().specialization else { return Err(eyre!("Wrong item type?")) }; - Ok(flexbuffers::singleton(environment_item.path.as_str())) + Ok(serialize(environment_item.path.as_str())?.into()) } - pub fn serialize_start_data(&self, id: &str) -> Result> { - serialize((id, self.path.as_str())).map_err(|e| e.into()) + pub fn serialize_start_data(&self, id: &str) -> Result { + Ok(serialize((id, self.path.as_str()))?.into()) } } pub(super) fn create_environment_item_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { #[derive(Deserialize)] struct CreateEnvironmentItemInfo<'a> { @@ -70,7 +74,7 @@ pub(super) fn create_environment_item_flex( transform: Transform, item_data: String, } - let info: CreateEnvironmentItemInfo = deserialize(data)?; + let info: CreateEnvironmentItemInfo = deserialize(message.as_ref())?; let parent_name = format!("/item/{}/item", ITEM_TYPE_INFO_ENVIRONMENT.type_name); let space = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); diff --git a/src/nodes/items/mod.rs b/src/nodes/items/mod.rs index eeb2cd4..09ae37a 100644 --- a/src/nodes/items/mod.rs +++ b/src/nodes/items/mod.rs @@ -5,7 +5,7 @@ use self::environment::{EnvironmentItem, ITEM_TYPE_INFO_ENVIRONMENT}; use self::panel::{PanelItemTrait, ITEM_TYPE_INFO_PANEL}; use super::fields::Field; use super::spatial::{find_spatial_parent, parse_transform, Spatial}; -use super::{Alias, Node}; +use super::{Alias, Message, Node}; use crate::core::client::Client; use crate::core::node_collections::LifeLinkedNodeMap; use crate::core::registry::Registry; @@ -17,7 +17,7 @@ use nanoid::nanoid; use parking_lot::Mutex; use portable_atomic::Ordering; use serde::Deserialize; -use stardust_xr::schemas::flex::{deserialize, flexbuffers, serialize}; +use stardust_xr::schemas::flex::{deserialize, serialize}; use stardust_xr::values::Transform; use std::hash::Hash; use std::sync::{Arc, Weak}; @@ -153,7 +153,7 @@ impl Item { self.make_alias_named(client, parent, &self.uid) } - fn release_flex(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result<()> { + fn release_flex(node: &Node, _calling_client: Arc, _message: Message) -> Result<()> { let item = node.get_aspect("Item", "item", |n| &n.item)?; release(item, None); @@ -175,7 +175,7 @@ pub enum ItemType { Panel(Arc), } impl ItemType { - fn serialize_start_data(&self, id: &str) -> Result> { + fn serialize_start_data(&self, id: &str) -> Result { match self { ItemType::Environment(e) => e.serialize_start_data(id), ItemType::Panel(p) => p.serialize_start_data(id), @@ -226,47 +226,44 @@ impl ItemUI { Ok(()) } fn send_state(&self, state: &str, name: &str) { + let Ok(serialized_data) = serialize(name) else {return}; let _ = self .node .upgrade() .unwrap() - .send_remote_signal(state, flexbuffers::singleton(name).as_slice()); + .send_remote_signal(state, serialized_data); } fn handle_create_item(&self, item: &Item) { - let Some(node) = self.node.upgrade() else { return }; - let Some(client) = node.get_client() else { return }; + let Some(node) = self.node.upgrade() else {return}; + let Some(client) = node.get_client() else {return}; if let Ok(alias_node) = item.make_alias(&client, &(node.get_path().to_string() + "/item")) { self.item_aliases.add(item.uid.clone(), &alias_node); } let Ok(serialized_data) = item.specialization.serialize_start_data(&item.uid) else {return}; - let _ = node.send_remote_signal("create_item", &serialized_data); + let _ = node.send_remote_signal("create_item", serialized_data); } fn handle_destroy_item(&self, item: &Item) { self.item_aliases.remove(&item.uid); self.send_state("destroy_item", item.uid.as_str()); } fn handle_capture_item(&self, item: &Item, acceptor: &ItemAcceptor) { - let Some(node) = self.node.upgrade() else { return }; + let Some(node) = self.node.upgrade() else {return}; - let _ = node.send_remote_signal( - "capture_item", - &serialize((item.uid.as_str(), acceptor.uid.as_str())).unwrap(), - ); + let Ok(message) = serialize((item.uid.as_str(), acceptor.uid.as_str())) else {return}; + let _ = node.send_remote_signal("capture_item", message); } fn handle_release_item(&self, item: &Item, acceptor: &ItemAcceptor) { - let Some(node) = self.node.upgrade() else { return }; + let Some(node) = self.node.upgrade() else {return}; - let _ = node.send_remote_signal( - "release_item", - &serialize((item.uid.as_str(), acceptor.uid.as_str())).unwrap(), - ); + let Ok(message) = serialize((item.uid.as_str(), acceptor.uid.as_str())) else {return}; + let _ = node.send_remote_signal("release_item", message); } fn handle_create_acceptor(&self, acceptor: &ItemAcceptor) { - let Some(node) = self.node.upgrade() else { return }; - let Some(client) = node.get_client() else { return }; + let Some(node) = self.node.upgrade() else {return}; + let Some(client) = node.get_client() else {return}; let Ok((alias, field_alias)) = acceptor.make_aliases( &client, @@ -275,7 +272,8 @@ impl ItemUI { self.acceptor_aliases.add(acceptor.uid.clone(), &alias); self.acceptor_field_aliases .add(acceptor.uid.clone(), &field_alias); - let _ = node.send_remote_signal("create_acceptor", &serialize(&acceptor.uid).unwrap()); + let Ok(message) = serialize(&acceptor.uid) else {return}; + let _ = node.send_remote_signal("create_acceptor", message); } fn handle_destroy_acceptor(&self, acceptor: &ItemAcceptor) { self.send_state("destroy_acceptor", acceptor.uid.as_str()); @@ -314,13 +312,13 @@ impl ItemAcceptor { let _ = node.item_acceptor.set(acceptor); } - fn capture_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { + fn capture_flex(node: &Node, calling_client: Arc, message: Message) -> Result<()> { if !node.enabled.load(Ordering::Relaxed) { return Ok(()); } let acceptor = node.item_acceptor.get().unwrap(); - let item_path: &str = deserialize(data)?; + let item_path: &str = deserialize(message.as_ref())?; let item_node = calling_client.get_node("Item", item_path)?; let item = item_node.get_aspect("Item", "item", |n| &n.item)?; capture(item, acceptor); @@ -352,8 +350,8 @@ impl ItemAcceptor { Ok((acceptor_alias, acceptor_field_alias)) } fn handle_capture(&self, item: &Arc) { - let Some(node) = self.node.upgrade() else { return }; - let Some(client) = node.get_client() else { return }; + let Some(node) = self.node.upgrade() else {return}; + let Some(client) = node.get_client() else {return}; self.accepted_registry.add_raw(item); if let Ok(alias_node) = item.make_alias(&client, &node.path) { @@ -361,14 +359,15 @@ impl ItemAcceptor { } let Ok(serialized_data) = item.specialization.serialize_start_data(&item.uid) else {return}; - let _ = node.send_remote_signal("capture", &serialized_data); + let _ = node.send_remote_signal("capture", serialized_data); } fn handle_release(&self, item: &Item) { - let Some(node) = self.node.upgrade() else { return }; + let Some(node) = self.node.upgrade() else {return}; self.accepted_registry.remove(item); self.accepted_aliases.remove(&item.uid); - let _ = node.send_remote_signal("release", &serialize(&item.uid).unwrap()); + let Ok(message) = serialize(&item.uid) else {return}; + let _ = node.send_remote_signal("release", message); } } impl Drop for ItemAcceptor { @@ -403,12 +402,16 @@ fn type_info(name: &str) -> Result<&'static TypeInfo> { } } -pub fn register_item_ui_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn register_item_ui_flex( + _node: &Node, + calling_client: Arc, + message: Message, +) -> Result<()> { #[derive(Deserialize)] struct RegisterItemUIInfo<'a> { item_type: &'a str, } - let info: RegisterItemUIInfo = deserialize(data)?; + let info: RegisterItemUIInfo = deserialize(message.as_ref())?; let type_info = type_info(info.item_type)?; let ui = Node::create(&calling_client, "/item", type_info.type_name, true).add_to_scenegraph()?; @@ -416,7 +419,11 @@ pub fn register_item_ui_flex(_node: &Node, calling_client: Arc, data: &[ Ok(()) } -fn create_item_acceptor_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +fn create_item_acceptor_flex( + _node: &Node, + calling_client: Arc, + message: Message, +) -> Result<()> { #[derive(Deserialize)] struct CreateItemAcceptorInfo<'a> { name: &'a str, @@ -425,7 +432,7 @@ fn create_item_acceptor_flex(_node: &Node, calling_client: Arc, data: &[ field_path: &'a str, item_type: &'a str, } - let info: CreateItemAcceptorInfo = deserialize(data)?; + let info: CreateItemAcceptorInfo = deserialize(message.as_ref())?; let space = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); let field = find_field(&calling_client, info.field_path)?; diff --git a/src/nodes/items/panel.rs b/src/nodes/items/panel.rs index ddadfc1..b58fd21 100644 --- a/src/nodes/items/panel.rs +++ b/src/nodes/items/panel.rs @@ -7,7 +7,7 @@ use crate::{ drawable::{model::ModelPart, Drawable}, items::{self, Item, ItemType, TypeInfo}, spatial::Spatial, - Node, + Message, Node, }, }; use color_eyre::eyre::{bail, eyre, Result}; @@ -129,8 +129,8 @@ pub enum RecommendedState { } pub trait Backend: Send + Sync + 'static { - fn serialize_start_data(&self, id: &str) -> Result>; - fn serialize_toplevel(&self) -> Result>; + fn serialize_start_data(&self, id: &str) -> Result; + fn serialize_toplevel(&self) -> Result; fn set_toplevel_capabilities(&self, capabilities: Vec); fn configure_toplevel( &self, @@ -244,7 +244,7 @@ impl PanelItem { fn apply_surface_material_flex( node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) }; @@ -254,7 +254,7 @@ impl PanelItem { model_node_path: &'a str, } - let info: SurfaceMaterialInfo = deserialize(data)?; + let info: SurfaceMaterialInfo = deserialize(message.as_ref())?; let model_node = calling_client .scenegraph @@ -268,26 +268,38 @@ impl PanelItem { Ok(()) } - fn pointer_motion_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + fn pointer_motion_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) }; - let (surface_id, position): (SurfaceID, Vector2) = deserialize(data)?; + let (surface_id, position): (SurfaceID, Vector2) = deserialize(message.as_ref())?; debug!(?surface_id, ?position, "Pointer deactivate"); panel_item.pointer_motion(&surface_id, position); Ok(()) } - fn pointer_button_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + fn pointer_button_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) }; - let (surface_id, button, state): (SurfaceID, u32, u32) = deserialize(data)?; + let (surface_id, button, state): (SurfaceID, u32, u32) = deserialize(message.as_ref())?; debug!(?surface_id, button, state, "Pointer button"); panel_item.pointer_button(&surface_id, button, state == 0); Ok(()) } - fn pointer_scroll_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + fn pointer_scroll_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) }; #[derive(Debug, Deserialize)] @@ -296,7 +308,7 @@ impl PanelItem { axis_continuous: Option>, axis_discrete: Option>, } - let info: PointerScrollInfo = deserialize(data)?; + let info: PointerScrollInfo = deserialize(message.as_ref())?; debug!(?info, "Pointer scroll"); panel_item.pointer_scroll(&info.surface_id, info.axis_continuous, info.axis_discrete); @@ -307,9 +319,9 @@ impl PanelItem { fn keyboard_set_keymap_string_flex( node: &Node, _calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { - let keymap_string: &str = deserialize(data)?; + let keymap_string: &str = deserialize(message.as_ref())?; let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) }; debug!("Keyboard set keymap"); @@ -320,7 +332,7 @@ impl PanelItem { // fn keyboard_set_keymap_names_flex( // node: &Node, // _calling_client: Arc, - // data: &[u8], + // message: Message, // ) -> Result<()> { // #[derive(Debug, Deserialize)] // struct Names<'a> { @@ -330,7 +342,7 @@ impl PanelItem { // variant: &'a str, // options: Option, // } - // let names: Names = deserialize(data)?; + // let names: Names = deserialize(message.as_ref())?; // let context = xkb::Context::new(0); // let keymap = Keymap::new_from_names( // &context, @@ -363,9 +375,13 @@ impl PanelItem { // Ok(()) // } - fn keyboard_key_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + fn keyboard_key_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) }; - let (surface_id, key, state): (SurfaceID, u32, u32) = deserialize(data)?; + let (surface_id, key, state): (SurfaceID, u32, u32) = deserialize(message.as_ref())?; debug!(key, state, "Set keyboard key state"); panel_item.keyboard_key(&surface_id, key, state == 0); @@ -373,15 +389,16 @@ impl PanelItem { Ok(()) } pub fn grab_keyboard(&self, sid: Option) { - let Some(node) = self.node.upgrade() else { return }; + let Some(node) = self.node.upgrade() else {return}; - let _ = node.send_remote_signal("grab_keyboard", &serialize(sid).unwrap()); + let Ok(message) = serialize(sid) else {return}; + let _ = node.send_remote_signal("grab_keyboard", message); } fn configure_toplevel_flex( node: &Node, _calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) }; @@ -391,7 +408,7 @@ impl PanelItem { states: Vec, bounds: Option>, } - let info: ConfigureToplevelInfo = deserialize(data)?; + let info: ConfigureToplevelInfo = deserialize(message.as_ref())?; panel_item.configure_toplevel(info.size, info.states, info.bounds); Ok(()) @@ -400,11 +417,11 @@ impl PanelItem { fn set_toplevel_capabilities_flex( node: &Node, _calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { let Some(panel_item) = panel_item_from_node(node) else { return Ok(()) }; - let capabilities: Vec = deserialize(data)?; + let capabilities: Vec = deserialize(message.as_ref())?; debug!("Set toplevel capabilities"); panel_item.set_toplevel_capabilities(capabilities); @@ -413,17 +430,17 @@ impl PanelItem { pub fn commit_toplevel(&self) { debug!("Commit toplevel"); - let Some(node) = self.node.upgrade() else { return }; + let Some(node) = self.node.upgrade() else {return}; let Ok(data) = self.backend.serialize_toplevel() else {return}; - let _ = node.send_remote_signal("commit_toplevel", &data); + let _ = node.send_remote_signal("commit_toplevel", data); } pub fn recommend_toplevel_state(&self, state: RecommendedState) { - let Some(node) = self.node.upgrade() else { return }; + let Some(node) = self.node.upgrade() else {return}; let data = serialize(state).unwrap(); debug!(?state, "Recommend toplevel state"); - let _ = node.send_remote_signal("recommend_toplevel_state", &data); + let _ = node.send_remote_signal("recommend_toplevel_state", data); } } impl PanelItemTrait for PanelItem { @@ -432,11 +449,11 @@ impl PanelItemTrait for PanelItem { } } impl Backend for PanelItem { - fn serialize_start_data(&self, id: &str) -> Result> { + fn serialize_start_data(&self, id: &str) -> Result { self.backend.serialize_start_data(id) } - fn serialize_toplevel(&self) -> Result> { + fn serialize_toplevel(&self) -> Result { self.backend.serialize_toplevel() } diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index d2a2662..3bd719f 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -22,6 +22,7 @@ use stardust_xr::messenger::MessageSenderHandle; use stardust_xr::scenegraph::ScenegraphError; use stardust_xr::schemas::flex::deserialize; use std::fmt::Debug; +use std::os::fd::OwnedFd; use std::sync::{Arc, Weak}; use std::vec::Vec; use tracing::{debug_span, instrument}; @@ -40,8 +41,26 @@ use self::spatial::zone::Zone; use self::spatial::Spatial; use self::startup::StartupSettings; -pub type Signal = fn(&Node, Arc, &[u8]) -> Result<()>; -pub type Method = fn(&Node, Arc, &[u8]) -> Result>; +pub struct Message { + pub data: Vec, + pub fds: Vec, +} +impl From> for Message { + fn from(data: Vec) -> Self { + Message { + data, + fds: Vec::new(), + } + } +} +impl AsRef<[u8]> for Message { + fn as_ref(&self) -> &[u8] { + &self.data + } +} + +pub type Signal = fn(&Node, Arc, Message) -> Result<()>; +pub type Method = fn(&Node, Arc, Message) -> Result; pub struct Node { pub enabled: Arc, @@ -144,11 +163,20 @@ impl Node { } } - pub fn set_enabled_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { - node.enabled.store(deserialize(data)?, Ordering::Relaxed); + pub fn set_enabled_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { + node.enabled + .store(deserialize(message.as_ref())?, Ordering::Relaxed); Ok(()) } - pub fn destroy_flex(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result<()> { + pub fn destroy_flex( + node: &Node, + _calling_client: Arc, + _message: Message, + ) -> Result<()> { if node.destroyable { node.destroy(); } @@ -180,7 +208,7 @@ impl Node { &self, calling_client: Arc, method: &str, - data: &[u8], + message: Message, ) -> Result<(), ScenegraphError> { if let Some(alias) = self.alias.get() { if !alias.info.server_signals.iter().any(|e| e == &method) { @@ -190,13 +218,13 @@ impl Node { .original .upgrade() .ok_or(ScenegraphError::BrokenAlias)? - .send_local_signal(calling_client, method, data) + .send_local_signal(calling_client, method, message) } else { let signal = self .local_signals .get(method) .ok_or(ScenegraphError::SignalNotFound)?; - signal(self, calling_client, data).map_err(|error| ScenegraphError::SignalError { + signal(self, calling_client, message).map_err(|error| ScenegraphError::SignalError { error: error.to_string(), }) } @@ -205,8 +233,8 @@ impl Node { &self, calling_client: Arc, method: &str, - data: &[u8], - ) -> Result, ScenegraphError> { + message: Message, + ) -> Result { if let Some(alias) = self.alias.get() { if !alias.info.server_methods.iter().any(|e| e == &method) { return Err(ScenegraphError::MethodNotFound); @@ -215,7 +243,14 @@ impl Node { .original .upgrade() .ok_or(ScenegraphError::BrokenAlias)? - .execute_local_method(calling_client, method, data) + .execute_local_method( + calling_client, + method, + Message { + data: message.data.clone(), + fds: Vec::new(), + }, + ) } else { let method = self .local_methods @@ -223,27 +258,36 @@ impl Node { .ok_or(ScenegraphError::MethodNotFound)?; debug_span!("Handle method").in_scope(|| { - method(self, calling_client, data).map_err(|error| ScenegraphError::MethodError { - error: error.to_string(), + method(self, calling_client, message).map_err(|error| { + ScenegraphError::MethodError { + error: error.to_string(), + } }) }) } } #[instrument(level = "debug", skip_all)] - pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> { + pub fn send_remote_signal(&self, method: &str, message: impl Into) -> Result<()> { + let message = message.into(); self.aliases .get_valid_contents() .iter() .filter(|alias| alias.info.client_signals.iter().any(|e| e == &method)) .filter_map(|alias| alias.node.upgrade()) .for_each(|node| { - let _ = node.send_remote_signal(method, data); + // Beware! file descriptors will not be sent to aliases!!! + let _ = node.send_remote_signal( + method, + Message { + data: message.data.clone(), + fds: Vec::new(), + }, + ); }); let path = self.path.clone(); let method = method.to_string(); - let data = data.to_vec(); if let Some(handle) = self.message_sender_handle.as_ref() { - handle.signal(path.as_str(), method.as_str(), data.as_slice())?; + handle.signal(path.as_str(), method.as_str(), &message.data, message.fds)?; } Ok(()) } @@ -252,7 +296,7 @@ impl Node { // &self, // method: &str, // data: Vec, - // ) -> Result>>> { + // ) -> Result>> { // let message_sender_handle = self // .message_sender_handle // .as_ref() diff --git a/src/nodes/root.rs b/src/nodes/root.rs index 021ed3a..2477bb4 100644 --- a/src/nodes/root.rs +++ b/src/nodes/root.rs @@ -1,5 +1,5 @@ use super::spatial::Spatial; -use super::Node; +use super::{Message, Node}; use crate::core::client::Client; use crate::core::registry::Registry; use color_eyre::eyre::Result; @@ -39,7 +39,11 @@ impl Root { })) } - fn subscribe_frame_flex(_node: &Node, calling_client: Arc, _data: &[u8]) -> Result<()> { + fn subscribe_frame_flex( + _node: &Node, + calling_client: Arc, + _message: Message, + ) -> Result<()> { calling_client .root .get() @@ -54,7 +58,7 @@ impl Root { if let Ok(data) = serialize((delta, 0.0)) { for root in ROOT_REGISTRY.get_valid_contents() { if root.send_frame_event.load(Ordering::Relaxed) { - let _ = root.node.send_remote_signal("frame", &data); + let _ = root.node.send_remote_signal("frame", data.clone()); } } } @@ -63,9 +67,9 @@ impl Root { fn set_base_prefixes_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { - *calling_client.base_resource_prefixes.lock() = deserialize(data)?; + *calling_client.base_resource_prefixes.lock() = deserialize(message.as_ref())?; Ok(()) } } diff --git a/src/nodes/spatial/mod.rs b/src/nodes/spatial/mod.rs index 4409464..f49ed87 100644 --- a/src/nodes/spatial/mod.rs +++ b/src/nodes/spatial/mod.rs @@ -1,7 +1,7 @@ pub mod zone; use self::zone::{create_zone_flex, Zone}; -use super::Node; +use super::{Message, Node}; use crate::core::client::Client; use crate::core::registry::Registry; use color_eyre::eyre::{ensure, eyre, Result}; @@ -232,13 +232,13 @@ impl Spatial { pub fn get_bounding_box_flex( node: &Node, calling_client: Arc, - data: &[u8], - ) -> Result> { + message: Message, + ) -> Result { let this_spatial = node .spatial .get() .ok_or_else(|| eyre!("Node doesn't have a spatial?"))?; - let relative_spatial_path: Option<&str> = deserialize(data)?; + let relative_spatial_path: Option<&str> = deserialize(message.as_ref())?; let bounds = if let Some(relative_spatial_path) = relative_spatial_path { let relative_spatial = find_reference_space(&calling_client, relative_spatial_path)?; let center = @@ -260,23 +260,24 @@ impl Spatial { this_spatial.get_bounding_box() }; - serialize(( + Ok(serialize(( mint::Vector3::from(bounds.center), mint::Vector3::from(bounds.dimensions), - )) - .map_err(|e| e.into()) + ))? + .into()) } pub fn get_transform_flex( node: &Node, calling_client: Arc, - data: &[u8], - ) -> Result> { + message: Message, + ) -> Result { let this_spatial = node .spatial .get() .ok_or_else(|| eyre!("Node doesn't have a spatial?"))?; - let relative_spatial = find_reference_space(&calling_client, deserialize(data)?)?; + let relative_spatial = + find_reference_space(&calling_client, deserialize(message.as_ref())?)?; let (scale, rotation, position) = Spatial::space_to_space_matrix( Some(this_spatial.as_ref()), @@ -284,20 +285,24 @@ impl Spatial { ) .to_scale_rotation_translation(); - serialize(( + Ok(serialize(( mint::Vector3::from(position), mint::Quaternion::from(rotation), mint::Vector3::from(scale), - )) - .map_err(|e| e.into()) + ))? + .into()) } - pub fn set_transform_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { + pub fn set_transform_flex( + node: &Node, + calling_client: Arc, + message: Message, + ) -> Result<()> { #[derive(Deserialize)] struct TransformArgs<'a> { reference_space_path: Option<&'a str>, transform: Transform, } - let transform_args: TransformArgs = deserialize(data)?; + let transform_args: TransformArgs = deserialize(message.as_ref())?; let reference_space_transform = transform_args .reference_space_path .map(|path| find_reference_space(&calling_client, path)) @@ -312,25 +317,29 @@ impl Spatial { pub fn set_spatial_parent_flex( node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { - let parent = find_spatial_parent(&calling_client, deserialize(data)?)?; + let parent = find_spatial_parent(&calling_client, deserialize(message.as_ref())?)?; node.spatial.get().unwrap().set_spatial_parent(Some(parent)) } pub fn set_spatial_parent_in_place_flex( node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { - let parent = find_spatial_parent(&calling_client, deserialize(data)?)?; + let parent = find_spatial_parent(&calling_client, deserialize(message.as_ref())?)?; node.spatial .get() .unwrap() .set_spatial_parent_in_place(Some(parent))?; Ok(()) } - pub fn set_zoneable_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { - let zoneable: bool = deserialize(data)?; + pub fn set_zoneable_flex( + node: &Node, + _calling_client: Arc, + message: Message, + ) -> Result<()> { + let zoneable: bool = deserialize(message.as_ref())?; let spatial = node.spatial.get().unwrap(); if zoneable { ZONEABLE_REGISTRY.add_raw(spatial); @@ -344,9 +353,9 @@ impl Spatial { pub fn field_distance_flex( node: &Node, calling_client: Arc, - data: &[u8], - ) -> Result> { - let (point, fields): (Vector3, Vec>) = deserialize(data)?; + message: Message, + ) -> Result { + let (point, fields): (Vector3, Vec>) = deserialize(message.as_ref())?; let spatial = node.spatial.get().unwrap(); let output = fields @@ -362,14 +371,14 @@ impl Spatial { .map(|f| f.map(|f| f.distance(spatial, point.into()))) .collect::>>(); - Ok(serialize(output)?) + Ok(serialize(output)?.into()) } pub fn field_normal_flex( node: &Node, calling_client: Arc, - data: &[u8], - ) -> Result> { - let (point, fields): (Vector3, Vec>) = deserialize(data)?; + message: Message, + ) -> Result { + let (point, fields): (Vector3, Vec>) = deserialize(message.as_ref())?; let spatial = node.spatial.get().unwrap(); let output = fields @@ -385,14 +394,14 @@ impl Spatial { .map(|f| f.map(|f| Vector3::from(f.normal(spatial, point.into(), 0.001)))) .collect::>(); - Ok(serialize(output)?) + Ok(serialize(output)?.into()) } pub fn field_closest_point_flex( node: &Node, calling_client: Arc, - data: &[u8], - ) -> Result> { - let (point, fields): (Vector3, Vec>) = deserialize(data)?; + message: Message, + ) -> Result { + let (point, fields): (Vector3, Vec>) = deserialize(message.as_ref())?; let spatial = node.spatial.get().unwrap(); let output = fields @@ -408,7 +417,7 @@ impl Spatial { .map(|f| f.map(|f| Vector3::from(f.closest_point(spatial, point.into(), 0.001)))) .collect::>(); - Ok(serialize(output)?) + Ok(serialize(output)?.into()) } #[instrument] @@ -490,7 +499,11 @@ pub fn create_interface(client: &Arc) -> Result<()> { node.add_to_scenegraph().map(|_| ()) } -pub fn create_spatial_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn create_spatial_flex( + _node: &Node, + calling_client: Arc, + message: Message, +) -> Result<()> { #[derive(Deserialize)] struct CreateSpatialInfo<'a> { name: &'a str, @@ -498,7 +511,7 @@ pub fn create_spatial_flex(_node: &Node, calling_client: Arc, data: &[u8 transform: Transform, zoneable: bool, } - let info: CreateSpatialInfo = deserialize(data)?; + let info: CreateSpatialInfo = deserialize(message.as_ref())?; let node = Node::create(&calling_client, "/spatial/spatial", info.name, true); let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, true); diff --git a/src/nodes/spatial/zone.rs b/src/nodes/spatial/zone.rs index ea2400a..d236185 100644 --- a/src/nodes/spatial/zone.rs +++ b/src/nodes/spatial/zone.rs @@ -5,7 +5,7 @@ use crate::{ alias::{Alias, AliasInfo}, fields::{find_field, Field}, spatial::{find_spatial_parent, parse_transform}, - Node, + Message, Node, }, }; use color_eyre::eyre::Result; @@ -31,17 +31,19 @@ pub fn capture(spatial: &Arc, zone: &Arc) { *spatial.old_parent.lock() = spatial.get_parent(); *spatial.zone.lock() = Arc::downgrade(zone); zone.captured.add_raw(spatial); - let node = zone.spatial.node.upgrade().unwrap(); - let _ = node.send_remote_signal("capture", &serialize(&spatial.uid).unwrap()); + let Some(node) = zone.spatial.node.upgrade() else {return}; + let Ok(message) = serialize(&spatial.uid) else {return}; + let _ = node.send_remote_signal("capture", message); } } pub fn release(spatial: &Spatial) { let _ = spatial.set_spatial_parent_in_place(spatial.old_parent.lock().take()); let mut spatial_zone = spatial.zone.lock(); if let Some(spatial_zone) = spatial_zone.upgrade() { - let node = spatial_zone.spatial.node.upgrade().unwrap(); + let Some(node) = spatial_zone.spatial.node.upgrade() else {return}; spatial_zone.captured.remove(spatial); - let _ = node.send_remote_signal("release", &serialize(&spatial.uid).unwrap()); + let Ok(message) = serialize(&spatial.uid) else {return}; + let _ = node.send_remote_signal("release", message); } *spatial_zone = Weak::new(); } @@ -66,20 +68,20 @@ impl Zone { let _ = node.zone.set(zone.clone()); zone } - fn capture_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { + fn capture_flex(node: &Node, calling_client: Arc, message: Message) -> Result<()> { let zone = node.zone.get().unwrap(); - let capture_path: &str = deserialize(data)?; + let capture_path: &str = deserialize(message.as_ref())?; let spatial = find_spatial(&calling_client, "Spatial", capture_path)?; capture(&spatial, zone); Ok(()) } - fn release_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { - let capture_path: &str = deserialize(data)?; + fn release_flex(_node: &Node, calling_client: Arc, message: Message) -> Result<()> { + let capture_path: &str = deserialize(message.as_ref())?; let spatial = find_spatial(&calling_client, "Spatial", capture_path)?; release(&spatial); Ok(()) } - fn update(node: &Node, _calling_client: Arc, _data: &[u8]) -> Result<()> { + fn update(node: &Node, _calling_client: Arc, _message: Message) -> Result<()> { let zone = node.zone.get().unwrap(); let Some(field) = zone.field.upgrade() else { return Err(color_eyre::eyre::eyre!("Zone's field has been destroyed")) }; let Some((zone_client, zone_node)) = zone @@ -129,10 +131,10 @@ impl Zone { .collect::>>(); for entered_uid in zoneables.keys().filter(|k| !old_zoneables.contains_key(*k)) { - node.send_remote_signal("enter", &serialize(entered_uid)?)?; + node.send_remote_signal("enter", serialize(entered_uid)?)?; } for left_uid in old_zoneables.keys().filter(|k| !zoneables.contains_key(*k)) { - node.send_remote_signal("leave", &serialize(left_uid)?)?; + node.send_remote_signal("leave", serialize(left_uid)?)?; } *old_zoneables = zoneables; @@ -148,7 +150,7 @@ impl Drop for Zone { } } -pub fn create_zone_flex(_node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { +pub fn create_zone_flex(_node: &Node, calling_client: Arc, message: Message) -> Result<()> { #[derive(Deserialize)] struct CreateZoneInfo<'a> { name: &'a str, @@ -156,7 +158,7 @@ pub fn create_zone_flex(_node: &Node, calling_client: Arc, data: &[u8]) transform: Transform, field_path: &'a str, } - let info: CreateZoneInfo = deserialize(data)?; + let info: CreateZoneInfo = deserialize(message.as_ref())?; let parent = find_spatial_parent(&calling_client, info.parent_path)?; let transform = parse_transform(info.transform, true, true, false); let field = find_field(&calling_client, info.field_path)?; diff --git a/src/nodes/startup.rs b/src/nodes/startup.rs index 9f99f32..6968319 100644 --- a/src/nodes/startup.rs +++ b/src/nodes/startup.rs @@ -5,7 +5,7 @@ use crate::{core::client::Client, wayland::WAYLAND_DISPLAY, STARDUST_INSTANCE}; use super::{ items::{ItemAcceptor, TypeInfo}, spatial::find_spatial, - Node, + Message, Node, }; use color_eyre::eyre::Result; use glam::Mat4; @@ -33,8 +33,12 @@ impl StartupSettings { .set(Mutex::new(StartupSettings::default())); } - fn set_root_flex(node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { - let spatial = find_spatial(&calling_client, "Root spatial", deserialize(data)?)?; + fn set_root_flex(node: &Node, calling_client: Arc, message: Message) -> Result<()> { + let spatial = find_spatial( + &calling_client, + "Root spatial", + deserialize(message.as_ref())?, + )?; node.startup_settings.get().unwrap().lock().transform = spatial.global_transform(); Ok(()) @@ -43,9 +47,10 @@ impl StartupSettings { fn add_automatic_acceptor_flex( node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { - let acceptor_node = calling_client.get_node("Item acceptor", deserialize(data)?)?; + let acceptor_node = + calling_client.get_node("Item acceptor", deserialize(message.as_ref())?)?; let acceptor = acceptor_node.get_aspect("Item acceptor", "item acceptor", |n| &n.item_acceptor)?; let mut startup_settings = node.startup_settings.get().unwrap().lock(); @@ -59,14 +64,14 @@ impl StartupSettings { fn generate_startup_token_flex( node: &Node, _calling_client: Arc, - _data: &[u8], - ) -> Result> { + _message: Message, + ) -> Result { let id = nanoid::nanoid!(); let data = serialize(&id)?; STARTUP_SETTINGS .lock() .insert(id, node.startup_settings.get().unwrap().lock().clone()); - Ok(data) + Ok(data.into()) } } impl Debug for StartupSettings { @@ -98,12 +103,12 @@ pub fn create_interface(client: &Arc) -> Result<()> { pub fn create_startup_settings_flex( _node: &Node, calling_client: Arc, - data: &[u8], + message: Message, ) -> Result<()> { let node = Node::create( &calling_client, "/startup/settings", - deserialize(data)?, + deserialize(message.as_ref())?, true, ) .add_to_scenegraph()?; @@ -130,8 +135,8 @@ macro_rules! var_env_insert { pub fn get_connection_environment_flex( _node: &Node, _calling_client: Arc, - _data: &[u8], -) -> Result> { + _message: Message, +) -> Result { let mut env: FxHashMap = FxHashMap::default(); var_env_insert!(env, STARDUST_INSTANCE); #[cfg(feature = "wayland")] @@ -146,5 +151,5 @@ pub fn get_connection_environment_flex( env.insert("SDL_VIDEODRIVER".to_string(), "wayland".to_string()); } - Ok(serialize(env)?) + Ok(serialize(env)?.into()) } diff --git a/src/wayland/surface.rs b/src/wayland/surface.rs index e568e3b..2ee0b55 100644 --- a/src/wayland/surface.rs +++ b/src/wayland/surface.rs @@ -83,7 +83,7 @@ impl CoreSurface { } pub fn process(&self, sk: &impl StereoKitDraw, renderer: &mut GlesRenderer) { - let Some(wl_surface) = self.wl_surface() else { return }; + let Some(wl_surface) = self.wl_surface() else {return}; let sk_tex = self.sk_tex.get_or_init(|| { SendWrapper::new(sk.tex_create(TextureType::IMAGE_NO_MIPS, TextureFormat::RGBA32)) @@ -166,7 +166,7 @@ impl CoreSurface { } pub fn frame(&self, sk: &impl StereoKitDraw, output: Output) { - let Some(wl_surface) = self.wl_surface() else { return }; + let Some(wl_surface) = self.wl_surface() else {return}; send_frames_surface_tree( &wl_surface, diff --git a/src/wayland/xdg_shell.rs b/src/wayland/xdg_shell.rs index eb8b794..682bcb0 100644 --- a/src/wayland/xdg_shell.rs +++ b/src/wayland/xdg_shell.rs @@ -7,7 +7,7 @@ use super::{ use crate::nodes::{ drawable::model::ModelPart, items::panel::{Backend, PanelItem, RecommendedState, SurfaceID}, - Node, + Message, Node, }; use color_eyre::eyre::{eyre, Result}; use mint::Vector2; @@ -521,7 +521,7 @@ impl Dispatch, WaylandState> for WaylandState { } xdg_toplevel::Request::Move { seat, serial } => { debug!(?xdg_toplevel, ?seat, serial, "XDG Toplevel move request"); - let Some(panel_item) = data.lock().panel_item() else { return }; + let Some(panel_item) = data.lock().panel_item() else {return}; panel_item.recommend_toplevel_state(RecommendedState::Move); } xdg_toplevel::Request::Resize { @@ -529,7 +529,7 @@ impl Dispatch, WaylandState> for WaylandState { serial, edges, } => { - let WEnum::Value(edges) = edges else { return }; + let WEnum::Value(edges) = edges else {return}; debug!( ?xdg_toplevel, ?seat, @@ -537,7 +537,7 @@ impl Dispatch, WaylandState> for WaylandState { ?edges, "XDG Toplevel resize request" ); - let Some(panel_item) = data.lock().panel_item() else { return }; + let Some(panel_item) = data.lock().panel_item() else {return}; panel_item.recommend_toplevel_state(RecommendedState::Resize(edges as u32)); } xdg_toplevel::Request::SetMaxSize { width, height } => { @@ -551,28 +551,28 @@ impl Dispatch, WaylandState> for WaylandState { .then_some(Vector2::from([width as u32, height as u32])); } xdg_toplevel::Request::SetMaximized => { - let Some(panel_item) = data.lock().panel_item() else { return }; + let Some(panel_item) = data.lock().panel_item() else {return}; panel_item.recommend_toplevel_state(RecommendedState::Maximize(true)); } xdg_toplevel::Request::UnsetMaximized => { - let Some(panel_item) = data.lock().panel_item() else { return }; + let Some(panel_item) = data.lock().panel_item() else {return}; panel_item.recommend_toplevel_state(RecommendedState::Maximize(false)); } xdg_toplevel::Request::SetFullscreen { output: _ } => { - let Some(panel_item) = data.lock().panel_item() else { return }; + let Some(panel_item) = data.lock().panel_item() else {return}; panel_item.recommend_toplevel_state(RecommendedState::Fullscreen(true)); } xdg_toplevel::Request::UnsetFullscreen => { - let Some(panel_item) = data.lock().panel_item() else { return }; + let Some(panel_item) = data.lock().panel_item() else {return}; panel_item.recommend_toplevel_state(RecommendedState::Fullscreen(true)); } xdg_toplevel::Request::SetMinimized => { - let Some(panel_item) = data.lock().panel_item() else { return }; + let Some(panel_item) = data.lock().panel_item() else {return}; panel_item.recommend_toplevel_state(RecommendedState::Minimize); } xdg_toplevel::Request::Destroy => { debug!(?xdg_toplevel, "Destroy XDG Toplevel"); - let Some(panel_item) = data.lock().panel_item() else { return }; + let Some(panel_item) = data.lock().panel_item() else {return}; panel_item.backend.on_drop(); } _ => unreachable!(), @@ -779,15 +779,16 @@ impl XDGBackend { self.popups.lock().insert(uid.clone(), popup.downgrade()); - let Some(node) = panel_item.node() else { return }; - let _ = node.send_remote_signal("new_popup", &serialize(&(&uid, data)).unwrap()); + let Some(node) = panel_item.node() else {return}; + let Ok(message) = serialize(&(&uid, data)) else {return}; + let _ = node.send_remote_signal("new_popup", message); } pub fn reposition_popup(&self, panel_item: &PanelItem, popup_state: &PopupData) { - let Some(node) = panel_item.node() else { return }; + let Some(node) = panel_item.node() else {return}; let _ = node.send_remote_signal( "reposition_popup", - &serialize(popup_state.positioner_data().unwrap()).unwrap(), + serialize(popup_state.positioner_data().unwrap()).unwrap(), ); } pub fn drop_popup(&self, panel_item: &PanelItem, uid: &str) { @@ -802,8 +803,9 @@ impl XDGBackend { self.seat.drop_surface(&wl_surface); } - let Some(node) = panel_item.node() else { return }; - let _ = node.send_remote_signal("drop_popup", &serialize(uid).unwrap()); + let Some(node) = panel_item.node() else {return}; + let Ok(message) = serialize(uid) else {return}; + let _ = node.send_remote_signal("drop_popup", message); } fn popups_data(&self) -> Vec { @@ -829,7 +831,7 @@ impl XDGBackend { } } impl Backend for XDGBackend { - fn serialize_start_data(&self, id: &str) -> Result> { + fn serialize_start_data(&self, id: &str) -> Result { let toplevel_state = self .toplevel() .map(|t| ToplevelData::get(&t).lock().clone()); @@ -837,7 +839,7 @@ impl Backend for XDGBackend { let pointer_grab = self.pointer_grab.lock().clone(); let keyboard_grab = self.keyboard_grab.lock().clone(); - serialize(( + Ok(serialize(( id, ( self.cursor.borrow().as_ref().and_then(|c| c.cursor_data()), @@ -846,16 +848,16 @@ impl Backend for XDGBackend { pointer_grab, keyboard_grab, ), - )) - .map_err(|e| e.into()) + ))? + .into()) } - fn serialize_toplevel(&self) -> Result> { + fn serialize_toplevel(&self) -> Result { let toplevel = self .toplevel() .ok_or_else(|| eyre!("Toplevel does not exist"))?; let state = ToplevelData::get(&toplevel); let data = serialize(&state.lock().clone())?; - Ok(data) + Ok(data.into()) } fn set_toplevel_capabilities(&self, capabilities: Vec) { diff --git a/src/wayland/xwayland.rs b/src/wayland/xwayland.rs index f60b196..e4d6350 100644 --- a/src/wayland/xwayland.rs +++ b/src/wayland/xwayland.rs @@ -7,6 +7,7 @@ use crate::{ nodes::{ drawable::model::ModelPart, items::panel::{Backend, PanelItem, RecommendedState, SurfaceID}, + Message, }, wayland::surface::CoreSurface, }; @@ -279,7 +280,7 @@ impl X11Backend { // } } impl Backend for X11Backend { - fn serialize_start_data(&self, id: &str) -> Result> { + fn serialize_start_data(&self, id: &str) -> Result { let size = ( self.toplevel.geometry().size.w as u32, self.toplevel.geometry().size.h as u32, @@ -304,9 +305,9 @@ impl Backend for X11Backend { None::, None::, ); - serialize((id, info)).map_err(|e| e.into()) + Ok(serialize((id, info))?.into()) } - fn serialize_toplevel(&self) -> Result> { + fn serialize_toplevel(&self) -> Result { let toplevel_state = ( None::, self.toplevel.title(), @@ -319,7 +320,7 @@ impl Backend for X11Backend { self.toplevel.max_size().map(|s| (s.w, s.w)), ); let data = serialize(&toplevel_state)?; - Ok(data) + Ok(data.into()) } fn set_toplevel_capabilities(&self, _capabilities: Vec) {}