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-17 11:42:13 +01:00
|
|
|
use std::convert::TryFrom;
|
2019-11-19 09:31:14 +01:00
|
|
|
use std::ffi::{CStr, CString};
|
2019-11-17 07:34:01 +01:00
|
|
|
use std::ops::Deref;
|
2019-11-19 09:31:14 +01:00
|
|
|
use std::os::raw::{c_char, c_uint};
|
2019-11-16 08:03:30 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-11-16 05:59:08 +01:00
|
|
|
use std::pin::Pin;
|
2019-11-17 11:42:13 +01:00
|
|
|
use std::time::Duration;
|
2019-11-11 01:24:29 +01:00
|
|
|
|
2019-11-16 05:59:08 +01:00
|
|
|
use num_enum::TryFromPrimitive;
|
2019-11-12 08:46:53 +01:00
|
|
|
|
2019-11-16 08:03:30 +01:00
|
|
|
use super::constants::*;
|
|
|
|
use super::ffi::*;
|
2019-11-15 04:16:44 +01:00
|
|
|
use super::loading::*;
|
2019-11-24 05:18:52 +01:00
|
|
|
use super::wrapped_types::*;
|
2019-11-11 01:24:29 +01:00
|
|
|
|
2019-11-19 09:31:14 +01:00
|
|
|
static mut CB_SINGLETON: StaticCallbacks = StaticCallbacks { handler: None };
|
2019-11-11 01:24:29 +01:00
|
|
|
|
2019-11-17 10:31:45 +01:00
|
|
|
// 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.
|
2019-11-17 06:34:01 +01:00
|
|
|
pub type WrappedLogPrintFn = extern "C" fn(level: LogLevel, fmt: *const c_char);
|
2019-11-19 09:31:14 +01:00
|
|
|
extern "C" {
|
2019-11-17 06:34:01 +01:00
|
|
|
fn c_ext_handle_get_log_interface(cb: *mut LogCallback) -> bool;
|
|
|
|
fn c_ext_set_log_print_cb(cb: WrappedLogPrintFn);
|
|
|
|
}
|
|
|
|
|
2019-11-19 09:31:14 +01:00
|
|
|
#[rustfmt::skip]
|
2019-11-16 08:03:30 +01:00
|
|
|
#[allow(unused)]
|
|
|
|
pub trait Handler: Unpin + 'static {
|
2019-11-18 05:52:35 +01:00
|
|
|
fn libretro_core(&mut self) -> &mut LibretroWrapper;
|
|
|
|
fn delegate_handler(&self) -> Option<&mut dyn Handler> { None }
|
2019-11-17 05:27:42 +01:00
|
|
|
|
|
|
|
// -- main callbacks --
|
2019-11-16 08:03:30 +01:00
|
|
|
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) {}
|
2019-11-24 04:37:18 +01:00
|
|
|
fn input_state(&mut self, port: u32, device: InputDeviceId, index: InputIndex) -> i16 { 0 }
|
2019-11-16 08:03:30 +01:00
|
|
|
|
|
|
|
// -- 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 }
|
|
|
|
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<PathBuf> { None }
|
|
|
|
fn set_pixel_format(&mut self, format: PixelFormat) -> bool { false }
|
2019-11-24 04:37:18 +01:00
|
|
|
fn set_input_descriptors(&mut self, input_descriptors: Vec<InputDescriptor2>) -> bool { false }
|
2019-11-16 08:03:30 +01:00
|
|
|
fn set_hw_render(&mut self, hw_render_callback: HwRenderCallback) -> bool { false }
|
2019-11-17 10:42:29 +01:00
|
|
|
fn get_variable(&mut self, key: &str) -> Option<String> { None }
|
2019-11-24 04:37:18 +01:00
|
|
|
fn set_variables(&mut self, variables: Vec<Variable2>) -> bool { false }
|
2019-11-16 08:03:30 +01:00
|
|
|
fn get_variable_update(&mut self) -> Option<bool> { None }
|
|
|
|
fn set_support_no_game(&mut self, supports_no_game: bool) -> bool { false }
|
|
|
|
fn get_libretro_path(&mut self) -> Option<PathBuf> { None }
|
|
|
|
fn get_input_device_capabilities(&mut self) -> Option<u64> { None }
|
|
|
|
fn get_sensor_interface(&mut self) -> Option<SensorInterface> { None }
|
|
|
|
fn get_camera_interface(&mut self) -> Option<CameraCallback> { None }
|
|
|
|
fn get_log_interface(&mut self) -> Option<LogCallback> { None }
|
|
|
|
fn get_perf_interface(&mut self) -> Option<PerfCallback> { None }
|
|
|
|
fn get_location_interface(&mut self) -> Option<LocationCallback> { None }
|
|
|
|
fn get_core_assets_directory(&mut self) -> Option<PathBuf> { None }
|
|
|
|
fn get_save_directory(&mut self) -> Option<PathBuf> { 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 }
|
2019-11-19 09:31:14 +01:00
|
|
|
fn set_subsystem_info(&mut self, subsystem_info: Vec<SubsystemInfo2>) -> bool { false }
|
|
|
|
fn set_controller_info(&mut self, controller_info: Vec<ControllerDescription2>) -> bool { false }
|
2019-11-16 08:03:30 +01:00
|
|
|
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<String> { None }
|
|
|
|
fn get_language(&mut self) -> Option<Language> { None }
|
|
|
|
// fn set_serialization_quirks(&mut self, quirks: &mut u64) -> bool { false }
|
2019-11-17 06:34:01 +01:00
|
|
|
|
|
|
|
// -- environment-set callbacks (API extensions) --
|
|
|
|
fn log_print(&mut self, level: LogLevel, msg: &str) {}
|
2019-11-17 10:31:45 +01:00
|
|
|
fn set_rumble_state(&mut self, port: c_uint, effect: RumbleEffect, strength: u16) -> bool { false }
|
2019-11-24 05:18:52 +01:00
|
|
|
fn perf_get_time_usec_cb(&mut self) -> Time { 0 }
|
|
|
|
fn perf_get_counter_cb(&mut self) -> PerfTick { 0 }
|
|
|
|
fn perf_get_cpu_features_cb(&mut self) -> u64 { 0 }
|
|
|
|
fn perf_log_cb(&mut self) {}
|
|
|
|
fn perf_register_cb(&mut self, counter: &mut PerfCounter) {}
|
|
|
|
fn perf_start_cb(&mut self, counter: &mut PerfCounter) {}
|
|
|
|
fn perf_stop_cb(&mut self, counter: &mut PerfCounter) {}
|
|
|
|
fn set_sensor_state(&mut self, port: c_uint, action: SensorAction, rate: c_uint) -> bool { false }
|
|
|
|
fn get_sensor_input(&mut self, port: c_uint, id: c_uint) -> f32 { 0.0 }
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
|
|
|
|
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> {
|
2019-11-19 09:31:14 +01:00
|
|
|
*unsafe { (data as *mut *const c_char).as_mut()? } =
|
|
|
|
CString::new(source.as_ref()).ok()?.into_raw();
|
2019-11-11 08:21:38 +01:00
|
|
|
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>
|
2019-11-19 09:31:14 +01:00
|
|
|
where
|
|
|
|
T: TryFromPrimitive,
|
|
|
|
<T as TryFromPrimitive>::Primitive: 'static,
|
2019-11-11 08:21:38 +01:00
|
|
|
{
|
|
|
|
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-17 11:42:13 +01:00
|
|
|
let maybe_handler = unsafe { CB_SINGLETON.handler.as_mut() };
|
2019-11-18 08:02:40 +01:00
|
|
|
if cfg!(debug) && maybe_handler.is_none() {
|
2019-11-17 11:42:13 +01:00
|
|
|
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();
|
2019-11-18 07:03:37 +01:00
|
|
|
if cfg!(debug) && parsed_cmd.is_none() {
|
2019-11-17 11:42:13 +01:00
|
|
|
eprintln!(
|
|
|
|
"Unknown{} env cmd: {}",
|
2019-11-19 09:31:14 +01:00
|
|
|
if cmd >= ENVIRONMENT_EXPERIMENTAL {
|
|
|
|
", experimental"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
2019-11-17 11:42:13 +01:00
|
|
|
cmd % ENVIRONMENT_EXPERIMENTAL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
match parsed_cmd? {
|
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)?),
|
2019-11-19 09:31:14 +01:00
|
|
|
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-24 04:37:18 +01:00
|
|
|
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)
|
2019-11-24 05:18:52 +01:00
|
|
|
}
|
2019-11-18 05:52:35 +01:00
|
|
|
EnvCmd::SetKeyboardCallback => {
|
|
|
|
let kc: &mut KeyboardCallback = Self::from_void(data)?;
|
2019-11-19 09:31:14 +01:00
|
|
|
handler
|
|
|
|
.libretro_core()
|
|
|
|
.keyboard_event_cb
|
|
|
|
.replace(kc.callback);
|
2019-11-18 05:52:35 +01:00
|
|
|
true
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-18 05:52:35 +01:00
|
|
|
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);
|
2019-11-19 09:31:14 +01:00
|
|
|
core.disk_replace_image_index_cb
|
|
|
|
.replace(dcc.replace_image_index);
|
2019-11-18 05:52:35 +01:00
|
|
|
core.disk_add_image_index_cb.replace(dcc.add_image_index);
|
|
|
|
true
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-18 08:35:56 +01:00
|
|
|
EnvCmd::SetHwRender => {
|
|
|
|
let hwr = Self::from_void::<HwRenderCallback>(data)?;
|
2019-11-19 09:31:14 +01:00
|
|
|
handler
|
|
|
|
.libretro_core()
|
|
|
|
.hw_context_reset_cb
|
|
|
|
.replace(hwr.context_reset);
|
|
|
|
handler
|
|
|
|
.libretro_core()
|
|
|
|
.hw_context_destroy_cb
|
|
|
|
.replace(hwr.context_destroy);
|
2019-11-18 08:35:56 +01:00
|
|
|
hwr.get_current_framebuffer = Self::hw_get_current_framebuffer_fn;
|
|
|
|
hwr.get_proc_address = Self::hw_get_proc_address_fn;
|
|
|
|
false // TODO: finish
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-17 10:31:45 +01:00
|
|
|
EnvCmd::GetVariable => {
|
2019-11-17 10:42:29 +01:00
|
|
|
let mut var = Self::from_void::<Variable>(data)?;
|
2019-11-17 11:42:13 +01:00
|
|
|
let key = unsafe { CStr::from_ptr(var.key) }.to_str().ok()?;
|
|
|
|
let value = handler.get_variable(key)?;
|
2019-11-17 10:42:29 +01:00
|
|
|
// leaks memory.
|
|
|
|
var.value = CString::new(value).ok()?.into_raw();
|
|
|
|
true
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-18 07:03:37 +01:00
|
|
|
EnvCmd::SetVariables => {
|
|
|
|
let mut var = data as *const Variable;
|
|
|
|
let mut descriptors = Vec::new();
|
|
|
|
while !unsafe { var.as_ref() }?.key.is_null() {
|
2019-11-19 09:31:14 +01:00
|
|
|
descriptors.push(unsafe { var.as_ref() }?.into());
|
2019-11-18 07:03:37 +01:00
|
|
|
var = var.wrapping_add(1);
|
|
|
|
}
|
2019-11-18 08:02:40 +01:00
|
|
|
handler.set_variables(descriptors)
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
|
|
|
EnvCmd::GetVariableUpdate => {
|
|
|
|
Self::clone_into_void(data, &handler.get_variable_update()?)?
|
|
|
|
}
|
2019-11-12 08:46:53 +01:00
|
|
|
EnvCmd::SetSupportNoGame => handler.set_support_no_game(*Self::from_void(data)?),
|
|
|
|
EnvCmd::GetLibretroPath => Self::path_into_void(data, handler.get_libretro_path()?)?,
|
2019-11-18 05:52:35 +01:00
|
|
|
EnvCmd::SetFrameTimeCallback => {
|
|
|
|
let ftc: &mut FrameTimeCallback = Self::from_void(data)?;
|
|
|
|
handler.libretro_core().frame_time_cb.replace(ftc.callback);
|
|
|
|
true
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-18 05:52:35 +01:00
|
|
|
EnvCmd::SetAudioCallback => {
|
|
|
|
let ac: &mut AudioCallback = Self::from_void(data)?;
|
|
|
|
handler.libretro_core().audio_ready_cb.replace(ac.callback);
|
2019-11-19 09:31:14 +01:00
|
|
|
handler
|
|
|
|
.libretro_core()
|
|
|
|
.audio_set_state_cb
|
|
|
|
.replace(ac.set_state);
|
2019-11-18 05:52:35 +01:00
|
|
|
true
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-17 10:31:45 +01:00
|
|
|
EnvCmd::GetRumbleInterface => {
|
2019-11-19 09:31:14 +01:00
|
|
|
let ri = RumbleInterface {
|
|
|
|
set_rumble_state: StaticCallbacks::set_rumble_state_cb,
|
|
|
|
};
|
2019-11-17 10:31:45 +01:00
|
|
|
Self::clone_into_void(data, &ri)?
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-17 11:42:13 +01:00
|
|
|
// TODO: wrap this in something nicer than a raw u64 bit field
|
2019-11-19 09:31:14 +01:00
|
|
|
EnvCmd::GetInputDeviceCapabilities => {
|
|
|
|
Self::clone_into_void(data, &handler.get_input_device_capabilities()?)?
|
|
|
|
}
|
2019-11-24 05:18:52 +01:00
|
|
|
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)?
|
|
|
|
}
|
2019-11-12 08:56:28 +01:00
|
|
|
// TODO EnvCmd::GetCameraInterface => {},
|
2019-11-19 09:31:14 +01:00
|
|
|
EnvCmd::GetLogInterface => unsafe {
|
|
|
|
c_ext_handle_get_log_interface(data as *mut LogCallback)
|
2019-11-24 05:18:52 +01:00
|
|
|
}
|
2019-11-18 08:13:25 +01:00
|
|
|
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)?
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-12 08:56:28 +01:00
|
|
|
// TODO EnvCmd::GetLocationInterface => {},
|
2019-11-19 09:31:14 +01:00
|
|
|
EnvCmd::GetCoreAssetsDirectory => {
|
|
|
|
Self::path_into_void(data, handler.get_core_assets_directory()?)?
|
|
|
|
}
|
2019-11-12 08:46:53 +01:00
|
|
|
EnvCmd::GetSaveDirectory => Self::path_into_void(data, handler.get_save_directory()?)?,
|
2019-11-19 09:31:14 +01:00
|
|
|
EnvCmd::SetSystemAvInfo => {
|
|
|
|
handler.set_system_av_info(Self::from_void::<SystemAvInfo>(data)?.clone())
|
|
|
|
}
|
2019-11-18 10:01:33 +01:00
|
|
|
EnvCmd::SetProcAddressCallback => {
|
|
|
|
let gpa = Self::from_void::<GetProcAddressInterface>(data)?;
|
2019-11-19 09:31:14 +01:00
|
|
|
handler
|
|
|
|
.libretro_core()
|
|
|
|
.get_proc_address_cb
|
|
|
|
.replace(gpa.get_proc_address);
|
2019-11-18 10:01:33 +01:00
|
|
|
true
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2019-11-17 11:42:13 +01:00
|
|
|
EnvCmd::SetControllerInfo => {
|
|
|
|
let info = unsafe { (data as *const ControllerInfo).as_ref() }?;
|
2019-11-18 08:02:40 +01:00
|
|
|
// FIXME: beetle/mednafen saturn crashes without this -1... add conditional on name?
|
|
|
|
let len = info.num_types as usize - 1;
|
2019-11-19 09:31:14 +01:00
|
|
|
let controller_info = unsafe { from_raw_parts(info.types, len) }
|
|
|
|
.iter()
|
|
|
|
.map(TryInto::try_into)
|
|
|
|
.filter_map(|x| x.ok())
|
|
|
|
.collect();
|
2019-11-18 08:02:40 +01:00
|
|
|
handler.set_controller_info(controller_info)
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-12 08:56:28 +01:00
|
|
|
// TODO EnvCmd::SetMemoryMaps => {},
|
2019-11-19 09:31:14 +01:00
|
|
|
EnvCmd::SetGeometry => {
|
|
|
|
handler.set_geometry(Self::from_void::<GameGeometry>(data)?.clone())
|
|
|
|
}
|
2019-11-12 08:46:53 +01:00
|
|
|
EnvCmd::GetUsername => Self::string_into_void(data, handler.get_username()?)?,
|
|
|
|
EnvCmd::GetLanguage => Self::clone_into_void(data, &handler.get_language()?)?,
|
2019-11-24 05:28:56 +01:00
|
|
|
// TODO (not in libretro-sys) EnvCmd::SetSerializationQuirks => handler.set_serialization_quirks(Self::from_void(data)?),
|
2019-11-17 10:31:45 +01:00
|
|
|
x => {
|
2019-11-18 08:02:40 +01:00
|
|
|
if cfg!(debug) {
|
|
|
|
eprintln!("Known but unsupported env cmd: {:?}", x);
|
|
|
|
}
|
2019-11-17 10:31:45 +01:00
|
|
|
false
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.into()
|
2019-11-11 08:21:38 +01:00
|
|
|
}
|
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-17 10:31:45 +01:00
|
|
|
if !data.is_null() && data != HW_FRAME_BUFFER_VALID {
|
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,
|
2019-11-16 08:03:30 +01:00
|
|
|
false => {
|
|
|
|
let len = frames * 2; // stereo
|
|
|
|
let result = cb.audio_sample_batch(from_raw_parts(data, len));
|
|
|
|
result / 2
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 10:31:45 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 09:31:14 +01:00
|
|
|
extern "C" fn perf_get_time_usec_cb() -> Time {
|
2019-11-24 05:18:52 +01:00
|
|
|
unsafe {
|
|
|
|
CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_get_time_usec_cb())
|
|
|
|
}.unwrap_or_default()
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
|
|
|
extern "C" fn perf_get_counter_cb() -> PerfTick {
|
2019-11-24 05:18:52 +01:00
|
|
|
unsafe {
|
|
|
|
CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_get_counter_cb())
|
|
|
|
}.unwrap_or_default()
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
|
|
|
extern "C" fn perf_get_cpu_features_cb() -> u64 {
|
2019-11-24 05:18:52 +01:00
|
|
|
unsafe {
|
|
|
|
CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_get_cpu_features_cb())
|
|
|
|
}.unwrap_or_default()
|
|
|
|
}
|
|
|
|
extern "C" fn perf_log_cb() {
|
|
|
|
unsafe { CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_log_cb()); }
|
|
|
|
}
|
|
|
|
extern "C" fn perf_register_cb(counter: *mut PerfCounter) {
|
|
|
|
unsafe {
|
|
|
|
match (CB_SINGLETON.handler.as_mut(), counter.as_mut()) {
|
|
|
|
(Some(cb), Some(counter)) => {
|
|
|
|
cb.perf_register_cb(counter);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern "C" fn perf_start_cb(counter: *mut PerfCounter) {
|
|
|
|
unsafe {
|
|
|
|
match (CB_SINGLETON.handler.as_mut(), counter.as_mut()) {
|
|
|
|
(Some(cb), Some(counter)) => {
|
|
|
|
cb.perf_start_cb(counter);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern "C" fn perf_stop_cb(counter: *mut PerfCounter) {
|
|
|
|
unsafe {
|
|
|
|
match (CB_SINGLETON.handler.as_mut(), counter.as_mut()) {
|
|
|
|
(Some(cb), Some(counter)) => {
|
|
|
|
cb.perf_stop_cb(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()
|
2019-11-19 09:31:14 +01:00
|
|
|
}
|
2019-11-18 08:35:56 +01:00
|
|
|
|
2019-11-24 05:18:52 +01:00
|
|
|
// TODO: trait methods, etc.
|
2019-11-18 08:35:56 +01:00
|
|
|
extern "C" fn hw_dummy_fn() {}
|
|
|
|
extern "C" fn hw_get_proc_address_fn(_sym: *const c_char) -> ProcAddressFn {
|
|
|
|
Self::hw_dummy_fn // FIXME: obvious hack
|
|
|
|
}
|
|
|
|
// note: libretro.h claims this is obsolete
|
2019-11-19 09:31:14 +01:00
|
|
|
extern "C" fn hw_get_current_framebuffer_fn() -> usize {
|
|
|
|
0
|
|
|
|
}
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 06:21:58 +01:00
|
|
|
pub struct LibretroWrapper {
|
|
|
|
api: LibretroApi,
|
2019-11-17 10:31:45 +01:00
|
|
|
keyboard_event_cb: Option<KeyboardEventFn>,
|
|
|
|
frame_time_cb: Option<FrameTimeCallbackFn>,
|
|
|
|
audio_ready_cb: Option<AudioCallbackFn>,
|
|
|
|
audio_set_state_cb: Option<AudioSetStateCallbackFn>,
|
2019-11-18 05:52:35 +01:00
|
|
|
disk_get_eject_state_cb: Option<GetEjectStateFn>,
|
|
|
|
disk_set_eject_state_cb: Option<SetEjectStateFn>,
|
|
|
|
disk_get_image_index_cb: Option<GetImageIndexFn>,
|
|
|
|
disk_set_image_index_cb: Option<SetImageIndexFn>,
|
|
|
|
disk_get_num_images_cb: Option<GetNumImagesFn>,
|
|
|
|
disk_replace_image_index_cb: Option<ReplaceImageIndexFn>,
|
|
|
|
disk_add_image_index_cb: Option<AddImageIndexFn>,
|
2019-11-18 08:35:56 +01:00
|
|
|
hw_context_reset_cb: Option<HwContextResetFn>,
|
|
|
|
hw_context_destroy_cb: Option<HwContextResetFn>, // same signature, libretro-sys deduplicated...
|
2019-11-18 10:01:33 +01:00
|
|
|
get_proc_address_cb: Option<GetProcAddressFn>,
|
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-17 10:31:45 +01:00
|
|
|
LibretroWrapper {
|
|
|
|
api,
|
|
|
|
keyboard_event_cb: None,
|
|
|
|
frame_time_cb: None,
|
|
|
|
audio_ready_cb: None,
|
2019-11-18 05:52:35 +01:00
|
|
|
audio_set_state_cb: None,
|
|
|
|
disk_set_eject_state_cb: None,
|
|
|
|
disk_get_eject_state_cb: None,
|
|
|
|
disk_get_image_index_cb: None,
|
|
|
|
disk_set_image_index_cb: None,
|
|
|
|
disk_get_num_images_cb: None,
|
|
|
|
disk_replace_image_index_cb: None,
|
2019-11-18 08:35:56 +01:00
|
|
|
disk_add_image_index_cb: None,
|
|
|
|
hw_context_reset_cb: None,
|
|
|
|
hw_context_destroy_cb: None,
|
2019-11-18 10:01:33 +01:00
|
|
|
get_proc_address_cb: None,
|
2019-11-17 10:31:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LibretroWrapper {
|
|
|
|
// TODO: enum for the RETROK_* constants instead of c_uint for keycode...
|
2019-11-19 09:31:14 +01:00
|
|
|
pub fn keyboard_event(
|
|
|
|
&self,
|
|
|
|
down: bool,
|
|
|
|
keycode: c_uint,
|
|
|
|
character: u32,
|
|
|
|
key_modifiers: u16,
|
|
|
|
) -> Option<()> {
|
|
|
|
self.keyboard_event_cb
|
|
|
|
.map(|f| unsafe { f(down, keycode, character, key_modifiers) })
|
2019-11-17 10:31:45 +01:00
|
|
|
}
|
2019-11-18 05:52:35 +01:00
|
|
|
pub fn frame_time(&self, time: Duration) -> Option<()> {
|
2019-11-19 09:31:14 +01:00
|
|
|
self.frame_time_cb
|
|
|
|
.map(|f| unsafe { f(time.as_micros() as Usec) })
|
2019-11-17 10:31:45 +01:00
|
|
|
}
|
2019-11-18 05:52:35 +01:00
|
|
|
pub fn audio_ready(&self) -> Option<()> {
|
|
|
|
self.audio_ready_cb.map(|f| unsafe { f() })
|
2019-11-17 10:31:45 +01:00
|
|
|
}
|
2019-11-18 05:52:35 +01:00
|
|
|
pub fn audio_set_state(&self, enabled: bool) -> Option<()> {
|
|
|
|
self.audio_set_state_cb.map(|f| unsafe { f(enabled) })
|
|
|
|
}
|
|
|
|
pub fn disk_get_eject_state(&self) -> Option<bool> {
|
|
|
|
self.disk_get_eject_state_cb.map(|f| unsafe { f() })
|
|
|
|
}
|
2019-11-18 07:03:37 +01:00
|
|
|
pub fn disk_set_eject_state(&self, ejected: bool) -> Option<bool> {
|
2019-11-18 05:52:35 +01:00
|
|
|
self.disk_set_eject_state_cb.map(|f| unsafe { f(ejected) })
|
|
|
|
}
|
|
|
|
pub fn disk_get_image_index(&self) -> Option<c_uint> {
|
|
|
|
self.disk_get_image_index_cb.map(|f| unsafe { f() })
|
|
|
|
}
|
|
|
|
pub fn disk_set_image_index(&self, index: c_uint) -> Option<bool> {
|
|
|
|
self.disk_set_image_index_cb.map(|f| unsafe { f(index) })
|
|
|
|
}
|
|
|
|
pub fn disk_get_num_images(&self) -> Option<c_uint> {
|
|
|
|
self.disk_get_num_images_cb.map(|f| unsafe { f() })
|
|
|
|
}
|
|
|
|
pub fn disk_replace_image_index(&self, index: c_uint, info: Option<GameInfo>) -> Option<bool> {
|
|
|
|
self.disk_replace_image_index_cb.map(|f| unsafe {
|
|
|
|
let info_ptr = match info {
|
|
|
|
None => ::core::ptr::null(),
|
|
|
|
Some(x) => &x,
|
|
|
|
};
|
|
|
|
f(index, info_ptr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pub fn disk_add_image_index(&self) -> Option<bool> {
|
|
|
|
self.disk_add_image_index_cb.map(|f| unsafe { f() })
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
2019-11-18 08:35:56 +01:00
|
|
|
pub fn hw_context_reset(&self) -> Option<()> {
|
|
|
|
self.hw_context_reset_cb.map(|f| unsafe { f() })
|
|
|
|
}
|
|
|
|
pub fn hw_context_destroy(&self) -> Option<()> {
|
|
|
|
self.hw_context_destroy_cb.map(|f| unsafe { f() })
|
|
|
|
}
|
2019-11-18 10:01:33 +01:00
|
|
|
pub fn get_proc_address(&self, sym: &str) -> Option<ProcAddressFn> {
|
|
|
|
self.get_proc_address_cb.and_then(|f| unsafe {
|
|
|
|
let csym = CString::new(sym).ok()?;
|
|
|
|
Some(f(csym.as_ptr()))
|
|
|
|
})
|
|
|
|
}
|
2019-11-11 01:24:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-17 07:34:01 +01:00
|
|
|
impl Deref for LibretroWrapper {
|
|
|
|
type Target = LibretroApi;
|
|
|
|
fn deref(&self) -> &LibretroApi {
|
2019-11-11 01:24:29 +01:00
|
|
|
&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)
|
2019-11-17 05:27:42 +01:00
|
|
|
pub fn set_handler(handler: Pin<&'_ mut (dyn Handler + '_)>) {
|
2019-11-16 05:59:08 +01:00
|
|
|
unsafe {
|
2019-11-16 08:03:30 +01:00
|
|
|
let ptr = handler.get_unchecked_mut() as *mut dyn Handler;
|
2019-11-19 09:31:14 +01:00
|
|
|
CB_SINGLETON
|
|
|
|
.handler
|
|
|
|
.replace(Pin::new_unchecked(ptr.as_mut().unwrap()));
|
2019-11-18 05:52:35 +01:00
|
|
|
|
|
|
|
c_ext_set_log_print_cb(StaticCallbacks::log_print_cb);
|
2019-11-16 05:59:08 +01:00
|
|
|
}
|
2019-11-18 05:52:35 +01:00
|
|
|
// 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
|
2019-11-19 09:31:14 +01:00
|
|
|
let api = unsafe { CB_SINGLETON.handler.as_mut() }
|
|
|
|
.unwrap()
|
|
|
|
.libretro_core();
|
2019-11-18 05:52:35 +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 01:24:29 +01:00
|
|
|
}
|
2019-11-17 07:25:03 +01:00
|
|
|
|
|
|
|
pub fn unset_handler() {
|
|
|
|
unsafe {
|
|
|
|
CB_SINGLETON.handler.take();
|
|
|
|
}
|
|
|
|
}
|