From c0d4e2c34792dca4033e6256619b6a19863ba522 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 26 Jan 2023 21:38:47 -0800 Subject: [PATCH] Scripting: Expose gamepad name to scripts --- include/mgba/script/input.h | 3 +++ res/scripts/gamepad-demo.lua | 1 + src/platform/qt/scripting/ScriptingController.cpp | 6 ++++++ src/script/input.c | 2 ++ 4 files changed, 12 insertions(+) diff --git a/include/mgba/script/input.h b/include/mgba/script/input.h index 53ef15c30..135605452 100644 --- a/include/mgba/script/input.h +++ b/include/mgba/script/input.h @@ -220,6 +220,9 @@ struct mScriptTriggerEvent { struct mScriptGamepad { unsigned pad; + char visibleName[128]; + char internalName[64]; + struct mScriptList axes; struct mScriptList buttons; struct mScriptList hats; diff --git a/res/scripts/gamepad-demo.lua b/res/scripts/gamepad-demo.lua index f211767ea..90327c11c 100644 --- a/res/scripts/gamepad-demo.lua +++ b/res/scripts/gamepad-demo.lua @@ -13,6 +13,7 @@ function readPad() local buttons = gamepad.buttons local hats = gamepad.hats + inputBuffer:print(gamepad.visibleName .. "\n") inputBuffer:print(string.format("%i buttons, %i axes, %i hats\n", #buttons, #axes, #hats)) local sbuttons = {} diff --git a/src/platform/qt/scripting/ScriptingController.cpp b/src/platform/qt/scripting/ScriptingController.cpp index 47cd08674..d8babf06d 100644 --- a/src/platform/qt/scripting/ScriptingController.cpp +++ b/src/platform/qt/scripting/ScriptingController.cpp @@ -21,6 +21,7 @@ #include #include +#include using namespace QGBA; @@ -237,6 +238,11 @@ void ScriptingController::updateGamepad() { detachGamepad(); return; } + + QString name = gamepad->name(); + strlcpy(m_gamepad.internalName, name.toUtf8().constData(), sizeof(m_gamepad.internalName)); + name = gamepad->visibleName(); + strlcpy(m_gamepad.visibleName, name.toUtf8().constData(), sizeof(m_gamepad.visibleName)); attachGamepad(); QList buttons = gamepad->currentButtons(); diff --git a/src/script/input.c b/src/script/input.c index 5f0901353..24818ebc5 100644 --- a/src/script/input.c +++ b/src/script/input.c @@ -95,6 +95,8 @@ mSCRIPT_DEFINE_STRUCT(mScriptGamepadHatEvent) mSCRIPT_DEFINE_END; mSCRIPT_DEFINE_STRUCT(mScriptGamepad) + mSCRIPT_DEFINE_STRUCT_MEMBER(mScriptGamepad, CHARP, visibleName) + mSCRIPT_DEFINE_STRUCT_MEMBER(mScriptGamepad, CHARP, internalName) mSCRIPT_DEFINE_STRUCT_MEMBER(mScriptGamepad, LIST, axes) mSCRIPT_DEFINE_STRUCT_MEMBER(mScriptGamepad, LIST, buttons) mSCRIPT_DEFINE_STRUCT_MEMBER(mScriptGamepad, LIST, hats)