1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
// devela::os::linux
//
//!
//
use super::super::{linux_sys_ioctl, LINUX_ERRNO, LINUX_FILENO, LINUX_IOCTL, LINUX_TERMIOS_LFLAG};
use crate::codegen::iif;
use core::ffi::c_uint;
/// Represents the [`termios`] structure from libc,
/// used to control terminal I/O.
///
/// It has fields for input, output, control, and local modes,
/// as well as a line discipline and control characters.
///
/// [`termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[repr(C)]
pub struct LinuxTermios {
/// Input flags.
pub c_iflag: c_uint,
/// Output flags.
pub c_oflag: c_uint,
/// Control flags.
pub c_cflag: c_uint,
/// Local flags.
pub c_lflag: c_uint,
/// …
pub c_line: u8,
/// …
pub c_cc: [u8; 19],
}
unsafe impl bytemuck::NoUninit for LinuxTermios {}
impl LinuxTermios {
/// Returns a new empty struct.
pub const fn new() -> Self {
Self {
c_iflag: 0,
c_oflag: 0,
c_cflag: 0,
c_lflag: 0,
c_line: 0,
c_cc: [0; 19],
}
}
/// Returns a raw byte pointer to self.
pub const fn as_bytes_ptr(&self) -> *const u8 {
self as *const Self as *const u8
}
/// Returns a raw mutable byte pointer to self.
pub fn as_mut_bytes_ptr(&mut self) -> *mut u8 {
self as *mut Self as *mut u8
}
}
#[cfg(all(
any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "riscv32",
target_arch = "riscv64"
),
feature = "unsafe_os",
not(miri),
))]
impl LinuxTermios {
/// Gets the current termios state into `state`.
///
/// # Errors
/// In case of an error returns the [`LINUX_ERRNO`] value from [`linux_sys_ioctl`].
pub fn get_state() -> Result<LinuxTermios, isize> {
let mut state = LinuxTermios::new();
let res = unsafe {
linux_sys_ioctl(
LINUX_FILENO::STDIN,
LINUX_IOCTL::TCGETS,
state.as_mut_bytes_ptr(),
)
};
iif![res >= 0; Ok(state); Err(res)]
}
/// Sets the current termios `state`.
///
/// Returns the [`LINUX_ERRNO`] value from [`linux_sys_ioctl`].
pub fn set_state(mut state: LinuxTermios) -> Result<(), isize> {
let res = unsafe {
linux_sys_ioctl(
LINUX_FILENO::STDIN,
LINUX_IOCTL::TCSETS,
state.as_mut_bytes_ptr(),
)
};
iif![res >= 0; Ok(()); Err(res)]
}
/// Returns `true` if we're in a terminal context.
pub fn is_terminal() -> bool {
match Self::get_state() {
Ok(_) => true,
Err(e) => e != -LINUX_ERRNO::ENOTTY && e != -LINUX_ERRNO::EINVAL,
}
}
/// Disables raw mode.
pub fn disable_raw_mode() -> Result<(), isize> {
let mut state = LinuxTermios::get_state()?;
state.c_lflag |= LINUX_TERMIOS_LFLAG::ICANON;
state.c_lflag |= LINUX_TERMIOS_LFLAG::ECHO;
LinuxTermios::set_state(state)
}
/// Enables raw mode.
pub fn enable_raw_mode() -> Result<(), isize> {
let mut state = Self::get_state()?;
state.c_lflag &= !LINUX_TERMIOS_LFLAG::ICANON;
state.c_lflag &= !LINUX_TERMIOS_LFLAG::ECHO;
LinuxTermios::set_state(state)
}
/// Returns the size of the window, in cells and pixels.
pub fn get_winsize() -> Result<LinuxTerminalSize, isize> {
let mut winsize = LinuxTerminalSize::default();
let res = unsafe {
linux_sys_ioctl(
LINUX_FILENO::STDIN,
LINUX_IOCTL::TIOCGWINSZ,
&mut winsize as *mut LinuxTerminalSize as *mut u8,
)
};
iif![res >= 0; Ok(winsize); Err(res)]
}
}
/// The size of the terminal.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct LinuxTerminalSize {
/// Rows.
pub rows: u16,
/// Columns.
pub cols: u16,
/// Horizontal pixels.
pub x: u16,
/// Vertical pixels.
pub y: u16,
}
impl LinuxTerminalSize {
/// Returns a tuple of (x, y) pixels.
pub const fn pixels(&self) -> (u16, u16) {
(self.x, self.y)
}
/// Returns a tuple of (columns, rows) cells.
pub const fn cells(&self) -> (u16, u16) {
(self.rows, self.cols)
}
}