diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d46748584..abf4a1633 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -132,7 +132,7 @@ jobs: run: nix develop -c zig build -Dapp-runtime=none test - name: Test GTK Build - run: nix develop -c zig build -Dapp-runtime=gtk + run: nix develop -c zig build -Dapp-runtime=gtk -Dgtk-libadwaita=true - name: Test GLFW Build run: nix develop -c zig build -Dapp-runtime=glfw diff --git a/.gitignore b/.gitignore index cc549e044..8e7a003ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,9 @@ +*~ +.*.swp +.swp +*.log .DS_Store -.vscode +.vscode/ .direnv/ .flatpak-builder/ zig-cache/ diff --git a/build.zig b/build.zig index cc914dca2..fc5af6c9b 100644 --- a/build.zig +++ b/build.zig @@ -39,6 +39,7 @@ var flatpak: bool = false; var app_runtime: apprt.Runtime = .none; var renderer_impl: renderer.Impl = .opengl; var font_backend: font.Backend = .freetype; +var libadwaita: bool = false; pub fn build(b: *std.Build) !void { const optimize = b.standardOptimizeOption(.{}); @@ -87,6 +88,12 @@ pub fn build(b: *std.Build) !void { "The app runtime to use. Not all values supported on all platforms.", ) orelse apprt.Runtime.default(target); + libadwaita = b.option( + bool, + "gtk-libadwaita", + "Enables the use of libadwaita when using the gtk rendering backend.", + ) orelse true; + renderer_impl = b.option( renderer.Impl, "renderer", @@ -203,6 +210,7 @@ pub fn build(b: *std.Build) !void { exe_options.addOption(apprt.Runtime, "app_runtime", app_runtime); exe_options.addOption(font.Backend, "font_backend", font_backend); exe_options.addOption(renderer.Impl, "renderer", renderer_impl); + exe_options.addOption(bool, "libadwaita", libadwaita); // Exe if (exe_) |exe| { @@ -669,6 +677,10 @@ fn addDeps( .@"enable-freetype" = true, .@"enable-coretext" = font_backend.hasCoretext(), }); + const ziglyph_dep = b.dependency("ziglyph", .{ + .target = step.target, + .optimize = step.optimize, + }); // Wasm we do manually since it is such a different build. if (step.target.getCpuArch() == .wasm32) { @@ -716,6 +728,7 @@ fn addDeps( step.addModule("xev", libxev_dep.module("xev")); step.addModule("pixman", pixman_dep.module("pixman")); step.addModule("utf8proc", utf8proc_dep.module("utf8proc")); + step.addModule("ziglyph", ziglyph_dep.module("ziglyph")); // Mac Stuff if (step.target.isDarwin()) { @@ -814,6 +827,7 @@ fn addDeps( .gtk => { step.linkSystemLibrary2("gtk4", dynamic_link_opts); + if (libadwaita) step.linkSystemLibrary2("adwaita-1", dynamic_link_opts); }, } } diff --git a/build.zig.zon b/build.zig.zon index bfca4e766..765e3c0ee 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,6 +4,10 @@ .paths = .{""}, .dependencies = .{ // Zig libs + .glfw = .{ + .url = "https://pkg.machengine.org/glfw/14181bd28aa65915262ac3b4549bbd2dc70bbaa5.tar.gz", + .hash = "1220c6bb317ae3948b95161b9706777dde0509e72e8b35b62b92898aef801897d904", + }, .libxev = .{ .url = "https://github.com/mitchellh/libxev/archive/5ecbc871f3bfa80fb7bf0fa853866cb93b99bc18.tar.gz", .hash = "1220416854e424601ecc9814afb461a5dc9cf95db5917d82f794594a58ffc723b82c", @@ -20,10 +24,9 @@ .url = "https://github.com/mitchellh/zig-js/archive/60ac42ab137461cdba2b38cc6c5e16376470aae6.tar.gz", .hash = "1220319b42fbc0116f3f198343256018e9f1da9483cef259201afe4ebab0ce0d8f6a", }, - - .glfw = .{ - .url = "https://pkg.machengine.org/glfw/14181bd28aa65915262ac3b4549bbd2dc70bbaa5.tar.gz", - .hash = "1220c6bb317ae3948b95161b9706777dde0509e72e8b35b62b92898aef801897d904", + .ziglyph = .{ + .url = "https://codeberg.org/dude_the_builder/ziglyph/archive/v0.11.1.tar.gz", + .hash = "1220dee955839b7f267c1bb21e0ee60888c08f408c30f0722b243cabcc8cce8b7508", }, // C libs diff --git a/macos/Ghostty.xcodeproj/project.pbxproj b/macos/Ghostty.xcodeproj/project.pbxproj index 98fd5e58d..153b146a9 100644 --- a/macos/Ghostty.xcodeproj/project.pbxproj +++ b/macos/Ghostty.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 8503D7C72A549C66006CFF3D /* FullScreenHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8503D7C62A549C66006CFF3D /* FullScreenHandler.swift */; }; 857F63812A5E64F200CA4815 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 857F63802A5E64F200CA4815 /* MainMenu.xib */; }; + A51B78472AF4B58B00F3EDB9 /* TerminalWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51B78462AF4B58B00F3EDB9 /* TerminalWindow.swift */; }; A5278A9B2AA05B2600CD3039 /* Ghostty.Input.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5278A9A2AA05B2600CD3039 /* Ghostty.Input.swift */; }; A53426352A7DA53D00EBB7A2 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A53426342A7DA53D00EBB7A2 /* AppDelegate.swift */; }; A535B9DA299C569B0017E2E4 /* ErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A535B9D9299C569B0017E2E4 /* ErrorView.swift */; }; @@ -45,6 +46,7 @@ /* Begin PBXFileReference section */ 8503D7C62A549C66006CFF3D /* FullScreenHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullScreenHandler.swift; sourceTree = ""; }; 857F63802A5E64F200CA4815 /* MainMenu.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MainMenu.xib; sourceTree = ""; }; + A51B78462AF4B58B00F3EDB9 /* TerminalWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalWindow.swift; sourceTree = ""; }; A5278A9A2AA05B2600CD3039 /* Ghostty.Input.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Ghostty.Input.swift; sourceTree = ""; }; A53426342A7DA53D00EBB7A2 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; A535B9D9299C569B0017E2E4 /* ErrorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorView.swift; sourceTree = ""; }; @@ -171,6 +173,7 @@ A596309F2AEF6AEB00D64628 /* TerminalManager.swift */, A596309B2AEE1C9E00D64628 /* TerminalController.swift */, A596309D2AEE1D6C00D64628 /* TerminalView.swift */, + A51B78462AF4B58B00F3EDB9 /* TerminalWindow.swift */, A535B9D9299C569B0017E2E4 /* ErrorView.swift */, ); path = Terminal; @@ -313,6 +316,7 @@ A59630A22AF0415000D64628 /* Ghostty.TerminalSplit.swift in Sources */, A5FEB3002ABB69450068369E /* main.swift in Sources */, A55B7BB829B6F53A0055DE60 /* Package.swift in Sources */, + A51B78472AF4B58B00F3EDB9 /* TerminalWindow.swift in Sources */, A55B7BB629B6F47F0055DE60 /* AppState.swift in Sources */, A5CEAFDC29B8009000646FDA /* SplitView.swift in Sources */, A5CDF1932AAF9E0800513312 /* ConfigurationErrorsController.swift in Sources */, diff --git a/macos/Sources/Features/Terminal/Terminal.xib b/macos/Sources/Features/Terminal/Terminal.xib index f0f0937db..b9e9a12b6 100644 --- a/macos/Sources/Features/Terminal/Terminal.xib +++ b/macos/Sources/Features/Terminal/Terminal.xib @@ -13,7 +13,7 @@ - + diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index 6a4ae7afb..8ad1509d5 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -28,13 +28,6 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele /// True when an alert is active so we don't overlap multiple. private var alert: NSAlert? = nil - /// The style mask to use for the new window - private var styleMask: NSWindow.StyleMask { - var mask: NSWindow.StyleMask = [.resizable, .closable, .miniaturizable] - if (ghostty.windowDecorations) { mask.insert(.titled) } - return mask - } - init(_ ghostty: Ghostty.AppState, withBaseConfig base: Ghostty.SurfaceConfiguration? = nil) { self.ghostty = ghostty super.init(window: nil) @@ -76,7 +69,9 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele override func windowDidLoad() { guard let window = window else { return } - window.styleMask = self.styleMask + + // If window decorations are disabled, remove our title + if (!ghostty.windowDecorations) { window.styleMask.remove(.titled) } // Terminals typically operate in sRGB color space and macOS defaults // to "native" which is typically P3. There is a lot more resources diff --git a/macos/Sources/Features/Terminal/TerminalWindow.swift b/macos/Sources/Features/Terminal/TerminalWindow.swift new file mode 100644 index 000000000..9a74a7e2b --- /dev/null +++ b/macos/Sources/Features/Terminal/TerminalWindow.swift @@ -0,0 +1,8 @@ +import Cocoa + +class TerminalWindow: NSWindow { + // Both of these must be true for windows without decorations to be able to + // still become key/main and receive events. + override var canBecomeKey: Bool { return true } + override var canBecomeMain: Bool { return true } +} diff --git a/nix/devshell.nix b/nix/devshell.nix index 78d8de6fb..5804cceb5 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -28,6 +28,7 @@ , freetype , glib , gtk4 +, libadwaita , harfbuzz , libpng , libGL @@ -59,6 +60,7 @@ let libXi libXrandr + libadwaita gtk4 glib ]; @@ -120,6 +122,7 @@ in mkShell rec { libXrandr # Only needed for GTK builds + libadwaita gtk4 glib ]; diff --git a/src/Surface.zig b/src/Surface.zig index 31a032647..9b89f6618 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -2169,19 +2169,19 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool .reload_config => try self.app.reloadConfig(self.rt_app), - .csi => |data| { - // We need to send the CSI sequence as a single write request. + .csi, .esc => |data| { + // We need to send the CSI/ESC sequence as a single write request. // If you split it across two then the shell can interpret it // as two literals. var buf: [128]u8 = undefined; - const full_data = try std.fmt.bufPrint(&buf, "\x1b[{s}", .{data}); + const full_data = try std.fmt.bufPrint(&buf, "\x1b{s}{s}", .{if(action==.csi)"["else"", data}); _ = self.io_thread.mailbox.push(try termio.Message.writeReq( self.alloc, full_data, ), .{ .forever = {} }); try self.io_thread.wakeup.notify(); - // CSI triggers a scroll. + // CSI/ESC triggers a scroll. { self.renderer_state.mutex.lock(); defer self.renderer_state.mutex.unlock(); diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index bf96e1e6c..be4103f77 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -353,6 +353,7 @@ pub const Surface = struct { win.setScrollCallback(scrollCallback); win.setCursorPosCallback(cursorPosCallback); win.setMouseButtonCallback(mouseButtonCallback); + win.setDropCallback(dropCallback); // Build our result self.* = .{ @@ -964,4 +965,39 @@ pub const Surface = struct { return; }; } + + fn dropCallback(window: glfw.Window, paths: [][*:0]const u8) void { + const tracy = trace(@src()); + defer tracy.end(); + + const surface = window.getUserPointer(CoreSurface) orelse return; + + var list = std.ArrayList(u8).init(surface.alloc); + defer list.deinit(); + + for (paths) |path| { + const path_slice = std.mem.span(path); + + // preallocate worst case of escaping every char + space + list.ensureTotalCapacity(path_slice.len * 2 + 1) catch |err| { + log.err("error in drop callback err={}", .{err}); + return; + }; + + const writer = list.writer(); + for (path_slice) |c| { + if (std.mem.indexOfScalar(u8, "\\ ()[]{}<>\"'`!#$&;|*?\t", c)) |_| { + writer.print("\\{c}", .{c}) catch unreachable; // memory preallocated + } else writer.writeByte(c) catch unreachable; // same here + } + writer.writeByte(' ') catch unreachable; // separate paths + + surface.textCallback(list.items) catch |err| { + log.err("error in drop callback err={}", .{err}); + return; + }; + + list.clearRetainingCapacity(); // avoid unnecessary reallocations + } + } }; diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index 47e69eba3..5e5c143b4 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -19,6 +19,7 @@ const internal_os = @import("../../os/main.zig"); const Config = configpkg.Config; const CoreApp = @import("../../App.zig"); const CoreSurface = @import("../../Surface.zig"); +const build_options = @import("build_options"); const Surface = @import("Surface.zig"); const Window = @import("Window.zig"); @@ -52,6 +53,9 @@ running: bool = true, pub fn init(core_app: *CoreApp, opts: Options) !App { _ = opts; + // Initialize libadwaita + if (build_options.libadwaita) c.adw_init(); + // Load our configuration var config = try Config.load(core_app.alloc); errdefer config.deinit(); @@ -63,6 +67,18 @@ pub fn init(core_app: *CoreApp, opts: Options) !App { } } + // Set the style based on our configuration file + if (build_options.libadwaita) { + c.adw_style_manager_set_color_scheme( + c.adw_style_manager_get_default(), + switch (config.@"window-theme") { + .system => c.ADW_COLOR_SCHEME_PREFER_LIGHT, + .dark => c.ADW_COLOR_SCHEME_FORCE_DARK, + .light => c.ADW_COLOR_SCHEME_FORCE_LIGHT, + }, + ); + } + // The "none" cursor is used for hiding the cursor const cursor_none = c.gdk_cursor_new_from_name("none", null); errdefer if (cursor_none) |cursor| c.g_object_unref(cursor); diff --git a/src/apprt/gtk/c.zig b/src/apprt/gtk/c.zig index c5952d321..7eb19233f 100644 --- a/src/apprt/gtk/c.zig +++ b/src/apprt/gtk/c.zig @@ -1,5 +1,6 @@ const c = @cImport({ @cInclude("gtk/gtk.h"); + @cInclude("libadwaita-1/adwaita.h"); }); pub usingnamespace c; diff --git a/src/config/Config.zig b/src/config/Config.zig index dc47fd5bd..ba29ed6f2 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -321,6 +321,8 @@ command: ?[]const u8 = null, /// is removed, and the key will be sent through to the child command /// if it is printable. /// - "csi:text" - Send a CSI sequence. i.e. "csi:A" sends "cursor up". +/// - "esc:text" - Send an Escape sequence. i.e. "esc:d" deletes to the +/// end of the word to the right. /// /// Some notes for the action: /// @@ -378,7 +380,7 @@ keybind: Keybinds = .{}, /// also be set to "light" or "dark" to force a specific theme regardless /// of the system settings. /// -/// This is currently only supported on macOS. +/// This is currently only supported on macOS and linux. @"window-theme": WindowTheme = .system, /// The initial window size. This size is in terminal grid cells by default. diff --git a/src/font/shaper/run.zig b/src/font/shaper/run.zig index 94a5da3f0..9af4a9b4c 100644 --- a/src/font/shaper/run.zig +++ b/src/font/shaper/run.zig @@ -1,6 +1,7 @@ const std = @import("std"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; +const ziglyph = @import("ziglyph"); const font = @import("../main.zig"); const shape = @import("../shape.zig"); const terminal = @import("../../terminal/main.zig"); @@ -109,12 +110,20 @@ pub const RunIterator = struct { // presentation format must be directly adjacent to the codepoint. var it = self.row.codepointIterator(j); if (it.next()) |cp| { - if (cp == 0xFE0E) break :p font.Presentation.text; - if (cp == 0xFE0F) break :p font.Presentation.emoji; + if (cp == 0xFE0E) break :p .text; + if (cp == 0xFE0F) break :p .emoji; } break :p null; - } else null; + } else emoji: { + // If we're not a grapheme, our individual char could be + // an emoji so we want to check if we expect emoji presentation. + if (ziglyph.emoji.isEmojiPresentation(@intCast(cell.char))) { + break :emoji .emoji; + } + + break :emoji .text; + }; // If our cursor is on this line then we break the run around the // cursor. This means that any row with a cursor has at least diff --git a/src/input/Binding.zig b/src/input/Binding.zig index c326b21b0..9198e2f53 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -117,6 +117,9 @@ pub const Action = union(enum) { /// without the CSI header ("ESC ]" or "\x1b]"). csi: []const u8, + /// Send an ESC sequence. + esc: []const u8, + /// Send data to the pty depending on whether cursor key mode is /// enabled ("application") or disabled ("normal"). cursor_key: CursorKey, @@ -665,6 +668,12 @@ test "parse: action with string" { try testing.expect(binding.action == .csi); try testing.expectEqualStrings("A", binding.action.csi); } + // parameter + { + const binding = try parse("a=esc:A"); + try testing.expect(binding.action == .esc); + try testing.expectEqualStrings("A", binding.action.esc); + } } test "parse: action with enum" {