use core::convert::TryInto; use core::ffi::c_void; use core::slice::from_raw_parts; use std::ffi::{CString, CStr}; use std::ops::Deref; use std::os::raw::{c_uint, c_char}; use std::path::{Path, PathBuf}; use std::pin::Pin; use num_enum::TryFromPrimitive; use super::constants::*; use super::ffi::*; use super::loading::*; use std::time::Duration; static mut CB_SINGLETON: StaticCallbacks = StaticCallbacks { handler: None, }; // stable Rust doesn't have varargs, so we can't represent a callback with the signature of // void (*retro_log_printf_t)(enum retro_log_level level, const char* fmt, ...) // without a little help from an Actual-C wrapper. pub type WrappedLogPrintFn = extern "C" fn(level: LogLevel, fmt: *const c_char); extern "C" { fn c_ext_handle_get_log_interface(cb: *mut LogCallback) -> bool; fn c_ext_set_log_print_cb(cb: WrappedLogPrintFn); } #[allow(unused)] pub trait Handler: Unpin + 'static { fn _delegate_handler(&self) -> Option<&mut dyn Handler> { None } // -- main callbacks -- 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 { None } fn get_can_dupe(&mut self) -> Option { None } fn set_message(&mut self, message: Message) -> bool { false } fn shutdown(&mut self) -> bool { false } fn set_performance_level(&mut self, level: c_uint) -> bool { false } fn get_system_directory(&mut self) -> Option { None } fn set_pixel_format(&mut self, format: PixelFormat) -> bool { false } fn set_input_descriptors(&mut self, input_descriptors: &[InputDescriptor]) -> bool { false } fn set_hw_render(&mut self, hw_render_callback: HwRenderCallback) -> bool { false } fn get_variable(&mut self) -> Option { None } fn set_variables(&mut self, variables: &[EnvVariable]) -> bool { false } fn get_variable_update(&mut self) -> Option { None } fn set_support_no_game(&mut self, supports_no_game: bool) -> bool { false } fn get_libretro_path(&mut self) -> Option { None } fn get_input_device_capabilities(&mut self) -> Option { None } fn get_sensor_interface(&mut self) -> Option { None } fn get_camera_interface(&mut self) -> Option { None } fn get_log_interface(&mut self) -> Option { None } fn get_perf_interface(&mut self) -> Option { None } fn get_location_interface(&mut self) -> Option { None } fn get_core_assets_directory(&mut self) -> Option { None } fn get_save_directory(&mut self) -> Option { None } fn set_system_av_info(&mut self, system_av_info: SystemAvInfo) -> bool { false } fn set_proc_address_callback(&mut self, cb: GetProcAddressInterface) -> bool { false } fn set_subsystem_info(&mut self, subsystem_info: SubsystemInfo) -> bool { false } fn set_controller_info(&mut self, controller_info: ControllerInfo) -> bool { false } fn set_memory_maps(&mut self, memory_map: MemoryMap) -> bool { false } fn set_geometry(&mut self, game_geometry: GameGeometry) -> bool { false } fn get_username(&mut self) -> Option { None } fn get_language(&mut self) -> Option { None } // fn set_serialization_quirks(&mut self, quirks: &mut u64) -> bool { false } // -- environment-set callbacks (API extensions) -- fn log_print(&mut self, level: LogLevel, msg: &str) {} fn set_rumble_state(&mut self, port: c_uint, effect: RumbleEffect, strength: u16) -> bool { false } } #[derive(Default)] struct StaticCallbacks { handler: Option>, } unsafe impl Sync for StaticCallbacks {} impl StaticCallbacks { // helpers for environ cb fn clone_into_void(data: *mut c_void, source: &T) -> Option { unsafe { (data as *mut T).as_mut() }?.clone_from(source); Some(true) } fn string_into_void(data: *mut c_void, source: impl AsRef) -> Option { *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) -> Option { Self::string_into_void(data, source.as_ref().to_string_lossy()) } fn from_void(data: *mut c_void) -> Option<&'static mut T> { unsafe { (data as *mut T).as_mut() } } fn enum_from_void(data: *mut c_void) -> Option where T: 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 { let 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()?)?, EnvCmd::GetCanDupe => Self::clone_into_void(data, &handler.get_can_dupe()?)?, EnvCmd::SetMessage => handler.set_message(Self::from_void::(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)?)?), // TODO EnvCmd::SetInputDescriptors => {}, // TODO EnvCmd::SetKeyboardCallback => {}, // TODO EnvCmd::SetDiskControlInterface => {}, // TODO EnvCmd::SetHwRender => {}, EnvCmd::GetVariable => { // TODO: actually implement let v = Self::from_void::(data)?; eprintln!( "Unsupported env cmd: GetVariable ({})", unsafe { CStr::from_ptr(v.key) }.to_string_lossy()); false }, // 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 EnvCmd::SetFrameTimeCallback => {}, // TODO EnvCmd::SetAudioCallback => {}, EnvCmd::GetRumbleInterface => { let ri = RumbleInterface { set_rumble_state: StaticCallbacks::set_rumble_state_cb }; Self::clone_into_void(data, &ri)? }, EnvCmd::GetInputDeviceCapabilities => Self::clone_into_void(data, &handler.get_input_device_capabilities()?)?, // TODO EnvCmd::GetSensorInterface => {}, // TODO EnvCmd::GetCameraInterface => {}, EnvCmd::GetLogInterface => unsafe { c_ext_handle_get_log_interface(data as *mut LogCallback) }, // 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::(data)?.clone()), // TODO EnvCmd::SetProcAddressCallback => {}, // TODO EnvCmd::SetSubsystemInfo => {}, // TODO EnvCmd::SetControllerInfo => {}, // TODO EnvCmd::SetMemoryMaps => {}, EnvCmd::SetGeometry => handler.set_geometry(Self::from_void::(data)?.clone()), EnvCmd::GetUsername => Self::string_into_void(data, handler.get_username()?)?, EnvCmd::GetLanguage => Self::clone_into_void(data, &handler.get_language()?)?, // EnvCmd::SetSerializationQuirks => handler.set_serialization_quirks(Self::from_void(data)?), x => { eprintln!("Unsupported env cmd: {:?}", x); false }, }.into() } extern "C" fn environment_cb(cmd: u32, data: *mut c_void) -> bool { Self::environment_cb_inner(cmd, data).unwrap_or(false) } extern "C" fn video_refresh_cb( data: *const c_void, width: c_uint, height: c_uint, pitch: usize, ) { if !data.is_null() && data != HW_FRAME_BUFFER_VALID { 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) }; cb.video_refresh(slice, width, height, pitch as u32); } } } extern "C" fn audio_sample_cb(left: i16, right: i16) { 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.handler.as_mut() { Some(cb) => match data.is_null() { true => 0, false => { let len = frames * 2; // stereo let result = cb.audio_sample_batch(from_raw_parts(data, len)); result / 2 }, }, None => 0, } } } extern "C" fn input_poll_cb() { 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.handler.as_mut() } { Some(cb) => match ((device, id).try_into(), index.try_into()) { (Ok(input), Ok(index)) => cb.input_state(port, input, index), _ => 0, }, None => 0, } } extern "C" fn log_print_cb(level: LogLevel, msg: *const c_char) { unsafe { if let Some(cb) = CB_SINGLETON.handler.as_mut() { cb.log_print(level, CStr::from_ptr(msg).to_string_lossy().as_ref()); } } } extern "C" fn set_rumble_state_cb(port: c_uint, effect: RumbleEffect, strength: u16) -> bool { match unsafe { CB_SINGLETON.handler.as_mut() } { Some(cb) => cb.set_rumble_state(port, effect, strength), None => false, } } } pub struct LibretroWrapper { api: LibretroApi, keyboard_event_cb: Option, frame_time_cb: Option, audio_ready_cb: Option, audio_set_state_cb: Option, } impl From for LibretroWrapper { fn from(api: LibretroApi) -> Self { 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); unsafe { c_ext_set_log_print_cb(StaticCallbacks::log_print_cb); } LibretroWrapper { api, keyboard_event_cb: None, frame_time_cb: None, audio_ready_cb: None, audio_set_state_cb: None } } } impl LibretroWrapper { // TODO: enum for the RETROK_* constants instead of c_uint for keycode... pub fn keyboard_event(&self, down: bool, keycode: c_uint, character: u32, key_modifiers: u16) { self.keyboard_event_cb.map(|f| unsafe { f(down, keycode, character, key_modifiers) }); } pub fn frame_time(&self, time: Duration) { self.frame_time_cb.map(|f| unsafe { f(time.as_micros() as Usec) }); } pub fn audio_ready(&self) { self.audio_ready_cb.map(|f| unsafe { f() }); } pub fn audio_set_state(&self, enabled: bool) { self.audio_set_state_cb.map(|f| unsafe { f(enabled) }); } } impl Deref for LibretroWrapper { type Target = LibretroApi; fn deref(&self) -> &LibretroApi { &self.api } } // 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) pub fn set_handler(handler: Pin<&'_ mut (dyn Handler + '_)>) { unsafe { let ptr = handler.get_unchecked_mut() as *mut dyn Handler; CB_SINGLETON.handler.replace(Pin::new_unchecked(ptr.as_mut().unwrap())); } } pub fn unset_handler() { unsafe { CB_SINGLETON.handler.take(); } }