Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Replace RAW_MODE_ENABLED with is_raw_mode_enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
zrzka authored Oct 3, 2019
1 parent 7eac239 commit 152cd56
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Add deprecation note
- Remove all references to the crossterm book
- `sys::unix::RAW_MODE_ENABLED` replaced with `sys::unix::is_raw_mode_enabled()` (breaking)
- New `lazy_static` dependency

# Version 0.3.1

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ crossterm_winapi = { version = "0.2.1" }

[target.'cfg(unix)'.dependencies]
libc = "0.2.51"

[dependencies]
lazy_static = "1.4"
45 changes: 30 additions & 15 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
//! This module contains all `unix` specific terminal related logic.
use std::sync::Mutex;
use std::{io, mem};

pub use libc::termios as Termios;
use libc::{cfmakeraw, tcgetattr, tcsetattr, STDIN_FILENO, TCSANOW};

use lazy_static::lazy_static;

use crate::{ErrorKind, Result};

static mut ORIGINAL_TERMINAL_MODE: Option<Termios> = None;
pub static mut RAW_MODE_ENABLED: bool = false;
lazy_static! {
// Some(Termios) -> we're in the raw mode and this is the previous mode
// None -> we're not in the raw mode
static ref TERMINAL_MODE_PRIOR_RAW_MODE: Mutex<Option<Termios>> = Mutex::new(None);
}

pub fn is_raw_mode_enabled() -> bool {
TERMINAL_MODE_PRIOR_RAW_MODE.lock().unwrap().is_some()
}

fn wrap_with_result(t: i32) -> Result<()> {
if t == -1 {
Expand Down Expand Up @@ -36,27 +46,32 @@ pub fn set_terminal_attr(termios: &Termios) -> Result<()> {
}

pub fn enable_raw_mode() -> Result<()> {
let mut ios = get_terminal_attr()?;
let prev_ios = ios;

unsafe {
if ORIGINAL_TERMINAL_MODE.is_none() {
ORIGINAL_TERMINAL_MODE = Some(prev_ios.clone());
}
let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock().unwrap();

RAW_MODE_ENABLED = true;
if original_mode.is_some() {
return Ok(());
}

let mut ios = get_terminal_attr()?;
let original_mode_ios = ios;

raw_terminal_attr(&mut ios);
set_terminal_attr(&ios)?;

// Keep it last - set the original mode only if we were able to switch to the raw mode
*original_mode = Some(original_mode_ios);

Ok(())
}

pub fn disable_raw_mode() -> Result<()> {
unsafe {
if let Some(original_terminal_mode) = ORIGINAL_TERMINAL_MODE.as_ref() {
set_terminal_attr(original_terminal_mode)?;
RAW_MODE_ENABLED = false;
}
let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock().unwrap();

if let Some(original_mode_ios) = original_mode.as_ref() {
set_terminal_attr(original_mode_ios)?;
// Keep it last - remove the original mode only if we were able to switch back
*original_mode = None;
}

Ok(())
}

0 comments on commit 152cd56

Please sign in to comment.