2021-10-22 04:55:27 +00:00
|
|
|
|
2022-10-02 20:07:15 +00:00
|
|
|
use std::collections::VecDeque;
|
2021-11-18 16:46:41 +00:00
|
|
|
use std::sync::{Arc, Mutex, MutexGuard};
|
|
|
|
|
2022-10-02 20:07:15 +00:00
|
|
|
use crate::{Clock, Error};
|
2022-09-30 02:55:12 +00:00
|
|
|
use crate::host::gfx::Frame;
|
2022-10-03 03:39:02 +00:00
|
|
|
use crate::host::keys::KeyEvent;
|
2021-12-04 22:41:27 +00:00
|
|
|
use crate::host::controllers::{ControllerDevice, ControllerEvent};
|
2022-10-03 03:39:02 +00:00
|
|
|
use crate::host::mouse::MouseEvent;
|
2021-10-22 04:55:27 +00:00
|
|
|
|
|
|
|
pub trait Host {
|
2021-12-07 03:52:04 +00:00
|
|
|
fn create_pty(&self) -> Result<Box<dyn Tty>, Error> {
|
|
|
|
Err(Error::new("This frontend doesn't support PTYs"))
|
|
|
|
}
|
|
|
|
|
2021-12-12 23:20:09 +00:00
|
|
|
fn add_window(&mut self, _updater: Box<dyn WindowUpdater>) -> Result<(), Error> {
|
2021-12-07 03:52:04 +00:00
|
|
|
Err(Error::new("This frontend doesn't support windows"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn register_controller(&mut self, _device: ControllerDevice, _input: Box<dyn ControllerUpdater>) -> Result<(), Error> {
|
|
|
|
Err(Error::new("This frontend doesn't support game controllers"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn register_keyboard(&mut self, _input: Box<dyn KeyboardUpdater>) -> Result<(), Error> {
|
|
|
|
Err(Error::new("This frontend doesn't support the keyboard"))
|
|
|
|
}
|
2021-12-12 23:20:09 +00:00
|
|
|
|
2022-10-03 03:39:02 +00:00
|
|
|
fn register_mouse(&mut self, _input: Box<dyn MouseUpdater>) -> Result<(), Error> {
|
|
|
|
Err(Error::new("This frontend doesn't support the mouse"))
|
|
|
|
}
|
|
|
|
|
2021-12-12 23:20:09 +00:00
|
|
|
fn create_audio_source(&mut self) -> Result<Box<dyn Audio>, Error> {
|
|
|
|
Err(Error::new("This frontend doesn't support the sound"))
|
|
|
|
}
|
2021-10-22 04:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-12 23:20:09 +00:00
|
|
|
|
2021-10-23 02:36:05 +00:00
|
|
|
pub trait Tty {
|
|
|
|
fn device_name(&self) -> String;
|
|
|
|
fn read(&mut self) -> Option<u8>;
|
|
|
|
fn write(&mut self, output: u8) -> bool;
|
2021-10-22 04:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 02:36:05 +00:00
|
|
|
pub trait WindowUpdater: Send {
|
2022-10-01 19:12:25 +00:00
|
|
|
fn max_size(&mut self) -> (u32, u32);
|
|
|
|
fn take_frame(&mut self) -> Result<Frame, Error>;
|
|
|
|
|
|
|
|
fn update_frame(&mut self, width: u32, _height: u32, bitmap: &mut [u32]) {
|
|
|
|
if let Ok(frame) = self.take_frame() {
|
|
|
|
for y in 0..frame.height {
|
|
|
|
for x in 0..frame.width {
|
|
|
|
bitmap[(x + (y * width)) as usize] = frame.bitmap[(x + (y * frame.width)) as usize];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 04:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 23:04:41 +00:00
|
|
|
pub trait ControllerUpdater: Send {
|
2021-12-04 22:41:27 +00:00
|
|
|
fn update_controller(&mut self, event: ControllerEvent);
|
2021-10-31 18:00:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 04:44:25 +00:00
|
|
|
pub trait KeyboardUpdater: Send {
|
2022-10-03 03:39:02 +00:00
|
|
|
fn update_keyboard(&mut self, event: KeyEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait MouseUpdater: Send {
|
|
|
|
fn update_mouse(&mut self, event: MouseEvent);
|
2021-11-07 04:44:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-12 23:20:09 +00:00
|
|
|
pub trait Audio {
|
|
|
|
fn samples_per_second(&self) -> usize;
|
2022-01-19 04:24:18 +00:00
|
|
|
fn space_available(&self) -> usize;
|
2022-10-08 20:26:17 +00:00
|
|
|
fn write_samples(&mut self, clock: Clock, buffer: &[f32]);
|
2022-01-26 19:12:09 +00:00
|
|
|
fn flush(&mut self);
|
2021-12-12 23:20:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 04:01:18 +00:00
|
|
|
pub trait BlitableSurface {
|
|
|
|
fn set_size(&mut self, width: u32, height: u32);
|
2021-12-31 06:46:57 +00:00
|
|
|
fn set_pixel(&mut self, pos_x: u32, pos_y: u32, pixel: u32);
|
2021-10-28 04:01:18 +00:00
|
|
|
fn blit<B: Iterator<Item=u32>>(&mut self, pos_x: u32, pos_y: u32, bitmap: B, width: u32, height: u32);
|
2021-11-07 04:44:25 +00:00
|
|
|
fn clear(&mut self, value: u32);
|
2021-10-28 04:01:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 18:00:14 +00:00
|
|
|
|
2021-11-18 16:46:41 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2021-12-07 22:29:38 +00:00
|
|
|
pub struct HostData<T>(Arc<Mutex<T>>);
|
2021-11-18 16:46:41 +00:00
|
|
|
|
2021-12-07 22:29:38 +00:00
|
|
|
impl<T> HostData<T> {
|
|
|
|
pub fn new(init: T) -> HostData<T> {
|
|
|
|
HostData(Arc::new(Mutex::new(init)))
|
2021-11-18 16:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lock(&self) -> MutexGuard<'_, T> {
|
|
|
|
self.0.lock().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:29:38 +00:00
|
|
|
impl<T: Copy> HostData<T> {
|
2021-11-18 16:46:41 +00:00
|
|
|
pub fn set(&mut self, value: T) {
|
|
|
|
*(self.0.lock().unwrap()) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&mut self) -> T {
|
|
|
|
*(self.0.lock().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 20:07:15 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ClockedQueue<T>(Arc<Mutex<VecDeque<(Clock, T)>>>);
|
|
|
|
|
|
|
|
impl<T: Clone> ClockedQueue<T> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self(Arc::new(Mutex::new(VecDeque::new())))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&self, clock: Clock, data: T) {
|
|
|
|
self.0.lock().unwrap().push_back((clock, data));
|
|
|
|
}
|
|
|
|
|
2022-10-08 20:26:17 +00:00
|
|
|
pub fn pop_next(&self) -> Option<(Clock, T)> {
|
|
|
|
self.0.lock().unwrap().pop_front()
|
|
|
|
}
|
|
|
|
|
2022-10-02 20:07:15 +00:00
|
|
|
pub fn pop_latest(&self) -> Option<(Clock, T)> {
|
|
|
|
self.0.lock().unwrap().drain(..).last()
|
|
|
|
}
|
2022-10-08 20:26:17 +00:00
|
|
|
|
|
|
|
pub fn unpop(&mut self, clock: Clock, data: T) {
|
|
|
|
self.0.lock().unwrap().push_front((clock, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn peek_clock(&self) -> Option<Clock> {
|
|
|
|
self.0.lock().unwrap().front().map(|(clock, _)| *clock)
|
|
|
|
}
|
2022-10-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-30 02:55:12 +00:00
|
|
|
pub struct DummyAudio();
|
|
|
|
|
|
|
|
impl Audio for DummyAudio {
|
|
|
|
fn samples_per_second(&self) -> usize {
|
|
|
|
48000
|
|
|
|
}
|
|
|
|
|
|
|
|
fn space_available(&self) -> usize {
|
|
|
|
4800
|
|
|
|
}
|
|
|
|
|
2022-10-08 20:26:17 +00:00
|
|
|
fn write_samples(&mut self, _clock: Clock, _buffer: &[f32]) {}
|
2022-09-30 02:55:12 +00:00
|
|
|
|
|
|
|
fn flush(&mut self) {}
|
|
|
|
}
|
|
|
|
|