fix: calculation of read backlight values
This commit is contained in:
parent
16f8cd7812
commit
cbe57ab95f
2 changed files with 52 additions and 58 deletions
|
|
@ -2,18 +2,18 @@
|
||||||
// How should key bindings be handled? Need something like gnome-settings-daemon?
|
// How should key bindings be handled? Need something like gnome-settings-daemon?
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
fmt::Debug,
|
||||||
fs::File,
|
fs::File,
|
||||||
|
hash::Hash,
|
||||||
io::{self, Read},
|
io::{self, Read},
|
||||||
os::unix::ffi::OsStrExt,
|
os::unix::ffi::OsStrExt,
|
||||||
path::Path,
|
path::Path,
|
||||||
str::{self, FromStr},
|
str::{self, FromStr},
|
||||||
hash::Hash,
|
|
||||||
fmt::Debug
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use cosmic::iced;
|
use cosmic::iced;
|
||||||
use iced::subscription;
|
use iced::subscription;
|
||||||
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel};
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
||||||
|
|
||||||
const BACKLIGHT_SYSDIR: &str = "/sys/class/backlight";
|
const BACKLIGHT_SYSDIR: &str = "/sys/class/backlight";
|
||||||
|
|
||||||
|
|
@ -61,7 +61,7 @@ pub async fn backlight() -> io::Result<Option<Backlight>> {
|
||||||
let mut best_backlight = None;
|
let mut best_backlight = None;
|
||||||
let mut best_max_brightness = 0;
|
let mut best_max_brightness = 0;
|
||||||
let mut dir_stream = tokio::fs::read_dir(BACKLIGHT_SYSDIR).await?;
|
let mut dir_stream = tokio::fs::read_dir(BACKLIGHT_SYSDIR).await?;
|
||||||
while let Ok(Some(entry)) = dir_stream.next_entry().await {
|
while let Ok(Some(entry)) = dir_stream.next_entry().await {
|
||||||
if let Ok(filename) = str::from_utf8(entry.file_name().as_bytes()) {
|
if let Ok(filename) = str::from_utf8(entry.file_name().as_bytes()) {
|
||||||
let backlight = Backlight(filename.to_string());
|
let backlight = Backlight(filename.to_string());
|
||||||
if let Some(max_brightness) = backlight.max_brightness().await {
|
if let Some(max_brightness) = backlight.max_brightness().await {
|
||||||
|
|
@ -75,7 +75,6 @@ pub async fn backlight() -> io::Result<Option<Backlight>> {
|
||||||
Ok(best_backlight)
|
Ok(best_backlight)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn screen_backlight_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
pub fn screen_backlight_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
||||||
id: I,
|
id: I,
|
||||||
) -> iced::Subscription<(I, ScreenBacklightUpdate)> {
|
) -> iced::Subscription<(I, ScreenBacklightUpdate)> {
|
||||||
|
|
@ -84,11 +83,18 @@ pub fn screen_backlight_subscription<I: 'static + Hash + Copy + Send + Sync + De
|
||||||
|
|
||||||
pub enum State {
|
pub enum State {
|
||||||
Ready,
|
Ready,
|
||||||
Waiting(Backlight, LogindSessionProxy<'static>, UnboundedReceiver<ScreenBacklightRequest>),
|
Waiting(
|
||||||
|
Backlight,
|
||||||
|
LogindSessionProxy<'static>,
|
||||||
|
UnboundedReceiver<ScreenBacklightRequest>,
|
||||||
|
),
|
||||||
Finished,
|
Finished,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, ScreenBacklightUpdate)>, State) {
|
async fn start_listening<I: Copy>(
|
||||||
|
id: I,
|
||||||
|
state: State,
|
||||||
|
) -> (Option<(I, ScreenBacklightUpdate)>, State) {
|
||||||
match state {
|
match state {
|
||||||
State::Ready => {
|
State::Ready => {
|
||||||
let conn = match zbus::Connection::system().await {
|
let conn = match zbus::Connection::system().await {
|
||||||
|
|
@ -105,44 +111,38 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, ScreenBack
|
||||||
};
|
};
|
||||||
let (tx, rx) = unbounded_channel();
|
let (tx, rx) = unbounded_channel();
|
||||||
|
|
||||||
|
let b = (backlight.brightness().await.unwrap_or_default() as f64
|
||||||
|
/ backlight.max_brightness().await.unwrap_or(1) as f64)
|
||||||
|
.clamp(0., 1.);
|
||||||
return (
|
return (
|
||||||
Some((
|
Some((id, ScreenBacklightUpdate::Init(tx, b))),
|
||||||
id,
|
|
||||||
ScreenBacklightUpdate::Init(tx, backlight.brightness().await.unwrap_or_default() as f64)
|
|
||||||
)),
|
|
||||||
State::Waiting(backlight, screen_proxy, rx),
|
State::Waiting(backlight, screen_proxy, rx),
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
State::Waiting(backlight, proxy, mut rx) => {
|
State::Waiting(backlight, proxy, mut rx) => match rx.recv().await {
|
||||||
match rx.recv().await {
|
Some(req) => match req {
|
||||||
Some(req) => match req {
|
ScreenBacklightRequest::Get => {
|
||||||
ScreenBacklightRequest::Get => {
|
let msg = if let Some(max_brightness) = backlight.max_brightness().await {
|
||||||
let msg = if let Some(max_brightness) = backlight.max_brightness().await {
|
let value = (backlight.brightness().await.unwrap_or_default() as f64
|
||||||
let value = (backlight.brightness().await.unwrap_or_default() as f64 / max_brightness as f64).clamp(0., 1.);
|
/ max_brightness as f64)
|
||||||
Some((
|
.clamp(0., 1.);
|
||||||
id,
|
Some((id, ScreenBacklightUpdate::Update(value)))
|
||||||
ScreenBacklightUpdate::Update(value)
|
} else {
|
||||||
))
|
None
|
||||||
} else { None };
|
};
|
||||||
(msg, State::Waiting(backlight, proxy, rx))
|
(msg, State::Waiting(backlight, proxy, rx))
|
||||||
|
}
|
||||||
|
ScreenBacklightRequest::Set(value) => {
|
||||||
|
if let Some(max_brightness) = backlight.max_brightness().await {
|
||||||
|
let value = value.clamp(0., 1.) * (max_brightness as f64);
|
||||||
|
let value = value.round() as u32;
|
||||||
|
let _ = backlight.set_brightness(&proxy, value).await;
|
||||||
}
|
}
|
||||||
,
|
(None, State::Waiting(backlight, proxy, rx))
|
||||||
ScreenBacklightRequest::Set(value) => {
|
}
|
||||||
if let Some(max_brightness) = backlight.max_brightness().await {
|
},
|
||||||
let value = value.clamp(0., 1.) * (max_brightness as f64);
|
None => (None, State::Finished),
|
||||||
let value = value.round() as u32;
|
},
|
||||||
let _ = backlight.set_brightness(&proxy, value).await;
|
|
||||||
}
|
|
||||||
(
|
|
||||||
None,
|
|
||||||
State::Waiting(backlight, proxy, rx),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
None => (None, State::Finished),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
State::Finished => iced::futures::future::pending().await,
|
State::Finished => iced::futures::future::pending().await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -150,7 +150,7 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, ScreenBack
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ScreenBacklightUpdate {
|
pub enum ScreenBacklightUpdate {
|
||||||
Update(f64),
|
Update(f64),
|
||||||
Init(UnboundedSender<ScreenBacklightRequest>, f64)
|
Init(UnboundedSender<ScreenBacklightRequest>, f64),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -159,7 +159,6 @@ pub enum ScreenBacklightRequest {
|
||||||
Set(f64),
|
Set(f64),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// TODO: Cache device, max_brightness, etc.
|
// TODO: Cache device, max_brightness, etc.
|
||||||
async fn set_display_brightness(brightness: f64) -> io::Result<()> {
|
async fn set_display_brightness(brightness: f64) -> io::Result<()> {
|
||||||
|
|
|
||||||
|
|
@ -64,28 +64,23 @@ async fn start_listening<I: Copy>(
|
||||||
};
|
};
|
||||||
let (tx, rx) = unbounded_channel();
|
let (tx, rx) = unbounded_channel();
|
||||||
|
|
||||||
|
let b = kbd_proxy.get_brightness().await.unwrap_or_default() as f64
|
||||||
|
/ kbd_proxy.get_max_brightness().await.unwrap_or(1) as f64;
|
||||||
return (
|
return (
|
||||||
Some((
|
Some((id, KeyboardBacklightUpdate::Init(tx, b))),
|
||||||
id,
|
|
||||||
KeyboardBacklightUpdate::Init(
|
|
||||||
tx,
|
|
||||||
kbd_proxy.get_brightness().await.unwrap_or_default() as f64,
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
State::Waiting(kbd_proxy, rx),
|
State::Waiting(kbd_proxy, rx),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
State::Waiting(proxy, mut rx) => match rx.recv().await {
|
State::Waiting(proxy, mut rx) => match rx.recv().await {
|
||||||
Some(req) => match req {
|
Some(req) => match req {
|
||||||
KeyboardBacklightRequest::Get => (
|
KeyboardBacklightRequest::Get => {
|
||||||
Some((
|
let b = proxy.get_brightness().await.unwrap_or_default() as f64
|
||||||
id,
|
/ proxy.get_max_brightness().await.unwrap_or(1) as f64;
|
||||||
KeyboardBacklightUpdate::Update(
|
(
|
||||||
proxy.get_brightness().await.unwrap_or_default() as f64,
|
Some((id, KeyboardBacklightUpdate::Update(b))),
|
||||||
),
|
State::Waiting(proxy, rx),
|
||||||
)),
|
)
|
||||||
State::Waiting(proxy, rx),
|
}
|
||||||
),
|
|
||||||
KeyboardBacklightRequest::Set(value) => {
|
KeyboardBacklightRequest::Set(value) => {
|
||||||
if let Ok(max_brightness) = proxy.get_max_brightness().await {
|
if let Ok(max_brightness) = proxy.get_max_brightness().await {
|
||||||
let value = value.clamp(0., 1.) * (max_brightness as f64);
|
let value = value.clamp(0., 1.) * (max_brightness as f64);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue