Skip to content

Commit

Permalink
add clipboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrec authored and ikskuh committed Apr 6, 2022
1 parent 183b45c commit 47b8ab4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/binding/sdl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ pub extern fn SDL_ComposeCustomBlendMode(srcColorFactor: SDL_BlendFactor, dstCol
pub extern fn SDL_SetClipboardText(text: [*c]const u8) c_int;
pub extern fn SDL_GetClipboardText() [*c]u8;
pub extern fn SDL_HasClipboardText() SDL_bool;
pub extern fn SDL_free(data: [*c]const u8) void;
pub const SDL_AudioFormat = u16;
pub const SDL_AudioCallback = ?fn (?*anyopaque, [*c]u8, c_int) callconv(.C) void;
pub const SDL_AudioSpec = extern struct {
Expand Down
22 changes: 22 additions & 0 deletions src/wrapper/sdl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,28 @@ pub const Keycode = enum(c.SDL_Keycode) {
_,
};

pub const Clipboard = struct {
pub fn get() !?[]const u8 {
if (c.SDL_HasClipboardText() == c.SDL_FALSE)
return null;
const c_string = c.SDL_GetClipboardText();
const txt = std.mem.sliceTo(c_string, 0);
if (txt.len == 0) {
c.SDL_free(c_string);
return makeError();
}
return txt;
}
/// free is to be called with a previously fetched clipboard content
pub fn free(txt: []const u8) void {
c.SDL_free(@ptrCast([*c]const u8, txt));
}
pub fn set(txt: []const u8) !void {
if (c.SDL_SetClipboardText(@ptrCast([*c]const u8, txt)) != 0)
return makeError();
}
};

pub fn getTicks() u32 {
return c.SDL_GetTicks();
}
Expand Down

0 comments on commit 47b8ab4

Please sign in to comment.