Commit 226a38e9 authored by Jezza's avatar Jezza

Correctly set the console modes.

parent 03eeeca9
Pipeline #2274 passed with stages
in 7 minutes and 49 seconds
......@@ -22,5 +22,5 @@ redox_termios = "0.1"
[target.'cfg(windows)'.dependencies]
[dependencies.winapi]
version = "0.3.6"
features = ["winbase", "wincon", "processenv", "consoleapi"]
features = ["winbase", "wincon", "processenv", "consoleapi", "handleapi"]
......@@ -24,7 +24,7 @@ pub fn async_stdin_until(delimiter: u8) -> AsyncReader {
}
});
AsyncReader { recv: recv }
AsyncReader { recv }
}
/// Construct an asynchronous handle to the TTY standard input.
......
......@@ -95,7 +95,7 @@ impl<W: Write> IntoRawMode for W {
set_terminal_attr(&ios)?;
Ok(RawTerminal {
prev_ios: prev_ios,
prev_ios,
output: self,
})
}
......
use std::{io, mem};
use super::{DWORD, Termios};
use super::winapi::um::{consoleapi, processenv, winbase, wincon};
use super::winapi::um::{consoleapi, handleapi, processenv, winbase, wincon};
pub fn get_terminal_attr() -> io::Result<Termios> {
let old_mode: DWORD = unsafe {
let input_handle = processenv::GetStdHandle(winbase::STD_INPUT_HANDLE);
let mut old_mode: DWORD = mem::zeroed();
consoleapi::GetConsoleMode(input_handle, &mut old_mode);
old_mode
};
let input_mode = get_console_mode(winbase::STD_INPUT_HANDLE)?;
let output_mode = get_console_mode(winbase::STD_OUTPUT_HANDLE)?;
Ok(Termios(old_mode))
Ok(Termios(input_mode, output_mode))
}
pub fn set_terminal_attr(termios: &Termios) -> io::Result<()> {
let mode = termios.0;
unsafe {
let input_handle = processenv::GetStdHandle(winbase::STD_INPUT_HANDLE);
consoleapi::SetConsoleMode(input_handle, mode);
};
set_console_mode(winbase::STD_INPUT_HANDLE, termios.0)?;
set_console_mode(winbase::STD_OUTPUT_HANDLE, termios.1)?;
Ok(())
}
fn get_console_mode(handle: DWORD) -> io::Result<DWORD> {
unsafe {
let handle = processenv::GetStdHandle(handle);
if handle == handleapi::INVALID_HANDLE_VALUE {
return Err(io::Error::last_os_error());
}
let mut mode: DWORD = mem::zeroed();
consoleapi::GetConsoleMode(handle, &mut mode);
Ok(mode)
}
}
fn set_console_mode(handle: DWORD, mode: DWORD) -> io::Result<()> {
unsafe {
let handle = processenv::GetStdHandle(handle);
if handle == handleapi::INVALID_HANDLE_VALUE {
return Err(io::Error::last_os_error());
}
let check = consoleapi::SetConsoleMode(handle, mode);
if check != 1 {
return Err(io::Error::last_os_error());
}
Ok(())
}
}
pub fn raw_terminal_attr(termios: &mut Termios) {
termios.0 = wincon::ENABLE_EXTENDED_FLAGS
| wincon::ENABLE_WINDOW_INPUT
| wincon::ENABLE_MOUSE_INPUT
| wincon::ENABLE_VIRTUAL_TERMINAL_INPUT
| wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING;
termios.0 = wincon::ENABLE_VIRTUAL_TERMINAL_INPUT
| wincon::ENABLE_EXTENDED_FLAGS
| wincon::ENABLE_WINDOW_INPUT;
termios.1 = wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING
| wincon::ENABLE_PROCESSED_OUTPUT
| wincon::DISABLE_NEWLINE_AUTO_RETURN;
}
......@@ -3,7 +3,7 @@ extern crate winapi;
use self::winapi::shared::minwindef::DWORD;
#[derive(Clone, Copy, Debug)]
pub struct Termios(DWORD);
pub struct Termios(DWORD, DWORD);
pub mod attr;
pub mod size;
......
......@@ -11,7 +11,7 @@ pub fn is_tty<T: IntoRawHandle>(stream: &T) -> bool {
/// This allows for getting stdio representing _only_ the TTY, and not other streams.
pub fn get_tty() -> io::Result<fs::File> {
unsafe {
let handle = io::stdout().as_raw_handle();
let handle = io::stdin().as_raw_handle();
let file: fs::File = FromRawHandle::from_raw_handle(handle);
Ok(file)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment