W3C

Gamepad

W3C Working Draft 29 May 2012

This version:
http://www.w3.org/TR/2012/WD-gamepad-20120529/
Latest published version:
http://www.w3.org/TR/gamepad/
Latest editor's draft:
http://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html
Editors:
Scott Graham, Google
Ted Mielczarek, Mozilla

Abstract

The Gamepad specification defines a low-level interface that represents gamepad devices.

Status of This Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This document was published by the Web Applications Working Group as the 29 May 2012 First Public Working Draft. This document is intended to become a W3C Recommendation. If you wish to make comments regarding this document, please send them to public-webapps@w3.org (subscribe, archives) with [gamepad] at the start of the subject header. All feedback is welcome.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Table of Contents

1. Introduction

This section is non-normative.

Some user agents have connected gamepad devices. These devices are desirable and suited to input for gaming applications, and for "10 foot" user interfaces (presentations, media viewers).

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.

Meanwhile, native applications are capable of accessing these devices via system APIs.

The Gamepad API provides a solution to this problem by specifying interfaces that allow web applications to directly act on gamepad data.

This specification references interfaces from a number of other specifications:

2. Conformance

As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.

The key words must, must not, required, should, should not, recommended, may, and optional in this specification are to be interpreted as described in [RFC2119].

This specification defines conformance criteria that apply to a single product: the user agent that implements the interfaces that it contains.

Implementations that use ECMAScript to implement the APIs defined in this specification must implement them in a manner consistent with the ECMAScript Bindings defined in the Web IDL specification [WEBIDL] as this specification uses that specification and terminology.

A conforming implementation is required to implement all fields defined in this specification.

3. Scope

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.

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].

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.

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.

4. Gamepad Interface

This interface defines an individual gamepad device.

interface Gamepad {
    readonly attribute string       id;
    readonly attribute long         index;
    readonly attribute DOMTimeStamp timestamp;
    readonly attribute float[]      axes;
    readonly attribute float[]      buttons;
};

4.1 Attributes

axes of type array of float, readonly
Array of values for all axes of the gamepad. All axis values must be linearly normalized to the range [-1.0 .. 1.0]. As appropriate, -1.0 should correspond to "up" or "left", and 1.0 should correspond to "down" 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.
buttons of type array of float, readonly
Array of values for all buttons of the gamepad. All button values must be linearly normalized to the range [0.0 .. 1.0]. 0.0 must mean fully unpressed, and 1.0 must mean fully pressed. 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.
id of type string, readonly
An identification string for the gamepad. This string identifies the brand or style of connected gamepad device. Typically, this will include the USB vendor and a product ID.
index of type long, readonly
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, index entries must be reused.
timestamp of type DOMTimeStamp, readonly
Last time the data for this gamepad was updated. Timestamp is a monotonically increasing value that allows the author to determine if the axes and button data have been updated from the hardware, relative to a previously saved timestamp.

6. GamepadEvent Interface

interface GamepadEvent : Event {
    readonly attribute Gamepad gamepad;
};

6.1 Attributes

gamepad of type Gamepad, readonly
The single gamepad attribute provides access to the associated gamepad data for this event.

7. Usage Examples

This section is non-normative.

The example below demonstrates typical access to gamepads. Note the relationship with the WindowAnimationTiming interface.

function runAnimation()
{
    window.requestAnimationFrame(runAnimation);

    for (var i = 0; i < navigator.gamepads.length; ++i)
    {
        var pad = navigator.gamepads[i];
        // todo; simple demo of displaying pad.axes and pad.buttons
    }
}

window.requestAnimationFrame(runAnimation);

Best Practice 1: Coordination with WindowAnimationTiming

Interactive applications will typically be using the WindowAnimationTiming interface to drive animation, and will want coordinate animation with user gamepad input. As such, the gamepad data should be polled as closely as possible to immediately before the animation callbacks are executed, and with frequency matching that of the animation. That is, if the animation callbacks are running at 60Hz, the gamepad inputs should also be sampled at that rate.

8. The gamepadconnected event

A user agent must dispatch this event type to indicate the user has connected a gamepad. If a gamepad was already connected when the page was loaded, the gamepadconnected event will be dispatched when the user presses a button or moves an axis.

9. The gamepaddisconnected event

When a gamepad is disconnected from the user agent, if the user agent has previously dispatched a gamepadconnected event, a gamepaddisconnected event must be dispatched.

10. Other events

More discussion needed, on whether to include or exclude axis and button changed events, and whether to roll them more together (gamepadchanged?), separate somewhat (gamepadaxischanged?), or separate by individual axis and button.

A. Acknowledgements

This section is non-normative.

Many have made contributions in code, comments, or documentation:

Please let me know if I have inadvertently omitted your name.

B. References

B.1 Normative references

[NAVIGATOR]
Ian Hickson, David Hyatt. Navigator interface in HTML5. 15 April 2011. Editors' draft. (Work in progress.) URL: http://dev.w3.org/html5/spec/system-state-and-capabilities.html#the-navigator-object
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Internet RFC 2119. URL: http://www.ietf.org/rfc/rfc2119.txt
[WEBIDL]
Cameron McCormack. Web IDL. 19 April 2012. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2012/CR-WebIDL-20120419/

B.2 Informative references

No informative references.