forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gamepad API support for chrome on android
This change adds code to get gamepad data from java objects and provide these updates to the GamepadProvider which then writes to GamepadHardwareBuffer which in turn is read by SharedMemoryReader and later returned by JS to the web page. Added singleton class GamepadsReader which is responsible for communication with java class and accessing gamepad data. It adds methods for communication with singleton java GamepadList class to get gamepads data. This also adds new framework classes/methods required for Gamepad API support. Frameworks changes are responsible for : - Identifying gamepad devices and their capabilities. - Managing connected gamepad devices - Map the connected gamepad devices to standard Gamepad format. - Keeping gamepads axes/buttons data up-to-date and returning it to native whenever requested. In android we cannot get gamepad data directly from sources, so framework is modified to capture gamepad key and motion events and extract gamepad data from these events. * Class GamepadPlatformDataFetcherAndroid : Android specific implementation of gamepad data fetcher. * Class ContentViewCore : Manages gamepad list and notifies of new key/motion event for gamepads. * Class GamepadList : A new class to manage connected gamepad devices * Class GamepadDevice : A new class to manage information related to each gamepad device. * Class GamepadMappings : This class is responsible for mapping of known gamepads to the standard gamepad. This change enables gamepad API by default. Adds support for parsing float array return type in JNI generator. NVIDIA Shield and XBox360 gamepads are mapped to the standard gamepad BUG=330094 TEST=http://www.html5rocks.com/en/tutorials/doodles/gamepad/gamepad-tester/tester.html R=tsepez@chromium.org R=darin@chromium.org Review URL: https://codereview.chromium.org/133943002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270620 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
skhatri@nvidia.com
committed
May 15, 2014
1 parent
59686ac
commit e468a5b
Showing
20 changed files
with
897 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2014 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// This file intentionally does not have header guards, it's included | ||
// inside a macro to generate enum values. | ||
|
||
// This file defines the canonical axes mapping order for gamepad-like devices. | ||
|
||
// TODO(SaurabhK): Consolidate with CanonicalAxisIndex enum in | ||
// gamepad_standard_mappings.h, crbug.com/351558. | ||
CANONICAL_AXIS_INDEX(AXIS_LEFT_STICK_X, 0) | ||
CANONICAL_AXIS_INDEX(AXIS_LEFT_STICK_Y, 1) | ||
CANONICAL_AXIS_INDEX(AXIS_RIGHT_STICK_X, 2) | ||
CANONICAL_AXIS_INDEX(AXIS_RIGHT_STICK_Y, 3) | ||
CANONICAL_AXIS_INDEX(NUM_CANONICAL_AXES, 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2014 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// This file intentionally does not have header guards, it's included | ||
// inside a macro to generate enum values. | ||
// This defines the canonical button mapping order for gamepad-like devices. | ||
|
||
// TODO(SaurabhK): Consolidate with CanonicalButtonIndex enum in | ||
// gamepad_standard_mappings.h, crbug.com/351558. | ||
CANONICAL_BUTTON_INDEX(BUTTON_PRIMARY, 0) | ||
CANONICAL_BUTTON_INDEX(BUTTON_SECONDARY, 1) | ||
CANONICAL_BUTTON_INDEX(BUTTON_TERTIARY, 2) | ||
CANONICAL_BUTTON_INDEX(BUTTON_QUATERNARY, 3) | ||
CANONICAL_BUTTON_INDEX(BUTTON_LEFT_SHOULDER, 4) | ||
CANONICAL_BUTTON_INDEX(BUTTON_RIGHT_SHOULDER, 5) | ||
CANONICAL_BUTTON_INDEX(BUTTON_LEFT_TRIGGER, 6) | ||
CANONICAL_BUTTON_INDEX(BUTTON_RIGHT_TRIGGER, 7) | ||
CANONICAL_BUTTON_INDEX(BUTTON_BACK_SELECT, 8) | ||
CANONICAL_BUTTON_INDEX(BUTTON_START, 9) | ||
CANONICAL_BUTTON_INDEX(BUTTON_LEFT_THUMBSTICK, 10) | ||
CANONICAL_BUTTON_INDEX(BUTTON_RIGHT_THUMBSTICK, 11) | ||
CANONICAL_BUTTON_INDEX(BUTTON_DPAD_UP, 12) | ||
CANONICAL_BUTTON_INDEX(BUTTON_DPAD_DOWN, 13) | ||
CANONICAL_BUTTON_INDEX(BUTTON_DPAD_LEFT, 14) | ||
CANONICAL_BUTTON_INDEX(BUTTON_DPAD_RIGHT, 15) | ||
CANONICAL_BUTTON_INDEX(BUTTON_META, 16) | ||
CANONICAL_BUTTON_INDEX(NUM_CANONICAL_BUTTONS, 17) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
content/browser/gamepad/gamepad_platform_data_fetcher_android.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2014 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "content/browser/gamepad/gamepad_platform_data_fetcher_android.h" | ||
|
||
#include "base/android/jni_android.h" | ||
#include "base/android/jni_array.h" | ||
#include "base/android/jni_string.h" | ||
#include "base/debug/trace_event.h" | ||
#include "base/strings/string_number_conversions.h" | ||
#include "base/strings/string_util.h" | ||
#include "base/strings/utf_string_conversions.h" | ||
|
||
#include "jni/GamepadList_jni.h" | ||
|
||
#include "third_party/WebKit/public/platform/WebGamepads.h" | ||
|
||
using base::android::AttachCurrentThread; | ||
using base::android::CheckException; | ||
using base::android::ClearException; | ||
using base::android::ConvertJavaStringToUTF8; | ||
using base::android::ScopedJavaLocalRef; | ||
using blink::WebGamepad; | ||
using blink::WebGamepads; | ||
|
||
namespace content { | ||
|
||
bool | ||
GamepadPlatformDataFetcherAndroid::RegisterGamepadPlatformDataFetcherAndroid( | ||
JNIEnv* env) { | ||
return RegisterNativesImpl(env); | ||
} | ||
|
||
GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() { | ||
PauseHint(false); | ||
} | ||
|
||
GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() { | ||
PauseHint(true); | ||
} | ||
|
||
void GamepadPlatformDataFetcherAndroid::GetGamepadData( | ||
blink::WebGamepads* pads, | ||
bool devices_changed_hint) { | ||
TRACE_EVENT0("GAMEPAD", "GetGamepadData"); | ||
|
||
pads->length = 0; | ||
|
||
JNIEnv* env = AttachCurrentThread(); | ||
if (!env) | ||
return; | ||
|
||
Java_GamepadList_updateGamepadData(env, reinterpret_cast<intptr_t>(pads)); | ||
} | ||
|
||
void GamepadPlatformDataFetcherAndroid::PauseHint(bool paused) { | ||
JNIEnv* env = AttachCurrentThread(); | ||
if (!env) | ||
return; | ||
|
||
Java_GamepadList_notifyForGamepadsAccess(env, paused); | ||
} | ||
|
||
static void SetGamepadData(JNIEnv* env, | ||
jobject obj, | ||
jlong gamepads, | ||
jint index, | ||
jboolean mapping, | ||
jboolean connected, | ||
jstring devicename, | ||
jlong timestamp, | ||
jfloatArray jaxes, | ||
jfloatArray jbuttons) { | ||
DCHECK(gamepads); | ||
blink::WebGamepads* pads = reinterpret_cast<WebGamepads*>(gamepads); | ||
DCHECK_EQ(pads->length, unsigned(index)); | ||
DCHECK_LT(index, static_cast<int>(blink::WebGamepads::itemsLengthCap)); | ||
|
||
++pads->length; | ||
|
||
blink::WebGamepad& pad = pads->items[index]; | ||
|
||
pad.connected = connected; | ||
|
||
pad.timestamp = timestamp; | ||
|
||
// Do not set gamepad parameters for all the gamepad devices that are not | ||
// attached. | ||
if (!connected) | ||
return; | ||
|
||
// Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should | ||
// be mapped to vendor and product information but it is only available at | ||
// kernel level and it can not be queried using class | ||
// android.hardware.input.InputManager. | ||
// TODO(SaurabhK): Store a cached WebGamePad object in | ||
// GamepadPlatformDataFetcherAndroid and only update constant WebGamepad | ||
// values when a device has changed. | ||
base::string16 device_name; | ||
base::android::ConvertJavaStringToUTF16(env, devicename, &device_name); | ||
const size_t name_to_copy = | ||
std::min(device_name.size(), WebGamepad::idLengthCap - 1); | ||
memcpy(pad.id, | ||
device_name.data(), | ||
name_to_copy * sizeof(base::string16::value_type)); | ||
pad.id[name_to_copy] = 0; | ||
|
||
base::string16 mapping_name = base::UTF8ToUTF16(mapping ? "standard" : ""); | ||
const size_t mapping_to_copy = | ||
std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1); | ||
memcpy(pad.mapping, | ||
mapping_name.data(), | ||
mapping_to_copy * sizeof(base::string16::value_type)); | ||
pad.mapping[mapping_to_copy] = 0; | ||
|
||
pad.timestamp = timestamp; | ||
|
||
std::vector<float> axes; | ||
base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes); | ||
|
||
// Set WebGamepad axeslength to total number of axes on the gamepad device. | ||
// Only return the first axesLengthCap if axeslength captured by GamepadList | ||
// is larger than axesLengthCap. | ||
pad.axesLength = std::min(static_cast<int>(axes.size()), | ||
static_cast<int>(WebGamepad::axesLengthCap)); | ||
|
||
memcpy(pad.axes, axes.begin(), pad.axesLength * sizeof(float)); | ||
|
||
std::vector<float> buttons; | ||
base::android::JavaFloatArrayToFloatVector(env, jbuttons, &buttons); | ||
|
||
// Set WebGamepad buttonslength to total number of axes on the gamepad | ||
// device. Only return the first buttonsLengthCap if axeslength captured by | ||
// GamepadList is larger than buttonsLengthCap. | ||
pad.buttonsLength = std::min(static_cast<int>(buttons.size()), | ||
static_cast<int>(WebGamepad::buttonsLengthCap)); | ||
|
||
// Copy buttons state to the WebGamepad buttons[]. | ||
for (unsigned int j = 0; j < pad.buttonsLength; j++) { | ||
pad.buttons[j].pressed = buttons[j]; | ||
pad.buttons[j].value = buttons[j]; | ||
} | ||
} | ||
|
||
} // namespace content |
40 changes: 40 additions & 0 deletions
40
content/browser/gamepad/gamepad_platform_data_fetcher_android.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2014 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Define the data fetcher that GamepadProvider will use for android port. | ||
// (GamepadPlatformDataFetcher). | ||
|
||
#ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_ANDROID_H_ | ||
#define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_ANDROID_H_ | ||
|
||
#include <jni.h> | ||
|
||
#include "base/android/jni_android.h" | ||
#include "content/browser/gamepad/gamepad_data_fetcher.h" | ||
#include "content/browser/gamepad/gamepad_provider.h" | ||
#include "content/browser/gamepad/gamepad_standard_mappings.h" | ||
#include "third_party/WebKit/public/platform/WebGamepads.h" | ||
|
||
namespace content { | ||
|
||
class GamepadPlatformDataFetcherAndroid : public GamepadDataFetcher { | ||
public: | ||
GamepadPlatformDataFetcherAndroid(); | ||
virtual ~GamepadPlatformDataFetcherAndroid(); | ||
|
||
virtual void PauseHint(bool paused) OVERRIDE; | ||
|
||
virtual void GetGamepadData(blink::WebGamepads* pads, | ||
bool devices_changed_hint) OVERRIDE; | ||
|
||
// Registers the JNI methods for GamepadsReader. | ||
static bool RegisterGamepadPlatformDataFetcherAndroid(JNIEnv* env); | ||
|
||
private: | ||
DISALLOW_COPY_AND_ASSIGN(GamepadPlatformDataFetcherAndroid); | ||
}; | ||
|
||
} // namespace content | ||
|
||
#endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_ANDROID_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.