Skip to content

Commit

Permalink
brightness
Browse files Browse the repository at this point in the history
  • Loading branch information
BL-CZY committed Sep 21, 2024
1 parent c04c96e commit 343e735
Show file tree
Hide file tree
Showing 13 changed files with 765 additions and 221 deletions.
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ once_cell = "1.19.0"
anyhow = "1.0"
toml = "0.8.19"
lazy_static = "1.5.0"
backlight = "0.1.1"

115 changes: 113 additions & 2 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::daemon::structs::{Bri, DaemonCmd, Vol};
use clap::{Parser, Subcommand};

#[derive(Parser)]
Expand All @@ -16,11 +17,17 @@ pub enum Command {
option: Option<DaemonSubCmd>,
},

#[clap(about = "Connect to the daemon")]
#[clap(about = "Configure the volume panel")]
Volume {
#[clap(subcommand)]
actions: VolCmd,
},

#[clap(about = "Configure the brightness panel")]
Brightness {
#[clap(subcommand)]
actions: BriCmd,
},
}

#[derive(Subcommand)]
Expand All @@ -31,6 +38,26 @@ pub enum DaemonSubCmd {
Shutdown,
}

#[derive(Subcommand)]
pub enum BriCmd {
#[clap(about = "Set brightness")]
SetRough { value: u32 },
#[clap(about = "Set brightness with murph effect")]
Set { value: u32 },
#[clap(about = "Get the current scale")]
Get,
#[clap(about = "Increase the brightness")]
Inc { value: u32 },
#[clap(about = "Decrease the brightness")]
Dec { value: u32 },
#[clap(about = "Hide 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> },
}

