From 5ad720d81e57be1b45e78df9204709af6c4cdc35 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 15 Nov 2021 20:56:14 +0100 Subject: namespace ioctl related symbols --- src/lib.rs | 98 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 99fc821..03c9524 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,55 @@ fn cvt_r(f: &mut dyn FnMut() -> T) -> io::Result } } +/** Wrappers for ``ioctl_console(2)`` functionality. */ +pub mod ioctl +{ + use super::{cvt_r, Palette}; + use libc::ioctl; + use std::{io, os::unix::io::AsRawFd}; + + /* XXX: can we get these into ``libc``? */ + pub const KDGKBTYPE: libc::c_ulong = 0x4b33; /* kd.h */ + pub const GIO_CMAP: libc::c_ulong = 0x00004B70; /* kd.h */ + pub const PIO_CMAP: libc::c_ulong = 0x00004B71; /* kd.h */ + pub const KB_101: libc::c_char = 0x0002; /* kd.h */ + + pub fn pio_cmap(fd: &F, pal: &Palette) -> io::Result<()> + { + /* cvt_r because technically it can’t be ruled out that we hit EINTR. */ + cvt_r(&mut || { + unsafe { + ioctl( + fd.as_raw_fd(), + PIO_CMAP, + std::mem::transmute::<&Palette, *const libc::c_void>(&pal), + ) + } + }) + .map(|_| ()) + } + + pub fn gio_cmap(fd: &F) -> io::Result + { + let mut pal = Palette::new(); + + /* cvt_r because technically it can’t be ruled out that we hit EINTR. */ + cvt_r(&mut || { + unsafe { + ioctl( + fd.as_raw_fd(), + GIO_CMAP, + std::mem::transmute::<&mut Palette, *mut libc::c_void>( + &mut pal, + ), + ) + } + }) + .map(|_| ())?; + Ok(pal) + } +} + #[derive(Debug)] pub struct Fd(libc::c_int); @@ -67,12 +116,6 @@ impl fmt::Display for Fd const PALETTE_SIZE: usize = 16; const PALETTE_BYTES: usize = PALETTE_SIZE * 3; /* 16 * sizeof(int) */ -/* XXX: can we get these into ``libc``? */ -const KDGKBTYPE: libc::c_ulong = 0x4b33; /* kd.h */ -const GIO_CMAP: libc::c_ulong = 0x00004B70; /* kd.h */ -const PIO_CMAP: libc::c_ulong = 0x00004B71; /* kd.h */ -const KB_101: libc::c_char = 0x0002; /* kd.h */ - const RAW_COLEXPR_SIZE: usize = 6; /* e. g. 0xBADF00 */ pub type RawPalette<'a> = [&'a str; PALETTE_SIZE]; @@ -551,41 +594,6 @@ impl From<&RawPalette<'_>> for Palette } } -pub fn ioctl_pio_cmap(fd: &F, pal: &Palette) -> io::Result<()> -{ - /* cvt_r because technically it can’t be ruled out that we hit EINTR. */ - cvt_r(&mut || { - unsafe { - ioctl( - fd.as_raw_fd(), - PIO_CMAP, - std::mem::transmute::<&Palette, *const libc::c_void>(&pal), - ) - } - }) - .map(|_| ()) -} - -pub fn ioctl_gio_cmap(fd: &F) -> io::Result -{ - let mut pal = Palette::new(); - - /* cvt_r because technically it can’t be ruled out that we hit EINTR. */ - cvt_r(&mut || { - unsafe { - ioctl( - fd.as_raw_fd(), - GIO_CMAP, - std::mem::transmute::<&mut Palette, *mut libc::c_void>( - &mut pal, - ), - ) - } - }) - .map(|_| ())?; - Ok(pal) -} - const CONSOLE_PATHS: [&str; 6] = [ "/proc/self/fd/0", "/dev/tty", @@ -611,11 +619,11 @@ impl Console let mut tty_type: libc::c_char = 0; let _ = cvt_r(&mut || unsafe { - ioctl(fd, KDGKBTYPE, &mut tty_type as *mut _) + ioctl(fd, ioctl::KDGKBTYPE, &mut tty_type as *mut _) })?; /* Sanity check. */ - if tty_type != KB_101 { + if tty_type != ioctl::KB_101 { return Err(io::Error::new( io::ErrorKind::Other, format!( @@ -680,14 +688,14 @@ impl Console #[inline] pub fn current_palette(&self) -> io::Result { - ioctl_gio_cmap(&self.as_raw_fd()) + ioctl::gio_cmap(&self.as_raw_fd()) } /** Tell the kernel to use the specified palette on the console. */ #[inline] pub fn apply_palette(&self, pal: &Palette) -> io::Result<()> { - ioctl_pio_cmap(&self.as_raw_fd(), pal) + ioctl::pio_cmap(&self.as_raw_fd(), pal) } /** Read the current palette and determine the scheme. */ -- cgit v1.2.3