Skip to content

Commit

Permalink
open and close the volume
Browse files Browse the repository at this point in the history
  • Loading branch information
BL-CZY committed Aug 31, 2024
1 parent eb8bf54 commit db2d700
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 30 deletions.
6 changes: 6 additions & 0 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64> },
}
3 changes: 2 additions & 1 deletion src/daemon/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn start_daemon(path: Option<String>) -> 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 || {
Expand All @@ -40,7 +41,7 @@ pub fn start_daemon(path: Option<String>) -> Result<(), DaemonErr> {

let _g = handle.enter();

start_app(evt_receiver, path);
start_app(evt_receiver, evt_sender_clone.clone(), path);

Ok(())
}
31 changes: 21 additions & 10 deletions src/daemon/renderer/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ lazy_static! {
pub static ref IDS: Mutex<HashMap<Widget, u32>> = Mutex::new(HashMap::new());
}

fn process_evt(evt: DaemonCmd, app: Arc<Application>) -> Result<DaemonRes, DaemonErr> {
fn process_evt(
evt: DaemonCmd,
app: Arc<Application>,
sender: UnboundedSender<DaemonEvt>,
) -> Result<DaemonRes, DaemonErr> {
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(),
Expand All @@ -54,23 +58,27 @@ fn process_evt(evt: DaemonCmd, app: Arc<Application>) -> Result<DaemonRes, Daemo
evt,
&app.window_by_id(*guard.get(&Widget::Volume).unwrap())
.unwrap(),
sender,
)?;
}

_ => {}
}

Ok(DaemonRes::Success)
}

fn send_res(sender: UnboundedSender<DaemonRes>, res: DaemonRes) {
if let Err(e) = sender.send(res) {
fn send_res(sender: Option<UnboundedSender<DaemonRes>>, 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<DaemonEvt>,
evt_sender: UnboundedSender<DaemonEvt>,
app: Arc<Application>,
) -> Result<(), DaemonErr> {
glib::MainContext::default().spawn_local(async move {
Expand All @@ -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),
}
Expand All @@ -107,7 +114,11 @@ fn activate(app: &gtk4::Application) {
app.window_by_id(1).unwrap();
}

pub fn start_app(evt_receiver: UnboundedReceiver<DaemonEvt>, path: Option<String>) {
pub fn start_app(
evt_receiver: UnboundedReceiver<DaemonEvt>,
evt_sender: UnboundedSender<DaemonEvt>,
path: Option<String>,
) {
gtk4::init().unwrap();

let app = Arc::new(gtk4::Application::new(
Expand All @@ -117,7 +128,7 @@ pub fn start_app(evt_receiver: UnboundedReceiver<DaemonEvt>, path: Option<String

let _app_descriptor = read_config(path);

if let Err(e) = init_gtk_async(evt_receiver, app.clone()) {
if let Err(e) = init_gtk_async(evt_receiver, evt_sender, app.clone()) {
println!("Err handling command: {:?}", e);
}

Expand Down
38 changes: 31 additions & 7 deletions src/daemon/renderer/popup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::daemon::structs::DaemonRes;
use std::time::Duration;

use crate::daemon::structs::{DaemonEvt, DaemonRes, Vol};
use crate::utils;
use crate::{daemon::structs::DaemonCmd, utils::DaemonErr};

Expand All @@ -7,22 +9,27 @@ use super::{
window::{self, WindowDescriptor},
};
use gtk4::{prelude::*, Adjustment, Application, ApplicationWindow, Scale, Window};
use tokio::sync::mpsc::UnboundedSender;

pub fn handle_vol_cmd(cmd: DaemonCmd, window: &Window) -> Result<DaemonRes, DaemonErr> {
pub fn handle_vol_cmd(
cmd: Vol,
window: &Window,
sender: UnboundedSender<DaemonEvt>,
) -> Result<DaemonRes, DaemonErr> {
match cmd {
DaemonCmd::SetVol(val) => {
Vol::Set(val) => {
window
.child()
.and_downcast_ref::<Scale>()
.unwrap()
.set_value(utils::vol_round(val as f64));
}
DaemonCmd::GetVol => {
Vol::Get => {
return Ok(DaemonRes::VolGet(
window.child().and_downcast_ref::<Scale>().unwrap().value(),
));
}
DaemonCmd::IncVol(val) => {
Vol::Inc(val) => {
let value = window.child().and_downcast_ref::<Scale>().unwrap().value();

window
Expand All @@ -31,7 +38,7 @@ pub fn handle_vol_cmd(cmd: DaemonCmd, window: &Window) -> Result<DaemonRes, Daem
.unwrap()
.set_value(utils::vol_round(value + val as f64));
}
DaemonCmd::DecVol(val) => {
Vol::Dec(val) => {
let value = window.child().and_downcast_ref::<Scale>().unwrap().value();

window
Expand All @@ -40,7 +47,24 @@ pub fn handle_vol_cmd(cmd: DaemonCmd, window: &Window) -> Result<DaemonRes, Daem
.unwrap()
.set_value(utils::vol_round(value - val as f64));
}
_ => {}
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)
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 13 additions & 6 deletions src/daemon/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DaemonRes>,
pub sender: Option<UnboundedSender<DaemonRes>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit db2d700

Please sign in to comment.