From c43c6aa562c5222477f66d50179e368ca0f25a9b Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 15 Nov 2021 20:55:30 +0100 Subject: turn Fd into proper wrapper --- src/lib.rs | 30 +++++++++++++++++++++++++----- src/vtcol.rs | 30 ++++++++++++++---------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 47f650d..f8bef99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,9 +2,29 @@ use libc::ioctl; use std::{convert::TryFrom, fmt, io::{self, BufWriter, Error, Write}, + os::unix::io::{AsRawFd, RawFd}, path::{Path, PathBuf}}; -pub type Fd = libc::c_int; +#[derive(Debug)] +pub struct Fd(libc::c_int); + +impl From for Fd +{ + fn from(fd: libc::c_int) -> Self { Self(fd) } +} + +impl AsRawFd for Fd +{ + fn as_raw_fd(&self) -> RawFd { self.0 } +} + +impl fmt::Display for Fd +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result + { + write!(f, "Fd({})", self.0) + } +} const PALETTE_SIZE: usize = 16; const PALETTE_BYTES: usize = PALETTE_SIZE * 3; /* 16 * sizeof(int) */ @@ -493,11 +513,11 @@ impl From<&RawPalette<'_>> for Palette } } -pub fn ioctl_pio_cmap(fd: Fd, pal: &Palette) -> io::Result<()> +pub fn ioctl_pio_cmap(fd: &F, pal: &Palette) -> io::Result<()> { if unsafe { ioctl( - fd, + fd.as_raw_fd(), PIO_CMAP, std::mem::transmute::<&Palette, *const libc::c_void>(&pal), ) @@ -509,13 +529,13 @@ pub fn ioctl_pio_cmap(fd: Fd, pal: &Palette) -> io::Result<()> } } -pub fn ioctl_gio_cmap(fd: Fd) -> io::Result +pub fn ioctl_gio_cmap(fd: &F) -> io::Result { let mut pal = Palette::new(); if unsafe { ioctl( - fd, + fd.as_raw_fd(), GIO_CMAP, std::mem::transmute::<&mut Palette, *mut libc::c_void>(&mut pal), ) diff --git a/src/vtcol.rs b/src/vtcol.rs index 2c41d7b..0cc713c 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -1,9 +1,10 @@ pub mod lib; -use vtcol::{Palette, Scheme}; +use vtcol::{Fd, Palette, Scheme}; use anyhow::{anyhow, Result}; use std::{io::{self, BufWriter, Error}, + os::unix::io::AsRawFd, sync::atomic::{AtomicBool, Ordering}}; static VERBOSITY: AtomicBool = AtomicBool::new(false); @@ -225,9 +226,9 @@ impl<'a> Job let fd = get_console_fd()?; vrb!("console fd: {}", fd); - vtcol::ioctl_pio_cmap(fd, &pal)?; + vtcol::ioctl_pio_cmap(&fd, &pal)?; - clear_term(fd)?; + clear_term(&fd)?; vrb!("successfully enabled scheme {:?}", scheme); /* It’s fine to leak the fd, the kernel will clean up anyways. */ Ok(()) @@ -238,7 +239,7 @@ impl<'a> Job let fd = get_console_fd()?; vrb!("console fd: {}", fd); - let pal = vtcol::ioctl_gio_cmap(fd)?; + let pal = vtcol::ioctl_gio_cmap(&fd)?; let scm = Scheme::from(pal); vrb!("active scheme:"); @@ -255,7 +256,7 @@ impl<'a> Job let fd = get_console_fd()?; vrb!("console fd: {}", fd); - let pal = vtcol::ioctl_gio_cmap(fd)?; + let pal = vtcol::ioctl_gio_cmap(&fd)?; if pal == Palette::from(&one) { Self::set_scheme(two) @@ -289,11 +290,7 @@ fn fd_of_path(path: &std::path::Path) -> Option let mut tty_type: libc::c_char = 0; let res = unsafe { - libc::ioctl( - fd, - vtcol::KDGKBTYPE, - &mut tty_type as *mut _ - ) + libc::ioctl(fd, vtcol::KDGKBTYPE, &mut tty_type as *mut _) }; if res < 0 { vrb!(" *> ioctl failed"); @@ -304,12 +301,12 @@ fn fd_of_path(path: &std::path::Path) -> Option return None; } - Some(fd) + Some(Fd::from(fd)) }, } } -fn get_console_fd() -> Result +fn get_console_fd() -> Result { for path in CONSOLE_PATHS.iter() { vrb!("trying path: {:?}", path); @@ -322,12 +319,13 @@ fn get_console_fd() -> Result Err(anyhow!("could not retrieve fd for any of the search paths")) } -fn write_to_term(fd: vtcol::Fd, buf: &str) -> Result<()> +fn write_to_term(fd: &Fd, buf: &str) -> Result<()> { let len = buf.len() as libc::size_t; - if unsafe { libc::write(fd, buf.as_ptr() as *const libc::c_void, len) } - != len as isize + if unsafe { + libc::write(fd.as_raw_fd(), buf.as_ptr() as *const libc::c_void, len) + } != len as isize { Err(anyhow!( "failed to write {} B to fd {}: {}", @@ -340,7 +338,7 @@ fn write_to_term(fd: vtcol::Fd, buf: &str) -> Result<()> } } -fn clear_term(fd: vtcol::Fd) -> Result<()> +fn clear_term(fd: &Fd) -> Result<()> { let clear = "\x1b[2J"; let cursor = "\x1b[1;1H"; -- cgit v1.2.3