//! The Disk System's disks through libretro's disk control interface //! (RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, or the older one): //! each side of each disk is one "image"; ejecting takes the disk out of the //! drive, inserting puts the chosen side in. It is the reference window's //! change_disk (frontend.py) with the side chosen by the frontend's menu. //! //! Beside it, by default, [`famicom_rust::fds_auto::AutoDisk`] changes the //! sides as the program asks (the reference window's default too); the core //! option `famicom_auto_disk` turns it off. Its changes are shown as //! frontend messages. use crate::{Core, CORE}; use famicom_rust::cartridge::fds::Fds; use std::ffi::{c_char, c_uint, c_void, CString}; use std::sync::Mutex; pub const RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE: c_uint = 13; pub const RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE: c_uint = 58; #[repr(C)] pub struct DiskControl { pub set_eject_state: unsafe extern "C" fn(bool) -> bool, pub get_eject_state: unsafe extern "C" fn() -> bool, pub get_image_index: unsafe extern "C" fn() -> c_uint, pub set_image_index: unsafe extern "C" fn(c_uint) -> bool, pub get_num_images: unsafe extern "C" fn() -> c_uint, pub replace_image_index: unsafe extern "C" fn(c_uint, *const c_void) -> bool, pub add_image_index: unsafe extern "C" fn() -> bool, } #[repr(C)] pub struct DiskControlExt { pub base: DiskControl, pub set_initial_image: unsafe extern "C" fn(c_uint, *const c_char) -> bool, pub get_image_path: unsafe extern "C" fn(c_uint, *mut c_char, usize) -> bool, pub get_image_label: unsafe extern "C" fn(c_uint, *mut c_char, usize) -> bool, } pub static CONTROL: DiskControlExt = DiskControlExt { base: DiskControl { set_eject_state, get_eject_state, get_image_index, set_image_index, get_num_images, replace_image_index, add_image_index, }, set_initial_image, get_image_path, get_image_label, }; /// The side the frontend chose while the drive is empty (what goes in next). pub static CHOSEN: Mutex = Mutex::new(0); fn with_fds(f: impl FnOnce(&mut Fds) -> R) -> Option { let mut guard = CORE.lock().unwrap(); let core: &mut Core = guard.as_mut()?; core.system.board().ppu.cart.fds_mut().map(f) } unsafe extern "C" fn set_eject_state(ejected: bool) -> bool { with_fds(|fds| { if ejected { if let Some(side) = fds.side { *CHOSEN.lock().unwrap() = side; } fds.insert(None); } else if fds.side.is_none() && !fds.sides.is_empty() { let chosen = (*CHOSEN.lock().unwrap()).min(fds.sides.len() - 1); fds.insert(Some(chosen)); } true }) .unwrap_or(false) } unsafe extern "C" fn get_eject_state() -> bool { with_fds(|fds| fds.side.is_none()).unwrap_or(false) } unsafe extern "C" fn get_image_index() -> c_uint { with_fds(|fds| fds.side.unwrap_or(*CHOSEN.lock().unwrap()) as c_uint).unwrap_or(0) } /// Libretro's rule: the index is chosen with the tray open; it goes in when /// the tray closes. unsafe extern "C" fn set_image_index(index: c_uint) -> bool { with_fds(|fds| { if index as usize >= fds.sides.len() || fds.side.is_some() { return false; } *CHOSEN.lock().unwrap() = index as usize; true }) .unwrap_or(false) } unsafe extern "C" fn get_num_images() -> c_uint { with_fds(|fds| fds.sides.len() as c_uint).unwrap_or(0) } /// The sides are the loaded image's: none are added or replaced. unsafe extern "C" fn replace_image_index(_index: c_uint, _info: *const c_void) -> bool { false } unsafe extern "C" fn add_image_index() -> bool { false } unsafe extern "C" fn set_initial_image(_index: c_uint, _path: *const c_char) -> bool { false } unsafe extern "C" fn get_image_path(_index: c_uint, _path: *mut c_char, _len: usize) -> bool { false } /// "disk 1 side A", the reference's name for the side. unsafe extern "C" fn get_image_label(index: c_uint, label: *mut c_char, len: usize) -> bool { if label.is_null() || len == 0 { return false; } let count = with_fds(|fds| fds.sides.len()).unwrap_or(0); if index as usize >= count { return false; } let text = CString::new(Fds::side_name(Some(index as usize))).unwrap(); let bytes = text.as_bytes_with_nul(); let n = bytes.len().min(len); std::ptr::copy_nonoverlapping(bytes.as_ptr() as *const c_char, label, n); *label.add(n - 1) = 0; true }