diff --git a/Capy64/Core/SDL.cs b/Capy64/Core/SDL.cs index 8a94ac9..9e074c9 100644 --- a/Capy64/Core/SDL.cs +++ b/Capy64/Core/SDL.cs @@ -29,6 +29,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 4937104..a19c5b1 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 999ea5e..83387f7 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; @@ -22,6 +23,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Input; namespace Capy64.Runtime.Libraries; @@ -65,6 +67,16 @@ public class Machine : IPlugin name = "vibrate", function = L_Vibrate, }, + new() + { + name = "setClipboard", + function = L_SetClipboard, + }, + new() + { + name = "getClipboard", + function = L_GetClipboard, + }, new(), }; @@ -104,7 +116,12 @@ public class Machine : IPlugin { var newTitle = L.CheckString(1); - L.ArgumentCheck(!string.IsNullOrEmpty(newTitle), 1, "string must not be empty"); + if (string.IsNullOrEmpty(newTitle)) + { + newTitle = "Capy64 " + Capy64.Version; + } + + newTitle = newTitle[..Math.Min(newTitle.Length, 256)]; Capy64.Instance.Window.Title = newTitle; } @@ -114,6 +131,26 @@ public class Machine : IPlugin 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);