add setClipboard and getClipboard to machine (#5)

This commit is contained in:
Apache 2023-02-18 12:14:41 -06:00 committed by GitHub
parent 1dbbb23ff6
commit 03e0992328
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 0 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);