daemon/root: Allow DsInhibit to be toggled at runtime

This commit is contained in:
Vicki Pfau 2024-06-11 17:48:36 -07:00
parent cae5a69e6e
commit 037d418553
2 changed files with 111 additions and 17 deletions

View file

@ -8,13 +8,16 @@
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot;
use tokio_util::sync::CancellationToken;
use tracing::error;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, Registry};
use zbus::connection::Connection;
use zbus::ConnectionBuilder;
use crate::daemon::{channel, Daemon, DaemonContext};
use crate::daemon::{channel, Daemon, DaemonCommand, DaemonContext};
use crate::ds_inhibit::Inhibitor;
use crate::manager::root::SteamOSManager;
use crate::path;
@ -34,12 +37,61 @@ pub(crate) struct RootState {
}
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
pub(crate) struct RootServicesState {}
pub(crate) struct RootServicesState {
pub ds_inhibit: DsInhibit,
}
#[derive(Debug)]
pub(crate) enum RootCommand {}
pub(crate) enum RootCommand {
SetDsInhibit(bool),
GetDsInhibit(oneshot::Sender<bool>),
}
struct RootContext {}
#[derive(Copy, Clone, Deserialize, Serialize, Debug)]
pub(crate) struct DsInhibit {
pub enabled: bool,
}
impl Default for DsInhibit {
fn default() -> DsInhibit {
DsInhibit { enabled: true }
}
}
pub(crate) struct RootContext {
state: RootState,
channel: Sender<Command>,
ds_inhibit: Option<CancellationToken>,
}
impl RootContext {
pub(crate) fn new(channel: Sender<Command>) -> RootContext {
RootContext {
state: RootState::default(),
channel,
ds_inhibit: None,
}
}
async fn reload_ds_inhibit(&mut self, daemon: &mut Daemon<RootContext>) -> Result<()> {
match (
self.state.services.ds_inhibit.enabled,
self.ds_inhibit.as_ref(),
) {
(false, Some(handle)) => {
handle.cancel();
self.ds_inhibit = None;
}
(true, None) => {
let inhibitor = Inhibitor::init().await?;
self.ds_inhibit = Some(daemon.add_service(inhibitor));
}
_ => (),
}
Ok(())
}
}
impl DaemonContext for RootContext {
type State = RootState;
@ -79,19 +131,31 @@ impl DaemonContext for RootContext {
async fn handle_command(
&mut self,
_cmd: RootCommand,
_daemon: &mut Daemon<RootContext>,
cmd: RootCommand,
daemon: &mut Daemon<RootContext>,
) -> Result<()> {
match cmd {
RootCommand::SetDsInhibit(enable) => {
self.state.services.ds_inhibit.enabled = enable;
self.reload_ds_inhibit(daemon).await?;
self.channel.send(DaemonCommand::WriteState).await?;
}
RootCommand::GetDsInhibit(sender) => {
let _ = sender.send(self.ds_inhibit.is_some());
}
}
Ok(())
}
}
async fn create_connection() -> Result<Connection> {
pub(crate) type Command = DaemonCommand<RootCommand>;
async fn create_connection(channel: Sender<Command>) -> Result<Connection> {
let connection = ConnectionBuilder::system()?
.name("com.steampowered.SteamOSManager1")?
.build()
.await?;
let manager = SteamOSManager::new(connection.clone()).await?;
let manager = SteamOSManager::new(connection.clone(), channel).await?;
connection
.object_server()
.at("/com/steampowered/SteamOSManager1", manager)
@ -105,9 +169,9 @@ pub async fn daemon() -> Result<()> {
let stdout_log = fmt::layer();
let subscriber = Registry::default().with(stdout_log);
let (_tx, rx) = channel::<RootContext>();
let (tx, rx) = channel::<RootContext>();
let connection = match create_connection().await {
let connection = match create_connection(tx.clone()).await {
Ok(c) => c,
Err(e) => {
let _guard = tracing::subscriber::set_default(subscriber);
@ -116,14 +180,11 @@ pub async fn daemon() -> Result<()> {
}
};
let context = RootContext {};
let context = RootContext::new(tx);
let mut daemon = Daemon::new(subscriber, connection.clone(), rx).await?;
let ftrace = Ftrace::init(connection.clone()).await?;
let ftrace = Ftrace::init(connection).await?;
daemon.add_service(ftrace);
let inhibitor = Inhibitor::init().await?;
daemon.add_service(inhibitor);
daemon.run(context).await
}