From 3fdbe9a4f7e8d2b3545555009457a8c6499afa2d Mon Sep 17 00:00:00 2001 From: Logan McNaughton <848146+loganmc10@users.noreply.github.com> Date: Sat, 30 May 2026 13:36:32 +0200 Subject: [PATCH] implement default for Ui (#989) * implement default for Ui * more --- src/device.rs | 2 +- src/device/vi.rs | 4 ++-- src/ui.rs | 16 +++++++++++----- src/ui/config.rs | 15 ++++++++++----- src/ui/input.rs | 1 + src/ui/storage.rs | 7 ++++--- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/device.rs b/src/device.rs index 3727d224..612bb5f2 100644 --- a/src/device.rs +++ b/src/device.rs @@ -226,7 +226,7 @@ pub fn get_rom_contents(file_path: &std::path::PathBuf) -> Option> { pub struct Device { #[serde(skip)] pub netplay: Option, - #[serde(skip, default = "ui::Ui::new")] + #[serde(skip)] pub ui: ui::Ui, pub byte_swap: usize, pub save_state: bool, diff --git a/src/device/vi.rs b/src/device/vi.rs index 1651b02b..c2a620a4 100644 --- a/src/device/vi.rs +++ b/src/device/vi.rs @@ -111,7 +111,7 @@ pub fn write_regs(device: &mut device::Device, address: u64, value: u32, mask: u let current_origin = device.vi.regs[reg as usize]; device::memory::masked_write_32(&mut device.vi.regs[reg as usize], value, mask); if current_origin != device.vi.regs[reg as usize] { - let _ = device.ui.video.fps_tx.try_send(true); + let _ = device.ui.video.fps_tx.as_ref().unwrap().try_send(true); } } _ => { @@ -127,7 +127,7 @@ pub fn vertical_interrupt_event(device: &mut device::Device) { } ui::video::render_frame(); - let _ = device.ui.video.vis_tx.try_send(true); + let _ = device.ui.video.vis_tx.as_ref().unwrap().try_send(true); retroachievements::do_frame(); diff --git a/src/ui.rs b/src/ui.rs index b1a14bfd..2c3cf691 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -28,24 +28,27 @@ pub static WEB_CLIENT: std::sync::LazyLock = std::sync::LazyLoc .unwrap() }); -#[derive(Clone)] +#[derive(Default, Clone)] pub struct Dirs { pub config_dir: std::path::PathBuf, pub data_dir: std::path::PathBuf, pub cache_dir: std::path::PathBuf, } +#[derive(Default)] pub struct Audio { pub audio_stream: *mut sdl3_sys::audio::SDL_AudioStream, pub gain: f32, } +#[derive(Default)] pub struct Input { pub keyboard_state: *const bool, pub last_polled: u64, pub controllers: [input::Controllers; 4], } +#[derive(Default)] pub struct Storage { pub save_type: Vec, pub paths: storage::Paths, @@ -53,15 +56,17 @@ pub struct Storage { pub save_state_slot: u32, } +#[derive(Default)] pub struct Video { pub window: *mut sdl3_sys::video::SDL_Window, pub fullscreen: bool, - pub fps_tx: tokio::sync::mpsc::Sender, + pub fps_tx: Option>, pub fps_rx: Option>, - pub vis_tx: tokio::sync::mpsc::Sender, + pub vis_tx: Option>, pub vis_rx: Option>, } +#[derive(Default)] pub struct Usb { pub usb_tx: Option>, pub cart_rx: Option>, @@ -75,6 +80,7 @@ pub struct GameSettings { pub load_savestate_slot: Option, } +#[derive(Default)] pub struct Ui { pub dirs: Dirs, pub config: config::Config, @@ -254,9 +260,9 @@ impl Ui { video: Video { window: std::ptr::null_mut(), fullscreen: false, - fps_tx, + fps_tx: Some(fps_tx), fps_rx: Some(fps_rx), - vis_tx, + vis_tx: Some(vis_tx), vis_rx: Some(vis_rx), }, usb: Usb { diff --git a/src/ui/config.rs b/src/ui/config.rs index 2b515435..05a15c8b 100644 --- a/src/ui/config.rs +++ b/src/ui/config.rs @@ -33,7 +33,7 @@ pub struct InputProfile { pub deadzone: i32, } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Default, serde::Serialize, serde::Deserialize)] pub struct Input { pub input_profiles: std::collections::BTreeMap, pub input_profile_binding: [String; 4], @@ -45,7 +45,7 @@ pub struct Input { pub emulate_vru: bool, } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Default, serde::Serialize, serde::Deserialize)] pub struct Video { pub upscale: u32, pub ssaa: bool, @@ -56,7 +56,7 @@ pub struct Video { pub crt: bool, } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Default, serde::Serialize, serde::Deserialize)] pub struct Emulation { pub overclock: bool, pub disable_expansion_pak: bool, @@ -69,13 +69,15 @@ pub struct Cheats { std::collections::HashMap>>, } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Default, serde::Serialize, serde::Deserialize)] pub struct Config { pub input: Input, pub video: Video, pub emulation: Emulation, pub rom_dir: std::path::PathBuf, pub recent_roms: Vec, + #[serde(skip)] + write_to_disk: bool, } impl Drop for Cheats { @@ -113,7 +115,9 @@ impl Cheats { impl Drop for Config { fn drop(&mut self) { - write_config(self); + if self.write_to_disk { + write_config(self); + } } } @@ -172,6 +176,7 @@ impl Config { }, rom_dir: std::path::PathBuf::new(), recent_roms: Vec::new(), + write_to_disk: true, } } } diff --git a/src/ui/input.rs b/src/ui/input.rs index 77e06ba5..d1d5f6f3 100644 --- a/src/ui/input.rs +++ b/src/ui/input.rs @@ -32,6 +32,7 @@ pub const DEADZONE_DEFAULT: i32 = 5; pub const UNKNOWN_CONTROLLER_NAME: &str = "Unknown controller"; +#[derive(Default)] pub struct Controllers { pub rumble: bool, pub game_controller: *mut sdl3_sys::gamepad::SDL_Gamepad, diff --git a/src/ui/storage.rs b/src/ui/storage.rs index cf87d561..07c837a7 100644 --- a/src/ui/storage.rs +++ b/src/ui/storage.rs @@ -13,6 +13,7 @@ pub enum SaveTypes { Romsave, } +#[derive(Default)] pub struct Paths { pub eep_file_path: std::path::PathBuf, pub sra_file_path: std::path::PathBuf, @@ -25,19 +26,19 @@ pub struct Paths { // the bool indicates whether the save has been written to // if that is the case, it will be flushed to the disk when the program closes -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Default, serde::Serialize, serde::Deserialize)] pub struct Save { pub data: Vec, pub write_pending: bool, } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Default, serde::Serialize, serde::Deserialize)] pub struct RomSave { pub data: std::collections::HashMap, pub write_pending: bool, } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Default, serde::Serialize, serde::Deserialize)] pub struct Saves { pub eeprom: Save, pub sram: Save,