From 951df26618973a74d8c68a80634f965c23ba0734 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 12 Dec 2021 00:42:47 +0100 Subject: =?UTF-8?q?bin:=20implement=20=E2=80=9Cleds=20set=E2=80=9D=20subco?= =?UTF-8?q?mmand?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently only supports setting the raw state from an integer value: $ vtcol leds set -8 2 --- src/lib.rs | 28 +++++++++++++++++++++++++++- src/vtcol.rs | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 2ef43e8..6193277 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -160,7 +160,7 @@ pub mod ioctl } } -#[derive(Debug)] +#[derive(Clone, Copy, Debug)] pub struct KbLedState(u8); impl KbLedState @@ -179,6 +179,12 @@ impl KbLedState #[inline] pub fn get(con: &Console) -> io::Result { ioctl::kdgetled(con) } + #[inline] + pub fn set(&self, con: &Console) -> io::Result<()> + { + ioctl::kdsetled(con, Some(*self)) + } + #[inline] pub fn cap(&self) -> bool { (self.0 & 0x4) != 0 } @@ -221,6 +227,26 @@ impl From for u8 fn from(state: KbLedState) -> Self { state.0 } } +impl TryFrom for KbLedState +{ + type Error = io::Error; + + fn try_from(val: u8) -> io::Result + { + if val <= 0b111 { + Ok(Self(val)) + } else { + Err(io::Error::new( + io::ErrorKind::Other, + format!( + "invalid raw led value: {:#b}; must not exceed 3 b", + val + ), + )) + } + } +} + #[cfg(test)] mod kb_led_state { diff --git a/src/vtcol.rs b/src/vtcol.rs index e9f9c2b..2fdec10 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -24,7 +24,7 @@ enum LedJob /** Get keyboard LED state. */ Get(bool), /** Set keyboard LED state. */ - Set, + Set(KbLedState), } #[derive(Debug)] @@ -212,15 +212,27 @@ impl<'a> Job .about("operations regarding keyboard LEDs") .subcommand( SubCommand::with_name("get") - .about("get keyboard LED state") + .about("get LED state") .arg( Arg::with_name("u8") - .value_name("NAME") .short("8") .long("u8") .help("output raw state as integer") .takes_value(false), ), + ) + .subcommand( + SubCommand::with_name("set") + .about("set LED state") + .arg( + Arg::with_name("u8") + .short("8") + .long("u8") + .help("provide desired state as integer") + .takes_value(true) + .required(true) + .value_name("STATE"), + ), ), ); @@ -336,6 +348,11 @@ impl<'a> Job let raw = subm.is_present("u8"); Ok(Self::Leds(LedJob::Get(raw))) }, + ("set", Some(subm)) => { + let st: u8 = subm.value_of("u8").unwrap().parse()?; + let st = KbLedState::try_from(st)?; + Ok(Self::Leds(LedJob::Set(st))) + }, (junk, _) => Err(anyhow!( "invalid sub-subcommand to leds: [{}]; try ``{} \ @@ -413,7 +430,7 @@ impl<'a> Job Self::Colors(ColorJob::Fade(from, to, ms, hz, clear)) => Self::fade(from, to, ms, hz, clear)?, Self::Leds(LedJob::Get(raw)) => Self::get_leds(raw)?, - Self::Leds(LedJob::Set) => unimplemented!(), + Self::Leds(LedJob::Set(st)) => Self::set_leds(st)?, } Ok(()) @@ -490,7 +507,7 @@ impl<'a> Job Ok(()) } - /** Get the keyboard led state. */ + /** Get the keyboard LED state. */ fn get_leds(raw: bool) -> Result<()> { let fd = Console::current()?; @@ -506,6 +523,18 @@ impl<'a> Job Ok(()) } + + /** Set the keyboard LED state. */ + fn set_leds(st: KbLedState) -> Result<()> + { + let fd = Console::current()?; + vrb!("console fd: {}", fd); + + st.set(&fd)?; + vrb!("applied"); + + Ok(()) + } } /* [impl Job] */ fn main() -> Result<()> -- cgit v1.2.3