satoshinm / NetCraft

Web-based fork of fogleman/Craft ⛺
https://satoshinm.github.io/NetCraft/
MIT License
57 stars 13 forks source link

[Joy] Chrome detects more gamepads when disconnected #94

Closed satoshinm closed 7 years ago

satoshinm commented 7 years ago

Load up NetCraft in Google Chrome, with the controller off. Turn it on, press a button, it is detected (both standard and empty mapping):

ongamepadconnected about to call joystickfunc for  0
craft.html:1233 Joystick 0 connected: Nimbus (Vendor: 0111 Product: 1420)
craft.html:1233 Joystick axes: 8, buttons: 0
craft.js:3083 ongamepadconnected about to call joystickfunc for  1
craft.html:1233 Joystick 1 connected: Nimbus (STANDARD GAMEPAD)
craft.html:1233 Joystick axes: 4, buttons: 16

then turn it off. Two new joysticks connect on disconnect(?):

ongamepadconnected about to call joystickfunc for  2
craft.html:1233 Joystick 2 connected: Nimbus (Vendor: 0111 Product: 1420)
craft.html:1233 Joystick axes: 8, buttons: 0
craft.js:3083 ongamepadconnected about to call joystickfunc for  3
craft.html:1233 Joystick 3 connected: Nimbus (STANDARD GAMEPAD)
craft.html:1233 Joystick axes: 4, buttons: 16

These are correctly detected as disconnected on Safari TP 29. Why is Chrome firing ongamepadconnected instead of ongamepaddisconnected?

The test site http://html5gamepad.com/ correctly detects the gamepad disconnections, so check what they are doing. https://github.com/bwiklund/gamepad.js

satoshinm commented 7 years ago

https://github.com/bwiklund/gamepad.js/blob/master/source/javascripts/all.js.coffee doesn't use gamepadconnected/disconnected events, instead, it polls navigator.getGamepads() in requestAnimationFrame().

Maybe use gamepadconnected as a hint and check the getGamepads() array for truth.


https://github.com/kripken/emscripten/pull/4292/files shows how to do it:

// For previously disconnected gamepads there should be an empty slot (null/undefined/false) at the index.

satoshinm commented 7 years ago

Fixing in https://github.com/satoshinm/emscripten/commit/608e1bb9cb860ab51fc95a8469a740c72d213d46 Change to sample gamepad data once per frame for glfw, ala GH-4292