diff --git a/tools/bionic-runtime/dd-directinput/src/dd_directinput.c b/tools/bionic-runtime/dd-directinput/src/dd_directinput.c index e43def438c..8092d44563 100644 --- a/tools/bionic-runtime/dd-directinput/src/dd_directinput.c +++ b/tools/bionic-runtime/dd-directinput/src/dd_directinput.c @@ -27,7 +27,15 @@ * fakeinput, the app's vibration and its Game vibration switch). Wine's XInputSetState holds its lock * across a synchronous output report; on the worker that never holds up a game thread. * + * MODES + * Direct (the above) or pass-through: every call goes to Wine's xinput copy, as if this DLL were not + * there. launch.sh names a mode file in DD_DIRECTINPUT_CONTROL (a unix path); its first byte, '1' direct + * or '0' pass-through, is read every 250 ms by a background thread, so the mode can change while the + * game runs. Without the file the DLL stays direct. The same thread does the once-a-second ring and pad + * node checks, so a game thread never waits on a file. + * * ENVIRONMENT + * DD_DIRECTINPUT_CONTROL unix path of the mode file (launch.sh) * DD_DIRECTINPUT_LOG=0 no diagnostics (default: one line per load, attach and loss) * DD_DIRECTINPUT_LOGVIA auto (default) | stderr | ods: where diagnostics go * DD_DIRECTINPUT_PATHMODE auto (default) | wine | unix | z: which DOS path form to use @@ -143,6 +151,7 @@ _Static_assert(sizeof(DDI_KEYSTROKE) == 8, "XINPUT_KEYSTROKE"); #define DDI_PVER 0x0114 #define DDI_RECHECK_MS 1000u +#define DDI_CTL_POLL_MS 250u #define DDI_SNAP_ATTEMPTS 64u #define DDI_WINE_COPY L"dd-directinput-wine-xinput.dll" @@ -180,6 +189,10 @@ static int g_pathmode = PATH_AUTO; static uint32_t g_mask; typedef WCHAR *(CDECL *wine_dos_fn)(const char *); static wine_dos_fn g_wine_dos; +enum { MODE_DIRECT = 1, MODE_FORWARD = 2 }; +static volatile LONG g_mode = MODE_DIRECT; +static char *g_ctl_path; /* UTF-8 unix path of the mode file, or NULL */ +static volatile LONG g_watching; /* the background thread runs the slot checks */ /* ---------------------------------------------------------------------------------------------- */ /* diagnostics */ @@ -316,6 +329,11 @@ static BOOL CALLBACK init_once(PINIT_ONCE once, PVOID param, PVOID *ctx) { ddi_parse_paths_w(paths, take_path, NULL, DDI_SLOTS); HeapFree(GetProcessHeap(), 0, paths); } + WCHAR *ctl = env_dup(L"DD_DIRECTINPUT_CONTROL"); + if (ctl) { + g_ctl_path = utf8_dup(ctl, (size_t)lstrlenW(ctl)); + HeapFree(GetProcessHeap(), 0, ctl); + } WCHAR *dir = env_dup(L"FAKE_EVDEV_DIR"); char *udir = dir ? utf8_dup(dir, (size_t)lstrlenW(dir)) : NULL; if (dir) HeapFree(GetProcessHeap(), 0, dir); @@ -328,15 +346,19 @@ static BOOL CALLBACK init_once(PINIT_ONCE once, PVOID param, PVOID *ctx) { g_slots[i].node_path = node; } static const char *const modes[] = {"auto", "wine", "unix", "z"}; - ddi_log("%s %s (%s) slots 0x%x from FAKE_EVDEV_MEMFD_PATHS, pad nodes %s, paths %s%s", DDI_VERSION, g_dllname, - DDI_BUILD, (unsigned)g_mask, udir ? udir : "(no FAKE_EVDEV_DIR: not checked)", modes[g_pathmode], - g_wine_dos ? "" : " (no wine_get_dos_file_name)"); + ddi_log("%s %s (%s) slots 0x%x from FAKE_EVDEV_MEMFD_PATHS, pad nodes %s, mode file %s, paths %s%s", DDI_VERSION, + g_dllname, DDI_BUILD, (unsigned)g_mask, udir ? udir : "(no FAKE_EVDEV_DIR: not checked)", + g_ctl_path ? g_ctl_path : "(none: direct only)", modes[g_pathmode], g_wine_dos ? "" : " (no wine_get_dos_file_name)"); if (udir) HeapFree(GetProcessHeap(), 0, udir); return TRUE; } +static INIT_ONCE g_watch_init = INIT_ONCE_STATIC_INIT; +static BOOL CALLBACK watch_once(PINIT_ONCE once, PVOID param, PVOID *ctx); + static void ddi_init(void) { InitOnceExecuteOnce(&g_init, init_once, NULL, NULL); + InitOnceExecuteOnce(&g_watch_init, watch_once, NULL, NULL); } /* ---------------------------------------------------------------------------------------------- */ @@ -395,7 +417,8 @@ static int map_path(const WCHAR *path, const uint8_t **view) { return 1; } -static int slot_open(ddi_slot *s) { +/* A new read-only view of slot s's ring header, or NULL (logged once until it opens). */ +static const uint8_t *slot_map(ddi_slot *s) { int last = 0; for (int i = 0; i < 3; i++) { int mode = PATH_ORDER[i]; @@ -408,12 +431,10 @@ static int slot_open(ddi_slot *s) { char shown[1024]; WideCharToMultiByte(CP_UTF8, 0, p, -1, shown, (int)sizeof shown, NULL, NULL); shown[sizeof shown - 1] = 0; - s->view = v; - s->have_last = 0; s->missing_logged = 0; ddi_log("slot %u attached: %s", s->index, shown); HeapFree(GetProcessHeap(), 0, p); - return 1; + return v; } HeapFree(GetProcessHeap(), 0, p); if (last < 0) break; /* there but not a ring: another path form names the same file */ @@ -423,7 +444,7 @@ static int slot_open(ddi_slot *s) { last < 0 ? "is not a version-2 controller ring" : "cannot be opened yet"); s->missing_logged = 1; } - return 0; + return NULL; } static void slot_close(ddi_slot *s) { @@ -447,24 +468,55 @@ static int node_exists(const ddi_slot *s) { return 0; } +/* Under s->lock: record whether a pad is on the slot, logging a change. */ +static void set_node(ddi_slot *s, int present) { + if (present != s->node_present || !s->absent_logged) { + if (!present) ddi_log("slot %u: no pad on this slot (%s missing)", s->index, s->node_path); + else if (s->absent_logged) ddi_log("slot %u: pad present", s->index); + s->absent_logged = 1; + } + s->node_present = present; +} + +/* The once-a-second checks of slot s: a view that no longer holds a ring goes, a missing one is opened, + * the pad node is looked up. The file work happens outside s->lock, so a game thread never waits on it. */ +static void slot_check(ddi_slot *s) { + if (!s->configured) return; + AcquireSRWLockExclusive(&s->lock); + if (s->view && !ddi_valid(s->view)) { + ddi_log("slot %u: its ring file no longer holds a controller ring, re-opening", s->index); + slot_close(s); + } + int need = !s->view; + ReleaseSRWLockExclusive(&s->lock); + const uint8_t *v = need ? slot_map(s) : NULL; + int present = node_exists(s); + AcquireSRWLockExclusive(&s->lock); + if (v && !s->view) { + s->view = v; + s->have_last = 0; + v = NULL; + } + set_node(s, present); + ReleaseSRWLockExclusive(&s->lock); + if (v) UnmapViewOfFile(v); +} + /* Under s->lock. The current snapshot into *out; returns 1 if the slot is connected. */ static int slot_read(ddi_slot *s, ddi_snap *out) { if (!s->configured) return 0; - ULONGLONG now = GetTickCount64(); - if (!s->next_check || now >= s->next_check) { - s->next_check = now + DDI_RECHECK_MS; - if (s->view && !ddi_valid(s->view)) { - ddi_log("slot %u: its ring file no longer holds a controller ring, re-opening", s->index); - slot_close(s); + if (!__atomic_load_n(&g_watching, __ATOMIC_ACQUIRE)) { + /* no background thread: the checks run here, at most once a second */ + ULONGLONG now = GetTickCount64(); + if (!s->next_check || now >= s->next_check) { + s->next_check = now + DDI_RECHECK_MS; + if (s->view && !ddi_valid(s->view)) slot_close(s); + if (!s->view) { + s->view = slot_map(s); + s->have_last = 0; + } + set_node(s, node_exists(s)); } - if (!s->view) slot_open(s); - int present = node_exists(s); - if (present != s->node_present || !s->absent_logged) { - if (!present) ddi_log("slot %u: no pad on this slot (%s missing)", s->index, s->node_path); - else if (s->absent_logged) ddi_log("slot %u: pad present", s->index); - s->absent_logged = 1; - } - s->node_present = present; } if (!s->view || !s->node_present) return 0; if (ddi_snapshot(s->view, out, DDI_SNAP_ATTEMPTS)) { @@ -490,22 +542,27 @@ static int connected(DWORD idx) { } /* ---------------------------------------------------------------------------------------------- */ -/* rumble: Wine's own xinput on a worker thread */ +/* Wine's own xinput: the pass-through mode and the rumble path */ -typedef DWORD(WINAPI *fwd_set_fn)(DWORD, DDI_VIBRATION *); +typedef DWORD(WINAPI *fx_state_fn)(DWORD, DDI_STATE *); +typedef DWORD(WINAPI *fx_set_fn)(DWORD, DDI_VIBRATION *); +typedef DWORD(WINAPI *fx_caps_fn)(DWORD, DWORD, DDI_CAPABILITIES *); +typedef DWORD(WINAPI *fx_caps_ex_fn)(DWORD, DWORD, DWORD, DDI_CAPABILITIES_EX *); +typedef void(WINAPI *fx_enable_fn)(BOOL); +typedef DWORD(WINAPI *fx_battery_fn)(DWORD, BYTE, DDI_BATTERY_INFORMATION *); +typedef DWORD(WINAPI *fx_keystroke_fn)(DWORD, DWORD, DDI_KEYSTROKE *); static struct { INIT_ONCE once; - SRWLOCK lock; - HANDLE wake; /* auto-reset */ - fwd_set_fn set_state; /* Wine's XInputSetState, NULL = rumble off */ - WORD game_l[DDI_SLOTS], game_r[DDI_SLOTS]; - WORD sent_l[DDI_SLOTS], sent_r[DDI_SLOTS]; - int sent_valid[DDI_SLOTS]; - int set_logged[DDI_SLOTS]; - int logged_on[DDI_SLOTS]; - DWORD log_tick[DDI_SLOTS]; -} g_rumble = {INIT_ONCE_STATIC_INIT, SRWLOCK_INIT, NULL, NULL, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}}; + volatile LONG ok; + fx_state_fn get_state, get_state_ex; + fx_set_fn set_state; + fx_caps_fn caps; + fx_caps_ex_fn caps_ex; + fx_enable_fn enable; + fx_battery_fn battery; + fx_keystroke_fn keystroke; +} g_fwd = {INIT_ONCE_STATIC_INIT, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; /* 1 if the file at `path` is one of our own DLLs (it carries our marker). */ static int file_is_ours(const WCHAR *path) { @@ -535,10 +592,67 @@ static int file_is_ours(const WCHAR *path) { return found; } +/* Never from DllMain: the first rumble or pass-through request loads the copy. */ +static BOOL CALLBACK fwd_once(PINIT_ONCE once, PVOID param, PVOID *ctx) { + (void)once; (void)param; (void)ctx; + HMODULE h = LoadLibraryExW(DDI_WINE_COPY, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (!h) { + ddi_log("%ls does not load from system32 (error %lu): no rumble, no pass-through", DDI_WINE_COPY, + (unsigned long)GetLastError()); + return TRUE; + } + WCHAR path[MAX_PATH]; + char shown[MAX_PATH * 3] = "?"; + DWORD n = GetModuleFileNameW(h, path, MAX_PATH); + if (n && n < MAX_PATH) WideCharToMultiByte(CP_UTF8, 0, path, -1, shown, (int)sizeof shown, NULL, NULL); + shown[sizeof shown - 1] = 0; + if (h == g_module || (n && n < MAX_PATH && file_is_ours(path))) { + ddi_log("%s is one of our own DLLs, not Wine's xinput: no rumble, no pass-through", shown); + FreeLibrary(h); + return TRUE; + } + g_fwd.get_state = (fx_state_fn)(void (*)(void))GetProcAddress(h, "XInputGetState"); + g_fwd.set_state = (fx_set_fn)(void (*)(void))GetProcAddress(h, "XInputSetState"); + g_fwd.caps = (fx_caps_fn)(void (*)(void))GetProcAddress(h, "XInputGetCapabilities"); + g_fwd.enable = (fx_enable_fn)(void (*)(void))GetProcAddress(h, "XInputEnable"); + g_fwd.battery = (fx_battery_fn)(void (*)(void))GetProcAddress(h, "XInputGetBatteryInformation"); + g_fwd.keystroke = (fx_keystroke_fn)(void (*)(void))GetProcAddress(h, "XInputGetKeystroke"); + g_fwd.get_state_ex = (fx_state_fn)(void (*)(void))GetProcAddress(h, (LPCSTR)(ULONG_PTR)100); + g_fwd.caps_ex = (fx_caps_ex_fn)(void (*)(void))GetProcAddress(h, (LPCSTR)(ULONG_PTR)108); + if (!g_fwd.get_state || !g_fwd.set_state || !g_fwd.caps || !g_fwd.enable || !g_fwd.get_state_ex) { + ddi_log("%s lacks XInputGetState/SetState/GetCapabilities/Enable/#100: no rumble, no pass-through", shown); + FreeLibrary(h); + return TRUE; + } + __atomic_store_n(&g_fwd.ok, 1, __ATOMIC_RELEASE); + ddi_log("Wine's xinput loaded from %s (#108 %s, battery %s, keystroke %s)", shown, g_fwd.caps_ex ? "yes" : "no", + g_fwd.battery ? "yes" : "no", g_fwd.keystroke ? "yes" : "no"); + return TRUE; +} + +static int fwd_ready(void) { + InitOnceExecuteOnce(&g_fwd.once, fwd_once, NULL, NULL); + return __atomic_load_n(&g_fwd.ok, __ATOMIC_ACQUIRE) != 0; +} + +/* Direct mode's rumble: XInputSetState records the speeds, a worker hands changes to Wine's xinput. */ +static struct { + INIT_ONCE once; + SRWLOCK lock; + HANDLE wake; /* auto-reset; NULL = no worker (rumble off) */ + WORD game_l[DDI_SLOTS], game_r[DDI_SLOTS]; + WORD sent_l[DDI_SLOTS], sent_r[DDI_SLOTS]; + int sent_valid[DDI_SLOTS]; + int set_logged[DDI_SLOTS]; + int logged_on[DDI_SLOTS]; + DWORD log_tick[DDI_SLOTS]; +} g_rumble = {INIT_ONCE_STATIC_INIT, SRWLOCK_INIT, NULL, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}}; + static DWORD WINAPI rumble_worker(LPVOID param) { (void)param; for (;;) { WaitForSingleObject(g_rumble.wake, INFINITE); + if (__atomic_load_n(&g_mode, __ATOMIC_ACQUIRE) != MODE_DIRECT) continue; /* pass-through sets them itself */ for (DWORD i = 0; i < DDI_SLOTS; i++) { int on = __atomic_load_n(&g_enabled, __ATOMIC_ACQUIRE) != 0; AcquireSRWLockExclusive(&g_rumble.lock); @@ -561,7 +675,7 @@ static DWORD WINAPI rumble_worker(LPVOID param) { ReleaseSRWLockExclusive(&g_rumble.lock); if (!due) continue; DDI_VIBRATION v = {l, r}; - DWORD rc = g_rumble.set_state(i, &v); + DWORD rc = g_fwd.set_state(i, &v); if (note) ddi_log("slot %u: motors %s (low %u, high %u) -> Wine's xinput returned %lu", (unsigned)i, (l || r) ? "ON" : "off", (unsigned)l, (unsigned)r, (unsigned long)rc); } @@ -569,48 +683,120 @@ static DWORD WINAPI rumble_worker(LPVOID param) { return 0; } -/* Never from DllMain: the first XInputSetState loads Wine's xinput copy and starts the worker. */ static BOOL CALLBACK rumble_once(PINIT_ONCE once, PVOID param, PVOID *ctx) { (void)once; (void)param; (void)ctx; - HMODULE h = LoadLibraryExW(DDI_WINE_COPY, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); - if (!h) { - ddi_log("rumble off: %ls does not load from system32 (error %lu)", DDI_WINE_COPY, (unsigned long)GetLastError()); - return TRUE; - } - WCHAR path[MAX_PATH]; - char shown[MAX_PATH * 3] = "?"; - DWORD n = GetModuleFileNameW(h, path, MAX_PATH); - if (n && n < MAX_PATH) WideCharToMultiByte(CP_UTF8, 0, path, -1, shown, (int)sizeof shown, NULL, NULL); - shown[sizeof shown - 1] = 0; - if (h == g_module || (n && n < MAX_PATH && file_is_ours(path))) { - ddi_log("rumble off: %s is one of our own DLLs, not Wine's xinput", shown); - FreeLibrary(h); - return TRUE; - } - fwd_set_fn set = (fwd_set_fn)(void (*)(void))GetProcAddress(h, "XInputSetState"); - if (!set) { - ddi_log("rumble off: %s has no XInputSetState", shown); - FreeLibrary(h); - return TRUE; - } + if (!fwd_ready()) return TRUE; HANDLE wake = CreateEventW(NULL, FALSE, FALSE, NULL); HANDLE t = wake ? CreateThread(NULL, 0, rumble_worker, NULL, 0, NULL) : NULL; if (!t) { ddi_log("rumble off: no worker thread (error %lu)", (unsigned long)GetLastError()); if (wake) CloseHandle(wake); - FreeLibrary(h); return TRUE; } CloseHandle(t); - g_rumble.wake = wake; - __atomic_store_n(&g_rumble.set_state, set, __ATOMIC_RELEASE); - ddi_log("rumble through Wine's xinput %s (%s, %s), on a worker thread", shown, g_dllname, DDI_BUILD); + __atomic_store_n(&g_rumble.wake, wake, __ATOMIC_RELEASE); + ddi_log("rumble through Wine's xinput on a worker thread (%s, %s)", g_dllname, DDI_BUILD); return TRUE; } static void rumble_wake(void) { InitOnceExecuteOnce(&g_rumble.once, rumble_once, NULL, NULL); - if (__atomic_load_n(&g_rumble.set_state, __ATOMIC_ACQUIRE)) SetEvent(g_rumble.wake); + HANDLE w = __atomic_load_n(&g_rumble.wake, __ATOMIC_ACQUIRE); + if (w) SetEvent(w); +} + +/* ---------------------------------------------------------------------------------------------- */ +/* mode file and the background thread */ + +/* The mode file's first byte: 1 = '1' (direct), 0 = '0' (pass-through), -1 = no file or another byte. */ +static int read_mode_file(void) { + for (int i = 0; i < 3; i++) { + int mode = PATH_ORDER[i]; + if (g_pathmode != PATH_AUTO && mode != g_pathmode) continue; + WCHAR *p = dos_path(g_ctl_path, mode); + if (!p) continue; + HANDLE f = CreateFileW(p, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); + HeapFree(GetProcessHeap(), 0, p); + if (f == INVALID_HANDLE_VALUE) continue; + char c = 0; + DWORD got = 0; + BOOL ok = ReadFile(f, &c, 1, &got, NULL); + CloseHandle(f); + if (ok && got == 1 && (c == '0' || c == '1')) return c == '1'; + return -1; + } + return -1; +} + +static void apply_mode(int direct) { + LONG want = direct ? MODE_DIRECT : MODE_FORWARD; + if (__atomic_load_n(&g_mode, __ATOMIC_ACQUIRE) == want) return; + if (want == MODE_FORWARD) { + static int refused; + if (!fwd_ready()) { + if (!refused) ddi_log("pass-through asked for, but Wine's xinput is not available: staying direct"); + refused = 1; + return; + } + g_fwd.enable(__atomic_load_n(&g_enabled, __ATOMIC_ACQUIRE) ? TRUE : FALSE); + __atomic_store_n(&g_mode, MODE_FORWARD, __ATOMIC_RELEASE); + ddi_log("mode: pass-through (every call goes to Wine's xinput)"); + } else { + __atomic_store_n(&g_mode, MODE_DIRECT, __ATOMIC_RELEASE); + /* the motors were last set through Wine's xinput: the game's speeds go out again from here */ + AcquireSRWLockExclusive(&g_rumble.lock); + for (uint32_t i = 0; i < DDI_SLOTS; i++) g_rumble.sent_valid[i] = 0; + ReleaseSRWLockExclusive(&g_rumble.lock); + HANDLE w = __atomic_load_n(&g_rumble.wake, __ATOMIC_ACQUIRE); + if (w) SetEvent(w); + ddi_log("mode: direct (XInput from the pad state)"); + } +} + +static DWORD WINAPI watch_thread(LPVOID param) { + (void)param; + ULONGLONG next_slots = GetTickCount64() + DDI_RECHECK_MS; + for (;;) { + Sleep(DDI_CTL_POLL_MS); + if (g_ctl_path) { + int m = read_mode_file(); + if (m >= 0) apply_mode(m); + } + ULONGLONG now = GetTickCount64(); + if (now >= next_slots) { + next_slots = now + DDI_RECHECK_MS; + for (uint32_t i = 0; i < DDI_SLOTS; i++) slot_check(&g_slots[i]); + } + } + return 0; +} + +/* After init_once, in the first call: the slots are checked and the mode read now, so that call already + * sees the pad and the right mode; then the background thread takes over. */ +static BOOL CALLBACK watch_once(PINIT_ONCE once, PVOID param, PVOID *ctx) { + (void)once; (void)param; (void)ctx; + for (uint32_t i = 0; i < DDI_SLOTS; i++) slot_check(&g_slots[i]); + if (g_ctl_path) { + int m = read_mode_file(); + if (m >= 0) apply_mode(m); + ddi_log("start mode: %s", __atomic_load_n(&g_mode, __ATOMIC_ACQUIRE) == MODE_FORWARD ? "pass-through" : "direct"); + } + HANDLE t = CreateThread(NULL, 0, watch_thread, NULL, 0, NULL); + if (t) { + CloseHandle(t); + __atomic_store_n(&g_watching, 1, __ATOMIC_RELEASE); + } else { + ddi_log("no background thread (error %lu): checks run in the calls, the mode stays as it started", + (unsigned long)GetLastError()); + } + return TRUE; +} + +/* 1 if the calls go to Wine's xinput. */ +static int forwarding(void) { + ddi_init(); + return __atomic_load_n(&g_mode, __ATOMIC_ACQUIRE) == MODE_FORWARD; } /* ---------------------------------------------------------------------------------------------- */ @@ -652,24 +838,30 @@ static DWORD get_state(DWORD idx, DDI_STATE *st, int guide) { } DWORD WINAPI XInputGetState(DWORD dwUserIndex, DDI_STATE *pState) { + if (dwUserIndex >= DDI_SLOTS || !pState) return ERROR_BAD_ARGUMENTS; + if (forwarding()) return g_fwd.get_state(dwUserIndex, pState); return get_state(dwUserIndex, pState, 0); } DWORD WINAPI XInputGetStateEx(DWORD dwUserIndex, DDI_STATE *pState) { + if (dwUserIndex >= DDI_SLOTS || !pState) return ERROR_BAD_ARGUMENTS; + if (forwarding()) return g_fwd.get_state_ex(dwUserIndex, pState); return get_state(dwUserIndex, pState, 1); } DWORD WINAPI XInputSetState(DWORD dwUserIndex, DDI_VIBRATION *pVibration) { if (dwUserIndex >= DDI_SLOTS || !pVibration) return ERROR_BAD_ARGUMENTS; - int conn = connected(dwUserIndex); + int fwd = forwarding(); + int conn = fwd || connected(dwUserIndex); WORD l = conn ? pVibration->wLeftMotorSpeed : 0, r = conn ? pVibration->wRightMotorSpeed : 0; AcquireSRWLockExclusive(&g_rumble.lock); int changed = g_rumble.game_l[dwUserIndex] != l || g_rumble.game_r[dwUserIndex] != r; - g_rumble.game_l[dwUserIndex] = l; + g_rumble.game_l[dwUserIndex] = l; /* kept in pass-through too: direct mode resends them */ g_rumble.game_r[dwUserIndex] = r; int first = !g_rumble.set_logged[dwUserIndex]; g_rumble.set_logged[dwUserIndex] = 1; ReleaseSRWLockExclusive(&g_rumble.lock); + if (fwd) return g_fwd.set_state(dwUserIndex, pVibration); if (first) ddi_log("slot %u: first XInputSetState in this process (%s, %s): low %u, high %u -> %s", (unsigned)dwUserIndex, g_dllname, DDI_BUILD, (unsigned)pVibration->wLeftMotorSpeed, (unsigned)pVibration->wRightMotorSpeed, @@ -680,7 +872,12 @@ DWORD WINAPI XInputSetState(DWORD dwUserIndex, DDI_VIBRATION *pVibration) { void WINAPI XInputEnable(BOOL enable) { LONG was = __atomic_exchange_n(&g_enabled, enable ? 1 : 0, __ATOMIC_ACQ_REL); - if (was != (enable ? 1 : 0) && __atomic_load_n(&g_rumble.set_state, __ATOMIC_ACQUIRE)) SetEvent(g_rumble.wake); + if (forwarding()) { + g_fwd.enable(enable ? TRUE : FALSE); + return; + } + HANDLE w = __atomic_load_n(&g_rumble.wake, __ATOMIC_ACQUIRE); + if (was != (enable ? 1 : 0) && w) SetEvent(w); } static void fill_caps(DDI_CAPABILITIES *c) { @@ -701,6 +898,7 @@ static void fill_caps(DDI_CAPABILITIES *c) { DWORD WINAPI XInputGetCapabilities(DWORD dwUserIndex, DWORD dwFlags, DDI_CAPABILITIES *pCapabilities) { if (dwUserIndex >= DDI_SLOTS || !pCapabilities || (dwFlags & ~DDI_FLAG_GAMEPAD)) return ERROR_BAD_ARGUMENTS; + if (forwarding()) return g_fwd.caps(dwUserIndex, dwFlags, pCapabilities); if (!connected(dwUserIndex)) return ERROR_DEVICE_NOT_CONNECTED; fill_caps(pCapabilities); return ERROR_SUCCESS; @@ -709,6 +907,17 @@ DWORD WINAPI XInputGetCapabilities(DWORD dwUserIndex, DWORD dwFlags, DDI_CAPABIL DWORD WINAPI XInputGetCapabilitiesEx(DWORD dwReserved, DWORD dwUserIndex, DWORD dwFlags, DDI_CAPABILITIES_EX *pCapabilities) { if (dwReserved != 1 || dwUserIndex >= DDI_SLOTS || !pCapabilities || (dwFlags & ~DDI_FLAG_GAMEPAD)) return ERROR_BAD_ARGUMENTS; + if (forwarding()) { + if (g_fwd.caps_ex) return g_fwd.caps_ex(dwReserved, dwUserIndex, dwFlags, pCapabilities); + memset(pCapabilities, 0, sizeof *pCapabilities); /* a Wine without #108: its capabilities, the pad's ids */ + DWORD r = g_fwd.caps(dwUserIndex, dwFlags, &pCapabilities->Capabilities); + if (r == ERROR_SUCCESS) { + pCapabilities->VendorId = DDI_VID; + pCapabilities->ProductId = DDI_PID; + pCapabilities->VersionNumber = DDI_PVER; + } + return r; + } if (!connected(dwUserIndex)) return ERROR_DEVICE_NOT_CONNECTED; memset(pCapabilities, 0, sizeof *pCapabilities); fill_caps(&pCapabilities->Capabilities); @@ -721,6 +930,7 @@ DWORD WINAPI XInputGetCapabilitiesEx(DWORD dwReserved, DWORD dwUserIndex, DWORD DWORD WINAPI XInputGetBatteryInformation(DWORD dwUserIndex, BYTE devType, DDI_BATTERY_INFORMATION *pInfo) { if (dwUserIndex >= DDI_SLOTS || !pInfo) return ERROR_BAD_ARGUMENTS; if (devType != DDI_BATTERY_DEVTYPE_GAMEPAD && devType != DDI_BATTERY_DEVTYPE_HEADSET) return ERROR_BAD_ARGUMENTS; + if (forwarding() && g_fwd.battery) return g_fwd.battery(dwUserIndex, devType, pInfo); if (!connected(dwUserIndex)) return ERROR_DEVICE_NOT_CONNECTED; if (devType == DDI_BATTERY_DEVTYPE_GAMEPAD) { pInfo->BatteryType = DDI_BATTERY_TYPE_WIRED; @@ -859,9 +1069,9 @@ static DWORD keystroke_one(DWORD idx, DDI_KEYSTROKE *k) { } DWORD WINAPI XInputGetKeystroke(DWORD dwUserIndex, DWORD dwReserved, DDI_KEYSTROKE *pKeystroke) { - (void)dwReserved; if (!pKeystroke) return ERROR_BAD_ARGUMENTS; if (dwUserIndex >= DDI_SLOTS && dwUserIndex != DDI_USER_ANY) return ERROR_BAD_ARGUMENTS; + if (forwarding()) return g_fwd.keystroke ? g_fwd.keystroke(dwUserIndex, dwReserved, pKeystroke) : ERROR_EMPTY; if (dwUserIndex == DDI_USER_ANY) { int any = 0; for (DWORD i = 0; i < DDI_SLOTS; i++) { @@ -884,12 +1094,20 @@ DWORD WINAPI XInputWaitForGuideButton(DWORD dwUserIndex, DWORD dwFlag, LPVOID pV ddi_slot *s = &g_slots[dwUserIndex]; LONG gen = __atomic_load_n(&s->guide_cancel, __ATOMIC_ACQUIRE); for (;;) { - ddi_snap snap; - AcquireSRWLockExclusive(&s->lock); - int c = slot_read(s, &snap); - ReleaseSRWLockExclusive(&s->lock); + int c, pressed; + if (forwarding()) { + DDI_STATE st; + c = g_fwd.get_state_ex(dwUserIndex, &st) == ERROR_SUCCESS; + pressed = c && (st.Gamepad.wButtons & DDI_XI_GUIDE); + } else { + ddi_snap snap; + AcquireSRWLockExclusive(&s->lock); + c = slot_read(s, &snap); + ReleaseSRWLockExclusive(&s->lock); + pressed = c && (snap.buttons & (1u << DDI_B_MODE)); + } if (!c) return ERROR_DEVICE_NOT_CONNECTED; - if (snap.buttons & (1u << DDI_B_MODE)) return ERROR_SUCCESS; + if (pressed) return ERROR_SUCCESS; if (__atomic_load_n(&s->guide_cancel, __ATOMIC_ACQUIRE) != gen) return ERROR_SUCCESS; Sleep(8); } diff --git a/tools/bionic-runtime/tool/launch.sh b/tools/bionic-runtime/tool/launch.sh index 7c54e4f0fb..d265f177b7 100644 --- a/tools/bionic-runtime/tool/launch.sh +++ b/tools/bionic-runtime/tool/launch.sh @@ -431,7 +431,9 @@ fi # (bionic/dd-directinput), which answer from the pad state DroidDeck keeps in each ring file instead of the queue # of HID reports Wine's xinput reads (a game that reads slower than the pad reports kept moving after the stick # was let go). They go into the prefix as copies, never through the links to this tool's builtins, with Wine's -# xinput1_4 beside them as dd-directinput-wine-xinput.dll for rumble. DD_DIRECTINPUT=0 keeps Wine's own; a game +# xinput1_4 beside them as dd-directinput-wine-xinput.dll for rumble and pass-through. The mode file next to the +# controller folder (DD_DIRECTINPUT_CONTROL, '1' direct / '0' pass-through to Wine's xinput) is read by the DLLs +# while the game runs, so DroidDeck can switch it live; it starts as DD_DIRECTINPUT says (0 = pass-through). A game # no longer set to start without Steam gets Wine's links back. DDI=$TOOL/bionic/dd-directinput ddi_names="xinput1_1 xinput1_2 xinput1_3 xinput1_4 xinput9_1_0 xinputuap" @@ -441,7 +443,7 @@ ddi_put() { # src dst: dst becomes a copy of src; a link there is removed first, } ddi_ours() { [ -f "$1" ] && [ ! -L "$1" ] && grep -qF "dd-directinput: " "$1"; } DDW=$COMPAT/pfx/drive_c/windows -if on "${DROIDDECK_NO_STEAM:-}" && [ "${DD_DIRECTINPUT:-1}" != 0 ] && [ -d "$DDI/arm64x" ] && [ -d "$DDI/i386" ]; then +if on "${DROIDDECK_NO_STEAM:-}" && [ -d "$DDI/arm64x" ] && [ -d "$DDI/i386" ]; then for a in arm64x:system32:aarch64-windows i386:syswow64:i386-windows; do src=$DDI/${a%%:*}; rest=${a#*:}; dir=$DDW/${rest%%:*}; wine=$F/lib/wine/${rest#*:} [ -d "$dir" ] || continue @@ -449,7 +451,14 @@ if on "${DROIDDECK_NO_STEAM:-}" && [ "${DD_DIRECTINPUT:-1}" != 0 ] && [ -d "$DDI ddi_put "$wine/xinput1_4.dll" "$dir/dd-directinput-wine-xinput.dll" done export WINEDLLOVERRIDES="$WINEDLLOVERRIDES;xinput1_1,xinput1_2,xinput1_3,xinput1_4,xinput9_1_0,xinputuap=n,b" - echo "$(date +%H:%M:%S) dd-directinput on (XInput from the pad state)" + ddi_mode=1 ddi_name=direct + [ "${DD_DIRECTINPUT:-1}" = 0 ] && ddi_mode=0 ddi_name=pass-through + if [ -n "${FAKE_EVDEV_DIR:-}" ] && echo "$ddi_mode" > "${FAKE_EVDEV_DIR%/*}/dd-directinput.mode"; then + export DD_DIRECTINPUT_CONTROL="${FAKE_EVDEV_DIR%/*}/dd-directinput.mode" + else + ddi_name="direct (no mode file)" + fi + echo "$(date +%H:%M:%S) dd-directinput on (starts $ddi_name)" else back=0 for a in system32:aarch64-windows syswow64:i386-windows; do diff --git a/tools/tests/test_bionic_paths.py b/tools/tests/test_bionic_paths.py index e48a28d20b..887515d2cd 100644 --- a/tools/tests/test_bionic_paths.py +++ b/tools/tests/test_bionic_paths.py @@ -146,9 +146,10 @@ class DirectInput(unittest.TestCase): on = re.search(r'^on\(\) \{.*\}$', text, re.M).group(0) start = text.index('# dd-directinput: a game set to start') end = text.index("dd-directinput off: Wine's xinput again\"\nfi\n") + len("dd-directinput off: Wine's xinput again\"\nfi\n") - script = on + '\n' + text[start:end] + 'echo "overrides=$WINEDLLOVERRIDES"\n' + script = on + '\n' + text[start:end] + 'echo "overrides=$WINEDLLOVERRIDES"; echo "control=${DD_DIRECTINPUT_CONTROL:-}"\n' + (self.root / 'session/dev/input').mkdir(parents=True, exist_ok=True) base = dict(PATH=os.environ['PATH'], TOOL=str(self.tool), COMPAT=str(self.compat), F=str(self.f), - WINEDLLOVERRIDES='d3d11=n,b') + WINEDLLOVERRIDES='d3d11=n,b', FAKE_EVDEV_DIR=str(self.root / 'session/dev/input')) result = subprocess.run(['sh', '-c', script], env=dict(base, **env), text=True, capture_output=True, check=True) return result.stdout @@ -167,7 +168,10 @@ class DirectInput(unittest.TestCase): self.assertIn('Wine builtin DLL', (self.win / pfx / 'dd-directinput-wine-xinput.dll').read_text()) self.builtins_untouched() self.assertIn('overrides=d3d11=n,b;xinput1_1,xinput1_2,xinput1_3,xinput1_4,xinput9_1_0,xinputuap=n,b', out) - self.assertIn('dd-directinput on', out) + mode = self.root / 'session/dev/dd-directinput.mode' + self.assertEqual('1', mode.read_text().strip()) + self.assertIn(f'control={mode}', out) + self.assertIn('dd-directinput on (starts direct)', out) def test_switched_off_puts_wines_links_back(self): self.run_step(DROIDDECK_NO_STEAM='1') @@ -182,11 +186,16 @@ class DirectInput(unittest.TestCase): self.assertIn('overrides=d3d11=n,b\n', out) self.assertIn("dd-directinput off: Wine's xinput again", out) - def test_dd_directinput_0_keeps_wines_own(self): + def test_dd_directinput_0_starts_in_pass_through(self): out = self.run_step(DROIDDECK_NO_STEAM='1', DD_DIRECTINPUT='0') - self.assertTrue((self.win / 'system32/xinput1_3.dll').is_symlink()) - self.assertIn('overrides=d3d11=n,b\n', out) - self.assertNotIn('dd-directinput on', out) + self.assertFalse((self.win / 'system32/xinput1_3.dll').is_symlink()) + self.assertEqual('0', (self.root / 'session/dev/dd-directinput.mode').read_text().strip()) + self.assertIn('dd-directinput on (starts pass-through)', out) + + def test_without_a_controller_folder_it_stays_direct(self): + text = self.run_step(DROIDDECK_NO_STEAM='1', FAKE_EVDEV_DIR='') + self.assertIn('control=\n', text) + self.assertIn('dd-directinput on (starts direct (no mode file))', text) def test_a_native_xinput_that_is_not_ours_stays(self): t = self.win / 'system32/xinput1_3.dll'