summaryrefslogtreecommitdiff
path: root/src/vtcol.rs
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2021-11-15 20:50:15 +0100
committerPhilipp Gesang <phg@phi-gamma.net>2021-11-15 20:14:17 +0100
commit695355b4882707a9f7ed0917d5188471f67f47b1 (patch)
tree72633650e4187d6c023c9f01c0d8ef5692d997f9 /src/vtcol.rs
parent3fcadd24b57ef31164b2410be869e998119db56c (diff)
downloadvtcol-695355b4882707a9f7ed0917d5188471f67f47b1.tar.gz
move console handling into lib
All operations are now exposed through wrappers that are member functions of ``vtcol::Console``.
Diffstat (limited to 'src/vtcol.rs')
-rw-r--r--src/vtcol.rs109
1 files changed, 10 insertions, 99 deletions
diff --git a/src/vtcol.rs b/src/vtcol.rs
index 0cc713c..d3399fc 100644
--- a/src/vtcol.rs
+++ b/src/vtcol.rs
@@ -1,10 +1,9 @@
pub mod lib;
-use vtcol::{Fd, Palette, Scheme};
+use vtcol::{Console, Palette, Scheme};
use anyhow::{anyhow, Result};
-use std::{io::{self, BufWriter, Error},
- os::unix::io::AsRawFd,
+use std::{io::{self, BufWriter},
sync::atomic::{AtomicBool, Ordering}};
static VERBOSITY: AtomicBool = AtomicBool::new(false);
@@ -220,15 +219,12 @@ impl<'a> Job
fn set_scheme(scheme: Scheme) -> Result<()>
{
- let pal = Palette::from(&scheme);
- vrb!("Using palette:");
- vrb!("{}", pal);
- let fd = get_console_fd()?;
- vrb!("console fd: {}", fd);
+ let con = Console::current()?;
+ vrb!("console fd: {}", con);
- vtcol::ioctl_pio_cmap(&fd, &pal)?;
+ con.apply_scheme(&scheme)?;
+ con.clear()?;
- clear_term(&fd)?;
vrb!("successfully enabled scheme {:?}", scheme);
/* It’s fine to leak the fd, the kernel will clean up anyways. */
Ok(())
@@ -236,11 +232,10 @@ impl<'a> Job
fn get_scheme() -> Result<()>
{
- let fd = get_console_fd()?;
+ let fd = Console::current()?;
vrb!("console fd: {}", fd);
- let pal = vtcol::ioctl_gio_cmap(&fd)?;
- let scm = Scheme::from(pal);
+ let scm = fd.current_scheme()?;
vrb!("active scheme:");
println!("{}", scm);
@@ -253,12 +248,10 @@ impl<'a> Job
*/
fn toggle_scheme(one: Scheme, two: Scheme) -> Result<()>
{
- let fd = get_console_fd()?;
+ let fd = Console::current()?;
vrb!("console fd: {}", fd);
- let pal = vtcol::ioctl_gio_cmap(&fd)?;
-
- if pal == Palette::from(&one) {
+ if fd.current_palette()? == Palette::from(&one) {
Self::set_scheme(two)
} else {
Self::set_scheme(one)
@@ -266,88 +259,6 @@ impl<'a> Job
}
} /* [impl Job] */
-const CONSOLE_PATHS: [&str; 6] = [
- "/proc/self/fd/0",
- "/dev/tty",
- "/dev/tty0",
- "/dev/vc/0",
- "/dev/systty",
- "/dev/console",
-];
-
-fn fd_of_path(path: &std::path::Path) -> Option<vtcol::Fd>
-{
- let p = std::ffi::CString::new(path.to_str().unwrap()).unwrap();
- match unsafe { libc::open(p.as_ptr(), libc::O_RDWR | libc::O_NOCTTY, 0) } {
- -1 => None,
- fd => {
- vrb!(" *> got fd");
- if unsafe { libc::isatty(fd) } == 0 {
- vrb!(" *> not a tty");
- return None;
- }
-
- let mut tty_type: libc::c_char = 0;
-
- let res = unsafe {
- libc::ioctl(fd, vtcol::KDGKBTYPE, &mut tty_type as *mut _)
- };
- if res < 0 {
- vrb!(" *> ioctl failed");
- return None;
- }
-
- if tty_type != vtcol::KB_101 {
- return None;
- }
-
- Some(Fd::from(fd))
- },
- }
-}
-
-fn get_console_fd() -> Result<Fd>
-{
- for path in CONSOLE_PATHS.iter() {
- vrb!("trying path: {:?}", path);
- let path = std::path::Path::new(path);
- if let Some(fd) = fd_of_path(path) {
- vrb!(" * Success!");
- return Ok(fd);
- }
- }
- Err(anyhow!("could not retrieve fd for any of the search paths"))
-}
-
-fn write_to_term(fd: &Fd, buf: &str) -> Result<()>
-{
- let len = buf.len() as libc::size_t;
-
- 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 {}: {}",
- len,
- fd,
- Error::last_os_error()
- ))
- } else {
- Ok(())
- }
-}
-
-fn clear_term(fd: &Fd) -> Result<()>
-{
- let clear = "\x1b[2J";
- let cursor = "\x1b[1;1H";
- write_to_term(fd, clear)?;
- write_to_term(fd, cursor)?;
-
- Ok(())
-}
-
fn main() -> Result<()>
{
let job = Job::from_argv()?;