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

@ -40,7 +40,7 @@ class GhosttyState: ObservableObject {
/// The ghostty app instance. We only have one of these for the entire app, although I guess /// The ghostty app instance. We only have one of these for the entire app, although I guess
/// in theory you can have multiple... I don't know why you would... /// in theory you can have multiple... I don't know why you would...
var app: ghostty_app_t? = nil var app: ghostty_app_t? = nil
init() { init() {
// Initialize ghostty global state. This happens once per process. // Initialize ghostty global state. This happens once per process.
guard ghostty_init() == GHOSTTY_SUCCESS else { guard ghostty_init() == GHOSTTY_SUCCESS else {
@ -67,9 +67,9 @@ 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 {
GhosttyApp.logger.critical("ghostty_app_new failed") GhosttyApp.logger.critical("ghostty_app_new failed")
@ -77,12 +77,23 @@ class GhosttyState: ObservableObject {
return return
} }
self.app = app self.app = app
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 {