From 0c393edfdf3e9e8dead6df1c93242e419bedbaa5 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 9 May 2013 14:32:21 +0000 Subject: [PATCH] added emu.keypost function to lua, and made autoboot_command execute that one instead of direct execution, note that you need to add \n for new line at the end now (nw) --- src/emu/luaengine.c | 42 ++++++++++++++++++++++++++++++++++++++---- src/emu/luaengine.h | 6 ++++-- src/emu/machine.c | 9 ++++----- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/emu/luaengine.c b/src/emu/luaengine.c index ae546e50a50..132aa52da75 100644 --- a/src/emu/luaengine.c +++ b/src/emu/luaengine.c @@ -53,14 +53,27 @@ lua_engine* lua_engine::luaThis = NULL; // emu_gamename - returns game full name //------------------------------------------------- -int lua_engine::emu_gamename(lua_State *L) { +int lua_engine::emu_gamename(lua_State *L) +{ lua_pushstring(L, luaThis->machine().system().description); return 1; } +//------------------------------------------------- +// emu_keypost - post keys to natural keyboard +//------------------------------------------------- + +int lua_engine::emu_keypost(lua_State *L) +{ + const char *keys = luaL_checkstring(L,1); + luaThis->machine().ioport().natkeyboard().post_utf8(keys); + return 1; +} + static const struct luaL_Reg emu_funcs [] = { { "gamename", lua_engine::emu_gamename }, + { "keypost", lua_engine::emu_keypost }, { NULL, NULL } /* sentinel */ }; @@ -141,12 +154,11 @@ void lua_engine::report_errors(int status) } } - //------------------------------------------------- -// execute - setup lua VM and load script +// createvm - setup lua VM and load script //------------------------------------------------- -void lua_engine::execute(const char *filename) +void lua_engine::createvm() { close(); @@ -155,13 +167,35 @@ void lua_engine::execute(const char *filename) luaL_openlibs(m_lua_state); luaL_requiref(m_lua_state, "emu", luaopen_emu, 1); lua_sethook(m_lua_state, hook, LUA_MASKLINE, 0); +} +//------------------------------------------------- +// execute - load and execute script +//------------------------------------------------- + +void lua_engine::execute(const char *filename) +{ + createvm(); + int s = luaL_loadfile(m_lua_state, filename); report_errors(s); mame_printf_verbose("[LUA] Start executing script\n"); } +//------------------------------------------------- +// execute_string - execute script from string +//------------------------------------------------- + +void lua_engine::execute_string(const char *value) +{ + createvm(); + + int s = luaL_loadstring(m_lua_state, value); + report_errors(s); + + mame_printf_verbose("[LUA] Start executing script\n"); +} //------------------------------------------------- // lua_execute - execute slice of lua script // this callback is hooked to frame notification diff --git a/src/emu/luaengine.h b/src/emu/luaengine.h index 23b75243fb7..0375312228a 100644 --- a/src/emu/luaengine.h +++ b/src/emu/luaengine.h @@ -62,12 +62,14 @@ public: void lua_execute(); void report_errors(int status); - void execute(const char *filename); - + void createvm(); + void execute(const char *filename); + void execute_string(const char *value); void close(); //static static int emu_gamename(lua_State *L); + static int emu_keypost(lua_State *L); private: // internal state running_machine & m_machine; // reference to our machine diff --git a/src/emu/machine.c b/src/emu/machine.c index 1c0f57dc7e4..2bf51875ccd 100644 --- a/src/emu/machine.c +++ b/src/emu/machine.c @@ -239,11 +239,10 @@ TIMER_CALLBACK_MEMBER(running_machine::autoboot_callback) m_lua_engine.execute(options().autoboot_script()); } if (strlen(options().autoboot_command())!=0) { - astring val = astring(options().autoboot_command()); - val.replace("\\n","\n"); - val.replace("\\r","\r"); - ioport().natkeyboard().post_utf8(val); - ioport().natkeyboard().post_utf8("\r"); + astring cmd = astring(options().autoboot_command()); + cmd.replace("'","\\'"); + astring val = astring("emu.keypost('",cmd,"')"); + m_lua_engine.execute_string(val); } }