From db2d700ea40f198a0f32ae186204ed49ebfe2db2 Mon Sep 17 00:00:00 2001 From: BL-CZY Date: Sat, 31 Aug 2024 17:04:39 +0200 Subject: [PATCH] open and close the volume --- src/cli/args.rs | 6 ++++++ src/daemon/daemon.rs | 3 ++- src/daemon/renderer/app.rs | 31 +++++++++++++++++++---------- src/daemon/renderer/popup.rs | 38 +++++++++++++++++++++++++++++------- src/daemon/server.rs | 2 +- src/daemon/structs.rs | 19 ++++++++++++------ src/main.rs | 18 ++++++++++++----- 7 files changed, 87 insertions(+), 30 deletions(-) diff --git a/src/cli/args.rs b/src/cli/args.rs index 3befe67..c3c20cf 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -41,4 +41,10 @@ pub enum VolCmd { Inc { value: u32 }, #[clap(about = "Decrease the volume")] Dec { value: u32 }, + #[clap(about = "Close the scale")] + Close, + #[clap( + about = "Show the scale. If there is a number given, close the scale after the given number seconds" + )] + Open { time: Option }, } diff --git a/src/daemon/daemon.rs b/src/daemon/daemon.rs index 87956d3..3c51af9 100644 --- a/src/daemon/daemon.rs +++ b/src/daemon/daemon.rs @@ -25,6 +25,7 @@ pub fn start_daemon(path: Option) -> Result<(), DaemonErr> { // run the server in a different thread let alt_path = path.clone(); + let evt_sender_clone = evt_sender.clone(); std::thread::Builder::new() .name("dvvidget server".into()) .spawn(move || { @@ -40,7 +41,7 @@ pub fn start_daemon(path: Option) -> Result<(), DaemonErr> { let _g = handle.enter(); - start_app(evt_receiver, path); + start_app(evt_receiver, evt_sender_clone.clone(), path); Ok(()) } diff --git a/src/daemon/renderer/app.rs b/src/daemon/renderer/app.rs index 45d067b..e25f9b3 100644 --- a/src/daemon/renderer/app.rs +++ b/src/daemon/renderer/app.rs @@ -38,13 +38,17 @@ lazy_static! { pub static ref IDS: Mutex> = Mutex::new(HashMap::new()); } -fn process_evt(evt: DaemonCmd, app: Arc) -> Result { +fn process_evt( + evt: DaemonCmd, + app: Arc, + sender: UnboundedSender, +) -> Result { match evt { DaemonCmd::ShutDown => { app.quit(); } - DaemonCmd::GetVol | DaemonCmd::SetVol(_) | DaemonCmd::IncVol(_) | DaemonCmd::DecVol(_) => { + DaemonCmd::Vol(evt) => { let guard = match IDS.lock() { Ok(g) => g, Err(poisoned) => poisoned.into_inner(), @@ -54,23 +58,27 @@ fn process_evt(evt: DaemonCmd, app: Arc) -> Result {} } Ok(DaemonRes::Success) } -fn send_res(sender: UnboundedSender, res: DaemonRes) { - if let Err(e) = sender.send(res) { +fn send_res(sender: Option>, res: DaemonRes) { + if sender.is_none() { + return; + } + + if let Err(e) = sender.unwrap().send(res) { println!("Err sending daemon response to the server: {:?}", e); } } pub fn init_gtk_async( mut evt_receiver: UnboundedReceiver, + evt_sender: UnboundedSender, app: Arc, ) -> Result<(), DaemonErr> { glib::MainContext::default().spawn_local(async move { @@ -82,8 +90,7 @@ pub fn init_gtk_async( } Some(evt) = evt_receiver.recv() => { - // TODO do this - match process_evt(evt.evt, app.clone()) { + match process_evt(evt.evt, app.clone(), evt_sender.clone()) { Err(e) => send_res(evt.sender, DaemonRes::Failure(format!("{:?}", e))), Ok(res) => send_res(evt.sender, res), } @@ -107,7 +114,11 @@ fn activate(app: >k4::Application) { app.window_by_id(1).unwrap(); } -pub fn start_app(evt_receiver: UnboundedReceiver, path: Option) { +pub fn start_app( + evt_receiver: UnboundedReceiver, + evt_sender: UnboundedSender, + path: Option, +) { gtk4::init().unwrap(); let app = Arc::new(gtk4::Application::new( @@ -117,7 +128,7 @@ pub fn start_app(evt_receiver: UnboundedReceiver, path: Option Result { +pub fn handle_vol_cmd( + cmd: Vol, + window: &Window, + sender: UnboundedSender, +) -> Result { match cmd { - DaemonCmd::SetVol(val) => { + Vol::Set(val) => { window .child() .and_downcast_ref::() .unwrap() .set_value(utils::vol_round(val as f64)); } - DaemonCmd::GetVol => { + Vol::Get => { return Ok(DaemonRes::VolGet( window.child().and_downcast_ref::().unwrap().value(), )); } - DaemonCmd::IncVol(val) => { + Vol::Inc(val) => { let value = window.child().and_downcast_ref::().unwrap().value(); window @@ -31,7 +38,7 @@ pub fn handle_vol_cmd(cmd: DaemonCmd, window: &Window) -> Result { + Vol::Dec(val) => { let value = window.child().and_downcast_ref::().unwrap().value(); window @@ -40,7 +47,24 @@ pub fn handle_vol_cmd(cmd: DaemonCmd, window: &Window) -> Result {} + Vol::Close => { + window.hide(); + } + Vol::Open => { + window.show(); + } + Vol::OpenTime(f64) => { + window.show(); + tokio::spawn(async move { + tokio::time::sleep(Duration::from_secs_f64(f64)).await; + if let Err(e) = sender.send(DaemonEvt { + evt: DaemonCmd::Vol(Vol::Close), + sender: None, + }) { + println!("Err closing the openned window: {}", e); + } + }); + } } Ok(DaemonRes::Success) diff --git a/src/daemon/server.rs b/src/daemon/server.rs index c4746a7..cff4d6b 100644 --- a/src/daemon/server.rs +++ b/src/daemon/server.rs @@ -77,7 +77,7 @@ async fn handle_connection( let cmd = DaemonEvt { evt: evt.clone(), - sender: res_sender, + sender: Some(res_sender), }; println!("Event receiverd from client: {:?}", evt); diff --git a/src/daemon/structs.rs b/src/daemon/structs.rs index e951df4..2102c1c 100644 --- a/src/daemon/structs.rs +++ b/src/daemon/structs.rs @@ -3,18 +3,25 @@ use tokio::sync::mpsc::UnboundedSender; #[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub enum DaemonCmd { - CloseWindow, ShutDown, - GetVol, - SetVol(u32), - IncVol(u32), - DecVol(u32), + Vol(Vol), +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy)] +pub enum Vol { + Get, + Set(u32), + Inc(u32), + Dec(u32), + Close, + Open, + OpenTime(f64), } #[derive(Debug, Clone)] pub struct DaemonEvt { pub evt: DaemonCmd, - pub sender: UnboundedSender, + pub sender: Option>, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/src/main.rs b/src/main.rs index 663eea1..a147689 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ pub mod utils; use clap::Parser; use cli::args::{self, Args, DaemonSubCmd}; -use daemon::structs::DaemonCmd; +use daemon::structs::{DaemonCmd, Vol}; fn main() { let args = Args::parse(); @@ -33,10 +33,18 @@ fn main() { args::Command::Volume { actions } => { let evt = match actions { - args::VolCmd::Get => DaemonCmd::GetVol, - args::VolCmd::Set { value } => DaemonCmd::SetVol(value), - args::VolCmd::Inc { value } => DaemonCmd::IncVol(value), - args::VolCmd::Dec { value } => DaemonCmd::DecVol(value), + args::VolCmd::Get => DaemonCmd::Vol(Vol::Get), + args::VolCmd::Set { value } => DaemonCmd::Vol(Vol::Set(value)), + args::VolCmd::Inc { value } => DaemonCmd::Vol(Vol::Inc(value)), + args::VolCmd::Dec { value } => DaemonCmd::Vol(Vol::Dec(value)), + args::VolCmd::Close => DaemonCmd::Vol(Vol::Close), + args::VolCmd::Open { time } => { + if time.is_none() { + DaemonCmd::Vol(Vol::Open) + } else { + DaemonCmd::Vol(Vol::OpenTime(time.unwrap())) + } + } }; if let Err(e) = cli::send_evt(evt) { println!("Err Sending event: {:?}", e);