Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/jk/OpenJKDF2
3 changes: 3 additions & 0 deletions components/jk/include/jk_esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
54 changes: 39 additions & 15 deletions components/jk/src/jk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(touch_buttons | usb_buttons);
const GamepadState usb = emu.usb_gamepad_state();
const GamepadDetail pad = emu.gamepad_detail();
out->buttons = static_cast<uint16_t>(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<uint16_t>(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<uint16_t>(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<uint8_t>((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;
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions components/tab5-emu/include/gamepad_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
};
6 changes: 5 additions & 1 deletion components/tab5-emu/include/tab5-emu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -461,12 +463,14 @@ class Tab5Emu : public espp::BaseComponent {
std::atomic<uint16_t> press_latch_{0};
std::atomic<uint16_t> 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<VideoSetting> video_setting_{VideoSetting::FIT};
Expand Down
9 changes: 8 additions & 1 deletion components/tab5-emu/src/tab5-emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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<std::mutex> lk(hid_mutex_);
press_latch_.fetch_or(static_cast<uint16_t>(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;
Expand Down
64 changes: 63 additions & 1 deletion components/tab5-emu/src/usb_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ struct Tab5Emu::UsbHid {
// its own last report and the HAL sees the union
std::map<const espp::UsbHost::HidDevice *, espp::hid_rp::KeyboardReport> 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<espp::hid_rp::ReportField> dpad_fields;
bool dpad_updown_swapped{false};
bool triggers_seen{false};
};
std::map<const espp::UsbHost::HidDevice *, PadInfo> pad_infos;
espp::hid_rp::KeyboardReport last_keyboard{};

void publish_keyboard() {
Expand Down Expand Up @@ -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<std::mutex> 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");
Expand Down Expand Up @@ -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<espp::hid_rp::KeyboardDecoder>(decoder)) {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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");
}
Expand Down
Loading