Chromium Code Reviews| Index: components/exo/gamepad.cc |
| diff --git a/components/exo/gamepad.cc b/components/exo/gamepad.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0528be31ce5bae10dc4e3d52d0ccf08a407c9736 |
| --- /dev/null |
| +++ b/components/exo/gamepad.cc |
| @@ -0,0 +1,171 @@ |
| +// Copyright 2016 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 "components/exo/gamepad.h" |
| + |
| +#include <cmath> |
| + |
| +#include "ash/shell.h" |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/threading/thread.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "components/exo/gamepad_delegate.h" |
| +#include "components/exo/shell_surface.h" |
| +#include "components/exo/surface.h" |
| +#include "device/gamepad/gamepad_data_fetcher.h" |
| +#include "device/gamepad/gamepad_platform_data_fetcher.h" |
| +#include "ui/aura/client/focus_client.h" |
| +#include "ui/aura/window.h" |
| + |
| +namespace exo { |
| +namespace { |
| + |
| +constexpr double kGamepadButtonValueEpsilon = 0.001; |
| +bool GamepadButtonValuesAreEqual(double a, double b) { |
| + return fabs(a - b) < kGamepadButtonValueEpsilon; |
| +} |
| + |
| +// Time between gamepad polls in milliseconds. |
| +constexpr unsigned kPollingTimeIntervalMs = 16; |
|
reveman
2016/06/30 18:06:11
I'm still not sure we can land this unless we can
denniskempin
2016/07/01 02:34:35
I've done some experimenting.
What we can do is
reveman
2016/07/07 22:14:02
Thanks for looking into this. This sgtm.
|
| + |
| +} // namespace |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// Gamepad, public: |
| + |
| +Gamepad::Gamepad(GamepadDelegate* delegate) : Gamepad(delegate, nullptr) {} |
| + |
| +Gamepad::Gamepad(GamepadDelegate* delegate, |
| + std::unique_ptr<device::GamepadDataFetcher> fetcher) |
| + : delegate_(delegate), |
| + origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| + polling_thread_(new base::Thread("Exo gamepad polling thread")), |
| + fetcher_(std::move(fetcher)), |
| + weak_factory_(this) { |
| + polling_thread_checker_.DetachFromThread(); |
| + weak_this_ = weak_factory_.GetWeakPtr(); |
| + |
| + aura::client::FocusClient* focus_client = |
| + aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); |
| + focus_client->AddObserver(this); |
| + OnWindowFocused(focus_client->GetFocusedWindow(), nullptr); |
| + |
| + polling_thread_->StartWithOptions( |
| + base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| + polling_thread_->task_runner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&Gamepad::InitializePollingThread, base::Unretained(this))); |
| +} |
| + |
| +Gamepad::~Gamepad() { |
| + polling_thread_->Stop(); |
| + delegate_->OnGamepadDestroying(this); |
| + aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()) |
| + ->RemoveObserver(this); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// aura::client::FocusChangeObserver overrides: |
| + |
| +void Gamepad::OnWindowFocused(aura::Window* gained_focus, |
| + aura::Window* lost_focus) { |
| + DCHECK(origin_thread_checker_.CalledOnValidThread()); |
| + Surface* target = nullptr; |
| + if (gained_focus) { |
| + target = Surface::AsSurface(gained_focus); |
| + if (!target) { |
| + aura::Window* top_level_window = gained_focus->GetToplevelWindow(); |
| + if (top_level_window) |
| + target = ShellSurface::GetMainSurface(top_level_window); |
| + } |
| + } |
| + exo_focused_ = target && delegate_->CanAcceptGamepadEventsForSurface(target); |
|
reveman
2016/06/30 18:06:11
What if the target is destroyed? Don't we need to
denniskempin
2016/07/01 02:34:35
Shouldn't we get an OnWindowFocused event in that
reveman
2016/07/07 22:14:02
Probably fine for now but it would be good in the
|
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// Gamepad, private: |
| + |
| +void Gamepad::ScheduleOnPoll() { |
| + polling_thread_->task_runner()->PostDelayedTask( |
| + FROM_HERE, base::Bind(&Gamepad::OnPoll, base::Unretained(this)), |
| + base::TimeDelta::FromMilliseconds(kPollingTimeIntervalMs)); |
| +} |
| + |
| +void Gamepad::InitializePollingThread() { |
| + DCHECK(polling_thread_checker_.CalledOnValidThread()); |
| + // The platform data fetcher has to be initialized on the polling thread. |
| + if (!fetcher_) |
| + fetcher_.reset(new device::GamepadPlatformDataFetcher()); |
| + ScheduleOnPoll(); |
| +} |
| + |
| +void Gamepad::OnPoll() { |
| + DCHECK(polling_thread_checker_.CalledOnValidThread()); |
| + DCHECK(fetcher_); |
| + |
| + blink::WebGamepads new_state = state_; |
| + fetcher_->GetGamepadData(&new_state, false); |
| + |
| + if (std::max(new_state.length, state_.length) > 0) { |
| + if (new_state.items[0].connected != state_.items[0].connected || |
| + new_state.items[0].timestamp > state_.items[0].timestamp) { |
| + origin_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&Gamepad::PostGamepadChanges, weak_this_, |
| + new_state.items[0])); |
| + } |
| + } |
| + |
| + state_ = new_state; |
| + ScheduleOnPoll(); |
| +} |
| + |
| +void Gamepad::PostGamepadChanges(const blink::WebGamepad new_pad) { |
| + DCHECK(origin_thread_checker_.CalledOnValidThread()); |
| + bool send_frame = false; |
| + |
| + // Do not update internal state, nor send updates until we are focused again. |
| + if (!exo_focused_) |
| + return; |
| + |
| + // Update connection state. |
| + if (new_pad.connected != pad_state_.connected) { |
| + delegate_->OnStateChange(new_pad.connected); |
| + } |
| + |
| + if (!new_pad.connected || new_pad.timestamp <= pad_state_.timestamp) { |
| + pad_state_ = new_pad; |
| + return; |
| + } |
| + |
| + // Notify delegate of updated axes. |
| + for (size_t axis = 0; |
| + axis < std::max(pad_state_.axesLength, new_pad.axesLength); ++axis) { |
| + if (!GamepadButtonValuesAreEqual(new_pad.axes[axis], |
| + pad_state_.axes[axis])) { |
| + send_frame = true; |
| + delegate_->OnAxis(axis, new_pad.axes[axis]); |
| + } |
| + } |
| + |
| + // Notify delegate of updated buttons. |
| + for (size_t button_id = 0; |
| + button_id < std::max(pad_state_.buttonsLength, new_pad.buttonsLength); |
| + ++button_id) { |
| + auto button = pad_state_.buttons[button_id]; |
| + auto new_button = new_pad.buttons[button_id]; |
| + if (button.pressed != new_button.pressed || |
| + !GamepadButtonValuesAreEqual(button.value, new_button.value)) { |
| + send_frame = true; |
| + delegate_->OnButton(button_id, new_button.pressed, new_button.value); |
| + } |
| + } |
| + if (send_frame) |
| + delegate_->OnFrame(); |
| + |
| + pad_state_ = new_pad; |
| +} |
| + |
| +} // namespace exo |