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 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/lib.rs') 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 { -- cgit v1.2.3