mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-10-05 09:26:32 +00:00
pkg/macos: add CoreVide for DisplayLink
This commit is contained in:
@@ -28,6 +28,7 @@ pub fn build(b: *std.Build) !void {
|
|||||||
lib.linkFramework("CoreFoundation");
|
lib.linkFramework("CoreFoundation");
|
||||||
lib.linkFramework("CoreGraphics");
|
lib.linkFramework("CoreGraphics");
|
||||||
lib.linkFramework("CoreText");
|
lib.linkFramework("CoreText");
|
||||||
|
lib.linkFramework("CoreVideo");
|
||||||
if (!target.isNative()) try apple_sdk.addPaths(b, lib);
|
if (!target.isNative()) try apple_sdk.addPaths(b, lib);
|
||||||
|
|
||||||
b.installArtifact(lib);
|
b.installArtifact(lib);
|
||||||
|
@@ -2,6 +2,7 @@ pub const foundation = @import("foundation.zig");
|
|||||||
pub const graphics = @import("graphics.zig");
|
pub const graphics = @import("graphics.zig");
|
||||||
pub const os = @import("os.zig");
|
pub const os = @import("os.zig");
|
||||||
pub const text = @import("text.zig");
|
pub const text = @import("text.zig");
|
||||||
|
pub const video = @import("video.zig");
|
||||||
|
|
||||||
test {
|
test {
|
||||||
@import("std").testing.refAllDecls(@This());
|
@import("std").testing.refAllDecls(@This());
|
||||||
|
6
pkg/macos/video.zig
Normal file
6
pkg/macos/video.zig
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
pub const c = @import("video/c.zig");
|
||||||
|
pub usingnamespace @import("video/display_link.zig");
|
||||||
|
|
||||||
|
test {
|
||||||
|
@import("std").testing.refAllDecls(@This());
|
||||||
|
}
|
3
pkg/macos/video/c.zig
Normal file
3
pkg/macos/video/c.zig
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub usingnamespace @cImport({
|
||||||
|
@cInclude("CoreVideo/CoreVideo.h");
|
||||||
|
});
|
71
pkg/macos/video/display_link.zig
Normal file
71
pkg/macos/video/display_link.zig
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const c = @import("c.zig");
|
||||||
|
|
||||||
|
pub const DisplayLink = opaque {
|
||||||
|
pub const Error = error{
|
||||||
|
InvalidOperation,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn createWithActiveCGDisplays() Allocator.Error!*DisplayLink {
|
||||||
|
var result: ?*DisplayLink = null;
|
||||||
|
if (c.CVDisplayLinkCreateWithActiveCGDisplays(
|
||||||
|
@ptrCast(&result),
|
||||||
|
) != c.kCVReturnSuccess)
|
||||||
|
return error.OutOfMemory;
|
||||||
|
|
||||||
|
return result orelse error.OutOfMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(self: *DisplayLink) void {
|
||||||
|
c.CVDisplayLinkRelease(@ptrCast(self));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start(self: *DisplayLink) Error!void {
|
||||||
|
if (c.CVDisplayLinkStart(@ptrCast(self)) != c.kCVReturnSuccess)
|
||||||
|
return error.InvalidOperation;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stop(self: *DisplayLink) Error!void {
|
||||||
|
if (c.CVDisplayLinkStop(@ptrCast(self)) != c.kCVReturnSuccess)
|
||||||
|
return error.InvalidOperation;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn isRunning(self: *DisplayLink) bool {
|
||||||
|
return c.CVDisplayLinkIsRunning(@ptrCast(self)) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: this purposely throws away a ton of arguments I didn't need.
|
||||||
|
// It would be trivial to refactor this into Zig types and properly
|
||||||
|
// pass this through.
|
||||||
|
pub fn setOutputCallback(
|
||||||
|
self: *DisplayLink,
|
||||||
|
comptime callbackFn: *const fn (*DisplayLink, ?*anyopaque) void,
|
||||||
|
userinfo: ?*anyopaque,
|
||||||
|
) Error!void {
|
||||||
|
if (c.CVDisplayLinkSetOutputCallback(
|
||||||
|
@ptrCast(self),
|
||||||
|
@ptrCast(&(struct {
|
||||||
|
fn callback(
|
||||||
|
displayLink: *DisplayLink,
|
||||||
|
inNow: *const c.CVTimeStamp,
|
||||||
|
inOutputTime: *const c.CVTimeStamp,
|
||||||
|
flagsIn: c.CVOptionFlags,
|
||||||
|
flagsOut: *c.CVOptionFlags,
|
||||||
|
inner_userinfo: ?*anyopaque,
|
||||||
|
) callconv(.C) c.CVReturn {
|
||||||
|
_ = inNow;
|
||||||
|
_ = inOutputTime;
|
||||||
|
_ = flagsIn;
|
||||||
|
_ = flagsOut;
|
||||||
|
|
||||||
|
callbackFn(displayLink, inner_userinfo);
|
||||||
|
return c.kCVReturnSuccess;
|
||||||
|
}
|
||||||
|
}).callback),
|
||||||
|
userinfo,
|
||||||
|
) != c.kCVReturnSuccess)
|
||||||
|
return error.InvalidOperation;
|
||||||
|
}
|
||||||
|
};
|
Reference in New Issue
Block a user