Scripting: Expose rumble callback

This commit is contained in:
Vicki Pfau 2023-06-04 22:22:31 -07:00
parent 17a549baf2
commit 20ab4d27b1
3 changed files with 28 additions and 0 deletions

View File

@ -44,6 +44,10 @@ CXX_GUARD_START
#define restrict __restrict #define restrict __restrict
#endif #endif
#ifndef containerof
#define containerof(PTR, TYPE, MEMBER) ((TYPE*) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER)))
#endif
#ifdef _MSC_VER #ifdef _MSC_VER
#include <Windows.h> #include <Windows.h>
#include <sys/types.h> #include <sys/types.h>

View File

@ -7,6 +7,9 @@
#include <mgba/core/core.h> #include <mgba/core/core.h>
#include <mgba/core/serialize.h> #include <mgba/core/serialize.h>
#ifdef M_CORE_GBA
#include <mgba/gba/interface.h>
#endif
#include <mgba/script/base.h> #include <mgba/script/base.h>
#include <mgba/script/context.h> #include <mgba/script/context.h>
#include <mgba-util/table.h> #include <mgba-util/table.h>
@ -189,6 +192,8 @@ struct mScriptCoreAdapter {
#ifdef USE_DEBUGGERS #ifdef USE_DEBUGGERS
struct mScriptDebugger debugger; struct mScriptDebugger debugger;
#endif #endif
struct mRumble rumble;
struct mRumble* oldRumble;
}; };
struct mScriptConsole { struct mScriptConsole {
@ -1003,6 +1008,20 @@ mSCRIPT_DEFINE_STRUCT(mScriptCoreAdapter)
mSCRIPT_DEFINE_STRUCT_CAST_TO_MEMBER(mScriptCoreAdapter, CS(mCore), _core) mSCRIPT_DEFINE_STRUCT_CAST_TO_MEMBER(mScriptCoreAdapter, CS(mCore), _core)
mSCRIPT_DEFINE_END; mSCRIPT_DEFINE_END;
static void _setRumble(struct mRumble* rumble, int enable) {
struct mScriptCoreAdapter* adapter = containerof(rumble, struct mScriptCoreAdapter, rumble);
if (adapter->oldRumble) {
adapter->oldRumble->setRumble(adapter->oldRumble, enable);
}
struct mScriptList args;
mScriptListInit(&args, 1);
*mScriptListAppend(&args) = mSCRIPT_MAKE_BOOL(!!enable);
mScriptContextTriggerCallback(adapter->context, "rumble", &args);
mScriptListDeinit(&args);
}
void mScriptContextAttachCore(struct mScriptContext* context, struct mCore* core) { void mScriptContextAttachCore(struct mScriptContext* context, struct mCore* core) {
struct mScriptValue* coreValue = mScriptValueAlloc(mSCRIPT_TYPE_MS_S(mScriptCoreAdapter)); struct mScriptValue* coreValue = mScriptValueAlloc(mSCRIPT_TYPE_MS_S(mScriptCoreAdapter));
struct mScriptCoreAdapter* adapter = calloc(1, sizeof(*adapter)); struct mScriptCoreAdapter* adapter = calloc(1, sizeof(*adapter));
@ -1014,6 +1033,10 @@ void mScriptContextAttachCore(struct mScriptContext* context, struct mCore* core
adapter->memory.type = mSCRIPT_TYPE_MS_TABLE; adapter->memory.type = mSCRIPT_TYPE_MS_TABLE;
adapter->memory.type->alloc(&adapter->memory); adapter->memory.type->alloc(&adapter->memory);
adapter->rumble.setRumble = _setRumble;
adapter->oldRumble = core->getPeripheral(core, mPERIPH_RUMBLE);
core->setPeripheral(core, mPERIPH_RUMBLE, &adapter->rumble);
_rebuildMemoryMap(context, adapter); _rebuildMemoryMap(context, adapter);
coreValue->value.opaque = adapter; coreValue->value.opaque = adapter;

View File

@ -86,6 +86,7 @@ mSCRIPT_DEFINE_STRUCT(mScriptCallbackManager)
"- **frame**: The emulation finished a frame\n" "- **frame**: The emulation finished a frame\n"
"- **keysRead**: The emulation is about to read the key input\n" "- **keysRead**: The emulation is about to read the key input\n"
"- **reset**: The emulation has been reset\n" "- **reset**: The emulation has been reset\n"
"- **rumble**: The state of the rumble motor was changed. This callback is passed a single argument that specifies if it was turned on (true) or off (false)\n"
"- **savedataUpdated**: The emulation has just finished modifying save data\n" "- **savedataUpdated**: The emulation has just finished modifying save data\n"
"- **sleep**: The emulation has used the sleep feature to enter a low-power mode\n" "- **sleep**: The emulation has used the sleep feature to enter a low-power mode\n"
"- **shutdown**: The emulation has been powered off\n" "- **shutdown**: The emulation has been powered off\n"