sorta works! ffmpeg component might need updating to latest example code

This commit is contained in:
lifning 2021-08-17 04:06:40 -07:00
parent 9dcbc94847
commit a7c4f0909d
13 changed files with 919 additions and 286 deletions

View File

@ -8,15 +8,21 @@ edition = "2018"
cc = "^1" cc = "^1"
[dependencies] [dependencies]
libretro-sys = "^0.1" libretro-sys = "0.1"
failure = "^0.1" libloading = "0.5"
libloading = "^0.5" num_enum = "0.4"
num_enum = "^0.4" ffmpeg-next = { version = "4.3.8", optional = true }
sdl2 = { version = "0.32", optional = true }
crossbeam-channel = { version = "0.4", optional = true }
[dev-dependencies] [dev-dependencies]
# example: sdl2_emulator # example: sdl2_emulator
sdl2 = "^0.32" sdl2 = "0.32"
crossbeam-channel = "^0.4" crossbeam-channel = "0.4"
structopt = "^0.3" structopt = "0.3"
# example: ffmpeg_recorder # example: ffmpeg_recorder
ffmpeg-next = "4.3.8" ffmpeg-next = "4.3.8"
[features]
ffmpeg_comp = ["ffmpeg-next"]
sdl2_comp = ["sdl2", "crossbeam-channel"]

View File

@ -7,7 +7,6 @@ use std::path::{Path, PathBuf};
use std::pin::Pin; use std::pin::Pin;
use structopt::StructOpt; use structopt::StructOpt;
use failure::Fallible;
use ferretro::retro; use ferretro::retro;
use ferretro::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo, SystemInfo}; use ferretro::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo, SystemInfo};
@ -383,7 +382,7 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
self.octx.write_trailer().unwrap(); self.octx.write_trailer().unwrap();
} }
pub fn unserialize(&mut self, state: impl AsRef<Path>) -> Fallible<()> { pub fn unserialize(&mut self, state: impl AsRef<Path>) -> Result<(), Box<dyn std::error::Error>> {
let path = state.as_ref(); let path = state.as_ref();
let mut v = Vec::new(); let mut v = Vec::new();
if let Ok(mut f) = std::fs::File::open(path) { if let Ok(mut f) = std::fs::File::open(path) {
@ -391,7 +390,7 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
return self.retro.unserialize(v.as_ref()); return self.retro.unserialize(v.as_ref());
} }
} }
Err(failure::err_msg("Couldn't read file to unserialize")) Err("Couldn't read file to unserialize".into())
} }
} }
@ -452,9 +451,9 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
self.sys_path.clone() self.sys_path.clone()
} }
fn set_pixel_format(&mut self, format: PixelFormat) -> bool { fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> {
if self.frame_properties_locked { if self.frame_properties_locked {
return true; return Some(true);
} }
self.video_pixel_format = match format { self.video_pixel_format = match format {
@ -463,12 +462,12 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
PixelFormat::RGB565 => format::Pixel::RGB565, PixelFormat::RGB565 => format::Pixel::RGB565,
}; };
self.video_filter = video_filter(&self.video_encoder, &self.av_info, format).unwrap(); self.video_filter = video_filter(&self.video_encoder, &self.av_info, format).unwrap();
true Some(true)
} }
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> bool { fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> Option<bool> {
if self.frame_properties_locked { if self.frame_properties_locked {
return true; return Some(true);
} }
//self.video_encoder.set_frame_rate(system_av_info.timing.fps.into()); //self.video_encoder.set_frame_rate(system_av_info.timing.fps.into());
@ -479,12 +478,12 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
} }
self.av_info.timing = system_av_info.timing.clone(); self.av_info.timing = system_av_info.timing.clone();
self.set_geometry(&system_av_info.geometry); self.set_geometry(&system_av_info.geometry);
true Some(true)
} }
fn set_geometry(&mut self, geometry: &GameGeometry) -> bool { fn set_geometry(&mut self, geometry: &GameGeometry) -> Option<bool> {
if self.frame_properties_locked { if self.frame_properties_locked {
return true; return Some(true);
} }
self.video_encoder.set_width(geometry.base_width); self.video_encoder.set_width(geometry.base_width);
@ -498,7 +497,7 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
_ => unimplemented!(), _ => unimplemented!(),
}; };
self.video_filter = video_filter(&self.video_encoder, &self.av_info, pixel_format).unwrap(); self.video_filter = video_filter(&self.video_encoder, &self.av_info, pixel_format).unwrap();
true Some(true)
} }
@ -511,11 +510,11 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
} }
} }
fn set_variables(&mut self, variables: &Vec<Variable2>) -> bool { fn set_variables(&mut self, variables: &Vec<Variable2>) -> Option<bool> {
for v in variables { for v in variables {
eprintln!("{:?}", v); eprintln!("{:?}", v);
} }
true Some(true)
} }
fn log_print(&mut self, level: retro::ffi::LogLevel, msg: &str) { fn log_print(&mut self, level: retro::ffi::LogLevel, msg: &str) {
@ -542,7 +541,7 @@ struct Opt {
system: Option<PathBuf>, system: Option<PathBuf>,
} }
fn main() -> Fallible<()> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let opt: Opt = Opt::from_args(); let opt: Opt = Opt::from_args();
ffmpeg::log::set_level(ffmpeg::log::Level::Trace); ffmpeg::log::set_level(ffmpeg::log::Level::Trace);
ffmpeg::init().unwrap(); ffmpeg::init().unwrap();
@ -562,7 +561,7 @@ fn main() -> Fallible<()> {
emu.frame_properties_locked = true; emu.frame_properties_locked = true;
if let Some(state) = opt.state { if let Some(state) = opt.state {
emu.unserialize(state).unwrap(); emu.unserialize(state)?;
} }
//for frame in 0..60*10 { //for frame in 0..60*10 {
@ -572,7 +571,7 @@ fn main() -> Fallible<()> {
} }
let mut packet = Packet::empty(); let mut packet = Packet::empty();
eprintln!("flushed: {:?}", emu.video_encoder.flush(&mut packet).unwrap()); eprintln!("flushed: {:?}", emu.video_encoder.flush(&mut packet)?);
emu.end(); emu.end();
//octx.write_trailer().unwrap(); //octx.write_trailer().unwrap();

View File

@ -0,0 +1,66 @@
extern crate crossbeam_channel;
extern crate ferretro;
extern crate ffmpeg_next as ffmpeg;
extern crate sdl2;
use std::path::PathBuf;
use structopt::StructOpt;
use ferretro::prelude::*;
use ferretro::components::provided::{
ffmpeg::FfmpegComponent,
sdl2::Sdl2Component,
stdlib::{PathBufComponent, StderrLogComponent},
};
use ferretro::components::ControlFlow;
use ferretro::components::provided::stdlib::StderrSysInfoLogComponent;
#[derive(StructOpt)]
struct Opt {
/// Core module to use.
#[structopt(short, long, parse(from_os_str))]
core: PathBuf,
/// ROM to load using the core.
#[structopt(short, long, parse(from_os_str))]
rom: PathBuf,
/// Save state to load at startup.
#[structopt(long, parse(from_os_str))]
state: Option<PathBuf>,
/// System directory, often containing BIOS files
#[structopt(short, long, parse(from_os_str))]
system: Option<PathBuf>,
/// Recorded video to write.
#[structopt(short, long, parse(from_os_str))]
video: Option<PathBuf>,
}
pub fn main() {
let opt: Opt = Opt::from_args();
let mut emu = RetroComponentBase::new(&opt.core);
let sdl2_comp = Sdl2Component::new(emu.libretro_core());
emu.register_component(sdl2_comp);
emu.register_component(StderrLogComponent::default());
emu.register_component(StderrSysInfoLogComponent::default());
emu.register_component(PathBufComponent {
sys_path: opt.system.clone(),
libretro_path: Some(opt.core.to_path_buf()),
core_assets_path: None,
save_path: Some(std::env::temp_dir()),
});
if let Some(video) = opt.video {
ffmpeg::log::set_level(ffmpeg::log::Level::Info);
ffmpeg::init().unwrap();
let ffmpeg_comp = FfmpegComponent::new(emu.libretro_core(), video);
emu.register_component(ffmpeg_comp);
}
emu.load_game(&opt.rom).unwrap();
if let Some(state) = opt.state {
emu.unserialize_path(state).unwrap();
}
let mut frame = 0;
while let ControlFlow::Continue = emu.run() {
frame += 1;
}
eprintln!("Ran for {} frames.", frame);
}

View File

@ -135,7 +135,7 @@ impl MyEmulator {
pin_emu pin_emu
} }
pub fn run(&mut self) { pub fn run_loop(&mut self) {
self.audio_device.resume(); self.audio_device.resume();
let mut event_pump = self.sdl_context.event_pump().unwrap(); let mut event_pump = self.sdl_context.event_pump().unwrap();
'running: loop { 'running: loop {
@ -268,13 +268,13 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
self.sys_path.clone() self.sys_path.clone()
} }
fn set_pixel_format(&mut self, pix_fmt: retro::ffi::PixelFormat) -> bool { fn set_pixel_format(&mut self, pix_fmt: retro::ffi::PixelFormat) -> Option<bool> {
self.pixel_format = match pix_fmt { self.pixel_format = match pix_fmt {
retro::ffi::PixelFormat::ARGB1555 => sdl2::pixels::PixelFormatEnum::RGB555, retro::ffi::PixelFormat::ARGB1555 => sdl2::pixels::PixelFormatEnum::RGB555,
retro::ffi::PixelFormat::ARGB8888 => sdl2::pixels::PixelFormatEnum::ARGB8888, retro::ffi::PixelFormat::ARGB8888 => sdl2::pixels::PixelFormatEnum::ARGB8888,
retro::ffi::PixelFormat::RGB565 => sdl2::pixels::PixelFormatEnum::RGB565, retro::ffi::PixelFormat::RGB565 => sdl2::pixels::PixelFormatEnum::RGB565,
}; };
true Some(true)
} }
fn get_variable(&mut self, key: &str) -> Option<String> { fn get_variable(&mut self, key: &str) -> Option<String> {
match key { match key {
@ -285,11 +285,11 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
} }
} }
fn set_variables(&mut self, variables: &Vec<Variable2>) -> bool { fn set_variables(&mut self, variables: &Vec<Variable2>) -> Option<bool> {
for v in variables { for v in variables {
eprintln!("{:?}", v); eprintln!("{:?}", v);
} }
true Some(true)
} }
fn get_libretro_path(&mut self) -> Option<PathBuf> { fn get_libretro_path(&mut self) -> Option<PathBuf> {
@ -305,18 +305,18 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
Some(std::env::temp_dir()) Some(std::env::temp_dir())
} }
fn set_system_av_info(&mut self, av_info: &SystemAvInfo) -> bool { fn set_system_av_info(&mut self, av_info: &SystemAvInfo) -> Option<bool> {
self.set_geometry(&av_info.geometry); self.set_geometry(&av_info.geometry);
self.av_info = av_info.clone(); self.av_info = av_info.clone();
true Some(true)
} }
fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> bool { fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> Option<bool> {
println!("subsystem info: {:?}", subsystem_info); println!("subsystem info: {:?}", subsystem_info);
true Some(true)
} }
fn set_controller_info(&mut self, controller_info: &Vec<ControllerDescription2>) -> bool { fn set_controller_info(&mut self, controller_info: &Vec<ControllerDescription2>) -> Option<bool> {
for ci in controller_info { for ci in controller_info {
// so we can have analog support in beetle/mednafen saturn // so we can have analog support in beetle/mednafen saturn
if ci.name.as_str() == "3D Control Pad" { if ci.name.as_str() == "3D Control Pad" {
@ -324,21 +324,21 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
break; break;
} }
} }
true Some(true)
} }
fn set_input_descriptors(&mut self, descriptors: &Vec<InputDescriptor2>) -> bool { fn set_input_descriptors(&mut self, descriptors: &Vec<InputDescriptor2>) -> Option<bool> {
for id in descriptors { for id in descriptors {
println!("{:?}", id); println!("{:?}", id);
} }
true Some(true)
} }
fn set_geometry(&mut self, geom: &GameGeometry) -> bool { fn set_geometry(&mut self, geom: &GameGeometry) -> Option<bool> {
let _ = self.canvas.window_mut().set_size(geom.base_width, geom.base_height); let _ = self.canvas.window_mut().set_size(geom.base_width, geom.base_height);
let _ = self.canvas.set_logical_size(geom.base_width, geom.base_height); let _ = self.canvas.set_logical_size(geom.base_width, geom.base_height);
self.av_info.geometry = geom.clone(); self.av_info.geometry = geom.clone();
true Some(true)
} }
fn log_print(&mut self, level: retro::ffi::LogLevel, msg: &str) { fn log_print(&mut self, level: retro::ffi::LogLevel, msg: &str) {
@ -363,13 +363,11 @@ impl AudioCallback for MySdlAudio {
} }
} }
pub fn main() -> failure::Fallible<()> { pub fn main() {
let opt: Opt = Opt::from_args(); let opt: Opt = Opt::from_args();
let mut emu = MyEmulator::new(&opt.core, &opt.system); let mut emu = MyEmulator::new(&opt.core, &opt.system);
emu.load_game(&opt.rom); emu.load_game(&opt.rom);
emu.run(); emu.run_loop();
Ok(())
} }
#[derive(StructOpt)] #[derive(StructOpt)]

