Skip to content

Commit

Permalink
Remove glfw joy <-> HTML5 Gamepad API index translation, use HTML5
Browse files Browse the repository at this point in the history
GLFW_JOYSTICK_1 is 0, so HTML5 Gamepad index 0 maps to GLFW_JOYSTICK_1.
Note: The API subtlety of spreading out glfw joystick IDs is lost.
  • Loading branch information
satoshinm committed May 6, 2017
1 parent 2d0032e commit ed23211
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/library_glfw.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ var LibraryGLFW = {
};
this.buttons = 0;
this.keys = new Array();
this.joyLast = 0; // next available: GLFW_JOYSTICK_1, 2, ...
this.joys = {}; // glfw joystick data, indexed by glfw joy id
this.gamepad2joy = {}; // HTML5 Gamepad API index to glfw joy id
this.joys = {}; // glfw joystick data
this.domKeys = new Array();
this.shouldClose = 0;
this.title = null;
Expand Down Expand Up @@ -386,12 +384,8 @@ var LibraryGLFW = {
},

onGamepadConnected: function(event) {
// HTML5 Gamepad API reuses indexes
// glfw joystick API spreads them out
// http://www.glfw.org/docs/latest/input_guide.html#joystick
// "Once a joystick is detected, it keeps its assigned index until it is disconnected,
// so as joysticks are connected and disconnected, they will become spread out."
var joy = GLFW.active.joyLast++;
var joy = event.gamepad.index;

GLFW.active.joys[joy] = {
index: event.gamepad.index,
id: allocate(intArrayFromString(event.gamepad.id), 'i8', ALLOC_NORMAL),
Expand All @@ -400,15 +394,14 @@ var LibraryGLFW = {
buttons: allocate(new Array(event.gamepad.buttons.length), 'i8', ALLOC_NORMAL),
axes: allocate(new Array(event.gamepad.axes.length), 'float', ALLOC_NORMAL)
};
GLFW.active.gamepad2joy[event.gamepad.index] = joy;

if (GLFW.active.joystickFunc) {
Module['dynCall_vii'](GLFW.active.joystickFunc, joy, 0x00040001); // GLFW_CONNECTED
}
},

onGamepadDisconnected: function(event) {
var joy = GLFW.active.gamepad2joy[event.gamepad.index];
var joy = event.gamepad.index;

if (GLFW.active.joystickFunc) {
Module['dynCall_vii'](GLFW.active.joystickFunc, joy, 0x00040002); // GLFW_DISCONNECTED
Expand All @@ -418,7 +411,6 @@ var LibraryGLFW = {
_free(GLFW.active.joys[joy].buttons);
//_free(GLFW.active.joys[joy].axes); // TODO: fix abort, corrupted memory?

delete GLFW.active.gamepad2joy[event.gamepad.index];
delete GLFW.active.joys[joy];
},

Expand Down

0 comments on commit ed23211

Please sign in to comment.