feat(startup settings): use /proc/{pid}/environ
This commit is contained in:
@@ -1,7 +1,13 @@
|
|||||||
use super::{eventloop::EventLoop, scenegraph::Scenegraph};
|
use super::{eventloop::EventLoop, scenegraph::Scenegraph};
|
||||||
use crate::{
|
use crate::{
|
||||||
core::registry::Registry,
|
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::{
|
use color_eyre::{
|
||||||
eyre::{eyre, Result},
|
eyre::{eyre, Result},
|
||||||
@@ -10,9 +16,11 @@ use color_eyre::{
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
use rustc_hash::FxHashMap;
|
||||||
use stardust_xr::messenger::{self, MessageSenderHandle};
|
use stardust_xr::messenger::{self, MessageSenderHandle};
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
|
iter::FromIterator,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{Arc, Weak},
|
sync::{Arc, Weak},
|
||||||
};
|
};
|
||||||
@@ -25,6 +33,7 @@ lazy_static! {
|
|||||||
event_loop: Weak::new(),
|
event_loop: Weak::new(),
|
||||||
index: 0,
|
index: 0,
|
||||||
pid: None,
|
pid: None,
|
||||||
|
env: None,
|
||||||
exe: None,
|
exe: None,
|
||||||
|
|
||||||
stop_notifier: Default::default(),
|
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 {
|
pub struct Client {
|
||||||
event_loop: Weak<EventLoop>,
|
event_loop: Weak<EventLoop>,
|
||||||
index: usize,
|
index: usize,
|
||||||
pid: Option<i32>,
|
pid: Option<i32>,
|
||||||
|
env: Option<FxHashMap<String, String>>,
|
||||||
exe: Option<PathBuf>,
|
exe: Option<PathBuf>,
|
||||||
stop_notifier: Arc<Notify>,
|
stop_notifier: Arc<Notify>,
|
||||||
join_handle: OnceCell<JoinHandle<Result<()>>>,
|
join_handle: OnceCell<JoinHandle<Result<()>>>,
|
||||||
@@ -57,6 +82,7 @@ impl Client {
|
|||||||
connection: UnixStream,
|
connection: UnixStream,
|
||||||
) -> Arc<Self> {
|
) -> Arc<Self> {
|
||||||
let pid = connection.peer_cred().ok().and_then(|c| c.pid());
|
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());
|
let exe = pid.and_then(|pid| fs::read_link(format!("/proc/{}/exe", pid)).ok());
|
||||||
info!(
|
info!(
|
||||||
index = index,
|
index = index,
|
||||||
@@ -74,6 +100,7 @@ impl Client {
|
|||||||
event_loop: Arc::downgrade(event_loop),
|
event_loop: Arc::downgrade(event_loop),
|
||||||
index,
|
index,
|
||||||
pid,
|
pid,
|
||||||
|
env,
|
||||||
exe,
|
exe,
|
||||||
stop_notifier: Default::default(),
|
stop_notifier: Default::default(),
|
||||||
join_handle: OnceCell::new(),
|
join_handle: OnceCell::new(),
|
||||||
@@ -94,6 +121,15 @@ impl Client {
|
|||||||
input::create_interface(&client);
|
input::create_interface(&client);
|
||||||
startup::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.join_handle.set(tokio::spawn({
|
||||||
let client = client.clone();
|
let client = client.clone();
|
||||||
async move {
|
async move {
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
use super::spatial::Spatial;
|
use super::spatial::Spatial;
|
||||||
use super::startup::DESKTOP_STARTUP_IDS;
|
|
||||||
use super::Node;
|
use super::Node;
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use color_eyre::eyre::{eyre, Result};
|
use color_eyre::eyre::Result;
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use stardust_xr::schemas::flex::{deserialize, serialize};
|
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||||
|
|
||||||
@@ -19,7 +18,6 @@ pub struct Root {
|
|||||||
impl Root {
|
impl Root {
|
||||||
pub fn create(client: &Arc<Client>) -> Arc<Self> {
|
pub fn create(client: &Arc<Client>) -> Arc<Self> {
|
||||||
let node = Node::create(client, "", "", false);
|
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("subscribe_logic_step", Root::subscribe_logic_step);
|
||||||
node.add_local_signal("set_base_prefixes", Root::set_base_prefixes);
|
node.add_local_signal("set_base_prefixes", Root::set_base_prefixes);
|
||||||
let node = node.add_to_scenegraph();
|
let node = node.add_to_scenegraph();
|
||||||
@@ -31,21 +29,8 @@ impl Root {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_desktop_startup_id(
|
pub fn spatial(&self) -> &Arc<Spatial> {
|
||||||
node: &Node,
|
self.node.spatial.get().unwrap()
|
||||||
_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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscribe_logic_step(_node: &Node, calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
|
fn subscribe_logic_step(_node: &Node, calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
|
|
||||||
use super::Node;
|
use super::{
|
||||||
use color_eyre::eyre::{eyre, Result};
|
spatial::find_spatial,
|
||||||
|
Node,
|
||||||
|
};
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
@@ -23,15 +26,7 @@ impl StartupSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set_root_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
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 = find_spatial(&calling_client, "Root spatial", deserialize(data)?)?;
|
||||||
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"))?;
|
|
||||||
node.startup_settings.get().unwrap().lock().transform = spatial.global_transform();
|
node.startup_settings.get().unwrap().lock().transform = spatial.global_transform();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -43,7 +38,7 @@ impl StartupSettings {
|
|||||||
_data: &[u8],
|
_data: &[u8],
|
||||||
) -> Result<Vec<u8>> {
|
) -> Result<Vec<u8>> {
|
||||||
let id = nanoid::nanoid!();
|
let id = nanoid::nanoid!();
|
||||||
let data = flexbuffers::singleton(id.as_str());
|
let data = serialize(&id)?;
|
||||||
DESKTOP_STARTUP_IDS
|
DESKTOP_STARTUP_IDS
|
||||||
.lock()
|
.lock()
|
||||||
.insert(id, node.startup_settings.get().unwrap().lock().clone());
|
.insert(id, node.startup_settings.get().unwrap().lock().clone());
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use super::{
|
|||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{
|
core::{
|
||||||
client::{Client, INTERNAL_CLIENT},
|
client::{get_env, startup_settings, Client, INTERNAL_CLIENT},
|
||||||
registry::Registry,
|
registry::Registry,
|
||||||
},
|
},
|
||||||
nodes::{
|
nodes::{
|
||||||
@@ -68,7 +68,7 @@ impl PanelItem {
|
|||||||
&nanoid!(),
|
&nanoid!(),
|
||||||
true,
|
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 {
|
let specialization = ItemType::Panel(PanelItem {
|
||||||
node: Arc::downgrade(&node),
|
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_deactivate", PanelItem::keyboard_deactivate_flex);
|
||||||
node.add_local_signal("keyboard_key_state", PanelItem::keyboard_key_state_flex);
|
node.add_local_signal("keyboard_key_state", PanelItem::keyboard_key_state_flex);
|
||||||
node.add_local_signal("resize", PanelItem::resize_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
|
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>> {
|
pub fn wayland_state(&self) -> Arc<Mutex<WaylandState>> {
|
||||||
self.state.upgrade().unwrap()
|
self.state.upgrade().unwrap()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user