Something went wrong. Try again.
Some shell. slowshell.bushyice.com
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521use std::{ cell::RefCell, collections::HashMap, os::fd::{AsRawFd, OwnedFd}, sync::{ Arc, Mutex, OnceLock, atomic::{AtomicBool, Ordering}, }, time::{Duration, Instant},};
use slowshell_config::Config;use slowshell_core::{ ActionDispatcher, Store, listeners::{FdHandle, ListenerAction}, types::{PayloadBuilderRegistry, Ustr},};use slowshell_plugin_abi::{Context, RString, ServiceContext, ServiceFactoryBox, ServiceObjectBox};
use crate::state::{PluginScope, ScopedConfig, record_plugin_error, store_scope_mut};
pub(crate) const BUILTINS: &[&str] = &[ "network", "audio", "bluetooth", "power", "system", "tray", "notifications",];
pub(crate) fn event_name(name: &str) -> Ustr { format!("service/{name}").into()}
struct Entry { plugin_id: String, factory: Arc<ServiceFactoryBox>, autostart: bool,}
fn entries() -> &'static Mutex<HashMap<String, Entry>> { static ENTRIES: OnceLock<Mutex<HashMap<String, Entry>>> = OnceLock::new(); ENTRIES.get_or_init(|| Mutex::new(HashMap::new()))}
struct Running { stop: Arc<AtomicBool>, tx: std::sync::mpsc::Sender<String>, exited: Arc<AtomicBool>,}
fn running() -> &'static Mutex<HashMap<String, Running>> { static RUNNING: OnceLock<Mutex<HashMap<String, Running>>> = OnceLock::new(); RUNNING.get_or_init(|| Mutex::new(HashMap::new()))}
struct ThreadState { stop: Arc<AtomicBool>, wake: Option<OwnedFd>, rx: std::sync::mpsc::Receiver<String>, dispatcher: Option<ActionDispatcher>, #[allow(dead_code)] builders: Option<Arc<PayloadBuilderRegistry>>,}
thread_local! { static CURRENT: RefCell<Option<ThreadState>> = const { RefCell::new(None) };}
pub(crate) fn register(plugin_id: &str, name: String, factory: ServiceFactoryBox, autostart: bool) { if name.is_empty() { record_plugin_error(plugin_id, 2, "service name must not be empty"); return; } if BUILTINS.contains(&name.as_str()) { record_plugin_error( plugin_id, 2, &format!("service name `{name}` is reserved by a built-in service"), ); return; }
let mut map = entries().lock().unwrap(); if map.contains_key(&name) { record_plugin_error( plugin_id, 2, &format!("service `{name}` is already registered"), ); return; }
map.insert( name.clone(), Entry { plugin_id: plugin_id.to_owned(), factory: Arc::new(factory), autostart, }, ); drop(map);
crate::state::contribute(plugin_id, |contributions| { contributions.services.push(name); });}
pub fn start(name: &str) -> bool { if start_builtin(name) { return true; }
if is_running(name) { return true; }
if !await_exit(name, EXIT_GRACE) { return false; }
let (plugin_id, factory) = { let map = entries().lock().unwrap(); match map.get(name) { Some(entry) => (entry.plugin_id.clone(), entry.factory.clone()), None => return false, } };
let stop = Arc::new(AtomicBool::new(false)); let exited = Arc::new(AtomicBool::new(false)); let (tx, rx) = std::sync::mpsc::channel::<String>();
let (wake, dispatcher, builders) = { let store = crate::state::current_store(); let dispatcher = store .and_then(|store| store.borrow::<ActionDispatcher>()) .map(|dispatcher| ActionDispatcher(dispatcher.0.clone())); let builders = store .and_then(|store| store.borrow::<Arc<PayloadBuilderRegistry>>()) .cloned(); (watch_wake(name, store), dispatcher, builders) };
let mut inner: ServiceObjectBox = { let _scope = PluginScope::enter(&plugin_id); let ctx = Context::new(crate::host::host()); factory.create(&ctx) };
running().lock().unwrap().insert( name.to_owned(), Running { stop: stop.clone(), tx, exited: exited.clone(), }, );
let thread_name = name.to_owned(); let thread_stop = stop.clone(); let spawn = std::thread::Builder::new() .name(format!("slowshell-service-{name}")) .spawn(move || { let _scope = PluginScope::enter(&plugin_id); CURRENT.with(|cell| { *cell.borrow_mut() = Some(ThreadState { stop: thread_stop.clone(), wake, rx, dispatcher, builders, }) });
let ctx = ServiceContext::new(crate::host::host()); inner.run(&ctx);
CURRENT.with(|cell| { *cell.borrow_mut() = None; });
let mut map = running().lock().unwrap(); if map .get(&thread_name) .is_some_and(|entry| Arc::ptr_eq(&entry.stop, &thread_stop)) { map.remove(&thread_name); } drop(map); exited.store(true, Ordering::SeqCst); });
if spawn.is_err() { let mut map = running().lock().unwrap(); if map .get(name) .is_some_and(|entry| Arc::ptr_eq(&entry.stop, &stop)) { map.remove(name); } return false; }
true}
const EXIT_GRACE: Duration = Duration::from_millis(500);
fn await_exit(name: &str, timeout: Duration) -> bool { let deadline = Instant::now() + timeout; loop { let exited = { let map = running().lock().unwrap(); match map.get(name) { Some(entry) => entry.exited.clone(), None => return true, } };
if exited.load(Ordering::SeqCst) { return true; }
let now = Instant::now(); if now >= deadline { return false; } std::thread::sleep((deadline - now).min(Duration::from_millis(2))); }}
fn watch_wake(name: &str, store: Option<&Store>) -> Option<OwnedFd> { use nix::{ fcntl::{FcntlArg, OFlag, fcntl}, sys::epoll::EpollFlags, };
let handle = store?.borrow::<FdHandle>()?; let (read_fd, write_fd) = nix::unistd::pipe().ok()?; fcntl(&read_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK)).ok()?;
let raw = read_fd.as_raw_fd(); handle.watch_with_flags( read_fd, EpollFlags::EPOLLIN | EpollFlags::EPOLLET, ListenerAction::Signal { name: event_name(name), fd: raw, }, );
Some(write_fd)}
pub(crate) fn is_running(name: &str) -> bool { if running().lock().unwrap().contains_key(name) { return true; } builtin_running(name)}
pub(crate) fn stop(name: &str) -> bool { let map = running().lock().unwrap(); match map.get(name) { Some(entry) => { entry.stop.store(true, Ordering::SeqCst); true } None => false, }}
pub(crate) fn send(name: &str, message: &str) -> bool { let map = running().lock().unwrap(); match map.get(name) { Some(entry) => entry.tx.send(message.to_owned()).is_ok(), None => false, }}
pub(crate) fn notify_current() -> bool { CURRENT.with(|cell| { let cell = cell.borrow(); match cell.as_ref().and_then(|state| state.wake.as_ref()) { Some(wake) => { slowshell_services::util::notify_signal(wake); true } None => false, } })}
pub(crate) fn sleep(millis: u64) -> bool { CURRENT.with(|cell| { let cell = cell.borrow(); let Some(state) = cell.as_ref() else { return false; };
let deadline = Instant::now() + Duration::from_millis(millis); loop { if state.stop.load(Ordering::Relaxed) { return false; } let now = Instant::now(); if now >= deadline { return true; } std::thread::sleep((deadline - now).min(Duration::from_millis(50))); } })}
pub(crate) fn stopping() -> bool { CURRENT.with(|cell| { cell .borrow() .as_ref() .map(|state| state.stop.load(Ordering::Relaxed)) .unwrap_or(true) })}
pub(crate) fn recv(timeout_millis: u32) -> Option<String> { CURRENT.with(|cell| { let mut cell = cell.borrow_mut(); let state = cell.as_mut()?; state .rx .recv_timeout(Duration::from_millis(timeout_millis as u64)) .ok() })}
pub(crate) fn current_dispatcher() -> Option<ActionDispatcher> { CURRENT.with(|cell| { cell .borrow() .as_ref() .and_then(|state| state.dispatcher.clone()) })}
#[allow(dead_code)]pub(crate) fn current_builders() -> Option<Arc<PayloadBuilderRegistry>> { CURRENT.with(|cell| { cell .borrow() .as_ref() .and_then(|state| state.builders.clone()) })}
pub(crate) fn start_autostart(config: &Config, store: &mut Store) { let _config_scope = ScopedConfig::current(config); let _store_scope = store_scope_mut(store);
let names: Vec<String> = entries() .lock() .unwrap() .iter() .filter(|(_, entry)| entry.autostart) .map(|(name, _)| name.clone()) .collect();
for name in names { start(&name); }}
fn start_builtin(name: &str) -> bool { let Some(store) = crate::state::current_store_mut() else { return false; }; let store = unsafe { &mut *store };
match name { "system" => { slowshell_services::system::ensure(store, Duration::from_secs(5)); } #[cfg(feature = "service-audio")] "audio" => { slowshell_services::audio::ensure(store); } #[cfg(feature = "service-bluetooth")] "bluetooth" => { slowshell_services::bluetooth::ensure(store); } #[cfg(feature = "service-network")] "network" => { slowshell_services::network::ensure(store); } #[cfg(feature = "service-power")] "power" => { slowshell_services::power::ensure(store); } #[cfg(feature = "service-tray")] "tray" => { slowshell_services::tray::ensure(store); } #[cfg(feature = "service-notifications")] "notifications" => { slowshell_services::notifications::ensure(store); } _ => return false, }
builtin_running(name)}
fn builtin_running(name: &str) -> bool { match name { "system" => slowshell_services::system::running(), #[cfg(feature = "service-audio")] "audio" => slowshell_services::audio::running(), #[cfg(feature = "service-bluetooth")] "bluetooth" => slowshell_services::bluetooth::running(), #[cfg(feature = "service-network")] "network" => slowshell_services::network::running(), #[cfg(feature = "service-power")] "power" => slowshell_services::power::running(), #[cfg(feature = "service-tray")] "tray" => slowshell_services::tray::running(), #[cfg(feature = "service-notifications")] "notifications" => slowshell_services::notifications::running(), _ => false, }}
pub struct PluginNotchServiceStarter(pub RString, pub RString);
impl slowshell_commons::notch::NotchServiceStarter for PluginNotchServiceStarter { fn start(&mut self, _store: &mut Store) { crate::service::start(&self.1); }
fn stop(&self, _store: &Store) { slowshell_commons::notch::shared().remove_status(&self.0); crate::service::stop(&self.1); }}
#[cfg(test)]mod tests { use super::*; use slowshell_plugin_abi::{Service, service_factory};
struct Svc;
impl Service for Svc { fn new(_ctx: &Context<'_>) -> Self { Svc }
fn run(&mut self, ctx: &ServiceContext<'_>) { while ctx.sleep(1) {} } }
#[test] fn rejects_reserved_names() { register( "test-plugin", "network".to_owned(), service_factory::<Svc>(), false, ); assert!(!entries().lock().unwrap().contains_key("network")); }
#[test] fn rejects_duplicate_names() { register( "test-plugin", "test/dup".to_owned(), service_factory::<Svc>(), true, ); register( "other-plugin", "test/dup".to_owned(), service_factory::<Svc>(), false, );
let map = entries().lock().unwrap(); assert!(map.contains_key("test/dup")); assert!(map.get("test/dup").unwrap().autostart); }
#[test] fn starts_messages_and_stops() { register( "test-plugin", "test/lifecycle".to_owned(), service_factory::<Svc>(), false, );
assert!(start("test/lifecycle")); assert!(is_running("test/lifecycle")); assert!(send("test/lifecycle", "hello")); assert!(stop("test/lifecycle"));
let deadline = Instant::now() + Duration::from_secs(5); while is_running("test/lifecycle") && Instant::now() < deadline { std::thread::sleep(Duration::from_millis(5)); } assert!(!is_running("test/lifecycle")); }
#[test] fn unknown_names_do_not_start() { assert!(!start("test/missing")); assert!(!is_running("test/missing")); }}