View File

@ -1,17 +1,53 @@
//pub mod ffmpeg; pub mod provided;
use crate::prelude::*; use crate::prelude::*;
use crate::retro::ffi::*; use crate::retro::ffi::*;
use std::os::raw::c_uint; use std::os::raw::c_uint;
use std::path::{PathBuf, Path}; use std::path::{PathBuf, Path};
use std::pin::Pin; use std::pin::Pin;
use std::io::Read;
pub struct RetroComponentBase { pub struct RetroComponentBase {
retro: LibretroWrapper, retro: LibretroWrapper,
pub components: Vec<Box<dyn RetroCallbacks>>, libretro_path: PathBuf,
// TODO: control when things get added to this.
// probably shouldn't be after load_game?
// unless we invent a way to play back the latest set_pixel_format etc. metadata required.
components: Vec<Box<dyn RetroComponent>>,
// replaying env calls for late-added components
cached_rom_path: Option<PathBuf>,
cached_pixel_format: Option<PixelFormat>,
cached_input_descriptors: Option<Vec<InputDescriptor2>>,
cached_hw_render_callback: Option<HwRenderCallback>,
cached_variables: Option<Vec<Variable2>>,
cached_support_no_game: Option<bool>,
cached_system_av_info: Option<SystemAvInfo>,
cached_subsystem_info: Option<Vec<SubsystemInfo2>>,
cached_controller_info: Option<Vec<ControllerDescription2>>,
cached_memory_map: Option<MemoryMap>,
cached_geometry: Option<GameGeometry>,
}
// TODO: replace with std::ops::ControlFlow when it becomes stable
pub enum ControlFlow {
Continue,
Break,
}
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[rustfmt::skip]
#[allow(unused_variables)]
pub trait RetroComponent: RetroCallbacks {
fn pre_run(&mut self, retro: &mut LibretroWrapper) -> ControlFlow { ControlFlow::Continue }
fn post_run(&mut self, retro: &mut LibretroWrapper) -> ControlFlow { ControlFlow::Continue }
fn pre_load_game(&mut self, retro: &mut LibretroWrapper, rom: &Path) -> Result<()> { Ok(()) }
fn post_load_game(&mut self, retro: &mut LibretroWrapper, rom: &Path) -> Result<()> { Ok(()) }
} }
impl RetroComponentBase { impl RetroComponentBase {
// TODO: constructor & wrapper that uses a statically linked libretro?
pub fn new(core_path: impl AsRef<Path>) -> Pin<Box<Self>> { pub fn new(core_path: impl AsRef<Path>) -> Pin<Box<Self>> {
let lib = libloading::Library::new(core_path.as_ref()).unwrap(); let lib = libloading::Library::new(core_path.as_ref()).unwrap();
let raw_retro = crate::retro::loading::LibretroApi::from_library(lib).unwrap(); let raw_retro = crate::retro::loading::LibretroApi::from_library(lib).unwrap();
@ -19,7 +55,19 @@ impl RetroComponentBase {
let emu = RetroComponentBase { let emu = RetroComponentBase {
retro, retro,
libretro_path: core_path.as_ref().to_path_buf(),
components: Vec::new(), components: Vec::new(),
cached_rom_path: None,
cached_pixel_format: None,
cached_input_descriptors: None,
cached_hw_render_callback: None,
cached_variables: None,
cached_support_no_game: None,
cached_system_av_info: None,
cached_subsystem_info: None,
cached_controller_info: None,
cached_memory_map: None,
cached_geometry: None
}; };
let mut pin_emu = Box::pin(emu); let mut pin_emu = Box::pin(emu);
@ -27,6 +75,109 @@ impl RetroComponentBase {
pin_emu.retro.init(); pin_emu.retro.init();
pin_emu pin_emu
} }
pub fn register_component<T>(&mut self, comp: T) -> Option<()> // TODO: Result
where T: RetroComponent
{
// TODO: match comp.schedule { BeforeInit, BeforeLoad, BeforeFirstRun, Anytime }
let mut comp = Box::new(comp);
if let Some(cached) = &self.cached_pixel_format {
if let Some(false) = comp.set_pixel_format(*cached) {
// TODO: error, and propagate this pattern downward
}
}
if let Some(cached) = &self.cached_input_descriptors {
comp.set_input_descriptors(cached);
}
if let Some(cached) = &self.cached_hw_render_callback {
comp.set_hw_render(cached);
}
if let Some(cached) = &self.cached_variables {
comp.set_variables(cached);
}
if let Some(cached) = &self.cached_support_no_game {
comp.set_support_no_game(*cached);
}
if let Some(cached) = &self.cached_system_av_info {
comp.set_system_av_info(cached);
}
if let Some(cached) = &self.cached_subsystem_info {
comp.set_subsystem_info(cached);
}
if let Some(cached) = &self.cached_controller_info {
comp.set_controller_info(cached);
}
if let Some(cached) = &self.cached_memory_map {
comp.set_memory_maps(cached);
}
if let Some(cached) = &self.cached_geometry {
comp.set_geometry(cached);
}
if let Some(cached) = &self.cached_rom_path {
comp.post_load_game(&mut self.retro, &cached);
}
self.components.push(comp);
Some(())
}
pub fn load_game(&mut self, rom: impl AsRef<Path>) -> Result<()> {
let path = rom.as_ref();
self.cached_rom_path = Some(path.to_path_buf());
for comp in &mut self.components {
comp.pre_load_game(&mut self.retro, path)?;
}
let mut data = None;
let mut v = Vec::new();
if !self.retro.get_system_info().need_fullpath {
if let Ok(mut f) = std::fs::File::open(path) {
if f.read_to_end(&mut v).is_ok() {
data = Some(v.as_ref());
}
}
}
self.retro.load_game(Some(path), data, None)?;
for comp in &mut self.components {
comp.post_load_game(&mut self.retro, path)?;
}
Ok(())
}
pub fn run(&mut self) -> ControlFlow {
for comp in &mut self.components {
if let ControlFlow::Break = comp.pre_run(&mut self.retro) {
return ControlFlow::Break;
}
}
self.retro.run();
for comp in &mut self.components {
if let ControlFlow::Break = comp.post_run(&mut self.retro) {
return ControlFlow::Break;
}
}
ControlFlow::Continue
}
pub fn unserialize_path(&mut self, state: impl AsRef<Path>) -> Result<()> {
let path = state.as_ref();
let mut v = Vec::new();
if let Ok(mut f) = std::fs::File::open(path) {
if f.read_to_end(&mut v).is_ok(){
return self.unserialize_buf(v);
}
}
Err("Couldn't read file to unserialize".into())
}
pub fn unserialize_buf(&mut self, data: impl AsRef<[u8]>) -> Result<()> {
self.retro.unserialize(data.as_ref())
}
} }
impl LibretroWrapperAccess for RetroComponentBase { impl LibretroWrapperAccess for RetroComponentBase {
@ -76,10 +227,12 @@ impl RetroCallbacks for RetroComponentBase {
.unwrap_or_default() .unwrap_or_default()
} }
fn set_rotation(&mut self, rotation: EnvRotation) -> bool { fn set_rotation(&mut self, rotation: EnvRotation) -> Option<bool> {
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_rotation(rotation)) .map(|comp| comp.set_rotation(rotation))
.flatten()
.fold(false, |x, y| x || y) // not "any" because we don't short-circuit .fold(false, |x, y| x || y) // not "any" because we don't short-circuit
.into()
} }
fn get_overscan(&mut self) -> Option<bool> { fn get_overscan(&mut self) -> Option<bool> {
@ -92,22 +245,28 @@ impl RetroCallbacks for RetroComponentBase {
}) })
} }
fn set_message(&mut self, message: &Message) -> bool { fn set_message(&mut self, message: &Message) -> Option<bool> {
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_message(message)) .map(|comp| comp.set_message(message))
.flatten()
.fold(false, |x, y| x || y) // not "any" because we don't short-circuit .fold(false, |x, y| x || y) // not "any" because we don't short-circuit
.into()
} }
fn shutdown(&mut self) -> bool { fn shutdown(&mut self) -> Option<bool> {
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.shutdown()) .map(|comp| comp.shutdown())
.flatten()
.all(|x| x) .all(|x| x)
.into()
} }
fn set_performance_level(&mut self, level: c_uint) -> bool { fn set_performance_level(&mut self, level: c_uint) -> Option<bool> {
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_performance_level(level)) .map(|comp| comp.set_performance_level(level))
.flatten()
.all(|x| x) .all(|x| x)
.into()
} }
fn get_system_directory(&mut self) -> Option<PathBuf> { fn get_system_directory(&mut self) -> Option<PathBuf> {
@ -117,22 +276,31 @@ impl RetroCallbacks for RetroComponentBase {
.next() .next()
} }
fn set_pixel_format(&mut self, format: PixelFormat) -> bool { fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> {
self.cached_pixel_format = Some(format);
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_pixel_format(format)) .map(|comp| comp.set_pixel_format(format))
.flatten()
.all(|x| x) .all(|x| x)
.into()
} }
fn set_input_descriptors(&mut self, input_descriptors: &Vec<InputDescriptor2>) -> bool { fn set_input_descriptors(&mut self, input_descriptors: &Vec<InputDescriptor2>) -> Option<bool> {
self.cached_input_descriptors = Some(input_descriptors.to_vec());
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_input_descriptors(input_descriptors)) .map(|comp| comp.set_input_descriptors(input_descriptors))
.flatten()
.fold(false, |x, y| x || y) .fold(false, |x, y| x || y)
.into()
} }
fn set_hw_render(&mut self, hw_render_callback: &HwRenderCallback) -> bool { fn set_hw_render(&mut self, hw_render_callback: &HwRenderCallback) -> Option<bool> {
self.cached_hw_render_callback = Some(hw_render_callback.to_owned());
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_hw_render(hw_render_callback)) .map(|comp| comp.set_hw_render(hw_render_callback))
.flatten()
.all(|x| x) .all(|x| x)
.into()
} }
fn get_variable(&mut self, key: &str) -> Option<String> { fn get_variable(&mut self, key: &str) -> Option<String> {
@ -142,10 +310,13 @@ impl RetroCallbacks for RetroComponentBase {
.next() .next()
} }
fn set_variables(&mut self, variables: &Vec<Variable2>) -> bool { fn set_variables(&mut self, variables: &Vec<Variable2>) -> Option<bool> {
self.cached_variables = Some(variables.to_vec());
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_variables(variables)) .map(|comp| comp.set_variables(variables))
.flatten()
.fold(false, |x, y| x || y) .fold(false, |x, y| x || y)
.into()
} }
fn get_variable_update(&mut self) -> Option<bool> { fn get_variable_update(&mut self) -> Option<bool> {
@ -155,17 +326,23 @@ impl RetroCallbacks for RetroComponentBase {
.reduce(|x, y| x || y) .reduce(|x, y| x || y)
} }
fn set_support_no_game(&mut self, supports_no_game: bool) -> bool { fn set_support_no_game(&mut self, supports_no_game: bool) -> Option<bool> {
self.cached_support_no_game = Some(supports_no_game);
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_support_no_game(supports_no_game)) .map(|comp| comp.set_support_no_game(supports_no_game))
.flatten()
.all(|x| x) .all(|x| x)
.into()
} }
// allow it to be overridden, but we *do* have the answer at this level since we loaded it.
fn get_libretro_path(&mut self) -> Option<PathBuf> { fn get_libretro_path(&mut self) -> Option<PathBuf> {
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.get_libretro_path()) .map(|comp| comp.get_libretro_path())
.flatten() .flatten()
.next() .next()
.unwrap_or_else(|| self.libretro_path.clone())
.into()
} }
fn get_input_device_capabilities(&mut self) -> Option<u64> { fn get_input_device_capabilities(&mut self) -> Option<u64> {
@ -189,34 +366,50 @@ impl RetroCallbacks for RetroComponentBase {
.next() .next()
} }
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> bool { fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> Option<bool> {
self.cached_system_av_info = Some(system_av_info.to_owned());
self.cached_geometry = Some(system_av_info.geometry.clone());
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_system_av_info(system_av_info)) .map(|comp| comp.set_system_av_info(system_av_info))
.flatten()
.fold(false, |x, y| x || y) // not "any" because we don't short-circuit .fold(false, |x, y| x || y) // not "any" because we don't short-circuit
.into()
} }
fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> bool { fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> Option<bool> {
self.cached_subsystem_info = Some(subsystem_info.to_vec());
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_subsystem_info(subsystem_info)) .map(|comp| comp.set_subsystem_info(subsystem_info))
.flatten()
.all(|x| x) .all(|x| x)
.into()
} }
fn set_controller_info(&mut self, controller_info: &Vec<ControllerDescription2>) -> bool { fn set_controller_info(&mut self, controller_info: &Vec<ControllerDescription2>) -> Option<bool> {
self.cached_controller_info = Some(controller_info.to_vec());
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_controller_info(controller_info)) .map(|comp| comp.set_controller_info(controller_info))
.flatten()
.all(|x| x) .all(|x| x)
.into()
} }
fn set_memory_maps(&mut self, memory_map: &MemoryMap) -> bool { fn set_memory_maps(&mut self, memory_map: &MemoryMap) -> Option<bool> {
self.cached_memory_map = Some(memory_map.to_owned());
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_memory_maps(memory_map)) .map(|comp| comp.set_memory_maps(memory_map))
.flatten()
.all(|x| x) .all(|x| x)
.into()
} }
fn set_geometry(&mut self, game_geometry: &GameGeometry) -> bool { fn set_geometry(&mut self, game_geometry: &GameGeometry) -> Option<bool> {
self.cached_geometry = Some(game_geometry.to_owned());
self.components.iter_mut() self.components.iter_mut()
.map(|comp| comp.set_geometry(game_geometry)) .map(|comp| comp.set_geometry(game_geometry))
.flatten()
.fold(false, |x, y| x || y) // not "any" because we don't short-circuit .fold(false, |x, y| x || y) // not "any" because we don't short-circuit
.into()
} }
fn get_username(&mut self) -> Option<String> { fn get_username(&mut self) -> Option<String> {
@ -301,3 +494,9 @@ impl RetroCallbacks for RetroComponentBase {
.unwrap_or_default() .unwrap_or_default()
} }
} }
impl Drop for RetroComponentBase {
fn drop(&mut self) {
crate::retro::wrapper::unset_handler();
}
}

