diff --git a/glfw/src/Monitor.zig b/glfw/src/Monitor.zig new file mode 100644 index 00000000..6d1503c5 --- /dev/null +++ b/glfw/src/Monitor.zig @@ -0,0 +1,42 @@ +//! Monitor type and related functions + +const std = @import("std"); +const mem = std.mem; +const testing = std.testing; +const c = @cImport(@cInclude("GLFW/glfw3.h")); + +pub const Error = @import("errors.zig").Error; +const getError = @import("errors.zig").getError; + +const Monitor = @This(); + +handle: *c.GLFWmonitor, + +/// Returns the currently connected monitors. +/// +/// This function returns a slice of all currently connected monitors. The primary monitor is +/// always first. If no monitors were found, this function returns an empty slice. +/// +/// The returned slice memory is owned by the caller. The underlying handles are owned by GLFW, and +/// are valid until the monitor configuration changes or the `glfw.terminate` is called. +/// +/// @thread_safety This function must only be called from the main thread. +/// +/// see also: monitor_monitors, monitor_event, glfw.monitor.getPrimary +pub fn getAll(allocator: *mem.Allocator) ![]Monitor { + var count: c_int = 0; + const monitors = c.glfwGetMonitors(&count); + + const slice = allocator.alloc(Monitor, count); + var i = 0; + while (i < count) : (i += 1) { + slice[i] = Monitor{ .handle = monitors[i] }; + } + return slice; +} + +test "getAll" { + const allocator = testing.allocator; + const monitors = try getAll(allocator); + defer allocator.free(monitors); +} diff --git a/glfw/src/main.zig b/glfw/src/main.zig index fd6426ce..1355ec9f 100644 --- a/glfw/src/main.zig +++ b/glfw/src/main.zig @@ -14,6 +14,7 @@ pub const hat = @import("hat.zig"); pub const joystick = @import("joystick.zig"); pub const key = @import("key.zig"); pub const mod = @import("mod.zig"); +pub const Monitor = @import("Monitor.zig"); pub const mouse_button = @import("mouse_button.zig"); pub const version = @import("version.zig");