USB game controller

Button layouts
Unlike controllers for a single game console, USB Human Interface Device game controllers for a PC have their buttons in different layouts. Even if the buttons are in the same layout, they may be presented to the application in a different order. So unlike a console game, a PC game cannot assume that, say, "button 1 = jump".

This section describes how various controllers are seen by Pygame, a wrapper around SDL. Most tests were performed with a program under Xubuntu 12.04 "Precise Pangolin". Section headings represent the device name as seen through the test program, under Linux if not otherwise specified. Results on Windows 8 were similar, except SDL under Windows 8 omits the manufacturer name from the device name, and devices without a name show up as a less informative "2 axis 10 button joystick" instead of the hexadecimal vendor and product IDs.

HID controllers
The original iMac was the first popular personal computer to have USB as its only connection for external input, storage, and output devices. USB ports became common on other brands of PC starting in 1999. Once USB became standard and sound cards stopped carrying the 15-pin game ports to which joysticks had previously connected, companies started producing USB game controllers specifically for the PC.

Gravis GamePad Pro USB
One of the first USB game controllers, with the same physical layout as the original PlayStation controller.
 * Axes: 2
 * Control Pad X
 * Control Pad Y
 * Buttons: 10
 * Red (left)
 * Yellow (down)
 * Green (right)
 * Blue (up)
 * L1
 * R1
 * L2
 * R2
 * Select
 * Start

Logitech Logitech Dual Action
A controller with the same layout as Sony's Dual Shock, except the sticks' restrictors are square, not circular. When the "mode" switch is turned on, the left stick is swapped with the digital Control Pad. Surprisingly, buttons 1-10 are in the same order as on the Gravis.
 * Axes: 4
 * Left stick X
 * Left stick Y
 * Right stick X
 * Right stick Y
 * Buttons: 12
 * Square (Left)
 * X (Down)
 * Circle (Right)
 * Triangle (Up)
 * L1
 * R1
 * L2
 * R2
 * Select
 * Start
 * L3
 * R3
 * Hats: 1
 * Control Pad

Logitech Logitech(R) Precision(TM) Gamepad
This controller is essentially the same thing as the Dual Action, except with no analog sticks and the "mode" switch always on. It's similar to the original PlayStation controller with larger L2 and R2 buttons like those of the Dual Shock.
 * Axes: 2
 * Control Pad X
 * Control Pad Y
 * Buttons: 10; same placement and order as the first ten buttons of the Dual Action

DragonRise Inc. Generic USB Joystick
The SteelSeries 3GC Controller is mapped quite oddly. The name of this device, as seen by SDL on Linux, has a lot of spaces: DragonRise Inc.  Generic   USB  Joystick Buttons 1-10 have silk-screened numbers. Shoulder buttons 5-8 have the number silk-screened next to an engraved 1 or 2, showing that the manufacturer uses the same mold for PlayStation controllers. A "mode" button at the bottom center of the face switches between two modes, signaled by green and red LEDs next to the button. Green appears to be designed for games expecting only digital controls, while red makes all analog controls available.


 * Axes: 5
 * Left stick X OR Control Pad X (green only)
 * Left stick Y OR Control Pad Y (green only)
 * Left stick X OR right stick Y (red only)
 * Right stick Y (red only)
 * Right stick X (red only)
 * Buttons: 12
 * Triangle (Up) OR right stick up (green only)
 * Circle (Right) OR right stick right (green only)
 * X (Down) OR right stick down (green only)
 * Square (Left) or right stick left (green only)
 * Shoulder L2
 * Shoulder R2
 * Shoulder L1
 * Shoulder R1
 * Select
 * Start
 * L3
 * R3
 * Hats: 1
 * Control Pad (red only)

Jess Tech GGE909 PC Recoil Pad
This one has a flat, retractable USB cable. Its Control Pad is severely diagonal-biased, making it next to unusable for anything but isometric games like Q*bert, Snake Rattle 'n' Roll, and other games designed for a 4-way diagonal joystick. Its face buttons are in PlayStation order and have 1, 2, 3, and 4 symbols colored like PlayStation buttons.

Axes (4): Buttons (4): Hats (1)
 * 1) Left stick X (analog on) OR Control Pad X (analog off)
 * 2) Left stick Y (analog on) OR Control Pad Y (analog off)
 * 3) Right stick Y (analog on)
 * 4) Right stick X (analog on)
 * 1) Triangle
 * 2) Circle
 * X
 * 1) Square
 * L1
 * L2
 * R1
 * R2
 * 1) Select
 * 2) Start
 * L3
 * R3
 * 1) Control Pad (analog on)

