diff --git a/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp b/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp index 000883e..528577e 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp @@ -26,6 +26,9 @@ #include "Core/SysConf.h" #include "Core/System.h" #include "InputCommon/ControllerInterface/ControllerInterface.h" +#ifdef __EMSCRIPTEN__ +#include "InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.h" +#endif namespace IOS::HLE { @@ -342,6 +345,9 @@ void BluetoothEmuDevice::Update() { // Throttle before Wii Remote update so input is taken just before needed. (lower input latency) core_timing.Throttle(now); +#ifdef __EMSCRIPTEN__ + ciface::PlayAgain::OnBluetoothUpdate(); +#endif g_controller_interface.SetCurrentInputChannel(ciface::InputChannel::Bluetooth); g_controller_interface.UpdateInput(); diff --git a/Source/Core/InputCommon/CMakeLists.txt b/Source/Core/InputCommon/CMakeLists.txt index 64febf2..ed18dc9 100644 --- a/Source/Core/InputCommon/CMakeLists.txt +++ b/Source/Core/InputCommon/CMakeLists.txt @@ -166,6 +166,14 @@ if(LIBEVDEV_FOUND AND LIBUDEV_FOUND) ) endif() +if(EMSCRIPTEN) + # PlayAgain: Wii Remotes fed from the page. + target_sources(inputcommon PRIVATE + ControllerInterface/PlayAgain/PlayAgainWiimote.cpp + ControllerInterface/PlayAgain/PlayAgainWiimote.h + ) +endif() + if(UNIX) target_sources(inputcommon PRIVATE ControllerInterface/Pipes/Pipes.cpp diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 9dd8cdb..aae0a10 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -36,6 +36,9 @@ #ifdef CIFACE_USE_STEAMDECK #include "InputCommon/ControllerInterface/SteamDeck/SteamDeck.h" #endif +#ifdef __EMSCRIPTEN__ +#include "InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.h" +#endif ControllerInterface g_controller_interface; @@ -85,6 +88,10 @@ void ControllerInterface::Initialize(const WindowSystemInfo& wsi) #ifdef CIFACE_USE_STEAMDECK m_input_backends.emplace_back(ciface::SteamDeck::CreateInputBackend(this)); #endif +#ifdef __EMSCRIPTEN__ + // PlayAgain: four Wii Remotes fed from the page (PlayAgainWiimote.h). + m_input_backends.emplace_back(ciface::PlayAgain::CreateInputBackend(this)); +#endif // Don't allow backends to add devices before the first RefreshDevices() as they will be cleaned // there. Or they'd end up waiting on the devices mutex if populated from another thread. diff --git a/Source/Core/InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.cpp b/Source/Core/InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.cpp new file mode 100644 index 0000000..46e4fd4 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.cpp @@ -0,0 +1,559 @@ +// Copyright 2026 PlayAgain +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "Common/CommonTypes.h" +#include "Common/Config/Config.h" +#include "Common/FileUtil.h" +#include "Common/IniFile.h" +#include "Common/MathUtil.h" +#include "Common/Matrix.h" +#include "Core/Config/SYSCONFSettings.h" +#include "Core/Config/WiimoteSettings.h" +#include "Core/HW/Wiimote.h" +#include "Core/HW/WiimoteEmu/Camera.h" +#include "Core/HW/WiimoteEmu/Dynamics.h" +#include "InputCommon/ControllerInterface/ControllerInterface.h" +#include "InputCommon/ControllerInterface/CoreDevice.h" +#include "InputCommon/ControllerInterface/InputBackend.h" + +#ifdef __EMSCRIPTEN__ +#include +#define PA_WIIMOTE_EXPORT extern "C" EMSCRIPTEN_KEEPALIVE +#else +#define PA_WIIMOTE_EXPORT extern "C" +#endif + +namespace ciface::PlayAgain +{ +// ---- Shared block (mirror in the page's pa-wiimote.js; bump LAYOUT on any change) ---- +// +// u32 words. Header, then REMOTES remotes of REMOTE_WORDS each: +// header [0] MAGIC 'PAWM' [1] LAYOUT [2] REMOTES [3] REMOTE_WORDS [4] HEAD_WORDS +// [5] RING_CAP [6] ENTRY_WORDS [7] reserved +// remote HEAD_WORDS of slots (enum Slot), then RING_CAP entries of ENTRY_WORDS (enum Entry). +// Floats are IEEE-754 f32 bit patterns in the u32 words. +constexpr u32 MAGIC = 0x4D574150; // "PAWM" in memory order +constexpr u32 LAYOUT = 1; +constexpr int REMOTES = 4; +constexpr int HEAD_WORDS = 8; +constexpr int REMOTE_HEAD_WORDS = 32; +constexpr int RING_CAP = 512; // 2.56 s of samples at 200 Hz +constexpr int ENTRY_WORDS = 12; +constexpr int REMOTE_WORDS = REMOTE_HEAD_WORDS + RING_CAP * ENTRY_WORDS; + +enum Slot : int +{ + // Written by the producer (the page or pa_wiimote_set). + SEQ = 0, // seqlock: odd while the state below is written + WRITTEN = 1, // 1 once a state was written (until then: rest, face up, no buttons) + BUTTONS = 2, // WiimoteCommon::ButtonData bits (A 0x0800, B 0x0400, 1 0x0200, 2 0x0100, + // - 0x1000, + 0x0010, Home 0x8000, Up 0x08, Down 0x04, Left 0x01, Right 0x02) + FLAGS = 3, // bit0 pointer valid + ACCEL = 4, // f32 x3, m/s^2 + GYRO = 7, // f32 x3, rad/s + POINTER = 10, // f32 x2, -1..1, +x right, +y up + INPUT_SEQ = 12, + CONNECT_REQ = 13, // 1 connect (source Emulated), 2 disconnect (None); the core clears it + RING_WRITE = 14, // samples pushed (producer) + // Written by the core. + RING_READ = 15, // samples taken + UPDATES = 16, // 200 Hz Wii Remote updates seen + APPLIED_SEQ = 17, // INPUT_SEQ at the last update + RUMBLE = 18, // 0/1, the game's rumble motor + CONNECTED = 19, // 1 when the remote's source is Emulated + RING_PLAYED = 20, // samples played out + REPORTED_BUTTONS = 21, // buttons at the last update + RUMBLE_GEN = 22, // +1 per rumble change +}; + +enum Entry : int +{ + E_BUTTONS = 0, + E_FLAGS = 1, // bit0 buttons valid (else the state's), bit1 pointer fields valid (else the + // state's), bit2 pointer valid + E_ACCEL = 2, + E_GYRO = 5, + E_POINTER = 8, +}; + +struct Block +{ + std::atomic header[HEAD_WORDS]; + std::atomic remotes[REMOTES][REMOTE_WORDS]; +}; + +alignas(16) static Block s_block{{MAGIC, LAYOUT, REMOTES, REMOTE_WORDS, REMOTE_HEAD_WORDS, RING_CAP, + ENTRY_WORDS, 0}, + {}}; + +static float LoadF(const std::atomic* w) +{ + return std::bit_cast(w->load(std::memory_order_relaxed)); +} + +static void StoreF(std::atomic* w, float v) +{ + w->store(std::bit_cast(v), std::memory_order_relaxed); +} + +struct Snapshot +{ + u32 buttons = 0; + bool pointer_valid = false; + float accel[3] = {0, 0, float(MathUtil::GRAVITY_ACCELERATION)}; + float gyro[3] = {0, 0, 0}; + float pointer[2] = {0, 0}; + u32 input_seq = 0; +}; + +static bool ReadState(const std::atomic* r, Snapshot* out) +{ + if (r[WRITTEN].load(std::memory_order_acquire) != 1) + return false; + for (int attempt = 0; attempt < 4; ++attempt) + { + const u32 before = r[SEQ].load(std::memory_order_acquire); + if (before & 1) + continue; + Snapshot s; + s.buttons = r[BUTTONS].load(std::memory_order_relaxed); + s.pointer_valid = (r[FLAGS].load(std::memory_order_relaxed) & 1) != 0; + for (int k = 0; k < 3; ++k) + { + s.accel[k] = LoadF(&r[ACCEL + k]); + s.gyro[k] = LoadF(&r[GYRO + k]); + } + s.pointer[0] = LoadF(&r[POINTER]); + s.pointer[1] = LoadF(&r[POINTER + 1]); + s.input_seq = r[INPUT_SEQ].load(std::memory_order_relaxed); + std::atomic_thread_fence(std::memory_order_acquire); + if (r[SEQ].load(std::memory_order_relaxed) == before) + { + *out = s; + return true; + } + } + return false; +} + +// ---- The device ---- + +class WiimoteDevice final : public Core::Device +{ +public: + explicit WiimoteDevice(int index); + + std::string GetName() const override { return "Wii Remote"; } + std::string GetSource() const override { return "PlayAgain"; } + std::optional GetPreferredId() const override { return m_index; } + bool IsVirtualDevice() const override { return true; } + Core::DeviceRemoval UpdateInput() override; + +private: + class Button final : public Input + { + public: + Button(const char* name, const u32* buttons, u32 mask) + : m_name(name), m_buttons(buttons), m_mask(mask) + { + } + std::string GetName() const override { return m_name; } + ControlState GetState() const override { return (*m_buttons & m_mask) != 0; } + + private: + const char* m_name; + const u32* m_buttons; + u32 m_mask; + }; + + // value * scale; the expression parser clamps negatives to 0, so each axis is two half-axes. + class Value final : public Input + { + public: + Value(std::string name, const float* value, float scale) + : m_name(std::move(name)), m_value(value), m_scale(scale) + { + } + std::string GetName() const override { return m_name; } + ControlState GetState() const override { return ControlState(*m_value) * m_scale; } + bool IsDetectable() const override { return false; } + + private: + std::string m_name; + const float* m_value; + float m_scale; + }; + + class Motor final : public Output + { + public: + explicit Motor(std::atomic* remote) : m_remote(remote) {} + std::string GetName() const override { return "Motor"; } + void SetState(ControlState state) override + { + const u32 on = state > 0.5 ? 1 : 0; + if (m_remote[RUMBLE].exchange(on, std::memory_order_relaxed) != on) + m_remote[RUMBLE_GEN].fetch_add(1, std::memory_order_relaxed); + } + + private: + std::atomic* m_remote; + }; + + void ComputeDots(); + + const int m_index; + std::atomic* const m_remote; + Snapshot m_state; // the latest state from the producer + Snapshot m_effective; // what this update reports (a ring sample, else m_state) + // Pointer for "Point" (Cursor) users: x-, x+, y-, y+ as half-axes; 1 when hidden. + float m_cursor_hidden = 1; + // Point (Passthrough): two sensor-bar dots, x, y (0..1 of the camera), size (0..1 of 15). + std::array m_dots{}; +}; + +WiimoteDevice::WiimoteDevice(int index) + : m_index(index), m_remote(s_block.remotes[index]) +{ + static constexpr std::array, 11> buttons{{ + {"A", 0x0800}, + {"B", 0x0400}, + {"1", 0x0200}, + {"2", 0x0100}, + {"-", 0x1000}, + {"+", 0x0010}, + {"Home", 0x8000}, + {"Up", 0x0008}, + {"Down", 0x0004}, + {"Left", 0x0001}, + {"Right", 0x0002}, + }}; + for (const auto& [name, mask] : buttons) + AddInput(new Button(name, &m_effective.buttons, mask)); + + // IMUAccelerometer: x = Left - Right, y = Backward - Forward, z = Up - Down. + const float* a = m_effective.accel; + AddInput(new Value("Accel Left", &a[0], 1)); + AddInput(new Value("Accel Right", &a[0], -1)); + AddInput(new Value("Accel Backward", &a[1], 1)); + AddInput(new Value("Accel Forward", &a[1], -1)); + AddInput(new Value("Accel Up", &a[2], 1)); + AddInput(new Value("Accel Down", &a[2], -1)); + // IMUGyroscope: x = Pitch Down - Pitch Up, y = Roll Left - Roll Right, z = Yaw Left - Yaw Right. + const float* g = m_effective.gyro; + AddInput(new Value("Gyro Pitch Down", &g[0], 1)); + AddInput(new Value("Gyro Pitch Up", &g[0], -1)); + AddInput(new Value("Gyro Roll Left", &g[1], 1)); + AddInput(new Value("Gyro Roll Right", &g[1], -1)); + AddInput(new Value("Gyro Yaw Left", &g[2], 1)); + AddInput(new Value("Gyro Yaw Right", &g[2], -1)); + + for (int dot = 0; dot < 2; ++dot) + { + AddInput(new Value(fmt::format("IR Object {} X", dot + 1), &m_dots[dot * 3 + 0], 1)); + AddInput(new Value(fmt::format("IR Object {} Y", dot + 1), &m_dots[dot * 3 + 1], 1)); + AddInput(new Value(fmt::format("IR Object {} Size", dot + 1), &m_dots[dot * 3 + 2], 1)); + } + + // Alternative mapping for Dolphin's "Point" (Cursor) group; unused by default. + const float* p = m_effective.pointer; + AddInput(new Value("Cursor X-", &p[0], -1)); + AddInput(new Value("Cursor X+", &p[0], 1)); + AddInput(new Value("Cursor Y-", &p[1], -1)); + AddInput(new Value("Cursor Y+", &p[1], 1)); + AddInput(new Value("Cursor Hide", &m_cursor_hidden, 1)); + + AddOutput(new Motor(m_remote)); +} + +// The two dots Dolphin's emulated camera would see for this pointer: EmulatePoint (yaw/pitch toward +// the cursor at 2 m, the sensor bar's vertical offset of 10 cm) through GetCameraPoints, with the +// Cursor group's defaults (Total Yaw 25 deg, Total Pitch 20 deg) and the default camera FOV. +void WiimoteDevice::ComputeDots() +{ + m_dots.fill(0); + if (!m_effective.pointer_valid) + return; + + constexpr float NEUTRAL_DISTANCE = 2.f; + constexpr float VERTICAL_OFFSET = 0.10f; + constexpr float YAW_SCALE = float(MathUtil::TAU * 25 / 360) / 2; + constexpr float PITCH_SCALE = float(MathUtil::TAU * 20 / 360) / 2; + const bool sensor_bar_on_top = Config::Get(Config::SYSCONF_SENSOR_BAR_POSITION) != 0; + const float height = VERTICAL_OFFSET * (sensor_bar_on_top ? 1 : -1); + + const Common::Vec3 position{0, NEUTRAL_DISTANCE, -height}; + const Common::Vec3 angle{PITCH_SCALE * -m_effective.pointer[1], 0, + YAW_SCALE * -m_effective.pointer[0]}; + const auto transform = + Common::Matrix44::FromMatrix33(WiimoteEmu::GetRotationalMatrix(-angle)) * + Common::Matrix44::Translate(-position); + const auto points = WiimoteEmu::CameraLogic::GetCameraPoints( + transform, + Common::Vec2(WiimoteEmu::CameraLogic::CAMERA_FOV_X, WiimoteEmu::CameraLogic::CAMERA_FOV_Y)); + + for (int dot = 0; dot < 2; ++dot) + { + const auto& pt = points[dot]; + if (pt.position.x >= WiimoteEmu::CameraLogic::CAMERA_RES_X || + pt.position.y >= WiimoteEmu::CameraLogic::CAMERA_RES_Y || pt.size == 0) + continue; + m_dots[dot * 3 + 0] = float(pt.position.x) / (WiimoteEmu::CameraLogic::CAMERA_RES_X - 1); + m_dots[dot * 3 + 1] = float(pt.position.y) / (WiimoteEmu::CameraLogic::CAMERA_RES_Y - 1); + m_dots[dot * 3 + 2] = float(pt.size) / WiimoteEmu::CameraLogic::MAX_POINT_SIZE; + } +} + +Core::DeviceRemoval WiimoteDevice::UpdateInput() +{ + // One step per Wii Remote update (200 Hz of emulated time); the SI poll and the host leave the + // remote as it is. + if (ControllerInterface::GetCurrentInputChannel() != ciface::InputChannel::Bluetooth) + return Core::DeviceRemoval::Keep; + + std::atomic* r = m_remote; + ReadState(r, &m_state); + m_effective = m_state; + + const u32 read = r[RING_READ].load(std::memory_order_relaxed); + const u32 write = r[RING_WRITE].load(std::memory_order_acquire); + if (read != write) + { + const std::atomic* e = &r[REMOTE_HEAD_WORDS + (read % RING_CAP) * ENTRY_WORDS]; + const u32 flags = e[E_FLAGS].load(std::memory_order_relaxed); + if (flags & 1) + m_effective.buttons = e[E_BUTTONS].load(std::memory_order_relaxed); + for (int k = 0; k < 3; ++k) + { + m_effective.accel[k] = LoadF(&e[E_ACCEL + k]); + m_effective.gyro[k] = LoadF(&e[E_GYRO + k]); + } + if (flags & 2) + { + m_effective.pointer_valid = (flags & 4) != 0; + m_effective.pointer[0] = LoadF(&e[E_POINTER]); + m_effective.pointer[1] = LoadF(&e[E_POINTER + 1]); + } + r[RING_READ].store(read + 1, std::memory_order_release); + r[RING_PLAYED].fetch_add(1, std::memory_order_relaxed); + } + + m_cursor_hidden = m_effective.pointer_valid ? 0.f : 1.f; + ComputeDots(); + + r[REPORTED_BUTTONS].store(m_effective.buttons, std::memory_order_relaxed); + r[APPLIED_SEQ].store(m_state.input_seq, std::memory_order_relaxed); + r[UPDATES].fetch_add(1, std::memory_order_release); + return Core::DeviceRemoval::Keep; +} + +// ---- Backend ---- + +class InputBackend final : public ciface::InputBackend +{ +public: + using ciface::InputBackend::InputBackend; + void PopulateDevices() override + { + for (int i = 0; i < REMOTES; ++i) + GetControllerInterface().AddDevice(std::make_shared(i)); + } +}; + +std::unique_ptr CreateInputBackend(ControllerInterface* controller_interface) +{ + return std::make_unique(controller_interface); +} + +// ---- Mapping and connection ---- + +static void WriteDefaultMapping() +{ + Common::IniFile ini; + for (int i = 0; i < REMOTES; ++i) + { + auto* s = ini.GetOrCreateSection(fmt::format("Wiimote{}", i + 1)); + s->Set("Device", fmt::format("PlayAgain/{}/Wii Remote", i)); + for (const char* b : {"A", "B", "1", "2", "-", "+", "Home"}) + s->Set(fmt::format("Buttons/{}", b), fmt::format("`{}`", b)); + for (const char* d : {"Up", "Down", "Left", "Right"}) + s->Set(fmt::format("D-Pad/{}", d), fmt::format("`{}`", d)); + for (const char* a : {"Up", "Down", "Left", "Right", "Forward", "Backward"}) + s->Set(fmt::format("IMUAccelerometer/{}", a), fmt::format("`Accel {}`", a)); + for (const char* g : + {"Pitch Up", "Pitch Down", "Roll Left", "Roll Right", "Yaw Left", "Yaw Right"}) + s->Set(fmt::format("IMUGyroscope/{}", g), fmt::format("`Gyro {}`", g)); + // The phone's rates are already bias-corrected: no extra dead zone or recalibration. + s->Set("IMUGyroscope/Dead Zone", "0"); + s->Set("IMUGyroscope/Calibration Period", "0"); + // The pointer comes from the page, not from integrating the gyroscope. + s->Set("IMUIR/Enabled", "False"); + s->Set("IRPassthrough/Enabled", "True"); + for (int dot = 1; dot <= 2; ++dot) + { + for (const char* c : {"X", "Y", "Size"}) + { + s->Set(fmt::format("IRPassthrough/Object {} {}", dot, c), + fmt::format("`IR Object {} {}`", dot, c)); + } + } + s->Set("Rumble/Motor", "`Motor`"); + s->Set("Extension", "None"); + } + ini.Save(File::GetUserPath(D_CONFIG_IDX) + WIIMOTE_INI_NAME ".ini"); +} + +static void ApplyConnectRequests(bool force_status) +{ + for (int i = 0; i < REMOTES; ++i) + { + std::atomic* r = s_block.remotes[i]; + const u32 req = r[CONNECT_REQ].exchange(0, std::memory_order_acq_rel); + const auto& info = Config::GetInfoForWiimoteSource(i); + if (req == 1 || req == 2) + { + const WiimoteSource wanted = req == 1 ? WiimoteSource::Emulated : WiimoteSource::None; + if (Config::Get(info) != wanted) + Config::SetBase(info, wanted); + } + if (req == 1 || req == 2 || force_status) + { + r[CONNECTED].store(Config::Get(info) == WiimoteSource::Emulated ? 1 : 0, + std::memory_order_release); + } + } +} + +void InitializeWiimotes() +{ + WriteDefaultMapping(); + Wiimote::Initialize(Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES); + ApplyConnectRequests(true); +} + +void OnBluetoothUpdate() +{ + ApplyConnectRequests(false); +} + +} // namespace ciface::PlayAgain + +// ---- C API ---- +// i: remote 0-3. Callable from any thread; each remote expects one writer. + +using namespace ciface::PlayAgain; + +static std::atomic* Remote(int i) +{ + return (i >= 0 && i < REMOTES) ? s_block.remotes[i] : nullptr; +} + +// Byte address of the shared block (the page writes it through the core's SharedArrayBuffer). +PA_WIIMOTE_EXPORT u32 pa_wiimote_block() +{ + return static_cast(reinterpret_cast(&s_block)); +} + +PA_WIIMOTE_EXPORT u32 pa_wiimote_layout() +{ + return LAYOUT; +} + +// The remote's current state, held until the next call: buttons (Dolphin's bits), accelerometer +// (m/s^2) and gyroscope (rad/s) in the Wii Remote frame, pointer -1..1 (+x right, +y up). +PA_WIIMOTE_EXPORT void pa_wiimote_set(int i, u32 buttons, float ax, float ay, float az, float gx, + float gy, float gz, float px, float py, int pointer_valid) +{ + std::atomic* r = Remote(i); + if (!r) + return; + const u32 seq = r[SEQ].load(std::memory_order_relaxed) | 1; + r[SEQ].store(seq, std::memory_order_relaxed); + std::atomic_thread_fence(std::memory_order_release); + r[BUTTONS].store(buttons, std::memory_order_relaxed); + r[FLAGS].store(pointer_valid ? 1 : 0, std::memory_order_relaxed); + const float v[8] = {ax, ay, az, gx, gy, gz, px, py}; + for (int k = 0; k < 3; ++k) + { + StoreF(&r[ACCEL + k], v[k]); + StoreF(&r[GYRO + k], v[3 + k]); + } + StoreF(&r[POINTER], v[6]); + StoreF(&r[POINTER + 1], v[7]); + r[INPUT_SEQ].fetch_add(1, std::memory_order_relaxed); + r[SEQ].store(seq + 1, std::memory_order_release); + r[WRITTEN].store(1, std::memory_order_release); +} + +// Queue samples played out one per Wii Remote update (200 Hz of emulated time), ahead of the held +// state: n entries of 12 u32 words [buttons, flags, ax, ay, az, gx, gy, gz, px, py, 0, 0] (floats +// as f32 bits; flags bit0 buttons valid, bit1 pointer fields valid, bit2 pointer valid). +// Returns how many fit. +PA_WIIMOTE_EXPORT int pa_wiimote_push(int i, const u32* entries, int n) +{ + std::atomic* r = Remote(i); + if (!r || !entries || n <= 0) + return 0; + u32 write = r[RING_WRITE].load(std::memory_order_relaxed); + const u32 read = r[RING_READ].load(std::memory_order_acquire); + int pushed = 0; + while (pushed < n && write - read < static_cast(RING_CAP)) + { + std::atomic* e = &r[REMOTE_HEAD_WORDS + (write % RING_CAP) * ENTRY_WORDS]; + for (int k = 0; k < ENTRY_WORDS; ++k) + e[k].store(entries[pushed * ENTRY_WORDS + k], std::memory_order_relaxed); + ++write; + ++pushed; + } + r[RING_WRITE].store(write, std::memory_order_release); + return pushed; +} + +// Samples still queued. +PA_WIIMOTE_EXPORT int pa_wiimote_queued(int i) +{ + std::atomic* r = Remote(i); + return r ? static_cast(r[RING_WRITE].load(std::memory_order_acquire) - + r[RING_READ].load(std::memory_order_acquire)) : + 0; +} + +// Plug the remote in (source Emulated) or out (None). Applied at the next Wii Remote update (or at +// once before the first boot). Remote 0 starts connected, 1-3 not (Dolphin's defaults). +PA_WIIMOTE_EXPORT void pa_wiimote_connect(int i, int on) +{ + std::atomic* r = Remote(i); + if (r) + r[CONNECT_REQ].store(on ? 1 : 2, std::memory_order_release); +} + +PA_WIIMOTE_EXPORT int pa_wiimote_connected(int i) +{ + std::atomic* r = Remote(i); + return r ? static_cast(r[CONNECTED].load(std::memory_order_acquire)) : 0; +} + +PA_WIIMOTE_EXPORT int pa_wiimote_rumble(int i) +{ + std::atomic* r = Remote(i); + return r ? static_cast(r[RUMBLE].load(std::memory_order_relaxed)) : 0; +} + +PA_WIIMOTE_EXPORT u32 pa_wiimote_updates(int i) +{ + std::atomic* r = Remote(i); + return r ? r[UPDATES].load(std::memory_order_acquire) : 0; +} diff --git a/Source/Core/InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.h b/Source/Core/InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.h new file mode 100644 index 0000000..372642a --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/PlayAgain/PlayAgainWiimote.h @@ -0,0 +1,41 @@ +// Copyright 2026 PlayAgain +// SPDX-License-Identifier: GPL-2.0-or-later + +// PlayAgain Wii Remotes: four ControllerInterface devices ("PlayAgain/0..3/Wii Remote") fed from +// JavaScript, mapped by default onto the four emulated Wii Remotes' buttons, D-pad, accelerometer, +// gyroscope (MotionPlus) and IR camera (Point (Passthrough)). +// +// Units and frame are Dolphin's emulated Wii Remote's own (WiimoteEmu, DSU client): +// accelerometer m/s^2, gyroscope rad/s, in the Wii Remote frame: +x = the remote's left, +// +y = toward the player (away from the IR end), +z = up out of the button face. +// At rest, face up: accel (0, 0, +9.80665). IR end pitching up: gyro x < 0. Yaw left: gyro z > 0. +// Pointer: -1..1 of Dolphin's pointing box (Total Yaw 25 deg, Total Pitch 20 deg), +x right, +// +y up; turned into the two sensor-bar dots with Dolphin's own camera model. +// +// Inputs reach the core through a block of shared memory (PA_WIIMOTE_LAYOUT, below) that any thread +// may write (seqlock state + a single-producer ring of samples played out one per 200 Hz Wii Remote +// update, in emulated time). The C API (pa_wiimote_*) writes the same block. + +#pragma once + +#include + +class ControllerInterface; + +namespace ciface +{ +class InputBackend; +} + +namespace ciface::PlayAgain +{ +std::unique_ptr CreateInputBackend(ControllerInterface* controller_interface); + +// Host thread, once, after g_controller_interface.Initialize(): writes the default mapping +// (WiimoteNew.ini) and initializes the emulated Wii Remotes. +void InitializeWiimotes(); + +// CPU thread, at each Bluetooth (Wii Remote) update, before ControllerInterface::UpdateInput: +// applies pending connect/disconnect requests (Wii Remote source Emulated/None). +void OnBluetoothUpdate(); +} // namespace ciface::PlayAgain