glfw: dont call getError unless we need to

This commit is contained in:
Lee Cannon 2022-02-08 01:49:22 +00:00 committed by Stephen Gutekanst
parent 746b0dd1f0
commit 3e79a12f3d
11 changed files with 111 additions and 92 deletions

View file

@ -63,13 +63,13 @@ pub const Shape = enum(i32) {
pub inline fn create(image: Image, xhot: i32, yhot: i32) error{PlatformError}!Cursor { pub inline fn create(image: Image, xhot: i32, yhot: i32) error{PlatformError}!Cursor {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const img = image.toC(); const img = image.toC();
const cursor = c.glfwCreateCursor(&img, @intCast(c_int, xhot), @intCast(c_int, yhot)); if (c.glfwCreateCursor(&img, @intCast(c_int, xhot), @intCast(c_int, yhot))) |cursor| return Cursor{ .ptr = cursor };
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err), Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable, else => unreachable,
}; };
return Cursor{ .ptr = cursor.? }; unreachable;
} }
/// Creates a cursor with a standard shape. /// Creates a cursor with a standard shape.
@ -83,14 +83,14 @@ pub inline fn create(image: Image, xhot: i32, yhot: i32) error{PlatformError}!Cu
/// see also: cursor_object, glfwCreateCursor /// see also: cursor_object, glfwCreateCursor
pub inline fn createStandard(shape: Shape) error{PlatformError}!Cursor { pub inline fn createStandard(shape: Shape) error{PlatformError}!Cursor {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const cursor = c.glfwCreateStandardCursor(@intCast(c_int, @enumToInt(shape))); if (c.glfwCreateStandardCursor(@intCast(c_int, @enumToInt(shape)))) |cursor| return Cursor{ .ptr = cursor };
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable, Error.InvalidEnum => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err), Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable, else => unreachable,
}; };
return Cursor{ .ptr = cursor.? }; unreachable;
} }
/// Destroys a cursor. /// Destroys a cursor.

View file

