From 01afa293c8e03b2f62fd9d486e116e924bd42658 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 18 Jan 2024 08:55:03 -0800 Subject: [PATCH] apprt/embedded: fix initialization on macOS --- include/ghostty.h | 21 +++++++++++++++++---- macos/Sources/Ghostty/SurfaceView.swift | 5 ++++- src/apprt/embedded.zig | 6 +++--- src/renderer/Metal.zig | 4 +++- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index 63668a4c3..9035d9c0a 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -31,6 +31,7 @@ typedef void *ghostty_inspector_t; // Enums are up top so we can reference them later. typedef enum { + GHOSTTY_PLATFORM_INVALID, GHOSTTY_PLATFORM_MACOS, } ghostty_platform_e; @@ -354,8 +355,17 @@ typedef struct { } ghostty_error_s; typedef struct { - void *userdata; void *nsview; +} ghostty_platform_macos_s; + +typedef union { + ghostty_platform_macos_s macos; +} ghostty_platform_u; + +typedef struct { + ghostty_platform_e platform_tag; + ghostty_platform_u platform; + void *userdata; double scale_factor; uint16_t font_size; const char *working_directory; @@ -476,9 +486,6 @@ uintptr_t ghostty_surface_pwd(ghostty_surface_t, char *, uintptr_t); ghostty_inspector_t ghostty_surface_inspector(ghostty_surface_t); void ghostty_inspector_free(ghostty_surface_t); -bool ghostty_inspector_metal_init(ghostty_inspector_t, void *); -void ghostty_inspector_metal_render(ghostty_inspector_t, void *, void *); -bool ghostty_inspector_metal_shutdown(ghostty_inspector_t); void ghostty_inspector_set_focus(ghostty_inspector_t, bool); void ghostty_inspector_set_content_scale(ghostty_inspector_t, double, double); void ghostty_inspector_set_size(ghostty_inspector_t, uint32_t, uint32_t); @@ -488,6 +495,12 @@ void ghostty_inspector_mouse_scroll(ghostty_inspector_t, double, double, ghostty void ghostty_inspector_key(ghostty_inspector_t, ghostty_input_action_e, ghostty_input_key_e, ghostty_input_mods_e); void ghostty_inspector_text(ghostty_inspector_t, const char *); +#ifdef __APPLE__ +bool ghostty_inspector_metal_init(ghostty_inspector_t, void *); +void ghostty_inspector_metal_render(ghostty_inspector_t, void *, void *); +bool ghostty_inspector_metal_shutdown(ghostty_inspector_t); +#endif + // APIs I'd like to get rid of eventually but are still needed for now. // Don't use these unless you know what you're doing. void ghostty_set_window_background_blur(ghostty_surface_t, void *); diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index 22c3c3e7d..2d320eab1 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -277,8 +277,11 @@ extension Ghostty { /// in the returned struct is only valid as long as this struct is retained. func ghosttyConfig(view: SurfaceView) -> ghostty_surface_config_s { var config = ghostty_surface_config_new() + config.platform_tag = GHOSTTY_PLATFORM_MACOS + config.platform = ghostty_platform_u(macos: ghostty_platform_macos_s( + nsview: Unmanaged.passUnretained(view).toOpaque() + )) config.userdata = Unmanaged.passUnretained(view).toOpaque() - config.nsview = Unmanaged.passUnretained(view).toOpaque() config.scale_factor = NSScreen.main!.backingScaleFactor if let fontSize = fontSize { config.font_size = fontSize } diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index db3fe422e..44cc044ce 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -283,14 +283,14 @@ pub const Surface = struct { inspector: ?*Inspector = null, pub const Options = extern struct { - /// Userdata passed to some of the callbacks. - userdata: ?*anyopaque = null, - /// The platform that this surface is being initialized for and /// the associated platform-specific configuration. platform_tag: c_int = 0, platform: Platform.C = undefined, + /// Userdata passed to some of the callbacks. + userdata: ?*anyopaque = null, + /// The scale factor of the screen. scale_factor: f64 = 1, diff --git a/src/renderer/Metal.zig b/src/renderer/Metal.zig index c881e7be0..7a689f6be 100644 --- a/src/renderer/Metal.zig +++ b/src/renderer/Metal.zig @@ -466,7 +466,9 @@ pub fn finalizeSurfaceInit(self: *const Metal, surface: *apprt.Surface) !void { }, apprt.embedded => .{ - .view = surface.nsview, + .view = switch (surface.platform) { + .macos => |v| v.nsview, + }, .scaleFactor = @floatCast(surface.content_scale.x), },