wip: macos

This commit is contained in:
Kalle Carlbark 2024-07-10 22:39:45 +02:00
parent 54c43bab5d
commit 045955d5db
No known key found for this signature in database
4 changed files with 33 additions and 17 deletions

View file

@ -49,6 +49,7 @@ pub fn main() !void {
}; };
const shell = env.get("SHELL") orelse "bash"; const shell = env.get("SHELL") orelse "bash";
const argv = [_][]const u8{shell}; const argv = [_][]const u8{shell};
std.debug.print("executing: {s}\n", .{shell});
var vt = try vaxis.widgets.Terminal.init( var vt = try vaxis.widgets.Terminal.init(
alloc, alloc,
&argv, &argv,
@ -62,7 +63,6 @@ pub fn main() !void {
var redraw: bool = false; var redraw: bool = false;
while (true) { while (true) {
std.debug.print("inside while loop before resize\n", .{}); std.debug.print("inside while loop before resize\n", .{});
std.time.sleep(8 * std.time.ns_per_ms);
// try vt events first // try vt events first
while (vt.tryEvent()) |event| { while (vt.tryEvent()) |event| {
std.debug.print("inside stryEventloop \n", .{}); std.debug.print("inside stryEventloop \n", .{});

View file

@ -20,12 +20,13 @@ pty: Pty,
const TIOCSCTTY = if (builtin.os.tag == .macos) 536900705 else c.TIOCSCTTY; const TIOCSCTTY = if (builtin.os.tag == .macos) 536900705 else c.TIOCSCTTY;
const TIOCSWINSZ = if (builtin.os.tag == .macos) 2148037735 else c.TIOCSWINSZ; const TIOCSWINSZ = if (builtin.os.tag == .macos) 2148037735 else c.TIOCSWINSZ;
const TIOCGWINSZ = if (builtin.os.tag == .macos) 1074295912 else c.TIOCGWINSZ; const TIOCGWINSZ = if (builtin.os.tag == .macos) 1074295912 else c.TIOCGWINSZ;
extern "c" fn setsid() std.c.pid_t; // extern "c" fn setsid() std.c.pid_t;
const c = struct { const c = struct {
usingnamespace switch (builtin.os.tag) { usingnamespace switch (builtin.os.tag) {
.macos => @cImport({ .macos => @cImport({
@cInclude("sys/ioctl.h"); // ioctl and constants @cInclude("sys/ioctl.h"); // ioctl and constants
@cInclude("util.h"); // openpty() @cInclude("util.h");
@cInclude("unistd.h"); // openpty()
}), }),
else => @cImport({ else => @cImport({
@cInclude("sys/ioctl.h"); // ioctl and constants @cInclude("sys/ioctl.h"); // ioctl and constants
@ -47,15 +48,27 @@ pub fn spawn(self: *Command, allocator: std.mem.Allocator) !void {
const pid = try std.posix.fork(); const pid = try std.posix.fork();
if (pid == 0) { if (pid == 0) {
// we are the child std.posix.close(self.pty.pty);
_ = std.os.linux.setsid(); std.debug.print("inside child\n", .{});
_ = setsid(); if (c.setsid() != 0) return error.SetSid;
// if (setsid() < 0) return error.ProcessGroupFailed; std.debug.print("setting up io for tty\n", .{});
// set the controlling terminal
var u: c_uint = std.posix.STDIN_FILENO; var u: c_uint = std.posix.STDIN_FILENO;
if (c.ioctl(self.pty.tty, TIOCSCTTY, @intFromPtr(&u)) != 0) return error.IoctlError; if (c.ioctl(self.pty.tty, TIOCSCTTY, @intFromPtr(&u)) != 0) return error.IoctlError;
// switch (builtin.os.tag) {
// .linux => {},
// .ios, .macos => {
// // Mac doesn't support dup3 so we use dup2. We purposely clear
// // CLO_ON_EXEC for this fd.
// const flags = try posix.fcntl(self.pty.tty, posix.F.GETFD, 0);
// if (flags & posix.FD_CLOEXEC != 0) {
// _ = try posix.fcntl(self.pty.tty, posix.F.SETFD, flags & ~@as(u32, posix.FD_CLOEXEC));
// }
// },
// else => @compileError("unsupported platform"),
// }
// set up io // set up io
try posix.dup2(self.pty.tty, std.posix.STDIN_FILENO); try posix.dup2(self.pty.tty, std.posix.STDIN_FILENO);
try posix.dup2(self.pty.tty, std.posix.STDOUT_FILENO); try posix.dup2(self.pty.tty, std.posix.STDOUT_FILENO);
@ -69,17 +82,17 @@ pub fn spawn(self: *Command, allocator: std.mem.Allocator) !void {
} }
// exec // exec
const err = std.posix.execvpeZ(argv_buf.ptr[0].?, argv_buf.ptr, envp); _ = std.posix.execvpeZ(argv_buf.ptr[0].?, argv_buf.ptr, envp) catch null;
_ = err catch {};
} else { //return error.ExecFailed;
std.debug.print("we are not child: {d}\n", .{pid}); // const err = std.posix.execvpeZ("/bin/zsh", "/bin/zsh", envp);
// posix.close(self.pty.tty); // _ = err catch {};
_ = posix.waitpid(pid, 0);
} }
// we are the parent // we are the parent
self.pid = @intCast(pid); self.pid = @intCast(pid);
_ = posix.waitpid(pid, 0);
std.debug.print("waitpid in parent\n", .{});
if (!Terminal.global_sigchild_installed) { if (!Terminal.global_sigchild_installed) {
Terminal.global_sigchild_installed = true; Terminal.global_sigchild_installed = true;
var act = posix.Sigaction{ var act = posix.Sigaction{

View file

@ -33,7 +33,7 @@ tty: posix.fd_t,
pub fn init() !Pty { pub fn init() !Pty {
switch (builtin.os.tag) { switch (builtin.os.tag) {
.linux => return openPtyLinux(), .linux => return openPtyLinux(),
.macos => return openPtyMacos2(), .macos => return openPtyMacos(),
else => @compileError("unsupported os"), else => @compileError("unsupported os"),
} }
} }

View file

@ -94,6 +94,9 @@ pub fn init(
unicode: *const vaxis.Unicode, unicode: *const vaxis.Unicode,
opts: Options, opts: Options,
) !Terminal { ) !Terminal {
if (opts.initial_working_directory) |pwd| {
if (!std.fs.path.isAbsolute(pwd)) return error.InvalidWorkingDirectory;
}
const pty = try Pty.init(); const pty = try Pty.init();
try pty.setSize(opts.winsize); try pty.setSize(opts.winsize);
std.debug.print("set size done\n", .{}); std.debug.print("set size done\n", .{});