@ -414,7 +414,7 @@ pub inline fn setCallback(callback: ?fn (joystick: Joystick, event: Event) void)
/// @ingroup input /// @ingroup input
pub inline fn updateGamepadMappings(gamepad_mappings: [*:0]const u8) error{InvalidValue}!void { pub inline fn updateGamepadMappings(gamepad_mappings: [*:0]const u8) error{InvalidValue}!void {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
_ = c.glfwUpdateGamepadMappings(gamepad_mappings); if (c.glfwUpdateGamepadMappings(gamepad_mappings) == c.GLFW_TRUE) return;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
// TODO: Maybe return as 'ParseError' here? // TODO: Maybe return as 'ParseError' here?

View file

@ -164,12 +164,12 @@ pub inline fn getContentScale(self: Monitor) error{PlatformError}!ContentScale {
/// see also: monitor_properties /// see also: monitor_properties
pub inline fn getName(self: Monitor) [*:0]const u8 { pub inline fn getName(self: Monitor) [*:0]const u8 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const name = c.glfwGetMonitorName(self.handle); if (c.glfwGetMonitorName(self.handle)) |name| return name;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return name; unreachable;
} }
/// Sets the user pointer of the specified monitor. /// Sets the user pointer of the specified monitor.
@ -234,19 +234,20 @@ pub inline fn getUserPointer(self: Monitor, comptime T: type) ?*T {
pub inline fn getVideoModes(self: Monitor, allocator: mem.Allocator) (mem.Allocator.Error || error{PlatformError})![]VideoMode { pub inline fn getVideoModes(self: Monitor, allocator: mem.Allocator) (mem.Allocator.Error || error{PlatformError})![]VideoMode {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
var count: c_int = 0; var count: c_int = 0;
const modes = c.glfwGetVideoModes(self.handle, &count); if (c.glfwGetVideoModes(self.handle, &count)) |modes| {
const slice = try allocator.alloc(VideoMode, @intCast(u32, count));
var i: u32 = 0;
while (i < count) : (i += 1) {
slice[i] = VideoMode{ .handle = modes[i] };
}
return slice;
}
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err), Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable, else => unreachable,
}; };
unreachable;
const slice = try allocator.alloc(VideoMode, @intCast(u32, count));
var i: u32 = 0;
while (i < count) : (i += 1) {
slice[i] = VideoMode{ .handle = modes[i] };
}
return slice;
} }
/// Returns the current mode of the specified monitor. /// Returns the current mode of the specified monitor.
@ -262,13 +263,13 @@ pub inline fn getVideoModes(self: Monitor, allocator: mem.Allocator) (mem.Alloca
/// see also: monitor_modes, glfw.Monitor.getVideoModes /// see also: monitor_modes, glfw.Monitor.getVideoModes
pub inline fn getVideoMode(self: Monitor) error{PlatformError}!VideoMode { pub inline fn getVideoMode(self: Monitor) error{PlatformError}!VideoMode {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const mode = c.glfwGetVideoMode(self.handle); if (c.glfwGetVideoMode(self.handle)) |mode| return VideoMode{ .handle = mode.* };
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err), Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable, else => unreachable,
}; };
return VideoMode{ .handle = mode.?.* }; unreachable;
} }
/// Generates a gamma ramp and sets it for the specified monitor. /// Generates a gamma ramp and sets it for the specified monitor.
@ -323,13 +324,13 @@ pub inline fn setGamma(self: Monitor, gamma: f32) error{PlatformError}!void {
/// see also: monitor_gamma /// see also: monitor_gamma
pub inline fn getGammaRamp(self: Monitor) error{PlatformError}!GammaRamp { pub inline fn getGammaRamp(self: Monitor) error{PlatformError}!GammaRamp {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const ramp = c.glfwGetGammaRamp(self.handle); if (c.glfwGetGammaRamp(self.handle)) |ramp| return GammaRamp.fromC(ramp.*);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err), Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable, else => unreachable,
}; };
return GammaRamp.fromC(ramp.*); unreachable;
} }
/// Sets the current gamma ramp for the specified monitor. /// Sets the current gamma ramp for the specified monitor.
@ -381,17 +382,19 @@ pub inline fn setGammaRamp(self: Monitor, ramp: GammaRamp) error{PlatformError}!
pub inline fn getAll(allocator: mem.Allocator) mem.Allocator.Error![]Monitor { pub inline fn getAll(allocator: mem.Allocator) mem.Allocator.Error![]Monitor {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
var count: c_int = 0; var count: c_int = 0;
const monitors = c.glfwGetMonitors(&count); if (c.glfwGetMonitors(&count)) |monitors| {
const slice = try allocator.alloc(Monitor, @intCast(u32, count));
var i: u32 = 0;
while (i < count) : (i += 1) {
slice[i] = Monitor{ .handle = monitors[i].? };
}
return slice;
}
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
const slice = try allocator.alloc(Monitor, @intCast(u32, count)); return &.{};
var i: u32 = 0;
while (i < count) : (i += 1) {
slice[i] = Monitor{ .handle = monitors[i].? };
}
return slice;
} }
/// Returns the primary monitor. /// Returns the primary monitor.
@ -406,15 +409,12 @@ pub inline fn getAll(allocator: mem.Allocator) mem.Allocator.Error![]Monitor {
/// see also: monitor_monitors, glfw.monitors.getAll /// see also: monitor_monitors, glfw.monitors.getAll
pub inline fn getPrimary() ?Monitor { pub inline fn getPrimary() ?Monitor {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const handle = c.glfwGetPrimaryMonitor(); if (c.glfwGetPrimaryMonitor()) |handle| return Monitor{ .handle = handle };
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
if (handle == null) { return null;
return null;
}
return Monitor{ .handle = handle.? };
} }
var callback_fn_ptr: ?usize = null; var callback_fn_ptr: ?usize = null;

View file

@ -424,13 +424,13 @@ pub inline fn create(
if (!ignore_hints_struct) hints.set(); if (!ignore_hints_struct) hints.set();
defer if (!ignore_hints_struct) defaultHints(); defer if (!ignore_hints_struct) defaultHints();
const handle = c.glfwCreateWindow( if (c.glfwCreateWindow(
@intCast(c_int, width), @intCast(c_int, width),
@intCast(c_int, height), @intCast(c_int, height),
&title[0], &title[0],
if (monitor) |m| m.handle else null, if (monitor) |m| m.handle else null,
if (share) |w| w.handle else null, if (share) |w| w.handle else null,
); )) |handle| return from(handle);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
@ -444,7 +444,7 @@ pub inline fn create(
else => unreachable, else => unreachable,
}; };
return from(handle.?); unreachable;
} }
var testing_ignore_window_hints_struct = if (@import("builtin").is_test) false else @as(void, {}); var testing_ignore_window_hints_struct = if (@import("builtin").is_test) false else @as(void, {});
@ -1179,12 +1179,11 @@ pub inline fn swapBuffers(self: Window) error{ NoWindowContext, PlatformError }!
/// see also: window_monitor, glfw.Window.setMonitor /// see also: window_monitor, glfw.Window.setMonitor
pub inline fn getMonitor(self: Window) ?Monitor { pub inline fn getMonitor(self: Window) ?Monitor {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const monitor = c.glfwGetWindowMonitor(self.handle); if (c.glfwGetWindowMonitor(self.handle)) |monitor| return Monitor{ .handle = monitor };
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
if (monitor) |m| return Monitor{ .handle = m };
return null; return null;
} }
@ -1299,13 +1298,14 @@ pub const Attrib = enum(c_int) {
pub inline fn getAttrib(self: Window, attrib: Attrib) error{PlatformError}!i32 { pub inline fn getAttrib(self: Window, attrib: Attrib) error{PlatformError}!i32 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const v = c.glfwGetWindowAttrib(self.handle, @enumToInt(attrib)); const v = c.glfwGetWindowAttrib(self.handle, @enumToInt(attrib));
if (v != 0) return v;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable, Error.InvalidEnum => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err), Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable, else => unreachable,
}; };
return v; unreachable;
} }
/// Sets an attribute of the specified window. /// Sets an attribute of the specified window.

