| 1 | //! UNIX related logic for terminal manipulation. |
| 2 | |
| 3 | #[cfg(feature = "events")] |
| 4 | use crate::event::KeyboardEnhancementFlags; |
| 5 | use crate::terminal::{ |
| 6 | sys::file_descriptor::{tty_fd, FileDesc}, |
| 7 | WindowSize, |
| 8 | }; |
| 9 | #[cfg(feature = "libc")] |
| 10 | use libc::{ |
| 11 | cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDOUT_FILENO, TCSANOW, |
| 12 | TIOCGWINSZ, |
| 13 | }; |
| 14 | use parking_lot::Mutex; |
| 15 | #[cfg(not(feature = "libc"))] |
| 16 | use rustix::{ |
| 17 | fd::AsFd, |
| 18 | termios::{Termios, Winsize}, |
| 19 | }; |
| 20 | |
| 21 | use std::{fs::File, io, process}; |
| 22 | #[cfg(feature = "libc")] |
| 23 | use std::{ |
| 24 | mem, |
| 25 | os::unix::io::{IntoRawFd, RawFd}, |
| 26 | }; |
| 27 | |
| 28 | // Some(Termios) -> we're in the raw mode and this is the previous mode |
| 29 | // None -> we're not in the raw mode |
| 30 | static TERMINAL_MODE_PRIOR_RAW_MODE: Mutex<Option<Termios>> = parking_lot::const_mutex(None); |
| 31 | |
| 32 | pub(crate) fn is_raw_mode_enabled() -> bool { |
| 33 | TERMINAL_MODE_PRIOR_RAW_MODE.lock().is_some() |
| 34 | } |
| 35 | |
| 36 | #[cfg(feature = "libc")] |
| 37 | impl From<winsize> for WindowSize { |
| 38 | fn from(size: winsize) -> WindowSize { |
| 39 | WindowSize { |
| 40 | columns: size.ws_col, |
| 41 | rows: size.ws_row, |
| 42 | width: size.ws_xpixel, |
| 43 | height: size.ws_ypixel, |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | #[cfg(not(feature = "libc"))] |
| 48 | impl From<Winsize> for WindowSize { |
| 49 | fn from(size: Winsize) -> WindowSize { |
| 50 | WindowSize { |
| 51 | columns: size.ws_col, |
| 52 | rows: size.ws_row, |
| 53 | width: size.ws_xpixel, |
| 54 | height: size.ws_ypixel, |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #[allow(clippy::useless_conversion)] |
| 60 | #[cfg(feature = "libc")] |
| 61 | pub(crate) fn window_size() -> io::Result<WindowSize> { |
| 62 | // http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc |
| 63 | let mut size = winsize { |
| 64 | ws_row: 0, |
| 65 | ws_col: 0, |
| 66 | ws_xpixel: 0, |
| 67 | ws_ypixel: 0, |
| 68 | }; |
| 69 | |
| 70 | let file = File::open("/dev/tty").map(|file| (FileDesc::new(file.into_raw_fd(), true))); |
| 71 | let fd = if let Ok(file) = &file { |
| 72 | file.raw_fd() |
| 73 | } else { |
| 74 | // Fallback to libc::STDOUT_FILENO if /dev/tty is missing |
| 75 | STDOUT_FILENO |
| 76 | }; |
| 77 | |
| 78 | if wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }).is_ok() { |
| 79 | return Ok(size.into()); |
| 80 | } |
| 81 | |
| 82 | Err(std::io::Error::last_os_error().into()) |
| 83 | } |
| 84 | |
| 85 | #[cfg(not(feature = "libc"))] |
| 86 | pub(crate) fn window_size() -> io::Result<WindowSize> { |
| 87 | let file = File::open("/dev/tty").map(|file| FileDesc::Owned(file.into())); |
| 88 | let fd = if let Ok(file) = &file { |
| 89 | file.as_fd() |
| 90 | } else { |
| 91 | // Fallback to libc::STDOUT_FILENO if /dev/tty is missing |
| 92 | rustix::stdio::stdout() |
| 93 | }; |
| 94 | let size = rustix::termios::tcgetwinsize(fd)?; |
| 95 | Ok(size.into()) |
| 96 | } |
| 97 | |
| 98 | #[allow(clippy::useless_conversion)] |
| 99 | pub(crate) fn size() -> io::Result<(u16, u16)> { |
| 100 | if let Ok(window_size) = window_size() { |
| 101 | return Ok((window_size.columns, window_size.rows)); |
| 102 | } |
| 103 | |
| 104 | tput_size().ok_or_else(|| std::io::Error::last_os_error().into()) |
| 105 | } |
| 106 | |
| 107 | #[cfg(feature = "libc")] |
| 108 | pub(crate) fn enable_raw_mode() -> io::Result<()> { |
| 109 | let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock(); |
| 110 | if original_mode.is_some() { |
| 111 | return Ok(()); |
| 112 | } |
| 113 | |
| 114 | let tty = tty_fd()?; |
| 115 | let fd = tty.raw_fd(); |
| 116 | let mut ios = get_terminal_attr(fd)?; |
| 117 | let original_mode_ios = ios; |
| 118 | raw_terminal_attr(&mut ios); |
| 119 | set_terminal_attr(fd, &ios)?; |
| 120 | // Keep it last - set the original mode only if we were able to switch to the raw mode |
| 121 | *original_mode = Some(original_mode_ios); |
| 122 | Ok(()) |
| 123 | } |
| 124 | |
| 125 | #[cfg(not(feature = "libc"))] |
| 126 | pub(crate) fn enable_raw_mode() -> io::Result<()> { |
| 127 | let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock(); |
| 128 | if original_mode.is_some() { |
| 129 | return Ok(()); |
| 130 | } |
| 131 | |
| 132 | let tty = tty_fd()?; |
| 133 | let mut ios = get_terminal_attr(&tty)?; |
| 134 | let original_mode_ios = ios.clone(); |
| 135 | ios.make_raw(); |
| 136 | set_terminal_attr(&tty, &ios)?; |
| 137 | // Keep it last - set the original mode only if we were able to switch to the raw mode |
| 138 | *original_mode = Some(original_mode_ios); |
| 139 | Ok(()) |
| 140 | } |
| 141 | |
| 142 | /// Reset the raw mode. |
| 143 | /// |
| 144 | /// More precisely, reset the whole termios mode to what it was before the first call |
| 145 | /// to [enable_raw_mode]. If you don't mess with termios outside of crossterm, it's |
| 146 | /// effectively disabling the raw mode and doing nothing else. |
| 147 | #[cfg(feature = "libc")] |
| 148 | pub(crate) fn disable_raw_mode() -> io::Result<()> { |
| 149 | let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock(); |
| 150 | if let Some(original_mode_ios) = original_mode.as_ref() { |
| 151 | let tty = tty_fd()?; |
| 152 | set_terminal_attr(tty.raw_fd(), original_mode_ios)?; |
| 153 | // Keep it last - remove the original mode only if we were able to switch back |
| 154 | *original_mode = None; |
| 155 | } |
| 156 | Ok(()) |
| 157 | } |
| 158 | |
| 159 | #[cfg(not(feature = "libc"))] |
| 160 | pub(crate) fn disable_raw_mode() -> io::Result<()> { |
| 161 | let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock(); |
| 162 | if let Some(original_mode_ios) = original_mode.as_ref() { |
| 163 | let tty = tty_fd()?; |
| 164 | set_terminal_attr(&tty, original_mode_ios)?; |
| 165 | // Keep it last - remove the original mode only if we were able to switch back |
| 166 | *original_mode = None; |
| 167 | } |
| 168 | Ok(()) |
| 169 | } |
| 170 | |
| 171 | #[cfg(not(feature = "libc"))] |
| 172 | fn get_terminal_attr(fd: impl AsFd) -> io::Result<Termios> { |
| 173 | let result = rustix::termios::tcgetattr(fd)?; |
| 174 | Ok(result) |
| 175 | } |
| 176 | |
| 177 | #[cfg(not(feature = "libc"))] |
| 178 | fn set_terminal_attr(fd: impl AsFd, termios: &Termios) -> io::Result<()> { |
| 179 | rustix::termios::tcsetattr(fd, rustix::termios::OptionalActions::Now, termios)?; |
| 180 | Ok(()) |
| 181 | } |
| 182 | |
| 183 | /// Queries the terminal's support for progressive keyboard enhancement. |
| 184 | /// |
| 185 | /// On unix systems, this function will block and possibly time out while |
| 186 | /// [`crossterm::event::read`](crate::event::read) or [`crossterm::event::poll`](crate::event::poll) are being called. |
| 187 | #[cfg(feature = "events")] |
| 188 | pub fn supports_keyboard_enhancement() -> io::Result<bool> { |
| 189 | query_keyboard_enhancement_flags().map(|flags| flags.is_some()) |
| 190 | } |
| 191 | |
| 192 | /// Queries the terminal's currently active keyboard enhancement flags. |
| 193 | /// |
| 194 | /// On unix systems, this function will block and possibly time out while |
| 195 | /// [`crossterm::event::read`](crate::event::read) or [`crossterm::event::poll`](crate::event::poll) are being called. |
| 196 | #[cfg(feature = "events")] |
| 197 | pub fn query_keyboard_enhancement_flags() -> io::Result<Option<KeyboardEnhancementFlags>> { |
| 198 | if is_raw_mode_enabled() { |
| 199 | query_keyboard_enhancement_flags_raw() |
| 200 | } else { |
| 201 | query_keyboard_enhancement_flags_nonraw() |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | #[cfg(feature = "events")] |
| 206 | fn query_keyboard_enhancement_flags_nonraw() -> io::Result<Option<KeyboardEnhancementFlags>> { |
| 207 | enable_raw_mode()?; |
| 208 | let flags = query_keyboard_enhancement_flags_raw(); |
| 209 | disable_raw_mode()?; |
| 210 | flags |
| 211 | } |
| 212 | |
| 213 | #[cfg(feature = "events")] |
| 214 | fn query_keyboard_enhancement_flags_raw() -> io::Result<Option<KeyboardEnhancementFlags>> { |
| 215 | use crate::event::{ |
| 216 | filter::{KeyboardEnhancementFlagsFilter, PrimaryDeviceAttributesFilter}, |
| 217 | poll_internal, read_internal, InternalEvent, |
| 218 | }; |
| 219 | use std::io::Write; |
| 220 | use std::time::Duration; |
| 221 | |
| 222 | // This is the recommended method for testing support for the keyboard enhancement protocol. |
| 223 | // We send a query for the flags supported by the terminal and then the primary device attributes |
| 224 | // query. If we receive the primary device attributes response but not the keyboard enhancement |
| 225 | // flags, none of the flags are supported. |
| 226 | // |
| 227 | // See <https://sw.kovidgoyal.net/kitty/keyboard-protocol/#detection-of-support-for-this-protocol> |
| 228 | |
| 229 | // ESC [ ? u Query progressive keyboard enhancement flags (kitty protocol). |
| 230 | // ESC [ c Query primary device attributes. |
| 231 | const QUERY: &[u8] = b"\x1B[?u\x1B[c"; |
| 232 | |
| 233 | let result = File::open("/dev/tty").and_then(|mut file| { |
| 234 | file.write_all(QUERY)?; |
| 235 | file.flush() |
| 236 | }); |
| 237 | if result.is_err() { |
| 238 | let mut stdout = io::stdout(); |
| 239 | stdout.write_all(QUERY)?; |
| 240 | stdout.flush()?; |
| 241 | } |
| 242 | |
| 243 | let timeout = Duration::from_millis(2000); |
| 244 | let started = std::time::Instant::now(); |
| 245 | match poll_internal(Some(timeout), &KeyboardEnhancementFlagsFilter)? { |
| 246 | true => match read_internal(&KeyboardEnhancementFlagsFilter)? { |
| 247 | InternalEvent::KeyboardEnhancementFlags(current_flags) => { |
| 248 | // Drain PrimaryDeviceAttributes within the original query deadline. |
| 249 | if poll_internal( |
| 250 | Some(timeout.saturating_sub(started.elapsed())), |
| 251 | &PrimaryDeviceAttributesFilter, |
| 252 | )? { |
| 253 | read_internal(&PrimaryDeviceAttributesFilter)?; |
| 254 | } |
| 255 | Ok(Some(current_flags)) |
| 256 | } |
| 257 | _ => Ok(None), |
| 258 | }, |
| 259 | false => Err(io::Error::new( |
| 260 | io::ErrorKind::Other, |
| 261 | "The keyboard enhancement status could not be read within a normal duration", |
| 262 | )), |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /// execute tput with the given argument and parse |
| 267 | /// the output as a u16. |
| 268 | /// |
| 269 | /// The arg should be "cols" or "lines" |
| 270 | fn tput_value(arg: &str) -> Option<u16> { |
| 271 | let output = process::Command::new("tput").arg(arg).output().ok()?; |
| 272 | let value = output |
| 273 | .stdout |
| 274 | .into_iter() |
| 275 | .filter_map(|b| char::from(b).to_digit(10)) |
| 276 | .fold(0, |v, n| v * 10 + n as u16); |
| 277 | |
| 278 | if value > 0 { |
| 279 | Some(value) |
| 280 | } else { |
| 281 | None |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /// Returns the size of the screen as determined by tput. |
| 286 | /// |
| 287 | /// This alternate way of computing the size is useful |
| 288 | /// when in a subshell. |
| 289 | fn tput_size() -> Option<(u16, u16)> { |
| 290 | match (tput_value("cols"), tput_value("lines")) { |
| 291 | (Some(w), Some(h)) => Some((w, h)), |
| 292 | _ => None, |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | #[cfg(feature = "libc")] |
| 297 | // Transform the given mode into an raw mode (non-canonical) mode. |
| 298 | fn raw_terminal_attr(termios: &mut Termios) { |
| 299 | unsafe { cfmakeraw(termios) } |
| 300 | } |
| 301 | |
| 302 | #[cfg(feature = "libc")] |
| 303 | fn get_terminal_attr(fd: RawFd) -> io::Result<Termios> { |
| 304 | unsafe { |
| 305 | let mut termios = mem::zeroed(); |
| 306 | wrap_with_result(tcgetattr(fd, &mut termios))?; |
| 307 | Ok(termios) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | #[cfg(feature = "libc")] |
| 312 | fn set_terminal_attr(fd: RawFd, termios: &Termios) -> io::Result<()> { |
| 313 | wrap_with_result(unsafe { tcsetattr(fd, TCSANOW, termios) }) |
| 314 | } |
| 315 | |
| 316 | #[cfg(feature = "libc")] |
| 317 | fn wrap_with_result(result: i32) -> io::Result<()> { |
| 318 | if result == -1 { |
| 319 | Err(io::Error::last_os_error()) |
| 320 | } else { |
| 321 | Ok(()) |
| 322 | } |
| 323 | } |
| 324 |