mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 15:38:38 +00:00
Expose SDL3's gyroscope and accelerometer through the GameInput shim: - gameinput_abi.h: GameInputMotionAccuracy, GameInputMotionInfo and GameInputMotionState with the v0 layouts (Microsoft's public docs), size asserts included. - The shim enables both sensors when a pad with them is opened, reads the latest sample each poll, and reports GameInputKindMotion plus a GameInputMotionInfo on such pads. GetMotionState returns the state as signed fractions of the motionInfo maxima (2 g accel, 2000 deg/s gyro, the shim's choice since SDL exposes no sensor range; VERIFY). All-zero accel means no sample yet, so readings before the device's first sample carry no motion. Magnetic field and orientation stay zero and Unknown: SDL has no compass and nothing synthesises a quaternion. Pads with sensors stream a reading every ~8 ms so motion is not stuck at the last button event; other pads keep the old change-driven behaviour. - GetCurrentReading and GetNextReading with a device filter now reject kinds the device does not support (READING_NOT_FOUND), matching the v0 docs and the device-less path, so a motion query on a plain pad fails instead of returning a reading without motion. - gameinput_test: a virtual pad with SDL sensors covers kind/motionInfo presence, no-motion-before-first-sample, normalisation, accuracy values, the +-1 clamp, the cadence, and the plain-pad rejection. gameinput_probe prints the live accel and gyro fractions. - docs/gameinput.md: a Motion section, the API table, and VERIFY notes for the chosen maxima and the unprovided members. Tested: gameinput_test 185/185 and ctest 90/90 (RelWithDebInfo) on Windows 11 Pro 10.0.26200; SDL3 3.4.20 via -DLEONIDA_SDL3_FETCH=ON. Follows: #660. AI-assisted (ZCode agent).
110 lines
4.9 KiB
C++
110 lines
4.9 KiB
C++
// gameinput_probe: the GameInput shim on a real controller. Not a CTest; run it by hand with a pad connected.
|
|
//
|
|
// gameinput_probe [--seconds N] [--rumble]
|
|
//
|
|
// Lists the gamepads the shim sees, then prints the live gamepad reading of each for N seconds (default 10). With
|
|
// --rumble it first plays, on every pad: the low-frequency motor, the high-frequency motor, the left and the right
|
|
// impulse trigger, one second each. On a DualSense the trigger steps run in the LEONIDA_DS_TRIGGERS mode (rumble by
|
|
// default; set it to resistance to feel that one). Everything goes through GameInputCreate from the shim registry,
|
|
// as a title would call it. No game data.
|
|
#include <windows.h>
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <vector>
|
|
|
|
#include "gameinput.h"
|
|
#include "gameinput_abi.h"
|
|
#include "shims.h"
|
|
|
|
using namespace gi;
|
|
|
|
namespace {
|
|
|
|
std::vector<IGameInputDevice*> g_devices;
|
|
|
|
void CALLBACK on_device(GameInputCallbackToken, void*, IGameInputDevice* device, uint64_t, uint32_t current, uint32_t previous) {
|
|
const GameInputDeviceInfo* i = device->GetDeviceInfo();
|
|
std::printf("\n%s %s (%04x:%04x) motors=0x%x\n", (current & GameInputDeviceConnected) ? "connected " : "disconnected",
|
|
i->displayName ? i->displayName->data : "?", i->vendorId, i->productId, i->supportedRumbleMotors);
|
|
if ((current & GameInputDeviceConnected) && !(previous & GameInputDeviceConnected)) {
|
|
device->AddRef();
|
|
g_devices.push_back(device);
|
|
}
|
|
}
|
|
|
|
void play(const char* what, GameInputRumbleParams p) {
|
|
std::printf("rumble: %s\n", what);
|
|
for (IGameInputDevice* d : g_devices) d->SetRumbleState(&p);
|
|
Sleep(1000);
|
|
for (IGameInputDevice* d : g_devices) d->SetRumbleState(nullptr);
|
|
Sleep(300);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
int seconds = 10;
|
|
bool rumble = false;
|
|
for (int i = 1; i < argc; ++i) {
|
|
if (!std::strcmp(argv[i], "--seconds") && i + 1 < argc) seconds = std::atoi(argv[++i]);
|
|
else if (!std::strcmp(argv[i], "--rumble")) rumble = true;
|
|
else {
|
|
std::printf("usage: gameinput_probe [--seconds N] [--rumble]\n");
|
|
return 2;
|
|
}
|
|
}
|
|
auto create = reinterpret_cast<GameInputCreateFn>(rt::shims::find("gameinput.dll", "GameInputCreate"));
|
|
IGameInput* input = nullptr;
|
|
if (!create || create(&input) != S_OK) {
|
|
std::printf("GameInputCreate failed\n");
|
|
return 1;
|
|
}
|
|
Sleep(500); // let the poll thread open what is connected
|
|
GameInputCallbackToken token = 0;
|
|
input->RegisterDeviceCallback(nullptr, GameInputKindGamepad, GameInputDeviceAnyStatus, GameInputBlockingEnumeration,
|
|
nullptr, on_device, &token);
|
|
if (g_devices.empty()) std::printf("no gamepad connected; plug one in (watching for %d s)\n", seconds);
|
|
|
|
if (rumble && !g_devices.empty()) {
|
|
const auto& c = rt::gameinput::config();
|
|
std::printf("DualSense trigger mode: %s\n", c.ds_triggers == rt::gameinput::DsTriggerMode::Off ? "off"
|
|
: c.ds_triggers == rt::gameinput::DsTriggerMode::Resistance ? "resistance"
|
|
: "rumble");
|
|
play("low-frequency motor", {1, 0, 0, 0});
|
|
play("high-frequency motor", {0, 1, 0, 0});
|
|
play("left trigger", {0, 0, 1, 0});
|
|
play("right trigger", {0, 0, 0, 1});
|
|
play("left trigger at 30%", {0, 0, 0.3f, 0});
|
|
}
|
|
|
|
const ULONGLONG end = GetTickCount64() + static_cast<ULONGLONG>(seconds) * 1000;
|
|
while (GetTickCount64() < end) {
|
|
IGameInputReading* r = nullptr;
|
|
if (input->GetCurrentReading(GameInputKindGamepad, nullptr, &r) == S_OK) {
|
|
GameInputGamepadState s{};
|
|
GameInputUiNavigationState ui{};
|
|
GameInputMotionState m{};
|
|
const bool motion = r->GetMotionState(&m);
|
|
r->GetGamepadState(&s);
|
|
r->GetUiNavigationState(&ui);
|
|
std::printf("\rbuttons=%04x ui=%06x LT=%.2f RT=%.2f L=(%+.2f,%+.2f) R=(%+.2f,%+.2f) seq=%llu ", s.buttons,
|
|
ui.buttons, s.leftTrigger, s.rightTrigger, s.leftThumbstickX, s.leftThumbstickY,
|
|
s.rightThumbstickX, s.rightThumbstickY,
|
|
static_cast<unsigned long long>(r->GetSequenceNumber(GameInputKindGamepad)));
|
|
if (motion) // fractions of motionInfo's maxima: accel includes gravity, so a resting pad shows ~0.5 g
|
|
std::printf("\n[a]=(%+.2f,%+.2f,%+.2f) [w]=(%+.2f,%+.2f,%+.2f) ", m.accelerationX, m.accelerationY,
|
|
m.accelerationZ, m.angularVelocityX, m.angularVelocityY, m.angularVelocityZ);
|
|
std::fflush(stdout);
|
|
r->Release();
|
|
}
|
|
Sleep(16);
|
|
}
|
|
std::printf("\n");
|
|
input->UnregisterCallback(token, 0);
|
|
for (IGameInputDevice* d : g_devices) d->Release();
|
|
input->Release();
|
|
return 0;
|
|
}
|