diff --git a/.idea/dictionaries/luke.xml b/.idea/dictionaries/luke.xml index 8d06eb22..4b09ad05 100644 --- a/.idea/dictionaries/luke.xml +++ b/.idea/dictionaries/luke.xml @@ -1,6 +1,7 @@ + backlight hotkey diff --git a/Cargo.lock b/Cargo.lock index b0fa6d65..be9995b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,6 +125,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "numtoa" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e521b6adefa0b2c1fa5d2abdf9a5216288686fe6146249215d884c0e5ab320b0" + [[package]] name = "pkg-config" version = "0.3.17" @@ -180,6 +186,7 @@ dependencies = [ "rusb", "serde", "serde_derive", + "sysfs-class", "toml", ] @@ -222,6 +229,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "sysfs-class" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "718e4a5d0e144ff01e05bb8618a4997d8901cdec74cf25826bca2535ef406f3e" +dependencies = [ + "numtoa", +] + [[package]] name = "take_mut" version = "0.2.2" diff --git a/rog-lib/Cargo.toml b/rog-lib/Cargo.toml index a5de50d8..cf7c59b8 100644 --- a/rog-lib/Cargo.toml +++ b/rog-lib/Cargo.toml @@ -9,4 +9,5 @@ rusb = "0.5" gumdrop = "0.8" serde = "1.0" serde_derive = "1.0" -toml = "0.5" \ No newline at end of file +toml = "0.5" +sysfs-class = "0.1.2" \ No newline at end of file diff --git a/rog-lib/src/core.rs b/rog-lib/src/core.rs index 93906dd7..f10d91da 100644 --- a/rog-lib/src/core.rs +++ b/rog-lib/src/core.rs @@ -4,6 +4,8 @@ use rusb::{DeviceHandle, Error}; use std::cell::{Ref, RefCell}; use std::str::FromStr; use std::time::Duration; +use sysfs_class::Brightness; +use sysfs_class::SysClass; pub const LED_MSG_LEN: usize = 17; static LED_INIT1: [u8; 2] = [0x5d, 0xb9]; @@ -165,15 +167,6 @@ impl RogCore { Err(Error::NotSupported) } - // pub fn load_config(&mut self) -> Result<(), Error> { - // if let Some(current) = self.config.get_current() { - // self.aura_set_and_save(¤t)?; - // } - // let bright = RogCore::aura_brightness_bytes(self.config.brightness)?; - // self.aura_set_and_save(&bright)?; - // Ok(()) - // } - pub fn poll_keyboard(&self, buf: &mut [u8; 32]) -> Result, Error> { match self .handle @@ -192,3 +185,43 @@ impl RogCore { Ok(None) } } + +pub struct Backlight { + backlight: sysfs_class::Backlight, + step: u64, + max: u64, +} + +impl Backlight { + pub fn new(id: &str) -> Result { + for bl in sysfs_class::Backlight::iter() { + let bl = bl?; + if bl.id() == id { + let max = bl.max_brightness()?; + let step = max / 50; + return Ok(Backlight { + backlight: bl, + step, + max, + }); + } + } + panic!("Backlight not found") + } + pub fn step_up(&self) { + let brightness = self.backlight.brightness().unwrap(); + if brightness + self.step <= self.max { + self.backlight + .set_brightness(brightness + self.step) + .unwrap(); + } + } + pub fn step_down(&self) { + let brightness = self.backlight.brightness().unwrap(); + if brightness > self.step { + self.backlight + .set_brightness(brightness - self.step) + .unwrap(); + } + } +} diff --git a/rog-lib/src/laptops.rs b/rog-lib/src/laptops.rs index edad49db..ce3b7b2d 100644 --- a/rog-lib/src/laptops.rs +++ b/rog-lib/src/laptops.rs @@ -1,15 +1,11 @@ use crate::aura::BuiltInModeByte; -use crate::core::RogCore; +use crate::core::{Backlight, RogCore}; // ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="gdbus call // --session --dest org.gnome.SettingsDaemon.Power // --object-path /org/gnome/SettingsDaemon/Power // --method org.freedesktop.DBus.Properties.Set org.gnome.SettingsDaemon.Power.Screen Brightness ''" -const POWER_BUS_NAME: &'static str = "org.gnome.SettingsDaemon.Power"; -const POWER_IFACE_NAME: &'static str = "org.gnome.SettingsDaemon.Power.Screen"; -const POWER_PATH: &'static str = "/org/gnome/SettingsDaemon/Power"; - pub trait Laptop { fn do_hotkey_action(&self, core: &mut RogCore, key_byte: u8); fn hotkey_group_byte(&self) -> u8; @@ -19,6 +15,7 @@ pub trait Laptop { fn board_name(&self) -> &str; fn prod_family(&self) -> &str; } + pub struct LaptopGX502GW { usb_vendor: u16, usb_product: u16, @@ -28,9 +25,12 @@ pub struct LaptopGX502GW { min_bright: u8, max_bright: u8, supported_modes: Vec, + backlight: Backlight, } + impl LaptopGX502GW { pub fn new() -> Self { + // Find backlight LaptopGX502GW { usb_vendor: 0x0B05, usb_product: 0x1866, @@ -53,6 +53,7 @@ impl LaptopGX502GW { BuiltInModeByte::ThinZoomy, BuiltInModeByte::WideZoomy, ], + backlight: Backlight::new("intel_backlight").unwrap(), } } } @@ -107,7 +108,12 @@ impl Laptop for LaptopGX502GW { rogcore.config().write(); } } - GX502GWKeys::ScreenBrightUp => {} + GX502GWKeys::ScreenBrightUp => { + self.backlight.step_up(); + } + GX502GWKeys::ScreenBrightDown => { + self.backlight.step_down(); + } _ => { if key_byte != 0 { dbg!(&key_byte);