Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: v0lumehi <[email protected]>
Date: Wed, 28 Oct 2026 13:33:56 +0100
Subject: [PATCH] ref: hle_hid_d2r.c
Reference only; not built.
Upstream-Status: fork-only
Reason: Reference copy of the d2r-mobile HID layer
Issue: LNPANDY/NX2Apple#123
---
ref/hle_hid_d2r.c | 414 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 414 insertions(+)
create mode 100644 ref/hle_hid_d2r.c
diff --git a/ref/hle_hid_d2r.c b/ref/hle_hid_d2r.c
new file mode 100644
index 0000000..584d867
--- /dev/null
+++ b/ref/hle_hid_d2r.c
@@ -0,0 +1,414 @@
+// nn::hid (controller / touchscreen): on the real device shared memory is read through the hid service; the host takes it from the current dm_input state, written by the front end (Mac keyboard / iOS touchscreen).
+// The connected controller follows the operation mode: DM_OPMODE=1 (console, default) → 1P full-key controller; 0 (handheld) → handheld controller. Vibration is not supported (device count 0).
+#include "hle.h"
+#include "dm_uiscreen.h"
+#include "dm_actions.h"
+#include "dm_cursor_item.h"
+#include <unistd.h>
+#include "dm_input.h"
+#include "front.h"
+#include <stdatomic.h>
+#include "dm_cursor.h"
+#include <math.h>
+_Atomic int g_dm_l_presses;
+
+enum { NPAD_HANDHELD = 0x20 };
+enum { STYLE_FULLKEY = 1u << 0, STYLE_HANDHELD = 1u << 1 };
+enum { ATTR_CONNECTED = 1u << 0 };
+
+// nn::hid::NpadFullKeyState / NpadHandheldState have the same layout
+typedef struct {
+ int64_t sampling;
+ uint64_t buttons;
+ int32_t lx, ly, rx, ry;
+ uint32_t attributes;
+ uint32_t _pad;
+} NpadState;
+_Static_assert(sizeof(NpadState) == 0x28, "NpadState");
+
+static struct { _Atomic uint64_t buttons; _Atomic int32_t lx, ly, rx, ry; } g_in;
+static _Atomic int64_t g_sampling;
+static _Atomic uint64_t g_sampled_right;
+void dm_input_sampled_right(int32_t *rx,int32_t *ry) {
+ uint64_t v=atomic_load_explicit(&g_sampled_right,memory_order_acquire);
+ *rx=(int32_t)(uint32_t)v;*ry=(int32_t)(uint32_t)(v>>32);
+}
+
+// Test switch DM_TEST_HID_STALL_MS=<milliseconds>: each time a press edge of the A published by the front end appears, the game's next controller read first sleeps this long before reading -- simulates "the game stalls at the moment of the tap".
+// After opening the function page the game did not read input for about 0.4 s, and a tap pulse of 0.12 s wall clock was lost entirely (tapped, no response); the front end now ends the pulse by dm_input_samples
+static int g_stall_ms = -1; static _Atomic int g_stall_due;
+// Press-edge latch: when a key published by the front end goes from "not pressed" to "pressed" it is recorded in g_press_latch, delivered along with the game's next controller read and cleared once delivered.
+// When press and release fell between two controller reads of the game (two steps sent back to back when the script lags, or the game stalling input reads while loading / opening a panel) the whole key press used to be lost -- real device rf1, restarts 2 and 3:
+// the bottom bar menu key had only 0.09 ms from press to release, the game did not see it and the function page did not open. The latch only gives one extra sample, afterwards the front end's current value applies (released is released)
+static _Atomic uint64_t g_press_latch;
+// Controller (dm_input.h dm_pad_set): buttons and the four axes are packed into two atomics, and press edges are likewise latched until the game's next
+// read (short controller presses are not lost)
+static _Atomic uint64_t g_pad_btn, g_pad_axes, g_pad_latch;
+void dm_pad_set(uint64_t buttons, int32_t lx, int32_t ly, int32_t rx, int32_t ry) {
+ uint64_t old = atomic_exchange(&g_pad_btn, buttons); atomic_fetch_or(&g_pad_latch, buttons & ~old);
+ atomic_store(&g_pad_axes, (uint64_t)(uint16_t)lx | (uint64_t)(uint16_t)ly << 16 | (uint64_t)(uint16_t)rx << 32 | (uint64_t)(uint16_t)ry << 48);
+}
+void dm_input_set(uint64_t buttons, int32_t lx, int32_t ly, int32_t rx, int32_t ry) {
+ if (g_stall_ms < 0) { const char *e = getenv("DM_TEST_HID_STALL_MS"); g_stall_ms = e ? atoi(e) : 0; if (g_stall_ms) LOG("DM_TEST_HID_STALL_MS: after each A press edge the game's controller read first sleeps %d ms", g_stall_ms); }
+ if (g_stall_ms > 0 && (buttons & DM_BTN_A) && !(g_in.buttons & DM_BTN_A)) atomic_store(&g_stall_due, 1);
+ atomic_fetch_or(&g_press_latch, buttons & ~g_in.buttons); // record newly pressed keys, guaranteeing the game reads them at least once (see fill)
+ g_in.buttons = buttons; g_in.lx = lx; g_in.ly = ly; g_in.rx = rx; g_in.ry = ry;
+}
+// Test switch DM_UI_TAPS="frame:x,y[:action];..." (x / y = per-mille of the picture). Default action = one tap (via dm_ui_touch: press, lift 6 frames later); the others go through dm_ui_ptr (for probing / UI cases):
+// k tap a tab / dialog menu item (the host presses keys to switch to it); t<frames> finger held this many frames then lifted (via dm_ui_touch); h<frames> hover; p<frames> left button held this many frames then released; r right button; l left button (pointer path); d<x2>,<y2> hold and drag to another point (done in 20 frames); g<x2>,<y2> continuous gesture (dm_ui_drag_*: typed as scroll / slider by the start-to-end direction, done in 60 frames); W tap the game world (dm_world_tap); N<value> set the quantity box value (dm_num_set); A<number> buy/sell / stat point entries (dm_actions.c); w<amount> wheel (120 per notch, negative = down); m<0|1> map back to minimap / to full screen (dm_ui_map_full); prefix s / c = holding Shift / Ctrl (VK 0x10 / 0x11)
+static void ui_taps_step(void) {
+ static struct { uint64_t fr; float x, y, x2, y2; int st, kind, arg, shift; } tp[128]; static int n = -1;
+ if (n < 0) { n = 0; const char *e = getenv("DM_UI_TAPS"); char buf[4096]; if (e) { strlcpy(buf, e, sizeof buf); for (char *t = strtok(buf, ";"); t && n < 128; t = strtok(NULL, ";")) {
+ unsigned long long f; int x, y, c = 0; if (sscanf(t, "%llu:%d,%d%n", &f, &x, &y, &c) < 3) continue;
+ tp[n].fr = f; tp[n].x = x / 1000.0f; tp[n].y = y / 1000.0f; tp[n].kind = 0; const char *a = t + c;
+ if (*a == ':') { a++; if (*a == 's' || *a == 'c') { tp[n].shift = *a == 's' ? 0x10 : 0x11; a++; } tp[n].kind = *a; int p = 0, q = 0;
+ if ((*a == 'd' || *a == 'g') && sscanf(a + 1, "%d,%d", &p, &q) == 2) { tp[n].x2 = p / 1000.0f; tp[n].y2 = q / 1000.0f; } else tp[n].arg = atoi(a + 1); }
+ n++; } LOG("DM_UI_TAPS: %d entries", n); } }
+ uint64_t fr = dm_guest_frame();
+ static int rel = -1; if (rel < 0) rel = getenv("DM_UI_TAPS_AT") != NULL;
+ if (rel) { uint64_t b = dm_ui_taps_base(); if (!b || fr < b) return; fr -= b; }
+ for (int i = 0; i < n; i++) { if (fr < tp[i].fr || tp[i].st < 0) continue; int d = (int)(fr - tp[i].fr), k = tp[i].kind; float x = tp[i].x, y = k == 'W' ? tp[i].y : dm_ui_ny(tp[i].y); // script coordinates are per-mille of the real picture; world taps use them as is, UI taps are converted into the UI rectangle
+ if (!k || k == 't') { int hold = k ? tp[i].arg : 6; if (tp[i].st == 0) { tp[i].st = 1; dm_ui_touch(0, x, y); } else if (tp[i].st == 1 && d >= hold) { tp[i].st = -1; dm_ui_touch(2, x, y); } continue; }
+ if (k == 'N') { void dm_num_set(int); dm_num_set(tp[i].arg); tp[i].st = -1; continue; } // set the quantity box value (dm_textinput.c; -1 all, -2 half)
+ if (k == 'A') { void dm_shop_buy(void), dm_shop_sell_cursor(void), dm_stat_add(int, int), dm_skill_add_selected(void); int v = tp[i].arg; // action entries (dm_actions.c): A1 buy, A2 sell what is on the cursor, A3 skill +1, A4 pick up the first sellable item in the inventory (for testing), A5 put the first potion in the inventory into the belt (for testing), A6 directly sell the first sellable item carried (for testing), A21-A24 immediately use the first potion / scroll / tome / cube in the inventory (for testing, dm_item_use), A1000+id put that item into the belt, A1xy add y (1 / 5 / 9 = all) to stat x (0–3)
+ if (v == 1) dm_shop_buy(); else if (v == 2) dm_shop_sell_cursor(); else if (v == 3) dm_skill_add_selected(); else if (v == 4) { void dm_test_hold_first_sellable(void); dm_test_hold_first_sellable(); } else if (v == 6) { void dm_test_sell_first(void); dm_test_sell_first(); } else if (v == 5) { void dm_test_belt_first_potion(void); dm_test_belt_first_potion(); } else if (v == 35 || v == 38 || v == 39) { void dm_inv_sort(int); dm_inv_sort(v == 35 ? 0 : v == 38 ? 3 : 4); } else if (v == 36) { void dm_name_random(void); dm_name_random(); } else if (v >= 44 && v <= 46) { void dm_test_identify(int); dm_test_identify(v - 43); } else if (v == 74) { void dm_held_stow(void); dm_held_stow(); } else if (v == 75) { void dm_test_swap_held(void); dm_test_swap_held(); } else if (v >= 70 && v <= 73) { dm_uiscreen_enable(v - 70); } /* A70-A73: UI picture off / top-aligned / centered / bottom-aligned (through the same entry the front end uses) */ else if (v == 47 || v == 48) { dm_stash_take(v == 47 ? 1 : 3); } else if (v == 43) { dm_cursor_item_info ci; int hw = 0, hh = 0, c = dm_cursor_item(&ci), hd = dm_held_size(&hw, &hh); LOG("test: cursor item %s container %d cell %d,%d size %dx%d stack %d; held %d (%dx%d); awaiting target %d", c ? ci.code : "none", c ? ci.container : 0, c ? ci.gx : 0, c ? ci.gy : 0, c ? ci.gw : 0, c ? ci.gh : 0, c ? ci.stack : 0, hd, hw, hh, dm_item_targeting()); } else if (v >= 50 && v <= 69) { void dm_test_stash_take(int); dm_test_stash_take(v - 50 ? v - 50 : 1); } else if (v >= 40 && v <= 42) { void dm_test_equip(int); dm_test_equip(v - 39); } else if (v == 33 || v == 34) { void dm_belt_expand(int); dm_belt_expand(v == 33); } else if (v == 31 || v == 32) { void dm_item_compare(int); dm_item_compare(v == 31); } else if (v == 30) { void dm_test_hover_at(float, float); dm_test_hover_at(x, y); } else if (v >= 21 && v <= 26) { void dm_test_use_first(int); dm_test_use_first(v - 20); } else if (v >= 1000) { void dm_belt_put(uint32_t); dm_belt_put((uint32_t)(v - 1000)); } else if (v >= 100) dm_stat_add(v / 10 % 10, v % 10 == 9 ? -1 : v % 10); tp[i].st = -1; continue; }
+ if (k == 'W') { unsigned dm_world_tap(float, float); dm_world_tap(x, y); tp[i].st = -1; continue; } // tap the game world (dm_world.c: tapping a target = interact)
+ if (k == 'g') { float t = d / 60.0f; if (t > 1) t = 1;
+ if (tp[i].st == 0) { tp[i].st = 1; int hz = fabsf(tp[i].x2 - x) * 16 > fabsf(tp[i].y2 - y) * 9; dm_ui_drag_begin(hz ? 2 : 1, x, y, tp[i].arg ? tp[i].arg / 1000.0f : 0.05f); }
+ dm_ui_drag_move(x + (tp[i].x2 - x) * t, y + (tp[i].y2 - y) * t); if (d >= 62) { dm_ui_drag_end(); tp[i].st = -1; } continue; }
+ if (k == 'k') { static unsigned ks[128]; if (tp[i].st == 0) { tp[i].st = 1; ks[i] = dm_ui_item_query(x, y); } else if (d >= 3) { tp[i].st = -1; if (dm_ui_item_result(ks[i]) == 2) dm_ui_tab_go(); } continue; } // k = tap once the "tab / dialog menu" way (query the hit → if yes, let the host press the key)
+ if (tp[i].st == 0) { tp[i].st = 1; dm_ui_item_query(x, y); dm_ui_ptr(DM_UP_HOVER, x, y, 0); if (tp[i].shift) dm_ui_ptr(DM_UP_KEYDOWN, x, y, tp[i].shift); continue; }
+ int done = 0;
+ if (k == 'h') done = d >= tp[i].arg;
+ else if (k == 'r' || k == 'l') { if (tp[i].st == 1 && d >= 4) { tp[i].st = 2; dm_ui_ptr(k == 'r' ? DM_UP_RDOWN : DM_UP_LDOWN, x, y, 0); dm_ui_ptr(k == 'r' ? DM_UP_RUP : DM_UP_LUP, x, y, 0); } done = d >= 10; }
+ else if (k == 'p') { if (tp[i].st == 1 && d >= 4) { tp[i].st = 2; dm_ui_ptr(DM_UP_LDOWN, x, y, 0); } else if (tp[i].st == 2 && d >= 4 + tp[i].arg) { tp[i].st = 3; dm_ui_ptr(DM_UP_LUP, x, y, 0); } done = d >= 10 + tp[i].arg; }
+ else if (k == 'm') { if (tp[i].st == 1) { tp[i].st = 2; dm_ui_map_full(tp[i].arg); } done = d >= 4; }
+ else if (k == 'w') { if (tp[i].st == 1 && d >= 4) { tp[i].st = 2; dm_ui_ptr(DM_UP_SCROLL, x, y, tp[i].arg); } done = d >= 8; }
+ else if (k == 'd') { if (tp[i].st == 1 && d >= 4) { tp[i].st = 2; dm_ui_ptr(DM_UP_LDOWN, x, y, 0); }
+ else if (tp[i].st == 2 && d >= 6) { float t = (d - 6) / 20.0f; if (t > 1) t = 1; dm_ui_ptr(DM_UP_MOVE, x + (tp[i].x2 - x) * t, y + (tp[i].y2 - y) * t, 0); if (d >= 30) { tp[i].st = 3; dm_ui_ptr(DM_UP_LUP, 0, 0, 0); } }
+ done = d >= 36; }
+ else done = 1;
+ if (done) { tp[i].st = -1; if (tp[i].shift) dm_ui_ptr(DM_UP_KEYUP, x, y, tp[i].shift); dm_ui_ptr(DM_UP_END, x, y, 0); } }
+}
+static _Atomic uint64_t g_stick_ns;
+uint64_t dm_stick_last_ns(void) { return atomic_load(&g_stick_ns); } // criterion for "click-to-move packets may only come from the stick" (diag.c c2s_deliver)
+uint64_t dm_input_samples(void) { return (uint64_t)atomic_load(&g_sampling); } // cumulative number of times the game has read the controller (the front end uses it to judge whether a key press has been seen by the game)
+
+static uint32_t connected_id(void) { return dm_opmode() == 0 ? NPAD_HANDHELD : 0; } // follows the operation mode (hle_oe.c is the single source)
+static uint32_t style_of(uint32_t id) {
+ if (id != connected_id()) return 0;
+ return id == NPAD_HANDHELD ? STYLE_HANDHELD : STYLE_FULLKEY;
+}
+// Scripted input (can inject without opening a window), two kinds of comma-separated items:
+// By time: key@seconds[+hold seconds] DM_KEYS="A@8,A@12+0.5" = press A at second 8 (held 0.2 s by default), press A for 0.5 s at second 12; time counts from host initialization
+// By UI message: key@pre[%delay seconds][+hold seconds][>confirm] DM_KEYS="A@OpenPanel:MainMenuButtonRibbon>OpenPanel:CharacterDifficultyModal,A@OpenPanel:CharacterDifficultyModal>FrontEndNavigation:ToGame"
+// Same rules as dm/verify/ryu/drive.py: message string = "category:action:panel name", pre / confirm match by substring; steps run in order.
+// The pre has appeared after the previous step was pressed (first step: since start) and the previous key press is idle (see below) → press for 3 frames (50 ms;
+// "+hold seconds" changes the hold duration, e.g. LSL@UnloadPanel:LoadScreenPanel+0.9 = push the left stick for 0.9 s as soon as the loading screen ends, so a key pressed by host time does not land before the run is entered);
+// only re-press if no confirm arrives within 150 server ticks, after 3 times without one report a failure and abort; the 60 s wall clock is only a fallback and reports a failure; a step with no confirm item is done once pressed.
+// An empty pre (e.g. LSU@%0.2+0.35) = press after a delay right after the previous step completes (confirm arrived / released if no confirm item).
+// Idle = the previous key press has been released, and the last InputMessage:ControllerActionBegin during the hold has got its ControllerActionEnd (Begin is only sent when a panel consumes
+// input: in the wild1 ground truth, pressing A in a run to open the waypoint has no Begin / End before or after, they only appear in pairs once a panel takes over; no Begin during the hold ⇒ idle on release.
+// Stick pseudo-keys follow the same rule). The former "must see End" never arrived after in-run key presses.
+// Key name = DM_BTN_* from dm_input.h without the prefix, plus the left stick pseudo-keys LSL / LSR / LSU / LSD (pushed to full while held). ORed bitwise with the keys written by the front end.
+// Touch item T<x>:<y> (only for steps driven by UI messages): while held, one finger (id 0, via dm_touch_set) is put down at normalized rendered-picture coordinates (x, y), lifted on release / confirm.
+#define K(n) {DM_BTN_##n, #n} // key names are generated directly from DM_BTN_* in dm_input.h, the bit order is defined in one place only
+static const struct { uint32_t mask; const char *name; } k_names[] = {
+ K(A), K(B), K(X), K(Y), K(LS), K(RS), K(L), K(R), K(ZL), K(ZR), K(PLUS), K(MINUS), K(LEFT), K(UP), K(RIGHT), K(DOWN)};
+#undef K
+// Left stick pseudo-keys (used only in scripts): while held the stick is pushed fully in that direction; the bits lie outside DM_BTN_* and do not enter the button bits
+enum { PK_LSL = 1u << 24, PK_LSR = 1u << 25, PK_LSU = 1u << 26, PK_LSD = 1u << 27, PK_MASK = 0xFu << 24, PK_TOUCH = 1u << 28, PK_MSG = 1u << 29, PK_CLICK = 1u << 30 }; // PK_CLICK: UI click step (C<x>:<y>, via dm_ui_touch = the same injected mouse message path as a real finger on the iOS front end) // PK_TOUCH: touch point step (T<x>:<y>, via dm_touch_set)
+static const struct { uint32_t mask; const char *name; } k_stick[] = {{PK_LSL, "LSL"}, {PK_LSR, "LSR"}, {PK_LSU, "LSU"}, {PK_LSD, "LSD"}};
+#define KMAX 256
+static struct { uint32_t mask; uint64_t t0, t1; } g_keys[KMAX];
+static int g_nkeys;
+static uint64_t g_kbase;
+// Steps driven by UI messages (state is driven from two sides, the UI message callback and the controller read, protected by g_klock)
+enum { KS_PRE, KS_HOLD, KS_OK, KS_DONE };
+static struct { uint32_t mask; char pre[96], ok[96]; int seen_pre, seen_server, hold_override; double delay_seconds, hold_seconds; uint64_t seen_clock, seen_at; float tx, ty; char msg[96]; } g_steps[KMAX]; // in a run, by server ticks; before entering the game, by presented frames
+static int g_nsteps, g_kcur, g_kstate, g_ktries, g_krel = 1, g_kbeg, g_kend; // beg/end are 0/1 markers of the action lifecycle, not message counts; idle = released and the last Begin has got its End
+static uint64_t g_kpress, g_kpress_clock; static int g_kpress_server;
+static pthread_mutex_t g_klock = PTHREAD_MUTEX_INITIALIZER;
+static struct { char msg[160]; int32_t x, y; } g_cursor_tests[KMAX];
+static int g_ncursor_tests, g_cursor_cur;
+#define SERVER_TICKS_PER_SECOND 25ull
+#define PRODUCT_FRAMES_PER_SECOND 60ull
+#define ACTION_HOLD_TICKS 2ull // action keys 2 ticks (about 80 ms, spanning at least one full tick): held too long, minus is taken as a repeated press and the automap switches to the full-screen mode (measured: automap red at 15 ticks)
+#define DIRECTION_HOLD_TICKS 3ull // direction keys at least 3 ticks: three consecutive samples are enough to catch it reliably
+#define OK_WAIT_TICKS 150ull
+#define WALL_FALLBACK_NS 60000000000ull
+
+typedef struct { uint64_t value; int server; } script_clock;
+static script_clock script_clock_now(void) {
+ uint64_t tick = dm_server_tick(); { extern _Atomic int g_dm_frozen; if (atomic_load(&g_dm_frozen)) tick = UINT64_MAX; } // while the world is frozen (panel freeze, diag.c) the tick number does not advance, the script times by presented frames instead, otherwise delays and hold durations would never be reached
+ return tick == UINT64_MAX ? (script_clock){dm_guest_frame(), 0} : (script_clock){tick, 1};
+}
+static uint64_t script_clock_elapsed(script_clock now, uint64_t start, int start_server) {
+ return now.server == start_server && now.value >= start ? now.value - start : 0;
+}
+static uint64_t script_seconds_to_units(double seconds, int server) {
+ return (uint64_t)(seconds * (server ? SERVER_TICKS_PER_SECOND : PRODUCT_FRAMES_PER_SECOND));
+}
+static uint64_t step_hold_units(const typeof(g_steps[0]) *st, int server) {
+ if (st->hold_override) return script_seconds_to_units(st->hold_seconds, server);
+ return (st->mask & (DM_BTN_UP | DM_BTN_DOWN | DM_BTN_LEFT | DM_BTN_RIGHT | PK_MASK)) ? DIRECTION_HOLD_TICKS : ACTION_HOLD_TICKS;
+}
+
+static void cursor_tests_parse(void) {
+ g_ncursor_tests = g_cursor_cur = 0;
+ const char *e = getenv("DM_TEST_CURSOR"); if (!e || !*e) return;
+ char buf[4096]; strncpy(buf, e, sizeof buf - 1); buf[sizeof buf - 1] = 0;
+ for (char *save, *tok = strtok_r(buf, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) {
+ char *colon = strrchr(tok, ':'); int x, y; char extra;
+ if (!colon || sscanf(colon + 1, "%d,%d%c", &x, &y, &extra) != 2 || g_ncursor_tests == KMAX)
+ die("DM_TEST_CURSOR entry '%s' format: UI-message-trigger:x,y", tok);
+ *colon = 0;
+ if (!*tok) die("DM_TEST_CURSOR entry has an empty trigger string");
+ strncpy(g_cursor_tests[g_ncursor_tests].msg, tok, sizeof g_cursor_tests[0].msg - 1);
+ g_cursor_tests[g_ncursor_tests].msg[sizeof g_cursor_tests[0].msg - 1] = 0;
+ g_cursor_tests[g_ncursor_tests].x = (int32_t)x;
+ g_cursor_tests[g_ncursor_tests].y = (int32_t)y;
+ g_ncursor_tests++;
+ }
+}
+
+static void step_chain(void) { // holds g_klock: the current step's pre is left empty ⇒ counts as seen as soon as the previous step completes
+ if (g_kcur < g_nsteps && !g_steps[g_kcur].pre[0] && !g_steps[g_kcur].seen_pre) { script_clock c = script_clock_now(); g_steps[g_kcur].seen_pre = 1; g_steps[g_kcur].seen_at = now_ns(); g_steps[g_kcur].seen_clock = c.value; g_steps[g_kcur].seen_server = c.server; }
+}
+static void keys_on_uimsg(const char *cat, const char *act, const char *arg) {
+ char m[256]; snprintf(m, sizeof m, "%s:%s:%s", cat, act, arg);
+ pthread_mutex_lock(&g_klock);
+ if (g_cursor_cur < g_ncursor_tests && strstr(m, g_cursor_tests[g_cursor_cur].msg)) {
+ int i = g_cursor_cur++;
+ dm_cursor_set(g_cursor_tests[i].x, g_cursor_tests[i].y);
+ }
+ // Repeat triggers of a long press/stick send a Begin each time, release sends only one End (observed on the iOS host: 73 Begin/1 End).
+ // A new Begin marks the action as still busy and cancels the old End; repeated Begins do not increase the wait count, only End marks it finished.
+ if (strstr(m, "InputMessage:ControllerActionBegin") && !g_krel) { g_kbeg = 1; g_kend = 0; }
+ if (strstr(m, "InputMessage:ControllerActionEnd")) g_kend = 1;
+ if (g_kcur < g_nsteps) {
+ if (g_kstate != KS_PRE && g_steps[g_kcur].ok[0] && strstr(m, g_steps[g_kcur].ok)) {
+ LOG("hid script step %d confirmed '%s' (press %d, waited %llu clock units, wall clock %llu ms)", g_kcur + 1, m, g_ktries,
+ (unsigned long long)(script_clock_now().value - g_kpress_clock), (unsigned long long)((now_ns() - g_kpress) / 1000000ull));
+ if (g_kstate == KS_HOLD && (g_steps[g_kcur].mask & PK_TOUCH)) dm_touch_set(NULL, 0);
+ if (g_kstate == KS_HOLD && (g_steps[g_kcur].mask & PK_CLICK)) dm_ui_touch(2, g_steps[g_kcur].tx, g_steps[g_kcur].ty); // touch step confirmed while held: lift the finger
+ g_krel = 1; // confirmed while still held (e.g. opening a panel): the state leaves KS_HOLD and no more key is output ⇒ released at this moment (wild1 step 3 used to get stuck here)
+ g_kcur++; g_kstate = KS_PRE; g_ktries = 0; step_chain();
+ }
+ // Pre: checked while the current step is waiting for its pre; when the current step is already pressed, the next step's is checked (only messages appearing after the previous step was pressed count)
+ int j = g_kstate == KS_PRE ? g_kcur : g_kcur + 1;
+ if (j < g_nsteps && !g_steps[j].seen_pre && g_steps[j].pre[0] && strstr(m, g_steps[j].pre)) { script_clock c = script_clock_now(); g_steps[j].seen_pre = 1; g_steps[j].seen_at = now_ns(); g_steps[j].seen_clock = c.value; g_steps[j].seen_server = c.server; }
+ }
+ pthread_mutex_unlock(&g_klock);
+}
+static uint32_t steps_now(void) {
+ if (!g_nsteps) return 0;
+ uint64_t t = now_ns(); uint32_t m = 0;
+ pthread_mutex_lock(&g_klock);
+ if (g_kcur < g_nsteps) {
+ typeof(g_steps[0]) *st = &g_steps[g_kcur];
+ script_clock c = script_clock_now();
+ // Clock source switch (main menu frames ↔ in-run ticks ↔ frozen-period frames): the start point is recounted from the new source, otherwise the elapsed time across sources is always 0 and the wait never ends
+ if (st->seen_pre && c.server != st->seen_server) { st->seen_clock = c.value; st->seen_server = c.server; }
+ if (g_kstate != KS_PRE && c.server != g_kpress_server) { g_kpress_clock = c.value; g_kpress_server = c.server; }
+ uint64_t elapsed = script_clock_elapsed(c, st->seen_clock, st->seen_server);
+ int dm_loadscreen_up(void); void dm_hold_for_loadscreen(void);
+ if (g_kstate == KS_PRE && st->seen_pre && elapsed >= script_seconds_to_units(st->delay_seconds, c.server) && dm_loadscreen_up()) dm_hold_for_loadscreen(); // due (pre seen and delay elapsed) but the loading screen is still up: freeze the tick, do not press yet (diag.c). Not frozen while the delay has not elapsed: after a waypoint act change a step due to press 62 seconds later was queued, and as soon as the loading screen opened the clock was frozen before the server handled the teleport, loading waited for the server, the server waited for the clock, deadlock
+ else if (g_kstate == KS_PRE && st->seen_pre && g_krel && (g_kend >= g_kbeg || (st->mask & (PK_MSG | PK_CLICK))) && elapsed >= script_seconds_to_units(st->delay_seconds, c.server)) { // a message step presses no key, so it need not wait for the previous key's controller action to wrap up (a Begin re-triggered while the previous step is held sometimes never gets its End, 1 of 3 runs of the Mac iOS app got stuck here)
+ g_kstate = KS_HOLD; g_kpress = t; g_kpress_clock = c.value; g_kpress_server = c.server; g_krel = 0; g_kbeg = g_kend = 0; g_ktries = 1;
+ if (st->mask & PK_TOUCH) { dm_touch p = {0, st->tx, st->ty}; dm_touch_set(&p, 1); }
+ if (st->mask & PK_MSG) dm_ui_send_queue(st->msg);
+ if (st->mask & PK_CLICK) dm_ui_touch(0, st->tx, st->ty);
+ LOG("hid script step %d pressed mask=%#x (pre '%s')", g_kcur + 1, st->mask, st->pre);
+ } else if (g_kstate == KS_OK && t - g_kpress >= WALL_FALLBACK_NS) {
+ die("[RED] hid script step %d: wall-clock fallback timeout (waited %llu server ticks, wall clock %llu ms, confirm '%s')", g_kcur + 1,
+ (unsigned long long)script_clock_elapsed(c, g_kpress_clock, g_kpress_server), (unsigned long long)((t - g_kpress) / 1000000ull), st->ok);
+ } else if (g_kstate == KS_OK && script_clock_elapsed(c, g_kpress_clock, g_kpress_server) >= OK_WAIT_TICKS) {
+ if (g_ktries >= 3) die("[RED] hid script step %d: no confirm '%s' after 3 presses (waited %llu server ticks, wall clock %llu ms)", g_kcur + 1, st->ok,
+ (unsigned long long)script_clock_elapsed(c, g_kpress_clock, g_kpress_server), (unsigned long long)((t - g_kpress) / 1000000ull));
+ LOG("hid script step %d waiting for confirm: %llu server ticks, wall clock %llu ms; pressing again (press %d)", g_kcur + 1,
+ (unsigned long long)script_clock_elapsed(c, g_kpress_clock, g_kpress_server), (unsigned long long)((t - g_kpress) / 1000000ull), g_ktries + 1);
+ g_kstate = KS_HOLD; g_kpress = t; g_kpress_clock = c.value; g_kpress_server = c.server; g_krel = 0; g_kbeg = g_kend = 0; g_ktries++;
+ if (st->mask & PK_TOUCH) { dm_touch p = {0, st->tx, st->ty}; dm_touch_set(&p, 1); }
+ if (st->mask & PK_MSG) dm_ui_send_queue(st->msg);
+ if (st->mask & PK_CLICK) dm_ui_touch(0, st->tx, st->ty);
+ LOG("hid script step %d pressed again (press %d)", g_kcur + 1, g_ktries);
+ }
+ if (g_kstate == KS_HOLD) {
+ uint64_t held = script_clock_elapsed(c, g_kpress_clock, g_kpress_server);
+ if (held < step_hold_units(st, c.server)) m = st->mask & ~(PK_TOUCH | PK_MSG | PK_CLICK);
+ else if (st->ok[0]) { LOG("hid script step %d released (server tick %llu, held %llu ticks)", g_kcur + 1, (unsigned long long)c.value, (unsigned long long)held); g_krel = 1; g_kstate = KS_OK; if (st->mask & PK_TOUCH) dm_touch_set(NULL, 0); if (st->mask & PK_CLICK) dm_ui_touch(2, st->tx, st->ty); }
+ else { // no confirm item: done on release
+ LOG("hid script step %d released (clock %llu, held %llu ticks)", g_kcur + 1, (unsigned long long)c.value, (unsigned long long)held); g_krel = 1; if (st->mask & PK_TOUCH) dm_touch_set(NULL, 0); if (st->mask & PK_CLICK) dm_ui_touch(2, st->tx, st->ty);
+ g_kcur++; g_kstate = KS_PRE; g_ktries = 0; step_chain();
+ }
+ }
+ }
+ pthread_mutex_unlock(&g_klock);
+ return m;
+}
+static void keys_parse(void) {
+ g_nkeys = 0; g_kbase = now_ns();
+ cursor_tests_parse();
+ const char *e = getenv("DM_KEYS"); if (!e) e = "";
+ char buf[4096]; strncpy(buf, e, sizeof buf - 1); buf[sizeof buf - 1] = 0;
+ for (char *save, *tok = strtok_r(buf, ",", &save); tok; tok = strtok_r(NULL, ",", &save)) {
+ char *at = strchr(tok, '@'); if (!at) die("DM_KEYS entry '%s' lacks @time / @message", tok);
+ *at = 0;
+ uint32_t mask = 0; float tx = 0, ty = 0;
+ if (tok[0] == 'T' && ((tok[1] >= '0' && tok[1] <= '9') || tok[1] == '.')) { // touch point: T<x>:<y> (normalized coordinates of the rendered picture [0,1), origin at top left)
+ char *c = strchr(tok, ':'); if (!c) die("DM_KEYS touch entry '%s' format: T<x>:<y>@pre…", tok);
+ tx = (float)atof(tok + 1); ty = (float)atof(c + 1); mask = PK_TOUCH;
+ }
+ if (tok[0] == 'C' && ((tok[1] >= '0' && tok[1] <= '9') || tok[1] == '.')) { // UI click: C<x>:<y> (normalized coordinates of the rendered picture), dm_ui_touch is sent once each for press / release
+ char *c = strchr(tok, ':'); if (!c) die("DM_KEYS click entry '%s' format: C<x>:<y>@pre…", tok);
+ tx = (float)atof(tok + 1); ty = (float)atof(c + 1); mask = PK_CLICK;
+ }
+ const char *msg = NULL; // step that sends a UI message: MSG:<category>:<action>[:<argument>]@pre[>confirm] -- presses no key, at the due time sends this message to the UI root widget (after the character select page was switched to the keyboard-and-mouse UI the controller confirm key can no longer enter a run, so the last step of test run entry sends MainMenuHD:Play instead)
+ if (!strncmp(tok, "MSG:", 4)) { if (!strchr(tok + 4, ':') || strlen(tok + 4) >= sizeof g_steps[0].msg) die("DM_KEYS message entry '%s' format: MSG:<category>:<action>[:<argument>]@pre[>confirm]", tok); msg = tok + 4; mask = PK_MSG; }
+ for (int i = 0; i < (int)(sizeof k_names / sizeof *k_names); i++) if (!strcmp(tok, k_names[i].name)) mask = k_names[i].mask;
+ for (int i = 0; i < 4; i++) if (!strcmp(tok, k_stick[i].name)) mask = k_stick[i].mask;
+ if (!mask) die("DM_KEYS unknown key name '%s'", tok);
+ const char *v = at + 1;
+ if ((*v >= '0' && *v <= '9') || *v == '.') {
+ if (mask & (PK_TOUCH | PK_MSG | PK_CLICK)) die("DM_KEYS touch / message entries only support UI-message steps (…@pre…), not timed ones");
+ char *plus = strchr(v, '+');
+ double t = atof(v), d = plus ? atof(plus + 1) : 0.2;
+ if (g_nkeys == KMAX) die("DM_KEYS has more than %d entries", KMAX);
+ g_keys[g_nkeys++] = (typeof(g_keys[0])){mask, (uint64_t)(t * 1e9), (uint64_t)((t + d) * 1e9)};
+ } else {
+ if (g_nsteps == KMAX) die("DM_KEYS has more than %d entries", KMAX);
+ typeof(g_steps[0]) *st = &g_steps[g_nsteps++]; st->mask = mask; st->tx = tx; st->ty = ty; st->msg[0] = 0; if (msg) strcpy(st->msg, msg);
+ const char *gt = strchr(v, '>');
+ size_t np = gt ? (size_t)(gt - v) : strlen(v);
+ if (np >= sizeof st->pre || (gt && (!gt[1] || strlen(gt + 1) >= sizeof st->ok))) die("DM_KEYS message step '%s' format: key@pre[%%delay seconds][+hold seconds][>confirm]", v);
+ memcpy(st->pre, v, np); st->pre[np] = 0;
+ char *pp = strchr(st->pre, '+'); if (pp) { st->hold_override = 1; st->hold_seconds = atof(pp + 1); *pp = 0; } // pre+hold seconds → 25 ticks/s in a run, 60 presented frames/s in the main menu; less than 1 unit cannot be sampled
+ char *pc = strchr(st->pre, '%'); if (pc) { st->delay_seconds = atof(pc + 1); *pc = 0; } // pre%seconds → 25 ticks/s in a run, 60 presented frames/s in the main menu
+ if (gt) strcpy(st->ok, gt + 1);
+ }
+ }
+ if (g_nsteps || g_ncursor_tests) { g_steps[0].seen_pre = 0; if (g_nsteps && !g_steps[0].pre[0]) { script_clock c = script_clock_now(); g_steps[0].seen_pre = 1; g_steps[0].seen_at = now_ns(); g_steps[0].seen_clock = c.value; g_steps[0].seen_server = c.server; } void uimsg_subscribe(void (*)(const char *, const char *, const char *)); uimsg_subscribe(keys_on_uimsg); }
+ LOG("hid script input: %d timed entries, %d UI-message steps", g_nkeys, g_nsteps);
+}
+static pthread_once_t g_kone = PTHREAD_ONCE_INIT;
+void hid_keys_init(void) { pthread_once(&g_kone, keys_parse); }
+static uint64_t keys_now(void) {
+ pthread_once(&g_kone, keys_parse);
+ uint64_t t = now_ns() - g_kbase, m = steps_now();
+ for (int i = 0; i < g_nkeys; i++) if (t >= g_keys[i].t0 && t < g_keys[i].t1) m |= g_keys[i].mask;
+ static _Atomic uint64_t seen; // print one line the first time each key is really read by the game (proves the injection arrived)
+ uint64_t newbits = m & ~atomic_fetch_or(&seen, m);
+ if (newbits) LOG("hid script keys read for the first time: mask=%#llx @%.2fs", newbits, t / 1e9);
+ return m;
+}
+
+// Optional sampling of actual GetNpadState returns; state changes + an activity witness at most once per second, a change in sampling alone does not flood the log.
+// After 1024 entries limit_reached is stated explicitly, a truncated log does not count as complete gesture evidence; without the environment variable no file is written.
+static FILE *g_receipt_file;
+static pthread_once_t g_receipt_once = PTHREAD_ONCE_INIT;
+static pthread_mutex_t g_receipt_lock = PTHREAD_MUTEX_INITIALIZER;
+static void receipt_init(void) {
+ const char *path = getenv("DM_HID_RECEIPT_LOG");
+ if (path && *path) {
+ g_receipt_file = fopen(path, "w");
+ if (!g_receipt_file) LOG("[RED] HID receipt log file failed to open: %s", path);
+ }
+}
+static void receipt(const NpadState *s, uint32_t id, uint32_t style, uint64_t km, uint64_t read_start_ns) {
+ if (!g_receipt_file) return;
+ static NpadState last; static uint32_t last_id, last_style;
+ static uint64_t last_km, last_ns; static unsigned count; static int have, truncated;
+ NpadState state = *s; state.sampling = 0; uint64_t t = now_ns();
+ pthread_mutex_lock(&g_receipt_lock);
+ if (truncated || (have && id == last_id && style == last_style && km == last_km && !memcmp(&state, &last, sizeof state) && t - last_ns < 1000000000ull)) {
+ pthread_mutex_unlock(&g_receipt_lock); return;
+ }
+ if (count == 1024) {
+ fprintf(g_receipt_file, "{\"event\":\"limit_reached\",\"limit\":1024}\n"); truncated = 1;
+ } else {
+ fprintf(g_receipt_file, "{\"event\":\"npad_return\",\"read_start_ns\":%llu,\"time_ns\":%llu,\"sampling\":%lld,\"id\":%u,\"style\":%u,\"buttons\":%llu,\"lx\":%d,\"ly\":%d,\"rx\":%d,\"ry\":%d,\"attributes\":%u,\"script_mask\":%llu}\n",
+ (unsigned long long)read_start_ns, (unsigned long long)t, (long long)s->sampling, id, style, (unsigned long long)s->buttons,
+ s->lx, s->ly, s->rx, s->ry, s->attributes, (unsigned long long)km);
+ last = state; last_id = id; last_style = style; last_km = km; last_ns = t; have = 1; count++;
+ }
+ if (fflush(g_receipt_file)) { LOG("[RED] HID receipt log write failed"); truncated = 1; }
+ pthread_mutex_unlock(&g_receipt_lock);
+}
+
+void (*dm_pad_hook)(uint64_t, uint64_t *, int32_t *, int32_t *, int32_t *, int32_t *);
+static void fill(NpadState *s, uint32_t id, uint32_t style) {
+ pthread_once(&g_receipt_once, receipt_init);
+ ui_taps_step();
+ dm_ui_frontend_pump();
+ { void dm_textinput_pump(void); dm_textinput_pump(); } // the extra "bring up keyboard" press for text boxes, setting the value of quantity boxes (dm_textinput.c)
+ { void dm_update_pump(void); dm_update_pump(); } // build number and new-version notice on the character select page (dm_update.c)
+ { void dm_actions_pump(void); dm_actions_pump(); } // vendor buy/sell / stat point entry and readout (dm_actions.c)
+ { void dm_world_pump(void); dm_world_pump(); } // tapping a world target = interact (dm_world.c)
+ dm_ui_mouse_pump(); // UI mouse messages (native clicks) are sent at the controller read: main thread, one per frame
+ { void dm_aim_cone_pump(void); dm_aim_cone_pump(); } // target selection by finger direction: restored when the right stick returns to zero (hle_private_aim.c)
+ if (atomic_load(&g_stall_due) && atomic_exchange(&g_stall_due, 0)) usleep((useconds_t)g_stall_ms * 1000); // DM_TEST_HID_STALL_MS
+ uint64_t read_start_ns = g_receipt_file ? now_ns() : 0; // covers the whole read interval of the multi-atomic fields
+ memset(s, 0, sizeof *s);
+ s->sampling = ++g_sampling;
+ if (!(style_of(id) & style)) { receipt(s, id, style, 0, read_start_ns); return; } // this style is not connected: an all-zero return is sampled as well
+ uint64_t km = keys_now();
+ // Merge the buttons of all channels first (touch / key script / host-pressed + controller; all 0 when no controller is connected), then arbitrate the
+ // "exclusive press": the press made on the player's behalf must look at what the game will actually receive this time,
+ // not at one single channel (previously only the touch channel was checked, so a controller player pressing + to open the inventory in the first 1.5
+ // seconds after joining would still turn the exclusive L into a tab switch)
+ { uint64_t dm_ui_alone_take(uint64_t);
+ uint64_t tb = ((g_in.buttons | atomic_load(&g_press_latch)) & ~dm_ui_auto_release()) | (km & ~(uint64_t)PK_MASK) | dm_ui_auto_buttons(), pb = atomic_load(&g_pad_btn) | atomic_load(&g_pad_latch);
+ static int t_old = -1; if (t_old < 0) t_old = getenv("DM_TEST_ALONE_NOPAD") != NULL; // Test control: reproduces the pre-change behavior (the arbitration ignores controller buttons, and controller buttons are still merged in during an exclusive press)
+ uint64_t alone = dm_ui_alone_take(t_old ? tb : tb | pb);
+ if (alone) s->buttons = t_old ? alone | atomic_load(&g_pad_btn) | atomic_exchange(&g_pad_latch, 0) : alone; // The exclusive press (dm_cursor.c): other buttons are withheld for these few frames, neither press-edge latch is cleared, and they are delivered as usual afterwards
+ else s->buttons = ((g_in.buttons | atomic_exchange(&g_press_latch, 0)) & ~dm_ui_auto_release()) | (km & ~(uint64_t)PK_MASK) | dm_ui_auto_buttons() | atomic_load(&g_pad_btn) | atomic_exchange(&g_pad_latch, 0); }
+ s->lx = g_in.lx; s->ly = g_in.ly; s->rx = g_in.rx; s->ry = g_in.ry;
+ // Sticks: if the touch stick has a value use it, otherwise use the controller's. The controller side only gives a non-zero value in controller mode and when the
+ // stick has actually been pushed out (dm_gamepad.m), so a slightly drifting controller left lying aside never reaches here
+ { uint64_t ax = atomic_load(&g_pad_axes); int16_t plx = (int16_t)ax, ply = (int16_t)(ax >> 16), prx = (int16_t)(ax >> 32), pry = (int16_t)(ax >> 48);
+ if (!(s->lx || s->ly) && (plx || ply)) { s->lx = plx; s->ly = ply; } if (!(s->rx || s->ry) && (prx || pry)) { s->rx = prx; s->ry = pry; } }
+ { int dm_padmode_input_gate(uint64_t *); if (dm_padmode_input_gate(&s->buttons)) s->lx = s->ly = s->rx = s->ry = 0; } // Controller / touch switch transaction in progress: neither keys nor sticks are handed to the game; keys held when the switch was triggered are not handed over either until released (dm_padmode.c)
+ if (dm_pad_hook) dm_pad_hook(guest_ns(), &s->buttons, &s->lx, &s->ly, &s->rx, &s->ry);
+ { uint64_t dm_runeword_pad_filter(uint64_t); s->buttons = dm_runeword_pad_filter(s->buttons); } // While the runeword list is open in controller mode, the D-pad / A / B / L / R belong to the list (dm_runeword.c)
+ { uint64_t dm_padmode_page_filter(uint64_t); s->buttons = dm_padmode_page_filter(s->buttons); } // While one of our own pages (the Custom page etc.) is open in controller mode, the buttons the page declares belong to the page (dm_padmode.c)
+ { uint64_t dm_swapskill_filter(uint64_t); s->buttons = dm_swapskill_filter(s->buttons); } // off-hand set skill: hold that key back until the weapon swap is done (dm_actions.c)
+ { static int trx, tryy, tdelay, held, tset = -1; if (tset < 0) { const char *e = getenv("DM_TEST_RSTICK"); tset = e && sscanf(e, "%d,%d,%d", &trx, &tryy, &tdelay) >= 2; } // test switch DM_TEST_RSTICK=rx,ry[,delay count]: while the key script holds A / B / X / Y, set the right stick to this value (simulates "hold a skill button and drag", the game itself also sees the right stick); delay = on which controller read after the press it starts being given (a real finger presses first, then drags out a direction)
+ if (tset > 0) { held = (s->buttons & 0xf) ? held + 1 : 0; if (held > tdelay) { s->rx = trx; s->ry = tryy; } } }
+ if (s->lx || s->ly || (km & PK_MASK & ~(uint64_t)PK_TOUCH)) atomic_store(&g_stick_ns, now_ns()); // the last time the player's stick (front end / script pseudo-keys) was non-zero; a stick rewritten by the cursor does not count
+ dm_cursor_apply(&s->buttons, &s->lx, &s->ly); // pointer target: within this sample, rewrite the left stick / A according to the original game's cursor position (see dm_cursor.h)
+ { static uint64_t prev; if ((s->buttons & DM_BTN_L) && !(prev & DM_BTN_L)) { int n = atomic_fetch_add(&g_dm_l_presses, 1) + 1; if (n <= 3) LOG("controller read: the 'Show Items' key (L) was delivered to the game, time %d in this process", n); } prev = s->buttons; } // number of presses of the "Show Items" key (L) delivered to the game (used by the boss drop readout, dm_bossrush.c)
+ if (km & PK_LSL) s->lx = -32767; if (km & PK_LSR) s->lx = 32767; // script stick pseudo-keys: push to full (Horizon stick ±32767, up is positive)
+ if (km & PK_LSU) s->ly = 32767; if (km & PK_LSD) s->ly = -32767;
+ s->attributes = ATTR_CONNECTED;
+ atomic_store_explicit(&g_sampled_right,(uint32_t)s->rx|((uint64_t)(uint32_t)s->ry<<32),memory_order_release);
+ receipt(s, id, style, km, read_start_ns);
+}
+
+HLE_FORCE("_ZN2nn3hid14InitializeNpadEv", "connect to the hid service; the host takes input from dm_input", void, h_hid_InitNpad, void) {}
+HLE_FORCE("_ZN2nn3hid21InitializeTouchScreenEv", "connect to the hid service; touch input is wired separately by the front end", void, h_hid_InitTouch, void) {}
+HLE_FORCE("_ZN2nn3hid22SetSupportedNpadIdTypeEPKjm", "supported controller ids; fixed by the host", void, h_hid_SetIds, const uint32_t *ids, uint64_t n) { (void)ids; (void)n; }
+// BitFlagSet<32> is passed by value in a w register
+HLE_FORCE("_ZN2nn3hid24SetSupportedNpadStyleSetENS_4util10BitFlagSetILi32ENS0_12NpadStyleTagEEE", "supported controller styles; fixed by the host", void, h_hid_SetStyles, uint32_t styles) { (void)styles; }
+HLE_FORCE("_ZN2nn3hid29SetNpadHandheldActivationModeENS0_26NpadHandheldActivationModeE", "handheld controller activation mode; fixed by the host", void, h_hid_SetHhMode, int m) { (void)m; }
+HLE_FORCE("_ZN2nn3hid15GetNpadStyleSetERKj", "controller style query; follows DM_OPMODE", uint32_t, h_hid_GetStyle, const uint32_t *id) { return style_of(*id); }
+HLE_FORCE("_ZN2nn3hid12GetNpadStateEPNS0_16NpadFullKeyStateERKj", "full-key controller state; from dm_input", void, h_hid_GetFullKey, NpadState *s, const uint32_t *id) { fill(s, *id, STYLE_FULLKEY); }
+HLE_FORCE("_ZN2nn3hid12GetNpadStateEPNS0_17NpadHandheldStateERKj", "handheld controller state; from dm_input", void, h_hid_GetHandheld, NpadState *s, const uint32_t *id) { fill(s, *id, STYLE_HANDHELD); }
+
+// Vibration: device count 0, the game will not initialize / send any further
+HLE_FORCE("_ZN2nn3hid20IsVibrationPermittedEv", "vibration permission; not supported by the host (returns bool)", unsigned, h_hid_VibPermit, void) { return 0; }
+HLE_FORCE("_ZN2nn3hid25GetVibrationDeviceHandlesEPNS0_21VibrationDeviceHandleEiRKjNS_4util10BitFlagSetILi32ENS0_12NpadStyleTagEEE",
+ "vibration devices; 0 on the host", int, h_hid_GetVibHandles, void *out, int max, const uint32_t *id, uint32_t style) { (void)out; (void)max; (void)id; (void)style; return 0; }
+HLE_FORCE("_ZN2nn3hid25InitializeVibrationDeviceERKNS0_21VibrationDeviceHandleE", "vibration; not supported by the host", void, h_hid_InitVib, const void *h) { (void)h; }
+HLE_FORCE("_ZN2nn3hid18SendVibrationValueERKNS0_21VibrationDeviceHandleERKNS0_14VibrationValueE", "vibration; not supported by the host", void, h_hid_SendVib, const void *h, const void *v) { (void)h; (void)v; }
+
+// Touchscreen: nn::hid::TouchScreenState<16> = { int64 sampling; int32 count; int32 _r; TouchState touches[16] (0x28 each) }
+// Contents are filled by hle_touch.c from the touch points pushed by the front end (see that file for the layout and the short-press latch); the game only looks at entry 0, returns the number of states written, 1
+typedef struct { int64_t sampling; int32_t count, _r; uint8_t touches[16][0x28]; } TouchState16;
+_Static_assert(sizeof(TouchState16) == 0x290, "TouchScreenState<16>");
+int touch_fill(void *out, uint64_t now_ns);
+HLE_FORCE("_ZN2nn3hid20GetTouchScreenStatesILm16EEEiPNS0_16TouchScreenStateIXT_EEEi", "touch screen states: front-end touch points (hle_touch.c)", int, h_hid_GetTouch, TouchState16 *out, int n) {
+ if (n < 1) return 0;
+ touch_fill(out, guest_ns());
+ return 1;
+}