View File

@ -1,21 +1,21 @@
extern crate ffmpeg_next as ffmpeg; extern crate ffmpeg_next as ffmpeg;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::io::Read; use std::error::Error;
use std::path::{Path, PathBuf}; use std::path::Path;
use std::pin::Pin;
use structopt::StructOpt;
use failure::Fallible;
use crate::prelude::*; use crate::prelude::*;
use ffmpeg::{ChannelLayout, Packet, codec, filter, format, frame, media}; use ffmpeg::{ChannelLayout, Packet, filter, format, frame, media};
use ffmpeg::util::rational::Rational; use ffmpeg::util::rational::Rational;
use crate::components::ControlFlow;
struct MyEmulator { enum EncoderToWriteFrom {
retro: LibretroWrapper, Video,
sys_info: SystemInfo, Audio,
}
pub struct FfmpegComponent {
av_info: SystemAvInfo, av_info: SystemAvInfo,
audio_buf: Vec<(i16, i16)>, audio_buf: Vec<(i16, i16)>,
video_pixel_format: format::Pixel, video_pixel_format: format::Pixel,
@ -25,10 +25,9 @@ struct MyEmulator {
audio_encoder: ffmpeg::encoder::Audio, audio_encoder: ffmpeg::encoder::Audio,
video_filter: filter::Graph, video_filter: filter::Graph,
audio_filter: filter::Graph, audio_filter: filter::Graph,
sys_path: Option<PathBuf>,
frame_properties_locked: bool, frame_properties_locked: bool,
octx: ffmpeg::format::context::Output, octx: ffmpeg::format::context::Output,
frame: u64, frame: i64,
} }
fn video_filter( fn video_filter(
@ -117,18 +116,99 @@ fn audio_filter(
Ok(afilter) Ok(afilter)
} }
impl MyEmulator { impl RetroComponent for FfmpegComponent {
pub fn new( fn pre_run(&mut self, _retro: &mut LibretroWrapper) -> ControlFlow {
core_path: impl AsRef<Path>, self.frame += 1;
sys_path: &Option<impl AsRef<Path>>, ControlFlow::Continue
video_path: impl AsRef<Path>, }
mut octx: ffmpeg::format::context::Output,
) -> Pin<Box<Self>> { fn post_run(&mut self, _retro: &mut LibretroWrapper) -> ControlFlow {
let lib = libloading::Library::new(core_path.as_ref()).unwrap(); match self.video_frames.pop_front() {
let raw_retro = retro::loading::LibretroApi::from_library(lib).unwrap(); Some(mut vframe) => {
let retro = retro::wrapper::LibretroWrapper::from(raw_retro); vframe.set_pts(Some(self.frame));
eprintln!("🎞 queue frame pts {:?}", vframe.pts());
self.video_filter.get("in").unwrap().source().add(&vframe).unwrap();
let mut filtered_vframe = frame::Video::empty();
loop {
match self.video_filter.get("out").unwrap().sink().frame(&mut filtered_vframe) {
Ok(..) => {
eprintln!("🎥 Got filtered video frame {}x{} pts {:?}", filtered_vframe.width(), filtered_vframe.height(), filtered_vframe.pts());
if self.video_filter.get("in").unwrap().source().failed_requests() > 0 {
println!("🎥 failed to put filter input frame");
}
//filtered_vframe.set_pts(Some(frame));
self.video_encoder.send_frame(&filtered_vframe).unwrap();
self.receive_and_write_packets(EncoderToWriteFrom::Video);
},
Err(e) => {
eprintln!("Error getting filtered video frame: {:?}", e);
break;
}
}
}
let mut aframe = frame::Audio::new(
format::Sample::I16(format::sample::Type::Packed),
self.audio_buf.len(),
ChannelLayout::STEREO
);
if aframe.planes() > 0 {
aframe.set_channels(2);
aframe.set_rate(44100);
aframe.set_pts(Some(self.frame));
let aplane: &mut [(i16, i16)] = aframe.plane_mut(0);
eprintln!("Audio buffer length {} -> {}", self.audio_buf.len(), aplane.len());
aplane.copy_from_slice(self.audio_buf.as_ref());
//eprintln!("src: {:?}, dest: {:?}", self.audio_buf, aplane);
self.audio_buf.clear();
eprintln!("frame audio: {:?}", aframe);
eprintln!("🎞 queue frame pts {:?}", aframe.pts());
self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap();
let mut filtered_aframe = frame::Audio::empty();
loop {
match self.audio_filter.get("out").unwrap().sink().frame(&mut filtered_aframe) {
Ok(..) => {
eprintln!("🔊 Got filtered audio frame {:?} pts {:?}", filtered_aframe, filtered_aframe.pts());
if self.audio_filter.get("in").unwrap().source().failed_requests() > 0 {
println!("🎥 failed to put filter input frame");
}
//let faplane: &[f32] = filtered_aframe.plane(0);
//filtered_aframe.set_pts(Some(frame));
self.audio_encoder.send_frame(&filtered_aframe).unwrap();
self.receive_and_write_packets(EncoderToWriteFrom::Audio);
},
Err(e) => {
eprintln!("Error getting filtered audio frame: {:?}", e);
break;
}
}
}
}
},
None => println!("Video not ready during frame {}", self.frame)
}
ControlFlow::Continue
}
fn post_load_game(&mut self, _retro: &mut LibretroWrapper, _rom: &Path) -> Result<(), Box<dyn Error>> {
self.frame_properties_locked = true;
Ok(())
}
}
impl FfmpegComponent {
pub fn new(
retro: &LibretroWrapper,
video_path: impl AsRef<Path>,
) -> Self {
let mut octx = format::output(&video_path).unwrap();
let sys_info = retro.get_system_info();
let mut av_info = retro.get_system_av_info(); let mut av_info = retro.get_system_av_info();
let fps_int = av_info.timing.fps.round() as i32; let fps_int = av_info.timing.fps.round() as i32;
@ -200,7 +280,7 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
audio_encoder.set_time_base(Rational::new(1, 60)); audio_encoder.set_time_base(Rational::new(1, 60));
audio_output.set_time_base(Rational::new(1, 60)); audio_output.set_time_base(Rational::new(1, 60));
let mut audio_encoder = audio_encoder.open_as(acodec).unwrap(); let audio_encoder = audio_encoder.open_as(acodec).unwrap();
//audio_output.set_parameters(&audio_encoder); //audio_output.set_parameters(&audio_encoder);
let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap(); let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap();
@ -210,9 +290,7 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
octx.write_header().unwrap(); octx.write_header().unwrap();
ffmpeg::format::context::output::dump(&octx, 0, None); ffmpeg::format::context::output::dump(&octx, 0, None);
let emu = MyEmulator { let mut comp = FfmpegComponent {
retro,
sys_info,
av_info: av_info.clone(), av_info: av_info.clone(),
audio_buf: Default::default(), audio_buf: Default::default(),
video_pixel_format: format::Pixel::RGB555, video_pixel_format: format::Pixel::RGB555,
@ -222,17 +300,12 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
audio_encoder, audio_encoder,
video_filter, video_filter,
audio_filter, audio_filter,
sys_path: sys_path.as_ref().map(|x| x.as_ref().to_path_buf()),
frame_properties_locked: false, frame_properties_locked: false,
octx, octx,
frame: 0 frame: 0
}; };
comp.set_system_av_info(&av_info);
let mut pin_emu = Box::pin(emu); comp
retro::wrapper::set_handler(pin_emu.as_mut());
pin_emu.retro.init();
pin_emu.set_system_av_info(&av_info);
pin_emu
} }
fn receive_and_write_packets(&mut self, encoder: EncoderToWriteFrom) fn receive_and_write_packets(&mut self, encoder: EncoderToWriteFrom)
@ -275,128 +348,25 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
} }
} }
pub fn run(&mut self, frame: i64) {
self.frame += 1;
self.retro.run();
match self.video_frames.pop_front() {
Some(mut vframe) => {
vframe.set_pts(Some(frame));
eprintln!("🎞 queue frame pts {:?}", vframe.pts());
self.video_filter.get("in").unwrap().source().add(&vframe).unwrap();
let mut filtered_vframe = frame::Video::empty();
loop {
match self.video_filter.get("out").unwrap().sink().frame(&mut filtered_vframe) {
Ok(..) => {
eprintln!("🎥 Got filtered video frame {}x{} pts {:?}", filtered_vframe.width(), filtered_vframe.height(), filtered_vframe.pts());
if self.video_filter.get("in").unwrap().source().failed_requests() > 0 {
println!("🎥 failed to put filter input frame");
}
//filtered_vframe.set_pts(Some(frame));
self.video_encoder.send_frame(&filtered_vframe).unwrap();
self.receive_and_write_packets(EncoderToWriteFrom::Video);
},
Err(e) => {
eprintln!("Error getting filtered video frame: {:?}", e);
break;
}
}
}
let mut aframe = frame::Audio::new(
format::Sample::I16(format::sample::Type::Packed),
self.audio_buf.len(),
ChannelLayout::STEREO
);
if aframe.planes() > 0 {
aframe.set_channels(2);
aframe.set_rate(44100);
aframe.set_pts(Some(frame));
let aplane: &mut [(i16, i16)] = aframe.plane_mut(0);
eprintln!("Audio buffer length {} -> {}", self.audio_buf.len(), aplane.len());
aplane.copy_from_slice(self.audio_buf.as_ref());
//eprintln!("src: {:?}, dest: {:?}", self.audio_buf, aplane);
self.audio_buf.clear();
eprintln!("frame audio: {:?}", aframe);
eprintln!("🎞 queue frame pts {:?}", aframe.pts());
self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap();
let mut filtered_aframe = frame::Audio::empty();
loop {
match self.audio_filter.get("out").unwrap().sink().frame(&mut filtered_aframe) {
Ok(..) => {
eprintln!("🔊 Got filtered audio frame {:?} pts {:?}", filtered_aframe, filtered_aframe.pts());
if self.audio_filter.get("in").unwrap().source().failed_requests() > 0 {
println!("🎥 failed to put filter input frame");
}
//let faplane: &[f32] = filtered_aframe.plane(0);
//filtered_aframe.set_pts(Some(frame));
self.audio_encoder.send_frame(&filtered_aframe).unwrap();
self.receive_and_write_packets(EncoderToWriteFrom::Audio);
},
Err(e) => {
eprintln!("Error getting filtered audio frame: {:?}", e);
break;
}
}
}
}
},
None => println!("Video not ready during frame {}", self.frame)
}
}
pub fn load_game(&self, rom: impl AsRef<Path>) {
let path = rom.as_ref();
let mut data = None;
let mut v = Vec::new();
if !self.sys_info.need_fullpath {
if let Ok(mut f) = std::fs::File::open(path) {
if f.read_to_end(&mut v).is_ok() {
data = Some(v.as_ref());
}
}
}
self.retro
.load_game(Some(path), data, None)
.unwrap();
}
pub fn end(&mut self) { pub fn end(&mut self) {
self.video_encoder.send_eof(); let mut packet = Packet::empty();
eprintln!("flushed: {:?}", self.video_encoder.flush(&mut packet).unwrap());
self.video_encoder.send_eof().unwrap();
self.receive_and_write_packets(EncoderToWriteFrom::Video); self.receive_and_write_packets(EncoderToWriteFrom::Video);
self.audio_encoder.send_eof(); self.audio_encoder.send_eof().unwrap();
self.receive_and_write_packets(EncoderToWriteFrom::Audio); self.receive_and_write_packets(EncoderToWriteFrom::Audio);
self.octx.write_trailer().unwrap(); self.octx.write_trailer().unwrap();
} }
}
pub fn unserialize(&mut self, state: impl AsRef<Path>) -> Fallible<()> { impl Drop for FfmpegComponent {
let path = state.as_ref(); fn drop(&mut self) {
let mut v = Vec::new(); self.end();
if let Ok(mut f) = std::fs::File::open(path) {
if f.read_to_end(&mut v).is_ok(){
return self.retro.unserialize(v.as_ref());
}
}
Err(failure::err_msg("Couldn't read file to unserialize"))
} }
} }
impl retro::wrapper::LibretroWrapperAccess for MyEmulator { impl RetroCallbacks for FfmpegComponent {
fn libretro_core(&mut self) -> &mut LibretroWrapper {
&mut self.retro
}
}
impl retro::wrapper::RetroCallbacks for MyEmulator {
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) { fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
let mut vframe = frame::Video::new(self.video_pixel_format, width, height); let mut vframe = frame::Video::new(self.video_pixel_format, width, height);
@ -443,13 +413,9 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
stereo_pcm.len() stereo_pcm.len()
} }
fn get_system_directory(&mut self) -> Option<PathBuf> { fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> {
self.sys_path.clone()
}
fn set_pixel_format(&mut self, format: PixelFormat) -> bool {
if self.frame_properties_locked { if self.frame_properties_locked {
return true; return Some(false);
} }
self.video_pixel_format = match format { self.video_pixel_format = match format {
@ -458,12 +424,21 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
PixelFormat::RGB565 => format::Pixel::RGB565, PixelFormat::RGB565 => format::Pixel::RGB565,
}; };
self.video_filter = video_filter(&self.video_encoder, &self.av_info, format).unwrap(); self.video_filter = video_filter(&self.video_encoder, &self.av_info, format).unwrap();
true Some(true)
} }
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> bool { fn get_variable(&mut self, key: &str) -> Option<String> {
match key {
"beetle_saturn_analog_stick_deadzone" => Some("15%".to_string()),
"parallel-n64-gfxplugin" => Some("angrylion".to_string()),
"parallel-n64-astick-deadzone" => Some("15%".to_string()),
_ => None,
}
}
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> Option<bool> {
if self.frame_properties_locked { if self.frame_properties_locked {
return true; return Some(false);
} }
//self.video_encoder.set_frame_rate(system_av_info.timing.fps.into()); //self.video_encoder.set_frame_rate(system_av_info.timing.fps.into());
@ -474,12 +449,12 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
} }
self.av_info.timing = system_av_info.timing.clone(); self.av_info.timing = system_av_info.timing.clone();
self.set_geometry(&system_av_info.geometry); self.set_geometry(&system_av_info.geometry);
true Some(true)
} }
fn set_geometry(&mut self, geometry: &GameGeometry) -> bool { fn set_geometry(&mut self, geometry: &GameGeometry) -> Option<bool> {
if self.frame_properties_locked { if self.frame_properties_locked {
return true; return Some(false);
} }
self.video_encoder.set_width(geometry.base_width); self.video_encoder.set_width(geometry.base_width);
@ -493,27 +468,6 @@ impl retro::wrapper::RetroCallbacks for MyEmulator {
_ => unimplemented!(), _ => unimplemented!(),
}; };
self.video_filter = video_filter(&self.video_encoder, &self.av_info, pixel_format).unwrap(); self.video_filter = video_filter(&self.video_encoder, &self.av_info, pixel_format).unwrap();
true Some(true)
}
fn get_variable(&mut self, key: &str) -> Option<String> {
match key {
"beetle_saturn_analog_stick_deadzone" => Some("15%".to_string()),
"parallel-n64-gfxplugin" => Some("angrylion".to_string()),
"parallel-n64-astick-deadzone" => Some("15%".to_string()),
_ => None,
}
}
fn set_variables(&mut self, variables: &Vec<Variable2>) -> bool {
for v in variables {
eprintln!("{:?}", v);
}
true
}
fn log_print(&mut self, level: retro::ffi::LogLevel, msg: &str) {
eprint!("🕹️ [{:?}] {}", level, msg);
} }
} }

View File

@ -0,0 +1,7 @@
#[cfg(feature = "ffmpeg_comp")]
pub mod ffmpeg;
#[cfg(feature = "sdl2_comp")]
pub mod sdl2;
pub mod stdlib;

View File

@ -0,0 +1,323 @@
use std::error::Error;
use std::ffi::CStr;
use std::path::Path;
use std::time::{Duration, Instant};
use crate::prelude::*;
use crate::components::ControlFlow;
use sdl2::audio::{AudioCallback, AudioFormat, AudioSpec, AudioSpecDesired, AudioDevice};
use sdl2::controller::{GameController, Button, Axis};
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::rect::Rect;
use sdl2::render::WindowCanvas;
// TODO: split up between video/audio/input!
pub struct Sdl2Component {
preferred_pad: Option<u32>,
av_info: SystemAvInfo,
_sdl_context: sdl2::Sdl,
// video bits
canvas: WindowCanvas,
pixel_format: sdl2::pixels::PixelFormatEnum,
// audio bits
audio_buffer: Vec<i16>,
audio_spec: AudioSpec,
audio_device: AudioDevice<MySdlAudio>,
audio_sender: crossbeam_channel::Sender<Vec<i16>>,
// input bits
gamepad_subsys: sdl2::GameControllerSubsystem,
gamepads: Vec<GameController>,
// timing and events
frame_begin: Instant,
event_pump: sdl2::EventPump,
}
impl RetroComponent for Sdl2Component {
fn pre_run(&mut self, _retro: &mut LibretroWrapper) -> ControlFlow {
self.frame_begin = Instant::now();
for event in self.event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => return ControlFlow::Break,
_ => {}
}
}
ControlFlow::Continue
}
fn post_run(&mut self, _retro: &mut LibretroWrapper) -> ControlFlow {
// The rest of the game loop goes here...
self.canvas.present();
// similar hack to the sample rate, make sure we don't divide by zero.
let mut spf = 1.0 / self.av_info.timing.fps;
if spf.is_nan() || spf.is_infinite() {
spf = 1.0 / 60.0;
}
Duration::from_secs_f64(spf)
.checked_sub(self.frame_begin.elapsed())
.map(std::thread::sleep);
ControlFlow::Continue
}
fn post_load_game(&mut self, retro: &mut LibretroWrapper, _rom: &Path) -> Result<(), Box<dyn Error>> {
if let Some(device) = self.preferred_pad {
for port in 0..self.gamepads.len() as u32 {
retro.set_controller_port_device(port, device);
}
}
self.audio_device.resume();
Ok(())
}
}
impl Sdl2Component {
pub fn new(retro: &LibretroWrapper) -> Self {
let sys_info = retro.get_system_info();
let title = format!(
"{} - ferretro",
unsafe { CStr::from_ptr(sys_info.library_name) }.to_string_lossy()
);
let mut av_info = retro.get_system_av_info();
let pixel_format = sdl2::pixels::PixelFormatEnum::ARGB1555;
// HACK: some cores don't report this 'til we get an environ call to set_system_av_info...
// which is too late for this constructor to pass along to SDL.
if av_info.timing.sample_rate == 0.0 {
av_info.timing.sample_rate = 32040.0;
}
let sdl_context = sdl2::init().unwrap();
let window = sdl_context
.video()
.unwrap()
.window(title.as_str(), av_info.geometry.base_width, av_info.geometry.base_height)
.opengl()
.build()
.unwrap();
let canvas = window.into_canvas().build().unwrap();
let (audio_sender, audio_receiver) = crossbeam_channel::bounded(2);
let audio = sdl_context.audio().unwrap();
let desired_spec = AudioSpecDesired {
freq: Some(av_info.timing.sample_rate.round() as i32),
channels: Some(2),
samples: None,
};
let mut audio_spec = None;
let audio_device = audio
.open_playback(None, &desired_spec, |spec| {
if spec.format != AudioFormat::S16LSB {
eprintln!("unsupported audio format {:?}", spec.format);
}
audio_spec = Some(spec.clone());
MySdlAudio {
audio_spec: spec,
audio_receiver,
}
})
.unwrap();
let gamepad_subsys = sdl_context.game_controller().unwrap();
let mut gamepads = Vec::new();
for i in 0..gamepad_subsys.num_joysticks().unwrap() {
gamepads.extend(gamepad_subsys.open(i).into_iter());
}
let event_pump = sdl_context.event_pump().unwrap();
Sdl2Component {
preferred_pad: None,
av_info,
_sdl_context: sdl_context,
canvas,
pixel_format,
audio_buffer: Default::default(),
audio_spec: audio_spec.unwrap(),
audio_device,
audio_sender,
gamepad_subsys,
gamepads,
frame_begin: Instant::now(),
event_pump,
}
}
fn send_audio_samples(&mut self) {
let stereo_samples = self.audio_spec.samples as usize * 2;
while self.audio_buffer.len() >= stereo_samples {
let remainder = self.audio_buffer.split_off(stereo_samples);
let msg = std::mem::replace(&mut self.audio_buffer, remainder);
let _ = self.audio_sender.try_send(msg);
}
}
}
impl RetroCallbacks for Sdl2Component {
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
let rect = Rect::new(0, 0, width, height);
if let Ok(mut tex) =
self.canvas
.texture_creator()
.create_texture_static(self.pixel_format, width, height)
{
if tex.update(rect, data, pitch as usize).is_ok() {
self.canvas.clear();
self.canvas.copy(&tex, None, None).unwrap();
}
}
}
fn audio_sample(&mut self, left: i16, right: i16) {
self.audio_buffer.push(left);
self.audio_buffer.push(right);
self.send_audio_samples()
}
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
self.audio_buffer.extend(stereo_pcm);
self.send_audio_samples();
stereo_pcm.len()
}
fn input_poll(&mut self) {
self.gamepad_subsys.update();
}
fn input_state(&mut self, port: u32, device: InputDeviceId, index: InputIndex) -> i16 {
match self.gamepads.get(port as usize) {
Some(gamepad) => {
match device {
InputDeviceId::Joypad(button) => {
match button_map(&button) {
Some(x) => gamepad.button(x) as i16,
None => match button {
JoypadButton::L2 => gamepad.axis(Axis::TriggerLeft),
JoypadButton::R2 => gamepad.axis(Axis::TriggerRight),
_ => 0,
}
}
}
InputDeviceId::Analog(axis) => gamepad.axis(axis_map(index, axis)),
_ => 0,
}
}
None => 0,
}
}
fn set_pixel_format(&mut self, pix_fmt: PixelFormat) -> Option<bool> {
self.pixel_format = match pix_fmt {
PixelFormat::ARGB1555 => sdl2::pixels::PixelFormatEnum::RGB555,
PixelFormat::ARGB8888 => sdl2::pixels::PixelFormatEnum::ARGB8888,
PixelFormat::RGB565 => sdl2::pixels::PixelFormatEnum::RGB565,
};
Some(true)
}
fn get_variable(&mut self, key: &str) -> Option<String> {
match key {
"beetle_saturn_analog_stick_deadzone" => Some("15%".to_string()),
"parallel-n64-gfxplugin" => Some("angrylion".to_string()),
"parallel-n64-astick-deadzone" => Some("15%".to_string()),
_ => None,
}
}
fn get_input_device_capabilities(&mut self) -> Option<u64> {
let bits = (1 << (DeviceType::Joypad as u32)) | (1 << (DeviceType::Analog as u32));
Some(bits as u64)
}
fn set_system_av_info(&mut self, av_info: &SystemAvInfo) -> Option<bool> {
self.set_geometry(&av_info.geometry);
self.av_info = av_info.clone();
Some(true)
}
fn set_controller_info(&mut self, controller_info: &Vec<ControllerDescription2>) -> Option<bool> {
for ci in controller_info {
// so we can have analog support in beetle/mednafen saturn
if ci.name.as_str() == "3D Control Pad" {
self.preferred_pad = Some(ci.device_id());
break;
}
}
Some(true)
}
fn set_geometry(&mut self, geom: &GameGeometry) -> Option<bool> {
let _ = self.canvas.window_mut().set_size(geom.base_width, geom.base_height);
let _ = self.canvas.set_logical_size(geom.base_width, geom.base_height);
self.av_info.geometry = geom.clone();
Some(true)
}
}
struct MySdlAudio {
audio_spec: AudioSpec,
audio_receiver: crossbeam_channel::Receiver<Vec<i16>>,
}
impl AudioCallback for MySdlAudio {
type Channel = i16;
fn callback(&mut self, out: &mut [Self::Channel]) {
if self.audio_spec.format == AudioFormat::S16LSB {
if let Ok(samples) = self.audio_receiver.recv() {
out.copy_from_slice(&samples[..out.len()]);
}
}
}
}
fn button_map(retro_button: &JoypadButton) -> Option<Button> {
match retro_button {
JoypadButton::B => Some(Button::A),
JoypadButton::Y => Some(Button::X),
JoypadButton::Select => Some(Button::Back),
JoypadButton::Start => Some(Button::Start),
JoypadButton::Up => Some(Button::DPadUp),
JoypadButton::Down => Some(Button::DPadDown),
JoypadButton::Left => Some(Button::DPadLeft),
JoypadButton::Right => Some(Button::DPadRight),
JoypadButton::A => Some(Button::B),
JoypadButton::X => Some(Button::Y),
JoypadButton::L => Some(Button::LeftShoulder),
JoypadButton::R => Some(Button::RightShoulder),
// SDL2 controller API doesn't have L2/R2 as buttons, they're considered axes
JoypadButton::L2 => None,
JoypadButton::R2 => None,
JoypadButton::L3 => Some(Button::LeftStick),
JoypadButton::R3 => Some(Button::RightStick),
}
}
fn axis_map(index: InputIndex, axis: AnalogAxis) -> Axis {
match (index, axis) {
(InputIndex::Left, AnalogAxis::X) => Axis::LeftX,
(InputIndex::Left, AnalogAxis::Y) => Axis::LeftY,
(InputIndex::Right, AnalogAxis::X) => Axis::RightX,
(InputIndex::Right, AnalogAxis::Y) => Axis::RightY,
}
}

