ferretro/ferretro_base/src/retro/statics.rs

495 lines
21 KiB
Rust

use core::convert::{TryFrom, TryInto};
use core::mem::size_of;
use core::pin::Pin;
use core::slice::from_raw_parts;
use std::ffi::{c_void, CString, CStr};
use std::os::raw::{c_char, c_uint};
use std::path::Path;
use num_enum::TryFromPrimitive;
use crate::retro::callbacks::*;
use crate::retro::constants::*;
use crate::retro::ffi::*;
use crate::retro::wrapped_types::*;
static mut CB_SINGLETON: StaticCallbacks = StaticCallbacks {
handler: None,
pix_fmt: PixelFormat::ARGB1555,
};
struct StaticCallbacks {
handler: Option<Pin<&'static mut dyn RootRetroCallbacks>>,
pix_fmt: PixelFormat,
}
impl Default for StaticCallbacks {
fn default() -> Self {
StaticCallbacks { handler: None, pix_fmt: PixelFormat::ARGB1555 }
}
}
unsafe impl Sync for StaticCallbacks {}
impl StaticCallbacks {
// 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> {
*unsafe { (data as *mut *const c_char).as_mut()? } =
CString::new(source.as_ref().to_str()?.as_bytes()).ok()?.into_raw();
Some(true)
}
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> {
let maybe_handler = unsafe { CB_SINGLETON.handler.as_mut() };
if cfg!(debug) && maybe_handler.is_none() {
eprintln!(
"WARNING: env cmd {} called before handler set!",
match EnvCmd::try_from(cmd) {
Ok(x) => format!("{:?}", x),
Err(_) => format!("{}", cmd),
}
);
}
let handler = maybe_handler?;
let parsed_cmd = cmd.try_into().ok();
if cfg!(debug) && parsed_cmd.is_none() {
eprintln!(
"Unknown{} env cmd: {}",
if cmd >= ENVIRONMENT_EXPERIMENTAL {
", experimental"
} else {
""
},
cmd % ENVIRONMENT_EXPERIMENTAL
);
}
match parsed_cmd? {
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, &true)?,
EnvCmd::SetMessage => handler.set_message(Self::from_void::<Message>(data)?)?,
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 => {
let format = PixelFormat::from_uint(*Self::from_void(data)?)?;
unsafe { CB_SINGLETON.pix_fmt = format };
handler.set_pixel_format(format)?
}
EnvCmd::SetInputDescriptors => {
let mut input_desc = data as *const InputDescriptor;
let mut descriptors = Vec::new();
while !unsafe { input_desc.as_ref() }?.description.is_null() {
descriptors.push(unsafe { input_desc.as_ref() }?.try_into().ok()?);
input_desc = input_desc.wrapping_add(1);
}
handler.set_input_descriptors(&descriptors)?
}
EnvCmd::SetKeyboardCallback => {
let kc: &mut KeyboardCallback = Self::from_void(data)?;
handler
.libretro_core()
.keyboard_event_cb
.replace(kc.callback);
true
}
EnvCmd::SetDiskControlInterface => {
let dcc: &mut DiskControlCallback = Self::from_void(data)?;
let core = handler.libretro_core();
core.disk_set_eject_state_cb.replace(dcc.set_eject_state);
core.disk_get_eject_state_cb.replace(dcc.get_eject_state);
core.disk_set_image_index_cb.replace(dcc.set_image_index);
core.disk_get_image_index_cb.replace(dcc.get_image_index);
core.disk_get_num_images_cb.replace(dcc.get_num_images);
core.disk_replace_image_index_cb
.replace(dcc.replace_image_index);
core.disk_add_image_index_cb.replace(dcc.add_image_index);
true
}
EnvCmd::SetHwRender => {
let hwr = Self::from_void::<HwRenderCallback>(data)?;
handler
.libretro_core()
.hw_context_reset_cb
.replace(hwr.context_reset);
handler
.libretro_core()
.hw_context_destroy_cb
.replace(hwr.context_destroy);
hwr.get_current_framebuffer = Self::hw_get_current_framebuffer_fn;
hwr.get_proc_address = Self::hw_get_proc_address_fn;
handler.set_hw_render(hwr).unwrap_or(false)
}
EnvCmd::GetVariable => {
let mut var = Self::from_void::<Variable>(data)?;
let key = unsafe { CStr::from_ptr(var.key) }.to_str().ok()?;
let value = handler.get_variable(key)?;
// leaks memory.
var.value = CString::new(value).ok()?.into_raw();
true
}
EnvCmd::SetVariables => {
let mut var = data as *const Variable;
let mut descriptors = Vec::new();
while !unsafe { var.as_ref() }?.key.is_null() {
descriptors.push(unsafe { var.as_ref() }?.into());
var = var.wrapping_add(1);
}
handler.set_variables(&descriptors)?
}
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()?)?,
EnvCmd::SetFrameTimeCallback => {
let ftc: &mut FrameTimeCallback = Self::from_void(data)?;
handler.libretro_core().frame_time_cb.replace(ftc.callback);
true
}
EnvCmd::SetAudioCallback => {
let ac: &mut AudioCallback = Self::from_void(data)?;
handler.libretro_core().audio_ready_cb.replace(ac.callback);
handler
.libretro_core()
.audio_set_state_cb
.replace(ac.set_state);
true
}
EnvCmd::GetRumbleInterface => {
let ri = RumbleInterface {
set_rumble_state: StaticCallbacks::set_rumble_state_cb,
};
Self::clone_into_void(data, &ri)?
}
// TODO: wrap this in something nicer than a raw u64 bit field
EnvCmd::GetInputDeviceCapabilities => {
Self::clone_into_void(data, &handler.get_input_device_capabilities()?)?
}
EnvCmd::GetSensorInterface => {
let si = SensorInterface {
set_sensor_state: Self::set_sensor_state,
get_sensor_input: Self::get_sensor_input,
};
Self::clone_into_void(data, &si)?
}
EnvCmd::GetCameraInterface => {
let cc: &mut CameraCallback = Self::from_void(data)?;
let cap_gl_tex = (cc.caps & 1 << (CameraBuffer::OpenGLTexture as u64)) != 0;
let cap_raw_fb = (cc.caps & 1 << (CameraBuffer::RawFramebuffer as u64)) != 0;
let wrapper = handler.libretro_core();
if !(cc.frame_raw_framebuffer as *const ()).is_null() {
wrapper.camera_frame_raw_framebuffer_cb.replace(cc.frame_raw_framebuffer);
}
if !(cc.frame_opengl_texture as *const ()).is_null() {
wrapper.camera_frame_opengl_texture_cb.replace(cc.frame_opengl_texture);
}
if !(cc.initialized as *const ()).is_null() {
wrapper.camera_lifetime_initialized_cb.replace(cc.initialized);
}
if !(cc.deinitialized as *const ()).is_null() {
wrapper.camera_lifetime_deinitialized_cb.replace(cc.deinitialized);
}
cc.start = Self::camera_start;
cc.stop = Self::camera_stop;
handler.get_camera_interface(cc.width, cc.height, cap_raw_fb, cap_gl_tex)
.unwrap_or(true) // only say false if someone explicitly objected. TODO: is this okay?
}
EnvCmd::GetLogInterface => unsafe {
c_ext_handle_get_log_interface(data as *mut LogCallback)
}
EnvCmd::GetPerfInterface => {
let pc = PerfCallback {
get_time_usec: Self::perf_get_time_usec_cb,
get_cpu_features: Self::perf_get_cpu_features_cb,
get_perf_counter: Self::perf_get_counter_cb,
perf_register: Self::perf_register_cb,
perf_start: Self::perf_start_cb,
perf_stop: Self::perf_stop_cb,
perf_log: Self::perf_log_cb,
};
Self::clone_into_void(data, &pc)?
}
// TODO (apathy) 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)?)?
}
EnvCmd::SetProcAddressCallback => {
let gpa = Self::from_void::<GetProcAddressInterface>(data)?;
handler
.libretro_core()
.get_proc_address_cb
.replace(gpa.get_proc_address);
true
}
EnvCmd::SetSubsystemInfo => {
let mut info = data as *const SubsystemInfo;
let mut descriptors = Vec::new();
while !unsafe { info.as_ref() }?.desc.is_null() {
descriptors.push(unsafe { info.as_ref() }?.into());
info = info.wrapping_add(1);
}
handler.set_subsystem_info(&descriptors)?
}
EnvCmd::SetControllerInfo => {
let info = unsafe { (data as *const ControllerInfo).as_ref() }?;
// FIXME: beetle/mednafen saturn crashes without this -1... add conditional on name?
let len = info.num_types as usize - 1;
let slice = unsafe { from_raw_parts(info.types, len) };
let controller_info: Vec<ControllerDescription2> = slice
.iter()
.map(TryInto::try_into)
.filter_map(|x| x.ok())
.collect();
handler.set_controller_info(&controller_info)?
}
EnvCmd::SetMemoryMaps => {
let map = Self::from_void::<MemoryMap>(data)?.clone();
handler.set_memory_maps(&map)?
},
EnvCmd::SetGeometry => {
handler.set_geometry(Self::from_void::<GameGeometry>(data)?)?
}
EnvCmd::GetUsername => Self::string_into_void(data, handler.get_username()?)?,
EnvCmd::GetLanguage => Self::clone_into_void(data, &handler.get_language()?)?,
// TODO EnvCmd::GetCurrentSoftwareFramebuffer => {}
// TODO EnvCmd::GetHwRenderInterface => {}
// TODO (not in libretro-sys) EnvCmd::SetSerializationQuirks => handler.set_serialization_quirks(Self::from_void(data)?),
x => {
if cfg!(debug) {
eprintln!("Known but 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 let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
const NULL: *const c_void = std::ptr::null();
let frame = match data {
NULL => VideoFrame::Duplicate { width, height, pitch_u8: pitch },
HW_FRAME_BUFFER_VALID => VideoFrame::HardwareRender { width, height },
ptr => match unsafe { CB_SINGLETON.pix_fmt } {
PixelFormat::ARGB1555 => {
let pitch = pitch / size_of::<u16>();
let len = pitch * (height as usize);
let data = unsafe { from_raw_parts(ptr as *const u16, len) };
VideoFrame::XRGB1555 { data, width, height, pitch_u16: pitch }
}
PixelFormat::RGB565 => {
let pitch = pitch / size_of::<u16>();
let len = pitch * (height as usize);
let data = unsafe { from_raw_parts(ptr as *const u16, len) };
VideoFrame::RGB565 { data, width, height, pitch_u16: pitch }
}
PixelFormat::ARGB8888 => {
let pitch = pitch / size_of::<u32>();
let len = pitch * (height as usize);
let data = unsafe { from_raw_parts(ptr as *const u32, len) };
VideoFrame::XRGB8888 { data, width, height, pitch_u32: pitch }
}
}
};
cb.video_refresh(&frame);
}
}
extern "C" fn audio_sample_cb(left: i16, right: i16) {
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
cb.audio_samples(&[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 => {
// paraLLEl-n64 sometimes gives us garbage here during initialization
let (len, over) = frames.overflowing_mul(2); // stereo
if over {
frames
} else {
cb.audio_samples(from_raw_parts(data, len))
}
}
},
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,
}
}
extern "C" fn perf_get_time_usec_cb() -> Time {
unsafe {
CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_get_time_usec())
}.unwrap_or_default()
}
extern "C" fn perf_get_counter_cb() -> PerfTick {
unsafe {
CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_get_counter())
}.unwrap_or_default()
}
extern "C" fn perf_get_cpu_features_cb() -> u64 {
unsafe {
CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_get_cpu_features())
}.unwrap_or_default()
}
extern "C" fn perf_log_cb() {
unsafe { CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_log()); }
}
extern "C" fn perf_register_cb(counter: *mut PerfCounter) {
unsafe {
if let Some(cb) = CB_SINGLETON.handler.as_mut() {
if let Some(counter) = counter.as_mut() {
cb.perf_register(counter);
}
}
}
}
extern "C" fn perf_start_cb(counter: *mut PerfCounter) {
unsafe {
if let Some(cb) = CB_SINGLETON.handler.as_mut() {
if let Some(counter) = counter.as_mut() {
cb.perf_start(counter);
}
}
}
}
extern "C" fn perf_stop_cb(counter: *mut PerfCounter) {
unsafe {
if let Some(cb) = CB_SINGLETON.handler.as_mut() {
if let Some(counter) = counter.as_mut() {
cb.perf_stop(counter);
}
}
}
}
extern "C" fn set_sensor_state(port: c_uint, action: SensorAction, rate: c_uint) -> bool {
unsafe {
CB_SINGLETON.handler.as_mut().map(|cb| cb.set_sensor_state(port, action, rate))
}.unwrap_or_default()
}
extern "C" fn get_sensor_input(port: c_uint, id: c_uint) -> f32 {
unsafe {
CB_SINGLETON.handler.as_mut().map(|cb| cb.get_sensor_input(port, id))
}.unwrap_or_default()
}
extern "C" fn camera_start() -> bool {
unsafe {
CB_SINGLETON.handler.as_mut().and_then(|cb| cb.camera_start())
}.unwrap_or_default()
}
extern "C" fn camera_stop() -> () {
unsafe {
CB_SINGLETON.handler.as_mut().and_then(|cb| cb.camera_stop())
}.unwrap_or_default()
}
extern "C" fn hw_get_proc_address_fn(sym: *const c_char) -> ProcAddressFn {
unsafe {
std::mem::transmute(
CB_SINGLETON.handler.as_mut()
.and_then(|cb| cb.hw_get_proc_address(CStr::from_ptr(sym).to_str().unwrap()))
.unwrap_or(std::ptr::null())
)
}
}
// note: libretro.h claims this is obsolete, but (at least) paraLLEl-n64 uses it
extern "C" fn hw_get_current_framebuffer_fn() -> usize {
unsafe {
CB_SINGLETON.handler.as_mut()
.and_then(|cb| cb.hw_get_current_framebuffer())
}.unwrap_or_default()
}
}
// 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(crate) fn set_handler(handler: Pin<&'_ mut (dyn RootRetroCallbacks + '_)>) {
unsafe {
let ptr = handler.get_unchecked_mut() as *mut dyn RootRetroCallbacks;
CB_SINGLETON
.handler
.replace(Pin::new_unchecked(ptr.as_mut().unwrap()));
c_ext_set_log_print_cb(StaticCallbacks::log_print_cb);
}
// some cores start calling the environment cb as soon as we call retro_set_environment,
// not waiting for a call to retro_init - so we'd better have CB_SINGLETON set first
let api = unsafe { CB_SINGLETON.handler.as_mut() }
.unwrap()
.libretro_core();
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);
}
pub(crate) fn unset_handler() {
unsafe {
CB_SINGLETON.handler.take();
}
}