2021-10-22 04:55:27 +00:00
|
|
|
|
2021-11-18 16:46:41 +00:00
|
|
|
use std::sync::{Arc, Mutex, MutexGuard};
|
|
|
|
|
2021-10-22 04:55:27 +00:00
|
|
|
use crate::error::Error;
|
2021-11-07 04:44:25 +00:00
|
|
|
use crate::host::keys::Key;
|
2021-12-04 22:41:27 +00:00
|
|
|
use crate::host::controllers::{ControllerDevice, ControllerEvent};
|
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
|
|
|
|
|
|
|
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 {
|
2021-11-11 17:52:18 +00:00
|
|
|
fn get_size(&mut self) -> (u32, u32);
|
2021-10-23 02:36:05 +00:00
|
|
|
fn update_frame(&mut self, width: u32, height: u32, bitmap: &mut [u32]);
|
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 {
|
|
|
|
fn update_keyboard(&mut self, key: Key, state: bool);
|
|
|
|
}
|
|
|
|
|
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-01-17 05:42:07 +00:00
|
|
|
fn write_samples(&mut self, 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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|