Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Support a second controller

alan.kungc36767029264
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1420ded..f713761 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -96,7 +96,7 @@ if(PS2_GENERATED)
endif()
target_link_libraries(ps2web PRIVATE ps2game ps2runtime)
target_link_options(ps2web PRIVATE --profiling-funcs -sJSPI -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=268435456 -sPTHREAD_POOL_SIZE=2 -sALLOW_TABLE_GROWTH=1 -lidbfs.js -sSTACK_SIZE=16777216 -sENVIRONMENT=web
- -sEXPORTED_FUNCTIONS=_main,_ps2web_pad,_ps2web_resize,_ps2web_gpu_ms,_ps2web_frames,_ps2web_primitives,_ps2web_audio_ring,_ps2web_audio_queued_ms,_ps2web_idle_ms,_ps2web_vu_interpreted
+ -sEXPORTED_FUNCTIONS=_main,_ps2web_pad,_ps2web_pad_connected,_ps2web_resize,_ps2web_gpu_ms,_ps2web_frames,_ps2web_primitives,_ps2web_audio_ring,_ps2web_audio_queued_ms,_ps2web_idle_ms,_ps2web_vu_interpreted
-sEXPORTED_RUNTIME_METHODS=callMain,addRunDependency,removeRunDependency,HEAPU8,ENV
--shell-file ${CMAKE_SOURCE_DIR}/web/shell.html --preload-file ${PS2_WEB_ELF}@/game/core.elf)
set_target_properties(ps2web PROPERTIES SUFFIX ".html" LINK_DEPENDS ${CMAKE_SOURCE_DIR}/web/shell.html)
diff --git a/host/web.cpp b/host/web.cpp
index cd8bb9e..8b5efe7 100644
--- a/host/web.cpp
+++ b/host/web.cpp
@@ -314,20 +314,22 @@ void *emulate(void *data) {
}
-extern "C" EMSCRIPTEN_KEEPALIVE void ps2web_pad(int buttons, int lx, int ly, int rx, int ry) {
- ps2::set_pad_buttons(static_cast<std::uint16_t>(buttons));
- ps2::set_pad_axis(0, static_cast<std::uint8_t>(rx));
- ps2::set_pad_axis(1, static_cast<std::uint8_t>(ry));
- ps2::set_pad_axis(2, static_cast<std::uint8_t>(lx));
- ps2::set_pad_axis(3, static_cast<std::uint8_t>(ly));
+extern "C" EMSCRIPTEN_KEEPALIVE void ps2web_pad(int pad, int buttons, int lx, int ly, int rx, int ry) {
+ ps2::set_pad_buttons(static_cast<std::uint16_t>(buttons), pad);
+ ps2::set_pad_axis(0, static_cast<std::uint8_t>(rx), pad);
+ ps2::set_pad_axis(1, static_cast<std::uint8_t>(ry), pad);
+ ps2::set_pad_axis(2, static_cast<std::uint8_t>(lx), pad);
+ ps2::set_pad_axis(3, static_cast<std::uint8_t>(ly), pad);
}
-extern "C" EMSCRIPTEN_KEEPALIVE void ps2web_pressure(int a, int b, int c, int d) {
+extern "C" EMSCRIPTEN_KEEPALIVE void ps2web_pad_connected(int pad, int connected) { ps2::set_pad_connected(pad, connected != 0); }
+
+extern "C" EMSCRIPTEN_KEEPALIVE void ps2web_pressure(int pad, int a, int b, int c, int d) {
const int packed[4] = {a, b, c, d};
- for (int i = 0; i < 16; ++i) ps2::set_pad_pressure(i, static_cast<std::uint8_t>(static_cast<unsigned>(packed[i / 4]) >> (i % 4 * 8)));
+ for (int i = 0; i < 16; ++i) ps2::set_pad_pressure(i, static_cast<std::uint8_t>(static_cast<unsigned>(packed[i / 4]) >> (i % 4 * 8)), pad);
}
-extern "C" EMSCRIPTEN_KEEPALIVE int ps2web_rumble() { return ps2::pad_rumble(); }
+extern "C" EMSCRIPTEN_KEEPALIVE int ps2web_rumble(int pad) { return ps2::pad_rumble(pad); }
extern "C" EMSCRIPTEN_KEEPALIVE void ps2web_resize(int width, int height) {
ps2::resize_window(static_cast<std::uint32_t>(width), static_cast<std::uint32_t>(height));
diff --git a/host/window.cpp b/host/window.cpp
index f5e8d58..509d01b 100644
--- a/host/window.cpp
+++ b/host/window.cpp
@@ -56,7 +56,8 @@ SDL_Window *g_window = nullptr;
SDL_Renderer *g_renderer = nullptr;
SDL_Texture *g_texture = nullptr;
SDL_AudioDeviceID g_audio = 0;
-SDL_GameController *g_pad = nullptr;
+// the keyboard drives the first controller port along with the first game controller, a second one is player 2
+SDL_GameController *g_pads[2] = {};
std::uint32_t g_tex_w = 0, g_tex_h = 0;
std::uint64_t g_last = 0;
std::vector<std::uint32_t> g_pixels;
@@ -102,86 +103,114 @@ bool attach_gpu() {
return ps2::attach_window(target);
}
-std::uint8_t axis(SDL_GameControllerAxis a) {
- const int v = SDL_GameControllerGetAxis(g_pad, a);
+std::uint8_t axis(SDL_GameController *pad, SDL_GameControllerAxis a) {
+ const int v = SDL_GameControllerGetAxis(pad, a);
return static_cast<std::uint8_t>(std::clamp((v + 32768) >> 8, 0, 255));
}
-void rumble() {
- static double last_strong = 0.0;
- static bool last_weak = false;
- static Uint32 sent = 0;
- if (!g_pad) return;
- const std::uint16_t motors = ps2::pad_rumble();
+void rumble(int port) {
+ static double last_strong[2] = {};
+ static bool last_weak[2] = {};
+ static Uint32 sent[2] = {};
+ SDL_GameController *pad = g_pads[port];
+ if (!pad) return;
+ const std::uint16_t motors = ps2::pad_rumble(port);
const double strong = (motors >> 8) < 16 ? 0.0 : std::sqrt((motors >> 8) / 255.0);
const bool weak = motors & 1u;
const Uint32 now = SDL_GetTicks();
- if (std::fabs(strong - last_strong) <= 0.08 && weak == last_weak && (!(strong > 0.0 || weak) || now - sent < 100)) return;
- last_strong = strong;
- last_weak = weak;
- sent = now;
- SDL_GameControllerRumble(g_pad, static_cast<Uint16>(strong * 65535.0), weak ? 0xFFFF : 0, strong > 0.0 || weak ? 200 : 0);
+ if (std::fabs(strong - last_strong[port]) <= 0.08 && weak == last_weak[port] && (!(strong > 0.0 || weak) || now - sent[port] < 100)) return;
+ last_strong[port] = strong;
+ last_weak[port] = weak;
+ sent[port] = now;
+ SDL_GameControllerRumble(pad, static_cast<Uint16>(strong * 65535.0), weak ? 0xFFFF : 0, strong > 0.0 || weak ? 200 : 0);
}
-void poll() {
- SDL_Event e;
- while (SDL_PollEvent(&e)) {
- if (e.type == SDL_QUIT) ps2::halt("window closed");
- if (e.type == SDL_KEYDOWN && e.key.keysym.scancode == SDL_SCANCODE_F11 && !e.key.repeat) {
- const bool full = SDL_GetWindowFlags(g_window) & SDL_WINDOW_FULLSCREEN;
- SDL_SetWindowFullscreen(g_window, full ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
- SDL_ShowCursor(full ? SDL_ENABLE : SDL_DISABLE);
- }
- if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED && g_gpu_present) {
- int w = 0, h = 0;
- SDL_GetWindowSizeInPixels(g_window, &w, &h);
- ps2::resize_window(static_cast<std::uint32_t>(w), static_cast<std::uint32_t>(h));
- }
- if (e.type == SDL_CONTROLLERDEVICEADDED && !g_pad) g_pad = SDL_GameControllerOpen(e.cdevice.which);
- if (e.type == SDL_CONTROLLERDEVICEREMOVED && g_pad && e.cdevice.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(g_pad))) {
- SDL_GameControllerClose(g_pad);
- g_pad = nullptr;
+// a new controller takes the first free port, the second one plugs in player 2
+void open_pad(int device) {
+ const SDL_JoystickID id = SDL_JoystickGetDeviceInstanceID(device);
+ for (SDL_GameController *pad : g_pads)
+ if (pad && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(pad)) == id) return;
+ for (int port = 0; port < 2; ++port)
+ if (!g_pads[port]) {
+ g_pads[port] = SDL_GameControllerOpen(device);
+ return;
}
+}
+
+void poll_port(int port) {
+ static bool plugged[2] = {};
+ SDL_GameController *pad = g_pads[port];
+ if (port > 0 && plugged[port] != (pad != nullptr)) {
+ plugged[port] = pad != nullptr;
+ ps2::set_pad_connected(port, plugged[port]);
}
- const Uint8 *keys = SDL_GetKeyboardState(nullptr);
std::uint16_t pressed = 0;
- for (const KeyBinding &b : kKeys)
- if (keys[b.key]) pressed |= b.button;
std::uint8_t l2 = 0xFF, r2 = 0xFF;
std::uint8_t lx = 0x80, ly = 0x80, rx = 0x80, ry = 0x80;
- if (keys[SDL_SCANCODE_LEFT]) lx = 0x00;
- if (keys[SDL_SCANCODE_RIGHT]) lx = 0xFF;
- if (keys[SDL_SCANCODE_UP]) ly = 0x00;
- if (keys[SDL_SCANCODE_DOWN]) ly = 0xFF;
- if (g_pad) {
+ if (port == 0) {
+ const Uint8 *keys = SDL_GetKeyboardState(nullptr);
+ for (const KeyBinding &b : kKeys)
+ if (keys[b.key]) pressed |= b.button;
+ if (keys[SDL_SCANCODE_LEFT]) lx = 0x00;
+ if (keys[SDL_SCANCODE_RIGHT]) lx = 0xFF;
+ if (keys[SDL_SCANCODE_UP]) ly = 0x00;
+ if (keys[SDL_SCANCODE_DOWN]) ly = 0xFF;
+ }
+ if (pad) {
for (const PadBinding &b : kPadButtons)
- if (SDL_GameControllerGetButton(g_pad, b.button)) pressed |= b.mask;
+ if (SDL_GameControllerGetButton(pad, b.button)) pressed |= b.mask;
const auto trigger = [&](SDL_GameControllerAxis a, std::uint16_t bit, std::uint8_t &pressure) {
- const int v = SDL_GameControllerGetAxis(g_pad, a);
+ const int v = SDL_GameControllerGetAxis(pad, a);
if (v <= 2000 || (pressed & bit)) return;
pressed |= bit;
pressure = static_cast<std::uint8_t>(std::max(1, v * 255 / 32767));
};
trigger(SDL_CONTROLLER_AXIS_TRIGGERLEFT, kL2, l2);
trigger(SDL_CONTROLLER_AXIS_TRIGGERRIGHT, kR2, r2);
- if (lx == 0x80) lx = axis(SDL_CONTROLLER_AXIS_LEFTX);
- if (ly == 0x80) ly = axis(SDL_CONTROLLER_AXIS_LEFTY);
- rx = axis(SDL_CONTROLLER_AXIS_RIGHTX);
- ry = axis(SDL_CONTROLLER_AXIS_RIGHTY);
+ if (lx == 0x80) lx = axis(pad, SDL_CONTROLLER_AXIS_LEFTX);
+ if (ly == 0x80) ly = axis(pad, SDL_CONTROLLER_AXIS_LEFTY);
+ rx = axis(pad, SDL_CONTROLLER_AXIS_RIGHTX);
+ ry = axis(pad, SDL_CONTROLLER_AXIS_RIGHTY);
}
- rumble();
- static std::uint64_t last = ~0ull;
+ rumble(port);
+ static std::uint64_t last[2] = {~0ull, ~0ull};
const std::uint64_t state = pressed | (static_cast<std::uint64_t>(rx) << 16) | (static_cast<std::uint64_t>(ry) << 24) | (static_cast<std::uint64_t>(lx) << 32) |
(static_cast<std::uint64_t>(ly) << 40) | (static_cast<std::uint64_t>(l2) << 48) | (static_cast<std::uint64_t>(r2) << 56);
- if (state == last) return;
- last = state;
- ps2::set_pad_pressure(8, l2);
- ps2::set_pad_pressure(9, r2);
- ps2::set_pad_buttons(pressed);
- ps2::set_pad_axis(0, rx);
- ps2::set_pad_axis(1, ry);
- ps2::set_pad_axis(2, lx);
- ps2::set_pad_axis(3, ly);
+ if (state == last[port]) return;
+ last[port] = state;
+ ps2::set_pad_pressure(8, l2, port);
+ ps2::set_pad_pressure(9, r2, port);
+ ps2::set_pad_buttons(pressed, port);
+ ps2::set_pad_axis(0, rx, port);
+ ps2::set_pad_axis(1, ry, port);
+ ps2::set_pad_axis(2, lx, port);
+ ps2::set_pad_axis(3, ly, port);
+}
+
+void poll() {
+ SDL_Event e;
+ while (SDL_PollEvent(&e)) {
+ if (e.type == SDL_QUIT) ps2::halt("window closed");
+ if (e.type == SDL_KEYDOWN && e.key.keysym.scancode == SDL_SCANCODE_F11 && !e.key.repeat) {
+ const bool full = SDL_GetWindowFlags(g_window) & SDL_WINDOW_FULLSCREEN;
+ SDL_SetWindowFullscreen(g_window, full ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
+ SDL_ShowCursor(full ? SDL_ENABLE : SDL_DISABLE);
+ }
+ if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED && g_gpu_present) {
+ int w = 0, h = 0;
+ SDL_GetWindowSizeInPixels(g_window, &w, &h);
+ ps2::resize_window(static_cast<std::uint32_t>(w), static_cast<std::uint32_t>(h));
+ }
+ if (e.type == SDL_CONTROLLERDEVICEADDED) open_pad(e.cdevice.which);
+ if (e.type == SDL_CONTROLLERDEVICEREMOVED)
+ for (SDL_GameController *&pad : g_pads)
+ if (pad && e.cdevice.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(pad))) {
+ SDL_GameControllerClose(pad);
+ pad = nullptr;
+ }
+ }
+ poll_port(0);
+ poll_port(1);
}
void show_pixels(const std::uint32_t *rgba, std::uint32_t w, std::uint32_t h) {
@@ -301,8 +330,8 @@ bool open_window(bool fullscreen) {
SDL_PauseAudioDevice(g_audio, 0);
ps2::set_audio_sink(queue_audio);
}
- for (int i = 0; i < SDL_NumJoysticks() && !g_pad; ++i)
- if (SDL_IsGameController(i)) g_pad = SDL_GameControllerOpen(i);
+ for (int i = 0; i < SDL_NumJoysticks(); ++i)
+ if (SDL_IsGameController(i)) open_pad(i);
if (fullscreen) SDL_ShowCursor(SDL_DISABLE);
return true;
}
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index 0eb5552..49f4272 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -111,10 +111,12 @@ double gpu_busy_ms();
void browser_yield();
std::uint64_t frames_run();
std::uint64_t primitives_drawn();
-void set_pad_buttons(std::uint16_t pressed);
-void set_pad_axis(int axis, std::uint8_t value);
-void set_pad_pressure(int button, std::uint8_t value);
-std::uint16_t pad_rumble();
+// pad 0 is the first controller port, pad 1 the second, which starts unplugged
+void set_pad_buttons(std::uint16_t pressed, int pad = 0);
+void set_pad_axis(int axis, std::uint8_t value, int pad = 0);
+void set_pad_pressure(int button, std::uint8_t value, int pad = 0);
+void set_pad_connected(int pad, bool connected);
+std::uint16_t pad_rumble(int pad = 0);
void set_audio_sink(std::function<void(const std::int16_t *samples, std::size_t frames)> sink);
}
diff --git a/runtime/src/iop/sio2.cpp b/runtime/src/iop/sio2.cpp
index 60adac5..4ca10f2 100644
--- a/runtime/src/iop/sio2.cpp
+++ b/runtime/src/iop/sio2.cpp
@@ -62,6 +62,8 @@ std::uint32_t g_index = 0;
std::uint32_t g_stat = 0;
std::deque<std::uint8_t> g_in, g_out;
Pad g_pads[2];
+// which ports the host has a controller in; kept out of save states so loading one does not unplug a pad
+bool g_plugged[2] = {true, false};
Feel g_feel[2];
Card g_card;
Dma g_dma[2];
@@ -310,7 +312,8 @@ void reset() {
g_out.clear();
g_pads[0] = Pad{};
g_pads[1] = Pad{};
- g_pads[0].connected = true;
+ g_pads[0].connected = g_plugged[0];
+ g_pads[1].connected = g_plugged[1];
for (Feel &f : g_feel) {
std::fill(std::begin(f.motors), std::end(f.motors), 0xFF);
f.rumble.store(0);
@@ -400,6 +403,10 @@ void set_pressure(int pad, int button, std::uint8_t value) {
if (pad >= 0 && pad < 2 && button >= 0 && button < 16) g_feel[pad].pressure[button] = value;
}
+void set_connected(int pad, bool connected) {
+ if (pad >= 0 && pad < 2) g_pads[pad].connected = g_plugged[pad] = connected;
+}
+
std::uint16_t rumble(int pad) { return pad >= 0 && pad < 2 ? g_feel[pad].rumble.load(std::memory_order_relaxed) : 0; }
}
@@ -416,11 +423,14 @@ void state(ps2::state::Stream &s) {
s.deque(g_in);
s.deque(g_out);
s.pod(g_pads);
- if (s.loading())
+ if (s.loading()) {
+ g_pads[0].connected = g_plugged[0];
+ g_pads[1].connected = g_plugged[1];
for (Feel &f : g_feel) {
std::copy(std::begin(kLibpadMotors), std::end(kLibpadMotors), f.motors);
f.rumble.store(0);
}
+ }
s.pod(g_dma);
s.size(g_card.address);
s.pod(g_card.term);
diff --git a/runtime/src/iop/sio2.hpp b/runtime/src/iop/sio2.hpp
index 262e7a7..1068dad 100644
--- a/runtime/src/iop/sio2.hpp
+++ b/runtime/src/iop/sio2.hpp
@@ -17,6 +17,7 @@ void arm_dma(int channel, std::uint32_t madr, std::uint32_t block_words, std::ui
void set_buttons(int pad, std::uint16_t pressed_mask);
void set_axis(int pad, int axis, std::uint8_t value);
void set_pressure(int pad, int button, std::uint8_t value);
+void set_connected(int pad, bool connected);
std::uint16_t rumble(int pad);
void flush_cards();
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index ca8f06c..e87ee66 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -522,10 +522,11 @@ void browser_yield() {}
std::uint64_t frames_run() { return g_frames; }
std::uint64_t primitives_drawn() { return gs::primitives_drawn(); }
-void set_pad_buttons(std::uint16_t pressed) { iop::sio2::set_buttons(0, pressed); }
-void set_pad_axis(int axis, std::uint8_t value) { iop::sio2::set_axis(0, axis, value); }
-void set_pad_pressure(int button, std::uint8_t value) { iop::sio2::set_pressure(0, button, value); }
-std::uint16_t pad_rumble() { return iop::sio2::rumble(0); }
+void set_pad_buttons(std::uint16_t pressed, int pad) { iop::sio2::set_buttons(pad, pressed); }
+void set_pad_axis(int axis, std::uint8_t value, int pad) { iop::sio2::set_axis(pad, axis, value); }
+void set_pad_pressure(int button, std::uint8_t value, int pad) { iop::sio2::set_pressure(pad, button, value); }
+void set_pad_connected(int pad, bool connected) { iop::sio2::set_connected(pad, connected); }
+std::uint16_t pad_rumble(int pad) { return iop::sio2::rumble(pad); }
void set_audio_sink(std::function<void(const std::int16_t *, std::size_t)> sink) { iop::spu2::set_sink(std::move(sink)); }
}
diff --git a/web/shell.html b/web/shell.html
index 7931df0..51db819 100644
--- a/web/shell.html
+++ b/web/shell.html
@@ -395,60 +395,72 @@ window.addEventListener('keyup', (e) => { keys.delete(e.code); });
const padButtons = [[0, 14], [1, 13], [2, 15], [3, 12], [4, 10], [5, 11], [6, 8], [7, 9], [8, 0], [9, 3], [10, 1], [11, 2],
[12, 4], [13, 6], [14, 7], [15, 5]];
const axis = (v) => Math.max(0, Math.min(255, Math.round((v + 1) * 127.5)));
-let lastPad = -1, lastPressure = '', lastStrong = 0, lastWeak = 0, rumbleAt = 0;
+// the keyboard and touch controls are player 1 along with the first gamepad, a second gamepad is player 2
+const players = [0, 1].map(() => ({ plugged: false, pad: -1, pressure: '', strong: 0, weak: 0, rumbleAt: 0 }));
const motorStrength = (v) => v < 16 ? 0 : Math.sqrt(v / 255);
-function pollInput() {
- let buttons = 0, lx = 128, ly = 128, rx = 128, ry = 128, gamepad = null;
+function pollPlayer(port, gamepad, own) {
+ const player = players[port];
+ const plugged = port === 0 || !!gamepad;
+ if (plugged !== player.plugged) {
+ player.plugged = plugged;
+ Module._ps2web_pad_connected(port, plugged ? 1 : 0);
+ }
+ if (!plugged) return;
+ let buttons = 0, lx = 128, ly = 128, rx = 128, ry = 128;
const pressure = new Array(16).fill(255);
- buttons |= touchPad.buttons;
- for (const k of keys) if (k in keyMap) buttons |= 1 << keyMap[k];
- const full = buttons;
- if (keys.has('KeyJ')) lx = 0;
- if (keys.has('KeyL')) lx = 255;
- if (keys.has('KeyI')) ly = 0;
- if (keys.has('KeyK')) ly = 255;
- if (touchPad.stick) {
- lx = axis(touchPad.x);
- ly = axis(touchPad.y);
+ if (own) {
+ buttons |= touchPad.buttons;
+ for (const k of keys) if (k in keyMap) buttons |= 1 << keyMap[k];
+ if (keys.has('KeyJ')) lx = 0;
+ if (keys.has('KeyL')) lx = 255;
+ if (keys.has('KeyI')) ly = 0;
+ if (keys.has('KeyK')) ly = 255;
+ if (touchPad.stick) {
+ lx = axis(touchPad.x);
+ ly = axis(touchPad.y);
+ }
}
- for (const pad of navigator.getGamepads ? navigator.getGamepads() : []) {
- if (!pad) continue;
- gamepad = pad;
+ const full = buttons;
+ if (gamepad) {
for (const [index, bit] of padButtons) {
- const b = pad.buttons[index];
+ const b = gamepad.buttons[index];
if (!b || !(b.pressed || b.value > 0.05)) continue;
buttons |= 1 << bit;
if (!(full & (1 << bit))) pressure[bit] = Math.max(1, Math.round((b.value > 0 ? b.value : 1) * 255));
}
- if (pad.axes.length >= 4) {
- if (lx === 128) lx = axis(pad.axes[0]);
- if (ly === 128) ly = axis(pad.axes[1]);
- rx = axis(pad.axes[2]);
- ry = axis(pad.axes[3]);
+ if (gamepad.axes.length >= 4) {
+ if (lx === 128) lx = axis(gamepad.axes[0]);
+ if (ly === 128) ly = axis(gamepad.axes[1]);
+ rx = axis(gamepad.axes[2]);
+ ry = axis(gamepad.axes[3]);
}
- break;
}
const state = buttons + lx * 65536 + ly * 16777216 + rx * 4294967296 + ry * 1099511627776;
- if (state !== lastPad) {
- lastPad = state;
- Module._ps2web_pad(buttons, lx, ly, rx, ry);
+ if (state !== player.pad) {
+ player.pad = state;
+ Module._ps2web_pad(port, buttons, lx, ly, rx, ry);
}
const packed = [0, 1, 2, 3].map((i) => pressure[i * 4] | (pressure[i * 4 + 1] << 8) | (pressure[i * 4 + 2] << 16) | (pressure[i * 4 + 3] << 24));
- if (packed.join() !== lastPressure) {
- lastPressure = packed.join();
- Module._ps2web_pressure(packed[0], packed[1], packed[2], packed[3]);
+ if (packed.join() !== player.pressure) {
+ player.pressure = packed.join();
+ Module._ps2web_pressure(port, packed[0], packed[1], packed[2], packed[3]);
}
- const rumble = Module._ps2web_rumble(), now = performance.now();
+ const rumble = Module._ps2web_rumble(port), now = performance.now();
const strong = motorStrength(rumble >> 8), weak = rumble & 1;
const actuator = gamepad && gamepad.vibrationActuator;
- if (actuator && (Math.abs(strong - lastStrong) > 0.08 || weak !== lastWeak || ((strong || weak) && now - rumbleAt > 100))) {
- lastStrong = strong;
- lastWeak = weak;
- rumbleAt = now;
+ if (actuator && (Math.abs(strong - player.strong) > 0.08 || weak !== player.weak || ((strong || weak) && now - player.rumbleAt > 100))) {
+ player.strong = strong;
+ player.weak = weak;
+ player.rumbleAt = now;
const done = strong || weak ? actuator.playEffect('dual-rumble', { duration: 200, weakMagnitude: weak, strongMagnitude: strong }) : actuator.reset();
if (done && done.catch) done.catch(() => {});
}
}
+function pollInput() {
+ const pads = [...(navigator.getGamepads ? navigator.getGamepads() : [])].filter((pad) => pad);
+ pollPlayer(0, pads[0] || null, true);
+ pollPlayer(1, pads[1] || null, false);
+}
// some browsers cannot show a canvas drawn by another GPU (hybrid laptops on Linux), so each adapter draws a few test frames first
async function canPresent(power) {