Skip to content
Closed
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
Record player input as DM_KEYS steps (DM_REC)
DM_REC=<file> writes player 1's physical input (keyboard, mouse touches,
player 1's controller and the controllers a one-player game merges into
it) to the file as DM_KEYS steps, so a run plays again from the same save
and build: a bug report's path without game data, or a smoke route
recorded instead of written (roadmap item in #313). It records where the
front ends publish (dm_input_set, dm_pad_set_player, dm_touch_set), so the
Mac and iOS front ends both feed it; DM_KEYS and DM_CTL touch through a
new dm_touch_script and keep their own button path, so scripted input is
never recorded. Off, a publish costs one relaxed load and branch; on, the
publishing thread compares and formats into memory and a writer thread
appends to the file every 250 ms and at exit.

Each step is one state from the presented frame it began to the frame it
ended ("f1524:A+L0.50/-0.25~f12,"), because under DM_DETCLOCK the guest
clock follows the presents while seconds drift with load. DM_KEYS gains
what replaying that needs: "~f<frames>" holds in frames, and a step due
when such a hold ends follows it without a release (buttons stay held, a
dragged finger moves instead of lifting). Commas, spaces and newlines
separate steps, the parser takes the whole value (it cut at 4,096 bytes)
and up to 1,048,576 steps (was 1,024), ordered by a stable merge sort.
Existing routes behave as before. Sticks are kept to hundredths and
re-recorded on a 0.02 move; touch to thousandths and a 0.005 move.

Checked: t_hid's new round trip records a known sequence (keyboard,
controller stick with drift, a sub-frame tap, a dragged touch, shake, a
press held at exit, and scripted presses that must not appear), compares
the exact text, parses it back with the DM_KEYS parser and replays it on
the scripted path with a simulated frame clock: the game's view of every
frame matches the recording (130 checks, also under ThreadSanitizer). Host
unit tests, Mario and Breath of the Wild host builds, the Python tool
tests, docs and env-switch tests pass; lint shows no new findings. On the
Mac host, Breath of the Wild ran 75 s with DM_REC and frame-held DM_KEYS
steps: the steps ran in order, SIGTERM flushed through the graceful exit,
and the recording stayed empty (only scripted input). Recording real
keyboard input on the Mac was not tried.
  • Loading branch information
v0lumehi committed Oct 29, 2026
commit f930a52ffe32868e2d6cc7a2b442ba6de93ee468
1 change: 1 addition & 0 deletions dm/host/front.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void dm_host_preload_start(int argc, char **argv);
// The host converts to the panel coordinates the game reads, 1280x720 (x·1280, y·720), and latches short taps (a down and up between two reads is reported at least once). May be called from any thread.
typedef struct { uint32_t id; float x, y; } dm_touch;
void dm_touch_set(const dm_touch *t, int n);
void dm_touch_script(const dm_touch *t, int n); // scripted touches (DM_KEYS, DM_CTL; hle_hid.c): as dm_touch_set, but never recorded by DM_REC

