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
use std::fmt;
use std::error::Error;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use femtos::Instant;

use crate::gfx::FrameReceiver;
use crate::audio::Sample;
use crate::keys::KeyEvent;
use crate::controllers::ControllerEvent;
use crate::mouse::MouseEvent;
use crate::input::EventSender;

#[derive(Clone, Debug, thiserror::Error)]
pub enum HostError<E> {
    TTYNotSupported,
    VideoSourceNotSupported,
    AudioSourceNotSupported,
    ControllerNotSupported,
    KeyboardNotSupported,
    MouseNotSupported,
    #[from(E)]
    Specific(E),
}


impl<E> fmt::Display for HostError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            HostError::TTYNotSupported => write!(f, "This frontend doesn't support PTYs"),
            HostError::VideoSourceNotSupported => write!(f, "This frontend doesn't support windows"),
            HostError::AudioSourceNotSupported => write!(f, "This frontend doesn't support the sound"),
            HostError::ControllerNotSupported => write!(f, "This frontend doesn't support game controllers"),
            HostError::KeyboardNotSupported => write!(f, "This frontend doesn't support the keyboard"),
            HostError::MouseNotSupported => write!(f, "This frontend doesn't support the mouse"),
            HostError::Specific(err) => write!(f, "{}", err),
        }
    }
}

pub trait Host {
    type Error: Error;

    fn add_pty(&self) -> Result<Box<dyn Tty>, HostError<Self::Error>> {
        Err(HostError::TTYNotSupported)
    }

    fn add_video_source(&mut self, _receiver: FrameReceiver) -> Result<(), HostError<Self::Error>> {
        Err(HostError::VideoSourceNotSupported)
    }

    fn add_audio_source(&mut self) -> Result<Box<dyn Audio>, HostError<Self::Error>> {
        Err(HostError::AudioSourceNotSupported)
    }

    fn register_controllers(&mut self, _sender: EventSender<ControllerEvent>) -> Result<(), HostError<Self::Error>> {
        Err(HostError::ControllerNotSupported)
    }

    fn register_keyboard(&mut self, _sender: EventSender<KeyEvent>) -> Result<(), HostError<Self::Error>> {
        Err(HostError::KeyboardNotSupported)
    }

    fn register_mouse(&mut self, _sender: EventSender<MouseEvent>) -> Result<(), HostError<Self::Error>> {
        Err(HostError::MouseNotSupported)
    }
}


pub trait Tty {
    fn device_name(&self) -> String;
    fn read(&mut self) -> Option<u8>;
    fn write(&mut self, output: u8) -> bool;
}

pub trait Audio {
    fn samples_per_second(&self) -> usize;
    fn write_samples(&mut self, clock: Instant, buffer: &[Sample]);
}


#[derive(Clone, Default)]
pub struct ClockedQueue<T>(Arc<Mutex<VecDeque<(Instant, T)>>>, usize);

impl<T: Clone> ClockedQueue<T> {
    pub fn new(max: usize) -> Self {
        Self(Arc::new(Mutex::new(VecDeque::new())), max)
    }

    pub fn push(&self, clock: Instant, data: T) {
        let mut queue = self.0.lock().unwrap();
        if queue.len() > self.1 {
            //log::warn!("dropping data from queue due to limit of {} items", self.1);
            queue.pop_front();
        }
        queue.push_back((clock, data));
    }

    pub fn pop_next(&self) -> Option<(Instant, T)> {
        self.0.lock().unwrap().pop_front()
    }

    pub fn pop_latest(&self) -> Option<(Instant, T)> {
        self.0.lock().unwrap().drain(..).last()
    }

    pub fn put_back(&self, clock: Instant, data: T) {
        self.0.lock().unwrap().push_front((clock, data));
    }

    pub fn peek_clock(&self) -> Option<Instant> {
        self.0.lock().unwrap().front().map(|(clock, _)| *clock)
    }

    pub fn is_empty(&self) -> bool {
        self.0.lock().unwrap().is_empty()
    }
}


pub struct DummyAudio();

impl Audio for DummyAudio {
    fn samples_per_second(&self) -> usize {
        48000
    }

    fn write_samples(&mut self, _clock: Instant, _buffer: &[Sample]) {}
}