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
// uses to interface with the application runtime environment.
var runtime_cfg = ghostty_runtime_config_s(
userdata: nil,
wakeup_cb: { userdata in GhosttyState.wakeup() })
userdata: Unmanaged.passUnretained(self).toOpaque(),
wakeup_cb: { userdata in GhosttyState.wakeup(userdata) })
// Create the ghostty app.
guard let app = ghostty_app_new(&runtime_cfg, cfg) else {
@ -81,8 +81,19 @@ class GhosttyState: ObservableObject {
self.readiness = .ready
}
static func wakeup() {
// TODO
func appTick() {
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 {

View File

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