From 52c4bc83135f8749a33d360e2e360a3d93552826 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 22 Dec 2021 13:42:45 +0100 Subject: =?UTF-8?q?Revert=20"lib:=20implement=20=E2=80=9Ckb=20flags?= =?UTF-8?q?=E2=80=9D"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This partially reverts commit 92f35f8e90199e4d9dfbcc545be456f4c2d7a1ce. The keyboard modifiers behave too differently from the LED state for the const generics to pay off. --- src/lib.rs | 132 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 71 insertions(+), 61 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5d777de..01f0187 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,7 @@ fn cvt_r(f: &mut dyn FnMut() -> T) -> io::Result /** Wrappers for ``ioctl_console(2)`` functionality. */ pub mod ioctl { - use super::{cvt_r, KbLeds, Palette}; + use super::{cvt_r, KbLedFlags, KbLedState, Palette}; use libc::ioctl; use std::{io::Result, os::unix::io::AsRawFd}; @@ -96,14 +96,7 @@ pub mod ioctl Ok(pal) } - #[allow(non_snake_case)] - pub fn kdg__led< - F: AsRawFd, - const GET: libc::c_ulong, - const SET: libc::c_ulong, - >( - fd: &F, - ) -> Result> + pub fn kdgetled(fd: &F) -> Result { let mut leds: libc::c_char = 0; @@ -111,7 +104,7 @@ pub mod ioctl unsafe { ioctl( fd.as_raw_fd(), - GET, + KDGETLED, std::mem::transmute::<&mut libc::c_char, *mut libc::c_void>( &mut leds, ), @@ -120,7 +113,7 @@ pub mod ioctl }) .map(|_| ())?; - Ok(KbLeds::from(leds)) + Ok(KbLedState::from(leds)) } /** If ``state`` is ``None`` it is taken to mean “revert to normal” as per @@ -132,15 +125,8 @@ pub mod ioctl order bit is set, the LEDs revert to normal: displaying the state of the keyboard functions of caps lock, num lock, and scroll lock. */ - #[allow(non_snake_case)] - pub fn kds__led< - F: AsRawFd, - const GET: libc::c_ulong, - const SET: libc::c_ulong, - >( - fd: &F, - state: Option>, - ) -> Result<()> + pub fn kdsetled(fd: &F, state: Option) + -> Result<()> { let leds: libc::c_ulong = if let Some(state) = state { state.into() @@ -152,7 +138,7 @@ pub mod ioctl unsafe { ioctl( fd.as_raw_fd(), - SET, + KDSETLED, std::mem::transmute::<&libc::c_ulong, *const libc::c_void>( &leds, ), @@ -164,6 +150,44 @@ pub mod ioctl Ok(()) } + pub fn kdgkbled(fd: &F) -> Result + { + let mut flags: libc::c_char = 0; + + cvt_r(&mut || { + unsafe { + ioctl( + fd.as_raw_fd(), + KDGKBLED, + std::mem::transmute::<&mut libc::c_char, *mut libc::c_void>( + &mut flags, + ), + ) + } + }) + .map(|_| ())?; + + Ok(KbLedFlags::from(flags)) + } + + pub fn kdskbled(fd: &F, flags: KbLedFlags) -> Result<()> + { + let flags = libc::c_ulong::from(flags); + + cvt_r(&mut || { + unsafe { + ioctl( + fd.as_raw_fd(), + KDSKBLED, + std::mem::transmute::<&libc::c_ulong, *const libc::c_void>( + &flags, + ), + ) + } + }) + .map(|_| ()) + } + pub fn kdgkbtype(fd: &F) -> Result { let mut kb: libc::c_char = 0; @@ -178,18 +202,12 @@ pub mod ioctl /** Base type which the LED state and flags are aliases of. */ #[derive(Clone, Copy, Debug)] -pub struct KbLeds(u8); - -/* XXX why do we need this? the constants from ioctl:: don’t work */ -const KDGETLED: libc::c_ulong = 0x4b31; -const KDSETLED: libc::c_ulong = 0x4b32; -const KDGKBLED: libc::c_ulong = 0x4B64; -const KDSKBLED: libc::c_ulong = 0x4B65; +pub struct KbLeds(u8); -pub type KbLedState = KbLeds; -pub type KbLedFlags = KbLeds; +pub type KbLedFlags = KbLeds; +pub type KbLedState = KbLeds; -impl KbLeds +impl KbLeds { pub fn new(cap: bool, num: bool, scr: bool) -> Self { @@ -203,21 +221,18 @@ impl KbLeds } #[inline] - pub fn get(con: &Console) -> io::Result - { - ioctl::kdg__led::(con) - } + pub fn get(con: &Console) -> io::Result { ioctl::kdgetled(con) } #[inline] pub fn set(&self, con: &Console) -> io::Result<()> { - ioctl::kds__led::(con, Some(*self)) + ioctl::kdsetled(con, Some(*self)) } #[inline] pub fn revert(con: &Console) -> io::Result<()> { - ioctl::kds__led::(con, None) + ioctl::kdsetled(con, None) } #[inline] @@ -251,8 +266,7 @@ impl KbLeds } } -impl fmt::Display - for KbLeds +impl fmt::Display for KbLeds { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -266,8 +280,7 @@ impl fmt::Display } } -impl From - for KbLeds +impl From for KbLeds { fn from(leds: libc::c_char) -> Self { @@ -275,20 +288,17 @@ impl From } } -impl From> - for libc::c_ulong +impl From for libc::c_ulong { - fn from(state: KbLeds) -> Self { state.0 as libc::c_ulong } + fn from(state: KbLeds) -> Self { state.0 as libc::c_ulong } } -impl From> - for u8 +impl From for u8 { - fn from(state: KbLeds) -> Self { state.0 } + fn from(state: KbLeds) -> Self { state.0 } } -impl TryFrom - for KbLeds +impl TryFrom for KbLeds { type Error = io::Error; @@ -311,22 +321,22 @@ impl TryFrom #[cfg(test)] mod kb_led_state { - use super::KbLedState; + use super::KbLeds; #[test] fn create() { - assert_eq!(0u8, KbLedState::new(false, false, false).into()); - assert_eq!(1u8, KbLedState::new(false, false, true).into()); - assert_eq!(2u8, KbLedState::new(false, true, false).into()); - assert_eq!(4u8, KbLedState::new(true, false, false).into()); - assert_eq!(6u8, KbLedState::new(true, true, false).into()); - - assert_eq!(0u8, KbLedState::from(0u8 as libc::c_char).into()); - assert_eq!(1u8, KbLedState::from(1u8 as libc::c_char).into()); - assert_eq!(2u8, KbLedState::from(2u8 as libc::c_char).into()); - assert_eq!(4u8, KbLedState::from(4u8 as libc::c_char).into()); - assert_eq!(6u8, KbLedState::from(6u8 as libc::c_char).into()); + assert_eq!(0u8, KbLeds::new(false, false, false).into()); + assert_eq!(1u8, KbLeds::new(false, false, true).into()); + assert_eq!(2u8, KbLeds::new(false, true, false).into()); + assert_eq!(4u8, KbLeds::new(true, false, false).into()); + assert_eq!(6u8, KbLeds::new(true, true, false).into()); + + assert_eq!(0u8, KbLeds::from(0u8 as libc::c_char).into()); + assert_eq!(1u8, KbLeds::from(1u8 as libc::c_char).into()); + assert_eq!(2u8, KbLeds::from(2u8 as libc::c_char).into()); + assert_eq!(4u8, KbLeds::from(4u8 as libc::c_char).into()); + assert_eq!(6u8, KbLeds::from(6u8 as libc::c_char).into()); } } -- cgit v1.2.3