Commit 7c498e59 authored by Matt Reynolds's avatar Matt Reynolds Committed by Commit Bot

Add a base class for haptic gamepad functionality

AbstractHapticGamepad encapsulates the logic for translating haptic
effect descriptions into a sequence of vibration actuator commands.
Users of AbstractHapticGamepad should override SetVibration so that it
updates the vibration actuators on the device with the specified
intensities.

BUG=749295

Change-Id: I3744cd3b06efb7970bd87048996250e52c472919
Reviewed-on: https://chromium-review.googlesource.com/810006Reviewed-by: default avatarBrandon Jones <bajones@chromium.org>
Commit-Queue: Matt Reynolds <mattreynolds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522221}
parent 4227e23c
...@@ -64,6 +64,7 @@ test("device_unittests") { ...@@ -64,6 +64,7 @@ test("device_unittests") {
"bluetooth/test/test_bluetooth_local_gatt_service_delegate.cc", "bluetooth/test/test_bluetooth_local_gatt_service_delegate.cc",
"bluetooth/test/test_bluetooth_local_gatt_service_delegate.h", "bluetooth/test/test_bluetooth_local_gatt_service_delegate.h",
"bluetooth/uribeacon/uri_encoder_unittest.cc", "bluetooth/uribeacon/uri_encoder_unittest.cc",
"gamepad/abstract_haptic_gamepad_unittest.cc",
"gamepad/gamepad_provider_unittest.cc", "gamepad/gamepad_provider_unittest.cc",
"gamepad/gamepad_service_unittest.cc", "gamepad/gamepad_service_unittest.cc",
"gamepad/public/interfaces/gamepad_struct_traits_unittest.cc", "gamepad/public/interfaces/gamepad_struct_traits_unittest.cc",
...@@ -280,7 +281,9 @@ source_set("usb_test_gadget") { ...@@ -280,7 +281,9 @@ source_set("usb_test_gadget") {
} }
if (is_android) { if (is_android) {
bluetooth_java_sources_needing_jni = [ "bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java" ] bluetooth_java_sources_needing_jni = [
"bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java",
]
generate_jni("bluetooth_test_jni_headers") { generate_jni("bluetooth_test_jni_headers") {
sources = bluetooth_java_sources_needing_jni sources = bluetooth_java_sources_needing_jni
......
...@@ -13,6 +13,8 @@ component("gamepad") { ...@@ -13,6 +13,8 @@ component("gamepad") {
output_name = "device_gamepad" output_name = "device_gamepad"
sources = [ sources = [
"abstract_haptic_gamepad.cc",
"abstract_haptic_gamepad.h",
"game_controller_data_fetcher_mac.h", "game_controller_data_fetcher_mac.h",
"game_controller_data_fetcher_mac.mm", "game_controller_data_fetcher_mac.mm",
"gamepad_consumer.cc", "gamepad_consumer.cc",
......
// Copyright 2017 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 "device/gamepad/abstract_haptic_gamepad.h"
namespace device {
AbstractHapticGamepad::AbstractHapticGamepad() : sequence_id_(0) {}
AbstractHapticGamepad::~AbstractHapticGamepad() {
if (playing_effect_callback_) {
SetZeroVibration();
RunCallbackOnMojoThread(
mojom::GamepadHapticsResult::GamepadHapticsResultPreempted);
}
}
void AbstractHapticGamepad::SetZeroVibration() {
SetVibration(0.0, 0.0);
}
base::TimeDelta AbstractHapticGamepad::TaskDelayFromMilliseconds(
double delay_millis) {
return base::TimeDelta::FromMillisecondsD(delay_millis);
}
void AbstractHapticGamepad::PlayEffect(
mojom::GamepadHapticEffectType type,
mojom::GamepadEffectParametersPtr params,
mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback callback) {
if (type !=
mojom::GamepadHapticEffectType::GamepadHapticEffectTypeDualRumble) {
// Only dual-rumble effects are supported.
std::move(callback).Run(
mojom::GamepadHapticsResult::GamepadHapticsResultNotSupported);
return;
}
int sequence_id = ++sequence_id_;
if (playing_effect_callback_) {
if (params->start_delay > 0.0)
SetZeroVibration();
RunCallbackOnMojoThread(
mojom::GamepadHapticsResult::GamepadHapticsResultPreempted);
}
playing_effect_task_runner_ = base::ThreadTaskRunnerHandle::Get();
playing_effect_callback_ = std::move(callback);
PlayDualRumbleEffect(sequence_id, params->duration, params->start_delay,
params->strong_magnitude, params->weak_magnitude);
}
void AbstractHapticGamepad::ResetVibration(
mojom::GamepadHapticsManager::ResetVibrationActuatorCallback callback) {
sequence_id_++;
if (playing_effect_callback_) {
SetZeroVibration();
RunCallbackOnMojoThread(
mojom::GamepadHapticsResult::GamepadHapticsResultPreempted);
}
std::move(callback).Run(
mojom::GamepadHapticsResult::GamepadHapticsResultComplete);
}
void AbstractHapticGamepad::PlayDualRumbleEffect(int sequence_id,
double duration,
double start_delay,
double strong_magnitude,
double weak_magnitude) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&AbstractHapticGamepad::StartVibration,
base::Unretained(this), sequence_id_, duration,
strong_magnitude, weak_magnitude),
TaskDelayFromMilliseconds(start_delay));
}
void AbstractHapticGamepad::StartVibration(int sequence_id,
double duration,
double strong_magnitude,
double weak_magnitude) {
if (sequence_id != sequence_id_)
return;
SetVibration(strong_magnitude, weak_magnitude);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&AbstractHapticGamepad::StopVibration,
base::Unretained(this), sequence_id),
TaskDelayFromMilliseconds(duration));
}
void AbstractHapticGamepad::StopVibration(int sequence_id) {
if (sequence_id != sequence_id_)
return;
SetZeroVibration();
RunCallbackOnMojoThread(
mojom::GamepadHapticsResult::GamepadHapticsResultComplete);
}
void AbstractHapticGamepad::RunCallbackOnMojoThread(
mojom::GamepadHapticsResult result) {
if (playing_effect_task_runner_->RunsTasksInCurrentSequence()) {
DoRunCallback(std::move(playing_effect_callback_), result);
return;
}
playing_effect_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&AbstractHapticGamepad::DoRunCallback,
std::move(playing_effect_callback_), result));
}
// static
void AbstractHapticGamepad::DoRunCallback(
mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback callback,
mojom::GamepadHapticsResult result) {
std::move(callback).Run(result);
}
} // namespace device
// Copyright 2017 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.
#ifndef DEVICE_GAMEPAD_ABSTRACT_HAPTIC_GAMEPAD_
#define DEVICE_GAMEPAD_ABSTRACT_HAPTIC_GAMEPAD_
#include "base/memory/scoped_refptr.h"
#include "base/sequenced_task_runner.h"
#include "base/time/time.h"
#include "device/gamepad/gamepad_export.h"
#include "device/gamepad/public/interfaces/gamepad.mojom.h"
namespace device {
// AbstractHapticGamepad is a base class for gamepads that support dual-rumble
// vibration effects. To use it, override the SetVibration method so that it
// sets the vibration intensity on the device. Then, calling PlayEffect or
// ResetVibration will call your SetVibration method at the appropriate times
// to produce the desired vibration effect. When the effect is complete, or when
// it has been preempted by another effect, the callback is invoked with a
// result code.
//
// By default, SetZeroVibration simply calls SetVibration with both parameters
// set to zero. You may optionally override SetZeroVibration if the device has a
// more efficient means of stopping an ongoing effect.
class DEVICE_GAMEPAD_EXPORT AbstractHapticGamepad {
public:
AbstractHapticGamepad();
virtual ~AbstractHapticGamepad();
// Start playing an effect.
void PlayEffect(
mojom::GamepadHapticEffectType,
mojom::GamepadEffectParametersPtr,
mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback);
// Reset vibration on the gamepad, perhaps interrupting an ongoing effect.
void ResetVibration(
mojom::GamepadHapticsManager::ResetVibrationActuatorCallback);
private:
// Set the vibration magnitude for the strong and weak vibration actuators.
virtual void SetVibration(double strong_magnitude, double weak_magnitude) = 0;
// Set the vibration magnitude for both actuators to zero.
virtual void SetZeroVibration();
// For testing.
virtual base::TimeDelta TaskDelayFromMilliseconds(double delay_millis);
void PlayDualRumbleEffect(int sequence_id,
double duration,
double start_delay,
double strong_magnitude,
double weak_magnitude);
void StartVibration(int sequence_id,
double duration,
double strong_magnitude,
double weak_magnitude);
void StopVibration(int sequence_id);
void RunCallbackOnMojoThread(mojom::GamepadHapticsResult result);
static void DoRunCallback(
mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback,
mojom::GamepadHapticsResult);
int sequence_id_;
scoped_refptr<base::SequencedTaskRunner> playing_effect_task_runner_;
mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback
playing_effect_callback_;
};
} // namespace device
#endif // DEVICE_GAMEPAD_ABSTRACT_HAPTIC_GAMEPAD_
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment