From 768dfb75bbbb1e8695795b6b614e928292aa468e Mon Sep 17 00:00:00 2001 From: lifning <> Date: Tue, 14 Dec 2021 03:14:10 -0800 Subject: [PATCH] simple core info querying --- ferretro_base/examples/core_info.rs | 12 ++++++ ferretro_base/src/retro/loading.rs | 5 ++- ferretro_base/src/retro/wrapped_types.rs | 42 +++++++++++++++---- .../src/provided/sdl2/canvas.rs | 3 +- .../src/provided/sdl2/opengl.rs | 3 +- 5 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 ferretro_base/examples/core_info.rs diff --git a/ferretro_base/examples/core_info.rs b/ferretro_base/examples/core_info.rs new file mode 100644 index 0000000..4a7eccc --- /dev/null +++ b/ferretro_base/examples/core_info.rs @@ -0,0 +1,12 @@ +use ferretro_base::retro::loading::LibretroApi; + +fn main() -> Result<(), Box> { + for core_path in std::env::args().skip(1) { + println!("{}:", core_path); + let lib = libloading::Library::new(&core_path)?; + let api = LibretroApi::from_library(lib)?; + let info = api.get_system_info(); + println!("\t{} {} {:?}", info.library_name, info.library_version, info.valid_extensions); + } + Ok(()) +} diff --git a/ferretro_base/src/retro/loading.rs b/ferretro_base/src/retro/loading.rs index 2c49de1..96af219 100644 --- a/ferretro_base/src/retro/loading.rs +++ b/ferretro_base/src/retro/loading.rs @@ -4,6 +4,7 @@ use std::os::raw::{c_char, c_void}; use std::path::Path; use libloading; +use crate::prelude::SystemInfo2; use super::ffi::*; @@ -143,11 +144,11 @@ impl LibretroApi { unsafe { (&self.core_api.retro_api_version)() as u32 } } /// Gets statically known system info. Can be called at any time, even before retro_init(). - pub fn get_system_info(&self) -> SystemInfo { + pub fn get_system_info(&self) -> SystemInfo2 { unsafe { let mut info = ::std::mem::zeroed::(); (&self.core_api.retro_get_system_info)(&mut info); - info + SystemInfo2::from(&info) } } /// Gets information about system audio/video timings and geometry. diff --git a/ferretro_base/src/retro/wrapped_types.rs b/ferretro_base/src/retro/wrapped_types.rs index 8705160..12d0411 100644 --- a/ferretro_base/src/retro/wrapped_types.rs +++ b/ferretro_base/src/retro/wrapped_types.rs @@ -2,12 +2,24 @@ use core::convert::{TryFrom, TryInto}; use core::mem::size_of; use std::ffi::CStr; use std::fmt::{Debug, Formatter}; -use std::os::raw::{c_uint}; +use std::os::raw::{c_char, c_uint}; use std::slice::from_raw_parts; use super::constants::*; use super::ffi::*; +fn delimited_cstr(cstr: *const c_char, delim: char) -> Vec { + if cstr.is_null() { + Vec::new() + } else { + unsafe { CStr::from_ptr(cstr) } + .to_string_lossy() + .split(delim) + .map(str::to_string) + .collect() + } +} + #[derive(Clone, Copy)] pub enum VideoFrame<'a> { XRGB1555 { data: &'a [u16], width: c_uint, height: c_uint, pitch_u16: usize }, @@ -124,6 +136,26 @@ impl TryFrom<(D, c_uint)> for InputDeviceId } } +pub struct SystemInfo2 { + pub library_name: String, + pub library_version: String, + pub valid_extensions: Vec, + pub need_fullpath: bool, + pub block_extract: bool, +} + +impl From<&SystemInfo> for SystemInfo2 { + fn from(info: &SystemInfo) -> Self { + SystemInfo2 { + library_name: unsafe { CStr::from_ptr(info.library_name) }.to_string_lossy().to_string(), + library_version: unsafe { CStr::from_ptr(info.library_version) }.to_string_lossy().to_string(), + valid_extensions: delimited_cstr(info.valid_extensions, '|'), + need_fullpath: info.need_fullpath, + block_extract: info.block_extract, + } + } +} + #[derive(Clone, Debug)] pub struct Variable2 { pub key: String, @@ -145,7 +177,7 @@ impl From<&Variable> for Variable2 { .get(1) .unwrap_or(&"") .split('|') - .map(String::from) + .map(str::to_string) .collect(); Variable2 { key, @@ -268,11 +300,7 @@ impl From<&SubsystemRomInfo> for SubsystemRomInfo2 { description: unsafe { CStr::from_ptr(sri.desc) } .to_string_lossy() .to_string(), - valid_extensions: unsafe { CStr::from_ptr(sri.valid_extensions) } - .to_string_lossy() - .split('|') - .map(str::to_string) - .collect(), + valid_extensions: delimited_cstr(sri.valid_extensions, '|'), need_fullpath: sri.need_fullpath, block_extract: sri.block_extract, required: sri.required, diff --git a/ferretro_components/src/provided/sdl2/canvas.rs b/ferretro_components/src/provided/sdl2/canvas.rs index 5216629..78bbb4c 100644 --- a/ferretro_components/src/provided/sdl2/canvas.rs +++ b/ferretro_components/src/provided/sdl2/canvas.rs @@ -1,6 +1,5 @@ use crate::prelude::*; -use std::ffi::CStr; use std::path::Path; use sdl2::Sdl; @@ -108,7 +107,7 @@ impl RetroComponent for SimpleSdl2CanvasComponent { let sys_info = retro.get_system_info(); let title = format!( "{} - {} - ferretro SDL", - unsafe { CStr::from_ptr(sys_info.library_name) }.to_string_lossy(), + sys_info.library_name, rom.file_stem().unwrap_or_default().to_string_lossy(), ); self.canvas.window_mut().set_title(&title)?; diff --git a/ferretro_components/src/provided/sdl2/opengl.rs b/ferretro_components/src/provided/sdl2/opengl.rs index 34fd24f..81d0f6c 100644 --- a/ferretro_components/src/provided/sdl2/opengl.rs +++ b/ferretro_components/src/provided/sdl2/opengl.rs @@ -1,6 +1,5 @@ use crate::prelude::*; -use std::ffi::CStr; use std::os::raw::c_uint; use std::path::Path; @@ -97,7 +96,7 @@ impl RetroComponent for SimpleSdl2OpenglComponent { let sys_info = retro.get_system_info(); let title = format!( "{} - {} - ferretro SDL GL", - unsafe { CStr::from_ptr(sys_info.library_name) }.to_string_lossy(), + sys_info.library_name, rom.file_stem().unwrap_or_default().to_string_lossy(), ); self.canvas.window_mut().set_title(&title)?;