From b4c996ff62b35e549e83d1f896b4a097c849e9cd Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Wed, 25 Nov 2026 09:24:26 -0700 Subject: [PATCH] Jedi Knight: gamepad bumpers switch weapons, the d-pad picks and uses Force powers A USB gamepad had no way to change weapons or use a Force power in Jedi Knight. Now, as on desktop OpenJKDF2: R1 / L1 = next / previous weapon, the d-pad's left / right = previous / next Force power and up = use it (held). Fire stays on R2 / L2, and the left stick moves. - Tab5 HAL: GamepadDetail (Tab5Emu::gamepad_detail()) keeps apart what GamepadState folds together: L1 / R1 alone, whether the pad has shown it has triggers (L2 / R2 pressed since it connected), and its d-pad alone. usb_hid.cpp reads the d-pad from the pad's own fields, a hat switch or the d-pad usages (the Backbone Pro has four of those and no hat); the decoder folds it with the left stick. - jk_esp_read_input: with the d-pad known, the left stick moves (W/A/S/D) and the d-pad goes to the engine as the joystick hat (jk_esp_input_t.dpad). Once the pad has shown triggers, they fire and L1 / R1 go to the joystick binds (bits 10 / 11 of joy_buttons). A pad without triggers keeps firing with L / R, and one whose d-pad can't be told apart keeps moving with it. - OpenJKDF2 esp32/gamepad-weapons-force: the ESP32 joystick binds and the input read that feeds them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/jk/OpenJKDF2 | 2 +- components/jk/include/jk_esp.h | 3 + components/jk/src/jk.cpp | 54 +++++++++++----- components/tab5-emu/include/gamepad_state.hpp | 18 ++++++ components/tab5-emu/include/tab5-emu.hpp | 6 +- components/tab5-emu/src/tab5-emu.cpp | 9 ++- components/tab5-emu/src/usb_hid.cpp | 64 ++++++++++++++++++- 7 files changed, 137 insertions(+), 19 deletions(-) diff --git a/components/jk/OpenJKDF2 b/components/jk/OpenJKDF2 index fc4edad..0f72de9 160000 --- a/components/jk/OpenJKDF2 +++ b/components/jk/OpenJKDF2 @@ -1 +1 @@ -Subproject commit fc4edad3915758736766782d55da6b7345c4b555 +Subproject commit 0f72de907ff6c724284f594ee40c13c66e9fc7a6 diff --git a/components/jk/include/jk_esp.h b/components/jk/include/jk_esp.h index 4a1cd0f..68c4d9a 100644 --- a/components/jk/include/jk_esp.h +++ b/components/jk/include/jk_esp.h @@ -15,6 +15,9 @@ typedef struct { // the same for the engine's joystick binds in play: without the USB gamepad's // a/b/x/y, which arrive as keys (see jk_esp_read_input) uint16_t joy_buttons; + // the USB gamepad's d-pad alone, when the pad has a hat switch (bit 0 up, + // 1 down, 2 left, 3 right): the engine's joystick hat in play (Force powers) + uint8_t dpad; int16_t touch_x; // logical (landscape) touch position, -1 if none int16_t touch_y; uint8_t touch_down; diff --git a/components/jk/src/jk.cpp b/components/jk/src/jk.cpp index 54ea7b0..e736007 100644 --- a/components/jk/src/jk.cpp +++ b/components/jk/src/jk.cpp @@ -320,14 +320,25 @@ void jk_esp_read_input(jk_esp_input_t *out) { // (W = R, S = Y ...) would reach the engine's joystick binds on top of the // keys themselves const uint16_t touch_buttons = emu.touch_gamepad_state().buttons; - const uint16_t usb_buttons = emu.usb_gamepad_state().buttons; - out->buttons = static_cast(touch_buttons | usb_buttons); + const GamepadState usb = emu.usb_gamepad_state(); + const GamepadDetail pad = emu.gamepad_detail(); + out->buttons = static_cast(touch_buttons | usb.buttons); // In play the USB gamepad's A/B/X/Y arrive as keys (below), so the engine's // joystick binds get them from the touch pad only. Both at once doubled each // action: B jumped and (B2 = duck) crouched, so its jump was lower; A also // fired, X also activated, Y also jumped. JK's menus still read all of them // (B = back), see stdControl_ReadControls. - out->joy_buttons = static_cast(touch_buttons | (usb_buttons & ~0x000Fu)); + // Its shoulders go to the joystick binds on their own, as bits 10 / 11 (L1 / + // R1: previous / next weapon), once the pad has shown it has triggers to fire + // with; until then they fire, as on a pad without triggers. + const bool cycle = pad.has_triggers; + out->joy_buttons = static_cast(touch_buttons | (usb.buttons & ~0x3C0Fu) | + (cycle && pad.l1 ? 1u << 10 : 0u) | (cycle && pad.r1 ? 1u << 11 : 0u)); + // its d-pad alone (the pad has a hat switch) is the joystick hat: Force powers + if (pad.dpad_known) { + out->dpad = static_cast((pad.dpad_up ? 1 : 0) | (pad.dpad_down ? 2 : 0) | (pad.dpad_left ? 4 : 0) | + (pad.dpad_right ? 8 : 0)); + } auto touch = emu.touchpad_data(); out->touch_down = touch.num_touch_points > 0; out->touch_x = out->touch_down ? touch.x : -1; @@ -340,19 +351,32 @@ void jk_esp_read_input(jk_esp_input_t *out) { const auto kb = emu.keyboard_state(); static_assert(sizeof(kb.keys) == sizeof(out->keys)); memcpy(out->keys, kb.keys, sizeof(out->keys)); - // gamepad as keyboard (the engine's default binds): left stick / d-pad = - // W A S D (forward, strafe, back), R = fire (ctrl), L = secondary fire (z), - // A = activate (space), B = jump (x), X = crouch (c), Y = use item (enter) - // from the USB gamepad only: the merged state also carries the keyboard's - // own keys (W = R shoulder, A = X ...) which would come back as extra keys - const GamepadState gp = emu.usb_gamepad_state(); + // gamepad as keyboard (the engine's default binds): left stick = W A S D + // (forward, strafe, back; the d-pad too on a pad without a hat switch), + // R2 = fire (ctrl) and L2 = secondary fire (z) -- R / L on a pad without + // triggers --, A = activate (space), B = jump (x), X = crouch (c), Y = use + // item (enter). From the USB gamepad only: the merged state also carries the + // keyboard's own keys (W = R shoulder, A = X ...) which would come back as + // extra keys + const GamepadState &gp = usb; auto press = [&](int usage) { out->keys[usage >> 3] |= 1 << (usage & 7); }; - if (gp.up) press(26); // w - if (gp.down) press(22); // s - if (gp.left) press(4); // a - if (gp.right) press(7); // d - if (gp.r) press(0xE0); // left ctrl: fire - if (gp.l) press(29); // z: secondary fire + if (pad.dpad_known) { + // the d-pad is the hat above; move with the left stick alone (with the + // decoder's 40% dead zone) + constexpr int DEAD = 32767 * 2 / 5; + const auto ax = emu.gamepad_axes(); + if (ax.ly < -DEAD) press(26); // w + if (ax.ly > DEAD) press(22); // s + if (ax.lx < -DEAD) press(4); // a + if (ax.lx > DEAD) press(7); // d + } else { + if (gp.up) press(26); // w + if (gp.down) press(22); // s + if (gp.left) press(4); // a + if (gp.right) press(7); // d + } + if (cycle ? gp.r2 : gp.r) press(0xE0); // left ctrl: fire + if (cycle ? gp.l2 : gp.l) press(29); // z: secondary fire if (gp.a) press(44); // space: activate if (gp.b) press(27); // x: jump if (gp.x) press(6); // c: crouch diff --git a/components/tab5-emu/include/gamepad_state.hpp b/components/tab5-emu/include/gamepad_state.hpp index e778dda..482fc33 100644 --- a/components/tab5-emu/include/gamepad_state.hpp +++ b/components/tab5-emu/include/gamepad_state.hpp @@ -78,3 +78,21 @@ struct GamepadAxes { int16_t rx{0}; int16_t ry{0}; }; + +/// What GamepadState folds together, kept apart for carts that tell them +/// apart (USB gamepads only). +struct GamepadDetail { + /// The shoulder buttons alone (GamepadState::l / r are shoulder or trigger). + bool l1{false}; + bool r1{false}; + /// L2 / R2 were pressed since the pad was connected, so it has triggers + /// (some pads without them still declare the buttons). + bool has_triggers{false}; + /// The pad has a hat switch, so dpad_* below are the d-pad alone + /// (GamepadState::up / down / left / right also carry the left stick). + bool dpad_known{false}; + bool dpad_up{false}; + bool dpad_down{false}; + bool dpad_left{false}; + bool dpad_right{false}; +}; diff --git a/components/tab5-emu/include/tab5-emu.hpp b/components/tab5-emu/include/tab5-emu.hpp index f614718..4dddc75 100644 --- a/components/tab5-emu/include/tab5-emu.hpp +++ b/components/tab5-emu/include/tab5-emu.hpp @@ -223,6 +223,8 @@ class Tab5Emu : public espp::BaseComponent { bool usb_gamepad_present() const { return usb_gamepads_ > 0; } /// The gamepad's sticks (zeros without a gamepad) GamepadAxes gamepad_axes() const; + /// The USB gamepad's shoulder buttons and d-pad on their own (see GamepadDetail) + GamepadDetail gamepad_detail() const; /// The USB gamepad's buttons alone (gamepad_state() also merges the touch /// pad and the keyboard-as-gamepad keys) GamepadState usb_gamepad_state() const; @@ -461,12 +463,14 @@ class Tab5Emu : public espp::BaseComponent { std::atomic press_latch_{0}; std::atomic remote_buttons_{0}; // buttons pressed since gamepad_state_latched() GamepadAxes usb_gamepad_axes_{}; + GamepadDetail usb_gamepad_detail_{}; lv_indev_t *keypad_indev_{nullptr}; uint32_t keypad_last_key_{0}; static void keypad_read(lv_indev_t *indev, lv_indev_data_t *data); void on_hid_keyboard(const espp::hid_rp::KeyboardReport &report); void on_hid_mouse(const espp::hid_rp::MouseReport &report); - void on_hid_gamepad_state(const GamepadState &state, bool menu, const GamepadAxes &axes = {}); + void on_hid_gamepad_state(const GamepadState &state, bool menu, const GamepadAxes &axes = {}, + const GamepadDetail &detail = {}); // video std::atomic video_setting_{VideoSetting::FIT}; diff --git a/components/tab5-emu/src/tab5-emu.cpp b/components/tab5-emu/src/tab5-emu.cpp index 93104f8..ad5c57e 100644 --- a/components/tab5-emu/src/tab5-emu.cpp +++ b/components/tab5-emu/src/tab5-emu.cpp @@ -640,13 +640,20 @@ GamepadAxes Tab5Emu::gamepad_axes() const { return usb_gamepad_axes_; } -void Tab5Emu::on_hid_gamepad_state(const GamepadState &state, bool menu, const GamepadAxes &axes) { +GamepadDetail Tab5Emu::gamepad_detail() const { + std::lock_guard lk(hid_mutex_); + return usb_gamepad_detail_; +} + +void Tab5Emu::on_hid_gamepad_state(const GamepadState &state, bool menu, const GamepadAxes &axes, + const GamepadDetail &detail) { { std::lock_guard lk(hid_mutex_); press_latch_.fetch_or(static_cast(state.buttons & ~usb_gamepad_.buttons), std::memory_order_relaxed); usb_gamepad_ = state; usb_gamepad_axes_ = axes; + usb_gamepad_detail_ = detail; } if (menu) { menu_requested_ = true; diff --git a/components/tab5-emu/src/usb_hid.cpp b/components/tab5-emu/src/usb_hid.cpp index c8f8a94..da6c02b 100644 --- a/components/tab5-emu/src/usb_hid.cpp +++ b/components/tab5-emu/src/usb_hid.cpp @@ -28,6 +28,16 @@ struct Tab5Emu::UsbHid { // its own last report and the HAL sees the union std::map keyboard_states; espp::hid_rp::GamepadReport last_gamepad{}; // for the mapping log + // per gamepad: the fields of its d-pad (a hat switch and / or the d-pad + // usages; the decoder folds them and the left stick into one d-pad, and + // GamepadDetail keeps the d-pad apart) and whether its triggers have been + // pressed + struct PadInfo { + std::vector dpad_fields; + bool dpad_updown_swapped{false}; + bool triggers_seen{false}; + }; + std::map pad_infos; espp::hid_rp::KeyboardReport last_keyboard{}; void publish_keyboard() { @@ -75,6 +85,25 @@ struct Tab5Emu::UsbHid { } else if (espp::hid_rp::GamepadDecoder::looks_like_gamepad(*map)) { espp::hid_rp::GamepadDecoder pad(std::move(*map)); pad.apply_quirks(info.vid, info.pid); + PadInfo pad_info; + pad_info.dpad_updown_swapped = pad.quirks().dpad_updown_swapped; + size_t hats = 0; + for (const auto &f : pad.map().fields()) { + if (f.usage_page != espp::hid_rp::usage::PAGE_GENERIC_DESKTOP || f.array) continue; + if (f.usage == espp::hid_rp::usage::GD_HAT) { + pad_info.dpad_fields.push_back(f); + hats++; + } else if (f.usage >= espp::hid_rp::usage::GD_DPAD_UP && f.usage <= espp::hid_rp::usage::GD_DPAD_LEFT) { + pad_info.dpad_fields.push_back(f); + } + } + logger.info("HID gamepad d-pad: {} hat switch(es), {} d-pad usage field(s){}", hats, + pad_info.dpad_fields.size() - hats, + pad_info.dpad_fields.empty() ? " (only as the merged direction with the left stick)" : ""); + { + std::lock_guard lk(mutex); + pad_infos[device.get()] = pad_info; + } logger.info("HID gamepad {:04x}:{:04x} '{}': {} fields, report ids {}, {} layout", info.vid, info.pid, info.product, pad.map().fields().size(), pad.map().uses_report_ids() ? "yes" : "no", pad.quirks().layout == espp::hid_rp::GamepadDecoder::Layout::Xbox ? "xbox-style" : "directinput"); @@ -108,6 +137,7 @@ struct Tab5Emu::UsbHid { } decoder = std::move(it->second); decoders.erase(it); + pad_infos.erase(device.get()); } const char *kind = "device"; if (std::holds_alternative(decoder)) { @@ -198,7 +228,38 @@ struct Tab5Emu::UsbHid { state.left = report.left; state.right = report.right; const bool menu = report.home || (report.select && report.start); - emu->on_hid_gamepad_state(state, menu, GamepadAxes{report.lx, report.ly, report.rx, report.ry}); + GamepadDetail detail{}; + detail.l1 = report.l1; + detail.r1 = report.r1; + auto &pad_info = pad_infos[dev]; + pad_info.triggers_seen |= report.l2 || report.r2; + detail.has_triggers = pad_info.triggers_seen; + if (!pad_info.dpad_fields.empty()) { + // the d-pad alone, read from its own fields as the decoder reads them + detail.dpad_known = true; + const auto split = pad->map().split(data); + for (const auto &f : pad_info.dpad_fields) { + const auto v = espp::hid_rp::ReportMap::extract(f, split.report_id, split.payload); + if (!v) continue; + if (f.usage == espp::hid_rp::usage::GD_HAT) { + const int32_t dir = *v - f.logical_min; // 0 = up, clockwise; out of range = centered + if (*v >= f.logical_min && *v <= f.logical_max && dir >= 0 && dir < 8) { + detail.dpad_up |= dir == 7 || dir == 0 || dir == 1; + detail.dpad_right |= dir >= 1 && dir <= 3; + detail.dpad_down |= dir >= 3 && dir <= 5; + detail.dpad_left |= dir >= 5; + } + } else { + const bool pressed = *v != 0; + const bool swapped = pad_info.dpad_updown_swapped; + if (f.usage == espp::hid_rp::usage::GD_DPAD_UP) (swapped ? detail.dpad_down : detail.dpad_up) |= pressed; + if (f.usage == espp::hid_rp::usage::GD_DPAD_DOWN) (swapped ? detail.dpad_up : detail.dpad_down) |= pressed; + if (f.usage == espp::hid_rp::usage::GD_DPAD_RIGHT) detail.dpad_right |= pressed; + if (f.usage == espp::hid_rp::usage::GD_DPAD_LEFT) detail.dpad_left |= pressed; + } + } + } + emu->on_hid_gamepad_state(state, menu, GamepadAxes{report.lx, report.ly, report.rx, report.ry}, detail); } } } @@ -256,6 +317,7 @@ void Tab5Emu::deinitialize_usb_host() { mouse_.buttons = 0; usb_gamepad_ = GamepadState{}; usb_gamepad_axes_ = GamepadAxes{}; + usb_gamepad_detail_ = GamepadDetail{}; } logger_.info("USB host stopped"); }