// Save not written (called by the host's hle_fs.c; the iOS front end provides a strong implementation of the same name that shows the native prompt "Not enough storage. Progress was not saved"; the default weak implementation only logs).
// Timing: when a save write fails (disk full etc.) within one of the game's save transactions (between two Commits), so that this Commit is refused and the previously committed save is unchanged, it is called once per refused transaction;
Expand Down
181 changes: 155 additions & 26 deletions dm/host/hle_hid.c

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dm/host/hle_lifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include <stdatomic.h>
#include <signal.h>
#include <unistd.h>
void hlecount_flush(void);
void hlecount_flush(void); void dm_rec_finish(void);
void dm_host_shutdown(const char *why) {
static _Atomic int once; if (atomic_exchange(&once, 1)) return;
uint64_t t0 = now_ns(); LOG("received %s: graceful exit", why);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1500 * NSEC_PER_MSEC), dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
LOG("[RED] graceful exit not done in 1.5 seconds, exiting at once"); _exit(0); });
hlecount_flush(); // DM_HLECOUNT: _exit does not run atexit, write a copy here
dm_rec_finish(); // DM_REC: the held state and the buffered steps (hle_hid.c)
dm_audio_suspend(1);
dm_gfx_suspend(1);
LOG("graceful exit done (%.3f s)", (now_ns() - t0) / 1e9);
Expand Down
11 changes: 10 additions & 1 deletion dm/host/hle_touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
#include "front.h"
#include <math.h>
#include <pthread.h>
#include <stdatomic.h>
enum { F_EMPTY, F_DOWN, F_LIFTED };
static struct { int st, reported, again; float x, y, ax, ay; } g_f[16]; // again/ax/ay: the same id was pressed again before End went out (the next read reports End first, then the new Start)
static struct { int down, pend; float x, y; } g_gf[16]; // the gesture recognizer's own view of the fingers; pend: pressed since its last sample (a press and lift between two samples still counts once)
static pthread_mutex_t g_tl = PTHREAD_MUTEX_INITIALIZER;
static int64_t g_tsamp; static uint64_t g_tlast_ns;

