summaryrefslogtreecommitdiffstats
path: root/alacritty_terminal/src/tty/windows/automatic_backend.rs
blob: a1bd540d2143ed6c0eda23adbe48360696a8c7c3 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Types to determine the appropriate PTY backend at runtime.
//!
//! Unless the winpty feature is disabled, the PTY backend will automatically fall back to
//! WinPTY when the newer ConPTY API is not supported, as long as the user hasn't explicitly
//! opted into the WinPTY config option.

use std::io::{self, Read, Write};

use log::info;
use mio::{Evented, Poll, PollOpt, Ready, Token};
use mio_anonymous_pipes::{EventedAnonRead, EventedAnonWrite};
use mio_named_pipes::NamedPipe;

use crate::config::Config;
use crate::event::OnResize;
use crate::term::SizeInfo;

use super::{conpty, winpty, Pty};

pub fn new<C>(config: &Config<C>, size: &SizeInfo, window_id: Option<usize>) -> Pty {
    if let Some(pty) = conpty::new(config, size, window_id) {
        info!("Using ConPTY backend");
        pty
    } else {
        info!("Using WinPTY backend");
        winpty::new(config, size, window_id)
    }
}

pub enum PtyBackend {
    Winpty(winpty::Agent),
    Conpty(conpty::Conpty),
}

impl OnResize for PtyBackend {
    fn on_resize(&mut self, size: &SizeInfo) {
        match self {
            PtyBackend::Winpty(w) => w.on_resize(size),
            PtyBackend::Conpty(c) => c.on_resize(size),
        }
    }
}

// TODO: The ConPTY API currently must use synchronous pipes as the input
// and output handles. This has led to the need to support two different
// types of pipe.
//
// When https://github.com/Microsoft/console/issues/262 lands then the
// Anonymous variant of this enum can be removed from the codebase and
// everything can just use NamedPipe.
pub enum EventedReadablePipe {
    Anonymous(EventedAnonRead),
    Named(NamedPipe),
}

pub enum EventedWritablePipe {
    Anonymous(EventedAnonWrite),
    Named(NamedPipe),
}

impl Evented for EventedReadablePipe {
    fn register(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        match self {
            EventedReadablePipe::Anonymous(p) => p.register(poll, token, interest, opts),
            EventedReadablePipe::Named(p) => p.register(poll, token, interest, opts),
        }
    }

    fn reregister(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        match self {
            EventedReadablePipe::Anonymous(p) => p.reregister(poll, token, interest, opts),
            EventedReadablePipe::Named(p) => p.reregister(poll, token, interest, opts),
        }
    }

    fn deregister(&self, poll: &Poll) -> io::Result<()> {
        match self {
            EventedReadablePipe::Anonymous(p) => p.deregister(poll),
            EventedReadablePipe::Named(p) => p.deregister(poll),
        }
    }
}

impl Read for EventedReadablePipe {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match self {
            EventedReadablePipe::Anonymous(p) => p.read(buf),
            EventedReadablePipe::Named(p) => p.read(buf),
        }
    }
}

impl Evented for EventedWritablePipe {
    fn register(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        match self {
            EventedWritablePipe::Anonymous(p) => p.register(poll, token, interest, opts),
            EventedWritablePipe::Named(p) => p.register(poll, token, interest, opts),
        }
    }

    fn reregister(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        match self {
            EventedWritablePipe::Anonymous(p) => p.reregister(poll, token, interest, opts),
            EventedWritablePipe::Named(p) => p.reregister(poll, token, interest, opts),
        }
    }

    fn deregister(&self, poll: &Poll) -> io::Result<()> {
        match self {
            EventedWritablePipe::Anonymous(p) => p.deregister(poll),
            EventedWritablePipe::Named(p) => p.deregister(poll),
        }
    }
}

impl Write for EventedWritablePipe {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        match self {
            EventedWritablePipe::Anonymous(p) => p.write(buf),
            EventedWritablePipe::Named(p) => p.write(buf),
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        match self {
            EventedWritablePipe::Anonymous(p) => p.flush(),
            EventedWritablePipe::Named(p) => p.flush(),
        }
    }
}

impl From<winpty::Agent> for PtyBackend {
    fn from(inner: winpty::Agent) -> Self {
        PtyBackend::Winpty(inner)
    }
}

impl From<conpty::Conpty> for PtyBackend {
    fn from(inner: conpty::Conpty) -> Self {
        PtyBackend::Conpty(inner)
    }
}

impl From<EventedAnonRead> for EventedReadablePipe {
    fn from(inner: EventedAnonRead) -> Self {
        EventedReadablePipe::Anonymous(inner)
    }
}

impl From<NamedPipe> for EventedReadablePipe {
    fn from(inner: NamedPipe) -> Self {
        EventedReadablePipe::Named(inner)
    }
}

impl From<EventedAnonWrite> for EventedWritablePipe {
    fn from(inner: EventedAnonWrite) -> Self {
        EventedWritablePipe::Anonymous(inner)
    }
}

impl From<NamedPipe> for EventedWritablePipe {
    fn from(inner: NamedPipe) -> Self {
        EventedWritablePipe::Named(inner)
    }
}