HID 1267:2afb
This is a Chinese no-name clone of the Super NES controller, except with lighter plastic and a USB HID protocol instead of the SPI-like protocol that the NES and Super NES controllers use.
 * Axes: 2
 * Control Pad X
 * Control Pad Y
 * Buttons: 8
 * Y
 * X
 * B
 * A
 * L
 * R
 * Select
 * Start

Adapters
Evidently a lot of people are dissatisfied with the Control Pad on no-name and Gravis controllers, preferring that of a Nintendo or Sony console. Several companies have produced adapters to use controllers for classic game consoles with any PC game that supports USB HID controllers.

Wish Technologies Adaptoid
Adapter to use Nintendo 64 controllers on a USB HID host. As with the Dual Analog, there is a way to swap the Control Pad and Control Stick. While holding L, R, and Z, press Up on the Control Pad three times to enable swapping or move the Control Stick up three times to disable it.
 * Axes: 2
 * Control Stick X
 * Control Stick Y
 * Buttons: 14
 * A
 * C Down
 * C Right
 * B
 * C Left
 * C Up
 * L
 * R
 * Start
 * Z
 * Control Pad Up
 * Control Pad Down
 * Control Pad Left
 * Control Pad Right

HID 0b43:0003
The EMS USB2 adapter can be switched between two modes. One mode allows the use of an original Namco Guncon with PS2 software designed for the Guncon 2. The other mode, described here, allows using one or two PlayStation, Dual Shock, or Dual Shock 2 controllers on a USB HID host. Some people report it works better when plugged into a hub than when plugged directly into the PC. It has become popular among StepMania users because Control Pad directions are always returned as buttons, even in digital mode, so that Left+Right and Up+Down presses on a dance pad work correctly. An original PlayStation controller or a Dual Shock or Dual Shock 2 with the Analog button turned off will replace all joystick axes with the corresponding direction on the Control Pad. The buttons on a Dual Shock 2 are not pressure-sensitive; it is treated as an original Dual Shock.
 * Axes: 4
 * Left stick X
 * Left stick Y
 * Right stick Y
 * Right stick X
 * Buttons: 14
 * Triangle
 * Circle
 * X
 * Square
 * L2
 * R2
 * L1
 * R1
 * Select
 * Start
 * L3
 * R3
 * Control Pad Up
 * Control Pad Right
 * Control Pad Down
 * Control Pad Left

XInput controllers
The Xbox 360 controller's native protocol is not the standard USB Human Interface Device protocol.

Microsoft X-Box 360 pad
This is a wired Xbox 360 Controller. Its Linux driver misspells "Xbox". The layout below is for SDL on the Linux driver.
 * Axes: 6
 * Left stick X
 * Left stick Y
 * LT
 * Right stick X
 * Right stick Y
 * RT
 * Buttons: 11
 * A (Down)
 * B (Right)
 * X (Left)
 * Y (Up)
 * LB
 * RB
 * Back
 * Start
 * Guide (the "hot cross bun" button in the middle)
 * L3
 * R3
 * Hats: 1
 * Control Pad

Controller (XBOX 360 For Windows)
Under Windows, SDL uses DirectInput, not XInput, for Xbox 360 controllers. The Windows DirectInput driver for the Xbox 360 controller produce the difference RT - LT as one axis, with no second axis for LT + RT, in order to encourage developers to switch from DirectInput to XInput, which is exclusive to Xbox 360 controllers. (Another thing to encourage use of XInput is the fact that Windows Store applications cannot use DirectInput and thus cannot use any controller other than the Xbox 360 Controller.)


 * Axes: 5
 * Left stick X
 * Left stick Y
 * LT - RT
 * Right stick Y
 * Right stick X
 * Buttons: 10
 * A
 * B
 * X
 * Y
 * LB
 * RB
 * Select
 * Start
 * L3
 * R3
 * Hats: 1
 * Control Pad

Microsoft makes a receiver for Xbox 360 wireless controllers that plugs into a PC's USB port, but I haven't had a chance to test it. One of my testers reports that it calls itself a "Controller (Xbox 360 Wireless Receiver for Windows)".

Conclusions
The primary axes are either the Control Pad or the left stick. Buttons come in a rough order: face buttons, then shoulder buttons, then Select and Start, then buttons under sticks, and finally Control Pad directions if not assigned to a hat. But the order and number of buttons within a category are unpredictable, as is which button the user expects to use for each action.

