Collapse bespoke handler traits/fields into one

This commit is contained in:
lif 2019-11-11 23:56:28 -08:00
parent 2a8b3f350a
commit b5b5a1041a
3 changed files with 51 additions and 181 deletions

View File

@ -5,14 +5,9 @@ use std::path::PathBuf;
use num_enum::{TryFromPrimitive, IntoPrimitive};
use crate::libretro_ffi::*;
/*
#[derive(TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Region {
NTSC = RETRO_REGION_NTSC,
PAL = RETRO_REGION_PAL,
}
*/
// NB: commented-out stuff is from newer versions of libretro.h not yet represented in libretro-sys
#[derive(TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum DeviceType {
@ -143,47 +138,14 @@ pub enum EnvRotation {
CounterClockwise180 = 2,
CounterClockwise270 = 3,
}
/*
#[derive(TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum EnvPixelFormat {
XRGB555 = retro_pixel_format_RETRO_PIXEL_FORMAT_0RGB1555,
RGB565 = retro_pixel_format_RETRO_PIXEL_FORMAT_RGB565,
XRGB8888 = retro_pixel_format_RETRO_PIXEL_FORMAT_XRGB8888,
Unknown = retro_pixel_format_RETRO_PIXEL_FORMAT_UNKNOWN,
}
*/
pub struct EnvVariable {
key: &'static str,
description: &'static str,
expected_values: Vec<&'static str>,
value: Option<String>,
}
/*
#[derive(TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum EnvLanguage {
English = retro_language_RETRO_LANGUAGE_ENGLISH,
Japanese = retro_language_RETRO_LANGUAGE_JAPANESE,
French = retro_language_RETRO_LANGUAGE_FRENCH,
Spanish = retro_language_RETRO_LANGUAGE_SPANISH,
German = retro_language_RETRO_LANGUAGE_GERMAN,
Italian = retro_language_RETRO_LANGUAGE_ITALIAN,
Dutch = retro_language_RETRO_LANGUAGE_DUTCH,
PortugueseBrazil = retro_language_RETRO_LANGUAGE_PORTUGUESE_BRAZIL,
PortuguesePortugal = retro_language_RETRO_LANGUAGE_PORTUGUESE_PORTUGAL,
Russian = retro_language_RETRO_LANGUAGE_RUSSIAN,
Korean = retro_language_RETRO_LANGUAGE_KOREAN,
ChineseTraditional = retro_language_RETRO_LANGUAGE_CHINESE_TRADITIONAL,
ChineseSimplified = retro_language_RETRO_LANGUAGE_CHINESE_SIMPLIFIED,
Esperanto = retro_language_RETRO_LANGUAGE_ESPERANTO,
Polish = retro_language_RETRO_LANGUAGE_POLISH,
Vietnamese = retro_language_RETRO_LANGUAGE_VIETNAMESE,
Arabic = retro_language_RETRO_LANGUAGE_ARABIC,
Greek = retro_language_RETRO_LANGUAGE_GREEK,
Last = retro_language_RETRO_LANGUAGE_LAST,
}
*/
// TODO: experimental calls
#[derive(TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
@ -227,7 +189,14 @@ pub enum EnvCmd {
// SetSerializationQuirks = ENVIRONMENT_SET_SERIALIZATION_QUIRKS,
}
pub trait HandlesEnvironment: Send + Sync + 'static {
pub trait Handler: Send + Sync + 'static {
fn video_refresh(&mut self, data: &[u8], width: c_uint, height: c_uint, pitch: c_uint) {}
fn audio_sample(&mut self, left: i16, right: i16) {}
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize { stereo_pcm.len() }
fn input_poll(&mut self) {}
fn input_state(&mut self, port: u32, device: Input, index: DeviceIndex) -> i16 { 0 }
// -- environment callbacks --
fn set_rotation(&mut self, rotation: EnvRotation) -> bool { false }
fn get_overscan(&mut self) -> Option<bool> { None }
fn get_can_dupe(&mut self) -> Option<bool> { None }
@ -264,21 +233,5 @@ pub trait HandlesEnvironment: Send + Sync + 'static {
fn set_geometry(&mut self, game_geometry: GameGeometry) -> bool { false }
fn get_username(&mut self) -> Option<String> { None }
fn get_language(&mut self) -> Option<Language> { None }
fn set_serialization_quirks(&mut self, quirks: &mut u64) -> bool { false }
}
pub trait HandlesVideoRefresh: Send + Sync + 'static {
fn video_refresh(&mut self, data: &[u8], width: c_uint, height: c_uint, pitch: c_uint);
}
pub trait HandlesAudioSample: Send + Sync + 'static {
fn audio_sample(&mut self, left: i16, right: i16);
}
pub trait HandlesAudioSampleBatch: Send + Sync + 'static {
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize;
}
pub trait HandlesInputPoll: Send + Sync + 'static {
fn input_poll(&mut self);
}
pub trait HandlesInputState: Send + Sync + 'static {
fn input_state(&mut self, port: u32, device: Input, index: DeviceIndex) -> i16;
// fn set_serialization_quirks(&mut self, quirks: &mut u64) -> bool { false }
}