View File

@ -0,0 +1,80 @@
use std::path::PathBuf;
use crate::prelude::*;
#[derive(Default)]
pub struct PathBufComponent {
pub sys_path: Option<PathBuf>,
pub libretro_path: Option<PathBuf>,
pub core_assets_path: Option<PathBuf>,
pub save_path: Option<PathBuf>,
}
impl RetroComponent for PathBufComponent {}
impl RetroCallbacks for PathBufComponent {
fn get_system_directory(&mut self) -> Option<PathBuf> {
self.sys_path.clone()
}
fn get_libretro_path(&mut self) -> Option<PathBuf> {
self.libretro_path.clone()
}
fn get_core_assets_directory(&mut self) -> Option<PathBuf> {
self.core_assets_path.clone()
}
fn get_save_directory(&mut self) -> Option<PathBuf> {
self.save_path.clone()
}
}
#[derive(Default)]
pub struct StderrLogComponent {
pub prefix: String,
}
impl RetroComponent for StderrLogComponent {}
impl RetroCallbacks for StderrLogComponent {
fn log_print(&mut self, level: crate::retro::ffi::LogLevel, msg: &str) {
eprint!("{}[{:?}] {}", self.prefix, level, msg);
}
}
#[derive(Default)]
pub struct StderrSysInfoLogComponent {
pub prefix: String,
}
impl RetroComponent for StderrSysInfoLogComponent {}
impl RetroCallbacks for StderrSysInfoLogComponent {
fn set_input_descriptors(&mut self, descriptors: &Vec<InputDescriptor2>) -> Option<bool> {
for id in descriptors {
eprintln!("{}{:?}", self.prefix, id);
}
Some(true)
}
fn set_variables(&mut self, variables: &Vec<Variable2>) -> Option<bool> {
for v in variables {
eprintln!("{}{:?}", self.prefix, v);
}
Some(true)
}
fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> Option<bool> {
for s in subsystem_info {
eprintln!("{}{:?}", self.prefix, s);
}
Some(true)
}
}
#[derive(Default)]
pub struct StderrCallTraceComponent {
pub prefix: String,
}
impl RetroComponent for StderrCallTraceComponent {}
impl RetroCallbacks for StderrCallTraceComponent {
fn audio_sample(&mut self, left: i16, right: i16) {
eprintln!("{}audio_sample({}, {})", self.prefix, left, right);
}
// TODO: etc...
}

