aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/libghostty-vt/pkg/macos/video/display_link.zig
blob: dc984422d016f25d04758a035fb5563241cf0f2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const c = @import("c.zig").c;

pub const DisplayLink = opaque {
    pub const Error = error{
        CreationFailed,
        InvalidOperation,
    };

    pub fn createWithActiveCGDisplays() Error!*DisplayLink {
        var result: ?*DisplayLink = null;
        if (c.CVDisplayLinkCreateWithActiveCGDisplays(
            @ptrCast(&result),
        ) != c.kCVReturnSuccess)
            return error.CreationFailed;

        return result orelse error.CreationFailed;
    }

    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;
    }

    pub fn setCurrentCGDisplay(
        self: *DisplayLink,
        display_id: c.CGDirectDisplayID,
    ) Error!void {
        if (c.CVDisplayLinkSetCurrentCGDisplay(
            @ptrCast(self),
            display_id,
        ) != c.kCVReturnSuccess)
            return error.InvalidOperation;
    }

    // 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 Userdata: type,
        comptime callbackFn: *const fn (*DisplayLink, ?*Userdata) void,
        userinfo: ?*Userdata,
    ) 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,
                        @ptrCast(@alignCast(inner_userinfo)),
                    );
                    return c.kCVReturnSuccess;
                }
            }).callback),
            userinfo,
        ) != c.kCVReturnSuccess)
            return error.InvalidOperation;
    }
};