Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drive/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ foreground_sleep_ms 2 // number of milliseconds to sleep each frame. Try 10 to c

background_sleep_ms 10 // number of milliseconds to sleep each frame when running in the background

sessions 925 // number of times program has been run
sessions 942 // number of times program has been run

// (scancode) hold this key down and left-click to simulate right-click
rmb_key 0 // 0 for none 226 for LALT
Expand Down
63 changes: 63 additions & 0 deletions src/hal/macos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod network;
use std::{
fs::{File, OpenOptions},
io::{BufRead, BufReader, Write},
path::{Path, PathBuf},
time::Duration,
};
Expand All @@ -11,6 +12,9 @@ use nix::{
unistd::Pid,
};
use tokio::process::Child;
use super::{launch_pico8_binary, PipeHAL};
use async_trait::async_trait;
use std::fs::remove_file;

pub const IN_PIPE: &str = "in_pipe";
pub const OUT_PIPE: &str = "out_pipe";
Expand All @@ -24,6 +28,7 @@ pub async fn pico8_to_bg(pico8_process: &Child, mut child: Child) {
warn!("pico8_to_bg not implemented for macos")
}

/*
// create named pipes if they don't exist
fn create_pipe(pipe: &Path) -> anyhow::Result<()> {
use nix::{sys::stat::Mode, unistd::mkfifo};
Expand All @@ -48,6 +53,30 @@ pub fn open_out_pipe() -> anyhow::Result<File> {

Ok(out_pipe)
}
*/

// just create a normal file
fn create_pipe(pipe: &Path) -> anyhow::Result<()> {
if Path::new(pipe).exists() {
remove_file(pipe)?;
}
OpenOptions::new().write(true).create(true).open(pipe)?;
Ok(())
}

pub fn open_in_pipe() -> anyhow::Result<File> {
create_pipe(&PathBuf::from(IN_PIPE))?;
let in_pipe = OpenOptions::new().write(true).open(IN_PIPE)?;

Ok(in_pipe)
}

pub fn open_out_pipe() -> anyhow::Result<File> {
create_pipe(&PathBuf::from(OUT_PIPE))?;
let out_pipe = OpenOptions::new().read(true).open(OUT_PIPE)?;

Ok(out_pipe)
}

pub fn kill_pico8_process(pico8_process: &Child) -> anyhow::Result<()> {
let pico8_pid = Pid::from_raw(
Expand Down Expand Up @@ -82,3 +111,37 @@ pub fn resume_pico8_process(pico8_process: &Child) -> anyhow::Result<()> {
kill(pico8_pid, Signal::SIGCONT)?;
Ok(())
}

pub struct MacosPipeHAL {
reader: BufReader<File>,
}

impl MacosPipeHAL {
pub fn init() -> anyhow::Result<Self> {
// need to drop the in_pipe (for some reason) for the pico8 process to start up
let in_pipe = open_in_pipe()?;
drop(in_pipe);

let out_pipe = open_out_pipe()?;
let mut reader = BufReader::new(out_pipe);

Ok(Self { reader })
}
}

#[async_trait]
impl PipeHAL for MacosPipeHAL {
async fn write_to_pico8(&mut self, msg: String) -> anyhow::Result<()> {
let mut in_pipe = open_in_pipe()?;
writeln!(in_pipe, "{msg}")?;
drop(in_pipe);
Ok(())
}

async fn read_from_pico8(&mut self) -> anyhow::Result<String> {
let mut line = String::new();
self.reader.read_line(&mut line)?;

Ok(line)
}
}
7 changes: 6 additions & 1 deletion src/hal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,18 @@ pub trait PipeHAL {
pub fn init_pipe_hal() -> Result<Box<dyn PipeHAL>> {
#[cfg(target_os = "linux")]
{
return Ok(Box::new(LinuxPipeHAL::init().unwrap()) as Box<dyn PipeHAL>);
return Ok(Box::new(MacosPipeHAL::init().unwrap()) as Box<dyn PipeHAL>);
}

#[cfg(target_os = "windows")]
{
return Ok(Box::new(WindowsPipeHAL::init().unwrap()) as Box<dyn PipeHAL>);
}

#[cfg(target_os = "macos")]
{
return Ok(Box::new(MacosPipeHAL::init().unwrap()) as Box<dyn PipeHAL>);
}

Ok(Box::new(DummyPipeHAL::init()) as Box<dyn PipeHAL>)
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ async fn main() {
} else {
format!("{},{}", cartdatas.len(), cartdatas_encoded)
};
println!("got {} carts", cartdatas.len());
println!("{data}");
pipe_hal.write_to_pico8(data).await;
},
"download" => {
Expand Down
Loading