An application that doesn't recognize a particular controller brand SHOULD do the following:
 * In the menu, map the primary axes to move, buttons 1 and 3 to OK, and 2 and 4 to Back.
 * In the game, map face and shoulder buttons for an Xbox 360 controller, and map 7 and 8 to Pause unless the controller has fewer than eight buttons, in which case the last should be Pause.
 * From both the game's menu and the pause menu, include a configuration form that can be operated with the menu bindings.
 * Save the result of this form after the user completes it.

To make things easier for the user, the form SHOULD be preloaded with layouts for the most widespread controllers. All XInput controllers use the layout for Xbox 360, and the layout for Gravis GamePad Pro USB, Logitech Precision, and Logitech Dual Action appears to be fairly widespread as well. If the user selects "Custom", ask the user to press the button for each game action in sequence. Assigning multiple buttons to a given action can be done later, after the basic configuration is in place.


 * Main Menu
 * Continue
 * New Game
 * &#x2325; Options
 * Exit


 * Options
 * &#x1F3AE; Control
 * &#x1F3A8; Graphics
 * &#x1F3A7; Sound
 * ← Back


 * Control
 * Show a drawing of the controller with button functions overlaid.
 * [Xbox 360 Standard] (or whatever controller was detected)
 * Custom Layout
 * &#x1F527; Fine-Tune
 * ← Back

buttons in order: Jump, Attack, Change Tool, Pause
 * Custom Layout
 * Press the following
 * [Up], Down, Left, Right,


 * Game Paused
 * &#9658; Continue
 * Abort Mission
 * &#x2325; Options

The player may end up starting a game with the wrong controls and not knowing how to get back to the menu to configure the controls. Such a player would have to press all the buttons on the controller but would eventually end up pressing the button whose number is the same as that of the Back or Start button on the Xbox 360 controller, or in the case of a controller with few buttons, the last button. (Based in part on recommendation by Belial6.)

Reading the controller
An analog joystick returns displacements along two axes: X (horizontal) and Y (vertical). These readings include a small amount of noise. So the way axes are mapped SHOULD have a dead zone, or an area at the center of the range where the stick is considered centered.

There are two ways to convert the X and Y axes to a direction: separable and polar. A "separable" interpretation treats the X and Y axes independently, giving each axis its own dead zone. The size of the dead zone controls how easy it is to hit the straight and diagonal areas, and it's next to impossible to go directly from centered to diagonal. Separable interpretation is useful for side-view games, where the X axis moves left and right and the Y axis looks up and down or jumps and crouches.

A "polar" interpretation first determines how far and at what angle the stick is moved. The straight sectors can be the same size as the diagonals (45 degrees), or it can be biased toward straight movement (60 degrees). The angle measurement need not use time-consuming trigonometry or square roots. It takes only two multiplies and three adds to calculate whether the squared displacement is greater than the square of the dead zone radius (x2 + y2 > r2), and then calculate whether min(|x|, |y|) > m * max(|x|, |y|), where the slope threshold m (0 < m < 1) is the tangent of half the width of the straight sector.

Here are plots of the nine possible zones for the six combinations of separable, polar with 45 degree straight sectors, and polar with 60 degree sectors, at both 25% and 50% dead zone radius.

Some games allow no diagonal movement at all, such as top-down games with 4-way movement (Pac-Man; Pokemon) and block puzzle games that don't let the user move a piece sideways and drop it at the same time. The best way I've found to handle this is to use a bit of hysteresis: if the diagonal is pressed, use the most recent straight sector (if it's adjacent). This works with separable interpretation or with 60-degree straight sectors.

Even on controllers with a digital Control Pad, some cheaper controllers may make it too easy for the player to accidentally press diagonally. This might make the character in a platformer suddenly switch from a run (Right) into a crawl (Down+Right). This can be prevented with a similar technique to the diagonal: only allow Down+Right to be entered from Down. Holding Right and pressing Down would continue moving to the right; to roll or crawl, the player would first have to roll the Control Pad to Down then back to Down+Right, as if performing a Dragon Punch. But games SHOULD still offer a way to turn this compensation off in case the user is using a higher-quality controller, such as a keyboard, an N64 controller through an Adaptoid, a Dual Shock through an EMS USB2, or a Wii Classic Controller through a Mayflash PC045.

And don't depend on the player being able to reach the four corners with the joystick. The thumb sticks on the Xbox 360 and PlayStation controllers, for example, have a circular restrictor. So scale each joystick's dead zone size relative to the maximum displacement that the program has seen on that joystick in this play session.