251 lines
6.7 KiB
Rust
251 lines
6.7 KiB
Rust
//! Terminal driver implementation
|
|
use abi::{
|
|
error::Error,
|
|
io::{TerminalInputOptions, TerminalLineOptions, TerminalOptions, TerminalOutputOptions},
|
|
};
|
|
|
|
use crate::{proc::wait::Wait, sync::IrqSafeSpinlock};
|
|
|
|
use super::serial::SerialDevice;
|
|
|
|
struct CharRingInner<const N: usize> {
|
|
rd: usize,
|
|
wr: usize,
|
|
data: [u8; N],
|
|
flags: u8,
|
|
}
|
|
|
|
/// Ring buffer for a character device. Handles reads, writes and channel notifications for a
|
|
/// terminal device.
|
|
pub struct CharRing<const N: usize> {
|
|
wait_read: Wait,
|
|
wait_write: Wait,
|
|
inner: IrqSafeSpinlock<CharRingInner<N>>,
|
|
config: IrqSafeSpinlock<TerminalOptions>,
|
|
}
|
|
|
|
/// Terminal device interface
|
|
pub trait TtyDevice<const N: usize>: SerialDevice {
|
|
/// Returns the ring buffer associated with the device
|
|
fn ring(&self) -> &CharRing<N>;
|
|
|
|
/// Returns `true` if data is ready to be read from or written to the terminal
|
|
fn is_ready(&self, write: bool) -> Result<bool, Error> {
|
|
let ring = self.ring();
|
|
if write {
|
|
todo!();
|
|
} else {
|
|
Ok(ring.is_readable())
|
|
}
|
|
}
|
|
|
|
/// Sends a single byte to the terminal
|
|
fn line_send(&self, byte: u8) -> Result<(), Error> {
|
|
let config = self.ring().config.lock();
|
|
|
|
if byte == b'\n' && config.output.contains(TerminalOutputOptions::NL_TO_CRNL) {
|
|
self.send(b'\r').ok();
|
|
}
|
|
|
|
self.send(byte)
|
|
}
|
|
|
|
/// Receives a single byte from the terminal
|
|
fn recv_byte(&self, mut byte: u8) {
|
|
let ring = self.ring();
|
|
let config = ring.config.lock();
|
|
|
|
if byte == b'\r' && config.input.contains(TerminalInputOptions::CR_TO_NL) {
|
|
byte = b'\n';
|
|
}
|
|
|
|
if byte == b'\n' {
|
|
let echo = config.line.contains(TerminalLineOptions::ECHO)
|
|
|| config
|
|
.line
|
|
.contains(TerminalLineOptions::CANONICAL | TerminalLineOptions::ECHO_NL);
|
|
if config.output.contains(TerminalOutputOptions::NL_TO_CRNL) {
|
|
self.send(b'\r').ok();
|
|
}
|
|
self.send(byte).ok();
|
|
} else if config.line.contains(TerminalLineOptions::ECHO) {
|
|
// TODO proper control
|
|
if byte.is_ascii_control() {
|
|
self.send(b'^').ok();
|
|
self.send(byte + 0x40).ok();
|
|
} else {
|
|
self.send(byte).ok();
|
|
}
|
|
}
|
|
|
|
// TODO handle signals
|
|
|
|
ring.putc(byte, false).ok();
|
|
}
|
|
|
|
/// Reads and processes data from the terminal
|
|
fn line_read(&'static self, data: &mut [u8]) -> Result<usize, Error> {
|
|
let ring = self.ring();
|
|
let mut config = ring.config.lock();
|
|
|
|
if data.is_empty() {
|
|
return Ok(0);
|
|
}
|
|
|
|
if !config.is_canonical() {
|
|
let byte = ring.getc()?;
|
|
data[0] = byte;
|
|
Ok(1)
|
|
} else {
|
|
let mut rem = data.len();
|
|
let mut off = 0;
|
|
|
|
// Run until either end of buffer or return condition is reached
|
|
while rem != 0 {
|
|
drop(config);
|
|
let byte = ring.getc()?;
|
|
config = ring.config.lock();
|
|
|
|
if byte == config.chars.eof && config.is_canonical() {
|
|
break;
|
|
}
|
|
|
|
// TODO handle special characters
|
|
|
|
data[off] = byte;
|
|
off += 1;
|
|
rem -= 1;
|
|
|
|
if byte == b'\n' || byte == b'\r' {
|
|
break;
|
|
}
|
|
}
|
|
|
|
Ok(off)
|
|
}
|
|
}
|
|
|
|
/// Processes and writes the data to the terminal
|
|
fn line_write(&self, data: &[u8]) -> Result<usize, Error> {
|
|
for &byte in data {
|
|
self.line_send(byte)?;
|
|
}
|
|
Ok(data.len())
|
|
}
|
|
|
|
/// Writes raw data to the terminal bypassing the processing functions
|
|
fn raw_write(&self, _data: &[u8]) -> Result<usize, Error> {
|
|
todo!();
|
|
}
|
|
}
|
|
|
|
impl<const N: usize> CharRingInner<N> {
|
|
#[inline]
|
|
const fn is_readable(&self) -> bool {
|
|
if self.rd <= self.wr {
|
|
(self.wr - self.rd) > 0
|
|
} else {
|
|
(self.wr + (N - self.rd)) > 0
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
unsafe fn read_unchecked(&mut self) -> u8 {
|
|
let res = self.data[self.rd];
|
|
self.rd = (self.rd + 1) % N;
|
|
res
|
|
}
|
|
|
|
#[inline]
|
|
unsafe fn write_unchecked(&mut self, ch: u8) {
|
|
self.data[self.wr] = ch;
|
|
self.wr = (self.wr + 1) % N;
|
|
}
|
|
}
|
|
|
|
impl<const N: usize> CharRing<N> {
|
|
/// Constructs an empty ring buffer
|
|
pub const fn new() -> Self {
|
|
Self {
|
|
inner: IrqSafeSpinlock::new(CharRingInner {
|
|
rd: 0,
|
|
wr: 0,
|
|
data: [0; N],
|
|
flags: 0,
|
|
}),
|
|
wait_read: Wait::new("char_ring_read"),
|
|
wait_write: Wait::new("char_ring_write"),
|
|
config: IrqSafeSpinlock::new(TerminalOptions::const_default()),
|
|
}
|
|
}
|
|
|
|
/// Returns `true` if the buffer has data to read
|
|
pub fn is_readable(&self) -> bool {
|
|
let inner = self.inner.lock();
|
|
let config = self.config.lock();
|
|
|
|
if config.is_canonical() {
|
|
let mut rd = inner.rd;
|
|
let mut count = 0usize;
|
|
|
|
loop {
|
|
let readable = if rd <= inner.wr {
|
|
(inner.wr - rd) > 0
|
|
} else {
|
|
(inner.wr + (N - rd)) > 0
|
|
};
|
|
|
|
if !readable {
|
|
break;
|
|
}
|
|
|
|
let byte = inner.data[rd];
|
|
if byte == b'\n' {
|
|
count += 1;
|
|
}
|
|
|
|
rd = (rd + 1) % N;
|
|
}
|
|
|
|
count != 0 || inner.flags != 0
|
|
} else {
|
|
inner.is_readable() || inner.flags != 0
|
|
}
|
|
}
|
|
|
|
/// Reads a single character from the buffer, blocking until available
|
|
pub fn getc(&'static self) -> Result<u8, Error> {
|
|
let mut lock = self.inner.lock();
|
|
loop {
|
|
if !lock.is_readable() && lock.flags == 0 {
|
|
drop(lock);
|
|
self.wait_read.wait(None)?;
|
|
lock = self.inner.lock();
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
let byte = unsafe { lock.read_unchecked() };
|
|
drop(lock);
|
|
self.wait_write.wakeup_one();
|
|
// TODO WAIT_SELECT
|
|
Ok(byte)
|
|
}
|
|
|
|
/// Sends a single character to the buffer
|
|
pub fn putc(&self, ch: u8, blocking: bool) -> Result<(), Error> {
|
|
let mut lock = self.inner.lock();
|
|
if blocking {
|
|
todo!();
|
|
}
|
|
unsafe {
|
|
lock.write_unchecked(ch);
|
|
}
|
|
drop(lock);
|
|
self.wait_read.wakeup_one();
|
|
// TODO WAIT_SELECT
|
|
Ok(())
|
|
}
|
|
}
|