From 11156b67778ce87a24611b918b60fe970b979755 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 12 Dec 2021 00:56:26 +0100 Subject: bin: implement led revert Command: $ vtcol leds set --revert Implementing this detail of KDSETLED: [I]f a higher order bit is set, the LEDs revert to normal: displaying the state of the keyboard functions of caps lock, num lock, and scroll lock. --- src/lib.rs | 3 +++ src/vtcol.rs | 30 +++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6193277..0669b87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -185,6 +185,9 @@ impl KbLedState ioctl::kdsetled(con, Some(*self)) } + #[inline] + pub fn revert(con: &Console) -> io::Result<()> { ioctl::kdsetled(con, None) } + #[inline] pub fn cap(&self) -> bool { (self.0 & 0x4) != 0 } diff --git a/src/vtcol.rs b/src/vtcol.rs index 2fdec10..738a622 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -23,8 +23,8 @@ enum LedJob { /** Get keyboard LED state. */ Get(bool), - /** Set keyboard LED state. */ - Set(KbLedState), + /** Set keyboard LED state; ``None`` indicates revert. */ + Set(Option), } #[derive(Debug)] @@ -230,8 +230,16 @@ impl<'a> Job .long("u8") .help("provide desired state as integer") .takes_value(true) - .required(true) + .required_unless_one(&["revert"]) .value_name("STATE"), + ) + .arg( + Arg::with_name("revert") + .short("r") + .long("revert") + .help("revert LED state to normal") + .takes_value(false) + .conflicts_with("u8"), ), ), ); @@ -349,9 +357,12 @@ impl<'a> Job Ok(Self::Leds(LedJob::Get(raw))) }, ("set", Some(subm)) => { + if subm.is_present("revert") { + return Ok(Self::Leds(LedJob::Set(None))); + } let st: u8 = subm.value_of("u8").unwrap().parse()?; let st = KbLedState::try_from(st)?; - Ok(Self::Leds(LedJob::Set(st))) + Ok(Self::Leds(LedJob::Set(Some(st)))) }, (junk, _) => Err(anyhow!( @@ -525,13 +536,18 @@ impl<'a> Job } /** Set the keyboard LED state. */ - fn set_leds(st: KbLedState) -> Result<()> + fn set_leds(st: Option) -> Result<()> { let fd = Console::current()?; vrb!("console fd: {}", fd); - st.set(&fd)?; - vrb!("applied"); + if let Some(st) = st { + st.set(&fd)?; + vrb!("applied"); + } else { + KbLedState::revert(&fd)?; + vrb!("reverted"); + } Ok(()) } -- cgit v1.2.3