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
//! This crate provides TTY abstractions.
//!
//! TTYs define the interface between a terminal and the application running in
//! the terminal.

#![no_std]

extern crate alloc;

mod channel;
mod discipline;

pub use discipline::{Event, LineDiscipline};

use alloc::sync::Arc;
use channel::Channel;
use core2::io::{Read, Result, Write};

/// A terminal device driver.
///
/// The design is based on the Unix TTY/PTY subsystem. Unlike Unix, Theseus does
/// not distinguish between teletypes and pseudo-teletypes. Each `Tty` consists
/// of two ends: a [`Master`] and a [`Slave`]. The terminal holds the master and
/// the application holds the slave. The TTY's [`LineDiscipline`] dictates how
/// the two interact.
///
/// In the context of Theseus, there are two terminals:
/// - `terminal_emulator`, which is the graphical terminal emulator implemented
///   in Theseus.
/// - `console`, which connects to an external terminal emulator through a
///   serial port.
///
/// When started, both terminals launch the `shell`, which contains the logic to
/// launch applications, store command history, autocomplete input, etc.
#[derive(Clone)]
pub struct Tty {
    master: Channel,
    slave: Channel,
    discipline: Arc<LineDiscipline>,
}

impl Default for Tty {
    fn default() -> Self {
        Self::new()
    }
}

impl Tty {
    pub fn new() -> Self {
        Self {
            master: Channel::new(),
            slave: Channel::new(),
            discipline: Default::default(),
        }
    }

    pub fn master(&self) -> Master {
        Master {
            master: self.master.clone(),
            slave: self.slave.clone(),
            discipline: self.discipline.clone(),
        }
    }

    pub fn slave(&self) -> Slave {
        Slave {
            master: self.master.clone(),
            slave: self.slave.clone(),
            discipline: self.discipline.clone(),
        }
    }
}

/// The master (i.e. terminal) end of a [`Tty`].
#[derive(Clone)]
pub struct Master {
    master: Channel,
    slave: Channel,
    discipline: Arc<LineDiscipline>,
}

impl Master {
    pub fn discipline(&self) -> Arc<LineDiscipline> {
        self.discipline.clone()
    }

    pub fn read_byte(&self) -> Result<u8> {
        self.master.receive()
    }

    pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
        self.master.receive_buf(buf)
    }

    pub fn try_read(&self, buf: &mut [u8]) -> Result<usize> {
        self.master.try_receive_buf(buf)
    }

    pub fn write_byte(&self, byte: u8) -> Result<()> {
        self.discipline
            .process_input_byte(byte, &self.master, &self.slave)?;
        Ok(())
    }

    pub fn write(&self, buf: &[u8]) -> Result<usize> {
        // TODO: Don't fail if we can't send entire buf.
        self.discipline
            .process_input_buf(buf, &self.master, &self.slave)?;
        Ok(buf.len())
    }
}

impl Read for Master {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        let immutable: &Self = self;
        immutable.read(buf)
    }
}

impl Write for Master {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        let immutable: &Self = self;
        immutable.write(buf)
    }

    fn flush(&mut self) -> core2::io::Result<()> {
        todo!("do we flush canonical buffer?");
    }
}

/// The slave (i.e. application) end of a [`Tty`].
#[derive(Clone)]
pub struct Slave {
    master: Channel,
    slave: Channel,
    discipline: Arc<LineDiscipline>,
}

impl Slave {
    pub fn discipline(&self) -> Arc<LineDiscipline> {
        self.discipline.clone()
    }

    pub fn read_byte(&self) -> Result<u8> {
        self.slave.receive()
    }

    pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
        self.slave.receive_buf(buf)
    }

    pub fn try_read(&self, buf: &mut [u8]) -> Result<usize> {
        self.slave.try_receive_buf(buf)
    }

    pub fn write_byte(&self, byte: u8) -> Result<()> {
        self.discipline.process_output_byte(byte, &self.master)?;
        Ok(())
    }

    pub fn write(&self, buf: &[u8]) -> Result<usize> {
        // TODO: Don't fail if we can't send entire buf.
        self.discipline.process_output_buf(buf, &self.master)?;
        Ok(buf.len())
    }
}

impl Read for Slave {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        let immutable: &Self = self;
        immutable.read(buf)
    }
}

impl Write for Slave {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        let immutable: &Self = self;
        immutable.write(buf)
    }

    fn flush(&mut self) -> Result<()> {
        Ok(())
    }
}