upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/gameinput_test.cpp
diff options
context:
space:
mode:
authorTyrannys <80750971+Tyrannys@users.noreply.github.com>2026-11-27 13:37:20 -0600
committerTyrannys <80750971+Tyrannys@users.noreply.github.com>2026-11-27 13:37:20 -0600
commitaeb24285be97e415c9f46e262791473d44a66f0c (patch)
tree6a9512f56fb6e5d282531d163c732123da42376e /runtime/test/gameinput_test.cpp
parent43a9d562d355e7c5c428e486071b456353de8b05 (diff)
gameinput: force-feedback kind changes, SetParams features, locked motor reads, no null memcpy
From a review of the shim by kevincommits (#1608): - ForceFeedbackEffect::SetParams accepted any kind, also one the motor does not report, which CreateForceFeedbackEffect refuses. It now refuses it the same way. - A kind change through SetParams was sent to SDL as an update of the existing effect, which SDL_UpdateHapticEffect refuses ("Updating effect type is illegal"), so the effect stopped. The poll thread now destroys the old SDL effect and creates one of the new type. The test's fake haptics refuses a type change in an update, as SDL does. - CreateForceFeedbackEffect and IsForceFeedbackMotorPoweredOn read the motor count, features and haptic handle that open_pad and close_pad write on the poll thread under g().mu, without the lock. They take it now. - GetControllerAxisState and GetControllerButtonState called memcpy with a null buffer for 0 bytes. They copy only when there is something to copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp34
1 files changed, 30 insertions, 4 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 09d959f34..55c0d2123 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -1272,17 +1272,19 @@ void test_twins(IGameInput* input) {
1272 1272
1273// ---- force feedback and haptic motors: a fake SDL haptic backend ---- 1273// ---- force feedback and haptic motors: a fake SDL haptic backend ----
1274// SDL's virtual joysticks report no haptics, so the shim's haptic calls go through this table instead. It records what 1274// SDL's virtual joysticks report no haptics, so the shim's haptic calls go through this table instead. It records what
1275// the shim asked SDL to do and reports the capabilities a force-feedback wheel has. 1275// the shim asked SDL to do and reports the capabilities a force-feedback wheel has: every effect kind but inertia,
1276// so that a kind the motor lacks can be refused.
1276struct FakeHaptic { 1277struct FakeHaptic {
1277 uint32_t features = SDL_HAPTIC_CONSTANT | SDL_HAPTIC_SINE | SDL_HAPTIC_SQUARE | SDL_HAPTIC_TRIANGLE | 1278 uint32_t features = SDL_HAPTIC_CONSTANT | SDL_HAPTIC_SINE | SDL_HAPTIC_SQUARE | SDL_HAPTIC_TRIANGLE |
1278 SDL_HAPTIC_SAWTOOTHUP | SDL_HAPTIC_SAWTOOTHDOWN | SDL_HAPTIC_RAMP | SDL_HAPTIC_SPRING | 1279 SDL_HAPTIC_SAWTOOTHUP | SDL_HAPTIC_SAWTOOTHDOWN | SDL_HAPTIC_RAMP | SDL_HAPTIC_SPRING |
1279 SDL_HAPTIC_FRICTION | SDL_HAPTIC_DAMPER | SDL_HAPTIC_INERTIA; 1280 SDL_HAPTIC_FRICTION | SDL_HAPTIC_DAMPER;
1280 int max_effects = 8; 1281 int max_effects = 8;
1281 int create_result = 1; // what create returns; negative is SDL refusing the effect 1282 int create_result = 1; // what create returns; negative is SDL refusing the effect
1282 int opens = 0, closes = 0, creates = 0, updates = 0, runs = 0, stops = 0, destroys = 0; 1283 int opens = 0, closes = 0, creates = 0, updates = 0, runs = 0, stops = 0, destroys = 0;
1283 int gain_calls = 0, last_gain = -1; 1284 int gain_calls = 0, last_gain = -1;
1284 uint32_t last_iterations = 0; 1285 uint32_t last_iterations = 0;
1285 SDL_HapticEffect last{}; 1286 SDL_HapticEffect last{};
1287 uint16_t created_type = 0; // the type of the effect create made; update refuses another, as SDL does
1286}; 1288};
1287FakeHaptic g_fake; 1289FakeHaptic g_fake;
1288bool g_fake_open = false; // pads attached while this is set are opened as haptic devices 1290bool g_fake_open = false; // pads attached while this is set are opened as haptic devices
@@ -1303,12 +1305,18 @@ int fake_create(void* h, const void* e) {
1303 return -1; 1305 return -1;
1304 } 1306 }
1305 f->last = *static_cast<const SDL_HapticEffect*>(e); 1307 f->last = *static_cast<const SDL_HapticEffect*>(e);
1308 f->created_type = f->last.type;
1306 return f->create_result; 1309 return f->create_result;
1307} 1310}
1308bool fake_update(void* h, int, const void* e) { 1311bool fake_update(void* h, int, const void* e) {
1309 FakeHaptic* f = static_cast<FakeHaptic*>(h); 1312 FakeHaptic* f = static_cast<FakeHaptic*>(h);
1310 ++f->updates; 1313 ++f->updates;
1311 f->last = *static_cast<const SDL_HapticEffect*>(e); 1314 const auto* effect = static_cast<const SDL_HapticEffect*>(e);
1315 if (effect->type != f->created_type) { // SDL_UpdateHapticEffect: "Haptic: Updating effect type is illegal"
1316 SDL_SetError("fake: updating effect type is illegal");
1317 return false;
1318 }
1319 f->last = *effect;
1312 return true; 1320 return true;
1313} 1321}
1314bool fake_run(void* h, int, uint32_t iterations) { 1322bool fake_run(void* h, int, uint32_t iterations) {
@@ -1379,6 +1387,10 @@ void test_force_feedback(IGameInput* input) {
1379 unknown.kind = static_cast<GameInputForceFeedbackEffectKind>(11); 1387 unknown.kind = static_cast<GameInputForceFeedbackEffectKind>(11);
1380 check(dev->CreateForceFeedbackEffect(0, &unknown, reinterpret_cast<void**>(&effect)) == E_INVALIDARG, 1388 check(dev->CreateForceFeedbackEffect(0, &unknown, reinterpret_cast<void**>(&effect)) == E_INVALIDARG,
1381 "force feedback: an effect kind outside the enum is E_INVALIDARG"); 1389 "force feedback: an effect kind outside the enum is E_INVALIDARG");
1390 GameInputForceFeedbackParams inertia{};
1391 inertia.kind = GameInputForceFeedbackInertia;
1392 check(dev->CreateForceFeedbackEffect(0, &inertia, reinterpret_cast<void**>(&effect)) == E_INVALIDARG && !effect,
1393 "force feedback: a kind the motor does not report is E_INVALIDARG");
1382 check(dev->CreateForceFeedbackEffect(0, &params, reinterpret_cast<void**>(&effect)) == S_OK && effect, 1394 check(dev->CreateForceFeedbackEffect(0, &params, reinterpret_cast<void**>(&effect)) == S_OK && effect,
1383 "force feedback: CreateForceFeedbackEffect"); 1395 "force feedback: CreateForceFeedbackEffect");
1384 if (!effect) { // leave nothing behind for the next test 1396 if (!effect) { // leave nothing behind for the next test
@@ -1459,8 +1471,19 @@ void test_force_feedback(IGameInput* input) {
1459 wave.data.sineWave.magnitude.linearX = 1.0f; 1471 wave.data.sineWave.magnitude.linearX = 1.0f;
1460 wave.data.sineWave.frequency = 50.0f; 1472 wave.data.sineWave.frequency = 50.0f;
1461 wave.data.sineWave.phase = 90.0f; 1473 wave.data.sineWave.phase = 90.0f;
1474 // A kind the motor lacks is refused by SetParams too, and the effect keeps what it had.
1475 check(!effect->SetParams(&inertia), "force feedback: SetParams refuses a kind the motor does not report");
1476 effect->GetParams(&read);
1477 check(read.kind == GameInputForceFeedbackConstant, "force feedback: a refused SetParams changes nothing");
1478 // Another kind: SDL cannot update an effect to another type, so the shim makes a new one (the fake refuses a type
1479 // change in an update, as SDL_UpdateHapticEffect does).
1480 const int creates_before_wave = g_fake.creates, destroys_before_wave = g_fake.destroys;
1462 check(effect->SetParams(&wave), "force feedback: SetParams for a sine wave"); 1481 check(effect->SetParams(&wave), "force feedback: SetParams for a sine wave");
1463 rt::gameinput::poll(); 1482 rt::gameinput::poll();
1483 rt::gameinput::OutputState wave_out{};
1484 check(g_fake.creates == creates_before_wave + 1 && g_fake.destroys == destroys_before_wave + 1 &&
1485 rt::gameinput::output_state(0, &wave_out) && wave_out.effect_ran && !wave_out.effect_error[0],
1486 "force feedback: a new kind replaces the SDL effect instead of updating its type");
1464 check(g_fake.last.type == SDL_HAPTIC_SINE && g_fake.last.periodic.period == 20 && 1487 check(g_fake.last.type == SDL_HAPTIC_SINE && g_fake.last.periodic.period == 20 &&
1465 g_fake.last.periodic.magnitude == 32767 && g_fake.last.periodic.phase == 9000, 1488 g_fake.last.periodic.magnitude == 32767 && g_fake.last.periodic.phase == 9000,
1466 "force feedback: a 50 Hz sine with a 90 degree phase becomes SDL's period and hundredths of a degree"); 1489 "force feedback: a 50 Hz sine with a 90 degree phase becomes SDL's period and hundredths of a degree");
@@ -1509,9 +1532,10 @@ void test_force_feedback(IGameInput* input) {
1509 rt::gameinput::poll(); 1532 rt::gameinput::poll();
1510 check(g_fake.runs == runs1 + 1, "force feedback: and it runs again once the mute is lifted"); 1533 check(g_fake.runs == runs1 + 1, "force feedback: and it runs again once the mute is lifted");
1511 1534
1535 const int destroys_before_release = g_fake.destroys;
1512 check(effect->Release() == 0, "force feedback: the effect is freed once the title releases it"); 1536 check(effect->Release() == 0, "force feedback: the effect is freed once the title releases it");
1513 rt::gameinput::poll(); 1537 rt::gameinput::poll();
1514 check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread"); 1538 check(g_fake.destroys == destroys_before_release + 1, "force feedback: its SDL effect is destroyed on the poll thread");
1515 1539
1516 // RT-262: the title releases effects while the poll thread applies them. The poll thread used to AddRef an effect 1540 // RT-262: the title releases effects while the poll thread applies them. The poll thread used to AddRef an effect
1517 // whose count had already reached 0 (its destructor waiting for the lock), use it after it was freed, and free it 1541 // whose count had already reached 0 (its destructor waiting for the lock), use it after it was freed, and free it
@@ -1914,6 +1938,8 @@ int main(int argc, char** argv) {
1914 check(reading->GetControllerButtonState(64, buttons) == SDL_GAMEPAD_BUTTON_COUNT && buttons[SDL_GAMEPAD_BUTTON_SOUTH] && 1938 check(reading->GetControllerButtonState(64, buttons) == SDL_GAMEPAD_BUTTON_COUNT && buttons[SDL_GAMEPAD_BUTTON_SOUTH] &&
1915 !buttons[SDL_GAMEPAD_BUTTON_EAST], 1939 !buttons[SDL_GAMEPAD_BUTTON_EAST],
1916 "controller buttons"); 1940 "controller buttons");
1941 check(reading->GetControllerAxisState(8, nullptr) == 0 && reading->GetControllerButtonState(64, nullptr) == 0,
1942 "controller axes and buttons with no buffer: none written");
1917 check(!reading->GetMotionState(nullptr) && !reading->GetMouseState(nullptr), "no motion or mouse state"); 1943 check(!reading->GetMotionState(nullptr) && !reading->GetMouseState(nullptr), "no motion or mouse state");
1918 IGameInputReading* none = nullptr; 1944 IGameInputReading* none = nullptr;
1919 check(input->GetNextReading(reading, GameInputKindGamepad, nullptr, &none) == GAMEINPUT_E_READING_NOT_FOUND && !none, 1945 check(input->GetNextReading(reading, GameInputKindGamepad, nullptr, &none) == GAMEINPUT_E_READING_NOT_FOUND && !none,