Enforce valid image brightness in daemon and asusctl

This commit is contained in:
I Al Istannen
2022-07-16 03:00:22 +00:00
committed by Luke Jones
parent ef04549c8e
commit c0e36295b7
6 changed files with 50 additions and 12 deletions

View File

@@ -232,7 +232,8 @@ fn handle_anime(
dbus.proxies().anime().set_boot_on_off(anime_boot)? dbus.proxies().anime().set_boot_on_off(anime_boot)?
} }
if let Some(bright) = cmd.brightness { if let Some(bright) = cmd.brightness {
dbus.proxies().anime().set_brightness(bright as f32)? verify_brightness(bright);
dbus.proxies().anime().set_brightness(bright)?
} }
if let Some(action) = cmd.command.as_ref() { if let Some(action) = cmd.command.as_ref() {
let anime_type = get_anime_type()?; let anime_type = get_anime_type()?;
@@ -245,6 +246,7 @@ fn handle_anime(
} }
std::process::exit(1); std::process::exit(1);
} }
verify_brightness(image.bright);
let matrix = AnimeImage::from_png( let matrix = AnimeImage::from_png(
Path::new(&image.path), Path::new(&image.path),
@@ -267,6 +269,7 @@ fn handle_anime(
} }
std::process::exit(1); std::process::exit(1);
} }
verify_brightness(image.bright);
let matrix = AnimeDiagonal::from_png( let matrix = AnimeDiagonal::from_png(
Path::new(&image.path), Path::new(&image.path),
@@ -287,6 +290,7 @@ fn handle_anime(
} }
std::process::exit(1); std::process::exit(1);
} }
verify_brightness(gif.bright);
let matrix = AnimeGif::from_gif( let matrix = AnimeGif::from_gif(
Path::new(&gif.path), Path::new(&gif.path),
@@ -320,6 +324,7 @@ fn handle_anime(
} }
std::process::exit(1); std::process::exit(1);
} }
verify_brightness(gif.bright);
let matrix = AnimeGif::from_diagonal_gif( let matrix = AnimeGif::from_diagonal_gif(
Path::new(&gif.path), Path::new(&gif.path),
@@ -347,6 +352,16 @@ fn handle_anime(
Ok(()) Ok(())
} }
fn verify_brightness(brightness: f32) {
if brightness < 0.0 || brightness > 1.0 {
println!(
"Image and global brightness must be between 0.0 and 1.0 (inclusive), was {}",
brightness
);
std::process::exit(1);
}
}
fn handle_led_mode( fn handle_led_mode(
dbus: &RogDbusClientBlocking, dbus: &RogDbusClientBlocking,
supported: &LedSupportedFunctions, supported: &LedSupportedFunctions,

View File

@@ -157,17 +157,20 @@ impl AnimeConfig {
if read_len == 0 { if read_len == 0 {
return AnimeConfig::create_default(&mut file); return AnimeConfig::create_default(&mut file);
} else { } else {
if let Ok(data) = serde_json::from_str(&buf) { if let Ok(mut data) = serde_json::from_str(&buf) {
Self::clamp_config_brightness(&mut data);
return data; return data;
} else if let Ok(data) = serde_json::from_str::<AnimeConfigV341>(&buf) { } else if let Ok(data) = serde_json::from_str::<AnimeConfigV341>(&buf) {
let config = data.into_current(); let mut config = data.into_current();
config.write(); config.write();
info!("Updated config version to: {}", VERSION); info!("Updated config version to: {}", VERSION);
Self::clamp_config_brightness(&mut config);
return config; return config;
} else if let Ok(data) = serde_json::from_str::<AnimeConfigV352>(&buf) { } else if let Ok(data) = serde_json::from_str::<AnimeConfigV352>(&buf) {
let config = data.into_current(); let mut config = data.into_current();
config.write(); config.write();
info!("Updated config version to: {}", VERSION); info!("Updated config version to: {}", VERSION);
Self::clamp_config_brightness(&mut config);
return config; return config;
} }
warn!( warn!(
@@ -186,6 +189,16 @@ impl AnimeConfig {
AnimeConfig::create_default(&mut file) AnimeConfig::create_default(&mut file)
} }
fn clamp_config_brightness(mut config: &mut AnimeConfig) {
if config.brightness < 0.0 || config.brightness > 1.0 {
warn!(
"Clamped brightness to [0.0 ; 1.0], was {}",
config.brightness
);
config.brightness = f32::max(0.0, f32::min(1.0, config.brightness));
}
}
fn create_default(file: &mut File) -> Self { fn create_default(file: &mut File) -> Self {
// create a default config here // create a default config here
let config = AnimeConfig { let config = AnimeConfig {

View File

@@ -44,8 +44,8 @@ impl CtrlAnimeZbus {
let mut bright = bright; let mut bright = bright;
if bright < 0.0 { if bright < 0.0 {
bright = 0.0 bright = 0.0
} else if bright > 254.0 { } else if bright > 1.0 {
bright = 254.0; bright = 1.0;
} }
lock.config.brightness = bright; lock.config.brightness = bright;
lock.config.write(); lock.config.write();

View File

@@ -16,6 +16,7 @@ pub enum AnimeError {
Udev(String, std::io::Error), Udev(String, std::io::Error),
NoDevice, NoDevice,
UnsupportedDevice, UnsupportedDevice,
InvalidBrightness(f32),
} }
impl fmt::Display for AnimeError { impl fmt::Display for AnimeError {
@@ -36,6 +37,11 @@ impl fmt::Display for AnimeError {
AnimeError::Udev(deets, error) => write!(f, "udev {}: {}", deets, error), AnimeError::Udev(deets, error) => write!(f, "udev {}: {}", deets, error),
AnimeError::NoDevice => write!(f, "No AniMe Matrix device found"), AnimeError::NoDevice => write!(f, "No AniMe Matrix device found"),
AnimeError::UnsupportedDevice => write!(f, "Unsupported AniMe Matrix device found"), AnimeError::UnsupportedDevice => write!(f, "Unsupported AniMe Matrix device found"),
AnimeError::InvalidBrightness(bright) => write!(
f,
"Image brightness must be between 0.0 and 1.0 (inclusive), was {}",
bright
),
} }
} }
} }

View File

@@ -191,7 +191,7 @@ impl AnimeGif {
pixels, pixels,
decoder.width() as u32, decoder.width() as u32,
anime_type, anime_type,
); )?;
while let Some(frame) = decoder.read_next_frame()? { while let Some(frame) = decoder.read_next_frame()? {
let wait = frame.delay * 10; let wait = frame.delay * 10;
@@ -206,7 +206,7 @@ impl AnimeGif {
pixels, pixels,
width as u32, width as u32,
anime_type, anime_type,
); )?;
} }
for (y, row) in frame.buffer.chunks(frame.width as usize * 4).enumerate() { for (y, row) in frame.buffer.chunks(frame.width as usize * 4).enumerate() {
for (x, px) in row.chunks(4).enumerate() { for (x, px) in row.chunks(4).enumerate() {

View File

@@ -83,8 +83,12 @@ impl AnimeImage {
pixels: Vec<Pixel>, pixels: Vec<Pixel>,
width: u32, width: u32,
anime_type: AnimeType, anime_type: AnimeType,
) -> Self { ) -> Result<Self, AnimeError> {
Self { if bright < 0.0 || bright > 1.0 {
return Err(AnimeError::InvalidBrightness(bright));
}
Ok(Self {
scale, scale,
angle, angle,
translation, translation,
@@ -93,7 +97,7 @@ impl AnimeImage {
img_pixels: pixels, img_pixels: pixels,
width, width,
anime_type, anime_type,
} })
} }
// TODO: Convert functions back to const after todo completed // TODO: Convert functions back to const after todo completed
@@ -435,7 +439,7 @@ impl AnimeImage {
pixels, pixels,
width, width,
anime_type, anime_type,
); )?;
matrix.update(); matrix.update();
Ok(matrix) Ok(matrix)