From f985171b4cdab352bc6642ea3ab3501b0acd1269 Mon Sep 17 00:00:00 2001 From: cracyc Date: Thu, 13 Jul 2017 21:30:29 -0500 Subject: plugins/cheat: poll for setting hotkeys [Carl] --- plugins/cheat/init.lua | 152 +++---------------------- plugins/cheat/keycodemap.lua | 241 ---------------------------------------- src/frontend/mame/luaengine.cpp | 45 +++++++- 3 files changed, 58 insertions(+), 380 deletions(-) delete mode 100644 plugins/cheat/keycodemap.lua diff --git a/plugins/cheat/init.lua b/plugins/cheat/init.lua index 1ea52abdbe3..e0f284437aa 100644 --- a/plugins/cheat/init.lua +++ b/plugins/cheat/init.lua @@ -121,22 +121,7 @@ function cheat.startplugin() for num, val in ipairs(hotkeys) do for num, cheat in pairs(cheats) do if val.desc == cheat.desc then - cheat.hotkeys = {} - local keymap = require("cheat/keycodemap") - cheat.hotkeys.keys = manager:machine():input():seq_from_tokens(val.keys) - local keysstr = {} - val.keys:gsub("([^ ]+)", function(s) - if s:find("KEYCODE_", 1, true) then - keysstr[#keysstr + 1] = keymap[s] - elseif s:find("JOYCODE_", 1, true) then - local stick, button = s:match("JOYCODE_([0-9]+)_BUTTON([0-9]+)") - if stick and button then - keysstr[#keysstr + 1] = string.format("Joy%dBut%d", stick, button - 1) - end - end - end) - cheat.hotkeys.keysstr = keysstr - cheat.hotkeys.pressed = false + cheat.hotkeys = {pressed = false, keys = manager:machine():input():seq_from_tokens(val.keys)} end end end @@ -146,21 +131,7 @@ function cheat.startplugin() local hotkeys = {} for num, cheat in ipairs(cheats) do if cheat.hotkeys then - local keymap = require("cheat/keycodemap") - local hotkey = {} - hotkey.desc = cheat.desc - hotkey.keys = "" - for num2, key in ipairs(cheat.hotkeys.keysstr) do - if #hotkey.keys > 0 then - hotkey.keys = hotkey.keys .. " " - end - if key:find("Joy", 1, true) then - local stick, button = key:match("Joy([0-9]+)But([0-9]+)") - hotkey.keys = hotkey.keys .. string.format("JOYCODE_%d_BUTTON%d", stick, button + 1) - else - hotkey.keys = hotkey.keys .. keymap[key] - end - end + local hotkey = {desc = cheat.desc, keys = manager:machine():input():seq_to_tokens(cheat.hotkeys.keys)} if hotkey.keys ~= "" then hotkeys[#hotkeys + 1] = hotkey end @@ -351,10 +322,6 @@ function cheat.startplugin() end local hotkeymenu = false - local hotkeysel = 0 - local hotkey = 1 - local hotmod = 1 - local hotmode = 1 local hotkeylist = {} local function run_if(func) if func then func() end return func or false end local function is_oneshot(cheat) return cheat.script and not cheat.script.run and not cheat.script.off end @@ -362,110 +329,22 @@ function cheat.startplugin() local function menu_populate() local menu = {} if hotkeymenu then - if hotkeysel > 0 then - return hotkeylist[hotkeysel].pop() - end - local keys = {"1","2","3","4","5","6","7","8","9","0"} - local mods = {"LSHFT","RSHFT","LALT","RALT","LCTRL","RCTRL","LWIN","RWIN","MENU"} - local mode = {"Key", "Joy"} - - local function hkpopfunc(cheat) - local function menu_lim(val, min, max) - if min == max then - return 0 - elseif val == min then - return "r" - elseif val == max then - return "l" - else - return "lr" - end - end - - local hkmenu = {} - hkmenu[1] = {"Set hotkey", "", "off"} - hkmenu[2] = {cheat.desc, "", "off"} - hkmenu[3] = {"Current Keys", cheat.hotkeys and table.concat(cheat.hotkeys.keysstr, " ") or "None", "off"} - hkmenu[4] = {"---", "", "off"} - hkmenu[5] = {"Hotkey Type", mode[hotmode], menu_lim(hotmode, 1, 2)} - if hotmode == 2 then - hkmenu[6] = {"Stick", hotkey, menu_lim(hotkey, 1, 9)} - hkmenu[7] = {"Button", hotmod, menu_lim(hotmod, 0, 9)} - else - hkmenu[6] = {"Key", keys[hotkey], menu_lim(hotkey, 1, #keys)} - hkmenu[7] = {"Modifier", mods[hotmod], menu_lim(hotmod, 1, #mods)} - end - hkmenu[8] = {"---", "", ""} - hkmenu[9] = {"Done", "", ""} - hkmenu[10] = {"Clear and Exit", "", ""} - hkmenu[11] = {"Cancel", "", ""} - return hkmenu - end - - local function hkcbfunc(cheat, index, event) - if event == "right" then - if index == 5 and hotmode == 1 then - hotmode = 2 - hotkey = 1 - hotmod = 0 - return true - elseif index == 6 then - hotkey = math.min(hotkey + 1, hotmode == 2 and 9 or #keys) - return true - elseif index == 7 then - hotmod = math.min(hotmod + 1, hotmode == 2 and 9 or #mods) - return true - end - elseif event == "left" then - if index == 5 and hotmode == 2 then - hotmode = 1 - hotkey = 1 - hotmod = 1 - return true - elseif index == 6 then - hotkey = math.max(hotkey - 1, 1) - return true - elseif index == 7 then - hotmod = math.max(hotmod - 1, hotmode == 2 and 0 or 1) - return true - end - elseif event == "select" then - if index == 9 then - local keymap = require("cheat/keycodemap") - cheat.hotkeys = {} - if hotmode == 2 then - cheat.hotkeys.keys = manager:machine():input():seq_from_tokens(string.format("JOYCODE_%d_BUTTON%d", hotkey, hotmod + 1)) - cheat.hotkeys.keysstr = {string.format("Joy%dBut%d", hotkey, hotmod)} - else - cheat.hotkeys.keys = manager:machine():input():seq_from_tokens(keymap[keys[hotkey]] .. " " .. keymap[mods[hotmod]]) - cheat.hotkeys.keysstr = {keys[hotkey], mods[hotmod]} - end - cheat.hotkeys.pressed = false - hotkeysel = 0 - hotkeymenu = false - return true - elseif index == 10 then - cheat.hotkeys = nil - hotkeysel = 0 - hotkeymenu = false - return true - elseif index == 11 then - hotkeysel = 0 - return true - end - end - return false - end - - menu[1] = {"Select cheat to set hotkey", "", "off"} menu[2] = {"---", "", "off"} hotkeylist = {} + + local function hkcbfunc(cheat) + local input = manager:machine():input() + input:seq_poll_start("switch") + local time = os.clock() + while (not input:seq_poll()) and (os.clock() < time + 1) do end + cheat.hotkeys = {pressed = false, keys = input:seq_poll_final()} + end + for num, cheat in ipairs(cheats) do if cheat.script then - menu[#menu + 1] = {cheat.desc, " ", ""} - hotkeylist[#hotkeylist + 1] = { pop = function() return hkpopfunc(cheat) end, - cb = function(index, event) return hkcbfunc(cheat, index, event) end } + menu[#menu + 1] = {cheat.desc, cheat.hotkeys and manager:machine():input():seq_name(cheat.hotkeys.keys) or "None", ""} + hotkeylist[#hotkeylist + 1] = function() return hkcbfunc(cheat) end end end menu[#menu + 1] = {"---", "", ""} @@ -525,13 +404,10 @@ function cheat.startplugin() local function menu_callback(index, event) manager:machine():popmessage() if hotkeymenu then - if hotkeysel > 0 then - return hotkeylist[hotkeysel].cb(index, event) - end if event == "select" then index = index - 2 if index >= 1 and index <= #hotkeylist then - hotkeysel = index + hotkeylist[index]() return true elseif index == #hotkeylist + 2 then hotkeymenu = false diff --git a/plugins/cheat/keycodemap.lua b/plugins/cheat/keycodemap.lua deleted file mode 100644 index a607afac5b8..00000000000 --- a/plugins/cheat/keycodemap.lua +++ /dev/null @@ -1,241 +0,0 @@ -local keycodemap = -{ - ["A"] = "KEYCODE_A", - ["B"] = "KEYCODE_B", - ["C"] = "KEYCODE_C", - ["D"] = "KEYCODE_D", - ["E"] = "KEYCODE_E", - ["F"] = "KEYCODE_F", - ["G"] = "KEYCODE_G", - ["H"] = "KEYCODE_H", - ["I"] = "KEYCODE_I", - ["J"] = "KEYCODE_J", - ["K"] = "KEYCODE_K", - ["L"] = "KEYCODE_L", - ["M"] = "KEYCODE_M", - ["N"] = "KEYCODE_N", - ["O"] = "KEYCODE_O", - ["P"] = "KEYCODE_P", - ["Q"] = "KEYCODE_Q", - ["R"] = "KEYCODE_R", - ["S"] = "KEYCODE_S", - ["T"] = "KEYCODE_T", - ["U"] = "KEYCODE_U", - ["V"] = "KEYCODE_V", - ["W"] = "KEYCODE_W", - ["X"] = "KEYCODE_X", - ["Y"] = "KEYCODE_Y", - ["Z"] = "KEYCODE_Z", - ["0"] = "KEYCODE_0", - ["1"] = "KEYCODE_1", - ["2"] = "KEYCODE_2", - ["3"] = "KEYCODE_3", - ["4"] = "KEYCODE_4", - ["5"] = "KEYCODE_5", - ["6"] = "KEYCODE_6", - ["7"] = "KEYCODE_7", - ["8"] = "KEYCODE_8", - ["9"] = "KEYCODE_9", - ["F1"] = "KEYCODE_F1", - ["F2"] = "KEYCODE_F2", - ["F3"] = "KEYCODE_F3", - ["F4"] = "KEYCODE_F4", - ["F5"] = "KEYCODE_F5", - ["F6"] = "KEYCODE_F6", - ["F7"] = "KEYCODE_F7", - ["F8"] = "KEYCODE_F8", - ["F9"] = "KEYCODE_F9", - ["F10"] = "KEYCODE_F10", - ["F11"] = "KEYCODE_F11", - ["F12"] = "KEYCODE_F12", - ["F13"] = "KEYCODE_F13", - ["F14"] = "KEYCODE_F14", - ["F15"] = "KEYCODE_F15", - ["F16"] = "KEYCODE_F16", - ["F17"] = "KEYCODE_F17", - ["F18"] = "KEYCODE_F18", - ["F19"] = "KEYCODE_F19", - ["F20"] = "KEYCODE_F20", - ["ESC"] = "KEYCODE_ESC", - ["~"] = "KEYCODE_TILDE", - ["-"] = "KEYCODE_MINUS", - ["="] = "KEYCODE_EQUALS", - ["BS"] = "KEYCODE_BACKSPACE", - ["TAB"] = "KEYCODE_TAB", - ["["] = "KEYCODE_OPENBRACE", - ["]"] = "KEYCODE_CLOSEBRACE", - ["ENTER"] = "KEYCODE_ENTER", - [":"] = "KEYCODE_COLON", - ["'"] = "KEYCODE_QUOTE", - ["/"] = "KEYCODE_BACKSLASH", - ["/_2"] = "KEYCODE_BACKSLASH2", - [","] = "KEYCODE_COMMA", - ["."] = "KEYCODE_STOP", - ["/"] = "KEYCODE_SLASH", - ["SPC"] = "KEYCODE_SPACE", - ["INSERT"] = "KEYCODE_INSERT", - ["DEL"] = "KEYCODE_DEL", - ["HOME"] = "KEYCODE_HOME", - ["END"] = "KEYCODE_END", - ["PGUP"] = "KEYCODE_PGUP", - ["PGDN"] = "KEYCODE_PGDN", - ["LEFT"] = "KEYCODE_LEFT", - ["RIGHT"] = "KEYCODE_RIGHT", - ["UP"] = "KEYCODE_UP", - ["DOWN"] = "KEYCODE_DOWN", - ["0_PAD"] = "KEYCODE_0_PAD", - ["1_PAD"] = "KEYCODE_1_PAD", - ["2_PAD"] = "KEYCODE_2_PAD", - ["3_PAD"] = "KEYCODE_3_PAD", - ["4_PAD"] = "KEYCODE_4_PAD", - ["5_PAD"] = "KEYCODE_5_PAD", - ["6_PAD"] = "KEYCODE_6_PAD", - ["7_PAD"] = "KEYCODE_7_PAD", - ["8_PAD"] = "KEYCODE_8_PAD", - ["9_PAD"] = "KEYCODE_9_PAD", - ["/_PAD"] = "KEYCODE_SLASH_PAD", - ["*"] = "KEYCODE_ASTERISK", - ["-_PAD"] = "KEYCODE_MINUS_PAD", - ["+_PAD"] = "KEYCODE_PLUS_PAD", - ["DEL_PAD"] = "KEYCODE_DEL_PAD", - ["ENTER_PAD"] = "KEYCODE_ENTER_PAD", - ["BS_PAD"] = "KEYCODE_BS_PAD", - ["TAB_PAD"] = "KEYCODE_TAB_PAD", - ["00_PAD"] = "KEYCODE_00_PAD", - ["000_PAD"] = "KEYCODE_000_PAD", - ["PRTSCR"] = "KEYCODE_PRTSCR", - ["PAUSE"] = "KEYCODE_PAUSE", - ["LSHFT"] = "KEYCODE_LSHIFT", - ["RSHFT"] = "KEYCODE_RSHIFT", - ["LCTRL"] = "KEYCODE_LCONTROL", - ["RCTRL"] = "KEYCODE_RCONTROL", - ["LALT"] = "KEYCODE_LALT", - ["RALT"] = "KEYCODE_RALT", - ["SCRLOCK"] = "KEYCODE_SCRLOCK", - ["NUMLOCK"] = "KEYCODE_NUMLOCK", - ["CAPSLOCK"] = "KEYCODE_CAPSLOCK", - ["LWIN"] = "KEYCODE_LWIN", - ["RWIN"] = "KEYCODE_RWIN", - ["MENU"] = "KEYCODE_MENU", - ["CANCEL"] = "KEYCODE_CANCEL", - ["KEYCODE_A"] = "A", - ["KEYCODE_B"] = "B", - ["KEYCODE_C"] = "C", - ["KEYCODE_D"] = "D", - ["KEYCODE_E"] = "E", - ["KEYCODE_F"] = "F", - ["KEYCODE_G"] = "G", - ["KEYCODE_H"] = "H", - ["KEYCODE_I"] = "I", - ["KEYCODE_J"] = "J", - ["KEYCODE_K"] = "K", - ["KEYCODE_L"] = "L", - ["KEYCODE_M"] = "M", - ["KEYCODE_N"] = "N", - ["KEYCODE_O"] = "O", - ["KEYCODE_P"] = "P", - ["KEYCODE_Q"] = "Q", - ["KEYCODE_R"] = "R", - ["KEYCODE_S"] = "S", - ["KEYCODE_T"] = "T", - ["KEYCODE_U"] = "U", - ["KEYCODE_V"] = "V", - ["KEYCODE_W"] = "W", - ["KEYCODE_X"] = "X", - ["KEYCODE_Y"] = "Y", - ["KEYCODE_Z"] = "Z", - ["KEYCODE_0"] = "0", - ["KEYCODE_1"] = "1", - ["KEYCODE_2"] = "2", - ["KEYCODE_3"] = "3", - ["KEYCODE_4"] = "4", - ["KEYCODE_5"] = "5", - ["KEYCODE_6"] = "6", - ["KEYCODE_7"] = "7", - ["KEYCODE_8"] = "8", - ["KEYCODE_9"] = "9", - ["KEYCODE_F1"] = "F1", - ["KEYCODE_F2"] = "F2", - ["KEYCODE_F3"] = "F3", - ["KEYCODE_F4"] = "F4", - ["KEYCODE_F5"] = "F5", - ["KEYCODE_F6"] = "F6", - ["KEYCODE_F7"] = "F7", - ["KEYCODE_F8"] = "F8", - ["KEYCODE_F9"] = "F9", - ["KEYCODE_F10"] = "F10", - ["KEYCODE_F11"] = "F11", - ["KEYCODE_F12"] = "F12", - ["KEYCODE_F13"] = "F13", - ["KEYCODE_F14"] = "F14", - ["KEYCODE_F15"] = "F15", - ["KEYCODE_F16"] = "F16", - ["KEYCODE_F17"] = "F17", - ["KEYCODE_F18"] = "F18", - ["KEYCODE_F19"] = "F19", - ["KEYCODE_F20"] = "F20", - ["KEYCODE_ESC"] = "ESC", - ["KEYCODE_TILDE"] = "~", - ["KEYCODE_MINUS"] = "-", - ["KEYCODE_EQUALS"] = "=", - ["KEYCODE_BACKSPACE"] = "BS", - ["KEYCODE_TAB"] = "TAB", - ["KEYCODE_OPENBRACE"] = "[", - ["KEYCODE_CLOSEBRACE"] = "]", - ["KEYCODE_ENTER"] = "ENTER", - ["KEYCODE_COLON"] = ":", - ["KEYCODE_QUOTE"] = "'", - ["KEYCODE_BACKSLASH"] = "/", - ["KEYCODE_BACKSLASH2"] = "/_2", - ["KEYCODE_COMMA"] = ",", - ["KEYCODE_STOP"] = ".", - ["KEYCODE_SLASH"] = "/", - ["KEYCODE_SPACE"] = "SPC", - ["KEYCODE_INSERT"] = "INSERT", - ["KEYCODE_DEL"] = "DEL", - ["KEYCODE_HOME"] = "HOME", - ["KEYCODE_END"] = "END", - ["KEYCODE_PGUP"] = "PGUP", - ["KEYCODE_PGDN"] = "PGDN", - ["KEYCODE_LEFT"] = "LEFT", - ["KEYCODE_RIGHT"] = "RIGHT", - ["KEYCODE_UP"] = "UP", - ["KEYCODE_DOWN"] = "DOWN", - ["KEYCODE_0_PAD"] = "0_PAD", - ["KEYCODE_1_PAD"] = "1_PAD", - ["KEYCODE_2_PAD"] = "2_PAD", - ["KEYCODE_3_PAD"] = "3_PAD", - ["KEYCODE_4_PAD"] = "4_PAD", - ["KEYCODE_5_PAD"] = "5_PAD", - ["KEYCODE_6_PAD"] = "6_PAD", - ["KEYCODE_7_PAD"] = "7_PAD", - ["KEYCODE_8_PAD"] = "8_PAD", - ["KEYCODE_9_PAD"] = "9_PAD", - ["KEYCODE_SLASH_PAD"] = "/_PAD", - ["KEYCODE_ASTERISK"] = "*", - ["KEYCODE_MINUS_PAD"] = "-_PAD", - ["KEYCODE_PLUS_PAD"] = "+_PAD", - ["KEYCODE_DEL_PAD"] = "DEL_PAD", - ["KEYCODE_ENTER_PAD"] = "ENTER_PAD", - ["KEYCODE_BS_PAD"] = "BS_PAD", - ["KEYCODE_TAB_PAD"] = "TAB_PAD", - ["KEYCODE_00_PAD"] = "00_PAD", - ["KEYCODE_000_PAD"] = "000_PAD", - ["KEYCODE_PRTSCR"] = "PRTSCR", - ["KEYCODE_PAUSE"] = "PAUSE", - ["KEYCODE_LSHIFT"] = "LSHFT", - ["KEYCODE_RSHIFT"] = "RSHFT", - ["KEYCODE_LCONTROL"] = "LCTRL", - ["KEYCODE_RCONTROL"] = "RCTRL", - ["KEYCODE_LALT"] = "LALT", - ["KEYCODE_RALT"] = "RALT", - ["KEYCODE_SCRLOCK"] = "SCRLOCK", - ["KEYCODE_NUMLOCK"] = "NUMLOCK", - ["KEYCODE_CAPSLOCK"] = "CAPSLOCK", - ["KEYCODE_LWIN"] = "LWIN", - ["KEYCODE_RWIN"] = "RWIN", - ["KEYCODE_MENU"] = "MENU", - ["KEYCODE_CANCEL"] = "CANCEL" -} - -return keycodemap diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp index 67d6cfecf17..1cf1f2133e4 100644 --- a/src/frontend/mame/luaengine.cpp +++ b/src/frontend/mame/luaengine.cpp @@ -139,6 +139,33 @@ namespace sol } }; template <> + struct checker + { + template + static bool check (lua_State* L, int index, Handler&& handler, record& tracking) + { + return stack::check(L, index, handler); + } + }; + template <> + struct getter + { + static input_item_class get(lua_State* L, int index, record& tracking) + { + const std::string item_class = stack::get(L, index); + if(item_class == "switch") + return ITEM_CLASS_SWITCH; + else if(item_class == "absolute" || item_class == "abs") + return ITEM_CLASS_ABSOLUTE; + else if(item_class == "relative" || item_class == "rel") + return ITEM_CLASS_RELATIVE; + else if(item_class == "maximum" || item_class == "max") + return ITEM_CLASS_MAXIMUM; + else + return ITEM_CLASS_INVALID; + } + }; + template <> struct pusher { static int push(lua_State* L, sol::buffer *buff) @@ -1425,15 +1452,31 @@ void lua_engine::initialize() /* machine:input() * input:code_from_token(token) - get input_code for KEYCODE_* string token * input:code_pressed(code) - get pressed state for input_code + * input:code_to_token(code) - get KEYCODE_* string token for code + * input:code_name(code) - get code friendly name * input:seq_from_tokens(tokens) - get input_seq for multiple space separated KEYCODE_* string tokens * input:seq_pressed(seq) - get pressed state for input_seq + * input:seq_to_token(seq) - get KEYCODE_* string tokens for seq + * input:seq_to_name(seq) - get seq friendly name */ sol().registry().new_usertype("input", "new", sol::no_constructor, "code_from_token", [](input_manager &input, const char *token) { return sol::make_user(input.code_from_token(token)); }, "code_pressed", [](input_manager &input, sol::user code) { return input.code_pressed(code); }, + "code_to_token", [](input_manager &input, sol::user code) { return input.code_to_token(code); }, + "code_name", [](input_manager &input, sol::user code) { return input.code_name(code); }, "seq_from_tokens", [](input_manager &input, const char *tokens) { input_seq seq; input.seq_from_tokens(seq, tokens); return sol::make_user(seq); }, - "seq_pressed", [](input_manager &input, sol::user seq) { return input.seq_pressed(seq); }); + "seq_pressed", [](input_manager &input, sol::user seq) { return input.seq_pressed(seq); }, + "seq_to_tokens", [](input_manager &input, sol::user seq) { return input.seq_to_tokens(seq); }, + "seq_name", [](input_manager &input, sol::user seq) { return input.seq_name(seq); }, + "seq_poll_start", [](input_manager &input, input_item_class cls, sol::object seq) { + input_seq *start = nullptr; + if(seq.is>()) + start = &seq.as>(); + input.seq_poll_start(cls, start); + }, + "seq_poll", &input_manager::seq_poll, + "seq_poll_final", [](input_manager &input) { return sol::make_user(input.seq_poll_final()); }); /* machine:uiinput() * uiinput:find_mouse() - returns x, y, button state, ui render target -- cgit v1.2.3