View file

@ -48,7 +48,7 @@ pub inline fn setClipboardString(value: [*:0]const u8) error{PlatformError}!void
/// see also: clipboard, glfwSetClipboardString /// see also: clipboard, glfwSetClipboardString
pub inline fn getClipboardString() error{ FormatUnavailable, PlatformError }![:0]const u8 { pub inline fn getClipboardString() error{ FormatUnavailable, PlatformError }![:0]const u8 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const value = c.glfwGetClipboardString(null); if (c.glfwGetClipboardString(null)) |c_str| return std.mem.span(c_str);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.FormatUnavailable, Error.FormatUnavailable,
@ -56,7 +56,7 @@ pub inline fn getClipboardString() error{ FormatUnavailable, PlatformError }![:0
=> @errSetCast(error{ FormatUnavailable, PlatformError }, err), => @errSetCast(error{ FormatUnavailable, PlatformError }, err),
else => unreachable, else => unreachable,
}; };
return std.mem.span(value); unreachable;
} }
test "setClipboardString" { test "setClipboardString" {

View file

@ -243,13 +243,14 @@ pub const Key = enum(c_int) {
pub inline fn getScancode(self: Key) error{PlatformError}!i32 { pub inline fn getScancode(self: Key) error{PlatformError}!i32 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const scancode = cc.glfwGetKeyScancode(@enumToInt(self)); const scancode = cc.glfwGetKeyScancode(@enumToInt(self));
if (scancode != -1) return scancode;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.InvalidEnum => unreachable, Error.InvalidEnum => unreachable,
Error.PlatformError => @errSetCast(error{PlatformError}, err), Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable, else => unreachable,
}; };
return scancode; unreachable;
} }
}; };

View file

