| Index: components/exo/gaming_seat_ozone.cc
|
| diff --git a/components/exo/gaming_seat_ozone.cc b/components/exo/gaming_seat_ozone.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c93e888a0806bc9e87e4d321e5670ba659390867
|
| --- /dev/null
|
| +++ b/components/exo/gaming_seat_ozone.cc
|
| @@ -0,0 +1,112 @@
|
| +// 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 "components/exo/gaming_seat_ozone.h"
|
| +#include "components/exo/gamepad_delegate.h"
|
| +#include "components/exo/gaming_seat_delegate.h"
|
| +#include "components/exo/shell_surface.h"
|
| +#include "components/exo/surface.h"
|
| +#include "ui/events/ozone/gamepad/gamepad_provider_ozone.h"
|
| +
|
| +namespace {
|
| +constexpr int kMaxGamepadCount = 4;
|
| +} // namespace
|
| +
|
| +namespace exo {
|
| +
|
| +GamingSeatOzone::GamingSeatOzone(GamingSeatDelegate* delegate)
|
| + : delegate_(delegate),
|
| + gamepad_provider_(ui::GamepadProviderOzone::GetInstance()) {
|
| + auto* helper = WMHelper::GetInstance();
|
| + helper->AddFocusObserver(this);
|
| + OnWindowFocused(helper->GetFocusedWindow(), nullptr);
|
| +}
|
| +
|
| +GamingSeatOzone::~GamingSeatOzone() {
|
| + gamepad_provider_->RemoveGamepadObserver(this);
|
| + delegate_->OnGamingSeatDestroying(this);
|
| + // Disconnect all the gamepads.
|
| + for (auto& entry : gamepads_) {
|
| + entry.second->OnRemoved();
|
| + }
|
| +
|
| + WMHelper::GetInstance()->RemoveFocusObserver(this);
|
| +}
|
| +
|
| +void GamingSeatOzone::OnGamepadDevicesUpdated() {
|
| + std::vector<ui::InputDevice> gamepad_devices =
|
| + gamepad_provider_->GetGamepadDevices();
|
| +
|
| + std::map<int, GamepadDelegate*> old_gamepad_map;
|
| + old_gamepad_map.swap(gamepads_);
|
| +
|
| + // Copy the "still connected gamepads".
|
| + for (auto& device : gamepad_devices) {
|
| + auto ite = old_gamepad_map.find(device.id);
|
| + if (ite != old_gamepad_map.end()) {
|
| + gamepads_[device.id] = ite->second;
|
| + old_gamepad_map.erase(ite);
|
| + }
|
| + }
|
| +
|
| + // Send event for the disconected gamepads.
|
| + for (auto& entry : old_gamepad_map) {
|
| + entry.second->OnRemoved();
|
| + }
|
| +
|
| + // Connect new connected gamepads if there is a slot.
|
| + for (auto& device : gamepad_devices) {
|
| + if (gamepads_.size() >= kMaxGamepadCount) {
|
| + break;
|
| + }
|
| + if (gamepads_.find(device.id) == gamepads_.end()) {
|
| + gamepads_[device.id] = delegate_->GamepadAdded();
|
| + }
|
| + }
|
| +}
|
| +
|
| +void GamingSeatOzone::OnGamepadEvent(const ui::GamepadEvent& event) {
|
| + auto ite = gamepads_.find(event.device_id());
|
| + if (ite == gamepads_.end())
|
| + return;
|
| +
|
| + switch (event.type()) {
|
| + case ui::GamepadEventType::BUTTON:
|
| + ite->second->OnButton(event.code(), event.value(), event.value());
|
| + break;
|
| + case ui::GamepadEventType::AXIS:
|
| + ite->second->OnAxis(event.code(), event.value());
|
| + break;
|
| + case ui::GamepadEventType::FRAME:
|
| + ite->second->OnFrame();
|
| + break;
|
| + }
|
| +}
|
| +
|
| +void GamingSeatOzone::OnWindowFocused(aura::Window* gained_focus,
|
| + aura::Window* lost_focus) {
|
| + 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);
|
| + }
|
| + }
|
| +
|
| + bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target);
|
| + Enable(focused);
|
| +}
|
| +
|
| +void GamingSeatOzone::Enable(bool enable) {
|
| + if (enable) {
|
| + gamepad_provider_->AddGamepadObserver(this);
|
| + OnGamepadDevicesUpdated();
|
| + } else {
|
| + gamepad_provider_->RemoveGamepadObserver(this);
|
| + }
|
| +}
|
| +
|
| +} // namespace exo
|
|
|