void dm_touch_set(const dm_touch *t, int n) {
static void touch_apply(const dm_touch *t, int n) {
pthread_mutex_lock(&g_tl);
int seen[16] = {0};
for (int i = 0; i < n; i++) {
Expand All @@ -36,6 +37,14 @@ void dm_touch_set(const dm_touch *t, int n) {
}
pthread_mutex_unlock(&g_tl);
}
// The front ends' touches (the Mac mouse, the iPhone's screen) go through dm_touch_set, which DM_REC sees (hle_hid.c sets the observer); DM_KEYS and DM_CTL touch through
// dm_touch_script, which it does not, so a recording never holds scripted input
void (*_Atomic dm_touch_observer)(const dm_touch *t, int n);
void dm_touch_set(const dm_touch *t, int n) {
touch_apply(t, n);
void (*o)(const dm_touch *, int) = atomic_load_explicit(&dm_touch_observer, memory_order_relaxed); if (o) o(t, n);
}
void dm_touch_script(const dm_touch *t, int n) { touch_apply(t, n); }

static int32_t clampi(float v, int hi) { int32_t r = (int32_t)(v * (float)(hi + 1)); return r < 0 ? 0 : r > hi ? hi : r; }
// Fill one TouchScreenState<max> (out is 0x10 + 0x28 · max bytes; max 1–16); now_ns is used for delta_time. Returns the number of touch points written.
Expand Down
4 changes: 2 additions & 2 deletions dm/host/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ run: $(B)/t_language $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture
$(B)/t_motion_apple
$(B)/t_motion_pro
$(B)/t_motion_pro_mac
$(B)/t_hid
TMPDIR="$(abspath $(B))" $(B)/t_hid
$(B)/t_haptics
$(B)/t_haptics_apple
$(B)/t_nro
Expand Down Expand Up @@ -192,7 +192,7 @@ resolution: $(B)/t_resolution
@for fps in 0 30 60; do for mode in 0 1 unset invalid; do $(B)/t_resolution $$mode $$fps asia || exit 1; done; done
tsan: $(B)/t_audio_spsc_tsan $(B)/t_hid_tsan
$(B)/t_audio_spsc_tsan
$(B)/t_hid_tsan
TMPDIR="$(abspath $(B))" $(B)/t_hid_tsan

MEM_FLAGS = -O1 -Wall -Werror -Wno-unused-function -Imemory_profile -I..
MEM_DEPS = t_svc_memory.c memory_profile/game_profile.h ../hle_svc.c ../hle_guestthread.c ../hle.h ../dm.h
Expand Down
91 changes: 88 additions & 3 deletions dm/host/tests/t_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ static int hid_usleep(useconds_t usec) { (void)usec; if (script_hold) script_hol
int dm_opmode(void) { return g_mode; }
uint64_t guest_ns(void) { return 1000000000ull; }
uint64_t now_ns(void) { return motion_now; }
uint64_t dm_guest_frame(void) { return 0; }
void dm_touch_set(const dm_touch *t, int n) { (void)t; (void)n; }
static uint64_t test_frame; // presented frames (t_rec moves it)
uint64_t dm_guest_frame(void) { return test_frame; }
// The touchscreen as the game would read it: the first finger, and whether one was put down since the last read (a touch's Start is reported once, hle_touch.c)
static int touch_down, touch_new, touch_downs; static float touch_x, touch_y;
static void touch_to(const dm_touch *t, int n) { if (n > 0) { touch_new |= !touch_down; touch_downs += !touch_down; touch_x = t[0].x; touch_y = t[0].y; } touch_down = n > 0; }
void (*_Atomic dm_touch_observer)(const dm_touch *t, int n);
void dm_touch_set(const dm_touch *t, int n) { touch_to(t, n); void (*o)(const dm_touch *, int) = atomic_load(&dm_touch_observer); if (o) o(t, n); }
void dm_touch_script(const dm_touch *t, int n) { touch_to(t, n); }
int touch_fill_n(void *out, int max, uint64_t now) { (void)out; (void)max; (void)now; return 0; }
int gesture_fill(void *out, int n, uint64_t now) { (void)out; (void)n; (void)now; return 0; }
void dm_gfx_shot_at(long frame, const char *path) { (void)frame; (void)path; }
Expand Down Expand Up @@ -237,6 +243,7 @@ static void t_script(void) {
g_mode = 1; dm_pad_players(1);
dm_pad_set(0, 0, 0, 0, 0); buttons_of(0, STYLE_FULLKEY);
dm_input_set(DM_BTN_X, 0, 0, 0, 0);
static key_step one[1]; g_steps = one; g_cap_steps = 0;
g_nsteps = 1; g_steps[0] = (key_step){.btn = DM_BTN_A | DM_BTN_SHAKE, .lx = -0x7000, .hold = 0.15};
script_hold = t_script_hold; keys_main(NULL); script_hold = NULL;
CHECK(buttons_of(0, STYLE_FULLKEY) == (DM_BTN_B | DM_BTN_X), "script release preserves the controller and keyboard buttons");
Expand All @@ -253,7 +260,84 @@ static void t_script(void) {
CHECK(s.buttons == DM_BTN_A && s.lx == 100 && s.ly == -200 && s.rx == 300 && s.ry == -400, "scripted buttons and all four axes reach player 1 in handheld mode");
script_set(0, 0, 0, 0, 0); buttons_of(NPAD_HANDHELD, STYLE_HANDHELD);
g_mode = 1; dm_pad_players(1);
g_nsteps = 0;
g_nsteps = 0; g_steps = NULL;
}

// DM_REC (GitHub issue #313): player 1's physical input written as DM_KEYS steps, read back by the DM_KEYS parser and played on the scripted path; the game's view of every frame
// (buttons with their press latch, the shake, sticks, the touchscreen) is the same in the recording and the replay, to the recorder's stick and touch steps
typedef struct { uint64_t btn; int32_t q[4]; int touch; int32_t tx, ty; } frame_view;
enum { REC_FRAMES = 64, REC_END = 62 };
static frame_view rec_live[REC_FRAMES], rec_replay[REC_FRAMES];
static frame_view game_reads(void) { // what the game reads in this frame (and consumes: the press latches, a new touch)
NpadState s; fill(&s, 0, STYLE_FULLKEY);
frame_view v = {(s.buttons & 0xffff) | ((atomic_load(&g_btn) | atomic_load(&g_sc_btn) | atomic_load(&g_pad_btn[0])) & DM_BTN_SHAKE)};
int32_t a[4] = {s.lx, s.ly, s.rx, s.ry}; for (int i = 0; i < 4; i++) v.q[i] = (int32_t)lrint(a[i] * 100.0 / DM_STICK_MAX);
if ((v.touch = touch_down || touch_new)) { v.tx = (int32_t)lrintf(touch_x * 1000); v.ty = (int32_t)lrintf(touch_y * 1000); }
touch_new = 0;
return v;
}
static void live_to(uint64_t f) { while (test_frame < f) { rec_live[test_frame] = game_reads(); test_frame++; } } // the game reads, then the next frame is presented
static void replay_frame(void) { if (test_frame < REC_FRAMES) rec_replay[test_frame] = game_reads(); test_frame++; } // keys_main waits: a frame goes by
static void t_rec(void) {
g_mode = 1; dm_pad_players(1);
dm_input_set(0, 0, 0, 0, 0); dm_pad_set(0, 0, 0, 0, 0); script_set(0, 0, 0, 0, 0); game_reads();
char path[512]; snprintf(path, sizeof path, "%s/t_hid_rec.keys", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
test_frame = 0;
CHECK(rec_open(path), "DM_REC opens its file");
live_to(10); dm_input_set(DM_BTN_A, 0, 0, 0, 0); // the keyboard's A
live_to(12); dm_pad_set(0, 16384, -8192, 0, 0); // the controller's left stick half right, a quarter down
live_to(13); dm_pad_set(0, 16400, -8200, 0, 0); // drift within a step: no new step
live_to(15); dm_input_set(0, 0, 0, 0, 0);
live_to(20); dm_pad_set(0, 0, 0, 0, 0);
live_to(22); dm_input_set(DM_BTN_B, 0, 0, 0, 0); dm_input_set(0, 0, 0, 0, 0); // B tapped between two reads: kept (~f0), the latch hands it over once
dm_pad_set(0, 0, 0, DM_STICK_MAX, 0); dm_pad_set(0, 0, 0, 0, 0); // a stick flicked between two reads: the game cannot see it, not recorded
live_to(30); dm_touch_set(&(dm_touch){0, .25f, .75f}, 1); // the mouse on the touchscreen, dragged, lifted
live_to(31); dm_touch_set(&(dm_touch){0, .3f, .75f}, 1);
live_to(32); dm_touch_set(&(dm_touch){0, .302f, .75f}, 1); // moved within a step
live_to(33); dm_touch_set(NULL, 0);
live_to(40); dm_input_set(DM_BTN_ZR | DM_BTN_SHAKE, 0, 0, 0, 0);
live_to(41); script_set(DM_BTN_X, 0, 0, 0, 0); dm_touch_script(&(dm_touch){0, .5f, .5f}, 1); // scripted input (DM_KEYS, DM_CTL) is never recorded
game_reads(); script_set(0, 0, 0, 0, 0); dm_touch_script(NULL, 0);
live_to(45); dm_input_set(0, 0, 0, 0, 0);
live_to(60); dm_input_set(DM_BTN_Y, 0, 0, 0, 0); // still held at exit: ends at the exit's frame
live_to(REC_END); dm_rec_finish();
dm_input_set(0, 0, 0, 0, 0); game_reads();
atomic_store(&g_rec_on, 0); atomic_store(&dm_touch_observer, NULL); fclose(g_rec_file); g_rec_file = NULL;

char text[1024] = {0}; FILE *f = fopen(path, "r"); size_t n = f ? fread(text, 1, sizeof text - 1, f) : 0; if (f) fclose(f); text[n] = 0; unlink(path);
const char *want = "f10:A~f2,\nf12:A+L0.50/-0.25~f3,\nf15:L0.50/-0.25~f5,\nf22:B~f0,\nf30:T0.250x0.750~f1,\nf31:T0.300x0.750~f2,\nf40:ZR+Shake~f5,\nf60:Y~f2,\n";
CHECK(!strcmp(text, want), "DM_REC writes the expected DM_KEYS steps");
if (strcmp(text, want)) fprintf(stderr, "recorded:\n%s", text);

keys_parse(text);
CHECK(g_nsteps == 8, "the DM_KEYS parser reads every recorded step");
if (g_nsteps == 8) {
CHECK(g_steps[1].frame && g_steps[1].at == 12 && g_steps[1].fhold && g_steps[1].hold == 3 && g_steps[1].btn == DM_BTN_A && g_steps[1].lx == 16384 && g_steps[1].ly == -8192,
"a recorded step reads back as its frame, frame hold, buttons and stick");
CHECK(g_steps[4].tap && g_steps[4].tx == .25f && g_steps[4].ty == .75f && g_steps[6].btn == (DM_BTN_ZR | DM_BTN_SHAKE), "touches and the shake read back");
}
touch_downs = 0; test_frame = 0; script_hold = replay_frame; keys_main(NULL); script_hold = NULL;
CHECK(touch_downs == 1, "a recorded drag replays as one finger that moves, not as taps");
CHECK(test_frame == REC_END, "the replay ends at the frame the recording ended");
int bad = -1;
for (int i = 0; i < REC_END && bad < 0; i++) {
const frame_view *a = &rec_live[i], *b = &rec_replay[i]; int same = a->btn == b->btn && a->touch == b->touch;
for (int k = 0; k < 4; k++) same &= abs(a->q[k] - b->q[k]) < REC_STICK_STEP;
if (a->touch) same &= abs(a->tx - b->tx) < REC_TOUCH_STEP && abs(a->ty - b->ty) < REC_TOUCH_STEP;
if (!same) bad = i;
}
CHECK(bad < 0, "the replay gives the game the recorded input in every frame");
if (bad >= 0) fprintf(stderr, "frame %d: recorded %#llx L%d/%d touch %d, replayed %#llx L%d/%d touch %d\n", bad, (unsigned long long)rec_live[bad].btn, rec_live[bad].q[0], rec_live[bad].q[1],
rec_live[bad].touch, (unsigned long long)rec_replay[bad].btn, rec_replay[bad].q[0], rec_replay[bad].q[1], rec_replay[bad].touch);
CHECK(rec_live[22].btn == DM_BTN_B && rec_replay[22].btn == DM_BTN_B, "a tap between two reads reaches the game once, live and replayed");
CHECK(!atomic_load(&g_sc_btn) && !touch_down, "the replay releases everything at its end");
free(g_steps); g_steps = NULL; g_nsteps = g_cap_steps = 0;
// With the step limit raised for long recordings the steps are ordered by a merge sort: repeated groups still interleave, and steps at one time keep their listed order
keys_parse("290:A*3/3,291:Left*3/3 291.5:B*3/3\n300:X,300:Y");
static const uint64_t order[11] = {DM_BTN_A, DM_BTN_LEFT, DM_BTN_B, DM_BTN_A, DM_BTN_LEFT, DM_BTN_B, DM_BTN_A, DM_BTN_LEFT, DM_BTN_B, DM_BTN_X, DM_BTN_Y};
int ordered = g_nsteps == 11; for (int i = 0; ordered && i < 11; i++) ordered = g_steps[i].btn == order[i];
CHECK(ordered, "repeats interleave in time order and equal times keep their listed order; spaces and newlines separate steps");
free(g_steps); g_steps = NULL; g_nsteps = g_cap_steps = 0;
}

// Live controller motion (dm_motion_set): player 1's six-axis sensor reports the controller's tilt and rotation instead of lying flat, and goes back to flat when cleared
Expand Down Expand Up @@ -376,6 +460,7 @@ int main(void) {
t_motion_threads();
t_motion_live();
t_motion_hid();
t_rec();
printf("hid players: %d checks, %d failed\n", checks, fails);
return fails != 0;
}
Loading