@ -74,7 +74,7 @@ pub inline fn init(hints: InitHints) error{PlatformError}!void {
initHint(init_hint, init_value); initHint(init_hint, init_value);
} }
_ = c.glfwInit(); if (c.glfwInit() == c.GLFW_TRUE) return;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.PlatformError => @errSetCast(error{PlatformError}, err), Error.PlatformError => @errSetCast(error{PlatformError}, err),
else => unreachable, else => unreachable,
@ -210,11 +210,7 @@ fn initHint(hint: InitHint, value: anytype) void {
/// ///
/// @thread_safety This function may be called from any thread. /// @thread_safety This function may be called from any thread.
pub inline fn getVersionString() [:0]const u8 { pub inline fn getVersionString() [:0]const u8 {
const result = std.mem.span(c.glfwGetVersionString()); return std.mem.span(c.glfwGetVersionString());
getError() catch |err| switch (err) {
else => unreachable,
};
return result;
} }
/// Processes all pending events. /// Processes all pending events.

View file

@ -67,12 +67,12 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getWin32Adapter(monitor: Monitor) [*:0]const u8 { pub fn getWin32Adapter(monitor: Monitor) [*:0]const u8 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const adapter = native.glfwGetWin32Adapter(@ptrCast(*native.GLFWmonitor, monitor.handle)); if (native.glfwGetWin32Adapter(@ptrCast(*native.GLFWmonitor, monitor.handle))) |adapter| return adapter;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return adapter; unreachable;
} }
/// Returns the display device name of the specified monitor. /// Returns the display device name of the specified monitor.
@ -85,12 +85,12 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getWin32Monitor(monitor: Monitor) [*:0]const u8 { pub fn getWin32Monitor(monitor: Monitor) [*:0]const u8 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const mon = native.glfwWin32Monitor(@ptrCast(*native.GLFWmonitor, monitor.handle)); if (native.glfwGetWin32Monitor(@ptrCast(*native.GLFWmonitor, monitor.handle))) |mon| return mon;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return mon; unreachable;
} }
/// Returns the `HWND` of the specified window. /// Returns the `HWND` of the specified window.
@ -108,12 +108,13 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getWin32Window(window: Window) std.os.windows.HWND { pub fn getWin32Window(window: Window) std.os.windows.HWND {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const win = native.glfwGetWin32Window(@ptrCast(*native.GLFWwindow, window.handle)); if (native.glfwGetWin32Window(@ptrCast(*native.GLFWwindow, window.handle))) |win|
return @ptrCast(std.os.windows.HWND, win);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return @ptrCast(std.os.windows.HWND, win); unreachable;
} }
/// Returns the `HGLRC` of the specified window. /// Returns the `HGLRC` of the specified window.
@ -131,13 +132,13 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getWGLContext(window: Window) error{NoWindowContext}!std.os.windows.HGLRC { pub fn getWGLContext(window: Window) error{NoWindowContext}!std.os.windows.HGLRC {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const context = native.glfwGetWGLContext(@ptrCast(*native.GLFWwindow, window.handle)); if (native.glfwGetWGLContext(@ptrCast(*native.GLFWwindow, window.handle))) |context| return context;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e), Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e),
else => unreachable, else => unreachable,
}; };
return context; unreachable;
} }
/// Returns the `CGDirectDisplayID` of the specified monitor. /// Returns the `CGDirectDisplayID` of the specified monitor.
@ -148,11 +149,12 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getCocoaMonitor(monitor: Monitor) u32 { pub fn getCocoaMonitor(monitor: Monitor) u32 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const mon = native.glfwGetCocoaMonitor(@ptrCast(*native.GLFWmonitor, monitor.handle)); const mon = native.glfwGetCocoaMonitor(@ptrCast(*native.GLFWmonitor, monitor.handle));
if (mon != native.kCGNullDirectDisplay) return mon;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return mon; unreachable;
} }
/// Returns the `NSWindow` of the specified window. /// Returns the `NSWindow` of the specified window.
@ -193,12 +195,12 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getX11Display() *anyopaque { pub fn getX11Display() *anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const display = native.glfwGetX11Display(); if (native.glfwGetX11Display()) |display| return @ptrCast(*anyopaque, display);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, display); unreachable;
} }
/// Returns the `RRCrtc` of the specified monitor. /// Returns the `RRCrtc` of the specified monitor.
@ -209,11 +211,12 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getX11Adapter(monitor: Monitor) u32 { pub fn getX11Adapter(monitor: Monitor) u32 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const adapter = native.glfwGetX11Adapter(@ptrCast(*native.GLFWMonitor, monitor.handle)); const adapter = native.glfwGetX11Adapter(@ptrCast(*native.GLFWMonitor, monitor.handle));
if (adapter != 0) return adapter;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return adapter; unreachable;
} }
/// Returns the `RROutput` of the specified monitor. /// Returns the `RROutput` of the specified monitor.
@ -224,11 +227,12 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getX11Monitor(monitor: Monitor) u32 { pub fn getX11Monitor(monitor: Monitor) u32 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const mon = native.glfwGetX11Monitor(@ptrCast(*native.GLFWmonitor, monitor.handle)); const mon = native.glfwGetX11Monitor(@ptrCast(*native.GLFWmonitor, monitor.handle));
if (mon != 0) return mon;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return mon; unreachable;
} }
/// Returns the `Window` of the specified window. /// Returns the `Window` of the specified window.
@ -239,11 +243,12 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getX11Window(window: Window) u32 { pub fn getX11Window(window: Window) u32 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const win = native.glfwGetX11Window(@ptrCast(*native.GLFWwindow, window.handle)); const win = native.glfwGetX11Window(@ptrCast(*native.GLFWwindow, window.handle));
if (win != 0) return @intCast(u32, win);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return @intCast(u32, win); unreachable;
} }
/// Sets the current primary selection to the specified string. /// Sets the current primary selection to the specified string.
@ -274,13 +279,13 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function must only be called from the main thread. /// thread_safety: This function must only be called from the main thread.
pub fn getX11SelectionString() error{FormatUnavailable}![*:0]const u8 { pub fn getX11SelectionString() error{FormatUnavailable}![*:0]const u8 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const str = native.glfwGetX11SelectionString(); if (native.glfwGetX11SelectionString()) |str| return str;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.FormatUnavailable => |e| @errSetCast(error{FormatUnavailable}, e), Error.FormatUnavailable => |e| @errSetCast(error{FormatUnavailable}, e),
else => unreachable, else => unreachable,
}; };
return str; unreachable;
} }
/// Returns the `GLXContext` of the specified window. /// Returns the `GLXContext` of the specified window.
@ -290,13 +295,13 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getGLXContext(window: Window) error{NoWindowContext}!*anyopaque { pub fn getGLXContext(window: Window) error{NoWindowContext}!*anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const context = native.glfwGetGLXContext(@ptrCast(*native.GLFWwindow, window.handle)); if (native.glfwGetGLXContext(@ptrCast(*native.GLFWwindow, window.handle))) |context| return @ptrCast(*anyopaque, context);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e), Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e),
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, context); unreachable;
} }
/// Returns the `GLXWindow` of the specified window. /// Returns the `GLXWindow` of the specified window.
@ -307,12 +312,13 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getGLXWindow(window: Window) error{NoWindowContext}!*anyopaque { pub fn getGLXWindow(window: Window) error{NoWindowContext}!*anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const win = native.glfwGetGLXWindow(@ptrCast(*native.GLFWwindow, window.handle)); const win = native.glfwGetGLXWindow(@ptrCast(*native.GLFWwindow, window.handle));
if (win != 0) return @ptrCast(*anyopaque, win);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e), Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e),
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, win); unreachable;
} }
/// Returns the `*wl_display` used by GLFW. /// Returns the `*wl_display` used by GLFW.
@ -322,12 +328,12 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getWaylandDisplay() *anyopaque { pub fn getWaylandDisplay() *anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const display = native.glfwGetWaylandDisplay(); if (native.glfwGetWaylandDisplay()) |display| return @ptrCast(*anyopaque, display);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, display); unreachable;
} }
/// Returns the `*wl_output` of the specified monitor. /// Returns the `*wl_output` of the specified monitor.
@ -337,12 +343,12 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getWaylandMonitor(monitor: Monitor) *anyopaque { pub fn getWaylandMonitor(monitor: Monitor) *anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const mon = native.glfwGetWaylandMonitor(@ptrCast(*native.GLFWmonitor, monitor.handle)); if (native.glfwGetWaylandMonitor(@ptrCast(*native.GLFWmonitor, monitor.handle))) |mon| return @ptrCast(*anyopaque, mon);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, mon); unreachable;
} }
/// Returns the `*wl_surface` of the specified window. /// Returns the `*wl_surface` of the specified window.
@ -352,12 +358,12 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getWaylandWindow(window: Window) *anyopaque { pub fn getWaylandWindow(window: Window) *anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const win = native.glfwGetWaylandWindow(@ptrCast(*native.GLFWwindow, window.handle)); if (native.glfwGetWaylandWindow(@ptrCast(*native.GLFWwindow, window.handle))) |win| return @ptrCast(*anyopaque, win);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, win); unreachable;
} }
/// Returns the `EGLDisplay` used by GLFW. /// Returns the `EGLDisplay` used by GLFW.
@ -368,11 +374,12 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getEGLDisplay() *anyopaque { pub fn getEGLDisplay() *anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const display = native.glfwGetEGLDisplay(); const display = native.glfwGetEGLDisplay();
if (display != native.EGL_NO_DISPLAY) return @ptrCast(*anyopaque, display);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, display); unreachable;
} }
/// Returns the `EGLContext` of the specified window. /// Returns the `EGLContext` of the specified window.
@ -383,12 +390,13 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getEGLContext(window: Window) error{NoWindowContext}!*anyopaque { pub fn getEGLContext(window: Window) error{NoWindowContext}!*anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const context = native.glfwGetEGLContext(@ptrCast(*native.GLFWwindow, window.handle)); const context = native.glfwGetEGLContext(@ptrCast(*native.GLFWwindow, window.handle));
if (context != native.EGL_NO_CONTEXT) return @ptrCast(*anyopaque, context);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e), Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e),
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, context); unreachable;
} }
/// Returns the `EGLSurface` of the specified window. /// Returns the `EGLSurface` of the specified window.
@ -399,12 +407,13 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getEGLSurface(window: Window) error{NoWindowContext}!*anyopaque { pub fn getEGLSurface(window: Window) error{NoWindowContext}!*anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const surface = native.glfwGetEGLSurface(@ptrCast(*native.GLFWwindow, window.handle)); const surface = native.glfwGetEGLSurface(@ptrCast(*native.GLFWwindow, window.handle));
if (surface != native.EGL_NO_SURFACE) return @ptrCast(*anyopaque, surface);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e), Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e),
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, surface); unreachable;
} }
pub const OSMesaColorBuffer = struct { pub const OSMesaColorBuffer = struct {
@ -423,13 +432,19 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getOSMesaColorBuffer(window: Window) error{ PlatformError, NoWindowContext }!OSMesaColorBuffer { pub fn getOSMesaColorBuffer(window: Window) error{ PlatformError, NoWindowContext }!OSMesaColorBuffer {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
var buf: OSMesaColorBuffer = undefined; var buf: OSMesaColorBuffer = undefined;
_ = native.glfwGetOSMesaColorBuffer(@ptrCast(*native.GLFWwindow, window.handle), &buf.width, &buf.height, &buf.format, &buf.buffer); if (native.glfwGetOSMesaColorBuffer(
@ptrCast(*native.GLFWwindow, window.handle),
&buf.width,
&buf.height,
&buf.format,
&buf.buffer,
) == native.GLFW_TRUE) return buf;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.PlatformError, Error.NoWindowContext => |e| @errSetCast(error{ PlatformError, NoWindowContext }, e), Error.PlatformError, Error.NoWindowContext => |e| @errSetCast(error{ PlatformError, NoWindowContext }, e),
else => unreachable, else => unreachable,
}; };
return buf; unreachable;
} }
pub const OSMesaDepthBuffer = struct { pub const OSMesaDepthBuffer = struct {
@ -448,13 +463,19 @@ pub fn Native(comptime options: BackendOptions) type {
pub fn getOSMesaDepthBuffer(window: Window) error{ PlatformError, NoWindowContext }!OSMesaDepthBuffer { pub fn getOSMesaDepthBuffer(window: Window) error{ PlatformError, NoWindowContext }!OSMesaDepthBuffer {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
var buf: OSMesaDepthBuffer = undefined; var buf: OSMesaDepthBuffer = undefined;
_ = native.glfwGetOSMesaDepthBuffer(@ptrCast(*native.GLFWwindow, window.handle), &buf.width, &buf.height, &buf.bytes_per_value, &buf.buffer); if (native.glfwGetOSMesaDepthBuffer(
@ptrCast(*native.GLFWwindow, window.handle),
&buf.width,
&buf.height,
&buf.bytes_per_value,
&buf.buffer,
) == native.GLFW_TRUE) return buf;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.PlatformError, Error.NoWindowContext => |e| @errSetCast(error{ PlatformError, NoWindowContext }, e), Error.PlatformError, Error.NoWindowContext => |e| @errSetCast(error{ PlatformError, NoWindowContext }, e),
else => unreachable, else => unreachable,
}; };
return buf; unreachable;
} }
/// Returns the 'OSMesaContext' of the specified window. /// Returns the 'OSMesaContext' of the specified window.
@ -464,13 +485,13 @@ pub fn Native(comptime options: BackendOptions) type {
/// thread_safety: This function may be called from any thread. Access is not synchronized. /// thread_safety: This function may be called from any thread. Access is not synchronized.
pub fn getOSMesaContext(window: Window) error{NoWindowContext}!*anyopaque { pub fn getOSMesaContext(window: Window) error{NoWindowContext}!*anyopaque {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const context = native.glfwGetOSMesa(@ptrCast(*native.GLFWwindow, window.handle)); if (native.glfwGetOSMesaContext(@ptrCast(*native.GLFWwindow, window.handle))) |context| return @ptrCast(*anyopaque, context);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e), Error.NoWindowContext => |e| @errSetCast(error{NoWindowContext}, e),
else => unreachable, else => unreachable,
}; };
return @ptrCast(*anyopaque, context); unreachable;
} }
}; };
} }