View File

@ -1,4 +1,3 @@
extern crate failure;
extern crate libloading; extern crate libloading;
pub mod retro; pub mod retro;
@ -6,6 +5,7 @@ pub mod components;
pub mod prelude { pub mod prelude {
pub use crate::retro::constants::*; pub use crate::retro::constants::*;
pub use crate::components::{RetroComponent, RetroComponentBase};
pub use crate::retro::wrapped_types::*; pub use crate::retro::wrapped_types::*;
pub use crate::retro::wrapper::{RetroCallbacks, LibretroWrapper, LibretroWrapperAccess}; pub use crate::retro::wrapper::{RetroCallbacks, LibretroWrapper, LibretroWrapperAccess};
pub use crate::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo, SystemInfo}; pub use crate::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo, SystemInfo};

View File

@ -2,18 +2,19 @@ use std::ffi::CString;
use std::os::raw::{c_char, c_void}; use std::os::raw::{c_char, c_void};
use std::path::Path; use std::path::Path;
use failure::Fallible;
use libloading; use libloading;
use super::ffi::*; use super::ffi::*;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub struct LibretroApi { pub struct LibretroApi {
_lib: libloading::Library, // for tying our lifetime to its own _lib: libloading::Library, // for tying our lifetime to its own
pub core_api: CoreAPI, pub core_api: CoreAPI,
} }
impl LibretroApi { impl LibretroApi {
pub fn from_library(lib: libloading::Library) -> Fallible<Self> { pub fn from_library(lib: libloading::Library) -> Result<Self> {
unsafe { unsafe {
let core_api = CoreAPI { let core_api = CoreAPI {
retro_set_environment: *lib.get(b"retro_set_environment")?, retro_set_environment: *lib.get(b"retro_set_environment")?,
@ -128,7 +129,7 @@ impl LibretroApi {
unsafe { (&self.core_api.retro_run)() } unsafe { (&self.core_api.retro_run)() }
} }
/// Serializes internal state. /// Serializes internal state.
pub fn serialize(&mut self) -> Fallible<Vec<u8>> { pub fn serialize(&mut self) -> Result<Vec<u8>> {
let size: usize = unsafe { (&self.core_api.retro_serialize_size)() }; let size: usize = unsafe { (&self.core_api.retro_serialize_size)() };
let mut vec = Vec::with_capacity(size); let mut vec = Vec::with_capacity(size);
vec.resize(size, 0); vec.resize(size, 0);
@ -136,20 +137,20 @@ impl LibretroApi {
if unsafe { (&self.core_api.retro_serialize)(vec.as_mut_ptr() as *mut c_void, size); true } { if unsafe { (&self.core_api.retro_serialize)(vec.as_mut_ptr() as *mut c_void, size); true } {
Ok(vec) Ok(vec)
} else { } else {
Err(failure::err_msg("Serialize failed")) Err("Serialize failed".into())
} }
} }
pub fn unserialize(&mut self, data: &[u8]) -> Fallible<()> { pub fn unserialize(&mut self, data: &[u8]) -> Result<()> {
// validate size of the data // validate size of the data
let size: usize = unsafe { (&self.core_api.retro_serialize_size)() }; let size: usize = unsafe { (&self.core_api.retro_serialize_size)() };
if data.len() != size { if data.len() != size {
return Err(failure::err_msg(format!("Size of data {} doesn't match this core's expected size {}", data.len(), size))) return Err(format!("Size of data {} doesn't match this core's expected size {}", data.len(), size).into())
} }
if unsafe { (&self.core_api.retro_unserialize)(data.as_ptr() as *const c_void, data.len()) } { if unsafe { (&self.core_api.retro_unserialize)(data.as_ptr() as *const c_void, data.len()) } {
Ok(()) Ok(())
} else { } else {
Err(failure::err_msg("Unserialize failed")) Err("Unserialize failed".into())
} }
} }
pub fn cheat_reset(&mut self) { pub fn cheat_reset(&mut self) {
@ -164,7 +165,7 @@ impl LibretroApi {
path: Option<&Path>, path: Option<&Path>,
data: Option<&[u8]>, data: Option<&[u8]>,
meta: Option<&str>, meta: Option<&str>,
) -> Fallible<()> { ) -> Result<()> {
let mut game = GameInfo { let mut game = GameInfo {
path: std::ptr::null(), path: std::ptr::null(),
data: std::ptr::null(), data: std::ptr::null(),
@ -190,7 +191,7 @@ impl LibretroApi {
if unsafe { (&self.core_api.retro_load_game)(&game) } { if unsafe { (&self.core_api.retro_load_game)(&game) } {
Ok(()) Ok(())
} else { } else {
Err(failure::err_msg("Failed to load game")) Err("Failed to load game".into())
} }
} }
/// Unloads the currently loaded game. Called before deinit(). /// Unloads the currently loaded game. Called before deinit().

View File

@ -21,9 +21,9 @@ impl<D> TryFrom<(D, c_uint)> for InputDeviceId
where D: TryInto<DeviceType>, where D: TryInto<DeviceType>,
<D as std::convert::TryInto<DeviceType>>::Error: std::error::Error + Send + Sync + 'static, <D as std::convert::TryInto<DeviceType>>::Error: std::error::Error + Send + Sync + 'static,
{ {
type Error = failure::Error; type Error = Box<dyn std::error::Error>;
fn try_from(pair: (D, c_uint)) -> failure::Fallible<Self> { fn try_from(pair: (D, c_uint)) -> Result<Self, Self::Error> {
let (device, id) = pair; let (device, id) = pair;
Ok(match device.try_into()? { Ok(match device.try_into()? {
DeviceType::None => InputDeviceId::None(id), DeviceType::None => InputDeviceId::None(id),
@ -37,7 +37,7 @@ impl<D> TryFrom<(D, c_uint)> for InputDeviceId
} }
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct Variable2 { pub struct Variable2 {
pub key: String, pub key: String,
pub description: String, pub description: String,
@ -68,7 +68,7 @@ impl From<&Variable> for Variable2 {
} }
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct ControllerDescription2 { pub struct ControllerDescription2 {
pub name: String, pub name: String,
pub base_type: DeviceType, pub base_type: DeviceType,
@ -104,7 +104,7 @@ impl TryFrom<&ControllerDescription> for ControllerDescription2 {
} }
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct InputDescriptor2 { pub struct InputDescriptor2 {
pub port: c_uint, pub port: c_uint,
pub device_id: InputDeviceId, pub device_id: InputDeviceId,
@ -131,7 +131,7 @@ impl TryFrom<&InputDescriptor> for InputDescriptor2 {
} }
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct SubsystemInfo2 { pub struct SubsystemInfo2 {
pub description: String, pub description: String,
pub identifier: String, pub identifier: String,
@ -155,7 +155,7 @@ impl From<&SubsystemInfo> for SubsystemInfo2 {
} }
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct SubsystemRomInfo2 { pub struct SubsystemRomInfo2 {
pub description: String, pub description: String,
pub valid_extensions: Vec<String>, pub valid_extensions: Vec<String>,
@ -185,7 +185,7 @@ impl From<&SubsystemRomInfo> for SubsystemRomInfo2 {
} }
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct SubsystemMemoryInfo2 { pub struct SubsystemMemoryInfo2 {
pub extension: String, pub extension: String,
pub kind: c_uint, pub kind: c_uint,

View File

@ -31,7 +31,7 @@ extern "C" {
// method docs largely copied/lightly-adapted-to-rust straight from libretro.h. // method docs largely copied/lightly-adapted-to-rust straight from libretro.h.
#[rustfmt::skip] #[rustfmt::skip]
#[allow(unused)] #[allow(unused_variables)]
pub trait RetroCallbacks: Unpin + 'static { pub trait RetroCallbacks: Unpin + 'static {
// -- main callbacks -- // -- main callbacks --
/// Render a frame. Pixel format is 15-bit 0RGB1555 native endian /// Render a frame. Pixel format is 15-bit 0RGB1555 native endian
@ -67,7 +67,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// Is only implemented if rotation can be accelerated by hardware. /// Is only implemented if rotation can be accelerated by hardware.
/// Valid values are 0, 1, 2, 3, which rotates screen by 0, 90, 180, /// Valid values are 0, 1, 2, 3, which rotates screen by 0, 90, 180,
/// 270 degrees counter-clockwise respectively. /// 270 degrees counter-clockwise respectively.
fn set_rotation(&mut self, rotation: EnvRotation) -> bool { false } fn set_rotation(&mut self, rotation: EnvRotation) -> Option<bool> { None }
/// Boolean value whether or not the implementation should use overscan, /// Boolean value whether or not the implementation should use overscan,
/// or crop away overscan. /// or crop away overscan.
fn get_overscan(&mut self) -> Option<bool> { None } fn get_overscan(&mut self) -> Option<bool> { None }
@ -76,11 +76,11 @@ pub trait RetroCallbacks: Unpin + 'static {
/// Should not be used for trivial messages, which should simply be /// Should not be used for trivial messages, which should simply be
/// logged via [Self::get_log_interface] (or as a /// logged via [Self::get_log_interface] (or as a
/// fallback, stderr). /// fallback, stderr).
fn set_message(&mut self, message: &Message) -> bool { false } fn set_message(&mut self, message: &Message) -> Option<bool> { None }
/// Requests the frontend to shutdown. /// Requests the frontend to shutdown.
/// Should only be used if game has a specific /// Should only be used if game has a specific
/// way to shutdown the game from a menu item or similar. /// way to shutdown the game from a menu item or similar.
fn shutdown(&mut self) -> bool { false } fn shutdown(&mut self) -> Option<bool> { None }
/// Gives a hint to the frontend how demanding this implementation /// Gives a hint to the frontend how demanding this implementation
/// is on a system. E.g. reporting a level of 2 means /// is on a system. E.g. reporting a level of 2 means
/// this implementation should run decently on all frontends /// this implementation should run decently on all frontends
@ -95,7 +95,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// as certain games an implementation can play might be /// as certain games an implementation can play might be
/// particularly demanding. /// particularly demanding.
/// If called, it should be called in [libretro_sys::CoreAPI::retro_load_game]. /// If called, it should be called in [libretro_sys::CoreAPI::retro_load_game].
fn set_performance_level(&mut self, level: c_uint) -> bool { false } fn set_performance_level(&mut self, level: c_uint) -> Option<bool> { None }
/// Returns the "system" directory of the frontend. /// Returns the "system" directory of the frontend.
/// This directory can be used to store system specific /// This directory can be used to store system specific
/// content such as BIOSes, configuration data, etc. /// content such as BIOSes, configuration data, etc.
@ -116,12 +116,12 @@ pub trait RetroCallbacks: Unpin + 'static {
/// ///
/// The core should call this function inside [libretro_sys::CoreAPI::retro_load_game] or /// The core should call this function inside [libretro_sys::CoreAPI::retro_load_game] or
/// [Self::set_system_av_info]. /// [Self::set_system_av_info].
fn set_pixel_format(&mut self, format: PixelFormat) -> bool { false } fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> { None }
/// Sets an array of [crate::prelude::InputDescriptor2]. /// Sets an array of [crate::prelude::InputDescriptor2].
/// It is up to the frontend to present this in a usable way. /// 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 /// This function can be called at any time, but it is recommended
/// for the core to call it as early as possible. /// for the core to call it as early as possible.
fn set_input_descriptors(&mut self, input_descriptors: &Vec<InputDescriptor2>) -> bool { false } fn set_input_descriptors(&mut self, input_descriptors: &Vec<InputDescriptor2>) -> Option<bool> { None }
/// Sets an interface to let a libretro core render with /// Sets an interface to let a libretro core render with
/// hardware acceleration. /// hardware acceleration.
/// The core should call this in [libretro_sys::CoreAPI::retro_load_game]. /// The core should call this in [libretro_sys::CoreAPI::retro_load_game].
@ -131,7 +131,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// max_width/max_height provided in [libretro_sys::CoreAPI::retro_get_system_av_info]. /// 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 /// If HW rendering is used, pass only [libretro_sys::HW_FRAME_BUFFER_VALID] or
/// NULL to [libretro_sys::VideoRefreshFn]. /// NULL to [libretro_sys::VideoRefreshFn].
fn set_hw_render(&mut self, hw_render_callback: &HwRenderCallback) -> bool { false } fn set_hw_render(&mut self, hw_render_callback: &HwRenderCallback) -> Option<bool> { None }
/// Interface to acquire user-defined information from environment /// Interface to acquire user-defined information from environment
/// that cannot feasibly be supported in a multi-system way. /// that cannot feasibly be supported in a multi-system way.
/// 'key' should be set to a key which has already been set by /// 'key' should be set to a key which has already been set by
@ -162,7 +162,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// ///
/// Only strings are operated on. The possible values will /// Only strings are operated on. The possible values will
/// generally be displayed and stored as-is by the frontend. /// generally be displayed and stored as-is by the frontend.
fn set_variables(&mut self, variables: &Vec<Variable2>) -> bool { false } fn set_variables(&mut self, variables: &Vec<Variable2>) -> Option<bool> { None }
/// Result is set to true if some variables are updated by /// Result is set to true if some variables are updated by
/// frontend since last call to [Self::get_variable]. /// frontend since last call to [Self::get_variable].
/// Variables should be queried with [Self::get_variable]. /// Variables should be queried with [Self::get_variable].
@ -171,7 +171,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// [libretro_sys::CoreAPI::retro_load_game] with NULL as argument. /// [libretro_sys::CoreAPI::retro_load_game] with NULL as argument.
/// Used by cores which can run without particular game data. /// Used by cores which can run without particular game data.
/// This should be called within [libretro_sys::CoreAPI::retro_set_environment] only. /// This should be called within [libretro_sys::CoreAPI::retro_set_environment] only.
fn set_support_no_game(&mut self, supports_no_game: bool) -> bool { false } fn set_support_no_game(&mut self, supports_no_game: bool) -> Option<bool> { None }
/// Retrieves the absolute path from where this libretro /// Retrieves the absolute path from where this libretro
/// implementation was loaded. /// implementation was loaded.
/// `None` is returned if the libretro was loaded statically /// `None` is returned if the libretro was loaded statically
@ -238,7 +238,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// ///
/// If this returns false, the frontend does not acknowledge a /// If this returns false, the frontend does not acknowledge a
/// changed av_info struct. /// changed av_info struct.
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> bool { false } fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> Option<bool> { None }
/// This environment call introduces the concept of libretro "subsystems". /// This environment call introduces the concept of libretro "subsystems".
/// A subsystem is a variant of a libretro core which supports /// A subsystem is a variant of a libretro core which supports
/// different kinds of games. /// different kinds of games.
@ -253,7 +253,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// ///
/// If a core wants to expose this interface, [Self::set_subsystem_info] /// If a core wants to expose this interface, [Self::set_subsystem_info]
/// **MUST** be called from within [libretro_sys::CoreAPI::retro_set_environment]. /// **MUST** be called from within [libretro_sys::CoreAPI::retro_set_environment].
fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> bool { false } fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> Option<bool> { None }
/// This environment call lets a libretro core tell the frontend /// This environment call lets a libretro core tell the frontend
/// which controller subclasses are recognized in calls to /// which controller subclasses are recognized in calls to
/// [libretro_sys::CoreAPI::retro_set_controller_port_device]. /// [libretro_sys::CoreAPI::retro_set_controller_port_device].
@ -287,7 +287,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// ///
/// NOTE: Even if special device types are set in the libretro core, /// NOTE: Even if special device types are set in the libretro core,
/// libretro should only poll input based on the base input device types. /// libretro should only poll input based on the base input device types.
fn set_controller_info(&mut self, controller_info: &Vec<ControllerDescription2>) -> bool { false } fn set_controller_info(&mut self, controller_info: &Vec<ControllerDescription2>) -> Option<bool> { None }
/// This environment call lets a libretro core tell the frontend /// This environment call lets a libretro core tell the frontend
/// about the memory maps this core emulates. /// about the memory maps this core emulates.
/// This can be used to implement, for example, cheats in a core-agnostic way. /// This can be used to implement, for example, cheats in a core-agnostic way.
@ -299,7 +299,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// [libretro_sys::CoreAPI::retro_get_memory_size] as well. /// [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]. /// 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) -> bool { false } 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 /// This environment call is similar to [Self::set_system_av_info] for changing
/// video parameters, but provides a guarantee that drivers will not be /// video parameters, but provides a guarantee that drivers will not be
/// reinitialized. /// reinitialized.
@ -316,7 +316,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// ///
/// A frontend must guarantee that this environment call completes in /// A frontend must guarantee that this environment call completes in
/// constant time. /// constant time.
fn set_geometry(&mut self, game_geometry: &GameGeometry) -> bool { false } fn set_geometry(&mut self, game_geometry: &GameGeometry) -> Option<bool> { None }
/// Returns the specified username of the frontend, if specified by the user. /// 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 /// 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. /// or any other mode where personalization of the user is desirable.
@ -327,7 +327,7 @@ pub trait RetroCallbacks: Unpin + 'static {
/// Returns the specified language of the frontend, if specified by the user. /// Returns the specified language of the frontend, if specified by the user.
/// It can be used by the core for localization purposes. /// It can be used by the core for localization purposes.
fn get_language(&mut self) -> Option<Language> { None } fn get_language(&mut self) -> Option<Language> { None }
// fn set_serialization_quirks(&mut self, quirks: &mut u64) -> bool { false } // fn set_serialization_quirks(&mut self, quirks: &mut u64) -> Option<bool> { None }
// -- environment-set callbacks (API extensions) -- // -- environment-set callbacks (API extensions) --
/// Logging function. /// Logging function.
@ -432,17 +432,17 @@ impl StaticCallbacks {
); );
} }
match parsed_cmd? { match parsed_cmd? {
EnvCmd::SetRotation => handler.set_rotation(Self::enum_from_void(data)?), EnvCmd::SetRotation => handler.set_rotation(Self::enum_from_void(data)?)?,
EnvCmd::GetOverscan => Self::clone_into_void(data, &handler.get_overscan()?)?, EnvCmd::GetOverscan => Self::clone_into_void(data, &handler.get_overscan()?)?,
EnvCmd::GetCanDupe => Self::clone_into_void(data, &true)?, EnvCmd::GetCanDupe => Self::clone_into_void(data, &true)?,
EnvCmd::SetMessage => handler.set_message(Self::from_void::<Message>(data)?), EnvCmd::SetMessage => handler.set_message(Self::from_void::<Message>(data)?)?,
EnvCmd::Shutdown => handler.shutdown(), EnvCmd::Shutdown => handler.shutdown()?,
EnvCmd::SetPerformanceLevel => handler.set_performance_level(*Self::from_void(data)?), EnvCmd::SetPerformanceLevel => handler.set_performance_level(*Self::from_void(data)?)?,
EnvCmd::GetSystemDirectory => { EnvCmd::GetSystemDirectory => {
Self::path_into_void(data, handler.get_system_directory()?)? Self::path_into_void(data, handler.get_system_directory()?)?
} }
EnvCmd::SetPixelFormat => { EnvCmd::SetPixelFormat => {
handler.set_pixel_format(PixelFormat::from_uint(*Self::from_void(data)?)?) handler.set_pixel_format(PixelFormat::from_uint(*Self::from_void(data)?)?)?
} }
EnvCmd::SetInputDescriptors => { EnvCmd::SetInputDescriptors => {
let mut input_desc = data as *const InputDescriptor; let mut input_desc = data as *const InputDescriptor;
@ -451,7 +451,7 @@ impl StaticCallbacks {
descriptors.push(unsafe { input_desc.as_ref() }?.try_into().ok()?); descriptors.push(unsafe { input_desc.as_ref() }?.try_into().ok()?);
input_desc = input_desc.wrapping_add(1); input_desc = input_desc.wrapping_add(1);
} }
handler.set_input_descriptors(&descriptors) handler.set_input_descriptors(&descriptors)?
} }
EnvCmd::SetKeyboardCallback => { EnvCmd::SetKeyboardCallback => {
let kc: &mut KeyboardCallback = Self::from_void(data)?; let kc: &mut KeyboardCallback = Self::from_void(data)?;
@ -503,12 +503,12 @@ impl StaticCallbacks {
descriptors.push(unsafe { var.as_ref() }?.into()); descriptors.push(unsafe { var.as_ref() }?.into());
var = var.wrapping_add(1); var = var.wrapping_add(1);
} }
handler.set_variables(&descriptors) handler.set_variables(&descriptors)?
} }
EnvCmd::GetVariableUpdate => { EnvCmd::GetVariableUpdate => {
Self::clone_into_void(data, &handler.get_variable_update()?)? Self::clone_into_void(data, &handler.get_variable_update()?)?
} }
EnvCmd::SetSupportNoGame => handler.set_support_no_game(*Self::from_void(data)?), EnvCmd::SetSupportNoGame => handler.set_support_no_game(*Self::from_void(data)?)?,
EnvCmd::GetLibretroPath => Self::path_into_void(data, handler.get_libretro_path()?)?, EnvCmd::GetLibretroPath => Self::path_into_void(data, handler.get_libretro_path()?)?,
EnvCmd::SetFrameTimeCallback => { EnvCmd::SetFrameTimeCallback => {
let ftc: &mut FrameTimeCallback = Self::from_void(data)?; let ftc: &mut FrameTimeCallback = Self::from_void(data)?;
@ -563,7 +563,7 @@ impl StaticCallbacks {
} }
EnvCmd::GetSaveDirectory => Self::path_into_void(data, handler.get_save_directory()?)?, EnvCmd::GetSaveDirectory => Self::path_into_void(data, handler.get_save_directory()?)?,
EnvCmd::SetSystemAvInfo => { EnvCmd::SetSystemAvInfo => {
handler.set_system_av_info(Self::from_void::<SystemAvInfo>(data)?) handler.set_system_av_info(Self::from_void::<SystemAvInfo>(data)?)?
} }
EnvCmd::SetProcAddressCallback => { EnvCmd::SetProcAddressCallback => {
let gpa = Self::from_void::<GetProcAddressInterface>(data)?; let gpa = Self::from_void::<GetProcAddressInterface>(data)?;
@ -580,7 +580,7 @@ impl StaticCallbacks {
descriptors.push(unsafe { info.as_ref() }?.into()); descriptors.push(unsafe { info.as_ref() }?.into());
info = info.wrapping_add(1); info = info.wrapping_add(1);
} }
handler.set_subsystem_info(&descriptors) handler.set_subsystem_info(&descriptors)?
} }
EnvCmd::SetControllerInfo => { EnvCmd::SetControllerInfo => {
let info = unsafe { (data as *const ControllerInfo).as_ref() }?; let info = unsafe { (data as *const ControllerInfo).as_ref() }?;
@ -591,11 +591,11 @@ impl StaticCallbacks {
.map(TryInto::try_into) .map(TryInto::try_into)
.filter_map(|x| x.ok()) .filter_map(|x| x.ok())
.collect(); .collect();
handler.set_controller_info(&controller_info) handler.set_controller_info(&controller_info)?
} }
// TODO (experimental) EnvCmd::SetMemoryMaps => {}, // TODO (experimental) EnvCmd::SetMemoryMaps => {},
EnvCmd::SetGeometry => { EnvCmd::SetGeometry => {
handler.set_geometry(Self::from_void::<GameGeometry>(data)?) handler.set_geometry(Self::from_void::<GameGeometry>(data)?)?
} }
EnvCmd::GetUsername => Self::string_into_void(data, handler.get_username()?)?, EnvCmd::GetUsername => Self::string_into_void(data, handler.get_username()?)?,
EnvCmd::GetLanguage => Self::clone_into_void(data, &handler.get_language()?)?, EnvCmd::GetLanguage => Self::clone_into_void(data, &handler.get_language()?)?,