-
Notifications
You must be signed in to change notification settings - Fork 17
/
manager.ts
385 lines (345 loc) · 9.3 KB
/
manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { GamepadListener } from 'gamepad.js'
/**
* Possible events from GamepadListener
* https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API
*/
export enum EventType {
Unknown = 'unknown',
Connected = 'connected',
Disconnected = 'disconnected',
Axis = 'axis',
Button = 'button',
}
/**
* Supported joystick models
*/
export enum JoystickModel {
DualSense = 'DualSense (PS5)',
DualShock4 = 'DualShock (PS4)',
XboxOne_Wireless = 'Xbox One Wireless Controller',
XboxOneS_Bluetooth = 'Xbox One S (bluetooth)',
XboxController_Bluetooth = 'Xbox controller (bluetooth)',
XboxController_Wired = 'Xbox controller (wired)',
XboxController_360 = 'Xbox 360 controller',
LogitechExtreme3DPro = 'Logitech Extreme 3D Pro',
IpegaPG9023 = 'Ipega PG-9023',
Unknown = 'Unknown Joystick Model',
}
const JoystickMapVidPid: Map<string, JoystickModel> = new Map([
// Sony
['054c:0ce6', JoystickModel.DualSense],
['054c:09cc', JoystickModel.DualShock4],
['045e:02e0', JoystickModel.XboxOne_Wireless],
['045e:02fd', JoystickModel.XboxOneS_Bluetooth],
['045e:0b13', JoystickModel.XboxController_Bluetooth],
['045e:0b12', JoystickModel.XboxController_Wired],
['28de:11ff', JoystickModel.XboxController_360],
['046d:c215', JoystickModel.LogitechExtreme3DPro],
['1949:0402', JoystickModel.IpegaPG9023],
])
// Necessary to add functions
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace EventType {
/**
* Return a list of possible events for gamepad api
* @returns {Array<string>}
*/
export function events(): Array<string> {
return Object.keys(EventType).map((name) => name.toLowerCase())
}
/**
* Return enum value from Gamepad API event
* @param {string} type
* @returns {EventType}
*/
export function fromGamepadEventType(type: string): EventType {
const fields = type.split(':')
if (fields.length <= 1) {
return EventType.Unknown
}
const name = fields[1]
for (const eventName of EventType.events()) {
if (eventName == name) {
return eventName as EventType
}
}
return EventType.Unknown
}
}
// From GamePad API
type GamepadEvent = {
/**
* Event name
*/
type: string
/**
* Generic object that describes event
*/
detail: unknown
}
// Encapsulate joystick event values
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace JoystickDetail {
/**
* Stick values
*/
export enum Stick {
Left = 0,
Right = 1,
}
/**
* Axis values
*/
export enum Axis {
Horizontal = 0,
Vertical = 1,
}
}
export type JoystickAxisEvent = {
/**
* Event type
*/
type: EventType.Axis
/**
* Detail information about the event
*/
detail: {
/**
* Joystick index
*/
index: number
/**
* Gamepad object
*/
gamepad: Gamepad
/**
* Stick position
*/
stick: JoystickDetail.Stick
/**
* Axis type
*/
axis: JoystickDetail.Axis
/**
* Axis value
*/
value: number
}
}
export type JoystickButtonEvent = {
/**
* Event type
*/
type: EventType.Button
/**
* Detail information about the event
*/
detail: {
/**
* Joystick index
*/
index: number
/**
* Gamepad object
*/
gamepad: Gamepad
/**
* Button number
*/
button: number
/**
* Pressing state
*/
pressed: boolean
/**
* Axis value
*/
value: number
}
}
export type JoysticConnectEvent = {
/**
* Event type
*/
type: EventType.Connected
/**
* Detail information about the event
*/
detail: {
/**
* Joystick index
*/
index: number
/**
* Gamepad object
*/
gamepad: Gamepad
}
}
export type JoysticDisconnectEvent = {
/**
* Event type
*/
type: EventType.Disconnected
/**
* Detail information about the event
*/
detail: {
/**
* Joystick index
*/
index: number
}
}
export type JoystickEvent = JoystickAxisEvent | JoystickButtonEvent | JoysticConnectEvent | JoysticDisconnectEvent
type callbackJoystickStateEventType = (event: JoystickEvent) => void
type callbackJoystickEventType = (event: Map<number, Gamepad>) => void
/**
* Joystick Manager
* Abstraction over Gamepad API
*/
class JoystickManager {
private static instance = new JoystickManager()
private callbacksJoystick: Array<callbackJoystickEventType> = []
private callbacksJoystickState: Array<callbackJoystickStateEventType> = []
private gamepadListener = new GamepadListener()
private joysticks: Map<number, Gamepad> = new Map()
// We need to safe in settings
private enabledJoysticks: Array<string> = []
/**
* Singleton constructor
*/
private constructor() {
this.gamepadListener.start()
this.connectEvents()
}
/**
* Singleton access
* @returns {JoystickManager}
*/
static self(): JoystickManager {
return JoystickManager.instance
}
/**
* Callback to be used and receive joystick updates
* @param {callbackJoystickEventType} callback
*/
onJoystickUpdate(callback: callbackJoystickEventType): void {
this.callbacksJoystick.push(callback)
}
/**
* Get Vendor ID and Product ID from joystick
* @param {Gamepad} gamepad Object
* @returns {'vendor_id: string | undefined, product_id: string | undefined'} VID and PID
*/
private getVidPid(gamepad: Gamepad): {
vendor_id: string | undefined // eslint-disable-line
product_id: string | undefined // eslint-disable-line
} {
const joystick_information = gamepad.id
const vendor_regex = new RegExp('Vendor: (?<vendor_id>[0-9a-f]{4})')
const product_regex = new RegExp('Product: (?<product_id>[0-9a-f]{4})')
const vendor_id = vendor_regex.exec(joystick_information)?.groups?.vendor_id
const product_id = product_regex.exec(joystick_information)?.groups?.product_id
return { vendor_id, product_id }
}
/**
* Get joystick model
* @param {Gamepad} gamepad Object
* @returns {JoystickModel} Joystick model
*/
getModel(gamepad: Gamepad): JoystickModel {
const { vendor_id, product_id } = this.getVidPid(gamepad)
if (vendor_id == undefined || product_id == undefined) {
return JoystickModel.Unknown
}
return JoystickMapVidPid.get(`${vendor_id}:${product_id}`) ?? JoystickModel.Unknown
}
/**
* Request user for joystick HID access
* @param {Gamepad} gamepad object
*/
private getHID(gamepad: Gamepad): void {
// The objective of this function is to get serial information and track the configuration
// for a specific joystick, including calibration.
// Electron API appears to be not working: https://www.electronjs.org/docs/latest/api/structures/hid-device
// W3C productName may help but it's not working: https://wicg.github.io/webhid/
// For both serialInformation is not available: https://github.com/w3c/gamepad/issues/73
const { vendor_id, product_id } = this.getVidPid(gamepad)
console.debug(`Joystick: ${gamepad.id} (${vendor_id}:${product_id})`)
if (vendor_id == undefined || product_id == undefined) {
return
}
// Enable joysticks to be used
// Needs to be more adopted by the browsers
/*
navigator.hid
.requestDevice({
filters: [
{
vendorId: parseInt(vendor_id, 16),
productID: parseInt(product_id, 16),
},
],
})
.then((devices: Array<string>) => {
if (devices.length != 0) {
this.enabledJoysticks.push(joystick_information)
}
})
*/
}
/**
* Process joystick event internally
* @param {JoystickEvent} event
*/
private processJoystickUpdate(event: JoystickEvent): void {
const index = event.detail.index
if (event.type == EventType.Connected) {
const gamepad = event.detail.gamepad
if (!this.joysticks.has(index)) {
this.getHID(gamepad)
this.enabledJoysticks.push(gamepad.id)
}
this.joysticks.set(index, gamepad)
} else {
// Disconnect
this.joysticks.delete(index)
}
for (const callback of this.callbacksJoystick) {
callback(this.joysticks)
}
}
/**
* Register joystick event callback
* @param {callbackJoystickStateEventType} callback
*/
onJoystickStateUpdate(callback: callbackJoystickStateEventType): void {
this.callbacksJoystickState.push(callback)
}
/**
* Connect necessary events with callbacks
*/
private connectEvents(): void {
for (const name of EventType.events()) {
this.gamepadListener.on(`gamepad:${name}`, (event: GamepadEvent) => {
const joystickEvent = event as JoystickEvent
const typeEnum = EventType.fromGamepadEventType(joystickEvent.type)
if (typeEnum === EventType.Unknown) return
joystickEvent.type = typeEnum
if (joystickEvent.type === EventType.Connected || joystickEvent.type === EventType.Disconnected) {
this.processJoystickUpdate(joystickEvent)
return
}
if (!this.enabledJoysticks.includes(joystickEvent.detail.gamepad.id)) {
console.warn(`Joystick is not enabled: ${joystickEvent.detail.gamepad.id}`)
return
}
for (const callback of this.callbacksJoystickState) {
callback(joystickEvent)
}
})
}
}
}
export const joystickManager = JoystickManager.self()