From 03e099232803c8caabfa351ceeff88b65d64a516 Mon Sep 17 00:00:00 2001 From: Apache <93100547+Apachedrag427@users.noreply.github.com> Date: Sat, 18 Feb 2023 12:14:41 -0600 Subject: [PATCH] add setClipboard and getClipboard to machine (#5) --- Capy64/Core/SDL.cs | 5 ++++ Capy64/Extensions/Bindings/SDL2.cs | 4 +++ Capy64/Runtime/Libraries/Machine.cs | 38 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/Capy64/Core/SDL.cs b/Capy64/Core/SDL.cs index a2a4423..25306f1 100644 --- a/Capy64/Core/SDL.cs +++ b/Capy64/Core/SDL.cs @@ -24,6 +24,11 @@ public class SDL return SDL2.UTF8_ToManaged(SDL2.Native_SDL_GetClipboardText(), true); } + public static void SetClipboardText(string contents) + { + SDL2.Native_SDL_SetClipboardText(contents); + } + public static bool HasClipboardText() { return SDL2.SDL_HasClipboardText() == 1; diff --git a/Capy64/Extensions/Bindings/SDL2.cs b/Capy64/Extensions/Bindings/SDL2.cs index 55142b7..2ce642e 100644 --- a/Capy64/Extensions/Bindings/SDL2.cs +++ b/Capy64/Extensions/Bindings/SDL2.cs @@ -41,6 +41,10 @@ public partial class SDL2 [UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] internal static partial int SDL_HasClipboardText(); + [LibraryImport(SDL, EntryPoint = "SDL_SetClipboardText", StringMarshalling = StringMarshalling.Utf8)] + [UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] + internal static partial int Native_SDL_SetClipboardText(string contents); + [LibraryImport(SDL)] [UnmanagedCallConv(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] internal static partial void SDL_free(IntPtr memblock); diff --git a/Capy64/Runtime/Libraries/Machine.cs b/Capy64/Runtime/Libraries/Machine.cs index 06e2e33..a245181 100644 --- a/Capy64/Runtime/Libraries/Machine.cs +++ b/Capy64/Runtime/Libraries/Machine.cs @@ -14,6 +14,7 @@ // limitations under the License. using Capy64.API; +using Capy64.Core; using KeraLua; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; @@ -61,6 +62,16 @@ public class Machine : IComponent name = "vibrate", function = L_Vibrate, }, + new() + { + name = "setClipboard", + function = L_SetClipboard, + }, + new() + { + name = "getClipboard", + function = L_GetClipboard, + }, new(), }; @@ -99,6 +110,13 @@ public class Machine : IComponent if (!L.IsNoneOrNil(1)) { var newTitle = L.CheckString(1); + + if (string.IsNullOrEmpty(newTitle)) + { + newTitle = "Capy64 " + Capy64.Version; + } + + newTitle = newTitle[..Math.Min(newTitle.Length, 256)]; Capy64.Instance.Window.Title = newTitle; } @@ -108,6 +126,26 @@ public class Machine : IComponent return 1; } + private static int L_GetClipboard(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + L.PushString(SDL.GetClipboardText()); + + return 1; + } + + private static int L_SetClipboard(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + var newcontents = L.CheckString(1); + + SDL.SetClipboardText(newcontents); + + return 0; + } + private static int L_Version(IntPtr state) { var L = Lua.FromIntPtr(state);