From c0e36295b7cca51bfa3ac003c7e8931b320894b5 Mon Sep 17 00:00:00 2001 From: I Al Istannen Date: Sat, 16 Jul 2022 03:00:22 +0000 Subject: [PATCH] Enforce valid image brightness in daemon and asusctl --- asusctl/src/main.rs | 17 ++++++++++++++++- daemon/src/ctrl_anime/config.rs | 19 ++++++++++++++++--- daemon/src/ctrl_anime/zbus.rs | 4 ++-- rog-anime/src/error.rs | 6 ++++++ rog-anime/src/gif.rs | 4 ++-- rog-anime/src/image.rs | 12 ++++++++---- 6 files changed, 50 insertions(+), 12 deletions(-) diff --git a/asusctl/src/main.rs b/asusctl/src/main.rs index 331980fc..e4adad81 100644 --- a/asusctl/src/main.rs +++ b/asusctl/src/main.rs @@ -232,7 +232,8 @@ fn handle_anime( dbus.proxies().anime().set_boot_on_off(anime_boot)? } 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() { let anime_type = get_anime_type()?; @@ -245,6 +246,7 @@ fn handle_anime( } std::process::exit(1); } + verify_brightness(image.bright); let matrix = AnimeImage::from_png( Path::new(&image.path), @@ -267,6 +269,7 @@ fn handle_anime( } std::process::exit(1); } + verify_brightness(image.bright); let matrix = AnimeDiagonal::from_png( Path::new(&image.path), @@ -287,6 +290,7 @@ fn handle_anime( } std::process::exit(1); } + verify_brightness(gif.bright); let matrix = AnimeGif::from_gif( Path::new(&gif.path), @@ -320,6 +324,7 @@ fn handle_anime( } std::process::exit(1); } + verify_brightness(gif.bright); let matrix = AnimeGif::from_diagonal_gif( Path::new(&gif.path), @@ -347,6 +352,16 @@ fn handle_anime( 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( dbus: &RogDbusClientBlocking, supported: &LedSupportedFunctions, diff --git a/daemon/src/ctrl_anime/config.rs b/daemon/src/ctrl_anime/config.rs index 7d6a2a98..7f185fe7 100644 --- a/daemon/src/ctrl_anime/config.rs +++ b/daemon/src/ctrl_anime/config.rs @@ -157,17 +157,20 @@ impl AnimeConfig { if read_len == 0 { return AnimeConfig::create_default(&mut file); } 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; } else if let Ok(data) = serde_json::from_str::(&buf) { - let config = data.into_current(); + let mut config = data.into_current(); config.write(); info!("Updated config version to: {}", VERSION); + Self::clamp_config_brightness(&mut config); return config; } else if let Ok(data) = serde_json::from_str::(&buf) { - let config = data.into_current(); + let mut config = data.into_current(); config.write(); info!("Updated config version to: {}", VERSION); + Self::clamp_config_brightness(&mut config); return config; } warn!( @@ -186,6 +189,16 @@ impl AnimeConfig { 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 { // create a default config here let config = AnimeConfig { diff --git a/daemon/src/ctrl_anime/zbus.rs b/daemon/src/ctrl_anime/zbus.rs index f769b2da..bb10c0d5 100644 --- a/daemon/src/ctrl_anime/zbus.rs +++ b/daemon/src/ctrl_anime/zbus.rs @@ -44,8 +44,8 @@ impl CtrlAnimeZbus { let mut bright = bright; if bright < 0.0 { bright = 0.0 - } else if bright > 254.0 { - bright = 254.0; + } else if bright > 1.0 { + bright = 1.0; } lock.config.brightness = bright; lock.config.write(); diff --git a/rog-anime/src/error.rs b/rog-anime/src/error.rs index 9a97859a..4fc0721d 100644 --- a/rog-anime/src/error.rs +++ b/rog-anime/src/error.rs @@ -16,6 +16,7 @@ pub enum AnimeError { Udev(String, std::io::Error), NoDevice, UnsupportedDevice, + InvalidBrightness(f32), } impl fmt::Display for AnimeError { @@ -36,6 +37,11 @@ impl fmt::Display for AnimeError { AnimeError::Udev(deets, error) => write!(f, "udev {}: {}", deets, error), AnimeError::NoDevice => write!(f, "No 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 + ), } } } diff --git a/rog-anime/src/gif.rs b/rog-anime/src/gif.rs index 5e33ba55..1bdafe13 100644 --- a/rog-anime/src/gif.rs +++ b/rog-anime/src/gif.rs @@ -191,7 +191,7 @@ impl AnimeGif { pixels, decoder.width() as u32, anime_type, - ); + )?; while let Some(frame) = decoder.read_next_frame()? { let wait = frame.delay * 10; @@ -206,7 +206,7 @@ impl AnimeGif { pixels, width as u32, anime_type, - ); + )?; } for (y, row) in frame.buffer.chunks(frame.width as usize * 4).enumerate() { for (x, px) in row.chunks(4).enumerate() { diff --git a/rog-anime/src/image.rs b/rog-anime/src/image.rs index af452edf..ba725a5e 100644 --- a/rog-anime/src/image.rs +++ b/rog-anime/src/image.rs @@ -83,8 +83,12 @@ impl AnimeImage { pixels: Vec, width: u32, anime_type: AnimeType, - ) -> Self { - Self { + ) -> Result { + if bright < 0.0 || bright > 1.0 { + return Err(AnimeError::InvalidBrightness(bright)); + } + + Ok(Self { scale, angle, translation, @@ -93,7 +97,7 @@ impl AnimeImage { img_pixels: pixels, width, anime_type, - } + }) } // TODO: Convert functions back to const after todo completed @@ -435,7 +439,7 @@ impl AnimeImage { pixels, width, anime_type, - ); + )?; matrix.update(); Ok(matrix)