View file

@ -57,12 +57,11 @@ pub inline fn makeContextCurrent(window: ?Window) error{ NoWindowContext, Platfo
/// see also: context_current, glfwMakeContextCurrent /// see also: context_current, glfwMakeContextCurrent
pub inline fn getCurrentContext() std.mem.Allocator.Error!?Window { pub inline fn getCurrentContext() std.mem.Allocator.Error!?Window {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const handle = c.glfwGetCurrentContext(); if (c.glfwGetCurrentContext()) |handle| return try Window.from(handle);
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
if (handle) |h| return try Window.from(h);
return null; return null;
} }
@ -194,9 +193,8 @@ pub const GLProc = fn () callconv(.C) void;
/// see also: context_glext, glfwExtensionSupported /// see also: context_glext, glfwExtensionSupported
pub fn getProcAddress(proc_name: [*:0]const u8) callconv(.C) ?GLProc { pub fn getProcAddress(proc_name: [*:0]const u8) callconv(.C) ?GLProc {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const proc_address = c.glfwGetProcAddress(proc_name); if (c.glfwGetProcAddress(proc_name)) |proc_address| return proc_address;
getError() catch |err| @panic(@errorName(err)); getError() catch |err| @panic(@errorName(err));
if (proc_address) |addr| return addr;
return null; return null;
} }

View file

@ -30,11 +30,12 @@ const internal_debug = @import("internal_debug.zig");
pub inline fn getTime() f64 { pub inline fn getTime() f64 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const time = c.glfwGetTime(); const time = c.glfwGetTime();
if (time != 0) return time;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return time; unreachable;
} }
/// Sets the GLFW time. /// Sets the GLFW time.
@ -88,11 +89,12 @@ pub inline fn setTime(time: f64) void {
pub inline fn getTimerValue() u64 { pub inline fn getTimerValue() u64 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const value = c.glfwGetTimerValue(); const value = c.glfwGetTimerValue();
if (value != 0) return value;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return value; unreachable;
} }
/// Returns the frequency, in Hz, of the raw timer. /// Returns the frequency, in Hz, of the raw timer.
@ -107,11 +109,12 @@ pub inline fn getTimerValue() u64 {
pub inline fn getTimerFrequency() u64 { pub inline fn getTimerFrequency() u64 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const frequency = c.glfwGetTimerFrequency(); const frequency = c.glfwGetTimerFrequency();
if (frequency != 0) frequency;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
else => unreachable, else => unreachable,
}; };
return frequency; unreachable;
} }
test "getTime" { test "getTime" {

View file

@ -62,13 +62,13 @@ pub inline fn vulkanSupported() bool {
pub inline fn getRequiredInstanceExtensions() error{APIUnavailable}![][*:0]const u8 { pub inline fn getRequiredInstanceExtensions() error{APIUnavailable}![][*:0]const u8 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
var count: u32 = 0; var count: u32 = 0;
const extensions = c.glfwGetRequiredInstanceExtensions(&count); if (c.glfwGetRequiredInstanceExtensions(&count)) |extensions| return @ptrCast([*][*:0]const u8, extensions)[0..count];
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.APIUnavailable => @errSetCast(error{APIUnavailable}, err), Error.APIUnavailable => @errSetCast(error{APIUnavailable}, err),
else => unreachable, else => unreachable,
}; };
return @ptrCast([*][*:0]const u8, extensions)[0..count]; unreachable;
} }
/// Vulkan API function pointer type. /// Vulkan API function pointer type.
@ -111,9 +111,8 @@ pub const VKProc = fn () callconv(.C) void;
/// @thread_safety This function may be called from any thread. /// @thread_safety This function may be called from any thread.
pub fn getInstanceProcAddress(vk_instance: ?*anyopaque, proc_name: [*:0]const u8) callconv(.C) ?VKProc { pub fn getInstanceProcAddress(vk_instance: ?*anyopaque, proc_name: [*:0]const u8) callconv(.C) ?VKProc {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const proc_address = c.glfwGetInstanceProcAddress(if (vk_instance) |v| @ptrCast(c.VkInstance, v) else null, proc_name); if (c.glfwGetInstanceProcAddress(if (vk_instance) |v| @ptrCast(c.VkInstance, v) else null, proc_name)) |proc_address| return proc_address;
getError() catch |err| @panic(@errorName(err)); getError() catch |err| @panic(@errorName(err));
if (proc_address) |addr| return addr;
return null; return null;
} }
@ -223,6 +222,7 @@ pub inline fn createWindowSurface(vk_instance: anytype, window: Window, vk_alloc
if (vk_allocation_callbacks == null) null else @ptrCast(*const c.VkAllocationCallbacks, @alignCast(@alignOf(c.VkAllocationCallbacks), vk_allocation_callbacks)), if (vk_allocation_callbacks == null) null else @ptrCast(*const c.VkAllocationCallbacks, @alignCast(@alignOf(c.VkAllocationCallbacks), vk_allocation_callbacks)),
@ptrCast(*c.VkSurfaceKHR, @alignCast(@alignOf(c.VkSurfaceKHR), vk_surface_khr)), @ptrCast(*c.VkSurfaceKHR, @alignCast(@alignOf(c.VkSurfaceKHR), vk_surface_khr)),
); );
if (v == c.VK_SUCCESS) return v;
getError() catch |err| return switch (err) { getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable, Error.NotInitialized => unreachable,
Error.InvalidValue => @panic("Attempted to use window with client api to create vulkan surface."), Error.InvalidValue => @panic("Attempted to use window with client api to create vulkan surface."),
@ -231,7 +231,7 @@ pub inline fn createWindowSurface(vk_instance: anytype, window: Window, vk_alloc
=> @errSetCast(error{ APIUnavailable, PlatformError }, err), => @errSetCast(error{ APIUnavailable, PlatformError }, err),
else => unreachable, else => unreachable,
}; };
return v; unreachable;
} }
test "vulkanSupported" { test "vulkanSupported" {