diff --git a/ferretro_base/Cargo.toml b/ferretro_base/Cargo.toml index 6672807..d513ec8 100644 --- a/ferretro_base/Cargo.toml +++ b/ferretro_base/Cargo.toml @@ -11,3 +11,4 @@ cc = "^1" libretro-sys = "0.1" libloading = "0.5" num_enum = "0.4" +once_cell = "1.8" diff --git a/ferretro_base/src/lib.rs b/ferretro_base/src/lib.rs index 27a2f8f..da23dc1 100644 --- a/ferretro_base/src/lib.rs +++ b/ferretro_base/src/lib.rs @@ -4,8 +4,10 @@ pub mod retro; pub mod prelude { //! Re-exports for types likely to be used by consumers of the crate. + pub use crate::retro::RetroHandlerId; + pub use crate::retro::callbacks::*; pub use crate::retro::constants::*; pub use crate::retro::wrapped_types::*; - pub use crate::retro::wrapper::{RetroCallbacks, LibretroWrapper, LibretroWrapperAccess}; + pub use crate::retro::wrapper::LibretroWrapper; pub use crate::retro::ffi::{PixelFormat, GameGeometry, HwContextResetFn, HwRenderCallback, SystemAvInfo, SystemInfo}; } diff --git a/ferretro_base/src/retro/callbacks.rs b/ferretro_base/src/retro/callbacks.rs new file mode 100644 index 0000000..93f23b6 --- /dev/null +++ b/ferretro_base/src/retro/callbacks.rs @@ -0,0 +1,377 @@ +use std::os::raw::{c_char, c_uint}; +use std::path::PathBuf; + +use crate::retro::constants::*; +use crate::retro::ffi::*; +use crate::retro::wrapped_types::*; +use crate::retro::wrapper::LibretroWrapper; + +// 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" { + pub(crate) fn c_ext_handle_get_log_interface(cb: *mut LogCallback) -> bool; + pub(crate) fn c_ext_set_log_print_cb(cb: WrappedLogPrintFn); +} + +/// This trait represents all the callbacks in the libretro API (including `retro_environment` +/// extensions), wrapped as methods on `&mut self`, all optional with default no-op implementations. +/// +/// NOTE: Most of the method docs provided here are adapted to Rust from the ones written in +/// libretro.h, and many of them are descriptions of the API contract written with an intended +/// audience of backend/core authors. +//noinspection RsSelfConvention +#[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, frame: &VideoFrame) {} + /// Renders audio frames. + /// + /// 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. + /// Format is signed 16-bit native endian PCM. + /// + /// The frontend should return the number of frames used (stereo slice length divided by two!) + fn audio_samples(&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 { None } + /// Boolean value whether or not the implementation should use overscan, + /// or crop away overscan. + fn get_overscan(&mut self) -> Option { 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 `retro_get_log_interface` (or as a fallback, stderr). + fn set_message(&mut self, message: &Message) -> Option { 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 { 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 [CoreAPI::retro_load_game]. + fn set_performance_level(&mut self, level: c_uint) -> Option { 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 { 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 [CoreAPI::retro_load_game] or + /// [Self::set_system_av_info]. + fn set_pixel_format(&mut self, format: PixelFormat) -> Option { None } + /// Sets an array of [InputDescriptor2](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: &[InputDescriptor2]) -> Option { None } + /// Sets an interface to let a libretro core render with + /// hardware acceleration. + /// The core should call this in [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 [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 { 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 { 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 [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. + /// + /// [Variable2::key](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'. + /// + /// [Variable2::description](crate::prelude::Variable2::description) should contain a human readable + /// description of the key. + /// + /// [Variable2::options](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: &[Variable2]) -> Option { 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 { None } + /// If true, the libretro implementation supports calls to + /// [CoreAPI::retro_load_game] with NULL as argument. + /// Used by cores which can run without particular game data. + /// This should be called within [CoreAPI::retro_set_environment] only. + fn set_support_no_game(&mut self, supports_no_game: bool) -> Option { 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 { 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 [CoreAPI::retro_run]. + fn get_input_device_capabilities(&mut self) -> Option { 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 { 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 ([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 { None } + /// Sets a new av_info structure. This can only be called from + /// within [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 [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 [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 { 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 [CoreAPI::retro_load_game_special], + /// and this environment call allows a libretro core to expose which + /// subsystems are supported for use with [CoreAPI::retro_load_game_special]. + /// + /// If a core wants to expose this interface, [Self::set_subsystem_info] + /// **MUST** be called from within [CoreAPI::retro_set_environment]. + fn set_subsystem_info(&mut self, subsystem_info: &[SubsystemInfo2]) -> Option { None } + /// This environment call lets a libretro core tell the frontend + /// which controller subclasses are recognized in calls to + /// [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 [ControllerDescription2](crate::prelude::ControllerDescription2). Each element of the + /// array corresponds to the ascending port index + /// that is passed to [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 [ControllerDescription2](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: &[ControllerDescription2]) -> Option { 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 + /// [CoreAPI::retro_get_memory_data] and + /// [CoreAPI::retro_get_memory_size] as well. + /// + /// Can be called from [CoreAPI::retro_init] and [CoreAPI::retro_load_game]. + fn set_memory_maps(&mut self, memory_map: &MemoryMap) -> Option { 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 [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 { 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 { 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 { None } + // fn set_serialization_quirks(&mut self, quirks: &mut u64) -> Option { 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 [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(&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(&mut self) -> PerfTick { 0 } + /// Returns a bit-mask of detected CPU features ([libretro_sys]::SIMD_*). + fn perf_get_cpu_features(&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(&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(&mut self, counter: &mut PerfCounter) {} + /// Starts a registered counter. + fn perf_start(&mut self, counter: &mut PerfCounter) {} + /// Stops a registered counter. + fn perf_stop(&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 } + fn get_camera_interface(&mut self, width: c_uint, height: c_uint, cap_raw_fb: bool, cap_gl_tex: bool) -> Option { None } + /// Starts the camera driver. Can only be called in retro_run(). + fn camera_start(&mut self) -> Option { None } + /// Stops the camera driver. Can only be called in retro_run(). + fn camera_stop(&mut self) -> Option<()> { None } + /// Gets current framebuffer which is to be rendered to. + /// Could change every frame potentially. + fn hw_get_current_framebuffer(&mut self) -> Option { None } + /// Get a symbol from HW context. + fn hw_get_proc_address(&mut self, sym: &str) -> Option<*const ()> { None } +} + +pub trait LibretroWrapperAccess { + fn libretro_core(&mut self) -> &mut LibretroWrapper; +} + +pub trait RootRetroCallbacks: RetroCallbacks + LibretroWrapperAccess {} + +impl RootRetroCallbacks for T {} diff --git a/ferretro_base/src/retro/mod.rs b/ferretro_base/src/retro/mod.rs index d76f167..1acd80f 100644 --- a/ferretro_base/src/retro/mod.rs +++ b/ferretro_base/src/retro/mod.rs @@ -1,6 +1,69 @@ +use std::pin::Pin; +use std::sync::Mutex; +use callbacks::RootRetroCallbacks; +use once_cell::sync::Lazy; + +pub mod callbacks; pub mod constants; #[allow(non_camel_case_types, non_upper_case_globals, non_snake_case, dead_code)] pub mod ffi; pub mod loading; pub mod wrapped_types; pub mod wrapper; + +// TODO: macro this + +pub const MAX_HANDLERS: usize = 8; + +mod statics_00 { include!("statics.rs"); } +mod statics_01 { include!("statics.rs"); } +mod statics_02 { include!("statics.rs"); } +mod statics_03 { include!("statics.rs"); } +mod statics_04 { include!("statics.rs"); } +mod statics_05 { include!("statics.rs"); } +mod statics_06 { include!("statics.rs"); } +mod statics_07 { include!("statics.rs"); } + +#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] +pub struct RetroHandlerId(usize); + +static mut CALLBACK_REGISTRY: Lazy> = Lazy::new(|| Mutex::new([false; MAX_HANDLERS])); +pub fn set_handler(handler: Pin<&'_ mut (dyn RootRetroCallbacks + '_)>) -> Result> { + let mut lock = unsafe { &mut CALLBACK_REGISTRY }.lock()?; + for (id, taken) in lock.iter_mut().enumerate() { + if !*taken { + match id { + 0 => statics_00::set_handler(handler), + 1 => statics_01::set_handler(handler), + 2 => statics_02::set_handler(handler), + 3 => statics_03::set_handler(handler), + 4 => statics_04::set_handler(handler), + 5 => statics_05::set_handler(handler), + 6 => statics_06::set_handler(handler), + 7 => statics_07::set_handler(handler), + _ => unreachable!(), + } + *taken = true; + return Ok(RetroHandlerId(id)) + } + } + Err(format!("only compiled with support for up to {} callback handlers", MAX_HANDLERS).into()) +} + +pub fn unset_handler(id: RetroHandlerId) -> Result<(), Box> { + let mut lock = unsafe { &mut CALLBACK_REGISTRY }.lock()?; + if let Some(taken) = lock.get_mut(id.0) { + *taken = false; + } + match id.0 { + 0 => Ok(statics_00::unset_handler()), + 1 => Ok(statics_01::unset_handler()), + 2 => Ok(statics_02::unset_handler()), + 3 => Ok(statics_03::unset_handler()), + 4 => Ok(statics_04::unset_handler()), + 5 => Ok(statics_05::unset_handler()), + 6 => Ok(statics_06::unset_handler()), + 7 => Ok(statics_07::unset_handler()), + x => Err(format!("invalid RetroHandlerId provided: {}", x).into()) + } +} diff --git a/ferretro_base/src/retro/statics.rs b/ferretro_base/src/retro/statics.rs new file mode 100644 index 0000000..86ec278 --- /dev/null +++ b/ferretro_base/src/retro/statics.rs @@ -0,0 +1,494 @@ +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>, + 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(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 { + *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(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 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::(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::(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::(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::(data)?)? + } + EnvCmd::SetProcAddressCallback => { + let gpa = Self::from_void::(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 = 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::(data)?.clone(); + handler.set_memory_maps(&map)? + }, + EnvCmd::SetGeometry => { + handler.set_geometry(Self::from_void::(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::(); + 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::(); + 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::(); + 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(); + } +} diff --git a/ferretro_base/src/retro/wrapper.rs b/ferretro_base/src/retro/wrapper.rs index 1c6e425..9b655f7 100644 --- a/ferretro_base/src/retro/wrapper.rs +++ b/ferretro_base/src/retro/wrapper.rs @@ -1,867 +1,39 @@ #![allow(clippy::option_map_unit_fn)] // sorry clippy, we really need the conciseness -use core::convert::{TryFrom, TryInto}; -use core::ffi::c_void; -use core::mem::size_of; use core::ops::{Deref, DerefMut}; -use core::pin::Pin; -use core::slice::from_raw_parts; use core::time::Duration; -use std::ffi::{CStr, CString}; -use std::os::raw::{c_char, c_uint}; -use std::path::{Path, PathBuf}; +use std::ffi::CString; +use std::os::raw::c_uint; -use num_enum::TryFromPrimitive; - -use super::constants::*; use super::ffi::*; use super::loading::*; -use super::wrapped_types::*; // #[cfg(doc)] <- broken as of (at least) 1.56 #[allow(unused_imports)] use libretro_sys::{self, CoreAPI}; -static mut CB_SINGLETON: StaticCallbacks = StaticCallbacks { - handler: None, - pix_fmt: PixelFormat::ARGB1555, -}; - -// 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); -} - -/// This trait represents all the callbacks in the libretro API (including `retro_environment` -/// extensions), wrapped as methods on `&mut self`, all optional with default no-op implementations. -/// -/// NOTE: Most of the method docs provided here are adapted to Rust from the ones written in -/// libretro.h, and many of them are descriptions of the API contract written with an intended -/// audience of backend/core authors. -//noinspection RsSelfConvention -#[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, frame: &VideoFrame) {} - /// Renders audio frames. - /// - /// 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. - /// Format is signed 16-bit native endian PCM. - /// - /// The frontend should return the number of frames used (stereo slice length divided by two!) - fn audio_samples(&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 { None } - /// Boolean value whether or not the implementation should use overscan, - /// or crop away overscan. - fn get_overscan(&mut self) -> Option { 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 `retro_get_log_interface` (or as a fallback, stderr). - fn set_message(&mut self, message: &Message) -> Option { 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 { 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 [CoreAPI::retro_load_game]. - fn set_performance_level(&mut self, level: c_uint) -> Option { 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 { 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 [CoreAPI::retro_load_game] or - /// [Self::set_system_av_info]. - fn set_pixel_format(&mut self, format: PixelFormat) -> Option { None } - /// Sets an array of [InputDescriptor2](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: &[InputDescriptor2]) -> Option { None } - /// Sets an interface to let a libretro core render with - /// hardware acceleration. - /// The core should call this in [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 [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 { 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 { 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 [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. - /// - /// [Variable2::key](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'. - /// - /// [Variable2::description](crate::prelude::Variable2::description) should contain a human readable - /// description of the key. - /// - /// [Variable2::options](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: &[Variable2]) -> Option { 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 { None } - /// If true, the libretro implementation supports calls to - /// [CoreAPI::retro_load_game] with NULL as argument. - /// Used by cores which can run without particular game data. - /// This should be called within [CoreAPI::retro_set_environment] only. - fn set_support_no_game(&mut self, supports_no_game: bool) -> Option { 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 { 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 [CoreAPI::retro_run]. - fn get_input_device_capabilities(&mut self) -> Option { 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 { 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 ([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 { None } - /// Sets a new av_info structure. This can only be called from - /// within [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 [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 [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 { 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 [CoreAPI::retro_load_game_special], - /// and this environment call allows a libretro core to expose which - /// subsystems are supported for use with [CoreAPI::retro_load_game_special]. - /// - /// If a core wants to expose this interface, [Self::set_subsystem_info] - /// **MUST** be called from within [CoreAPI::retro_set_environment]. - fn set_subsystem_info(&mut self, subsystem_info: &[SubsystemInfo2]) -> Option { None } - /// This environment call lets a libretro core tell the frontend - /// which controller subclasses are recognized in calls to - /// [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 [ControllerDescription2](crate::prelude::ControllerDescription2). Each element of the - /// array corresponds to the ascending port index - /// that is passed to [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 [ControllerDescription2](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: &[ControllerDescription2]) -> Option { 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 - /// [CoreAPI::retro_get_memory_data] and - /// [CoreAPI::retro_get_memory_size] as well. - /// - /// Can be called from [CoreAPI::retro_init] and [CoreAPI::retro_load_game]. - fn set_memory_maps(&mut self, memory_map: &MemoryMap) -> Option { 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 [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 { 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 { 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 { None } - // fn set_serialization_quirks(&mut self, quirks: &mut u64) -> Option { 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 [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(&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(&mut self) -> PerfTick { 0 } - /// Returns a bit-mask of detected CPU features ([libretro_sys]::SIMD_*). - fn perf_get_cpu_features(&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(&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(&mut self, counter: &mut PerfCounter) {} - /// Starts a registered counter. - fn perf_start(&mut self, counter: &mut PerfCounter) {} - /// Stops a registered counter. - fn perf_stop(&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 } - fn get_camera_interface(&mut self, width: c_uint, height: c_uint, cap_raw_fb: bool, cap_gl_tex: bool) -> Option { None } - /// Starts the camera driver. Can only be called in retro_run(). - fn camera_start(&mut self) -> Option { None } - /// Stops the camera driver. Can only be called in retro_run(). - fn camera_stop(&mut self) -> Option<()> { None } - /// Gets current framebuffer which is to be rendered to. - /// Could change every frame potentially. - fn hw_get_current_framebuffer(&mut self) -> Option { None } - /// Get a symbol from HW context. - fn hw_get_proc_address(&mut self, sym: &str) -> Option<*const ()> { None } -} - -pub trait LibretroWrapperAccess { - fn libretro_core(&mut self) -> &mut LibretroWrapper; -} - -pub trait RootRetroCallbacks : RetroCallbacks + LibretroWrapperAccess {} -impl RootRetroCallbacks for T {} - -struct StaticCallbacks { - handler: Option>, - 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(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 { - *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(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 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::(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::(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::(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::(data)?)? - } - EnvCmd::SetProcAddressCallback => { - let gpa = Self::from_void::(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 = 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::(data)?.clone(); - handler.set_memory_maps(&map)? - }, - EnvCmd::SetGeometry => { - handler.set_geometry(Self::from_void::(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::(); - 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::(); - 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::(); - 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() - } -} - pub struct LibretroWrapper { - api: LibretroApi, - keyboard_event_cb: Option, - frame_time_cb: Option, - audio_ready_cb: Option, - audio_set_state_cb: Option, - disk_get_eject_state_cb: Option, - disk_set_eject_state_cb: Option, - disk_get_image_index_cb: Option, - disk_set_image_index_cb: Option, - disk_get_num_images_cb: Option, - disk_replace_image_index_cb: Option, - disk_add_image_index_cb: Option, - camera_frame_raw_framebuffer_cb: Option, - camera_frame_opengl_texture_cb: Option, - camera_lifetime_initialized_cb: Option, - camera_lifetime_deinitialized_cb: Option, - hw_context_reset_cb: Option, - hw_context_destroy_cb: Option, // same signature, libretro-sys deduplicated... - get_proc_address_cb: Option, + pub(crate) api: LibretroApi, + pub(crate) keyboard_event_cb: Option, + pub(crate) frame_time_cb: Option, + pub(crate) audio_ready_cb: Option, + pub(crate) audio_set_state_cb: Option, + pub(crate) disk_get_eject_state_cb: Option, + pub(crate) disk_set_eject_state_cb: Option, + pub(crate) disk_get_image_index_cb: Option, + pub(crate) disk_set_image_index_cb: Option, + pub(crate) disk_get_num_images_cb: Option, + pub(crate) disk_replace_image_index_cb: Option, + pub(crate) disk_add_image_index_cb: Option, + pub(crate) camera_frame_raw_framebuffer_cb: Option, + pub(crate) camera_frame_opengl_texture_cb: Option, + pub(crate) camera_lifetime_initialized_cb: Option, + pub(crate) camera_lifetime_deinitialized_cb: Option, + pub(crate) hw_context_reset_cb: Option, + // same signature as above, libretro-sys deduplicated the type... + pub(crate) hw_context_destroy_cb: Option, + pub(crate) get_proc_address_cb: Option, } impl From for LibretroWrapper { @@ -982,33 +154,3 @@ impl DerefMut for LibretroWrapper { &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(); - } -} diff --git a/ferretro_components/src/base/mod.rs b/ferretro_components/src/base/mod.rs index a6bcfeb..eaa06f3 100644 --- a/ferretro_components/src/base/mod.rs +++ b/ferretro_components/src/base/mod.rs @@ -13,6 +13,7 @@ use std::path::{PathBuf, Path}; use std::io::Read; use std::collections::HashMap; use std::ffi::c_void; +use ferretro_base::prelude::{LibretroWrapperAccess, RetroHandlerId}; pub struct RetroComponentBase { retro: LibretroWrapper, @@ -34,6 +35,8 @@ pub struct RetroComponentBase { cached_controller_info: Option>, cached_memory_map: Option, cached_geometry: Option, + + handler_id: Option, } // TODO: replace with std::ops::ControlFlow when it becomes stable @@ -77,11 +80,13 @@ impl RetroComponentBase { cached_subsystem_info: None, cached_controller_info: None, cached_memory_map: None, - cached_geometry: None + cached_geometry: None, + handler_id: None, }; let mut pin_emu = Box::pin(emu); - ferretro_base::retro::wrapper::set_handler(pin_emu.as_mut()); + let id = ferretro_base::retro::set_handler(pin_emu.as_mut()).unwrap(); + pin_emu.handler_id = Some(id); pin_emu } @@ -584,6 +589,6 @@ impl RetroCallbacks for RetroComponentBase { impl Drop for RetroComponentBase { fn drop(&mut self) { - ferretro_base::retro::wrapper::unset_handler(); + ferretro_base::retro::unset_handler(self.handler_id.unwrap()).unwrap(); } } diff --git a/ferretro_components/src/lib.rs b/ferretro_components/src/lib.rs index b9df277..7b0d079 100644 --- a/ferretro_components/src/lib.rs +++ b/ferretro_components/src/lib.rs @@ -4,8 +4,9 @@ pub mod base; pub mod prelude { //! Re-exports for types likely to be used by consumers of the crate. pub use crate::base::{RetroComponent, RetroComponentBase}; + pub use ferretro_base::retro::callbacks::{RetroCallbacks, LibretroWrapperAccess}; pub use ferretro_base::retro::constants::*; pub use ferretro_base::retro::wrapped_types::*; - pub use ferretro_base::retro::wrapper::{RetroCallbacks, LibretroWrapper, LibretroWrapperAccess}; + pub use ferretro_base::retro::wrapper::LibretroWrapper; pub use ferretro_base::retro::ffi::{PixelFormat, GameGeometry, HwContextResetFn, HwRenderCallback, SystemAvInfo, SystemInfo, MemoryMap, LogLevel}; }