898 lines
41 KiB
Rust
898 lines
41 KiB
Rust
use core::convert::TryInto;
|
|
use core::ffi::c_void;
|
|
use core::slice::from_raw_parts;
|
|
|
|
use std::convert::TryFrom;
|
|
use std::ffi::{CStr, CString};
|
|
use std::ops::{Deref, DerefMut};
|
|
use std::os::raw::{c_char, c_uint};
|
|
use std::os::unix::ffi::OsStrExt;
|
|
use std::path::{Path, PathBuf};
|
|
use std::pin::Pin;
|
|
use std::time::Duration;
|
|
|
|
use num_enum::TryFromPrimitive;
|
|
|
|
use super::constants::*;
|
|
use super::ffi::*;
|
|
use super::loading::*;
|
|
use super::wrapped_types::*;
|
|
|
|
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);
|
|
}
|
|
|
|
// method docs largely copied/lightly-adapted-to-rust straight from libretro.h.
|
|
#[rustfmt::skip]
|
|
#[allow(unused_variables)]
|
|
pub trait RetroCallbacks: Unpin + 'static {
|
|
// -- main callbacks --
|
|
/// Render a frame. Pixel format is 15-bit 0RGB1555 native endian
|
|
/// unless changed (see [Self::set_pixel_format]).
|
|
///
|
|
/// Width and height specify dimensions of buffer.
|
|
/// Pitch specifices length in bytes between two lines in buffer.
|
|
///
|
|
/// For performance reasons, it is highly recommended to have a frame
|
|
/// that is packed in memory, i.e. pitch == width * byte_per_pixel.
|
|
/// Certain graphic APIs, such as OpenGL ES, do not like textures
|
|
/// that are not packed in memory.
|
|
fn video_refresh(&mut self, data: &[u8], width: c_uint, height: c_uint, pitch: c_uint) {}
|
|
/// Called instead of video_refresh when the core reports a duplicate frame (NULL).
|
|
fn video_refresh_dupe(&mut self, width: c_uint, height: c_uint, pitch: c_uint) {}
|
|
/// Renders a single audio frame. Should only be used if implementation
|
|
/// generates a single sample at a time.
|
|
/// Format is signed 16-bit native endian.
|
|
fn audio_sample(&mut self, left: i16, right: i16) {}
|
|
/// Renders multiple audio frames in one go.
|
|
///
|
|
/// One frame is defined as a sample of left and right channels, interleaved.
|
|
/// I.e. int16_t buf\[4\] = { l, r, l, r }; would be 2 frames.
|
|
/// Only one of the audio callbacks must ever be used.
|
|
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize { stereo_pcm.len() }
|
|
/// Polls input.
|
|
fn input_poll(&mut self) {}
|
|
/// Queries for input for player 'port'.
|
|
fn input_state(&mut self, port: u32, device: InputDeviceId, index: InputIndex) -> i16 { 0 }
|
|
|
|
// -- environment callbacks --
|
|
/// Sets screen rotation of graphics.
|
|
/// Is only implemented if rotation can be accelerated by hardware.
|
|
/// Valid values are 0, 1, 2, 3, which rotates screen by 0, 90, 180,
|
|
/// 270 degrees counter-clockwise respectively.
|
|
fn set_rotation(&mut self, rotation: EnvRotation) -> Option<bool> { None }
|
|
/// Boolean value whether or not the implementation should use overscan,
|
|
/// or crop away overscan.
|
|
fn get_overscan(&mut self) -> Option<bool> { None }
|
|
/// Sets a message to be displayed in implementation-specific manner
|
|
/// for a certain amount of 'frames'.
|
|
/// Should not be used for trivial messages, which should simply be
|
|
/// logged via [Self::get_log_interface] (or as a
|
|
/// fallback, stderr).
|
|
fn set_message(&mut self, message: &Message) -> Option<bool> { None }
|
|
/// Requests the frontend to shutdown.
|
|
/// Should only be used if game has a specific
|
|
/// way to shutdown the game from a menu item or similar.
|
|
fn shutdown(&mut self) -> Option<bool> { None }
|
|
/// Gives a hint to the frontend how demanding this implementation
|
|
/// is on a system. E.g. reporting a level of 2 means
|
|
/// this implementation should run decently on all frontends
|
|
/// of level 2 and up.
|
|
///
|
|
/// It can be used by the frontend to potentially warn
|
|
/// about too demanding implementations.
|
|
///
|
|
/// The levels are "floating".
|
|
///
|
|
/// This function can be called on a per-game basis,
|
|
/// as certain games an implementation can play might be
|
|
/// particularly demanding.
|
|
/// If called, it should be called in [libretro_sys::CoreAPI::retro_load_game].
|
|
fn set_performance_level(&mut self, level: c_uint) -> Option<bool> { None }
|
|
/// Returns the "system" directory of the frontend.
|
|
/// This directory can be used to store system specific
|
|
/// content such as BIOSes, configuration data, etc.
|
|
/// The returned value can be `None`.
|
|
/// If so, no such directory is defined,
|
|
/// and it's up to the implementation to find a suitable directory.
|
|
///
|
|
/// NOTE: Some cores used this folder also for "save" data such as
|
|
/// memory cards, etc, for lack of a better place to put it.
|
|
/// This is now discouraged, and if possible, cores should try to
|
|
/// use the new [Self::get_save_directory].
|
|
fn get_system_directory(&mut self) -> Option<PathBuf> { None }
|
|
/// Sets the internal pixel format used by the implementation.
|
|
/// The default pixel format is [libretro_sys::PixelFormat::ARGB1555].
|
|
/// This pixel format however, is deprecated (see enum retro_pixel_format).
|
|
/// If the call returns false, the frontend does not support this pixel
|
|
/// format.
|
|
///
|
|
/// The core should call this function inside [libretro_sys::CoreAPI::retro_load_game] or
|
|
/// [Self::set_system_av_info].
|
|
fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> { None }
|
|
/// Sets an array of [crate::prelude::InputDescriptor2].
|
|
/// It is up to the frontend to present this in a usable way.
|
|
/// This function can be called at any time, but it is recommended
|
|
/// for the core to call it as early as possible.
|
|
fn set_input_descriptors(&mut self, input_descriptors: &Vec<InputDescriptor2>) -> Option<bool> { None }
|
|
/// Sets an interface to let a libretro core render with
|
|
/// hardware acceleration.
|
|
/// The core should call this in [libretro_sys::CoreAPI::retro_load_game].
|
|
/// If successful, libretro cores will be able to render to a
|
|
/// frontend-provided framebuffer.
|
|
/// The size of this framebuffer will be at least as large as
|
|
/// max_width/max_height provided in [libretro_sys::CoreAPI::retro_get_system_av_info].
|
|
/// If HW rendering is used, pass only [libretro_sys::HW_FRAME_BUFFER_VALID] or
|
|
/// NULL to [libretro_sys::VideoRefreshFn].
|
|
fn set_hw_render(&mut self, hw_render_callback: &HwRenderCallback) -> Option<bool> { None }
|
|
/// Interface to acquire user-defined information from environment
|
|
/// that cannot feasibly be supported in a multi-system way.
|
|
/// 'key' should be set to a key which has already been set by
|
|
/// [Self::set_variables].
|
|
fn get_variable(&mut self, key: &str) -> Option<String> { None }
|
|
/// Allows an implementation to signal the environment
|
|
/// which variables it might want to check for later using
|
|
/// [Self::get_variable].
|
|
/// This allows the frontend to present these variables to
|
|
/// a user dynamically.
|
|
/// The core should call this for the first time as early as
|
|
/// possible (ideally in [libretro_sys::CoreAPI::retro_set_environment]).
|
|
/// Afterward it may be called again for the core to communicate
|
|
/// updated options to the frontend, but the number of core
|
|
/// options must not change from the number in the initial call.
|
|
///
|
|
/// [crate::prelude::Variable2::key] should be namespaced to not collide
|
|
/// with other implementations' keys. E.g. A core called
|
|
/// 'foo' should use keys named as 'foo_option'.
|
|
///
|
|
/// [crate::prelude::Variable2::description] should contain a human readable
|
|
/// description of the key.
|
|
///
|
|
/// [crate::prelude::Variable2::options] should contain the list of expected values.
|
|
/// The number of possible options should be very limited,
|
|
/// i.e. it should be feasible to cycle through options
|
|
/// without a keyboard. The first entry should be treated as a default.
|
|
///
|
|
/// Only strings are operated on. The possible values will
|
|
/// generally be displayed and stored as-is by the frontend.
|
|
fn set_variables(&mut self, variables: &Vec<Variable2>) -> Option<bool> { None }
|
|
/// Result is set to true if some variables are updated by
|
|
/// frontend since last call to [Self::get_variable].
|
|
/// Variables should be queried with [Self::get_variable].
|
|
fn get_variable_update(&mut self) -> Option<bool> { None }
|
|
/// If true, the libretro implementation supports calls to
|
|
/// [libretro_sys::CoreAPI::retro_load_game] with NULL as argument.
|
|
/// Used by cores which can run without particular game data.
|
|
/// This should be called within [libretro_sys::CoreAPI::retro_set_environment] only.
|
|
fn set_support_no_game(&mut self, supports_no_game: bool) -> Option<bool> { None }
|
|
/// Retrieves the absolute path from where this libretro
|
|
/// implementation was loaded.
|
|
/// `None` is returned if the libretro was loaded statically
|
|
/// (i.e. linked statically to frontend), or if the path cannot be
|
|
/// determined.
|
|
/// Mostly useful in cooperation with [Self::set_support_no_game] as assets can
|
|
/// be loaded without ugly hacks.
|
|
fn get_libretro_path(&mut self) -> Option<PathBuf> { None }
|
|
/// Gets a bitmask telling which device type are expected to be
|
|
/// handled properly in a call to retro_input_state_t.
|
|
/// Devices which are not handled or recognized always return
|
|
/// 0 in [Self::input_state].
|
|
/// Example bitmask: caps = (1 << [libretro_sys::DEVICE_JOYPAD]) | (1 << [libretro_sys::DEVICE_ANALOG]).
|
|
/// Should only be called in [libretro_sys::CoreAPI::retro_run].
|
|
fn get_input_device_capabilities(&mut self) -> Option<u64> { None }
|
|
/// Returns the "core assets" directory of the frontend.
|
|
/// This directory can be used to store specific assets that the
|
|
/// core relies upon, such as art assets,
|
|
/// input data, etc etc.
|
|
/// The returned value can be `None`.
|
|
/// If so, no such directory is defined,
|
|
/// and it's up to the implementation to find a suitable directory.
|
|
fn get_core_assets_directory(&mut self) -> Option<PathBuf> { None }
|
|
/// Returns the "save" directory of the frontend, unless there is no
|
|
/// save directory available. The save directory should be used to
|
|
/// store SRAM, memory cards, high scores, etc, if the libretro core
|
|
/// cannot use the regular memory interface ([libretro_sys::CoreAPI::retro_get_memory_data]).
|
|
///
|
|
/// If the frontend cannot designate a save directory, it will return
|
|
/// `None` to indicate that the core should attempt to operate without a
|
|
/// save directory set.
|
|
///
|
|
/// NOTE: early libretro cores used the system directory for save
|
|
/// files. Cores that need to be backwards-compatible can still check
|
|
/// [Self::get_system_directory].
|
|
fn get_save_directory(&mut self) -> Option<PathBuf> { None }
|
|
/// Sets a new av_info structure. This can only be called from
|
|
/// within [libretro_sys::CoreAPI::retro_run].
|
|
/// This should *only* be used if the core is completely altering the
|
|
/// internal resolutions, aspect ratios, timings, sampling rate, etc.
|
|
/// Calling this can require a full reinitialization of video/audio
|
|
/// drivers in the frontend,
|
|
///
|
|
/// so it is important to call it very sparingly, and usually only with
|
|
/// the users explicit consent.
|
|
/// An eventual driver reinitialize will happen so that video and
|
|
/// audio callbacks
|
|
/// happening after this call within the same [libretro_sys::CoreAPI::retro_run] call will
|
|
/// target the newly initialized driver.
|
|
///
|
|
/// This callback makes it possible to support configurable resolutions
|
|
/// in games, which can be useful to
|
|
/// avoid setting the "worst case" in `max_width`/`max_height`.
|
|
///
|
|
/// ***HIGHLY RECOMMENDED*** Do not call this callback every time
|
|
/// resolution changes in an emulator core if it's
|
|
/// expected to be a temporary change, for the reasons of possible
|
|
/// driver reinitialization.
|
|
/// This call is not a free pass for not trying to provide
|
|
/// correct values in [libretro_sys::CoreAPI::retro_get_system_av_info]. If you need to change
|
|
/// things like aspect ratio or nominal width/height,
|
|
/// use [Self::set_geometry], which is a softer variant
|
|
/// of [Self::set_system_av_info].
|
|
///
|
|
/// If this returns false, the frontend does not acknowledge a
|
|
/// changed av_info struct.
|
|
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> Option<bool> { None }
|
|
/// This environment call introduces the concept of libretro "subsystems".
|
|
/// A subsystem is a variant of a libretro core which supports
|
|
/// different kinds of games.
|
|
/// The purpose of this is to support e.g. emulators which might
|
|
/// have special needs, e.g. Super Nintendo's Super GameBoy, Sufami Turbo.
|
|
/// It can also be used to pick among subsystems in an explicit way
|
|
/// if the libretro implementation is a multi-system emulator itself.
|
|
///
|
|
/// Loading a game via a subsystem is done with [libretro_sys::CoreAPI::retro_load_game_special],
|
|
/// and this environment call allows a libretro core to expose which
|
|
/// subsystems are supported for use with [libretro_sys::CoreAPI::retro_load_game_special].
|
|
///
|
|
/// If a core wants to expose this interface, [Self::set_subsystem_info]
|
|
/// **MUST** be called from within [libretro_sys::CoreAPI::retro_set_environment].
|
|
fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> Option<bool> { None }
|
|
/// This environment call lets a libretro core tell the frontend
|
|
/// which controller subclasses are recognized in calls to
|
|
/// [libretro_sys::CoreAPI::retro_set_controller_port_device].
|
|
///
|
|
/// Some emulators such as Super Nintendo support multiple lightgun
|
|
/// types which must be specifically selected from. It is therefore
|
|
/// sometimes necessary for a frontend to be able to tell the core
|
|
/// about a special kind of input device which is not specifcally
|
|
/// provided by the Libretro API.
|
|
///
|
|
/// In order for a frontend to understand the workings of those devices,
|
|
/// they must be defined as a specialized subclass of the generic device
|
|
/// types already defined in the libretro API.
|
|
///
|
|
/// The core must pass an array of [crate::prelude::ControllerDescription2]. Each element of the
|
|
/// array corresponds to the ascending port index
|
|
/// that is passed to [libretro_sys::CoreAPI::retro_set_controller_port_device] when that function
|
|
/// is called to indicate to the core that the frontend has changed the
|
|
/// active device subclass.
|
|
///
|
|
/// The ascending input port indexes provided by the core in the struct
|
|
/// are generally presented by frontends as ascending User # or Player #,
|
|
/// such as Player 1, Player 2, Player 3, etc. Which device subclasses are
|
|
/// supported can vary per input port.
|
|
///
|
|
/// Each entry in the controller_info array specifies the names and
|
|
/// codes of all device subclasses that are available for the corresponding
|
|
/// User or Player, beginning with the generic Libretro device that the
|
|
/// subclasses are derived from. The second inner element of each entry is the
|
|
/// total number of subclasses that are listed in the [crate::prelude::ControllerDescription2].
|
|
///
|
|
/// NOTE: Even if special device types are set in the libretro core,
|
|
/// libretro should only poll input based on the base input device types.
|
|
fn set_controller_info(&mut self, controller_info: &Vec<ControllerDescription2>) -> Option<bool> { None }
|
|
/// This environment call lets a libretro core tell the frontend
|
|
/// about the memory maps this core emulates.
|
|
/// This can be used to implement, for example, cheats in a core-agnostic way.
|
|
///
|
|
/// Should only be used by emulators; it doesn't make much sense for
|
|
/// anything else.
|
|
/// It is recommended to expose all relevant pointers through
|
|
/// [libretro_sys::CoreAPI::retro_get_memory_data] and
|
|
/// [libretro_sys::CoreAPI::retro_get_memory_size] as well.
|
|
///
|
|
/// Can be called from [libretro_sys::CoreAPI::retro_init] and [libretro_sys::CoreAPI::retro_load_game].
|
|
fn set_memory_maps(&mut self, memory_map: &MemoryMap) -> Option<bool> { None }
|
|
/// This environment call is similar to [Self::set_system_av_info] for changing
|
|
/// video parameters, but provides a guarantee that drivers will not be
|
|
/// reinitialized.
|
|
/// This can only be called from within [libretro_sys::CoreAPI::retro_run].
|
|
///
|
|
/// The purpose of this call is to allow a core to alter nominal
|
|
/// width/heights as well as aspect ratios on-the-fly, which can be
|
|
/// useful for some emulators to change in run-time.
|
|
///
|
|
/// max_width/max_height arguments are ignored and cannot be changed
|
|
/// with this call as this could potentially require a reinitialization or a
|
|
/// non-constant time operation.
|
|
/// If max_width/max_height are to be changed, [Self::set_system_av_info] is required.
|
|
///
|
|
/// A frontend must guarantee that this environment call completes in
|
|
/// constant time.
|
|
fn set_geometry(&mut self, game_geometry: &GameGeometry) -> Option<bool> { None }
|
|
/// Returns the specified username of the frontend, if specified by the user.
|
|
/// This username can be used as a nickname for a core that has online facilities
|
|
/// or any other mode where personalization of the user is desirable.
|
|
/// The returned value can be `None`.
|
|
/// If this environ callback is used by a core that requires a valid username,
|
|
/// a default username should be specified by the core.
|
|
fn get_username(&mut self) -> Option<String> { None }
|
|
/// Returns the specified language of the frontend, if specified by the user.
|
|
/// It can be used by the core for localization purposes.
|
|
fn get_language(&mut self) -> Option<Language> { None }
|
|
// fn set_serialization_quirks(&mut self, quirks: &mut u64) -> Option<bool> { None }
|
|
|
|
// -- environment-set callbacks (API extensions) --
|
|
/// Logging function.
|
|
fn log_print(&mut self, level: LogLevel, msg: &str) {}
|
|
/// Sets rumble state for joypad plugged in port 'port'.
|
|
/// Rumble effects are controlled independently,
|
|
/// and setting e.g. strong rumble does not override weak rumble.
|
|
/// Strength has a range of \[0, 0xffff\].
|
|
///
|
|
/// Returns true if rumble state request was honored.
|
|
/// Calling this before first [libretro_sys::CoreAPI::retro_run] is likely to return false.
|
|
fn set_rumble_state(&mut self, port: c_uint, effect: RumbleEffect, strength: u16) -> bool { false }
|
|
/// Returns current time in microseconds.
|
|
/// Tries to use the most accurate timer available.
|
|
fn perf_get_time_usec_cb(&mut self) -> Time { 0 }
|
|
/// A simple counter. Usually nanoseconds, but can also be CPU cycles.
|
|
/// Can be used directly if desired (when creating a more sophisticated
|
|
/// performance counter system).
|
|
fn perf_get_counter_cb(&mut self) -> PerfTick { 0 }
|
|
/// Returns a bit-mask of detected CPU features ([libretro_sys]::SIMD_*).
|
|
fn perf_get_cpu_features_cb(&mut self) -> u64 { 0 }
|
|
/// Asks frontend to log and/or display the state of performance counters.
|
|
/// Performance counters can always be poked into manually as well.
|
|
fn perf_log_cb(&mut self) {}
|
|
/// Register a performance counter.
|
|
/// ident field must be set with a discrete value and other values in
|
|
/// retro_perf_counter must be 0.
|
|
/// Registering can be called multiple times. To avoid calling to
|
|
/// frontend redundantly, you can check registered field first.
|
|
fn perf_register_cb(&mut self, counter: &mut PerfCounter) {}
|
|
/// Starts a registered counter.
|
|
fn perf_start_cb(&mut self, counter: &mut PerfCounter) {}
|
|
/// Stops a registered counter.
|
|
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 }
|
|
}
|
|
|
|
pub trait LibretroWrapperAccess {
|
|
fn libretro_core(&mut self) -> &mut LibretroWrapper;
|
|
}
|
|
|
|
pub trait RootRetroCallbacks : RetroCallbacks + LibretroWrapperAccess {}
|
|
impl<T: RetroCallbacks + LibretroWrapperAccess> RootRetroCallbacks for T {}
|
|
|
|
#[derive(Default)]
|
|
struct StaticCallbacks {
|
|
handler: Option<Pin<&'static mut dyn RootRetroCallbacks>>,
|
|
}
|
|
|
|
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().as_os_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 => {
|
|
handler.set_pixel_format(PixelFormat::from_uint(*Self::from_void(data)?)?)?
|
|
}
|
|
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;
|
|
false // TODO: finish
|
|
}
|
|
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)?
|
|
}
|
|
// TODO (apathy) EnvCmd::GetCameraInterface => {},
|
|
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 controller_info = unsafe { from_raw_parts(info.types, len) }
|
|
.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 (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() } {
|
|
if data.is_null() {
|
|
cb.video_refresh_dupe(width, height, pitch as c_uint);
|
|
} else if data != HW_FRAME_BUFFER_VALID {
|
|
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 c_uint);
|
|
}
|
|
}
|
|
}
|
|
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,
|
|
}
|
|
}
|
|
extern "C" fn perf_get_time_usec_cb() -> Time {
|
|
unsafe {
|
|
CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_get_time_usec_cb())
|
|
}.unwrap_or_default()
|
|
}
|
|
extern "C" fn perf_get_counter_cb() -> PerfTick {
|
|
unsafe {
|
|
CB_SINGLETON.handler.as_mut().map(|cb| cb.perf_get_counter_cb())
|
|
}.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_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()
|
|
}
|
|
|
|
// TODO: trait methods, etc.
|
|
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
|
|
extern "C" fn hw_get_current_framebuffer_fn() -> usize {
|
|
0
|
|
}
|
|
}
|
|
|
|
pub struct LibretroWrapper {
|
|
api: LibretroApi,
|
|
keyboard_event_cb: Option<KeyboardEventFn>,
|
|
frame_time_cb: Option<FrameTimeCallbackFn>,
|
|
audio_ready_cb: Option<AudioCallbackFn>,
|
|
audio_set_state_cb: Option<AudioSetStateCallbackFn>,
|
|
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>,
|
|
hw_context_reset_cb: Option<HwContextResetFn>,
|
|
hw_context_destroy_cb: Option<HwContextResetFn>, // same signature, libretro-sys deduplicated...
|
|
get_proc_address_cb: Option<GetProcAddressFn>,
|
|
}
|
|
|
|
impl From<LibretroApi> for LibretroWrapper {
|
|
fn from(api: LibretroApi) -> Self {
|
|
LibretroWrapper {
|
|
api,
|
|
keyboard_event_cb: None,
|
|
frame_time_cb: None,
|
|
audio_ready_cb: None,
|
|
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,
|
|
disk_add_image_index_cb: None,
|
|
hw_context_reset_cb: None,
|
|
hw_context_destroy_cb: None,
|
|
get_proc_address_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,
|
|
) -> Option<()> {
|
|
self.keyboard_event_cb
|
|
.map(|f| unsafe { f(down, keycode, character, key_modifiers) })
|
|
}
|
|
pub fn frame_time(&self, time: Duration) -> Option<()> {
|
|
self.frame_time_cb
|
|
.map(|f| unsafe { f(time.as_micros() as Usec) })
|
|
}
|
|
pub fn audio_ready(&self) -> Option<()> {
|
|
self.audio_ready_cb.map(|f| unsafe { f() })
|
|
}
|
|
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() })
|
|
}
|
|
pub fn disk_set_eject_state(&self, ejected: bool) -> Option<bool> {
|
|
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() })
|
|
}
|
|
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() })
|
|
}
|
|
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()))
|
|
})
|
|
}
|
|
}
|
|
|
|
impl Deref for LibretroWrapper {
|
|
type Target = LibretroApi;
|
|
fn deref(&self) -> &LibretroApi {
|
|
&self.api
|
|
}
|
|
}
|
|
|
|
impl DerefMut for LibretroWrapper {
|
|
fn deref_mut(&mut self) -> &mut LibretroApi {
|
|
&mut 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 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 fn unset_handler() {
|
|
unsafe {
|
|
CB_SINGLETON.handler.take();
|
|
}
|
|
}
|