glfw: add monitor.getAll support

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-07-16 16:36:52 -07:00
parent 1cedf40671
commit 04cbfb2e1e
2 changed files with 43 additions and 0 deletions

42
glfw/src/Monitor.zig Normal file
View file

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

View file

@ -14,6 +14,7 @@ pub const hat = @import("hat.zig");
pub const joystick = @import("joystick.zig"); pub const joystick = @import("joystick.zig");
pub const key = @import("key.zig"); pub const key = @import("key.zig");
pub const mod = @import("mod.zig"); pub const mod = @import("mod.zig");
pub const Monitor = @import("Monitor.zig");
pub const mouse_button = @import("mouse_button.zig"); pub const mouse_button = @import("mouse_button.zig");
pub const version = @import("version.zig"); pub const version = @import("version.zig");