embedded wakeup calls callback

This commit is contained in:
Mitchell Hashimoto
2023-02-18 15:18:44 -08:00
parent 7a368da099
commit 7af516e7e6
2 changed files with 23 additions and 10 deletions

View File

@ -67,8 +67,8 @@ class GhosttyState: ObservableObject {
// Create our "runtime" config. The "runtime" is the configuration that ghostty // Create our "runtime" config. The "runtime" is the configuration that ghostty
// uses to interface with the application runtime environment. // uses to interface with the application runtime environment.
var runtime_cfg = ghostty_runtime_config_s( var runtime_cfg = ghostty_runtime_config_s(
userdata: nil, userdata: Unmanaged.passUnretained(self).toOpaque(),
wakeup_cb: { userdata in GhosttyState.wakeup() }) wakeup_cb: { userdata in GhosttyState.wakeup(userdata) })
// Create the ghostty app. // Create the ghostty app.
guard let app = ghostty_app_new(&runtime_cfg, cfg) else { guard let app = ghostty_app_new(&runtime_cfg, cfg) else {
@ -81,8 +81,19 @@ class GhosttyState: ObservableObject {
self.readiness = .ready self.readiness = .ready
} }
static func wakeup() { func appTick() {
// TODO guard let app = self.app else { return }
ghostty_app_tick(app)
}
static func wakeup(_ userdata: UnsafeMutableRawPointer?) {
let state = Unmanaged<GhosttyState>.fromOpaque(userdata!).takeUnretainedValue()
// Wakeup can be called from any thread so we schedule the app tick
// from the main thread. There is probably some improvements we can make
// to coalesce multiple ticks but I don't think it matters from a performance
// standpoint since we don't do this much.
DispatchQueue.main.async { state.appTick() }
} }
deinit { deinit {

View File

@ -31,8 +31,10 @@ pub const App = struct {
wakeup: *const fn (?*anyopaque) callconv(.C) void, wakeup: *const fn (?*anyopaque) callconv(.C) void,
}; };
pub fn init(_: Options) !App { opts: Options,
return .{};
pub fn init(opts: Options) !App {
return .{ .opts = opts };
} }
pub fn terminate(self: App) void { pub fn terminate(self: App) void {
@ -40,7 +42,7 @@ pub const App = struct {
} }
pub fn wakeup(self: App) !void { pub fn wakeup(self: App) !void {
_ = self; self.opts.wakeup(self.opts.userdata);
} }
pub fn wait(self: App) !void { pub fn wait(self: App) !void {