View File

@ -4,31 +4,21 @@ use core::slice::from_raw_parts;
use std::ffi::CString;
use std::os::raw::{c_uint, c_char};
use std::path::Path;
use libretro_sys::{Message, PixelFormat, SystemAvInfo, GameGeometry};
use num_enum::{IntoPrimitive, TryFromPrimitive};
use crate::libretro_convert::*;
use crate::libretro_loading::*;
use std::path::Path;
use num_enum::{IntoPrimitive, TryFromPrimitive};
static mut CB_SINGLETON: StaticCallbacks = StaticCallbacks {
environment: None,
video_refresh: None,
audio_sample: None,
audio_sample_batch: None,
input_poll: None,
input_state: None,
handler: None,
};
#[derive(Default)]
struct StaticCallbacks {
environment: Option<&'static mut dyn HandlesEnvironment>,
video_refresh: Option<&'static mut dyn HandlesVideoRefresh>,
audio_sample: Option<&'static mut dyn HandlesAudioSample>,
audio_sample_batch: Option<&'static mut dyn HandlesAudioSampleBatch>,
input_poll: Option<&'static mut dyn HandlesInputPoll>,
input_state: Option<&'static mut dyn HandlesInputState>,
handler: Option<&'static mut dyn Handler>,
}
unsafe impl Sync for StaticCallbacks {}
@ -56,7 +46,7 @@ impl StaticCallbacks {
T::try_from_primitive(number).ok()
}
fn environment_cb_inner(cmd: u32, data: *mut c_void) -> Option<bool> {
let mut handler = unsafe { CB_SINGLETON.environment.as_mut() }?;
let mut handler = unsafe { CB_SINGLETON.handler.as_mut() }?;
match cmd.try_into().ok()? {
EnvCmd::SetRotation => handler.set_rotation(Self::enum_from_void(data)?),
EnvCmd::GetOverscan => Self::clone_into_void(data, &handler.get_overscan()?)?,
@ -66,35 +56,35 @@ impl StaticCallbacks {
EnvCmd::SetPerformanceLevel => handler.set_performance_level(*Self::from_void(data)?),
EnvCmd::GetSystemDirectory => Self::path_into_void(data, handler.get_system_directory()?)?,
EnvCmd::SetPixelFormat => handler.set_pixel_format(PixelFormat::from_uint(*Self::from_void(data)?)?),
// TODO EnvironmentCmdData::SetInputDescriptors => {},
// TODO EnvironmentCmdData::SetKeyboardCallback => {},
// TODO EnvironmentCmdData::SetDiskControlInterface => {},
// TODO EnvironmentCmdData::SetHwRender => {},
// TODO EnvironmentCmdData::GetVariable => {}, -- also change to mut parameter?
// TODO EnvironmentCmdData::SetVariables => {},
// TODO EnvCmd::SetInputDescriptors => {},
// TODO EnvCmd::SetKeyboardCallback => {},
// TODO EnvCmd::SetDiskControlInterface => {},
// TODO EnvCmd::SetHwRender => {},
// TODO EnvCmd::GetVariable => {}, -- also change to mut parameter?
// TODO EnvCmd::SetVariables => {},
EnvCmd::GetVariableUpdate => Self::clone_into_void(data, &handler.get_variable_update()?)?,
EnvCmd::SetSupportNoGame => handler.set_support_no_game(*Self::from_void(data)?),
EnvCmd::GetLibretroPath => Self::path_into_void(data, handler.get_libretro_path()?)?,
// TODO EnvironmentCmdData::SetFrameTimeCallback => {},
// TODO EnvironmentCmdData::SetAudioCallback => {},
// TODO EnvironmentCmdData::GetRumbleInterface => {},
// TODO EnvCmd::SetFrameTimeCallback => {},
// TODO EnvCmd::SetAudioCallback => {},
// TODO EnvCmd::GetRumbleInterface => {},
EnvCmd::GetInputDeviceCapabilities => Self::clone_into_void(data, &handler.get_input_device_capabilities()?)?,
// TODO EnvironmentCmdData::GetSensorInterface => {},
// TODO EnvironmentCmdData::GetCameraInterface => {},
// TODO EnvironmentCmdData::GetLogInterface => {},
// TODO EnvironmentCmdData::GetPerfInterface => {},
// TODO EnvironmentCmdData::GetLocationInterface => {},
// TODO EnvCmd::GetSensorInterface => {},
// TODO EnvCmd::GetCameraInterface => {},
// TODO EnvCmd::GetLogInterface => {},
// TODO EnvCmd::GetPerfInterface => {},
// TODO EnvCmd::GetLocationInterface => {},
EnvCmd::GetCoreAssetsDirectory => Self::path_into_void(data, handler.get_core_assets_directory()?)?,
EnvCmd::GetSaveDirectory => Self::path_into_void(data, handler.get_save_directory()?)?,
EnvCmd::SetSystemAvInfo => handler.set_system_av_info(Self::from_void::<SystemAvInfo>(data)?.clone()),
// TODO EnvironmentCmdData::SetProcAddressCallback => {},
// TODO EnvironmentCmdData::SetSubsystemInfo => {},
// TODO EnvironmentCmdData::SetControllerInfo => {},
// TODO EnvironmentCmdData::SetMemoryMaps => {},
// TODO EnvCmd::SetProcAddressCallback => {},
// TODO EnvCmd::SetSubsystemInfo => {},
// TODO EnvCmd::SetControllerInfo => {},
// TODO EnvCmd::SetMemoryMaps => {},
EnvCmd::SetGeometry => handler.set_geometry(Self::from_void::<GameGeometry>(data)?.clone()),
EnvCmd::GetUsername => Self::string_into_void(data, handler.get_username()?)?,
EnvCmd::GetLanguage => Self::clone_into_void(data, &handler.get_language()?)?,
// EnvironmentCmdData::SetSerializationQuirks => handler.set_serialization_quirks(Self::from_void(data)?),
// EnvCmd::SetSerializationQuirks => handler.set_serialization_quirks(Self::from_void(data)?),
_ => false,
}.into()
}
@ -109,7 +99,7 @@ impl StaticCallbacks {
pitch: usize,
) {
if !data.is_null() {
if let Some(cb) = unsafe { CB_SINGLETON.video_refresh.as_mut() } {
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
let data = data as *const u8;
let len = pitch * (height as usize);
let slice = unsafe { from_raw_parts(data, len) };
@ -118,13 +108,13 @@ impl StaticCallbacks {
}
}
extern "C" fn audio_sample_cb(left: i16, right: i16) {
if let Some(cb) = unsafe { CB_SINGLETON.audio_sample.as_mut() } {
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
cb.audio_sample(left, right);
}
}
extern "C" fn audio_sample_batch_cb(data: *const i16, frames: usize) -> usize {
unsafe {
match CB_SINGLETON.audio_sample_batch.as_mut() {
match CB_SINGLETON.handler.as_mut() {
Some(cb) => match data.is_null() {
true => 0,
false => cb.audio_sample_batch(from_raw_parts(data, frames)),
@ -134,10 +124,10 @@ impl StaticCallbacks {
}
}
extern "C" fn input_poll_cb() {
unsafe { CB_SINGLETON.input_poll.as_mut().map(|cb| cb.input_poll()) };
unsafe { CB_SINGLETON.handler.as_mut().map(|cb| cb.input_poll()) };
}
extern "C" fn input_state_cb(port: c_uint, device: c_uint, index: c_uint, id: c_uint) -> i16 {
match unsafe { CB_SINGLETON.input_state.as_mut() } {
match unsafe { CB_SINGLETON.handler.as_mut() } {
Some(cb) => match ((device, id).try_into(), index.try_into()) {
(Ok(input), Ok(index)) => cb.input_state(port, input, index),
_ => 0,
@ -145,44 +135,14 @@ impl StaticCallbacks {
None => 0,
}
}
pub(crate) fn set_environment(cb: &'static mut dyn HandlesEnvironment) {
pub(crate) fn set_environment(cb: &'static mut dyn Handler) {
unsafe {
CB_SINGLETON.environment.replace(cb);
}
}
pub(crate) fn set_video_refresh(cb: &'static mut dyn HandlesVideoRefresh) {
unsafe {
CB_SINGLETON.video_refresh.replace(cb);
}
}
pub(crate) fn set_audio_sample(cb: &'static mut dyn HandlesAudioSample) {
unsafe {
CB_SINGLETON.audio_sample.replace(cb);
}
}
pub(crate) fn set_audio_sample_batch(cb: &'static mut dyn HandlesAudioSampleBatch) {
unsafe {
CB_SINGLETON.audio_sample_batch.replace(cb);
}
}
pub(crate) fn set_input_poll(cb: &'static mut dyn HandlesInputPoll) {
unsafe {
CB_SINGLETON.input_poll.replace(cb);
}
}
pub(crate) fn set_input_state(cb: &'static mut dyn HandlesInputState) {
unsafe {
CB_SINGLETON.input_state.replace(cb);
CB_SINGLETON.handler.replace(cb);
}
}
pub(crate) fn reset() {
unsafe {
CB_SINGLETON.environment.take();
CB_SINGLETON.video_refresh.take();
CB_SINGLETON.audio_sample.take();
CB_SINGLETON.audio_sample_batch.take();
CB_SINGLETON.input_poll.take();
CB_SINGLETON.input_state.take();
CB_SINGLETON.handler.take();
}
}
}
@ -218,47 +178,8 @@ impl<'a> Drop for LibretroWrapper<'a> {
// a note on lifetimes: we explicitly lie about them here because as long as they live as long as
// the library wrapper itself we're good (we wipe our 'static references on drop() too)
impl<'a> LibretroWrapper<'a> {
pub fn register_environment_handler(&self, handler: &'a mut dyn HandlesEnvironment) {
let ptr: *mut dyn HandlesEnvironment = handler;
pub fn register_handler(&self, handler: &'a mut dyn Handler) {
let ptr: *mut dyn Handler = handler;
StaticCallbacks::set_environment(unsafe { ptr.as_mut() }.unwrap());
}
pub fn register_video_refresh_handler(&self, handler: &'a mut dyn HandlesVideoRefresh) {
let ptr: *mut dyn HandlesVideoRefresh = handler;
StaticCallbacks::set_video_refresh(unsafe { ptr.as_mut() }.unwrap());
}
pub fn register_audio_sample_handler(&self, handler: &'a mut dyn HandlesAudioSample) {
let ptr: *mut dyn HandlesAudioSample = handler;
StaticCallbacks::set_audio_sample(unsafe { ptr.as_mut() }.unwrap());
}
pub fn register_audio_sample_batch_handler(
&self,
handler: &'a mut dyn HandlesAudioSampleBatch,
) {
let ptr: *mut dyn HandlesAudioSampleBatch = handler;
StaticCallbacks::set_audio_sample_batch(unsafe { ptr.as_mut() }.unwrap());
}
pub fn register_input_poll_handler(&self, handler: &'a mut dyn HandlesInputPoll) {
let ptr: *mut dyn HandlesInputPoll = handler;
StaticCallbacks::set_input_poll(unsafe { ptr.as_mut() }.unwrap());
}
pub fn register_input_state_handler(&self, handler: &'a mut dyn HandlesInputState) {
let ptr: *mut dyn HandlesInputState = handler;
StaticCallbacks::set_input_state(unsafe { ptr.as_mut() }.unwrap());
}
pub fn register_for_all<T>(&self, handler: &'a mut T)
where
T: HandlesEnvironment
+ HandlesVideoRefresh
+ HandlesAudioSample
+ HandlesAudioSampleBatch
+ HandlesInputPoll
+ HandlesInputState,
{
self.register_environment_handler(handler);
self.register_video_refresh_handler(handler);
self.register_audio_sample_handler(handler);
self.register_audio_sample_batch_handler(handler);
self.register_input_poll_handler(handler);
self.register_input_state_handler(handler);
}
}

View File

@ -12,13 +12,10 @@ use std::path::PathBuf;
use structopt::StructOpt;
use crate::libretro_convert::{HandlesEnvironment, HandlesVideoRefresh};
use crate::libretro_convert::Handler;
struct Foo;
impl HandlesEnvironment for Foo {
}
impl HandlesVideoRefresh for Foo {
impl Handler for Foo {
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
println!("video_refresh {}x{}", width, height);
}
@ -41,8 +38,7 @@ fn main() -> failure::Fallible<()> {
let retro = libretro_loading::LibretroApi::from_library(&lib)?;
let wrapper = libretro_wrapper::LibretroWrapper::from(retro);
let mut foo = Foo{};
wrapper.register_environment_handler(&mut foo);
wrapper.register_video_refresh_handler(&mut foo);
wrapper.register_handler(&mut foo);
println!("api version: {}", wrapper.as_ref().api_version());
println!("name: {}", unsafe { CStr::from_ptr(wrapper.as_ref().get_system_info().library_name) }.to_string_lossy());
wrapper.as_ref().init();