modular protostar
This commit is contained in:
100
src/application.rs
Normal file
100
src/application.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use crate::xdg::{DesktopFile, Icon, IconType};
|
||||
use nix::unistd::setsid;
|
||||
use regex::Regex;
|
||||
use stardust_xr_fusion::{
|
||||
client::Client,
|
||||
node::{NodeError, NodeType},
|
||||
spatial::Spatial,
|
||||
startup_settings::StartupSettings,
|
||||
};
|
||||
use std::{
|
||||
os::unix::process::CommandExt,
|
||||
process::{Command, Stdio},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Application {
|
||||
desktop_file: DesktopFile,
|
||||
startup_settings: Arc<StartupSettings>,
|
||||
}
|
||||
impl Application {
|
||||
pub fn create(client: &Arc<Client>, desktop_file: DesktopFile) -> Result<Self, NodeError> {
|
||||
if desktop_file.no_display {
|
||||
return Err(NodeError::DoesNotExist);
|
||||
}
|
||||
|
||||
let startup_settings = Arc::new(StartupSettings::create(client)?);
|
||||
Ok(Application {
|
||||
desktop_file,
|
||||
startup_settings,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<&str> {
|
||||
self.desktop_file.name.as_deref()
|
||||
}
|
||||
pub fn categories(&self) -> &[String] {
|
||||
self.desktop_file.categories.as_slice()
|
||||
}
|
||||
|
||||
pub fn icon(&self, preferred_px_size: u16, prefer_3d: bool) -> Option<Icon> {
|
||||
let raw_icons = self.desktop_file.get_raw_icons(preferred_px_size);
|
||||
let mut icon = raw_icons.iter().max_by_key(|i| i.size).cloned();
|
||||
if prefer_3d {
|
||||
icon = raw_icons
|
||||
.into_iter()
|
||||
.find(|i| match i.icon_type {
|
||||
IconType::Gltf => true,
|
||||
_ => false,
|
||||
})
|
||||
.or(icon);
|
||||
}
|
||||
|
||||
icon.and_then(|i| i.cached_process(preferred_px_size).ok())
|
||||
}
|
||||
|
||||
pub fn launch(&self, launch_space: &Spatial) -> Result<(), NodeError> {
|
||||
self.startup_settings.set_root(launch_space)?;
|
||||
let future_startup_token = self.startup_settings.generate_startup_token()?;
|
||||
let future_connection_env = self
|
||||
.startup_settings
|
||||
.node()
|
||||
.client()?
|
||||
.get_connection_environment()?;
|
||||
|
||||
let executable = self
|
||||
.desktop_file
|
||||
.command
|
||||
.clone()
|
||||
.ok_or(NodeError::DoesNotExist)?;
|
||||
tokio::task::spawn(async move {
|
||||
let Ok(startup_token) = future_startup_token.await else {return};
|
||||
let Ok(connection_env) = future_connection_env.await else {return};
|
||||
|
||||
for (k, v) in connection_env.into_iter() {
|
||||
std::env::set_var(k, v);
|
||||
}
|
||||
|
||||
std::env::set_var("STARDUST_STARTUP_TOKEN", startup_token);
|
||||
let re = Regex::new(r"%[fFuUdDnNickvm]").unwrap();
|
||||
let exec = re.replace_all(&executable, "");
|
||||
unsafe {
|
||||
Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(exec.to_string())
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.pre_exec(|| {
|
||||
_ = setsid();
|
||||
Ok(())
|
||||
})
|
||||
.spawn()
|
||||
.expect("Failed to start child process");
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod application;
|
||||
pub mod protostar;
|
||||
pub mod xdg;
|
||||
|
||||
38
src/main.rs
38
src/main.rs
@@ -1,8 +1,5 @@
|
||||
use clap::Parser;
|
||||
use color_eyre::{
|
||||
eyre::{bail, Result},
|
||||
Report,
|
||||
};
|
||||
use color_eyre::{eyre::Result, Report};
|
||||
use manifest_dir_macros::directory_relative_path;
|
||||
use protostar::{protostar::ProtoStar, xdg::parse_desktop_file};
|
||||
use stardust_xr_fusion::client::Client;
|
||||
@@ -11,21 +8,8 @@ use std::path::PathBuf;
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[clap(short, long, default_value_t = 0.1)]
|
||||
size: f32,
|
||||
#[clap(
|
||||
short,
|
||||
long,
|
||||
conflicts_with = "command",
|
||||
required_unless_present = "command",
|
||||
conflicts_with = "icon",
|
||||
required_unless_present = "icon"
|
||||
)]
|
||||
desktop_file: Option<PathBuf>,
|
||||
#[clap(short, long, conflicts_with = "desktop_file", requires = "command")]
|
||||
icon: Option<PathBuf>,
|
||||
#[clap(short, long, conflicts_with = "desktop_file", requires = "icon")]
|
||||
command: Option<String>,
|
||||
// #[clap(short, long)]
|
||||
desktop_file: PathBuf,
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
@@ -35,17 +19,11 @@ async fn main() -> Result<()> {
|
||||
let (client, event_loop) = Client::connect_with_async_loop().await?;
|
||||
client.set_base_prefixes(&[directory_relative_path!("res")]);
|
||||
|
||||
let protostar = if let Some(desktop_file) = args.desktop_file {
|
||||
ProtoStar::create_from_desktop_file(
|
||||
client.get_root(),
|
||||
[0.0, 0.0, 0.0],
|
||||
parse_desktop_file(desktop_file).map_err(|e| Report::msg(e))?,
|
||||
)?
|
||||
} else if let Some(command) = args.command {
|
||||
ProtoStar::new_raw(client.get_root(), [0.0, 0.0, 0.0], None, None, command)?
|
||||
} else {
|
||||
bail!("No command or desktop file, nothing to launch.");
|
||||
};
|
||||
let protostar = ProtoStar::create_from_desktop_file(
|
||||
client.get_root(),
|
||||
[0.0, 0.0, 0.0],
|
||||
parse_desktop_file(args.desktop_file).map_err(|e| Report::msg(e))?,
|
||||
)?;
|
||||
|
||||
let _root = client.wrap_root(protostar);
|
||||
|
||||
|
||||
113
src/protostar.rs
113
src/protostar.rs
@@ -1,9 +1,10 @@
|
||||
use crate::xdg::{DesktopFile, Icon, IconType};
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use crate::{
|
||||
application::Application,
|
||||
xdg::{DesktopFile, Icon, IconType},
|
||||
};
|
||||
use color_eyre::eyre::Result;
|
||||
use glam::Quat;
|
||||
use mint::Vector3;
|
||||
use nix::unistd::setsid;
|
||||
use regex::Regex;
|
||||
use stardust_xr_fusion::{
|
||||
client::{FrameInfo, RootHandler},
|
||||
core::values::Transform,
|
||||
@@ -11,12 +12,9 @@ use stardust_xr_fusion::{
|
||||
fields::BoxField,
|
||||
node::NodeType,
|
||||
spatial::Spatial,
|
||||
startup_settings::StartupSettings,
|
||||
};
|
||||
use stardust_xr_molecules::{GrabData, Grabbable};
|
||||
use std::f32::consts::PI;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::process::{Command, Stdio};
|
||||
use tween::{QuartInOut, Tweener};
|
||||
|
||||
const MODEL_SCALE: f32 = 0.03;
|
||||
@@ -35,13 +33,10 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
|
||||
t,
|
||||
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
|
||||
)?;
|
||||
model.set_material_parameter(
|
||||
1,
|
||||
"color",
|
||||
MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]),
|
||||
)?;
|
||||
model.set_material_parameter(
|
||||
0,
|
||||
model
|
||||
.model_part("Hex")?
|
||||
.set_material_parameter("color", MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]))?;
|
||||
model.model_part("Icon")?.set_material_parameter(
|
||||
"diffuse",
|
||||
MaterialParameter::Texture(ResourceID::Direct(icon.path.clone())),
|
||||
)?;
|
||||
@@ -57,16 +52,16 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
|
||||
}
|
||||
|
||||
pub struct ProtoStar {
|
||||
application: Application,
|
||||
parent: Spatial,
|
||||
position: Vector3<f32>,
|
||||
grabbable: Grabbable,
|
||||
field: BoxField,
|
||||
_field: BoxField,
|
||||
icon: Model,
|
||||
label: Option<Text>,
|
||||
grabbable_shrink: Option<Tweener<f32, f64, QuartInOut>>,
|
||||
grabbable_grow: Option<Tweener<f32, f64, QuartInOut>>,
|
||||
grabbable_move: Option<Tweener<f32, f64, QuartInOut>>,
|
||||
execute_command: String,
|
||||
currently_shown: bool,
|
||||
}
|
||||
impl ProtoStar {
|
||||
@@ -74,45 +69,11 @@ impl ProtoStar {
|
||||
parent: &Spatial,
|
||||
position: impl Into<Vector3<f32>>,
|
||||
desktop_file: DesktopFile,
|
||||
) -> Result<Self> {
|
||||
// dbg!(&desktop_file);
|
||||
let raw_icons = desktop_file.get_raw_icons();
|
||||
let mut icon = raw_icons
|
||||
.clone()
|
||||
.into_iter()
|
||||
.find(|i| match i.icon_type {
|
||||
IconType::Gltf => true,
|
||||
_ => false,
|
||||
})
|
||||
.or(raw_icons.into_iter().max_by_key(|i| i.size));
|
||||
|
||||
match icon {
|
||||
Some(i) => {
|
||||
icon = match i.cached_process(128) {
|
||||
Ok(i) => Some(i),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
Self::new_raw(
|
||||
parent,
|
||||
position,
|
||||
desktop_file.name.as_deref(),
|
||||
icon,
|
||||
desktop_file.command.ok_or_else(|| eyre!("No command"))?,
|
||||
)
|
||||
}
|
||||
pub fn new_raw(
|
||||
parent: &Spatial,
|
||||
position: impl Into<Vector3<f32>>,
|
||||
name: Option<&str>,
|
||||
icon: Option<Icon>,
|
||||
execute_command: String,
|
||||
) -> Result<Self> {
|
||||
let position = position.into();
|
||||
let field = BoxField::create(parent, Transform::default(), [MODEL_SCALE * 2.0; 3])?;
|
||||
let application = Application::create(&parent.client()?, desktop_file)?;
|
||||
let icon = application.icon(128, false);
|
||||
let grabbable = Grabbable::create(
|
||||
parent,
|
||||
Transform::from_position(position),
|
||||
@@ -147,7 +108,7 @@ impl ProtoStar {
|
||||
text_align: Alignment::Center.into(),
|
||||
..Default::default()
|
||||
};
|
||||
let label = name.and_then(|name| {
|
||||
let label = application.name().and_then(|name| {
|
||||
Text::create(
|
||||
&icon,
|
||||
Transform::from_position_rotation(
|
||||
@@ -163,14 +124,14 @@ impl ProtoStar {
|
||||
parent: parent.alias(),
|
||||
position,
|
||||
grabbable,
|
||||
field,
|
||||
_field: field,
|
||||
label,
|
||||
application,
|
||||
icon,
|
||||
grabbable_shrink: None,
|
||||
grabbable_grow: None,
|
||||
execute_command,
|
||||
currently_shown: true,
|
||||
grabbable_move: None,
|
||||
currently_shown: true,
|
||||
})
|
||||
}
|
||||
pub fn content_parent(&self) -> &Spatial {
|
||||
@@ -261,20 +222,15 @@ impl RootHandler for ProtoStar {
|
||||
.unwrap();
|
||||
self.grabbable_grow = None;
|
||||
}
|
||||
} else if self.grabbable.grab_action().actor_stopped() {
|
||||
let startup_settings = StartupSettings::create(&self.field.client().unwrap()).unwrap();
|
||||
startup_settings
|
||||
.set_root(self.grabbable.content_parent())
|
||||
.unwrap();
|
||||
} else if self.grabbable.valid() && self.grabbable.grab_action().actor_stopped() {
|
||||
self.grabbable_shrink = Some(Tweener::quart_in_out(MODEL_SCALE, 0.0001, 0.25));
|
||||
let distance_future = self
|
||||
.grabbable
|
||||
let Ok(distance_future) = self.grabbable
|
||||
.content_parent()
|
||||
.get_position_rotation_scale(&self.parent)
|
||||
.unwrap();
|
||||
else {return};
|
||||
|
||||
let executable = self.execute_command.clone();
|
||||
let client = self.content_parent().client().unwrap();
|
||||
let application = self.application.clone();
|
||||
let space = self.content_parent().alias();
|
||||
|
||||
//TODO: split the executable string for the args
|
||||
tokio::task::spawn(async move {
|
||||
@@ -283,30 +239,7 @@ impl RootHandler for ProtoStar {
|
||||
+ distance_vector.z.powi(2))
|
||||
.sqrt();
|
||||
if dbg!(distance) > ACTIVATION_DISTANCE {
|
||||
let future = startup_settings.generate_startup_token().unwrap();
|
||||
|
||||
let env = client.get_connection_environment().unwrap().await.unwrap();
|
||||
for (k, v) in env.into_iter() {
|
||||
std::env::set_var(k, v);
|
||||
}
|
||||
|
||||
std::env::set_var("STARDUST_STARTUP_TOKEN", future.await.unwrap());
|
||||
let re = Regex::new(r"%[fFuUdDnNickvm]").unwrap();
|
||||
let exec = re.replace_all(&executable, "");
|
||||
unsafe {
|
||||
Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(exec.to_string())
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.pre_exec(|| {
|
||||
_ = setsid();
|
||||
Ok(())
|
||||
})
|
||||
.spawn()
|
||||
.expect("Failed to start child process");
|
||||
}
|
||||
let _ = application.launch(&space);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -183,19 +183,19 @@ pub struct DesktopFile {
|
||||
pub no_display: bool,
|
||||
}
|
||||
impl DesktopFile {
|
||||
pub fn get_raw_icons(&self) -> Vec<Icon> {
|
||||
pub fn get_raw_icons(&self, preferred_px_size: u16) -> Vec<Icon> {
|
||||
// Get the name of the icon from the DesktopFile struct
|
||||
let Some(icon_name) = self.icon.as_ref() else { return Vec::new(); };
|
||||
let test_icon_path = self.path.join(Path::new(icon_name));
|
||||
if test_icon_path.exists() {
|
||||
if let Some(icon) = Icon::from_path(test_icon_path, 128) {
|
||||
if let Some(icon) = Icon::from_path(test_icon_path, preferred_px_size) {
|
||||
return vec![icon];
|
||||
}
|
||||
}
|
||||
|
||||
let cache_icon_path = get_image_cache_dir().join(icon_name).canonicalize();
|
||||
if cache_icon_path.is_ok() {
|
||||
if let Some(icon) = Icon::from_path(cache_icon_path.unwrap(), 128) {
|
||||
if let Some(icon) = Icon::from_path(cache_icon_path.unwrap(), preferred_px_size) {
|
||||
return vec![icon];
|
||||
}
|
||||
}
|
||||
@@ -272,7 +272,7 @@ fn test_get_icon_path() {
|
||||
};
|
||||
|
||||
// Call the get_icon_path() function with a size argument and store the result
|
||||
let icon_paths = desktop_file.get_raw_icons();
|
||||
let icon_paths = desktop_file.get_raw_icons(128);
|
||||
dbg!(&icon_paths);
|
||||
|
||||
// Assert that the get_icon_path() function returns the expected result
|
||||
|
||||
Reference in New Issue
Block a user