cli/gtk: clean ups and better error handling in GTK new-window IPC

This commit is contained in:
Jeffrey C. Ollie
2025-07-14 12:06:42 -05:00
parent 7d05f4c0c5
commit f5eb413c31
2 changed files with 17 additions and 3 deletions

View File

@ -41,6 +41,12 @@ pub fn openNewWindow(alloc: Allocator, target: apprt.ipc.Target, value: apprt.ip
.ReleaseFast, .ReleaseSmall => .{ "com.mitchellh.ghostty", "/com/mitchellh/ghostty" }, .ReleaseFast, .ReleaseSmall => .{ "com.mitchellh.ghostty", "/com/mitchellh/ghostty" },
}, },
}; };
defer {
switch (target) {
.class => alloc.free(object_path),
.detect => {},
}
}
if (gio.Application.idIsValid(bus_name.ptr) == 0) { if (gio.Application.idIsValid(bus_name.ptr) == 0) {
try stderr.print("D-Bus bus name is not valid: {s}\n", .{bus_name}); try stderr.print("D-Bus bus name is not valid: {s}\n", .{bus_name});
@ -80,7 +86,7 @@ pub fn openNewWindow(alloc: Allocator, target: apprt.ipc.Target, value: apprt.ip
// Initialize our builder to build up our parameters // Initialize our builder to build up our parameters
var builder: glib.VariantBuilder = undefined; var builder: glib.VariantBuilder = undefined;
builder.init(builder_type); builder.init(builder_type);
errdefer builder.unref(); errdefer builder.clear();
// action // action
if (value.arguments == null) { if (value.arguments == null) {
@ -96,6 +102,7 @@ pub fn openNewWindow(alloc: Allocator, target: apprt.ipc.Target, value: apprt.ip
var parameters: glib.VariantBuilder = undefined; var parameters: glib.VariantBuilder = undefined;
parameters.init(av); parameters.init(av);
errdefer parameters.clear();
if (value.arguments) |arguments| { if (value.arguments) |arguments| {
// If `-e` was specified on the command line, the first // If `-e` was specified on the command line, the first
@ -108,6 +115,7 @@ pub fn openNewWindow(alloc: Allocator, target: apprt.ipc.Target, value: apprt.ip
var command: glib.VariantBuilder = undefined; var command: glib.VariantBuilder = undefined;
command.init(as); command.init(as);
errdefer command.clear();
for (arguments) |argument| { for (arguments) |argument| {
command.add("s", argument.ptr); command.add("s", argument.ptr);

View File

@ -5,6 +5,9 @@ const Allocator = std.mem.Allocator;
const assert = std.debug.assert; const assert = std.debug.assert;
pub const Errors = error{ pub const Errors = error{
/// The IPC failed. If a function returns this error, it's expected that
/// an a more specific error message will have been written to stderr (or
/// otherwise shown to the user in an appropriate way).
IPCFailed, IPCFailed,
}; };
@ -38,8 +41,6 @@ pub const Target = union(Key) {
return .{ return .{
.key = @as(Key, self), .key = @as(Key, self),
.value = switch (self) { .value = switch (self) {
.release => .{ .release = {} },
.debug => .{ .debug = {} },
.class => |class| .{ .class = class.ptr }, .class => |class| .{ .class = class.ptr },
.detect => .{ .detect = {} }, .detect => .{ .detect = {} },
}, },
@ -68,6 +69,11 @@ pub const Action = union(enum) {
new_window: NewWindow, new_window: NewWindow,
pub const NewWindow = struct { pub const NewWindow = struct {
/// A list of command arguments to launch in the new window. If this is
/// `null` the command configured in the config or the user's default
/// shell should be launched.
///
/// It is an error for this to be non-`null`, but zero length.
arguments: ?[][:0]const u8, arguments: ?[][:0]const u8,
pub const C = extern struct { pub const C = extern struct {