Skip to content

Commit

Permalink
Unblock from read_key proof of concpet
Browse files Browse the repository at this point in the history
  • Loading branch information
george-cosma committed Oct 3, 2023
1 parent 0567bdc commit 7b29f1a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
12 changes: 12 additions & 0 deletions examples/unblock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::error::Error;

use console::Term;

fn main() -> Result<(), Box<dyn Error>> {
let term = Term::stdout();
term.unblock()?;
let key = term.read_key()?;
dbg!(key);

Ok(())
}
8 changes: 8 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ impl Term {
}
}

pub fn unblock(&self) -> io::Result<()> {
if self.is_tty {
write_to_input()
} else {
Ok(())
}
}

/// Read one line of input.
///
/// This does not include the trailing newline. If the terminal is not
Expand Down
31 changes: 31 additions & 0 deletions src/unix_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,37 @@ fn read_bytes(fd: i32, buf: &mut [u8], count: u8) -> io::Result<u8> {
}
}

pub fn write_to_input() -> io::Result<()> {
let tty_f;
let fd = unsafe {
if libc::isatty(libc::STDIN_FILENO) == 1 {
libc::STDIN_FILENO
} else {
tty_f = fs::OpenOptions::new()
.read(true)
.write(true)
.open("/dev/tty")?;
tty_f.as_raw_fd()
}
};

let mut termios = core::mem::MaybeUninit::uninit();
c_result(|| unsafe { libc::tcgetattr(fd, termios.as_mut_ptr()) })?;
let mut termios = unsafe { termios.assume_init() };
let original = termios;
unsafe { libc::cfmakeraw(&mut termios) };
termios.c_oflag = original.c_oflag;
c_result(|| unsafe { libc::tcsetattr(fd, libc::TCSADRAIN, &termios) })?;
{
let mut bytes = [b'x'];
let result = unsafe { libc::ioctl(fd, libc::TIOCSTI, &mut bytes) };
dbg!(result);
}
c_result(|| unsafe { libc::tcsetattr(fd, libc::TCSADRAIN, &original) })?;

Ok(())
}

fn read_single_key_impl(fd: i32) -> Result<Key, io::Error> {
loop {
match read_single_char(fd)? {
Expand Down
7 changes: 7 additions & 0 deletions src/wasm_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ pub fn read_secure() -> io::Result<String> {
))
}

pub fn write_to_input() -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Other,
"unsupported operation",
))
}

pub fn read_single_key() -> io::Result<Key> {
Err(io::Error::new(
io::ErrorKind::Other,
Expand Down
36 changes: 34 additions & 2 deletions src/windows_term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use windows_sys::Win32::System::Console::{
FillConsoleOutputAttribute, FillConsoleOutputCharacterA, GetConsoleCursorInfo, GetConsoleMode,
GetConsoleScreenBufferInfo, GetNumberOfConsoleInputEvents, GetStdHandle, ReadConsoleInputW,
SetConsoleCursorInfo, SetConsoleCursorPosition, SetConsoleMode, SetConsoleTitleW,
CONSOLE_CURSOR_INFO, CONSOLE_SCREEN_BUFFER_INFO, COORD, INPUT_RECORD, KEY_EVENT,
KEY_EVENT_RECORD, STD_ERROR_HANDLE, STD_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
WriteConsoleInputW, CONSOLE_CURSOR_INFO, CONSOLE_SCREEN_BUFFER_INFO, COORD, INPUT_RECORD,
INPUT_RECORD_0, KEY_EVENT, KEY_EVENT_RECORD, KEY_EVENT_RECORD_0, STD_ERROR_HANDLE, STD_HANDLE,
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
};
use windows_sys::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY;

Expand Down Expand Up @@ -469,6 +470,37 @@ fn get_key_event_count() -> io::Result<u32> {
}
}

pub fn write_to_input() -> io::Result<()> {
let handle = get_stdin_handle()?;
let mut buffer: INPUT_RECORD = unsafe { mem::zeroed() };
buffer.EventType = KEY_EVENT as u16;
buffer.Event = INPUT_RECORD_0 {
KeyEvent: KEY_EVENT_RECORD {
bKeyDown: 1,
wRepeatCount: 1,
wVirtualKeyCode: 65,
wVirtualScanCode: 30,
dwControlKeyState: 0,
uChar: KEY_EVENT_RECORD_0 { UnicodeChar: 97 },
},
};
let mut events_written: u32 = unsafe { mem::zeroed() };

let success = unsafe { WriteConsoleInputW(handle, &mut buffer, 1, &mut events_written) };

if success == 0 {
return Err(io::Error::last_os_error());
}
if events_written != 1 {
return Err(io::Error::new(
io::ErrorKind::Other,
"WriteConsoleInput returned no written events, but expected to write one.",
));
}

Ok(())
}

fn read_key_event() -> io::Result<KEY_EVENT_RECORD> {
let handle = get_stdin_handle()?;
let mut buffer: INPUT_RECORD = unsafe { mem::zeroed() };
Expand Down

0 comments on commit 7b29f1a

Please sign in to comment.