foxygit / fake-08-xbox360 Log in
commit 0143a289bf4fe73268314011b1d0170147f8d87e
Author:     mrfox <jens.se@icloud.com>
AuthorDate: Tue Nov 24 21:26:02 2026 +0100
Commit:     mrfox <jens.se@icloud.com>
CommitDate: Tue Nov 24 21:26:02 2026 +0100

    Multiplayer input (players 1-4), Xbox 360 reads 4 controllers

    Core:
    - Input keeps held/pressed state and btnp repeat counters per player
      (4 players). btn(i, p) / btnp(i, p) work for p = 0-3 instead of always
      returning false for p > 0. btn()/btnp() without arguments return player 2
      in bits 8-15, like PICO-8. Button state is written to pico RAM per player.
    - InputState_t gets extraKDown/extraKHeld for players 2-4, defaulting to 0,
      so hosts that only fill in player 1 are unchanged.
    - Test case for multiplayer input.

    Xbox 360: controllers on ports 1-3 are players 2-4 with the same layout,
    deadzone and debounce settings. Only player 1 drives pause/back/quit.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xenon/source/XenonHost.cpp | 86 ++++++++++++++++++++++++-------------
 source/Input.cpp                    | 60 ++++++++++++++++----------
 source/Input.h                      | 14 +++---
 source/hostVmShared.h               |  5 +++
 source/picoluaapi.cpp               |  4 +-
 source/vm.cpp                       |  3 ++
 test/picoluaapitests.cpp            | 29 +++++++++++++
 7 files changed, 142 insertions(+), 59 deletions(-)

diff --git a/platform/xenon/source/XenonHost.cpp b/platform/xenon/source/XenonHost.cpp
index 1626ecd..0571de8 100644
--- a/platform/xenon/source/XenonHost.cpp
+++ b/platform/xenon/source/XenonHost.cpp
@@ -464,26 +464,55 @@ void Host::forceStretch(StretchOption newStretch) {
     mouseOffsetY = destY;
 }

