[skip ci] Wait until the user interacts with the page

This commit is contained in:
Nintorch 2026-05-21 22:26:52 +05:00
parent c52bf5188f
commit 9ae773bcbc

View file

@ -140,11 +140,24 @@ static void SDL_WebHID_DisconnectEmscriptenGamepad(int device_index)
static void SDL_RequestWebHIDDevice(Uint16 vendor, Uint16 product, int device_index)
{
MAIN_THREAD_EM_ASM({
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
if ("hid" in navigator) {
async function handler() {
let devices = await navigator["hid"]["requestDevice"]({ "filters": [ { "vendorId": $0, "productId": $1, } ]});
if (devices) {
dynCall("vi", $2, [$3]);
while (true) {
try {
let devices = await navigator["hid"]["requestDevice"]({ "filters": [ { "vendorId": $0, "productId": $1, } ]});
if (devices) {
dynCall("vi", $2, [$3]);
}
return;
} catch(e) {
// Exception, most likely because the user hasn't interacted with the page yet.
// Let's wait until they do, hopefully.
await timeout(500);
}
}
}
handler();