glfw: force init in clipboard.zig

This commit is contained in:
InKryption 2021-11-21 15:53:19 +01:00 committed by Stephen Gutekanst
parent 400e86888b
commit fba8bb1cb2
2 changed files with 16 additions and 4 deletions

View file

@ -4,6 +4,8 @@ const c = @import("c.zig").c;
const Error = @import("errors.zig").Error;
const getError = @import("errors.zig").getError;
const internal_debug = @import("internal_debug.zig");
/// Sets the clipboard to the specified string.
///
/// This function sets the system clipboard to the specified, UTF-8 encoded string.
@ -17,9 +19,13 @@ const getError = @import("errors.zig").getError;
/// @thread_safety This function must only be called from the main thread.
///
/// see also: clipboard, glfwGetClipboardString
pub inline fn setClipboardString(value: [*:0]const u8) Error!void {
pub inline fn setClipboardString(value: [*:0]const u8) error{ PlatformError }!void {
internal_debug.assertInitialized();
c.glfwSetClipboardString(null, value);
try getError();
getError() catch |err| return switch (err) {
Error.PlatformError => err,
else => unreachable,
};
}
/// Returns the contents of the clipboard as a string.
@ -39,9 +45,13 @@ pub inline fn setClipboardString(value: [*:0]const u8) Error!void {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: clipboard, glfwSetClipboardString
pub inline fn getClipboardString() Error![:0]const u8 {
pub inline fn getClipboardString() error{ PlatformError }![:0]const u8 {
internal_debug.assertInitialized();
const value = c.glfwGetClipboardString(null);
try getError();
getError() catch |err| return switch (err) {
Error.PlatformError => err,
else => unreachable,
};
return std.mem.span(value);
}

View file

@ -1,6 +1,8 @@
const std = @import("std");
const zig_builtin = @import("builtin");
// TODO: Consider if this idea could also be applied to prevent 'PlatformError'
const debug_mode = (zig_builtin.mode == .Debug);
var glfw_initialized = if (debug_mode) false else @as(void, {});
pub inline fn toggleInitialized() void {