From 913131c8f1bce61008d5e95eab933ef848a93054 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 22 Feb 2023 14:52:38 -0800 Subject: [PATCH] rename more stuff --- src/DevMode.zig | 22 ++++---- src/Surface.zig | 74 ++++++++++++-------------- src/apprt.zig | 2 +- src/apprt/glfw.zig | 16 +++--- src/apprt/structs.zig | 4 +- src/apprt/{Surface.zig => surface.zig} | 16 +++--- src/renderer/OpenGL.zig | 6 +-- src/renderer/Options.zig | 6 +-- src/termio/Exec.zig | 16 +++--- src/termio/Options.zig | 6 +-- 10 files changed, 84 insertions(+), 84 deletions(-) rename src/apprt/{Surface.zig => surface.zig} (79%) diff --git a/src/DevMode.zig b/src/DevMode.zig index 931c04dea..bd28366ca 100644 --- a/src/DevMode.zig +++ b/src/DevMode.zig @@ -29,8 +29,8 @@ visible: bool = false, /// Our app config config: ?*const Config = null, -/// The window we're tracking. -window: ?*Surface = null, +/// The surface we're tracking. +surface: ?*Surface = null, /// Update the state associated with the dev mode. This should generally /// only be called paired with a render since it otherwise wastes CPU @@ -86,20 +86,20 @@ pub fn update(self: *const DevMode) !void { } } - if (self.window) |window| { + if (self.surface) |surface| { if (imgui.collapsingHeader("Font Manager", null, .{})) { - imgui.text("Glyphs: %d", window.font_group.glyphs.count()); + imgui.text("Glyphs: %d", surface.font_group.glyphs.count()); imgui.sameLine(0, -1); helpMarker("The number of glyphs loaded and rendered into a " ++ "font atlas currently."); - const Renderer = @TypeOf(window.renderer); + const Renderer = @TypeOf(surface.renderer); if (imgui.treeNode("Atlas: Greyscale", .{ .default_open = true })) { defer imgui.treePop(); - const atlas = &window.font_group.atlas_greyscale; + const atlas = &surface.font_group.atlas_greyscale; const tex = switch (Renderer) { - renderer.OpenGL => @intCast(usize, window.renderer.texture.id), - renderer.Metal => @ptrToInt(window.renderer.texture_greyscale.value), + renderer.OpenGL => @intCast(usize, surface.renderer.texture.id), + renderer.Metal => @ptrToInt(surface.renderer.texture_greyscale.value), else => @compileError("renderer unsupported, add it!"), }; try self.atlasInfo(atlas, tex); @@ -107,10 +107,10 @@ pub fn update(self: *const DevMode) !void { if (imgui.treeNode("Atlas: Color (Emoji)", .{ .default_open = true })) { defer imgui.treePop(); - const atlas = &window.font_group.atlas_color; + const atlas = &surface.font_group.atlas_color; const tex = switch (Renderer) { - renderer.OpenGL => @intCast(usize, window.renderer.texture_color.id), - renderer.Metal => @ptrToInt(window.renderer.texture_color.value), + renderer.OpenGL => @intCast(usize, surface.renderer.texture_color.id), + renderer.Metal => @ptrToInt(surface.renderer.texture_color.value), else => @compileError("renderer unsupported, add it!"), }; try self.atlasInfo(atlas, tex); diff --git a/src/Surface.zig b/src/Surface.zig index 04da5d0a8..6329a8760 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -34,7 +34,7 @@ const DevMode = @import("DevMode.zig"); const App = @import("App.zig"); const internal_os = @import("os/main.zig"); -const log = std.log.scoped(.window); +const log = std.log.scoped(.surface); // The renderer implementation to use. const Renderer = renderer.Renderer; @@ -56,7 +56,7 @@ font_size: font.face.DesiredSize, /// Imgui context imgui_ctx: if (DevMode.enabled) *imgui.Context else void, -/// The renderer for this window. +/// The renderer for this surface. renderer: Renderer, /// The render state @@ -94,7 +94,7 @@ config: *const Config, /// like such as "control-v" will write a "v" even if they're intercepted. ignore_char: bool = false, -/// Mouse state for the window. +/// Mouse state for the surface. const Mouse = struct { /// The last tracked mouse button state by button. click_state: [input.MouseButton.max]input.MouseButtonState = .{.release} ** input.MouseButton.max, @@ -109,7 +109,7 @@ const Mouse = struct { /// The starting xpos/ypos of the left click. Note that if scrolling occurs, /// these will point to different "cells", but the xpos/ypos will stay - /// stable during scrolling relative to the window. + /// stable during scrolling relative to the surface. left_click_xpos: f64 = 0, left_click_ypos: f64 = 0, @@ -283,7 +283,7 @@ pub fn init( .left = padding_x, }; - // Create our terminal grid with the initial window size + // Create our terminal grid with the initial size var renderer_impl = try Renderer.init(alloc, .{ .config = config, .font_group = font_group, @@ -291,15 +291,15 @@ pub fn init( .explicit = padding, .balance = config.@"window-padding-balance", }, - .window_mailbox = .{ .window = self, .app = app.mailbox }, + .surface_mailbox = .{ .surface = self, .app = app.mailbox }, }); errdefer renderer_impl.deinit(); // Calculate our grid size based on known dimensions. - const window_size = try rt_surface.getSize(); + const surface_size = try rt_surface.getSize(); const screen_size: renderer.ScreenSize = .{ - .width = window_size.width, - .height = window_size.height, + .width = surface_size.width, + .height = surface_size.height, }; const grid_size = renderer.GridSize.init( screen_size.subPadding(padding), @@ -328,7 +328,7 @@ pub fn init( .renderer_state = &self.renderer_state, .renderer_wakeup = render_thread.wakeup, .renderer_mailbox = render_thread.mailbox, - .window_mailbox = .{ .window = self, .app = app.mailbox }, + .surface_mailbox = .{ .surface = self, .app = app.mailbox }, }); errdefer io.deinit(); @@ -336,10 +336,10 @@ pub fn init( var io_thread = try termio.Thread.init(alloc, &self.io); errdefer io_thread.deinit(); - // True if this window is hosting devmode. We only host devmode on - // the first window since imgui is not threadsafe. We need to do some + // True if this surface is hosting devmode. We only host devmode on + // the first surface since imgui is not threadsafe. We need to do some // work to make DevMode work with multiple threads. - const host_devmode = DevMode.enabled and DevMode.instance.window == null; + const host_devmode = DevMode.enabled and DevMode.instance.surface == null; self.* = .{ .alloc = alloc, @@ -383,15 +383,15 @@ pub fn init( }, null); // Call our size callback which handles all our retina setup - // Note: this shouldn't be necessary and when we clean up the window + // Note: this shouldn't be necessary and when we clean up the surface // init stuff we should get rid of this. But this is required because // sizeCallback does retina-aware stuff we don't do here and don't want // to duplicate. - try self.sizeCallback(window_size); + try self.sizeCallback(surface_size); // Load imgui. This must be done LAST because it has to be done after // all our GLFW setup is complete. - if (DevMode.enabled and DevMode.instance.window == null) { + if (DevMode.enabled and DevMode.instance.surface == null) { const dev_io = try imgui.IO.get(); dev_io.cval().IniFilename = "ghostty_dev_mode.ini"; @@ -406,14 +406,14 @@ pub fn init( const style = try imgui.Style.get(); style.colorsDark(); - // Add our window to the instance if it isn't set. - DevMode.instance.window = self; + // Add our surface to the instance if it isn't set. + DevMode.instance.surface = self; // Let our renderer setup try renderer_impl.initDevMode(rt_surface); } - // Give the renderer one more opportunity to finalize any window + // Give the renderer one more opportunity to finalize any surface // setup on the main thread prior to spinning up the rendering thread. try renderer_impl.finalizeSurfaceInit(rt_surface); @@ -434,7 +434,7 @@ pub fn init( self.io_thr.setName("io") catch {}; } -pub fn destroy(self: *Surface) void { +pub fn deinit(self: *Surface) void { // Stop rendering thread { self.renderer_thread.stop.notify() catch |err| @@ -445,12 +445,12 @@ pub fn destroy(self: *Surface) void { self.renderer.threadEnter(self.rt_surface) catch unreachable; // If we are devmode-owning, clean that up. - if (DevMode.enabled and DevMode.instance.window == self) { + if (DevMode.enabled and DevMode.instance.surface == self) { // Let our renderer clean up self.renderer.deinitDevMode(); - // Clear the window - DevMode.instance.window = null; + // Clear the surface + DevMode.instance.surface = null; // Uninitialize imgui self.imgui_ctx.destroy(); @@ -471,19 +471,15 @@ pub fn destroy(self: *Surface) void { self.io_thread.deinit(); self.io.deinit(); - self.rt_surface.deinit(); - self.font_group.deinit(self.alloc); self.font_lib.deinit(); self.alloc.destroy(self.font_group); self.alloc.destroy(self.renderer_state.mutex); - - self.alloc.destroy(self); } /// Called from the app thread to handle mailbox messages to our specific -/// window. +/// surface. pub fn handleMessage(self: *Surface, msg: Message) !void { switch (msg) { .set_title => |*v| { @@ -657,7 +653,7 @@ fn queueRender(self: *const Surface) !void { try self.renderer_thread.wakeup.notify(); } -pub fn sizeCallback(self: *Surface, size: apprt.WindowSize) !void { +pub fn sizeCallback(self: *Surface, size: apprt.SurfaceSize) !void { const tracy = trace(@src()); defer tracy.end(); @@ -1034,8 +1030,8 @@ pub fn scrollCallback(self: *Surface, xoff: f64, yoff: f64) !void { const tracy = trace(@src()); defer tracy.end(); - // If our dev mode window is visible then we always schedule a render on - // cursor move because the cursor might touch our windows. + // If our dev mode surface is visible then we always schedule a render on + // cursor move because the cursor might touch our surfaces. if (DevMode.enabled and DevMode.instance.visible) { try self.queueRender(); @@ -1081,8 +1077,8 @@ fn mouseReport( mods: input.Mods, pos: apprt.CursorPos, ) !void { - // TODO: posToViewport currently clamps to the window boundary, - // do we want to not report mouse events at all outside the window? + // TODO: posToViewport currently clamps to the surface boundary, + // do we want to not report mouse events at all outside the surface? // Depending on the event, we may do nothing at all. switch (self.io.terminal.modes.mouse_event) { @@ -1278,8 +1274,8 @@ pub fn mouseButtonCallback( const tracy = trace(@src()); defer tracy.end(); - // If our dev mode window is visible then we always schedule a render on - // cursor move because the cursor might touch our windows. + // If our dev mode surface is visible then we always schedule a render on + // cursor move because the cursor might touch our surfaces. if (DevMode.enabled and DevMode.instance.visible) { try self.queueRender(); @@ -1395,8 +1391,8 @@ pub fn cursorPosCallback( const tracy = trace(@src()); defer tracy.end(); - // If our dev mode window is visible then we always schedule a render on - // cursor move because the cursor might touch our windows. + // If our dev mode surface is visible then we always schedule a render on + // cursor move because the cursor might touch our surfaces. if (DevMode.enabled and DevMode.instance.visible) { try self.queueRender(); @@ -1608,8 +1604,8 @@ fn dragLeftClickSingle( fn posToViewport(self: Surface, xpos: f64, ypos: f64) terminal.point.Viewport { // xpos and ypos can be negative if while dragging, the user moves the - // mouse off the window. Likewise, they can be larger than our window - // width if the user drags out of the window positively. + // mouse off the surface. Likewise, they can be larger than our surface + // width if the user drags out of the surface positively. return .{ .x = if (xpos < 0) 0 else x: { // Our cell is the mouse divided by cell width diff --git a/src/apprt.zig b/src/apprt.zig index 24b85b00b..01aea84e8 100644 --- a/src/apprt.zig +++ b/src/apprt.zig @@ -17,7 +17,7 @@ pub const glfw = @import("apprt/glfw.zig"); pub const gtk = @import("apprt/gtk.zig"); pub const browser = @import("apprt/browser.zig"); pub const embedded = @import("apprt/embedded.zig"); -pub const surface = @import("apprt/Surface.zig"); +pub const surface = @import("apprt/surface.zig"); /// The implementation to use for the app runtime. This is comptime chosen /// so that every build has exactly one application runtime implementation. diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index 2150711d5..beb560622 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -83,6 +83,7 @@ pub const App = struct { // Create the surface -- because windows are surfaces for glfw. try surface.init(self); + errdefer surface.deinit(); } fn glfwErrorCallback(code: glfw.ErrorCode, desc: [:0]const u8) void { @@ -232,12 +233,15 @@ pub const Surface = struct { // Initialize our surface now that we have the stable pointer. try self.core_surface.init(app.app, app.app.config, self); - errdefer self.core_surface.destroy(); + errdefer self.core_surface.deinit(); } pub fn deinit(self: *Surface) void { - var tabgroup_opt: if (builtin.target.isDarwin()) ?objc.Object else void = undefined; - if (comptime builtin.target.isDarwin()) { + // First clean up our core surface so that all the rendering and IO stop. + self.core_surface.deinit(); + + var tabgroup_opt: if (App.Darwin.enabled) ?objc.Object else void = undefined; + if (App.Darwin.enabled) { const nswindow = objc.Object.fromId(glfwNative.getCocoaWindow(self.window).?); const tabgroup = nswindow.getProperty(objc.Object, "tabGroup"); @@ -286,7 +290,7 @@ pub const Surface = struct { /// Note: this interface is not good, we should redo it if we plan /// to use this more. i.e. you can't set max width but no max height, /// or no mins. - pub fn setSizeLimits(self: *Surface, min: apprt.WindowSize, max_: ?apprt.WindowSize) !void { + pub fn setSizeLimits(self: *Surface, min: apprt.SurfaceSize, max_: ?apprt.SurfaceSize) !void { self.window.setSizeLimits(.{ .width = min.width, .height = min.height, @@ -308,9 +312,9 @@ pub const Surface = struct { /// Returns the size of the window in pixels. The pixel size may /// not match screen coordinate size but we should be able to convert /// back and forth using getContentScale. - pub fn getSize(self: *const Surface) !apprt.WindowSize { + pub fn getSize(self: *const Surface) !apprt.SurfaceSize { const size = self.window.getFramebufferSize(); - return apprt.WindowSize{ .width = size.width, .height = size.height }; + return apprt.SurfaceSize{ .width = size.width, .height = size.height }; } /// Returns the cursor position in scaled pixels relative to the diff --git a/src/apprt/structs.zig b/src/apprt/structs.zig index 2cdb8be9e..9ca2434d4 100644 --- a/src/apprt/structs.zig +++ b/src/apprt/structs.zig @@ -6,8 +6,8 @@ pub const ContentScale = struct { y: f32, }; -/// The size of the window in pixels. -pub const WindowSize = struct { +/// The size of the surface in pixels. +pub const SurfaceSize = struct { width: u32, height: u32, }; diff --git a/src/apprt/Surface.zig b/src/apprt/surface.zig similarity index 79% rename from src/apprt/Surface.zig rename to src/apprt/surface.zig index fff956504..f76a4ea06 100644 --- a/src/apprt/Surface.zig +++ b/src/apprt/surface.zig @@ -3,13 +3,13 @@ const Surface = @import("../Surface.zig"); const renderer = @import("../renderer.zig"); const termio = @import("../termio.zig"); -/// The message types that can be sent to a single window. +/// The message types that can be sent to a single surface. pub const Message = union(enum) { /// Represents a write request. Magic number comes from the max size /// we want this union to be. pub const WriteReq = termio.MessageData(u8, 256); - /// Set the title of the window. + /// Set the title of the surface. /// TODO: we should change this to a "WriteReq" style structure in /// the termio message so that we can more efficiently send strings /// of any length @@ -25,25 +25,25 @@ pub const Message = union(enum) { clipboard_write: WriteReq, }; -/// A window mailbox. +/// A surface mailbox. pub const Mailbox = struct { - window: *Surface, + surface: *Surface, app: *App.Mailbox, - /// Send a message to the window. + /// Send a message to the surface. pub fn push(self: Mailbox, msg: Message, timeout: App.Mailbox.Timeout) App.Mailbox.Size { // Surface message sending is actually implemented on the app - // thread, so we have to rewrap the message with our window + // thread, so we have to rewrap the message with our surface // pointer and send it to the app thread. const result = self.app.push(.{ .surface_message = .{ - .surface = self.window, + .surface = self.surface, .message = msg, }, }, timeout); // Wake up our app loop - self.window.app.wakeup(); + self.surface.app.wakeup(); return result; } diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 951835998..3c3554fdc 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -89,7 +89,7 @@ focused: bool, padding: renderer.Options.Padding, /// The mailbox for communicating with the window. -window_mailbox: apprt.surface.Mailbox, +surface_mailbox: apprt.surface.Mailbox, /// The raw structure that maps directly to the buffer sent to the vertex shader. /// This must be "extern" so that the field order is not reordered by the @@ -311,7 +311,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !OpenGL { null, .focused = true, .padding = options.padding, - .window_mailbox = options.window_mailbox, + .surface_mailbox = options.surface_mailbox, }; } @@ -504,7 +504,7 @@ pub fn setFontSize(self: *OpenGL, size: font.face.DesiredSize) !void { self.cell_size = new_cell_size; // Notify the window that the cell size changed. - _ = self.window_mailbox.push(.{ + _ = self.surface_mailbox.push(.{ .cell_size = new_cell_size, }, .{ .forever = {} }); } diff --git a/src/renderer/Options.zig b/src/renderer/Options.zig index 0687d4e99..c8b58c8c7 100644 --- a/src/renderer/Options.zig +++ b/src/renderer/Options.zig @@ -14,12 +14,12 @@ font_group: *font.GroupCache, /// Padding options for the viewport. padding: Padding, -/// The mailbox for sending the window messages. This is only valid +/// The mailbox for sending the surface messages. This is only valid /// once the thread has started and should not be used outside of the thread. -window_mailbox: apprt.surface.Mailbox, +surface_mailbox: apprt.surface.Mailbox, pub const Padding = struct { - // Explicit padding options, in pixels. The windowing thread is + // Explicit padding options, in pixels. The surface thread is // expected to convert points to pixels for a given DPI. explicit: renderer.Padding, diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index a6a56e90b..86dcf71e8 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -51,8 +51,8 @@ renderer_wakeup: xev.Async, /// The mailbox for notifying the renderer of things. renderer_mailbox: *renderer.Thread.Mailbox, -/// The mailbox for communicating with the window. -window_mailbox: apprt.surface.Mailbox, +/// The mailbox for communicating with the surface. +surface_mailbox: apprt.surface.Mailbox, /// The cached grid size whenever a resize is called. grid_size: renderer.GridSize, @@ -83,7 +83,7 @@ pub fn init(alloc: Allocator, opts: termio.Options) !Exec { .renderer_state = opts.renderer_state, .renderer_wakeup = opts.renderer_wakeup, .renderer_mailbox = opts.renderer_mailbox, - .window_mailbox = opts.window_mailbox, + .surface_mailbox = opts.surface_mailbox, .grid_size = opts.grid_size, .data = null, }; @@ -131,7 +131,7 @@ pub fn threadEnter(self: *Exec, thread: *termio.Thread) !ThreadData { .ev = ev_data_ptr, .terminal = &self.terminal, .grid_size = &self.grid_size, - .window_mailbox = self.window_mailbox, + .surface_mailbox = self.surface_mailbox, }, }, }; @@ -638,7 +638,7 @@ const StreamHandler = struct { alloc: Allocator, grid_size: *renderer.GridSize, terminal: *terminal.Terminal, - window_mailbox: apprt.surface.Mailbox, + surface_mailbox: apprt.surface.Mailbox, /// This is set to true when a message was written to the writer /// mailbox. This can be used by callers to determine if they need @@ -983,7 +983,7 @@ const StreamHandler = struct { std.mem.copy(u8, &buf, title); buf[title.len] = 0; - _ = self.window_mailbox.push(.{ + _ = self.surface_mailbox.push(.{ .set_title = buf, }, .{ .forever = {} }); } @@ -995,14 +995,14 @@ const StreamHandler = struct { // Get clipboard contents if (data.len == 1 and data[0] == '?') { - _ = self.window_mailbox.push(.{ + _ = self.surface_mailbox.push(.{ .clipboard_read = kind, }, .{ .forever = {} }); return; } // Write clipboard contents - _ = self.window_mailbox.push(.{ + _ = self.surface_mailbox.push(.{ .clipboard_write = try apprt.surface.Message.WriteReq.init( self.alloc, data, diff --git a/src/termio/Options.zig b/src/termio/Options.zig index df90a8369..06af6b4d0 100644 --- a/src/termio/Options.zig +++ b/src/termio/Options.zig @@ -15,7 +15,7 @@ screen_size: renderer.ScreenSize, config: *const Config, /// The render state. The IO implementation can modify anything here. The -/// window thread will setup the initial "terminal" pointer but the IO impl +/// surface thread will setup the initial "terminal" pointer but the IO impl /// is free to change that if that is useful (i.e. doing some sort of dual /// terminal implementation.) renderer_state: *renderer.State, @@ -27,5 +27,5 @@ renderer_wakeup: xev.Async, /// The mailbox for renderer messages. renderer_mailbox: *renderer.Thread.Mailbox, -/// The mailbox for sending the window messages. -window_mailbox: apprt.surface.Mailbox, +/// The mailbox for sending the surface messages. +surface_mailbox: apprt.surface.Mailbox,