2019-11-11 01:24:29 +01:00
|
|
|
use core::convert::TryInto;
|
|
|
|
use core::ffi::c_void;
|
|
|
|
use core::slice::from_raw_parts;
|
|
|
|
|
2019-11-11 08:21:38 +01:00
|
|
|
use std::ffi::CString;
|
|
|
|
use std::os::raw::{c_uint, c_char};
|
2019-11-16 05:59:08 +01:00
|
|
|
use std::ops::DerefMut;
|
2019-11-12 08:56:28 +01:00
|
|
|
use std::path::Path;
|
2019-11-16 05:59:08 +01:00
|
|
|
use std::pin::Pin;
|
2019-11-11 01:24:29 +01:00
|
|
|
|
2019-11-12 08:46:53 +01:00
|
|
|
use libretro_sys::{Message, PixelFormat, SystemAvInfo, GameGeometry};
|
2019-11-16 05:59:08 +01:00
|
|
|
use num_enum::TryFromPrimitive;
|
2019-11-12 08:46:53 +01:00
|
|
|
|
2019-11-15 04:16:44 +01:00
|
|
|
use super::convert::*;
|
|
|
|
use super::loading::*;
|
2019-11-11 01:24:29 +01:00
|
|
|
|
|
|
|
static mut CB_SINGLETON: StaticCallbacks = StaticCallbacks {
|
2019-11-12 08:56:28 +01:00
|
|
|
handler: None,
|
2019-11-11 01:24:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct StaticCallbacks {
|
2019-11-16 05:59:08 +01:00
|
|
|
handler: Option<Pin<&'static mut dyn Handler>>,
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Sync for StaticCallbacks {}
|
|
|
|
|
|
|
|
impl StaticCallbacks {
|
2019-11-11 08:21:38 +01:00
|
|
|
// helpers for environ cb
|
|
|
|
fn clone_into_void<T: Clone>(data: *mut c_void, source: &T) -> Option<bool> {
|
|
|
|
unsafe { (data as *mut T).as_mut() }?.clone_from(source);
|
|
|
|
Some(true)
|
|
|
|
}
|
|
|
|
fn string_into_void(data: *mut c_void, source: impl AsRef<str>) -> Option<bool> {
|
|
|
|
*unsafe { (data as *mut *const c_char).as_mut()? } = CString::new(source.as_ref()).ok()?.into_raw();
|
|
|
|
Some(true)
|
|
|
|
}
|
|
|
|
fn path_into_void(data: *mut c_void, source: impl AsRef<Path>) -> Option<bool> {
|
|
|
|
Self::string_into_void(data, source.as_ref().to_string_lossy())
|
|
|
|
}
|
|
|
|
fn from_void<T>(data: *mut c_void) -> Option<&'static mut T> {
|
|
|
|
unsafe { (data as *mut T).as_mut() }
|
|
|
|
}
|
|
|
|
fn enum_from_void<T>(data: *mut c_void) -> Option<T>
|
|
|
|
where T: TryFromPrimitive, <T as TryFromPrimitive>::Primitive: 'static
|
|
|
|
{
|
|
|
|
let number = Self::from_void(data).cloned()?;
|
|
|
|
T::try_from_primitive(number).ok()
|
|
|
|
}
|
|
|
|
fn environment_cb_inner(cmd: u32, data: *mut c_void) -> Option<bool> {
|
2019-11-12 08:56:28 +01:00
|
|
|
let mut handler = unsafe { CB_SINGLETON.handler.as_mut() }?;
|
2019-11-11 08:21:38 +01:00
|
|
|
match cmd.try_into().ok()? {
|
2019-11-12 08:46:53 +01:00
|
|
|
EnvCmd::SetRotation => handler.set_rotation(Self::enum_from_void(data)?),
|
|
|
|
EnvCmd::GetOverscan => Self::clone_into_void(data, &handler.get_overscan()?)?,
|
|
|
|
EnvCmd::GetCanDupe => Self::clone_into_void(data, &handler.get_can_dupe()?)?,
|
|
|
|
EnvCmd::SetMessage => handler.set_message(Self::from_void::<Message>(data)?.clone()),
|
|
|
|
EnvCmd::Shutdown => handler.shutdown(),
|
|
|
|
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)?)?),
|
2019-11-12 08:56:28 +01:00
|
|
|
// TODO EnvCmd::SetInputDescriptors => {},
|
|
|
|
// TODO EnvCmd::SetKeyboardCallback => {},
|
|
|
|
// TODO EnvCmd::SetDiskControlInterface => {},
|
|
|
|
// TODO EnvCmd::SetHwRender => {},
|
|
|
|
// TODO EnvCmd::GetVariable => {}, -- also change to mut parameter?
|
|
|
|
// TODO EnvCmd::SetVariables => {},
|
2019-11-12 08:46:53 +01:00
|
|
|
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()?)?,
|
2019-11-12 08:56:28 +01:00
|
|
|
// TODO EnvCmd::SetFrameTimeCallback => {},
|
|
|
|
// TODO EnvCmd::SetAudioCallback => {},
|
|
|
|
// TODO EnvCmd::GetRumbleInterface => {},
|
2019-11-12 08:46:53 +01:00
|
|
|
EnvCmd::GetInputDeviceCapabilities => Self::clone_into_void(data, &handler.get_input_device_capabilities()?)?,
|
2019-11-12 08:56:28 +01:00
|
|
|
// TODO EnvCmd::GetSensorInterface => {},
|
|
|
|
// TODO EnvCmd::GetCameraInterface => {},
|
|
|
|
// TODO EnvCmd::GetLogInterface => {},
|
|
|
|
// TODO EnvCmd::GetPerfInterface => {},
|
|
|
|
// TODO EnvCmd::GetLocationInterface => {},
|
2019-11-12 08:46:53 +01:00
|
|
|
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()),
|
2019-11-12 08:56:28 +01:00
|
|
|
// TODO EnvCmd::SetProcAddressCallback => {},
|
|
|
|
// TODO EnvCmd::SetSubsystemInfo => {},
|
|
|
|
// TODO EnvCmd::SetControllerInfo => {},
|
|
|
|
// TODO EnvCmd::SetMemoryMaps => {},
|
2019-11-12 08:46:53 +01:00
|
|
|
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()?)?,
|
2019-11-12 08:56:28 +01:00
|
|
|
// EnvCmd::SetSerializationQuirks => handler.set_serialization_quirks(Self::from_void(data)?),
|
2019-11-11 08:21:38 +01:00
|
|
|
_ => false,
|
|
|
|
}.into()
|
|
|
|
}
|
2019-11-11 01:24:29 +01:00
|
|
|
extern "C" fn environment_cb(cmd: u32, data: *mut c_void) -> bool {
|
2019-11-11 08:21:38 +01:00
|
|
|
Self::environment_cb_inner(cmd, data).unwrap_or(false)
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
2019-11-11 08:21:38 +01:00
|
|
|
|
|
|
|
extern "C" fn video_refresh_cb(
|
|
|
|
data: *const c_void,
|
|
|
|
width: c_uint,
|
|
|
|
height: c_uint,
|
|
|
|
pitch: usize,
|
|
|
|
) {
|
2019-11-11 01:24:29 +01:00
|
|
|
if !data.is_null() {
|
2019-11-12 08:56:28 +01:00
|
|
|
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
|
2019-11-11 01:24:29 +01:00
|
|
|
let data = data as *const u8;
|
|
|
|
let len = pitch * (height as usize);
|
|
|
|
let slice = unsafe { from_raw_parts(data, len) };
|
|
|
|
cb.video_refresh(slice, width, height, pitch as u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern "C" fn audio_sample_cb(left: i16, right: i16) {
|
2019-11-12 08:56:28 +01:00
|
|
|
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
|
2019-11-11 01:24:29 +01:00
|
|
|
cb.audio_sample(left, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern "C" fn audio_sample_batch_cb(data: *const i16, frames: usize) -> usize {
|
|
|
|
unsafe {
|
2019-11-12 08:56:28 +01:00
|
|
|
match CB_SINGLETON.handler.as_mut() {
|
2019-11-11 01:24:29 +01:00
|
|
|
Some(cb) => match data.is_null() {
|
|
|
|
true => 0,
|
|
|
|
false => cb.audio_sample_batch(from_raw_parts(data, frames)),
|
2019-11-11 08:21:38 +01:00
|
|
|
},
|
2019-11-11 01:24:29 +01:00
|
|
|
None => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern "C" fn input_poll_cb() {
|
2019-11-12 08:56:28 +01:00
|
|
|
unsafe { CB_SINGLETON.handler.as_mut().map(|cb| cb.input_poll()) };
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
|
|
|
extern "C" fn input_state_cb(port: c_uint, device: c_uint, index: c_uint, id: c_uint) -> i16 {
|
2019-11-12 08:56:28 +01:00
|
|
|
match unsafe { CB_SINGLETON.handler.as_mut() } {
|
2019-11-11 01:24:29 +01:00
|
|
|
Some(cb) => match ((device, id).try_into(), index.try_into()) {
|
|
|
|
(Ok(input), Ok(index)) => cb.input_state(port, input, index),
|
|
|
|
_ => 0,
|
|
|
|
},
|
|
|
|
None => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub(crate) fn reset() {
|
|
|
|
unsafe {
|
2019-11-12 08:56:28 +01:00
|
|
|
CB_SINGLETON.handler.take();
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:21:58 +01:00
|
|
|
pub struct LibretroWrapper {
|
|
|
|
api: LibretroApi,
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 06:21:58 +01:00
|
|
|
impl From<LibretroApi> for LibretroWrapper {
|
|
|
|
fn from(api: LibretroApi) -> Self {
|
2019-11-12 08:46:53 +01:00
|
|
|
api.set_environment(StaticCallbacks::environment_cb);
|
|
|
|
api.set_video_refresh(StaticCallbacks::video_refresh_cb);
|
|
|
|
api.set_audio_sample(StaticCallbacks::audio_sample_cb);
|
|
|
|
api.set_audio_sample_batch(StaticCallbacks::audio_sample_batch_cb);
|
|
|
|
api.set_input_poll(StaticCallbacks::input_poll_cb);
|
|
|
|
api.set_input_state(StaticCallbacks::input_state_cb);
|
2019-11-11 08:21:38 +01:00
|
|
|
LibretroWrapper { api }
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:21:58 +01:00
|
|
|
impl AsRef<LibretroApi> for LibretroWrapper {
|
|
|
|
fn as_ref(&self) -> &LibretroApi {
|
2019-11-11 01:24:29 +01:00
|
|
|
&self.api
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:21:58 +01:00
|
|
|
impl Drop for LibretroWrapper {
|
2019-11-11 01:24:29 +01:00
|
|
|
fn drop(&mut self) {
|
|
|
|
StaticCallbacks::reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2019-11-16 05:59:08 +01:00
|
|
|
pub fn register_handler(handler: Pin<&'_ mut (dyn Handler + '_)>) {
|
|
|
|
unsafe {
|
|
|
|
let mut ptr = handler.get_unchecked_mut() as *mut dyn Handler;
|
|
|
|
CB_SINGLETON.handler.replace(Pin::new_unchecked(ptr.as_mut().unwrap()));
|
|
|
|
}
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|