Convert the clipboard module to the ffi

This commit is contained in:
Thomas Goyne 2014-07-28 14:19:57 -07:00
parent 97a19d4fe2
commit 5b47758539
2 changed files with 16 additions and 15 deletions

View File

@ -12,4 +12,11 @@
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
return aegisub.__init_clipboard() local check = require 'aegisub.argcheck'
local ffi_util = require 'aegisub.ffi'
local impl = aegisub.__init_clipboard()
return {
get = function() return ffi_util.string(impl.get()) end,
set = check'string'(impl.set)
}

View File

@ -51,6 +51,7 @@
#include "utils.h" #include "utils.h"
#include <libaegisub/format.h> #include <libaegisub/format.h>
#include <libaegisub/lua/ffi.h>
#include <libaegisub/lua/modules.h> #include <libaegisub/lua/modules.h>
#include <libaegisub/lua/script_reader.h> #include <libaegisub/lua/script_reader.h>
#include <libaegisub/lua/utils.h> #include <libaegisub/lua/utils.h>
@ -118,20 +119,16 @@ namespace {
return 1; return 1;
} }
int clipboard_get(lua_State *L) const char *clipboard_get()
{ {
std::string data = GetClipboard(); std::string data = GetClipboard();
if (data.empty()) if (data.empty())
lua_pushnil(L); return nullptr;
else return strdup(data.c_str());
push_value(L, data);
return 1;
} }
int clipboard_set(lua_State *L) bool clipboard_set(const char *str)
{ {
std::string str(check_string(L, 1));
bool succeeded = false; bool succeeded = false;
#if wxUSE_OLE #if wxUSE_OLE
@ -142,20 +139,17 @@ namespace {
wxClipboard &cb = *wxTheClipboard; wxClipboard &cb = *wxTheClipboard;
#endif #endif
if (cb.Open()) { if (cb.Open()) {
succeeded = cb.SetData(new wxTextDataObject(to_wx(str))); succeeded = cb.SetData(new wxTextDataObject(wxString::FromUTF8(str)));
cb.Close(); cb.Close();
cb.Flush(); cb.Flush();
} }
lua_pushboolean(L, succeeded); return succeeded;
return 1;
} }
int clipboard_init(lua_State *L) int clipboard_init(lua_State *L)
{ {
lua_createtable(L, 0, 2); agi::lua::register_lib_table(L, {}, "get", clipboard_get, "set", clipboard_set);
set_field<clipboard_get>(L, "get");
set_field<clipboard_set>(L, "set");
return 1; return 1;
} }