diff --git a/crates/retrovnc/src/main.rs b/crates/retrovnc/src/main.rs index 5d77363..bab12ed 100644 --- a/crates/retrovnc/src/main.rs +++ b/crates/retrovnc/src/main.rs @@ -9,7 +9,7 @@ use retro_frontend::{ use tracing::Level; use tracing_subscriber::FmtSubscriber; -use clap::{arg, command}; +use clap::{arg, command, value_parser}; mod rfb; use rfb::*; @@ -139,6 +139,16 @@ fn main() -> Result<()> { // Not that it matters, but this is only really required for cores that require // content to be loaded; that's most cores, but libretro does support the difference. .arg(arg!(--rom ).required(false)) + .arg( + arg!(--rfb_listen
) + .required(false) + .default_value("127.0.0.1"), + ) + .arg( + arg!(--rfb_port ) + .value_parser(value_parser!(u16)) + .required(true), + ) .get_matches(); let core_path: &String = matches.get_one("core").unwrap(); @@ -149,8 +159,11 @@ fn main() -> Result<()> { // default WxH; this is overridden quickly width: 640, height: 480, - listen_address: "127.0.0.1".parse::()?, - listen_port: 6930 + listen_address: matches + .get_one::("rfb_listen") + .unwrap() + .parse::()?, + listen_port: *matches.get_one::("rfb_port").unwrap(), }; // Initalize the app diff --git a/crates/retrovnc/src/rfb.rs b/crates/retrovnc/src/rfb.rs index 3c8b15b..7791569 100644 --- a/crates/retrovnc/src/rfb.rs +++ b/crates/retrovnc/src/rfb.rs @@ -27,10 +27,12 @@ impl RfbServer { let ip_string = std::ffi::CString::new(config.listen_address.to_string())?; + info!("?? {:?}", ip_string); + let argc = 3; let argv: [*const std::ffi::c_char; 3] = [ - b"RfbServer".as_ptr() as *const i8, - b"-listen".as_ptr() as *const i8, + b"RfbServer\0".as_ptr() as *const i8, + b"-listen\0".as_ptr() as *const i8, ip_string.as_ptr(), ]; @@ -64,9 +66,11 @@ impl RfbServer { (*screen).newClientHook = Some(Self::connection_callback); (*screen).kbdAddEvent = Some(Self::on_key_callback); + // testing (*screen).port = config.listen_port as i32; (*screen).ipv6port = 0; + (*screen).deferUpdateTime = 16; ret.resize(config.width, config.height); @@ -93,14 +97,6 @@ impl RfbServer { } pub fn update_buffer(&mut self, slice: &[u32], width: u16, height: u16) { - // lame slow loop (dont use this sucks) - /* - for x in 0..width { - for y in 0..height { - self.framebuffer[(y * width + x) as usize] = slice[(y * width + x) as usize]; - } - }*/ - self.framebuffer.copy_from_slice(&slice); unsafe { @@ -114,6 +110,7 @@ impl RfbServer { } } + /// Start the server. pub fn start(&mut self) { unsafe { rfb::bindings::rfbInitServerWithPthreadsAndZRLE(self.ptr.as_ptr()); @@ -123,6 +120,14 @@ impl RfbServer { } } + /// Shutdown the server. This does not destroy the RFB screen, therefore [RfbServer::start] can be + /// used to start it back up. (Only dropping the RfbServer will destroy the screen currently.) + pub fn shutdown(&mut self) { + unsafe { + rfb::bindings::rfbShutdownServer(self.ptr.as_ptr(), 1); + } + } + pub fn get_buttons(&self) -> [bool; 32] { self.buttons } @@ -240,6 +245,15 @@ impl RfbServer { } } +impl Drop for RfbServer { + fn drop(&mut self) { + unsafe { + // Shut down the server and release resources. + rfb::bindings::rfbScreenCleanup(self.ptr.as_ptr()); + } + } +} + // Logging support use std::ffi;