Debian Codegraph @

Xtermina/src/event/source/windows.rs

raw symbols SCIP
1 // CREDIT: This one is shared between crossterm and termwiz but is mostly termwiz.
2 // Termwiz: <https://github.com/wezterm/wezterm/blame/a87358516004a652ad840bc1661bdf65ffc89b43/termwiz/src/terminal/windows.rs#L810-L853>
3 // Crossterm: <https://github.com/crossterm-rs/crossterm/blob/36d95b26a26e64b0f8c12edfe11f410a6d56a812/src/event/source/windows.rs>
4 // Also see the necessary methods on the handle from the terminal module and the credit comment
5 // there.
6 use std::{io, os::windows::prelude::*, ptr, sync::Arc, time::Duration};
7
8 use windows_sys::Win32::System::Threading;
9
10 use crate::{event::Event, parse::Parser, terminal::InputHandle, windows::InputReaderMode};
11
12 use super::{EventSource, PollTimeout};
13
14 #[derive(Debug)]
15 pub struct WindowsEventSource {
16 input: InputHandle,
17 parser: Parser,
18 waker: Arc<EventHandle>,
19 }
20
21 impl WindowsEventSource {
22 pub(crate) fn new(input: InputHandle, mode: InputReaderMode) -> io::Result<Self> {
23 Ok(Self {
24 input,
25 parser: Parser::with_mode(mode),
26 waker: Arc::new(EventHandle::new()?),
27 })
28 }
29 }
30
31 impl EventSource for WindowsEventSource {
32 fn waker(&self) -> WindowsWaker {
33 WindowsWaker {
34 handle: self.waker.clone(),
35 }
36 }
37
38 fn try_read(&mut self, timeout: Option<Duration>) -> io::Result<Option<Event>> {
39 use windows_sys::Win32::Foundation::{WAIT_FAILED, WAIT_OBJECT_0};
40 use Threading::{WaitForMultipleObjects, INFINITE};
41
42 let timeout = PollTimeout::new(timeout);
43
44 loop {
45 if let Some(event) = self.parser.pop() {
46 return Ok(Some(event));
47 }
48
49 let has_pending = self.input.has_pending_input_events()?;
50
51 if !has_pending {
52 let mut handles = [self.input.as_raw_handle(), self.waker.as_raw_handle()];
53 let wait = timeout
54 .leftover()
55 .map(|timeout| timeout.as_millis() as u32)
56 .unwrap_or(INFINITE);
57 let result = unsafe {
58 WaitForMultipleObjects(handles.len() as u32, handles.as_mut_ptr(), 0, wait)
59 };
60
61 if result == WAIT_OBJECT_0 + 1 {
62 return Err(io::Error::new(
63 io::ErrorKind::Interrupted,
64 "Poll operation was woken up",
65 ));
66 } else if result == WAIT_FAILED {
67 return Err(io::Error::new(
68 io::ErrorKind::Other,
69 format!(
70 "failed to poll input handles: {}",
71 io::Error::last_os_error()
72 ),
73 ));
74 } else {
75 return Ok(None);
76 }
77 }
78
79 let records = self.input.read_console_input()?;
80
81 self.parser.decode_input_records(records);
82
83 if timeout.leftover().is_some_and(|t| t.is_zero()) {
84 break;
85 }
86 }
87
88 Ok(None)
89 }
90 }
91
92 #[derive(Debug)]
93 struct EventHandle {
94 handle: OwnedHandle,
95 }
96
97 impl EventHandle {
98 fn new() -> io::Result<Self> {
99 let handle = unsafe { Threading::CreateEventW(ptr::null(), 0, 0, ptr::null()) };
100 if handle.is_null() {
101 Err(io::Error::last_os_error())
102 } else {
103 let handle = unsafe { OwnedHandle::from_raw_handle(handle) };
104 Ok(Self { handle })
105 }
106 }
107 }
108
109 impl AsRawHandle for EventHandle {
110 fn as_raw_handle(&self) -> RawHandle {
111 self.handle.as_raw_handle()
112 }
113 }
114
115 #[derive(Debug)]
116 pub struct WindowsWaker {
117 handle: Arc<EventHandle>,
118 }
119
120 impl WindowsWaker {
121 pub fn wake(&self) -> io::Result<()> {
122 if unsafe { Threading::SetEvent(self.handle.as_raw_handle()) } == 0 {
123 Err(io::Error::last_os_error())
124 } else {
125 Ok(())
126 }
127 }
128 }