Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binding for setSerialMessageCallback #327

Merged
merged 6 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions api/system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ bindgen-static = ["sys/bindgen-static"]
bindings-derive-debug = ["sys/bindings-derive-debug"]


[dependencies]
erased_set = "0.8"


[dependencies.sys]
workspace = true
default-features = false
Expand All @@ -46,6 +50,12 @@ crate-type = ["dylib", "staticlib"]
path = "examples/handler-pinned.rs"
required-features = ["sys/entry-point", "sys/lang-items"]

[[example]]
name = "set-serial-message-callback"
crate-type = ["dylib", "staticlib"]
path = "examples/set-serial-message-callback.rs"
required-features = ["sys/entry-point", "sys/lang-items"]

[package.metadata.playdate]
bundle-id = "rs.playdate.system"

Expand Down
2 changes: 2 additions & 0 deletions api/system/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ cargo playdate run -p=playdate-system --example=handler-static --features=sys/la
cargo playdate run -p=playdate-system --example=handler-boxed --features=sys/lang-items,sys/entry-point

cargo playdate run -p=playdate-system --example=handler-pinned --features=sys/lang-items,sys/entry-point

cargo playdate run -p=playdate-system --example=set-serial-message-callback --features=sys/lang-items,sys/entry-point
```

More information how to use [cargo-playdate][] in help: `cargo playdate --help`.
Expand Down
39 changes: 39 additions & 0 deletions api/system/examples/set-serial-message-callback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![no_std]
extern crate alloc;

#[macro_use]
extern crate sys;
extern crate playdate_system as system;

use core::ptr::NonNull;

use sys::EventLoopCtrl;
use sys::ffi::*;
use system::System;
use system::event::SystemEventExt as _;
use system::update::UpdateCtrl;


/// Entry point, event handler
#[no_mangle]
fn event_handler(_api: NonNull<PlaydateAPI>, event: PDSystemEvent, _: u32) -> EventLoopCtrl {
if event == PDSystemEvent::Init {
System::Default().set_serial_message_callback(|data| println!("serial_message_callback: {}", data));
}

System::Default().set_update_callback_static(Some(on_update), ());

// Continue event-loop:
EventLoopCtrl::Continue
}


/// Update handler
fn on_update(_: &mut ()) -> UpdateCtrl {
// Continue updates
UpdateCtrl::Continue
}


// Needed for debug build
ll_symbols!();
35 changes: 34 additions & 1 deletion api/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use core::ffi::c_float;
use core::ffi::c_int;
use core::ffi::c_uint;
use core::time::Duration;
use alloc::string::String;

mod storage;
use storage::*;

pub mod time;
pub mod lang;
Expand Down Expand Up @@ -247,10 +251,22 @@ impl<Api: api::Api> System<Api> {
let _ = dt; // this to prevent earlier drop.
epoch
}
}

/// Equivalent to [`sys::ffi::playdate_sys::setSerialMessageCallback`]
#[doc(alias = "sys::ffi::playdate_sys::setSerialMessageCallback")]
pub fn set_serial_message_callback<F>(&self, callback: F)
where F: FnMut(String),
F: 'static + Send {
init_store();
unsafe { STORE.as_mut() }.expect("impossible")
.insert::<F>(callback);
let f = self.0.set_serial_message_callback();
unsafe { f(Some(proxy_serial_message_callback::<F>)) }
}
}

pub mod api {
use core::ffi::c_char;
use core::ffi::c_float;
use core::ffi::c_int;
use core::ffi::c_uint;
Expand All @@ -263,6 +279,9 @@ pub mod api {
use sys::ffi::playdate_sys;


pub type FnSerialMessageCallback = Option<unsafe extern "C" fn(data: *const c_char)>;


/// Default system api end-point, ZST.
///
/// All calls approximately costs ~3 derefs.
Expand Down Expand Up @@ -418,6 +437,13 @@ pub mod api {
fn convert_date_time_to_epoch(&self) -> unsafe extern "C" fn(datetime: *mut PDDateTime) -> u32 {
self.0.convertDateTimeToEpoch.expect("convertDateTimeToEpoch")
}

/// Equivalent to [`sys::ffi::playdate_sys::setSerialMessageCallback`]
#[doc(alias = "sys::ffi::playdate_sys::setSerialMessageCallback")]
#[inline(always)]
fn set_serial_message_callback(&self) -> unsafe extern "C" fn(callback: FnSerialMessageCallback) {
self.0.setSerialMessageCallback.expect("setSerialMessageCallback")
}
}


Expand Down Expand Up @@ -518,5 +544,12 @@ pub mod api {
fn convert_date_time_to_epoch(&self) -> unsafe extern "C" fn(datetime: *mut PDDateTime) -> u32 {
*sys::api!(system.convertDateTimeToEpoch)
}

/// Equivalent to [`sys::ffi::playdate_sys::setSerialMessageCallback`]
#[doc(alias = "sys::ffi::playdate_sys::setSerialMessageCallback")]
#[inline(always)]
fn set_serial_message_callback(&self) -> unsafe extern "C" fn(callback: FnSerialMessageCallback) {
*sys::api!(system.setSerialMessageCallback)
}
}
}
26 changes: 26 additions & 0 deletions api/system/src/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use core::ffi::c_char;
use core::ffi::CStr;
use alloc::string::String;
use erased_set::ErasedSendSet;

pub static mut STORE: Option<ErasedSendSet> = None;

pub fn init_store() {
if unsafe { STORE.is_none() } {
unsafe { STORE = Some(ErasedSendSet::new()) }
}
}

pub fn clean_store() {
if let Some(true) = unsafe { STORE.as_mut() }.map(|store| store.is_empty()) {
unsafe { STORE = None }
}
}

pub unsafe extern "C" fn proxy_serial_message_callback<F: 'static + Send + FnMut(String)>(data: *const c_char) {
let data = CStr::from_ptr(data as _).to_string_lossy().into_owned();
let f = unsafe { STORE.as_mut() }.map(|store| store.remove::<F>())
.flatten();
f.map(|mut f| f(data)).or_else(|| panic!("missed callback"));
clean_store();
}
Loading