glfw: Denormalize errors (#115)

Closes hexops/mach#96

Co-authored-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
InKryption 2021-12-07 05:52:15 +01:00 committed by GitHub
parent 233fc53a17
commit 3648d6b9c4
Failed to generate hash of commit
11 changed files with 457 additions and 206 deletions

View file

@ -32,7 +32,10 @@ pub inline fn from(handle: *c.GLFWwindow) mem.Allocator.Error!Window {
if (ptr == null) {
const internal = try std.heap.c_allocator.create(InternalUserPointer);
c.glfwSetWindowUserPointer(handle, @ptrCast(*c_void, internal));
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
return Window{ .handle = handle };
}
@ -75,7 +78,10 @@ pub const InternalUserPointer = struct {
inline fn defaultHints() void {
internal_debug.assertInitialized();
c.glfwDefaultWindowHints();
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
/// Window hints
@ -278,7 +284,11 @@ pub const Hints = struct {
else => unreachable,
}
getError() catch unreachable;
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable,
else => unreachable,
};
}
}
};
@ -398,7 +408,14 @@ pub const Hints = struct {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_creation, glfw.Window.destroy
pub inline fn create(width: usize, height: usize, title: [*:0]const u8, monitor: ?Monitor, share: ?Window, hints: Hints) Error!Window {
pub inline fn create(
width: usize,
height: usize,
title: [*:0]const u8,
monitor: ?Monitor,
share: ?Window,
hints: Hints,
) (mem.Allocator.Error || error{ APIUnavailable, VersionUnavailable, FormatUnavailable, PlatformError })!Window {
internal_debug.assertInitialized();
const ignore_hints_struct = if (comptime @import("builtin").is_test) testing_ignore_window_hints_struct else false;
if (!ignore_hints_struct) hints.set();
@ -413,12 +430,14 @@ pub inline fn create(width: usize, height: usize, title: [*:0]const u8, monitor:
);
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable,
Error.InvalidValue => unreachable,
Error.APIUnavailable,
Error.VersionUnavailable,
Error.FormatUnavailable,
Error.PlatformError,
=> err,
=> @errSetCast(error{ APIUnavailable, VersionUnavailable, FormatUnavailable, PlatformError }, err),
else => unreachable,
};
@ -455,6 +474,7 @@ pub inline fn destroy(self: Window) void {
// Zig, so by returning an error we'd make it harder to destroy the window properly. So we differ
// from GLFW here: we discard any potential error from this operation.
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
Error.PlatformError => std.log.debug("{}: was unable to destroy Window.\n", .{err}),
else => unreachable,
};
@ -470,7 +490,10 @@ pub inline fn destroy(self: Window) void {
pub inline fn shouldClose(self: Window) bool {
internal_debug.assertInitialized();
const flag = c.glfwWindowShouldClose(self.handle);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
return flag == c.GLFW_TRUE;
}
@ -489,7 +512,10 @@ pub inline fn setShouldClose(self: Window, value: bool) void {
internal_debug.assertInitialized();
const boolean = if (value) c.GLFW_TRUE else c.GLFW_FALSE;
c.glfwSetWindowShouldClose(self.handle, boolean);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
/// Sets the UTF-8 encoded title of the specified window.
@ -503,11 +529,12 @@ pub inline fn setShouldClose(self: Window, value: bool) void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_title
pub inline fn setTitle(self: Window, title: [*:0]const u8) Error!void {
pub inline fn setTitle(self: Window, title: [*:0]const u8) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwSetWindowTitle(self.handle, title);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -540,7 +567,7 @@ pub inline fn setTitle(self: Window, title: [*:0]const u8) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_icon
pub inline fn setIcon(self: Window, allocator: mem.Allocator, images: ?[]Image) Error!void {
pub inline fn setIcon(self: Window, allocator: mem.Allocator, images: ?[]Image) (mem.Allocator.Error || error{PlatformError})!void {
internal_debug.assertInitialized();
if (images) |im| {
const tmp = try allocator.alloc(c.GLFWimage, im.len);
@ -549,7 +576,8 @@ pub inline fn setIcon(self: Window, allocator: mem.Allocator, images: ?[]Image)
c.glfwSetWindowIcon(self.handle, @intCast(c_int, im.len), &tmp[0]);
} else c.glfwSetWindowIcon(self.handle, 0, null);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -572,13 +600,14 @@ pub const Pos = struct {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_pos glfw.Window.setPos
pub inline fn getPos(self: Window) Error!Pos {
pub inline fn getPos(self: Window) error{PlatformError}!Pos {
internal_debug.assertInitialized();
var x: c_int = 0;
var y: c_int = 0;
c.glfwGetWindowPos(self.handle, &x, &y);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
return Pos{ .x = @intCast(usize, x), .y = @intCast(usize, y) };
@ -604,11 +633,12 @@ pub inline fn getPos(self: Window) Error!Pos {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_pos, glfw.Window.getPos
pub inline fn setPos(self: Window, pos: Pos) Error!void {
pub inline fn setPos(self: Window, pos: Pos) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwSetWindowPos(self.handle, @intCast(c_int, pos.x), @intCast(c_int, pos.y));
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -629,13 +659,14 @@ pub const Size = struct {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_size, glfw.Window.setSize
pub inline fn getSize(self: Window) Error!Size {
pub inline fn getSize(self: Window) error{PlatformError}!Size {
internal_debug.assertInitialized();
var width: c_int = 0;
var height: c_int = 0;
c.glfwGetWindowSize(self.handle, &width, &height);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
return Size{ .width = @intCast(usize, width), .height = @intCast(usize, height) };
@ -663,11 +694,12 @@ pub inline fn getSize(self: Window) Error!Size {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_size, glfw.Window.getSize, glfw.Window.SetMonitor
pub inline fn setSize(self: Window, size: Size) Error!void {
pub inline fn setSize(self: Window, size: Size) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwSetWindowSize(self.handle, @intCast(c_int, size.width), @intCast(c_int, size.height));
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -693,7 +725,7 @@ pub inline fn setSize(self: Window, size: Size) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_sizelimits, glfw.Window.setAspectRatio
pub inline fn setSizeLimits(self: Window, min: Size, max: Size) Error!void {
pub inline fn setSizeLimits(self: Window, min: Size, max: Size) error{PlatformError}!void {
internal_debug.assertInitialized();
if (min.width != glfw.dont_care and min.height != glfw.dont_care) {
@ -717,8 +749,9 @@ pub inline fn setSizeLimits(self: Window, min: Size, max: Size) Error!void {
@intCast(c_int, max.height),
);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.InvalidValue => unreachable, // we assert that 'min' and 'max' contain valid values, both absolutely and relative to each other, so this should be impossible
Error.NotInitialized => unreachable,
Error.InvalidValue => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -749,7 +782,7 @@ pub inline fn setSizeLimits(self: Window, min: Size, max: Size) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_sizelimits, glfw.Window.setSizeLimits
pub inline fn setAspectRatio(self: Window, numerator: usize, denominator: usize) Error!void {
pub inline fn setAspectRatio(self: Window, numerator: usize, denominator: usize) error{PlatformError}!void {
internal_debug.assertInitialized();
std.debug.assert(numerator != 0);
@ -762,8 +795,9 @@ pub inline fn setAspectRatio(self: Window, numerator: usize, denominator: usize)
c.glfwSetWindowAspectRatio(self.handle, @intCast(c_int, numerator), @intCast(c_int, denominator));
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.InvalidValue => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -778,13 +812,14 @@ pub inline fn setAspectRatio(self: Window, numerator: usize, denominator: usize)
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_fbsize, glfwWindow.setFramebufferSizeCallback
pub inline fn getFramebufferSize(self: Window) Error!Size {
pub inline fn getFramebufferSize(self: Window) error{PlatformError}!Size {
internal_debug.assertInitialized();
var width: c_int = 0;
var height: c_int = 0;
c.glfwGetFramebufferSize(self.handle, &width, &height);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
return Size{ .width = @intCast(usize, width), .height = @intCast(usize, height) };
@ -811,7 +846,7 @@ pub const FrameSize = struct {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_size
pub inline fn getFrameSize(self: Window) Error!FrameSize {
pub inline fn getFrameSize(self: Window) error{PlatformError}!FrameSize {
internal_debug.assertInitialized();
var left: c_int = 0;
var top: c_int = 0;
@ -819,7 +854,8 @@ pub inline fn getFrameSize(self: Window) Error!FrameSize {
var bottom: c_int = 0;
c.glfwGetWindowFrameSize(self.handle, &left, &top, &right, &bottom);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
return FrameSize{
@ -852,13 +888,14 @@ pub const ContentScale = struct {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_scale, glfwSetWindowContentScaleCallback, glfwGetMonitorContentScale
pub inline fn getContentScale(self: Window) Error!ContentScale {
pub inline fn getContentScale(self: Window) error{PlatformError}!ContentScale {
internal_debug.assertInitialized();
var x_scale: f32 = 0;
var y_scale: f32 = 0;
c.glfwGetWindowContentScale(self.handle, &x_scale, &y_scale);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
return ContentScale{ .x_scale = x_scale, .y_scale = y_scale };
@ -879,11 +916,12 @@ pub inline fn getContentScale(self: Window) Error!ContentScale {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_transparency, glfw.Window.setOpacity
pub inline fn getOpacity(self: Window) Error!f32 {
pub inline fn getOpacity(self: Window) error{PlatformError}!f32 {
internal_debug.assertInitialized();
const opacity = c.glfwGetWindowOpacity(self.handle);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
return opacity;
@ -906,11 +944,12 @@ pub inline fn getOpacity(self: Window) Error!f32 {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_transparency, glfw.Window.getOpacity
pub inline fn setOpacity(self: Window, opacity: f32) Error!void {
pub inline fn setOpacity(self: Window, opacity: f32) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwSetWindowOpacity(self.handle, opacity);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -931,11 +970,12 @@ pub inline fn setOpacity(self: Window, opacity: f32) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_iconify, glfw.Window.restore, glfw.Window.maximize
pub inline fn iconify(self: Window) Error!void {
pub inline fn iconify(self: Window) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwIconifyWindow(self.handle);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -953,11 +993,12 @@ pub inline fn iconify(self: Window) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_iconify, glfw.Window.iconify, glfw.Window.maximize
pub inline fn restore(self: Window) Error!void {
pub inline fn restore(self: Window) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwRestoreWindow(self.handle);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -974,11 +1015,12 @@ pub inline fn restore(self: Window) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_iconify, glfw.Window.iconify, glfw.Window.restore
pub inline fn maximize(self: Window) Error!void {
pub inline fn maximize(self: Window) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwMaximizeWindow(self.handle);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -997,11 +1039,12 @@ pub inline fn maximize(self: Window) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_hide, glfw.Window.hide
pub inline fn show(self: Window) Error!void {
pub inline fn show(self: Window) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwShowWindow(self.handle);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1016,11 +1059,12 @@ pub inline fn show(self: Window) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_hide, glfw.Window.show
pub inline fn hide(self: Window) Error!void {
pub inline fn hide(self: Window) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwHideWindow(self.handle);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1049,11 +1093,12 @@ pub inline fn hide(self: Window) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_focus, window_attention
pub inline fn focus(self: Window) Error!void {
pub inline fn focus(self: Window) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwFocusWindow(self.handle);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1073,11 +1118,12 @@ pub inline fn focus(self: Window) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_attention
pub inline fn requestAttention(self: Window) Error!void {
pub inline fn requestAttention(self: Window) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwRequestWindowAttention(self.handle);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1103,13 +1149,14 @@ pub inline fn requestAttention(self: Window) Error!void {
/// @thread_safety This function may be called from any thread.
///
/// see also: buffer_swap, glfwSwapInterval
pub inline fn swapBuffers(self: Window) Error!void {
pub inline fn swapBuffers(self: Window) error{ NoWindowContext, PlatformError }!void {
internal_debug.assertInitialized();
c.glfwSwapBuffers(self.handle);
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
Error.NoWindowContext,
Error.PlatformError,
=> err,
=> @errSetCast(error{ NoWindowContext, PlatformError }, err),
else => unreachable,
};
}
@ -1128,7 +1175,10 @@ pub inline fn swapBuffers(self: Window) Error!void {
pub inline fn getMonitor(self: Window) ?Monitor {
internal_debug.assertInitialized();
const monitor = c.glfwGetWindowMonitor(self.handle);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
if (monitor) |m| return Monitor{ .handle = m };
return null;
}
@ -1175,7 +1225,7 @@ pub inline fn getMonitor(self: Window) ?Monitor {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_monitor, window_full_screen, glfw.Window.getMonitor, glfw.Window.setSize
pub inline fn setMonitor(self: Window, monitor: ?Monitor, xpos: isize, ypos: isize, width: isize, height: isize, refresh_rate: isize) Error!void {
pub inline fn setMonitor(self: Window, monitor: ?Monitor, xpos: isize, ypos: isize, width: isize, height: isize, refresh_rate: isize) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwSetWindowMonitor(
self.handle,
@ -1187,7 +1237,8 @@ pub inline fn setMonitor(self: Window, monitor: ?Monitor, xpos: isize, ypos: isi
@intCast(c_int, refresh_rate),
);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1240,11 +1291,13 @@ pub const Attrib = enum(c_int) {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_attribs, glfw.Window.setAttrib
pub inline fn getAttrib(self: Window, attrib: Attrib) Error!isize {
pub inline fn getAttrib(self: Window, attrib: Attrib) error{PlatformError}!isize {
internal_debug.assertInitialized();
const v = c.glfwGetWindowAttrib(self.handle, @enumToInt(attrib));
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
return v;
@ -1274,7 +1327,7 @@ pub inline fn getAttrib(self: Window, attrib: Attrib) Error!isize {
///
/// see also: window_attribs, glfw.Window.getAttrib
///
pub inline fn setAttrib(self: Window, attrib: Attrib, value: bool) Error!void {
pub inline fn setAttrib(self: Window, attrib: Attrib, value: bool) error{PlatformError}!void {
internal_debug.assertInitialized();
std.debug.assert(switch (attrib) {
.decorated,
@ -1287,8 +1340,10 @@ pub inline fn setAttrib(self: Window, attrib: Attrib, value: bool) Error!void {
});
c.glfwSetWindowAttrib(self.handle, @enumToInt(attrib), if (value) c.GLFW_TRUE else c.GLFW_FALSE);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable,
Error.InvalidValue => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1361,9 +1416,15 @@ pub inline fn setPosCallback(self: Window, callback: ?fn (window: Window, xpos:
var internal = self.getInternal();
internal.setPosCallback = callback;
_ = c.glfwSetWindowPosCallback(self.handle, if (callback != null) setPosCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
// TODO: don't the calls to `from` in these `set*CallbackWrapper` functions cause a leak?
// is this what's being reported by valgrind in [#60](https://github.com/hexops/mach/issues/60)?
fn setSizeCallbackWrapper(handle: ?*c.GLFWwindow, width: c_int, height: c_int) callconv(.C) void {
internal_debug.assertInitialized();
const window = from(handle.?) catch unreachable;
@ -1389,7 +1450,10 @@ pub inline fn setSizeCallback(self: Window, callback: ?fn (window: Window, width
var internal = self.getInternal();
internal.setSizeCallback = callback;
_ = c.glfwSetWindowSizeCallback(self.handle, if (callback != null) setSizeCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setCloseCallbackWrapper(handle: ?*c.GLFWwindow) callconv(.C) void {
@ -1425,7 +1489,10 @@ pub inline fn setCloseCallback(self: Window, callback: ?fn (window: Window) void
var internal = self.getInternal();
internal.setCloseCallback = callback;
_ = c.glfwSetWindowCloseCallback(self.handle, if (callback != null) setCloseCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setRefreshCallbackWrapper(handle: ?*c.GLFWwindow) callconv(.C) void {
@ -1434,33 +1501,34 @@ fn setRefreshCallbackWrapper(handle: ?*c.GLFWwindow) callconv(.C) void {
internal.setRefreshCallback.?(window);
}
// TODO: Something's off about this comment?
// /// Sets the refresh callback for the specified window.
// ///
// /// This function sets the refresh callback of the specified window, which is
// /// called when the content area of the window needs to be redrawn, for example
// /// if the window has been exposed after having been covered by another window.
// ///
// /// On compositing window systems such as Aero, Compiz, Aqua or Wayland, where
// /// the window contents are saved off-screen, this callback may be called only
// /// very infrequently or never at all.
// ///
// /// @param[in] window The window whose callback to set.
// /// @param[in] callback The new callback, or null to remove the currently set
// /// callback.
// ///
// /// @callback_param `window` the window whose content needs to be refreshed.
// ///
// /// @thread_safety This function must only be called from the main thread.
// ///
// /// see also: window_refresh
// GLFWAPI GLFWwindowrefreshfun glfwSetWindowRefreshCallback(GLFWwindow* window, GLFWwindowrefreshfun callback);
/// Sets the refresh callback for the specified window.
///
/// This function sets the refresh callback of the specified window, which is
/// called when the content area of the window needs to be redrawn, for example
/// if the window has been exposed after having been covered by another window.
///
/// On compositing window systems such as Aero, Compiz, Aqua or Wayland, where
/// the window contents are saved off-screen, this callback may be called only
/// very infrequently or never at all.
///
/// @param[in] window The window whose callback to set.
/// @param[in] callback The new callback, or null to remove the currently set
/// callback.
///
/// @callback_param `window` the window whose content needs to be refreshed.
///
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_refresh
pub inline fn setRefreshCallback(self: Window, callback: ?fn (window: Window) void) void {
internal_debug.assertInitialized();
var internal = self.getInternal();
internal.setRefreshCallback = callback;
_ = c.glfwSetWindowRefreshCallback(self.handle, if (callback != null) setRefreshCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setFocusCallbackWrapper(handle: ?*c.GLFWwindow, focused: c_int) callconv(.C) void {
@ -1495,7 +1563,10 @@ pub inline fn setFocusCallback(self: Window, callback: ?fn (window: Window, focu
var internal = self.getInternal();
internal.setFocusCallback = callback;
_ = c.glfwSetWindowFocusCallback(self.handle, if (callback != null) setFocusCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setIconifyCallbackWrapper(handle: ?*c.GLFWwindow, iconified: c_int) callconv(.C) void {
@ -1528,7 +1599,10 @@ pub inline fn setIconifyCallback(self: Window, callback: ?fn (window: Window, ic
var internal = self.getInternal();
internal.setIconifyCallback = callback;
_ = c.glfwSetWindowIconifyCallback(self.handle, if (callback != null) setIconifyCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setMaximizeCallbackWrapper(handle: ?*c.GLFWwindow, maximized: c_int) callconv(.C) void {
@ -1559,7 +1633,10 @@ pub inline fn setMaximizeCallback(self: Window, callback: ?fn (window: Window, m
var internal = self.getInternal();
internal.setMaximizeCallback = callback;
_ = c.glfwSetWindowMaximizeCallback(self.handle, if (callback != null) setMaximizeCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setFramebufferSizeCallbackWrapper(handle: ?*c.GLFWwindow, width: c_int, height: c_int) callconv(.C) void {
@ -1590,7 +1667,10 @@ pub inline fn setFramebufferSizeCallback(self: Window, callback: ?fn (window: Wi
var internal = self.getInternal();
internal.setFramebufferSizeCallback = callback;
_ = c.glfwSetFramebufferSizeCallback(self.handle, if (callback != null) setFramebufferSizeCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setContentScaleCallbackWrapper(handle: ?*c.GLFWwindow, xscale: f32, yscale: f32) callconv(.C) void {
@ -1621,7 +1701,10 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
var internal = self.getInternal();
internal.setContentScaleCallback = callback;
_ = c.glfwSetWindowContentScaleCallback(self.handle, if (callback != null) setContentScaleCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
pub const InputMode = enum(c_int) {
@ -1647,7 +1730,7 @@ pub const InputModeCursor = enum(c_int) {
};
/// Sets the input mode of the cursor, whether it should behave normally, be hidden, or grabbed.
pub inline fn setInputModeCursor(self: Window, value: InputModeCursor) Error!void {
pub inline fn setInputModeCursor(self: Window, value: InputModeCursor) error{PlatformError}!void {
return self.setInputMode(InputMode.cursor, value);
}
@ -1661,7 +1744,7 @@ pub inline fn getInputModeCursor(self: Window) InputModeCursor {
///
/// This is useful when you are only interested in whether keys have been pressed but not when or
/// in which order.
pub inline fn setInputModeStickyKeys(self: Window, enabled: bool) Error!void {
pub inline fn setInputModeStickyKeys(self: Window, enabled: bool) error{PlatformError}!void {
return self.setInputMode(InputMode.sticky_keys, enabled);
}
@ -1676,7 +1759,7 @@ pub inline fn getInputModeStickyKeys(self: Window) bool {
///
/// This is useful when you are only interested in whether buttons have been pressed but not when
/// or in which order.
pub inline fn setInputModeStickyMouseButtons(self: Window, enabled: bool) Error!void {
pub inline fn setInputModeStickyMouseButtons(self: Window, enabled: bool) error{PlatformError}!void {
return self.setInputMode(InputMode.sticky_mouse_buttons, enabled);
}
@ -1688,7 +1771,7 @@ pub inline fn getInputModeStickyMouseButtons(self: Window) bool {
/// Sets the input mode of locking key modifiers, if enabled callbacks that receive modifier bits
/// will also have the glfw.mod.caps_lock bit set when the event was generated with Caps Lock on,
/// and the glfw.mod.num_lock bit when Num Lock was on.
pub inline fn setInputModeLockKeyMods(self: Window, enabled: bool) Error!void {
pub inline fn setInputModeLockKeyMods(self: Window, enabled: bool) error{PlatformError}!void {
return self.setInputMode(InputMode.lock_key_mods, enabled);
}
@ -1703,7 +1786,7 @@ pub inline fn getInputModeLockKeyMods(self: Window) bool {
///
/// If raw motion is not supported, attempting to set this will emit glfw.Error.PlatformError. Call
/// glfw.rawMouseMotionSupported to check for support.
pub inline fn setInputModeRawMouseMotion(self: Window, enabled: bool) Error!void {
pub inline fn setInputModeRawMouseMotion(self: Window, enabled: bool) error{PlatformError}!void {
return self.setInputMode(InputMode.raw_mouse_motion, enabled);
}
@ -1736,8 +1819,11 @@ pub inline fn getInputMode(self: Window, mode: InputMode) isize {
internal_debug.assertInitialized();
const value = c.glfwGetInputMode(self.handle, @enumToInt(mode));
// Possible errors: 'GLFW_NOT_INITIALIZED' and 'GLFW_INVALID_ENUM'; we guarantee both to be unreachable
getError() catch unreachable;
getError() catch |err| return switch (err) {
Error.InvalidEnum => unreachable,
Error.NotInitialized => unreachable,
else => unreachable,
};
return @intCast(isize, value);
}
@ -1761,7 +1847,7 @@ pub inline fn getInputMode(self: Window, mode: InputMode) isize {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: glfw.Window.getInputMode
pub inline fn setInputMode(self: Window, mode: InputMode, value: anytype) Error!void {
pub inline fn setInputMode(self: Window, mode: InputMode, value: anytype) error{PlatformError}!void {
internal_debug.assertInitialized();
const T = @TypeOf(value);
std.debug.assert(switch (mode) {
@ -1783,7 +1869,9 @@ pub inline fn setInputMode(self: Window, mode: InputMode, value: anytype) Error!
};
c.glfwSetInputMode(self.handle, @enumToInt(mode), int_value);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1817,7 +1905,11 @@ pub inline fn setInputMode(self: Window, mode: InputMode, value: anytype) Error!
pub inline fn getKey(self: Window, key: Key) Action {
internal_debug.assertInitialized();
const state = c.glfwGetKey(self.handle, @enumToInt(key));
getError() catch unreachable;
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable,
else => unreachable,
};
return @intToEnum(Action, state);
}
@ -1840,7 +1932,11 @@ pub inline fn getKey(self: Window, key: Key) Action {
pub inline fn getMouseButton(self: Window, button: MouseButton) Action {
internal_debug.assertInitialized();
const state = c.glfwGetMouseButton(self.handle, @enumToInt(button));
getError() catch unreachable;
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable,
else => unreachable,
};
return @intToEnum(Action, state);
}
@ -1874,12 +1970,13 @@ pub const CursorPos = struct {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: cursor_pos, glfw.Window.setCursorPos
pub inline fn getCursorPos(self: Window) Error!CursorPos {
pub inline fn getCursorPos(self: Window) error{PlatformError}!CursorPos {
internal_debug.assertInitialized();
var pos: CursorPos = undefined;
c.glfwGetCursorPos(self.handle, &pos.xpos, &pos.ypos);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
return pos;
@ -1910,11 +2007,12 @@ pub inline fn getCursorPos(self: Window) Error!CursorPos {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: cursor_pos, glfw.Window.getCursorPos
pub inline fn setCursorPos(self: Window, xpos: f64, ypos: f64) Error!void {
pub inline fn setCursorPos(self: Window, xpos: f64, ypos: f64) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwSetCursorPos(self.handle, xpos, ypos);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1934,11 +2032,12 @@ pub inline fn setCursorPos(self: Window, xpos: f64, ypos: f64) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: cursor_object
pub inline fn setCursor(self: Window, cursor: Cursor) Error!void {
pub inline fn setCursor(self: Window, cursor: Cursor) error{PlatformError}!void {
internal_debug.assertInitialized();
c.glfwSetCursor(self.handle, cursor.ptr);
getError() catch |err| return switch (err) {
Error.PlatformError => err,
Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable,
};
}
@ -1988,7 +2087,10 @@ pub inline fn setKeyCallback(self: Window, callback: ?fn (window: Window, key: K
var internal = self.getInternal();
internal.setKeyCallback = callback;
_ = c.glfwSetKeyCallback(self.handle, if (callback != null) setKeyCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setCharCallbackWrapper(handle: ?*c.GLFWwindow, codepoint: c_uint) callconv(.C) void {
@ -2026,7 +2128,10 @@ pub inline fn setCharCallback(self: Window, callback: ?fn (window: Window, codep
var internal = self.getInternal();
internal.setCharCallback = callback;
_ = c.glfwSetCharCallback(self.handle, if (callback != null) setCharCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setMouseButtonCallbackWrapper(handle: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) callconv(.C) void {
@ -2062,7 +2167,10 @@ pub inline fn setMouseButtonCallback(self: Window, callback: ?fn (window: Window
var internal = self.getInternal();
internal.setMouseButtonCallback = callback;
_ = c.glfwSetMouseButtonCallback(self.handle, if (callback != null) setMouseButtonCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setCursorPosCallbackWrapper(handle: ?*c.GLFWwindow, xpos: f64, ypos: f64) callconv(.C) void {
@ -2093,7 +2201,10 @@ pub inline fn setCursorPosCallback(self: Window, callback: ?fn (window: Window,
var internal = self.getInternal();
internal.setCursorPosCallback = callback;
_ = c.glfwSetCursorPosCallback(self.handle, if (callback != null) setCursorPosCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setCursorEnterCallbackWrapper(handle: ?*c.GLFWwindow, entered: c_int) callconv(.C) void {
@ -2121,7 +2232,10 @@ pub inline fn setCursorEnterCallback(self: Window, callback: ?fn (window: Window
var internal = self.getInternal();
internal.setCursorEnterCallback = callback;
_ = c.glfwSetCursorEnterCallback(self.handle, if (callback != null) setCursorEnterCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setScrollCallbackWrapper(handle: ?*c.GLFWwindow, xoffset: f64, yoffset: f64) callconv(.C) void {
@ -2153,7 +2267,10 @@ pub inline fn setScrollCallback(self: Window, callback: ?fn (window: Window, xof
var internal = self.getInternal();
internal.setScrollCallback = callback;
_ = c.glfwSetScrollCallback(self.handle, if (callback != null) setScrollCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
fn setDropCallbackWrapper(handle: ?*c.GLFWwindow, path_count: c_int, paths: [*c][*c]const u8) callconv(.C) void {
@ -2192,7 +2309,10 @@ pub inline fn setDropCallback(self: Window, callback: ?fn (window: Window, paths
var internal = self.getInternal();
internal.setDropCallback = callback;
_ = c.glfwSetDropCallback(self.handle, if (callback != null) setDropCallbackWrapper else null);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
};
}
/// For testing purposes only; see glfw.Window.Hints and glfw.Window.create for the public API.
@ -2255,7 +2375,11 @@ inline fn hint(h: Hint, value: anytype) void {
@compileError("expected a int, bool, enum, array, or pointer, got " ++ @typeName(value_type));
},
}
getError() catch unreachable;
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable,
else => unreachable,
};
}
test "defaultHints" {