bug 860413 - Allow setting standard gamepad mapping. r=smaug

This commit is contained in:
Ted Mielczarek 2013-04-09 08:43:25 -04:00
Родитель c844448aa0
Коммит 98dadecb4e
13 изменённых файлов: 52 добавлений и 6 удалений

Просмотреть файл

@ -25,10 +25,12 @@ NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(Gamepad, mParent)
Gamepad::Gamepad(nsISupports* aParent,
const nsAString& aID, uint32_t aIndex,
GamepadMappingType aMapping,
uint32_t aNumButtons, uint32_t aNumAxes)
: mParent(aParent),
mID(aID),
mIndex(aIndex),
mMapping(aMapping),
mConnected(true)
{
SetIsDOMBinding();
@ -146,7 +148,8 @@ already_AddRefed<Gamepad>
Gamepad::Clone(nsISupports* aParent)
{
nsRefPtr<Gamepad> out =
new Gamepad(aParent, mID, mIndex, mButtons.Length(), mAxes.Length());
new Gamepad(aParent, mID, mIndex, mMapping,
mButtons.Length(), mAxes.Length());
out->SyncState(this);
return out.forget();
}

Просмотреть файл

@ -17,6 +17,12 @@
namespace mozilla {
namespace dom {
enum GamepadMappingType
{
NoMapping = 0,
StandardMapping = 1
};
// TODO: fix the spec to expose both pressed and value:
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=21388
struct GamepadButton
@ -33,6 +39,7 @@ class Gamepad : public nsIDOMGamepad
public:
Gamepad(nsISupports* aParent,
const nsAString& aID, uint32_t aIndex,
GamepadMappingType aMapping,
uint32_t aNumButtons, uint32_t aNumAxes);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad)
@ -62,6 +69,15 @@ public:
aID = mID;
}
void GetMapping(nsAString& aMapping) const
{
if (mMapping == StandardMapping) {
aMapping = NS_LITERAL_STRING("standard");
} else {
aMapping = NS_LITERAL_STRING("");
}
}
bool Connected() const
{
return mConnected;
@ -97,6 +113,9 @@ protected:
nsString mID;
uint32_t mIndex;
// The mapping in use.
GamepadMappingType mMapping;
// true if this gamepad is currently connected.
bool mConnected;

Просмотреть файл

@ -131,6 +131,7 @@ GamepadService::RemoveListener(nsGlobalWindow* aWindow)
uint32_t
GamepadService::AddGamepad(const char* aId,
GamepadMappingType aMapping,
uint32_t aNumButtons,
uint32_t aNumAxes)
{
@ -139,6 +140,7 @@ GamepadService::AddGamepad(const char* aId,
new Gamepad(nullptr,
NS_ConvertUTF8toUTF16(nsDependentCString(aId)),
0,
aMapping,
aNumButtons,
aNumAxes);
int index = -1;
@ -505,13 +507,15 @@ GamepadServiceTest::GamepadServiceTest()
/* member initializers and constructor code */
}
/* uint32_t addGamepad (in string id, in uint32_t numButtons, in uint32_t numAxes); */
/* uint32_t addGamepad (in string id, in unsigned long mapping, in unsigned long numButtons, in unsigned long numAxes); */
NS_IMETHODIMP GamepadServiceTest::AddGamepad(const char* aID,
uint32_t aMapping,
uint32_t aNumButtons,
uint32_t aNumAxes,
uint32_t* aRetval)
{
*aRetval = gGamepadServiceSingleton->AddGamepad(aID,
static_cast<GamepadMappingType>(aMapping),
aNumButtons,
aNumAxes);
return NS_OK;

Просмотреть файл

@ -42,7 +42,8 @@ class GamepadService : public nsIObserver
void RemoveListener(nsGlobalWindow* aWindow);
// Add a gamepad to the list of known gamepads, and return its index.
uint32_t AddGamepad(const char* aID, uint32_t aNumButtons, uint32_t aNumAxes);
uint32_t AddGamepad(const char* aID, GamepadMappingType aMapping,
uint32_t aNumButtons, uint32_t aNumAxes);
// Remove the gamepad at |aIndex| from the list of known gamepads.
void RemoveGamepad(uint32_t aIndex);

Просмотреть файл

@ -9,10 +9,14 @@ interface nsIVariant;
/*
* This interface is intended only for use in tests.
*/
[scriptable, uuid(7edf77a2-6b3e-4bbb-9100-4452d425feaa)]
[scriptable, uuid(b6ed093c-6ea0-4141-a8eb-f99645162651)]
interface nsIGamepadServiceTest : nsISupports
{
unsigned long addGamepad(in string id, in unsigned long numButtons,
const unsigned long NO_MAPPING = 0;
const unsigned long STANDARD_MAPPING = 1;
unsigned long addGamepad(in string id, in unsigned long mapping,
in unsigned long numButtons,
in unsigned long numAxes);
void removeGamepad(in unsigned long index);
void newButtonEvent(in unsigned long index, in unsigned long button,

Просмотреть файл

@ -14,12 +14,14 @@ SimpleTest.waitForExplicitFinish();
window.addEventListener("gamepadconnected", connecthandler);
// Add a gamepad
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.STANDARD_MAPPING,
4, // buttons
2);// axes
// Press a button
GamepadService.newButtonEvent(index, 0, true);
function connecthandler(e) {
is(e.gamepad.id, "test gamepad", "correct gamepad name");
is(e.gamepad.id, "test gamepad", "correct gamepad name");
is(e.gamepad.mapping, "standard", "standard mapping");
is(e.gamepad.buttons.length, 4, "correct number of buttons");
is(e.gamepad.axes.length, 2, "correct number of axes");
SimpleTest.executeSoon(function() {

Просмотреть файл

@ -12,6 +12,7 @@
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes

Просмотреть файл

@ -12,6 +12,7 @@
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var index = GamepadService.addGamepad("test gamepad", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes

Просмотреть файл

@ -38,6 +38,7 @@ window.addEventListener("gamepadconnected", connecthandler);
window.addEventListener("gamepaddisconnected", disconnecthandler);
// Add a gamepad
var index1 = GamepadService.addGamepad("test gamepad 1", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes
var index2;
@ -53,6 +54,7 @@ function check_first_gamepad(e) {
is(gamepads[e.gamepad.index], e.gamepad, "right gamepad exposed at index");
// Add a second gamepad, should automatically show up.
index2 = GamepadService.addGamepad("test gamepad 2", // id
SpecialPowers.Ci.nsIGamepadServiceTest.NO_MAPPING,
4, // buttons
2);// axes
}

Просмотреть файл

@ -18,6 +18,12 @@ interface Gamepad {
*/
readonly attribute unsigned long index;
/**
* The mapping in use for this device. The empty string
* indicates that no mapping is in use.
*/
readonly attribute DOMString mapping;
/**
* true if this gamepad is currently connected to the system.
*/

Просмотреть файл

@ -171,6 +171,7 @@ DarwinGamepadService::DeviceAdded(IOHIDDeviceRef device)
sprintf(buffer, "%x-%x-%s", vendorId, productId, product_name);
nsRefPtr<GamepadService> service(GamepadService::GetService());
mGamepads[slot].mSuperIndex = service->AddGamepad(buffer,
mozilla::dom::NoMapping,
(int)mGamepads[slot].numButtons(),
(int)mGamepads[slot].numAxes());
}

Просмотреть файл

@ -140,6 +140,7 @@ LinuxGamepadService::AddDevice(struct udev_device* dev)
nsRefPtr<GamepadService> service(GamepadService::GetService());
gamepad.index = service->AddGamepad(gamepad.idstring,
mozilla::dom::NoMapping,
gamepad.numButtons,
gamepad.numAxes);

Просмотреть файл

@ -203,6 +203,7 @@ public:
nsRefPtr<GamepadService> gamepadsvc(GamepadService::GetService());
if (mType == Added) {
mGamepad.id = gamepadsvc->AddGamepad(mGamepad.idstring,
mozilla::dom::NoMapping,
mGamepad.numButtons,
mGamepad.numAxes +
mGamepad.numHats*2);