+// Maps one controller to PICO-8 button bits (without the pause button).
+static uint8_t padToButtons(const controller_data_s &c, int layout, int stickThreshold) {
+    uint8_t held = 0;
+    if (c.left  || c.s1_x < -stickThreshold) held |= P8_KEY_LEFT;
+    if (c.right || c.s1_x >  stickThreshold) held |= P8_KEY_RIGHT;
+    if (c.up    || c.s1_y >  stickThreshold) held |= P8_KEY_UP;
+    if (c.down  || c.s1_y < -stickThreshold) held |= P8_KEY_DOWN;
+    // Layout 0 (PICO-8 default): A/Y = O, B/X = X. Layout 1 swaps them.
+    uint8_t bottomKey = layout == 1 ? P8_KEY_X : P8_KEY_O;
+    uint8_t rightKey  = layout == 1 ? P8_KEY_O : P8_KEY_X;
+    if (c.a || c.y) held |= bottomKey;
+    if (c.b || c.x) held |= rightKey;
+    return held;
+}
+
+// Debounce releases (setting "button debounce"): a button only counts as
+// released after it has read "up" for releaseFrames consecutive polls.
+// Presses register immediately.
+static uint8_t debounceButtons(uint8_t held, uint8_t prevHeld, uint8_t upCount[8], int releaseFrames) {
+    uint8_t debounced = 0;
+    for (int i = 0; i < 8; i++) {
+        uint8_t bit = (uint8_t)(1 << i);
+        if (held & bit) {
+            upCount[i] = 0;
+            debounced |= bit;
+        } else if ((prevHeld & bit) && ++upCount[i] < releaseFrames) {
+            debounced |= bit;   // still treat as held until release is stable
+        }
+    }
+    return debounced;
+}
+
 InputState_t Host::scanInput() {
     static const int DeadzoneThresholds[3] = {8000, 16000, 24000};
-    const int StickThreshold = DeadzoneThresholds[(deadzone >= 0 && deadzone <= 2) ? deadzone : 1];
+    const int stickThreshold = DeadzoneThresholds[(deadzone >= 0 && deadzone <= 2) ? deadzone : 1];
+    const int releaseFrames = ((debounce >= 0 && debounce <= 3) ? debounce : 1) + 1;
+
+    static uint8_t upCount[4][8] = {};
+    static uint8_t extraHeld[3] = {0, 0, 0};

     usb_do_poll();
+
+    // Player 1 (port 0) also drives the system buttons.
     controller_data_s c;
     memset(&c, 0, sizeof(c));
     get_controller_data(&c, 0);

-    uint8_t held = 0;
-    if (c.left  || c.s1_x < -StickThreshold) held |= P8_KEY_LEFT;
-    if (c.right || c.s1_x >  StickThreshold) held |= P8_KEY_RIGHT;
-    if (c.up    || c.s1_y >  StickThreshold) held |= P8_KEY_UP;
-    if (c.down  || c.s1_y < -StickThreshold) held |= P8_KEY_DOWN;
-    // Layout 0 (PICO-8 default): A/Y = O, B/X = X. Layout 1 swaps them.
-    uint8_t bottomKey = btnlayout == 1 ? P8_KEY_X : P8_KEY_O;
-    uint8_t rightKey  = btnlayout == 1 ? P8_KEY_O : P8_KEY_X;
-    if (c.a || c.y) held |= bottomKey;
-    if (c.b || c.x) held |= rightKey;
-    if (c.start)    held |= P8_KEY_PAUSE;
+    uint8_t held = padToButtons(c, btnlayout, stickThreshold);
+    if (c.start) held |= P8_KEY_PAUSE;

     static bool backWasDown = false;
     stretchKeyPressed = c.back && !backWasDown && !c.start;
@@ -492,30 +521,29 @@ InputState_t Host::scanInput() {
     // BACK + START quits back to XeLL
     if (c.back && c.start) quit = 1;

-    // Debounce releases (setting "button debounce"): a button only counts as
-    // released after it has read "up" for debounce+1 consecutive polls.
-    // Presses register immediately. 0 = off.
-    const int ReleaseFrames = ((debounce >= 0 && debounce <= 3) ? debounce : 1) + 1;
-    static uint8_t upCount[8] = {0};
-    uint8_t debounced = 0;
-    for (int i = 0; i < 8; i++) {
-        uint8_t bit = (uint8_t)(1 << i);
-        if (held & bit) {
-            upCount[i] = 0;
-            debounced |= bit;
-        } else if ((currKHeld & bit) && ++upCount[i] < ReleaseFrames) {
-            debounced |= bit;   // still treat as held until release is stable
-        }
-    }
-
+    uint8_t debounced = debounceButtons(held, currKHeld, upCount[0], releaseFrames);
     currKDown = debounced & ~currKHeld;
     currKHeld = debounced;

-    return InputState_t{
+    InputState_t state{
         currKDown,
         currKHeld,
         0, 0, 0
     };
+
+    // Players 2-4 (ports 1-3). Disconnected controllers read as all zero.
+    for (int p = 0; p < 3; p++) {
+        controller_data_s pc;
+        memset(&pc, 0, sizeof(pc));
+        get_controller_data(&pc, p + 1);
+        uint8_t ph = debounceButtons(padToButtons(pc, btnlayout, stickThreshold),
+                                     extraHeld[p], upCount[p + 1], releaseFrames);
+        state.extraKDown[p] = ph & ~extraHeld[p];
+        state.extraKHeld[p] = ph;
+        extraHeld[p] = ph;
+    }
+
+    return state;
 }

 bool Host::shouldQuit() {
diff --git a/source/Input.cpp b/source/Input.cpp
index e430d45..f24ff1f 100644
--- a/source/Input.cpp
+++ b/source/Input.cpp
@@ -4,24 +4,34 @@
 #include "hostVmShared.h"
 #include "PicoRam.h"

-Input::Input(PicoRam* memory):
-    _currentKDown(0),
-    _currentKHeld(0)
+Input::Input(PicoRam* memory)
 {
     _memory = memory;

-    std::fill(_framesHeld, _framesHeld + 8, 0);
+    std::fill(_currentKDown, _currentKDown + MaxPlayers, 0);
+    std::fill(_currentKHeld, _currentKHeld + MaxPlayers, 0);
+    for (int p = 0; p < MaxPlayers; p++) {
+        std::fill(_framesHeld[p], _framesHeld[p] + 8, 0);
+    }
 }

 void Input::SetState(uint8_t kdown, uint8_t kheld){
-    _currentKDown = kdown;
-    _currentKHeld = kheld;
+    SetPlayerState(0, kdown, kheld);
+}
+
+void Input::SetPlayerState(uint8_t p, uint8_t kdown, uint8_t kheld){
+    if (p >= MaxPlayers) {
+        return;
+    }
+
+    _currentKDown[p] = kdown;
+    _currentKHeld[p] = kheld;
     //key 6 (PAUSE MENU) only fires for one frame, even if held
-    if ((_currentKHeld & BITMASK(6)) && !(_currentKDown & BITMASK(6))) {
-        _currentKHeld = _currentKHeld & ~(BITMASK(6));
+    if ((_currentKHeld[p] & BITMASK(6)) && !(_currentKDown[p] & BITMASK(6))) {
+        _currentKHeld[p] = _currentKHeld[p] & ~(BITMASK(6));
     }
     //memory only stores buttons 0-5, not 6 or 7
-    _memory->hwState.buttonStates[0] = kheld & 0x3F;
+    _memory->hwState.buttonStates[p] = kheld & 0x3F;

     uint8_t repeatDelay = _memory->hwState.btnpRepeatDelay == 0
         ? 15
@@ -38,7 +48,7 @@ void Input::SetState(uint8_t kdown, uint8_t kheld){
     for (int i = 0; i < 7; i ++) {
         bool down = BITMASK(i) & kheld;

-        _framesHeld[i] = down ? _framesHeld[i] + 1 : 0;
+        _framesHeld[p][i] = down ? _framesHeld[p][i] + 1 : 0;

         //update kdown to be true if held for 15 frames, then every 4th after that.
         //from wiki:
@@ -48,11 +58,11 @@ void Input::SetState(uint8_t kdown, uint8_t kheld){
         //player releases the button.

         bool repeatPressed =
-            (_framesHeld[i] == repeatDelay) ||
-            (_framesHeld[i] / repeatDelay >= 1 && _framesHeld[i] % repeatInterval == 0);
+            (_framesHeld[p][i] == repeatDelay) ||
+            (_framesHeld[p][i] / repeatDelay >= 1 && _framesHeld[p][i] % repeatInterval == 0);

         if (repeatPressed) {
-            _currentKDown = _currentKDown | BITMASK(i);
+            _currentKDown[p] = _currentKDown[p] | BITMASK(i);
         }
     }
 }
@@ -68,33 +78,37 @@ void Input::SetKeyboard(bool kbDown, std::string kbKey){
 	_kbKey = kbKey;
 }

-uint8_t Input::btn(){
-    return _currentKHeld;
+uint16_t Input::btn(){
+    return _currentKHeld[0] | ((uint16_t)_currentKHeld[1] << 8);
 }

 //todo: repetition behavior to match pico 8
-uint8_t Input::btnp(){
-    return _currentKDown;
+uint16_t Input::btnp(){
+    return _currentKDown[0] | ((uint16_t)_currentKDown[1] << 8);
 }

 bool Input::btn(uint8_t i){
-    return BITMASK(i) & btn();
+    return btn(i, 0);
 }

 //todo: repetition behavior to match pico 8
 bool Input::btnp(uint8_t i){
-    return BITMASK(i) & btnp();
+    return btnp(i, 0);
 }

 bool Input::btn(uint8_t i, uint8_t p){
-    //no multiplayer support for now
-    return p == 0 ? btn(i) : 0;
+    if (p >= MaxPlayers || i >= 8) {
+        return false;
+    }
+    return BITMASK(i) & _currentKHeld[p];
 }

 //todo: repetition behavior to match pico 8
 bool Input::btnp(uint8_t i, uint8_t p){
-    //no multiplayer support for now
-    return p == 0 ? btnp(i) : 0;
+    if (p >= MaxPlayers || i >= 8) {
+        return false;
+    }
+    return BITMASK(i) & _currentKDown[p];
 }

 int16_t Input::getMouseX() {
diff --git a/source/Input.h b/source/Input.h
index 2fb0d26..cfe8cbc 100644
--- a/source/Input.h
+++ b/source/Input.h
@@ -4,11 +4,13 @@
 #include "PicoRam.h"

 class Input{
+    static const int MaxPlayers = 4;
+
     PicoRam* _memory;
-	uint8_t _currentKDown;
-    uint8_t _currentKHeld;
+	uint8_t _currentKDown[MaxPlayers];
+    uint8_t _currentKHeld[MaxPlayers];

-	uint16_t _framesHeld[8];
+	uint16_t _framesHeld[MaxPlayers][8];

     int16_t _mouseX;
     int16_t _mouseY;
@@ -19,12 +21,14 @@ class Input{
     public:
     Input(PicoRam* memory);
     void SetState(uint8_t kdown, uint8_t kheld);
+    void SetPlayerState(uint8_t p, uint8_t kdown, uint8_t kheld);
     void SetMouse(int16_t mouseX, int16_t mouseY, uint8_t mouseBtnState);

     void SetKeyboard(bool kbdown, std::string kbkey);

-	uint8_t btn();
-    uint8_t btnp();
+	// no-arg versions: player 1 in bits 0-7, player 2 in bits 8-15 (like PICO-8)
+	uint16_t btn();
+    uint16_t btnp();

     bool btn(uint8_t i);
     bool btnp(uint8_t i);
diff --git a/source/hostVmShared.h b/source/hostVmShared.h
index b0e1dc7..e250713 100644
--- a/source/hostVmShared.h
+++ b/source/hostVmShared.h
@@ -77,6 +77,11 @@ struct InputState_t {
 	uint8_t mouseBtnState;
 	bool KBdown;
 	std::string KBkey;
+
+	// Players 2-4 (index 0 = player 2). Hosts that only support one
+	// controller can leave these at 0.
+	uint8_t extraKDown[3] = {0, 0, 0};
+	uint8_t extraKHeld[3] = {0, 0, 0};
 };

 enum PrintMode_t
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 721abb7..1b73d0e 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -765,7 +765,7 @@ int fillp(lua_State *L) {
 int btn(lua_State *L){
     int numArgs = lua_gettop(L);
     if (numArgs == 0) {
-        uint8_t btnstate = _inputForLuaApi->btn();
+        uint16_t btnstate = _inputForLuaApi->btn();

         lua_pushnumber(L, btnstate);
     }
@@ -786,7 +786,7 @@ int btn(lua_State *L){
 int btnp(lua_State *L){
     int numArgs = lua_gettop(L);
     if (numArgs == 0) {
-        uint8_t btnpstate = _inputForLuaApi->btnp();
+        uint16_t btnpstate = _inputForLuaApi->btnp();

         lua_pushnumber(L, btnpstate);
     }
diff --git a/source/vm.cpp b/source/vm.cpp
index e98aec6..5efac39 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -1217,6 +1217,9 @@ void Vm::update_buttons() {
     }

     _input->SetState(inputState.KDown, inputState.KHeld);
+    for (int p = 0; p < 3; p++) {
+        _input->SetPlayerState(p + 1, inputState.extraKDown[p], inputState.extraKHeld[p]);
+    }
     if (_memory->drawState.devkitMode) {
         _input->SetMouse(inputState.mouseX, inputState.mouseY, inputState.mouseBtnState);
         _input->SetKeyboard(inputState.KBdown,inputState.KBkey);
diff --git a/test/picoluaapitests.cpp b/test/picoluaapitests.cpp
index ee731a4..2e98076 100644
--- a/test/picoluaapitests.cpp
+++ b/test/picoluaapitests.cpp
@@ -244,3 +244,32 @@ TEST_CASE("peek with large count") {
   delete input;
   delete stubHost;
 }
+
+TEST_CASE("multiplayer input") {
+  PicoRam picoRam;
+  picoRam.Reset();
+  Input input(&picoRam);
+
+  input.SetState(BITMASK(0), BITMASK(0));             // player 1: left
+  input.SetPlayerState(1, BITMASK(4), BITMASK(4));    // player 2: O
+
+  SUBCASE("btn(i, p) reads the right player") {
+    CHECK(input.btn(0, 0));
+    CHECK_FALSE(input.btn(4, 0));
+    CHECK(input.btn(4, 1));
+    CHECK_FALSE(input.btn(0, 1));
+    CHECK(input.btnp(4, 1));
+  }
+  SUBCASE("btn() packs player 2 into the high byte") {
+    CHECK(input.btn() == (BITMASK(0) | (BITMASK(4) << 8)));
+  }
+  SUBCASE("button state is written to pico ram per player") {
+    CHECK(picoRam.hwState.buttonStates[0] == BITMASK(0));
+    CHECK(picoRam.hwState.buttonStates[1] == BITMASK(4));
+  }
+  SUBCASE("out of range players and buttons are false") {
+    CHECK_FALSE(input.btn(0, 7));
+    CHECK_FALSE(input.btnp(4, 200));
+    CHECK_FALSE(input.btn(9, 0));
+  }
+}