barkfetch/src/macos.zig

81 lines
2.5 KiB
Zig
Raw Normal View History

2024-08-20 07:59:09 +02:00
const objc = @import("objc");
const std = @import("std");
const utsname = @cImport({
@cInclude("sys/utsname.h");
});
const OsVersion = struct {
name: []const u8,
major: i64,
};
pub fn macosVersionAtLeast(major: i64, minor: i64, patch: i64) bool {
// Get the objc class from the runtime
const NSProcessInfo = objc.getClass("NSProcessInfo").?;
// Call a class method with no arguments that returns another objc object.
const info = NSProcessInfo.msgSend(objc.Object, "processInfo", .{});
// var NSOperatingSystemVersion = objc.getClass("NSOperatingSystemVersion").?;
// const os = info.msgSend(objc.Object, "")
// Call an instance method that returns a boolean and takes a single
// argument.
return info.msgSend(bool, "isOperatingSystemAtLeastVersion:", .{
NSOperatingSystemVersion{ .major = major, .minor = minor, .patch = patch },
});
}
pub fn macos_version(allocator: std.mem.Allocator) ![]const u8 {
const NSProcessInfo = objc.getClass("NSProcessInfo").?;
// Call a class method with no arguments that returns another objc object.
const info = NSProcessInfo.msgSend(objc.Object, "processInfo", .{});
const koko = info.msgSend(NSOperatingSystemVersion, "operatingSystemVersion", .{});
2024-08-21 22:45:53 +02:00
const buf = try std.fmt.allocPrint(allocator, " OS: macOS {s} {d}.{d}.{d}\n", .{ version_to_name(koko), koko.major, koko.minor, koko.patch });
2024-08-20 07:59:09 +02:00
return buf;
}
pub fn macos_kernel_version(allocator: std.mem.Allocator) ![]const u8 {
var name: utsname.struct_utsname = undefined;
if (utsname.uname(&name) != 0) {
return error.UnameFailed;
}
const sysname = std.mem.sliceTo(name.sysname[0..], 0);
const version = std.mem.sliceTo(name.release[0..], 0);
2024-08-20 07:59:09 +02:00
2024-08-21 22:45:53 +02:00
const buf = try std.fmt.allocPrint(allocator, " Kernel: {s} {s}\n", .{ sysname, version });
2024-08-20 07:59:09 +02:00
return buf;
}
fn version_to_name(version: NSOperatingSystemVersion) []const u8 {
switch (version.major) {
15 => return "Sequoia",
14 => return "Sonoma",
13 => return "Ventura",
12 => return "Monterey",
11 => return "Big Sur",
10 => {
switch (version.minor) {
15 => return "Catalina",
14 => return "Mojave",
else => return "Unknown",
}
},
else => return "Unknown",
}
}
/// This extern struct matches the Cocoa headers for layout.
const NSOperatingSystemVersion = extern struct {
major: i64,
minor: i64,
patch: i64,
};