-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.html
2271 lines (2259 loc) · 81.4 KB
/
index.html
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Gamepad
</title>
<meta charset="utf-8"><!--
=== NOTA BENE ===
For the three scripts below, if your spec resides on dev.w3 you can check them
out in the same tree and use relative links so that they'll work offline,
-->
<script src='https://www.w3.org/Tools/respec/respec-w3c' class=
'remove'></script>
<script class='remove'>
var respecConfig = {
previousMaturity: "FPWD",
shortName: "gamepad",
// specification status (e.g. WD, LCWD, NOTE, etc.). If in doubt use ED.
specStatus: "ED",
// if your specification has a subtitle that goes below the main
// formal title, define it here
// subtitle : "an excellent document",
// if you wish the publication date to be other than today, set this
//publishDate: "2011-01-01",
// if the specification's copyright date is a range of years, specify
// the start date here:
// copyrightStart: "2005"
// if there is a previously published draft, uncomment this and set its YYYY-MM-DD date
// and its maturity status
testSuiteURI: "https://wpt.live/gamepad/",
// if this is a LCWD, uncomment and set the end of its review period
// lcEnd: "2009-08-05",
// editors, add as many as you like
// only "name" is required
editors: [
{ name: "Steve Agoston", w3cid: "103815",
company: "Sony", companyURL: "https://www.sony.com/"},
{ name: "Matt Reynolds", w3cid: "105511",
company: "Google", companyURL: "https://www.google.com/"},
],
formerEditors: [
{ name: "James Hollyer", w3cid: 115233,
company: "Google", companyURL: "https://www.google.com/"},
{ name: "Brandon Jones", url: "http://tojicode.com/",
company: "Google", companyURL: "http://www.google.com/",
w3cid: 87824 },
{ name: "Scott Graham", url: "http://h4ck3r.net/",
company: "Google", companyURL: "https://www.google.com/",
w3cid: 49028 },
{ name: "Ted Mielczarek", url: "http://ted.mielczarek.org/",
company: "Mozilla", companyURL: "http://www.mozilla.org/",
w3cid: 49656 },
],
github: "w3c/gamepad",
group: "webapps",
implementationReportURI: "https://wpt.fyi/results/gamepad",
caniuse: "gamepad",
xref: ["HTML", "DOM", "PERMISSIONS-POLICY", "HR-TIME", "Infra"],
};
</script>
</head>
<body data-cite="">
<section id='abstract'>
<p>
The Gamepad specification defines a low-level interface that represents
gamepad devices.
</p>
</section>
<section id='sotd'>
<p>
This is a work in progress.
</p>
</section>
<section id='introduction' class='informative'>
<h2>
Introduction
</h2>
<p>
Some [=user agent=]s have connected gamepad devices. These devices are
desirable and suited to input for gaming applications, and for "10
foot" user interfaces (presentations, media viewers).
</p>
<p>
Currently, the only way for a gamepad to be used as input would be to
emulate mouse or keyboard events, however this would lose information
and require additional software outside of the [=user agent=] to
accomplish emulation.
</p>
<p>
Meanwhile, native applications are capable of accessing these devices
via system APIs.
</p>
<p>
The Gamepad API provides a solution to this problem by specifying
interfaces that allow web applications to directly act on gamepad data.
</p>
</section>
<section>
<h2>
Scope
</h2>
<p>
Interfacing with external devices designed to control games has the
potential to become large and intractable if approached in full
generality. In this specification we explicitly choose to narrow scope
to provide a useful subset of functionality that can be widely
implemented and broadly useful.
</p>
<p>
Specifically, we choose to only support the functionality required to
support gamepads. Support for gamepads requires two input types:
buttons and axes. Both buttons and axes are reported as analog values,
buttons ranging from [0..1], and axes ranging from [-1..1].
</p>
<p>
While the primary goal is support for gamepad devices, supporting these
two types of analog inputs allows support for other similar devices
common to current gaming systems including joysticks, driving wheels,
pedals, and accelerometers. As such, the name "gamepad" is exemplary
rather than trying to be a generic name for the entire set of devices
addressed by this specification.
</p>
<p>
We specifically exclude support for more complex devices that may also
be used in some gaming contexts, including those that that do motion
sensing, depth sensing, video analysis, gesture recognition, and so on.
</p>
</section>
<section data-dfn-for="Gamepad" data-link-for="Gamepad">
<h2>
<dfn>Gamepad</dfn> interface
</h2>
<p>
This interface defines an individual gamepad device.
</p>
<pre class="idl" data-cite="HR-TIME">
[Exposed=Window]
interface Gamepad {
readonly attribute DOMString id;
readonly attribute long index;
readonly attribute boolean connected;
readonly attribute DOMHighResTimeStamp timestamp;
readonly attribute GamepadMappingType mapping;
readonly attribute FrozenArray<double> axes;
readonly attribute FrozenArray<GamepadButton> buttons;
[SameObject] readonly attribute GamepadHapticActuator vibrationActuator;
};
</pre>
<p>
The algorithms used to communicate with the system typically complete
asynchronously, queuing work on the <dfn>gamepad task source</dfn>.
</p>
<p>
Instances of {{Gamepad}} are created with the internal slots described
in the following table:
</p>
<table class="simple">
<tr>
<th>
Internal slot
</th>
<th>
Initial value
</th>
<th>
Description (non-normative)
</th>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\connected]]</dfn>
</td>
<td>
`false`
</td>
<td>
A flag indicating that the device is connected to the system
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\timestamp]]</dfn>
</td>
<td>
undefined
</td>
<td>
The last time data for this {{Gamepad}} was updated
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\axes]]</dfn>
</td>
<td>
An empty [=sequence=]
</td>
<td>
A [=sequence=] of {{double}} values representing the current state
of axes exposed by this device
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\buttons]]</dfn>
</td>
<td>
An empty [=sequence=]
</td>
<td>
A [=sequence=] of {{GamepadButton}} objects representing the
current state of buttons exposed by this device
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\exposed]]</dfn>
</td>
<td>
`false`
</td>
<td>
A flag indicating that the {{Gamepad}} object has been exposed to
script
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\axisMapping]]</dfn>
</td>
<td>
An empty [=ordered map=]
</td>
<td>
Mapping from unmapped axis index to an index in the
{{Gamepad/axes}} array
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\axisMinimums]]</dfn>
</td>
<td>
An empty [=list=]
</td>
<td>
A [=list=] containing the minimum logical value for each axis
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\axisMaximums]]</dfn>
</td>
<td>
An empty [=list=]
</td>
<td>
A [=list=] containing the maximum logical value for each axis
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\buttonMapping]]</dfn>
</td>
<td>
An empty [=ordered map=]
</td>
<td>
Mapping from unmapped button index to an index in the
{{Gamepad/buttons}} array
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\buttonMinimums]]</dfn>
</td>
<td>
An empty [=list=]
</td>
<td>
A [=list=] containing the minimum logical value for each button.
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\buttonMaximums]]</dfn>
</td>
<td>
An empty [=list=]
</td>
<td>
A [=list=] containing the maximum logical value for each button
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="Gamepad">[[\vibrationActuator]]</dfn>
</td>
<td>
undefined
</td>
<td>
A {{GamepadHapticActuator}} object capable of generating a haptic
effect that vibrates the entire gamepad
</td>
</tr>
</table>
<dl>
<dt>
<dfn>id</dfn> attribute
</dt>
<dd>
<p>
An identification string for the gamepad. This string identifies
the brand or style of connected gamepad device.
</p>
<p>
The exact format of the {{Gamepad/id}} string is left unspecified.
It is RECOMMENDED that the [=user agent=] select a string that
identifies the product without uniquely identifying the device. For
example, a USB gamepad may be identified by its `idVendor` and
`idProduct` values. Unique identifiers like serial numbers or
Bluetooth device addresses MUST NOT be included in the
{{Gamepad/id}} string.
</p>
</dd>
<dt>
<dfn>index</dfn> attribute
</dt>
<dd>
The index of the gamepad in the {{Navigator}}. When multiple gamepads
are connected to a [=user agent=], indices MUST be assigned on a
first-come, first-serve basis, starting at zero. If a gamepad is
disconnected, previously assigned indices MUST NOT be reassigned to
gamepads that continue to be connected. However, if a gamepad is
disconnected, and subsequently the same or a different gamepad is
then connected, the lowest previously used index MUST be reused.
</dd>
<dt>
<dfn>connected</dfn> attribute
</dt>
<dd>
<p>
Indicates whether the physical device represented by this object is
still connected to the system. When a gamepad becomes unavailable,
whether by being physically disconnected, powered off or otherwise
unusable, the {{Gamepad/connected}} attribute MUST be set to
`false`.
</p>
<p>
The {{Gamepad/connected}} getter steps are:
</p>
<ol>
<li>Return [=this=].{{Gamepad/[[connected]]}}.
</li>
</ol>
</dd>
<dt>
<dfn>timestamp</dfn> attribute
</dt>
<dd>
<p>
The {{Gamepad/timestamp}} allows the author to determine the last
time the {{Gamepad/axes}} or {{Gamepad/buttons}} attribute for this
gamepad was updated. The value MUST be set to the [=current high
resolution time=] each time the system [=receives new button or
axis input values=] from the device. If no data has been received
from the hardware, {{Gamepad/timestamp}} MUST be the [=current high
resolution time=] at the time when the {{Gamepad}} was first made
available to script.
</p>
<p class="warning">
[=User agent=]s SHOULD set a minimum resolution of |gamepad|'s
{{Gamepad/timestamp}} attribute to 5 microseconds, following
[[HR-TIME]]'s clock resolution recommendation.
</p>
<p>
The {{Gamepad/timestamp}} getter steps are:
</p>
<ol>
<li>Return [=this=].{{Gamepad/[[timestamp]]}}.
</li>
</ol>
</dd>
<dt>
<dfn>mapping</dfn> attribute
</dt>
<dd>
<p>
The mapping in use for this device. If the user agent has knowledge
of the layout of the device, then it SHOULD indicate that a mapping
is in use by setting {{Gamepad/mapping}} to the corresponding
{{GamepadMappingType}} value.
</p>
<p>
To <dfn data-lt="selecting a mapping">select a mapping</dfn> for a
gamepad device, run the following steps:
</p>
<ol>
<li>If the button and axis layout of the gamepad device corresponds
with the [=Standard Gamepad=] layout, then return
{{GamepadMappingType/"standard"}}.
</li>
<li>Return {{GamepadMappingType/""}}.
</li>
</ol>
</dd>
<dt>
<dfn>axes</dfn> attribute
</dt>
<dd>
<p>
Array of values for all axes of the gamepad. All axis values MUST
be linearly normalized to the range [-1.0 .. 1.0]. If the
controller is perpendicular to the ground with the directional
stick pointing up, -1.0 SHOULD correspond to "forward" or "left",
and 1.0 SHOULD correspond to "backward" or "right". Axes that are
drawn from a 2D input device SHOULD appear next to each other in
the axes array, X then Y. It is RECOMMENDED that axes appear in
decreasing order of importance, such that element 0 and 1 typically
represent the X and Y axis of a directional stick. The same object
MUST be returned until the [=user agent=] needs to return different
values (or values in a different order).
</p>
<p>
The {{Gamepad/axes}} getter steps are:
</p>
<ol>
<li>Return [=this=].{{Gamepad/[[axes]]}}.
</li>
</ol>
</dd>
<dt>
<dfn>buttons</dfn> attribute
</dt>
<dd>
<p>
Array of button states for all buttons of the gamepad. It is
RECOMMENDED that buttons appear in decreasing importance such that
the primary button, secondary button, tertiary button, and so on
appear as elements 0, 1, 2, ... in the buttons array. The same
object MUST be returned until the [=user agent=] needs to return
different values (or values in a different order).
</p>
<p>
The {{Gamepad/buttons}} getter steps are:
</p>
<ol>
<li>Return [=this=].{{Gamepad/[[buttons]]}}.
</li>
</ol>
</dd>
<dt>
<dfn>vibrationActuator</dfn> attribute
</dt>
<dd>
<p>
A {{GamepadHapticActuator}} object that represents the device's
primary vibration actuator.
</p>
<p>
The {{Gamepad/vibrationActuator}} getter steps are:
</p>
<ol>
<li>Return [=this=].{{Gamepad/[[vibrationActuator]]}}.
</li>
</ol>
</dd>
</dl>
<section>
<h3>
Receiving inputs
</h3>
<p>
When the system <dfn>receives new button or axis input values</dfn>,
run the following steps:
</p>
<ol>
<li>Let |gamepad:Gamepad| be the {{Gamepad}} object representing the
device that received new button or axis input values.
</li>
<li>[=Queue a task=] on the [=gamepad task source=] to [=update
gamepad state=] for |gamepad|.
</li>
</ol>
<p>
To <dfn>update gamepad state</dfn> for |gamepad:Gamepad|, run the
following steps:
</p>
<ol>
<li>Let |now:DOMHighResTimeStamp| be the [=current high resolution
time=].
</li>
<li>Set |gamepad|.{{Gamepad/[[timestamp]]}} to |now|.
</li>
<li>Run the steps to [=map and normalize axes=] for |gamepad|.
</li>
<li>Run the steps to [=map and normalize buttons=] for |gamepad|.
</li>
<li>Let |navigator:Navigator| be |gamepad|'s [=relevant global
object=]'s {{Navigator}} object.
</li>
<li>If |navigator|.{{Navigator/[[hasGamepadGesture]]}} is `false` and
|gamepad| [=contains a gamepad user gesture=]:
<ol>
<li>Set |navigator|.{{Navigator/[[hasGamepadGesture]]}} to
`true`.
</li>
<li>[=list/For each=] |connectedGamepad:Gamepad?| of
|navigator|.{{Navigator/[[gamepads]]}}:
<ol>
<li>If |connectedGamepad| is not equal to `null`:
<ol>
<li>Set |connectedGamepad|.{{Gamepad/[[exposed]]}} to
`true`.
</li>
<li>Set |connectedGamepad|.{{Gamepad/[[timestamp]]}} to
|now|.
</li>
<li>Let |document:Document?| be |gamepad|'s [=relevant
global object=]'s [=associated `Document`=]; otherwise
`null`.
</li>
<li>If |document| is not `null` and is [=Document/fully
active=], then [=queue a task=] on the [=gamepad task
source=] to [=fire an event=] named {{gamepadconnected}}
at |gamepad|'s [=relevant global object=] using
{{GamepadEvent}} with its {{GamepadEvent/gamepad}}
attribute initialized to |connectedGamepad|.
</li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ol>
<p>
To <dfn>map and normalize axes</dfn> for |gamepad:Gamepad|, run the
following steps:
</p>
<ol>
<li>Let |axisValues:list| be a [=list=] of {{unsigned long}} values
representing the most recent logical axis input values for each axis
input of the device represented by |gamepad|.
</li>
<li>Let |maxRawAxisIndex:long| be the [=list/size=] of |axisValues| −
1.
</li>
<li>[=list/For each=] |rawAxisIndex:long| of [=the range=] from 0 to
|maxRawAxisIndex|:
<ol>
<li>Let |mappedIndex:long| be
|gamepad|.{{Gamepad/[[axisMapping]]}}[|rawAxisIndex|].
</li>
<li>Let |logicalValue:unsigned long| be
|axisValues|[|rawAxisIndex|].
</li>
<li>Let |logicalMinimum:unsigned long| be
|gamepad|.{{Gamepad/[[axisMinimums]]}}[|rawAxisIndex|].
</li>
<li>Let |logicalMaximum:unsigned long| be
|gamepad|.{{Gamepad/[[axisMaximums]]}}[|rawAxisIndex|].
</li>
<li>Let |normalizedValue:double| be 2 (|logicalValue| −
|logicalMinimum|) / (|logicalMaximum| − |logicalMinimum|) − 1.
</li>
<li>Set |gamepad|.{{Gamepad/[[axes]]}}[|axisIndex|] to be
|normalizedValue|.
</li>
</ol>
</li>
</ol>
<p>
To <dfn>map and normalize buttons</dfn> for |gamepad:Gamepad|, run
the following steps:
</p>
<ol>
<li>Let |buttonValues:list| be a [=list=] of {{unsigned long}} values
representing the most recent logical button input values for each
button input of the device represented by |gamepad|.
</li>
<li>Let |maxRawButtonIndex:long| be the [=list/size=] of
|buttonValues| − 1.
</li>
<li>[=list/For each=] |rawButtonIndex:long| of [=the range=] from 0
to |maxRawButtonIndex|:
<ol>
<li>Let |mappedIndex:long| be
|gamepad|.{{Gamepad/[[buttonMapping]]}}[|rawButtonIndex|].
</li>
<li>Let |logicalValue:unsigned long| be
|buttonValues|[|rawButtonIndex|].
</li>
<li>Let |logicalMinimum:unsigned long| be
|gamepad|.{{Gamepad/[[buttonMinimums]]}}[|rawButtonIndex|].
</li>
<li>Let |logicalMaximum:unsigned long| be
|gamepad|.{{Gamepad/[[buttonMaximums]]}}[|rawButtonIndex|].
</li>
<li>Let |normalizedValue:double| be (|logicalValue| −
|logicalMinimum|) / (|logicalMaximum| − |logicalMinimum|).
</li>
<li>Let |button:GamepadButton| be
|gamepad|.{{Gamepad/[[buttons]]}}[|mappedIndex|].
</li>
<li>Set |button|.{{GamepadButton/[[value]]}} to
|normalizedValue|.
</li>
<li>
<p>
If the button has a digital switch to indicate a pure pressed
or released state, set |button|.{{GamepadButton/[[pressed]]}}
to `true` if the button is pressed or `false` if it is not
pressed.
</p>
<p>
Otherwise, set |button|.{{GamepadButton/[[pressed]]}} to
`true` if the value is above the [=button press threshold=]
or `false` if it is not above the threshold.
</p>
</li>
<li>
<p>
If the button is capable of detecting touch, set
|button|.{{GamepadButton/[[touched]]}} to `true` if the
button is currently being touched.
</p>
<p>
Otherwise, set |button|.{{GamepadButton/[[touched]]}} to
|button|.{{GamepadButton/[[pressed]]}}.
</p>
</li>
</ol>
</li>
</ol>
</section>
<section>
<h3>
Constructing a `Gamepad`
</h3>
<p>
<dfn>A new `Gamepad`</dfn> representing a connected gamepad device is
constructed by performing the following steps:
</p>
<ol>
<li>Let |gamepad:Gamepad| be a newly created {{Gamepad}} instance:
<ol>
<li>Initialize |gamepad|'s {{Gamepad/id}} attribute to an
identification string for the gamepad.
</li>
<li>Initialize |gamepad|'s {{Gamepad/index}} attribute to the
result of [=selecting an unused gamepad index=] for |gamepad|.
</li>
<li>Initialize |gamepad|'s {{Gamepad/mapping}} attribute to the
result of [=selecting a mapping=] for the gamepad device.
</li>
<li>Initialize |gamepad|.{{Gamepad/[[connected]]}} to `true`.
</li>
<li>Initialize |gamepad|.{{Gamepad/[[timestamp]]}} to the
[=current high resolution time=].
</li>
<li>Initialize |gamepad|.{{Gamepad/[[axes]]}} to the result of
[=initializing axes=] for |gamepad|.
</li>
<li>Initialize |gamepad|.{{Gamepad/[[buttons]]}} to the result of
[=initializing buttons=] for |gamepad|.
</li>
<li>Initialize |gamepad|.{{Gamepad/[[vibrationActuator]]}}
following the steps of [=constructing a GamepadHapticActuator=]
for |gamepad|.
</li>
</ol>
</li>
<li>Return |gamepad|.
</li>
</ol>
<p>
To <dfn data-lt="selecting an unused gamepad index">select an unused
gamepad index</dfn> for |gamepad:Gamepad|, run the following steps:
</p>
<ol>
<li>Let |navigator:Navigator| be |gamepad|'s [=relevant global
object=]'s {{Navigator}} object.
</li>
<li>Let |maxGamepadIndex:long| be the [=list/size=] of
|navigator|.{{Navigator/[[gamepads]]}} − 1.
</li>
<li>[=list/For each=] |gamepadIndex:long| of [=the range=] from 0 to
|maxGamepadIndex|:
<ol>
<li>If |navigator|.{{Navigator/[[gamepads]]}}[|gamepadIndex|] is
`null`, then return |gamepadIndex|.
</li>
</ol>
</li>
<li>[=list/Append=] `null` to |navigator|.{{Navigator/[[gamepads]]}}.
</li>
<li>Return the [=list/size=] of
|navigator|.{{Navigator/[[gamepads]]}} − 1.
</li>
</ol>
<p>
To <dfn data-lt="initializing axes">initialize axes</dfn> for
|gamepad:Gamepad|, run the following steps:
</p>
<ol>
<li>Let |inputCount:long| be the number of axis inputs exposed by the
device represented by |gamepad|.
</li>
<li>Set |gamepad|.{{Gamepad/[[axisMinimums]]}} to a [=list=] of
{{unsigned long}} values with [=list/size=] equal to |inputCount|
containing minimum logical values for each of the axis inputs.
</li>
<li>Set |gamepad|.{{Gamepad/[[axisMaximums]]}} to a [=list=] of
{{unsigned long}} values with [=list/size=] equal to |inputCount|
containing maximum logical values for each of the axis inputs.
</li>
<li>Initialize |unmappedInputList| to be an empty [=list=].
</li>
<li>Initialize |mappedIndexList| to be an empty [=list=].
</li>
<li>Initialize |axesSize:long| to be 0.
</li>
<li>[=list/For each=] |rawInputIndex:long| of [=the range=] from 0 to
|inputCount| − 1:
<ol>
<li>If the the gamepad axis at index |rawInputIndex| [=represents
a Standard Gamepad axis=]:
<ol>
<li>Let |canonicalIndex:long| be the [=canonical index=] for
the axis.
</li>
<li>If |mappedIndexList| [=list/contain=]s |canonicalIndex|,
then append |rawInputIndex| to |unmappedInputList|.
<p>
Otherwise:
</p>
<ol>
<li>Set
|gamepad|.{{Gamepad/[[axisMapping]]}}[|rawInputIndex|] to
|canonicalIndex|.
</li>
<li>[=list/Append=] |canonicalIndex| to
|mappedIndexList|.
</li>
<li>If |canonicalIndex| + 1 is greater than |axesSize|,
then set |axesSize| to |canonicalIndex| + 1.
</li>
</ol>
</li>
</ol>
<p>
Otherwise, [=list/append=] |rawInputIndex| to
|unmappedInputList|.
</p>
</li>
</ol>
</li>
<li>Initialize |axisIndex:long| to be 0.
</li>
<li>[=list/For each=] |rawInputIndex:long| of |unmappedInputList|:
<ol>
<li>While |mappedIndexList| [=list/contain=]s |axisIndex|:
<ol>
<li>Increment |axisIndex|.
</li>
</ol>
</li>
<li>Set |gamepad|.{{Gamepad/[[axisMapping]]}}[|rawInputIndex|] to
|axisIndex|.
</li>
<li>[=list/Append=] |axisIndex| to |mappedIndexList|.
</li>
<li>If |axisIndex| + 1 is greater than |axesSize|, then set
|axesSize| to |axisIndex| + 1.
</li>
</ol>
</li>
<li>Initialize |axes| to be an empty [=list=].
</li>
<li>[=list/For each=] |axisIndex:long| of [=the range=] from 0 to
|axesSize| − 1, [=list/append=] 0 to |axes|.
</li>
<li>Return |axes|.
</li>
</ol>
<p>
To <dfn data-lt="initializing buttons">initialize buttons</dfn> for a
gamepad, run the following steps:
</p>
<ol>
<li>Let |inputCount:long| be the number of button inputs exposed by
the device represented by |gamepad|.
</li>
<li>Set |gamepad|.{{Gamepad/[[buttonMinimums]]}} to be a [=list=] of
{{unsigned long}} values with [=list/size=] equal to |inputCount|
containing minimum logical values for each of the button inputs.
</li>
<li>Set |gamepad|.{{Gamepad/[[buttonMaximums]]}} to be a [=list=] of
{{unsigned long}} values with [=list/size=] equal to |inputCount|
containing maximum logical values for each of the button inputs.
</li>
<li>Initialize |unmappedInputList| to be an empty [=list=].
</li>
<li>Initialize |mappedIndexList| to be an empty [=list=].
</li>
<li>Initialize |buttonsSize:long| to be 0.
</li>
<li>[=list/For each=] |rawInputIndex:long| of [=the range=] from 0 to
|inputCount| − 1:
<ol>
<li>If the the gamepad button at index |rawInputIndex|
[=represents a Standard Gamepad button=]:
<ol>
<li>Let |canonicalIndex:long| be the [=canonical index=] for
the button.
</li>
<li>If |mappedIndexList| [=list/contain=]s |canonicalIndex|,
then append |rawInputIndex| to |unmappedInputList|.
<p>
Otherwise:
</p>
<ol>
<li>Set
|gamepad|.{{Gamepad/[[buttonMapping]]}}[|rawInputIndex|]
to |canonicalIndex|.
</li>
<li>[=list/Append=] |canonicalIndex| to
|mappedIndexList|.
</li>
<li>If |canonicalIndex| + 1 is greater than
|buttonsSize|, then set |buttonsSize| to |canonicalIndex|
+ 1.
</li>
</ol>
</li>
</ol>
<p>
Otherwise, [=list/append=] |rawInputIndex| to
|unmappedInputList|.
</p>
</li>
<li>Increment |rawInputIndex|.
</li>
</ol>
</li>
<li>Initialize |buttonIndex:long| to be 0.
</li>
<li>[=list/For each=] |rawInputIndex:long| of |unmappedInputList|:
<ol>
<li>While |mappedIndexList| [=list/contain=]s |buttonIndex|:
<ol>
<li>Increment |buttonIndex|.
</li>
</ol>
</li>
<li>Set |gamepad|.{{Gamepad/[[buttonMapping]]}}[|rawInputIndex|]
to |buttonIndex|.
</li>
<li>[=list/Append=] |buttonIndex| to |mappedIndexList|.
</li>
<li>If |buttonIndex| + 1 is greater than |buttonsSize|, then set
|buttonsSize| to |buttonIndex| + 1.
</li>
</ol>
</li>
<li>Initialize |buttons| to be an empty [=list=].
</li>
<li>[=list/For each=] |buttonIndex:long| of [=the range=] from 0 to
|buttonsSize| − 1, [=list/append=] a [=new=] {{GamepadButton}} to
|buttons|.
</li>
<li>Return |buttons|.
</li>
</ol>
</section>
</section>
<section data-dfn-for="GamepadButton" data-link-for="GamepadButton">
<h2>
<dfn>GamepadButton</dfn> Interface
</h2>
<p>
This interface defines the state of an individual button on a gamepad
device.
</p>
<pre class="idl">
[Exposed=Window]
interface GamepadButton {
readonly attribute boolean pressed;
readonly attribute boolean touched;
readonly attribute double value;
};
</pre>
<p>
Instances of {{GamepadButton}} are created with the internal slots
described in the following table:
</p>
<table class="simple">
<tr>
<th>
Internal slot
</th>
<th>
Initial value
</th>
<th>
Description (non-normative)
</th>
</tr>
<tr>
<td>
<dfn data-dfn-for="GamepadButton">[[\pressed]]</dfn>
</td>
<td>
`false`
</td>
<td>
A flag indicating that the button is pressed
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="GamepadButton">[[\touched]]</dfn>
</td>
<td>
`false`
</td>
<td>
A flag indicating that the button is touched
</td>
</tr>
<tr>
<td>
<dfn data-dfn-for="GamepadButton">[[\value]]</dfn>
</td>
<td>
0.0
</td>
<td>
A {{double}} representing the button value scaled to the range [0.0
.. 1.0]
</td>
</tr>
</table>
<dl>
<dt>
<dfn>pressed</dfn> attribute
</dt>
<dd>
<p>
The pressed state of the button. This property MUST be `true` if
the button is currently pressed, and `false` if it is not pressed.
For buttons which do not have a digital switch to indicate a pure
pressed or released state, the [=user agent=] MUST choose a
<dfn>button press threshold</dfn> to indicate the button as pressed
when its value is above a certain amount. If the platform API gives
a recommended value, the user agent SHOULD use that. In other
cases, the user agent SHOULD choose some other reasonable value.
</p>
<p>
The {{GamepadButton/pressed}} getter steps are:
</p>
<ol>
<li>Return [=this=].{{GamepadButton/[[pressed]]}}.
</li>
</ol>
</dd>
<dt>
<dfn>touched</dfn> attribute
</dt>
<dd>
<p>
The touched state of the button. If the button is capable of
detecting touch, this property MUST be `true` if the button is
currently being touched, and `false` otherwise. If the button is
not capable of detecting touch and is capable of reporting an
analog value, this property MUST be `true` if the value property is
greater than 0, and `false` if the value is 0. If the button is not
capable of detecting touch and can only report a digital value,
this property MUST mirror the {{GamepadButton/pressed}} attribute.
</p>
<p>
The {{GamepadButton/touched}} getter steps are:
</p>
<ol>
<li>Return [=this=].{{GamepadButton/[[touched]]}}.
</li>
</ol>
</dd>