From 3a430716d6527b2e6a0e5835a276ce26839b1159 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 7 May 2023 09:56:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=99=A8=E9=85=8D=E7=BD=AE=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 | 67 ++++++++++++++++++++++++ src-tauri/src/display/display_state.rs | 4 +- src-tauri/src/display/manager.rs | 22 ++++---- src-tauri/src/display/mod.rs | 1 + 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 src-tauri/src/display/display_handler.rs diff --git a/src-tauri/src/display/display_handler.rs b/src-tauri/src/display/display_handler.rs new file mode 100644 index 0000000..69e3a91 --- /dev/null +++ b/src-tauri/src/display/display_handler.rs @@ -0,0 +1,67 @@ +use std::{sync::Arc, time::SystemTime}; + +use ddc_hi::{Ddc, Display}; +use tokio::sync::RwLock; + +use super::DisplayState; + +pub struct DisplayHandler { + pub state: Arc>, + pub controller: Arc>, +} + +impl DisplayHandler { + pub async fn fetch_state(&self) { + let mut controller = self.controller.write().await; + + let mut temp_state = DisplayState::default(); + + match controller.handle.get_vcp_feature(0x10) { + Ok(value) => { + temp_state.max_brightness = value.maximum(); + temp_state.min_brightness = 0; + temp_state.brightness = value.value(); + } + Err(_) => {} + }; + match controller.handle.get_vcp_feature(0x12) { + Ok(value) => { + temp_state.max_contrast = value.maximum(); + temp_state.min_contrast = 0; + temp_state.contrast = value.value(); + } + Err(_) => {} + }; + match controller.handle.get_vcp_feature(0xdc) { + Ok(value) => { + temp_state.max_mode = value.maximum(); + temp_state.min_mode = 0; + temp_state.mode = value.value(); + } + Err(_) => {} + }; + + temp_state.last_fetched_at = SystemTime::now(); + + let mut state = self.state.write().await; + *state = temp_state; + } + + pub async fn set_brightness(&self, brightness: u16) { + let mut state = self.state.write().await; + state.brightness = brightness; + state.last_modified_at = SystemTime::now(); + } + + pub async fn set_contrast(&self, contrast: u16) { + let mut state = self.state.write().await; + state.contrast = contrast; + state.last_modified_at = SystemTime::now(); + } + + pub async fn set_mode(&self, mode: u16) { + let mut state = self.state.write().await; + state.mode = mode; + state.last_modified_at = SystemTime::now(); + } +} diff --git a/src-tauri/src/display/display_state.rs b/src-tauri/src/display/display_state.rs index 315df69..d70fc18 100644 --- a/src-tauri/src/display/display_state.rs +++ b/src-tauri/src/display/display_state.rs @@ -14,6 +14,7 @@ pub struct DisplayState { pub max_mode: u16, pub min_mode: u16, pub last_modified_at: SystemTime, + pub last_fetched_at: SystemTime, } impl DisplayState { @@ -22,13 +23,14 @@ impl DisplayState { brightness: 30, contrast: 50, mode: 0, - last_modified_at: SystemTime::now(), + last_modified_at: SystemTime::UNIX_EPOCH, max_brightness: 100, min_brightness: 0, max_contrast: 100, min_contrast: 0, max_mode: 15, min_mode: 0, + last_fetched_at: SystemTime::UNIX_EPOCH, } } } diff --git a/src-tauri/src/display/manager.rs b/src-tauri/src/display/manager.rs index 7481cbc..73424b7 100644 --- a/src-tauri/src/display/manager.rs +++ b/src-tauri/src/display/manager.rs @@ -1,22 +1,15 @@ use std::{ - borrow::Borrow, - collections::HashMap, - ops::Sub, sync::Arc, time::{Duration, SystemTime}, }; use ddc_hi::Display; use paris::{error, info, warn}; -use tokio::sync::{OnceCell, OwnedMutexGuard, RwLock}; +use tokio::sync::{OnceCell, RwLock}; + +use super::{display_state::DisplayState, display_handler::DisplayHandler}; -use super::display_state::DisplayState; -use ddc_hi::Ddc; -pub struct DisplayHandler { - pub state: Arc>, - pub controller: Arc>, -} pub struct DisplayManager { displays: Arc>>>>, @@ -46,7 +39,14 @@ impl DisplayManager { for display in controllers { let controller = Arc::new(RwLock::new(display)); let state = Arc::new(RwLock::new(DisplayState::default())); - displays.push(Arc::new(RwLock::new(DisplayHandler { controller, state }))); + let handler = DisplayHandler { + state: state.clone(), + controller: controller.clone(), + }; + + handler.fetch_state().await; + + displays.push(Arc::new(RwLock::new(handler))); } } diff --git a/src-tauri/src/display/mod.rs b/src-tauri/src/display/mod.rs index 8499385..4cc5e2b 100644 --- a/src-tauri/src/display/mod.rs +++ b/src-tauri/src/display/mod.rs @@ -2,6 +2,7 @@ // mod manager; mod display_state; mod manager; +mod display_handler; pub use display_state::*;