forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestconsole.rs
More file actions
45 lines (40 loc) · 1.27 KB
/
Copy pathtestconsole.rs
File metadata and controls
45 lines (40 loc) · 1.27 KB
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
use std::io;
use windows_sys::Win32::{
Foundation::HANDLE,
System::Console::{INPUT_RECORD, KEY_EVENT, WriteConsoleInputW},
};
use crate::windows::{CheckWin32Bool, CheckWin32Handle};
pub fn write_console_input(fd: i32, data: &[u16]) -> io::Result<()> {
let handle = (unsafe { libc::get_osfhandle(fd) } as HANDLE).check_valid()?;
let size = data.len() as u32;
let mut records: Vec<INPUT_RECORD> = Vec::with_capacity(data.len());
for &wc in data {
let mut rec: INPUT_RECORD = unsafe { core::mem::zeroed() };
rec.EventType = KEY_EVENT as u16;
rec.Event.KeyEvent.bKeyDown = 1;
rec.Event.KeyEvent.wRepeatCount = 1;
rec.Event.KeyEvent.uChar.UnicodeChar = wc;
records.push(rec);
}
let mut total: u32 = 0;
while total < size {
let mut wrote: u32 = 0;
unsafe {
WriteConsoleInputW(
handle,
records[total as usize..].as_ptr(),
size - total,
&mut wrote,
)
}
.check_win32_bool()?;
if wrote == 0 {
return Err(io::Error::new(
io::ErrorKind::WriteZero,
"WriteConsoleInputW made no progress",
));
}
total += wrote;
}
Ok(())
}