diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 132 |
1 files changed, 71 insertions, 61 deletions
@@ -46,7 +46,7 @@ fn cvt_r<T: IsMinusOne>(f: &mut dyn FnMut() -> T) -> io::Result<T> /** 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<KbLeds<GET, SET>> + pub fn kdgetled<F: AsRawFd>(fd: &F) -> Result<KbLedState> { 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<KbLeds<GET, SET>>, - ) -> Result<()> + pub fn kdsetled<F: AsRawFd>(fd: &F, state: Option<KbLedState>) + -> 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<F: AsRawFd>(fd: &F) -> Result<KbLedFlags> + { + 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<F: AsRawFd>(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<F: AsRawFd>(fd: &F) -> Result<libc::c_char> { 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<const GET: libc::c_ulong, const SET: libc::c_ulong>(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<KDGETLED, KDSETLED>; -pub type KbLedFlags = KbLeds<KDGKBLED, KDSKBLED>; +pub type KbLedFlags = KbLeds; +pub type KbLedState = KbLeds; -impl<const GET: libc::c_ulong, const SET: libc::c_ulong> KbLeds<GET, SET> +impl KbLeds { pub fn new(cap: bool, num: bool, scr: bool) -> Self { @@ -203,21 +221,18 @@ impl<const GET: libc::c_ulong, const SET: libc::c_ulong> KbLeds<GET, SET> } #[inline] - pub fn get(con: &Console) -> io::Result<Self> - { - ioctl::kdg__led::<Console, GET, SET>(con) - } + pub fn get(con: &Console) -> io::Result<Self> { ioctl::kdgetled(con) } #[inline] pub fn set(&self, con: &Console) -> io::Result<()> { - ioctl::kds__led::<Console, GET, SET>(con, Some(*self)) + ioctl::kdsetled(con, Some(*self)) } #[inline] pub fn revert(con: &Console) -> io::Result<()> { - ioctl::kds__led::<Console, GET, SET>(con, None) + ioctl::kdsetled(con, None) } #[inline] @@ -251,8 +266,7 @@ impl<const GET: libc::c_ulong, const SET: libc::c_ulong> KbLeds<GET, SET> } } -impl<const GET: libc::c_ulong, const SET: libc::c_ulong> fmt::Display - for KbLeds<GET, SET> +impl fmt::Display for KbLeds { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -266,8 +280,7 @@ impl<const GET: libc::c_ulong, const SET: libc::c_ulong> fmt::Display } } -impl<const GET: libc::c_ulong, const SET: libc::c_ulong> From<libc::c_char> - for KbLeds<GET, SET> +impl From<libc::c_char> for KbLeds { fn from(leds: libc::c_char) -> Self { @@ -275,20 +288,17 @@ impl<const GET: libc::c_ulong, const SET: libc::c_ulong> From<libc::c_char> } } -impl<const GET: libc::c_ulong, const SET: libc::c_ulong> From<KbLeds<GET, SET>> - for libc::c_ulong +impl From<KbLeds> for libc::c_ulong { - fn from(state: KbLeds<GET, SET>) -> Self { state.0 as libc::c_ulong } + fn from(state: KbLeds) -> Self { state.0 as libc::c_ulong } } -impl<const GET: libc::c_ulong, const SET: libc::c_ulong> From<KbLeds<GET, SET>> - for u8 +impl From<KbLeds> for u8 { - fn from(state: KbLeds<GET, SET>) -> Self { state.0 } + fn from(state: KbLeds) -> Self { state.0 } } -impl<const GET: libc::c_ulong, const SET: libc::c_ulong> TryFrom<u8> - for KbLeds<GET, SET> +impl TryFrom<u8> for KbLeds { type Error = io::Error; @@ -311,22 +321,22 @@ impl<const GET: libc::c_ulong, const SET: libc::c_ulong> TryFrom<u8> #[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()); } } |