feat(startup settings): use /proc/{pid}/environ
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
use super::{eventloop::EventLoop, scenegraph::Scenegraph};
|
||||
use crate::{
|
||||
core::registry::Registry,
|
||||
nodes::{data, drawable, fields, hmd, input, items, root::Root, spatial, startup, Node},
|
||||
nodes::{
|
||||
data, drawable, fields, hmd, input, items,
|
||||
root::Root,
|
||||
spatial,
|
||||
startup::{self, StartupSettings, DESKTOP_STARTUP_IDS},
|
||||
Node,
|
||||
},
|
||||
};
|
||||
use color_eyre::{
|
||||
eyre::{eyre, Result},
|
||||
@@ -10,9 +16,11 @@ use color_eyre::{
|
||||
use lazy_static::lazy_static;
|
||||
use once_cell::sync::OnceCell;
|
||||
use parking_lot::Mutex;
|
||||
use rustc_hash::FxHashMap;
|
||||
use stardust_xr::messenger::{self, MessageSenderHandle};
|
||||
use std::{
|
||||
fs,
|
||||
iter::FromIterator,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
@@ -25,6 +33,7 @@ lazy_static! {
|
||||
event_loop: Weak::new(),
|
||||
index: 0,
|
||||
pid: None,
|
||||
env: None,
|
||||
exe: None,
|
||||
|
||||
stop_notifier: Default::default(),
|
||||
@@ -37,10 +46,26 @@ lazy_static! {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn get_env(pid: i32) -> Result<FxHashMap<String, String>, std::io::Error> {
|
||||
let env = fs::read_to_string(format!("/proc/{pid}/environ"))?;
|
||||
Ok(FxHashMap::from_iter(
|
||||
env.split('\0')
|
||||
.filter_map(|var| var.split_once('='))
|
||||
.map(|(k, v)| (k.to_string(), v.to_string())),
|
||||
))
|
||||
}
|
||||
pub fn startup_settings(env: &FxHashMap<String, String>) -> Option<StartupSettings> {
|
||||
DESKTOP_STARTUP_IDS
|
||||
.lock()
|
||||
.get(env.get("STARDUST_STARTUP_TOKEN")?)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
event_loop: Weak<EventLoop>,
|
||||
index: usize,
|
||||
pid: Option<i32>,
|
||||
env: Option<FxHashMap<String, String>>,
|
||||
exe: Option<PathBuf>,
|
||||
stop_notifier: Arc<Notify>,
|
||||
join_handle: OnceCell<JoinHandle<Result<()>>>,
|
||||
@@ -57,6 +82,7 @@ impl Client {
|
||||
connection: UnixStream,
|
||||
) -> Arc<Self> {
|
||||
let pid = connection.peer_cred().ok().and_then(|c| c.pid());
|
||||
let env = pid.and_then(|pid| get_env(pid).ok());
|
||||
let exe = pid.and_then(|pid| fs::read_link(format!("/proc/{}/exe", pid)).ok());
|
||||
info!(
|
||||
index = index,
|
||||
@@ -74,6 +100,7 @@ impl Client {
|
||||
event_loop: Arc::downgrade(event_loop),
|
||||
index,
|
||||
pid,
|
||||
env,
|
||||
exe,
|
||||
stop_notifier: Default::default(),
|
||||
join_handle: OnceCell::new(),
|
||||
@@ -94,6 +121,15 @@ impl Client {
|
||||
input::create_interface(&client);
|
||||
startup::create_interface(&client);
|
||||
|
||||
if let Some(startup_settings) = client.env.as_ref().and_then(|env| startup_settings(env)) {
|
||||
client
|
||||
.root
|
||||
.get()
|
||||
.unwrap()
|
||||
.spatial()
|
||||
.set_local_transform(startup_settings.transform);
|
||||
}
|
||||
|
||||
let _ = client.join_handle.set(tokio::spawn({
|
||||
let client = client.clone();
|
||||
async move {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::spatial::Spatial;
|
||||
use super::startup::DESKTOP_STARTUP_IDS;
|
||||
use super::Node;
|
||||
use crate::core::client::Client;
|
||||
use crate::core::registry::Registry;
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use color_eyre::eyre::Result;
|
||||
use glam::Mat4;
|
||||
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||
|
||||
@@ -19,7 +18,6 @@ pub struct Root {
|
||||
impl Root {
|
||||
pub fn create(client: &Arc<Client>) -> Arc<Self> {
|
||||
let node = Node::create(client, "", "", false);
|
||||
node.add_local_signal("apply_desktop_startup_id", Root::apply_desktop_startup_id);
|
||||
node.add_local_signal("subscribe_logic_step", Root::subscribe_logic_step);
|
||||
node.add_local_signal("set_base_prefixes", Root::set_base_prefixes);
|
||||
let node = node.add_to_scenegraph();
|
||||
@@ -31,21 +29,8 @@ impl Root {
|
||||
})
|
||||
}
|
||||
|
||||
fn apply_desktop_startup_id(
|
||||
node: &Node,
|
||||
_calling_client: Arc<Client>,
|
||||
data: &[u8],
|
||||
) -> Result<()> {
|
||||
let startup_settings = DESKTOP_STARTUP_IDS
|
||||
.lock()
|
||||
.remove(flexbuffers::Reader::get_root(data)?.get_str()?)
|
||||
.ok_or_else(|| eyre!("Desktop startup ID not found in the list!"))?;
|
||||
node.spatial
|
||||
.get()
|
||||
.unwrap()
|
||||
.set_local_transform(startup_settings.transform);
|
||||
|
||||
Ok(())
|
||||
pub fn spatial(&self) -> &Arc<Spatial> {
|
||||
self.node.spatial.get().unwrap()
|
||||
}
|
||||
|
||||
fn subscribe_logic_step(_node: &Node, calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use crate::core::client::Client;
|
||||
|
||||
use super::Node;
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use super::{
|
||||
spatial::find_spatial,
|
||||
Node,
|
||||
};
|
||||
use color_eyre::eyre::Result;
|
||||
use glam::Mat4;
|
||||
use parking_lot::Mutex;
|
||||
use rustc_hash::FxHashMap;
|
||||
@@ -23,15 +26,7 @@ impl StartupSettings {
|
||||
}
|
||||
|
||||
fn set_root_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
||||
let startup_id = flexbuffers::Reader::get_root(data)?.get_str()?;
|
||||
let spatial_node = calling_client
|
||||
.scenegraph
|
||||
.get_node(startup_id)
|
||||
.ok_or_else(|| eyre!("Root spatial node does not exist"))?;
|
||||
let spatial = spatial_node
|
||||
.spatial
|
||||
.get()
|
||||
.ok_or_else(|| eyre!("Root spatial node is not a spatial"))?;
|
||||
let spatial = find_spatial(&calling_client, "Root spatial", deserialize(data)?)?;
|
||||
node.startup_settings.get().unwrap().lock().transform = spatial.global_transform();
|
||||
|
||||
Ok(())
|
||||
@@ -43,7 +38,7 @@ impl StartupSettings {
|
||||
_data: &[u8],
|
||||
) -> Result<Vec<u8>> {
|
||||
let id = nanoid::nanoid!();
|
||||
let data = flexbuffers::singleton(id.as_str());
|
||||
let data = serialize(&id)?;
|
||||
DESKTOP_STARTUP_IDS
|
||||
.lock()
|
||||
.insert(id, node.startup_settings.get().unwrap().lock().clone());
|
||||
|
||||
@@ -4,7 +4,7 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
core::{
|
||||
client::{Client, INTERNAL_CLIENT},
|
||||
client::{get_env, startup_settings, Client, INTERNAL_CLIENT},
|
||||
registry::Registry,
|
||||
},
|
||||
nodes::{
|
||||
@@ -68,7 +68,7 @@ impl PanelItem {
|
||||
&nanoid!(),
|
||||
true,
|
||||
));
|
||||
Spatial::add_to(&node, None, Mat4::IDENTITY, false).unwrap();
|
||||
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false).unwrap();
|
||||
|
||||
let specialization = ItemType::Panel(PanelItem {
|
||||
node: Arc::downgrade(&node),
|
||||
@@ -103,6 +103,13 @@ impl PanelItem {
|
||||
node.add_local_signal("keyboard_deactivate", PanelItem::keyboard_deactivate_flex);
|
||||
node.add_local_signal("keyboard_key_state", PanelItem::keyboard_key_state_flex);
|
||||
node.add_local_signal("resize", PanelItem::resize_flex);
|
||||
if let Some(startup_settings) = core_surface
|
||||
.pid()
|
||||
.and_then(|pid| get_env(pid).ok())
|
||||
.and_then(|env| startup_settings(&env))
|
||||
{
|
||||
spatial.set_local_transform(startup_settings.transform);
|
||||
}
|
||||
node
|
||||
}
|
||||
|
||||
|
||||
@@ -213,6 +213,18 @@ impl CoreSurface {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn pid(&self) -> Option<i32> {
|
||||
Some(
|
||||
self.weak_surface
|
||||
.upgrade()
|
||||
.ok()?
|
||||
.client()?
|
||||
.get_credentials(&self.dh)
|
||||
.ok()?
|
||||
.pid,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn wayland_state(&self) -> Arc<Mutex<WaylandState>> {
|
||||
self.state.upgrade().unwrap()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user