From ba29bf759b4ec8ff1761915e466de52d82168cc5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 16 Aug 2024 14:35:10 -0700 Subject: [PATCH] lots more yeeting --- src/apprt/embedded.zig | 222 +++++++++++++++++++++-------------------- src/font/main.zig | 16 +-- src/main_c.zig | 8 +- src/main_wasm.zig | 12 ++- 4 files changed, 137 insertions(+), 121 deletions(-) diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 9b7fcccb7..dc91871de 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -1427,6 +1427,14 @@ pub const CAPI = struct { cell_height_px: u32, }; + // Reference the conditional exports based on target platform + // so they're included in the C API. + comptime { + if (builtin.target.isDarwin()) { + _ = Darwin; + } + } + /// Create a new app. export fn ghostty_app_new( opts: *const apprt.runtime.App.Options, @@ -1830,8 +1838,112 @@ pub const CAPI = struct { ptr.freeInspector(); } - // Inspector Metal APIs are only available on Apple systems - usingnamespace if (builtin.target.isDarwin()) struct { + export fn ghostty_inspector_set_size(ptr: *Inspector, w: u32, h: u32) void { + ptr.updateSize(w, h); + } + + export fn ghostty_inspector_set_content_scale(ptr: *Inspector, x: f64, y: f64) void { + ptr.updateContentScale(x, y); + } + + export fn ghostty_inspector_mouse_button( + ptr: *Inspector, + action: input.MouseButtonState, + button: input.MouseButton, + mods: c_int, + ) void { + ptr.mouseButtonCallback( + action, + button, + @bitCast(@as( + input.Mods.Backing, + @truncate(@as(c_uint, @bitCast(mods))), + )), + ); + } + + export fn ghostty_inspector_mouse_pos(ptr: *Inspector, x: f64, y: f64) void { + ptr.cursorPosCallback(x, y); + } + + export fn ghostty_inspector_mouse_scroll( + ptr: *Inspector, + x: f64, + y: f64, + scroll_mods: c_int, + ) void { + ptr.scrollCallback( + x, + y, + @bitCast(@as(u8, @truncate(@as(c_uint, @bitCast(scroll_mods))))), + ); + } + + export fn ghostty_inspector_key( + ptr: *Inspector, + action: input.Action, + key: input.Key, + c_mods: c_int, + ) void { + ptr.keyCallback( + action, + key, + @bitCast(@as( + input.Mods.Backing, + @truncate(@as(c_uint, @bitCast(c_mods))), + )), + ) catch |err| { + log.err("error processing key event err={}", .{err}); + return; + }; + } + + export fn ghostty_inspector_text( + ptr: *Inspector, + str: [*:0]const u8, + ) void { + ptr.textCallback(std.mem.sliceTo(str, 0)); + } + + export fn ghostty_inspector_set_focus(ptr: *Inspector, focused: bool) void { + ptr.focusCallback(focused); + } + + /// Sets the window background blur on macOS to the desired value. + /// I do this in Zig as an extern function because I don't know how to + /// call these functions in Swift. + /// + /// This uses an undocumented, non-public API because this is what + /// every terminal appears to use, including Terminal.app. + export fn ghostty_set_window_background_blur( + app: *App, + window: *anyopaque, + ) void { + // This is only supported on macOS + if (comptime builtin.target.os.tag != .macos) return; + + const config = app.config; + + // Do nothing if we don't have background transparency enabled + if (config.@"background-opacity" >= 1.0) return; + + // Do nothing if our blur value is zero + if (config.@"background-blur-radius" == 0) return; + + const nswindow = objc.Object.fromId(window); + _ = CGSSetWindowBackgroundBlurRadius( + CGSDefaultConnectionForThread(), + nswindow.msgSend(usize, objc.sel("windowNumber"), .{}), + @intCast(config.@"background-blur-radius"), + ); + } + + /// See ghostty_set_window_background_blur + extern "c" fn CGSSetWindowBackgroundBlurRadius(*anyopaque, usize, c_int) i32; + extern "c" fn CGSDefaultConnectionForThread() *anyopaque; + + // Darwin-only C APIs. + const Darwin = struct { export fn ghostty_surface_set_display_id(ptr: *Surface, display_id: u32) void { const surface = &ptr.core_surface; _ = surface.renderer_thread.mailbox.push( @@ -1984,109 +2096,5 @@ pub const CAPI = struct { ptr.backend = null; } } - } else struct {}; - - export fn ghostty_inspector_set_size(ptr: *Inspector, w: u32, h: u32) void { - ptr.updateSize(w, h); - } - - export fn ghostty_inspector_set_content_scale(ptr: *Inspector, x: f64, y: f64) void { - ptr.updateContentScale(x, y); - } - - export fn ghostty_inspector_mouse_button( - ptr: *Inspector, - action: input.MouseButtonState, - button: input.MouseButton, - mods: c_int, - ) void { - ptr.mouseButtonCallback( - action, - button, - @bitCast(@as( - input.Mods.Backing, - @truncate(@as(c_uint, @bitCast(mods))), - )), - ); - } - - export fn ghostty_inspector_mouse_pos(ptr: *Inspector, x: f64, y: f64) void { - ptr.cursorPosCallback(x, y); - } - - export fn ghostty_inspector_mouse_scroll( - ptr: *Inspector, - x: f64, - y: f64, - scroll_mods: c_int, - ) void { - ptr.scrollCallback( - x, - y, - @bitCast(@as(u8, @truncate(@as(c_uint, @bitCast(scroll_mods))))), - ); - } - - export fn ghostty_inspector_key( - ptr: *Inspector, - action: input.Action, - key: input.Key, - c_mods: c_int, - ) void { - ptr.keyCallback( - action, - key, - @bitCast(@as( - input.Mods.Backing, - @truncate(@as(c_uint, @bitCast(c_mods))), - )), - ) catch |err| { - log.err("error processing key event err={}", .{err}); - return; - }; - } - - export fn ghostty_inspector_text( - ptr: *Inspector, - str: [*:0]const u8, - ) void { - ptr.textCallback(std.mem.sliceTo(str, 0)); - } - - export fn ghostty_inspector_set_focus(ptr: *Inspector, focused: bool) void { - ptr.focusCallback(focused); - } - - /// Sets the window background blur on macOS to the desired value. - /// I do this in Zig as an extern function because I don't know how to - /// call these functions in Swift. - /// - /// This uses an undocumented, non-public API because this is what - /// every terminal appears to use, including Terminal.app. - export fn ghostty_set_window_background_blur( - app: *App, - window: *anyopaque, - ) void { - // This is only supported on macOS - if (comptime builtin.target.os.tag != .macos) return; - - const config = app.config; - - // Do nothing if we don't have background transparency enabled - if (config.@"background-opacity" >= 1.0) return; - - // Do nothing if our blur value is zero - if (config.@"background-blur-radius" == 0) return; - - const nswindow = objc.Object.fromId(window); - _ = CGSSetWindowBackgroundBlurRadius( - CGSDefaultConnectionForThread(), - nswindow.msgSend(usize, objc.sel("windowNumber"), .{}), - @intCast(config.@"background-blur-radius"), - ); - } - - /// See ghostty_set_window_background_blur - extern "c" fn CGSSetWindowBackgroundBlurRadius(*anyopaque, usize, c_int) i32; - extern "c" fn CGSDefaultConnectionForThread() *anyopaque; + }; }; diff --git a/src/font/main.zig b/src/font/main.zig index 3bbc091fc..4c0d206b3 100644 --- a/src/font/main.zig +++ b/src/font/main.zig @@ -27,13 +27,15 @@ pub const Descriptor = discovery.Descriptor; pub const Discover = discovery.Discover; pub const Library = library.Library; -/// If we're targeting wasm then we export some wasm APIs. -pub usingnamespace if (builtin.target.isWasm()) struct { - pub usingnamespace Atlas.Wasm; - pub usingnamespace DeferredFace.Wasm; - pub usingnamespace face.web_canvas.Wasm; - pub usingnamespace shape.web_canvas.Wasm; -} else struct {}; +// If we're targeting wasm then we export some wasm APIs. +comptime { + if (builtin.target.isWasm()) { + _ = Atlas.Wasm; + _ = DeferredFace.Wasm; + _ = face.web_canvas.Wasm; + _ = shape.web_canvas.Wasm; + } +} /// Build options pub const options: struct { diff --git a/src/main_c.zig b/src/main_c.zig index 007273172..4cbca4899 100644 --- a/src/main_c.zig +++ b/src/main_c.zig @@ -23,8 +23,12 @@ comptime { /// Global options so we can log. This is identical to main. pub const std_options = main.std_options; -pub usingnamespace @import("config.zig").CAPI; -pub usingnamespace apprt.runtime.CAPI; +comptime { + // These structs need to be referenced so the `export` functions + // are truly exported by the C API lib. + _ = @import("config.zig").CAPI; + _ = apprt.runtime.CAPI; +} /// ghostty_info_s const Info = extern struct { diff --git a/src/main_wasm.zig b/src/main_wasm.zig index b275b4626..bffe5e4b7 100644 --- a/src/main_wasm.zig +++ b/src/main_wasm.zig @@ -3,11 +3,13 @@ const std = @import("std"); const builtin = @import("builtin"); -pub usingnamespace @import("os/wasm.zig"); -pub usingnamespace @import("font/main.zig"); -pub usingnamespace @import("terminal/main.zig"); -pub usingnamespace @import("config.zig").Wasm; -pub usingnamespace @import("App.zig").Wasm; +comptime { + _ = @import("os/wasm.zig"); + _ = @import("font/main.zig"); + _ = @import("terminal/main.zig"); + _ = @import("config.zig").Wasm; + _ = @import("App.zig").Wasm; +} pub const std_options = struct { // Set our log level. We try to get as much logging as possible but in