#[derive(Subcommand)]
pub enum VolCmd {
#[clap(about = "toggle/set mute")]
Expand All @@ -47,10 +74,94 @@ pub enum VolCmd {
Inc { value: u32 },
#[clap(about = "Decrease the volume")]
Dec { value: u32 },
#[clap(about = "Close the scale")]
#[clap(about = "Hide 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> },
}

fn daemon_args(path: Option<String>, option: Option<DaemonSubCmd>) {
if option.is_none() {
if let Err(e) = crate::daemon::start_daemon(path) {
println!("Error starting the daemon: {:?}", e)
};
} else {
match option.unwrap() {
DaemonSubCmd::Start => {
if let Err(e) = crate::daemon::start_daemon(path) {
println!("Error starting the daemon: {:?}", e)
};
}
DaemonSubCmd::Shutdown => {
if let Err(e) = crate::cli::send_evt(DaemonCmd::ShutDown) {
println!("Error sending event: {:?}", e)
}
}
}
}
}

fn volume_args(actions: VolCmd) {
let evt = match actions {
VolCmd::SetMute { value } => match value {
Some(val) => DaemonCmd::Vol(Vol::SetMute(val)),
None => DaemonCmd::Vol(Vol::ToggleMute),
},
VolCmd::GetMute => DaemonCmd::Vol(Vol::GetMute),
VolCmd::Get => DaemonCmd::Vol(Vol::Get),
VolCmd::SetRough { value } => DaemonCmd::Vol(Vol::SetRough(value as f64)),
VolCmd::Set { value } => DaemonCmd::Vol(Vol::Set(value as f64)),
VolCmd::Inc { value } => DaemonCmd::Vol(Vol::Inc(value as f64)),
VolCmd::Dec { value } => DaemonCmd::Vol(Vol::Dec(value as f64)),
VolCmd::Close => DaemonCmd::Vol(Vol::Close),
VolCmd::Open { time } => {
if let Some(t) = time {
DaemonCmd::Vol(Vol::OpenTimed(t))
} else {
DaemonCmd::Vol(Vol::Open)
}
}
};
if let Err(e) = crate::cli::send_evt(evt) {
println!("Err Sending event: {:?}", e);
}
}

fn bri_args(actions: BriCmd) {
let evt = match actions {
BriCmd::Get => DaemonCmd::Bri(Bri::Get),
BriCmd::SetRough { value } => DaemonCmd::Bri(Bri::SetRough(value as f64)),
BriCmd::Set { value } => DaemonCmd::Bri(Bri::Set(value as f64)),
BriCmd::Inc { value } => DaemonCmd::Bri(Bri::Inc(value as f64)),
BriCmd::Dec { value } => DaemonCmd::Bri(Bri::Dec(value as f64)),
BriCmd::Close => DaemonCmd::Bri(Bri::Close),
BriCmd::Open { time } => {
if let Some(t) = time {
DaemonCmd::Bri(Bri::OpenTimed(t))
} else {
DaemonCmd::Bri(Bri::Open)
}
}
};
if let Err(e) = crate::cli::send_evt(evt) {
println!("Err Sending event: {:?}", e);
}
}

pub fn handle_args(args: Args) {
match args.commands {
Command::Daemon { path, option } => {
daemon_args(path, option);
}

Command::Volume { actions } => {
volume_args(actions);
}

Command::Brightness { actions } => {
bri_args(actions);
}
}
}
1 change: 1 addition & 0 deletions src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub async fn send_evt_async(evt: DaemonCmd) -> Result<(), ClientErr> {
DaemonRes::Success => println!("Success"),
DaemonRes::GetVol(val) => println!("{}", val),
DaemonRes::GetMute(val) => println!("{}", val),
DaemonRes::GetBri(val) => println!("{}", val),
}

Ok(())
Expand Down
50 changes: 41 additions & 9 deletions src/daemon/renderer/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ use gtk4::CssProvider;
use lazy_static::lazy_static;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::sync::mpsc::UnboundedSender;
use tokio::task::JoinHandle;

use super::bri::create_bri_osd;
use super::bri::handle_bri_cmd;
use super::bri::BriContext;
use super::config::AppConf;
use super::vol::create_sound_osd;
use super::vol::handle_vol_cmd;
Expand All @@ -28,16 +30,19 @@ use super::vol::VolContext;
#[derive(PartialEq, Eq, Hash)]
pub enum Widget {
Volume = 0,
Brightness = 1,
}

pub struct AppContext {
pub vol: VolContext,
pub bri: BriContext,
}

impl AppContext {
pub fn from_config(config: &Arc<AppConf>) -> Self {
AppContext {
vol: VolContext::from_config(config),
bri: BriContext::from_config(config),
}
}

Expand All @@ -52,6 +57,18 @@ impl AppContext {

self.vol.cur_vol
}

pub fn set_virtual_brightness(&mut self, val: f64) -> f64 {
if val > 100f64 {
self.bri.cur_bri = 100f64;
} else if val < 0f64 {
self.bri.cur_bri = 0f64;
} else {
self.bri.cur_bri = val;
}

self.bri.cur_bri
}
}

pub fn register_widget(widget: Widget, id: u32) {
Expand All @@ -72,7 +89,6 @@ fn process_evt(
app: Rc<Application>,
sender: UnboundedSender<DaemonEvt>,
config: Arc<AppConf>,
vol_tasks: Rc<RefCell<HashMap<VolTaskType, JoinHandle<()>>>>,
app_context: Rc<RefCell<AppContext>>,
) -> Result<DaemonRes, DaemonErr> {
match evt {
Expand All @@ -92,7 +108,24 @@ fn process_evt(
.unwrap(),
sender,
app_context,
vol_tasks,
config,
)?;

return Ok(result);
}

DaemonCmd::Bri(evt) => {
let guard = match IDS.lock() {
Ok(g) => g,
Err(poisoned) => poisoned.into_inner(),
};

let result = handle_bri_cmd(
evt,
&app.window_by_id(*guard.get(&Widget::Brightness).unwrap())
.unwrap(),
sender,
app_context,
config,
)?;

Expand All @@ -114,7 +147,7 @@ fn send_res(sender: Option<UnboundedSender<DaemonRes>>, res: DaemonRes) {
}

#[derive(Hash, PartialEq, Eq)]
pub enum VolTaskType {
pub enum VolBriTaskType {
AwaitClose,
MurphValue,
}
Expand All @@ -125,21 +158,19 @@ pub fn init_gtk_async(
app: Rc<Application>,
config: Arc<AppConf>,
) -> Result<(), DaemonErr> {
let vol_tasks: Rc<RefCell<HashMap<VolTaskType, JoinHandle<()>>>> =
Rc::new(RefCell::new(HashMap::new()));

let context = Rc::new(RefCell::new(AppContext::from_config(&config)));

glib::MainContext::default().spawn_local(async move {
loop {
tokio::select! {
Ok(()) = crate::utils::receive_exit() => {
println!("Shutting down the GUI...");
app.quit();
break;
}

Some(evt) = evt_receiver.recv() => {
match process_evt(evt.evt, app.clone(), evt_sender.clone(), config.clone(), vol_tasks.clone(), context.clone()) {
match process_evt(evt.evt, app.clone(), evt_sender.clone(), config.clone(), context.clone()) {
Err(e) => send_res(evt.sender, DaemonRes::Failure(format!("{:?}", e))),
Ok(res) => send_res(evt.sender, res),
}
Expand All @@ -159,7 +190,8 @@ fn activate(backend: DisplayBackend, app: &gtk4::Application, config: Arc<AppCon
&css,
gtk4::STYLE_PROVIDER_PRIORITY_SETTINGS,
);
create_sound_osd(backend, app, config);
create_sound_osd(backend, app, config.clone());
create_bri_osd(backend, app, config.clone());
}

pub fn start_app(
Expand Down
Loading

0 comments on commit 343e735

Please sign in to comment.