From 63ead46a2c2e7fb7593cbdc90e7dae0526f9b4cf Mon Sep 17 00:00:00 2001 From: Nova Date: Mon, 24 Apr 2023 09:53:20 -0400 Subject: [PATCH] feat(startup): get environment --- src/main.rs | 4 ++++ src/nodes/startup.rs | 36 +++++++++++++++++++++++++++++++----- src/wayland/mod.rs | 5 +++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 835731f..a36aaf9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use self::core::eventloop::EventLoop; use clap::Parser; use color_eyre::eyre::Result; use directories::ProjectDirs; +use once_cell::sync::OnceCell; use stardust_xr::server; use std::path::PathBuf; use std::process::Command; @@ -52,6 +53,8 @@ struct CliArgs { startup_script: Option, } +static STARDUST_INSTANCE: OnceCell = OnceCell::new(); + struct EventLoopInfo { tokio_handle: Handle, socket_path: PathBuf, @@ -259,6 +262,7 @@ async fn event_loop( ) -> color_eyre::eyre::Result<()> { let socket_path = server::get_free_socket_path().expect("Unable to find a free stardust socket path"); + STARDUST_INSTANCE.set(socket_path.file_name().unwrap().to_string_lossy().into_owned()).expect("Someone hasn't done their job, yell at Nova because how is this set multiple times what the hell"); let _event_loop = EventLoop::new(socket_path.clone()).expect("Couldn't create server socket"); info!("Init event loop"); info!( diff --git a/src/nodes/startup.rs b/src/nodes/startup.rs index ed9bf63..aa661e9 100644 --- a/src/nodes/startup.rs +++ b/src/nodes/startup.rs @@ -1,4 +1,4 @@ -use crate::core::client::Client; +use crate::{core::client::Client, wayland::WAYLAND_DISPLAY, STARDUST_INSTANCE}; use super::{ items::{ItemAcceptor, TypeInfo}, @@ -9,7 +9,7 @@ use color_eyre::eyre::Result; use glam::Mat4; use parking_lot::Mutex; use rustc_hash::FxHashMap; -use stardust_xr::schemas::flex::{deserialize, flexbuffers, serialize}; +use stardust_xr::schemas::flex::{deserialize, serialize}; use std::{ fmt::Debug, sync::{Arc, Weak}, @@ -86,6 +86,10 @@ impl Debug for StartupSettings { pub fn create_interface(client: &Arc) -> Result<()> { let node = Node::create(client, "", "startup", false); node.add_local_signal("create_startup_settings", create_startup_settings_flex); + node.add_local_method( + "get_connection_environment", + get_connection_environment_flex, + ); node.add_to_scenegraph().map(|_| ()) } @@ -94,9 +98,13 @@ pub fn create_startup_settings_flex( calling_client: Arc, data: &[u8], ) -> Result<()> { - let name = flexbuffers::Reader::get_root(data)?.get_str()?; - let node = - Node::create(&calling_client, "/startup/settings", name, true).add_to_scenegraph()?; + let node = Node::create( + &calling_client, + "/startup/settings", + deserialize(data)?, + true, + ) + .add_to_scenegraph()?; StartupSettings::add_to(&node); node.add_local_signal("set_root", StartupSettings::set_root_flex); @@ -111,3 +119,21 @@ pub fn create_startup_settings_flex( Ok(()) } + +macro_rules! env_insert { + ($env:ident, $name:ident) => { + $env.insert(stringify!($name).to_string(), $name.get().unwrap().clone()); + }; +} +pub fn get_connection_environment_flex( + _node: &Node, + _calling_client: Arc, + _data: &[u8], +) -> Result> { + let mut env: FxHashMap = FxHashMap::default(); + env_insert!(env, STARDUST_INSTANCE); + #[cfg(feature = "wayland")] + env_insert!(env, WAYLAND_DISPLAY); + + Ok(serialize(env)?) +} diff --git a/src/wayland/mod.rs b/src/wayland/mod.rs index 5060be5..09a5f07 100644 --- a/src/wayland/mod.rs +++ b/src/wayland/mod.rs @@ -31,6 +31,8 @@ use tokio::{ }; use tracing::{debug, debug_span, info, instrument}; +pub static WAYLAND_DISPLAY: OnceCell = OnceCell::new(); + pub static SERIAL_COUNTER: CounterU32 = CounterU32::new(0); struct EGLRawHandles { @@ -85,6 +87,9 @@ impl Wayland { let socket = ListeningSocket::bind_auto("wayland", 0..33)?; let socket_name = socket.socket_name().unwrap().to_str().unwrap().to_string(); + WAYLAND_DISPLAY + .set(socket_name.clone()) + .expect("seriously message nova this time they screwed up big time"); info!(socket_name, "Wayland active"); let join_handle =