barkfetch/src/macos.zig

80 lines
2.4 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", .{});
const buf = try std.fmt.allocPrint(allocator, "\n\t\t\tOS: {s} {d}.{d}.{d}", .{ version_to_name(koko), koko.major, koko.minor, koko.patch });
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 kaka = std.mem.span(@ptrCast(name.sysname));
const buf = try std.fmt.allocPrint(allocator, "\n\t\t\tKernel: {s} {s}", .{ kaka, name.version });
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,
};