From d9d73f01d79259ca03603d018288d1f6368f5e55 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 7 May 2023 14:48:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=99=A8=E5=8F=82=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/display/display_handler.rs | 35 +++++- src-tauri/src/display/manager.rs | 142 ++--------------------- 2 files changed, 39 insertions(+), 138 deletions(-) diff --git a/src-tauri/src/display/display_handler.rs b/src-tauri/src/display/display_handler.rs index 69e3a91..ac3903b 100644 --- a/src-tauri/src/display/display_handler.rs +++ b/src-tauri/src/display/display_handler.rs @@ -47,21 +47,50 @@ impl DisplayHandler { *state = temp_state; } - pub async fn set_brightness(&self, brightness: u16) { + pub async fn set_brightness(&self, brightness: u16) -> anyhow::Result<()> { + let mut controller = self.controller.write().await; let mut state = self.state.write().await; + + controller + .handle + .set_vcp_feature(0x10, brightness) + .map_err(|err| anyhow::anyhow!("can not set brightness. {:?}", err))?; + + state.brightness = brightness; + state.last_modified_at = SystemTime::now(); + + Ok(()) } - pub async fn set_contrast(&self, contrast: u16) { + pub async fn set_contrast(&self, contrast: u16) -> anyhow::Result<()> { + let mut controller = self.controller.write().await; let mut state = self.state.write().await; + + controller + .handle + .set_vcp_feature(0x12, contrast) + .map_err(|err| anyhow::anyhow!("can not set contrast. {:?}", err))?; + state.contrast = contrast; state.last_modified_at = SystemTime::now(); + + Ok(()) } - pub async fn set_mode(&self, mode: u16) { + pub async fn set_mode(&self, mode: u16) -> anyhow::Result<()> { + let mut controller = self.controller.write().await; let mut state = self.state.write().await; + + controller + .handle + .set_vcp_feature(0xdc, mode) + .map_err(|err| anyhow::anyhow!("can not set mode. {:?}", err))?; + state.mode = mode; state.last_modified_at = SystemTime::now(); + + Ok(()) } } diff --git a/src-tauri/src/display/manager.rs b/src-tauri/src/display/manager.rs index c4bf61c..06ed6b2 100644 --- a/src-tauri/src/display/manager.rs +++ b/src-tauri/src/display/manager.rs @@ -91,11 +91,17 @@ impl DisplayManager { log::info!("display setting request received. {:?}", message); let display = display.unwrap().write().await; - match message.setting { + let result = match message.setting { DisplaySetting::Brightness(value) => display.set_brightness(value as u16).await, DisplaySetting::Contrast(value) => display.set_contrast(value as u16).await, DisplaySetting::Mode(value) => display.set_mode(value as u16).await, + }; + + if let Err(err) = result { + error!("failed to set display setting: {}", err); + continue; } + drop(display); log::info!("display setting request handled. {:?}", message); @@ -119,140 +125,6 @@ impl DisplayManager { pub fn subscribe_displays_changed(&self) -> watch::Receiver> { self.displays_changed_sender.subscribe() } - - // fn read_display_config_by_ddc(index: usize) -> anyhow::Result { - // let mut displays = Display::enumerate(); - // match displays.get_mut(index) { - // Some(display) => { - // let mut config = DisplayState::default(index); - // match display.handle.get_vcp_feature(0x10) { - // Ok(value) => { - // config.max_brightness = value.maximum(); - // config.min_brightness = 0; - // config.brightness = value.value(); - // } - // Err(_) => {} - // }; - // match display.handle.get_vcp_feature(0x12) { - // Ok(value) => { - // config.max_contrast = value.maximum(); - // config.min_contrast = 0; - // config.contrast = value.value(); - // } - // Err(_) => {} - // }; - // match display.handle.get_vcp_feature(0xdc) { - // Ok(value) => { - // config.max_mode = value.maximum(); - // config.min_mode = 0; - // config.mode = value.value(); - // } - // Err(_) => {} - // }; - - // Ok(config) - // } - // None => anyhow::bail!("display#{} is missed.", index), - // } - // } - - // async fn get_display(&self, index: usize) -> anyhow::Result> { - // let mut displays = self.displays.lock().await; - // match displays.get_mut(&index) { - // Some(config) => { - // let mut config = config.to_owned().lock_owned().await; - // if config.last_modified_at > SystemTime::now().sub(Duration::from_secs(10)) { - // info!("cached"); - // return Ok(config); - // } - // return match Self::read_display_config_by_ddc(index) { - // Ok(config) => { - // let id = config.id; - // let value = Arc::new(Mutex::new(config)); - // let valueGuard = value.clone().lock_owned().await; - // displays.insert(id, value); - // info!("read form ddc"); - // Ok(valueGuard) - // } - // Err(err) => { - // warn!( - // "can not read config from display by ddc, use CACHED value. {:?}", - // err - // ); - // config.last_modified_at = SystemTime::now(); - // Ok(config) - // } - // }; - // } - // None => { - // let config = Self::read_display_config_by_ddc(index).map_err(|err| { - // anyhow::anyhow!( - // "can not read config from display by ddc,use DEFAULT value. {:?}", - // err - // ) - // })?; - // let id = config.id; - // let value = Arc::new(Mutex::new(config)); - // let valueGuard = value.clone().lock_owned().await; - // displays.insert(id, value); - // Ok(valueGuard) - // } - // } - // } - - // pub async fn set_display_brightness( - // &self, - // display_brightness: DisplayBrightness, - // ) -> anyhow::Result<()> { - // match Display::enumerate().get_mut(display_brightness.display_index) { - // Some(display) => { - // match self.get_display(display_brightness.display_index).await { - // Ok(mut config) => { - // let curr = config.brightness; - // info!("curr_brightness: {:?}", curr); - // let mut target = match display_brightness.brightness { - // Brightness::Relative(v) => curr.wrapping_add_signed(v), - // Brightness::Absolute(v) => v, - // }; - // if target.gt(&config.max_brightness) { - // target = config.max_brightness; - // } else if target.lt(&config.min_brightness) { - // target = config.min_brightness; - // } - // config.brightness = target; - // display - // .handle - // .set_vcp_feature(0x10, target as u16) - // .map_err(|err| anyhow::anyhow!("can not set brightness. {:?}", err))?; - - // let rpc = rpc::Manager::global().await; - - // rpc.publish_desktop_cmd( - // format!("display{}/brightness", display_brightness.display_index) - // .as_str(), - // target.to_be_bytes().to_vec(), - // ) - // .await; - // } - // Err(err) => { - // info!( - // "can not get display#{} brightness. {:?}", - // display_brightness.display_index, err - // ); - // if let Brightness::Absolute(v) = display_brightness.brightness { - // display.handle.set_vcp_feature(0x10, v).map_err(|err| { - // anyhow::anyhow!("can not set brightness. {:?}", err) - // })?; - // }; - // } - // }; - // } - // None => { - // warn!("display#{} is not found.", display_brightness.display_index); - // } - // } - // Ok(()) - // } } impl Drop for DisplayManager {