diff options
author | Philipp Gesang <phg@phi-gamma.net> | 2021-11-15 20:56:14 +0100 |
---|---|---|
committer | Philipp Gesang <phg@phi-gamma.net> | 2021-11-15 20:14:18 +0100 |
commit | 5ad720d81e57be1b45e78df9204709af6c4cdc35 (patch) | |
tree | 83d55ae44cdcb13129a9aa92f9259a5b84346eda | |
parent | 695355b4882707a9f7ed0917d5188471f67f47b1 (diff) | |
download | vtcol-5ad720d81e57be1b45e78df9204709af6c4cdc35.tar.gz |
namespace ioctl related symbols
-rw-r--r-- | src/lib.rs | 98 |
1 files changed, 53 insertions, 45 deletions
@@ -43,6 +43,55 @@ 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, 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<F: AsRawFd>(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<F: AsRawFd>(fd: &F) -> io::Result<Palette> + { + 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<F: AsRawFd>(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<F: AsRawFd>(fd: &F) -> io::Result<Palette> -{ - 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<Palette> { - 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. */ |