From a5b89a18526a5b56b74f3ced3d0ebe6ad35a4551 Mon Sep 17 00:00:00 2001 From: Astatin Date: Tue, 22 Jul 2025 23:14:05 +0200 Subject: Add headless option --- src/desktop/window.rs | 8 ++++++++ src/io.rs | 6 ++++++ src/main.rs | 22 +++++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/desktop/window.rs b/src/desktop/window.rs index 0d5bd8c..5265fd2 100644 --- a/src/desktop/window.rs +++ b/src/desktop/window.rs @@ -20,6 +20,14 @@ const HEIGHT: u32 = 144; pub type Keys = Arc>>; +pub struct Headless; + +impl Window for Headless { + fn update(&mut self, _fb: Box<[u32; 160 * 144]>) -> Option { + None + } +} + pub struct DesktopWindow { fb_send: Sender>, signal_recv: Receiver, diff --git a/src/io.rs b/src/io.rs index 730fc55..8ed35e8 100644 --- a/src/io.rs +++ b/src/io.rs @@ -36,6 +36,12 @@ pub trait Window { fn update(&mut self, fb: Box<[u32; 160 * 144]>) -> Option; } +impl Window for Box { + fn update(&mut self, fb: Box<[u32; 160 * 144]>) -> Option { + (**self).update(fb) + } +} + pub trait Serial { fn read_data(&self) -> u8; fn read_control(&self) -> u8; diff --git a/src/main.rs b/src/main.rs index 8303fc3..ee815f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,12 @@ pub mod mmio; pub mod opcodes; pub mod state; +use std::collections::HashSet; +use std::sync::{Arc, Mutex}; + use crate::desktop::input::{Gamepad, GamepadRecorder, GamepadReplay, Keyboard}; use crate::desktop::load_save::FSLoadSave; -use crate::io::{Input, Serial}; +use crate::io::{Input, Serial, Window}; use crate::logs::{log, LogLevel}; use clap::Parser; @@ -61,6 +64,10 @@ struct Cli { #[arg(long, default_value_t = false)] stop_dump_state: bool, + /// Do not create a window + #[arg(long, default_value_t = false)] + headless: bool, + /// Window title #[arg(long, default_value = "Gameboy Emulator")] title: String, @@ -89,7 +96,16 @@ fn main() { log(LogLevel::Infos, format!("Starting {:?}...", &cli.rom)); - let window = desktop::window::DesktopWindow::new(cli.title).unwrap(); + let (window, keys): (Box, desktop::window::Keys) = if cli.headless { + ( + Box::new(desktop::window::Headless), + Arc::new(Mutex::new(HashSet::new())), + ) + } else { + let window = desktop::window::DesktopWindow::new(cli.title).unwrap(); + let keys = window.keys.clone(); + (Box::new(window), keys) + }; let serial: Box = match (cli.fifo_input, cli.fifo_output, cli.listen, cli.connect) { @@ -109,7 +125,7 @@ fn main() { let mut gamepad: Box = if let Some(record_file) = cli.replay_input { Box::new(GamepadReplay::new(record_file)) } else if cli.keyboard { - Box::new(Keyboard::new(window.keys.clone())) + Box::new(Keyboard::new(keys)) } else { Box::new(Gamepad::new()) }; -- cgit v1.2.3-70-g09d2