Chromium Code Reviews| 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..de1d2357c71b477c2979dd82e120de19bab8c861 |
| --- /dev/null |
| +++ b/components/exo/gaming_seat_ozone.cc |
| @@ -0,0 +1,120 @@ |
| +// 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/gamepad_delegate.h" |
| +#include "components/exo/gaming_seat.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 { |
|
reveman
2017/06/05 22:48:04
nit: blank line after this
jkwang
2017/06/06 20:03:09
Done.
|
| +constexpr int kMaxGamepadCount = 4; |
|
reveman
2017/06/05 22:48:04
nit: s/constexpr/const/ as that's more consistent
jkwang
2017/06/06 20:03:08
Done.
|
| +} // namespace |
|
reveman
2017/06/05 22:48:04
nit: blank line before this
jkwang
2017/06/06 20:03:09
Done.
|
| + |
| +namespace exo { |
| + |
| +base::Thread* GamingSeat::CreatePollingThreadIfNeeded() { |
| + return nullptr; |
| +} |
| + |
| +GamingSeat::GamingSeat(GamingSeatDelegate* delegate, |
| + base::Thread* polling_thread) |
| + : delegate_(delegate), |
| + gamepad_provider_(ui::GamepadProviderOzone::GetInstance()) { |
| + DCHECK(!polling_thread); |
| + auto* helper = WMHelper::GetInstance(); |
| + helper->AddFocusObserver(this); |
| + OnWindowFocused(helper->GetFocusedWindow(), nullptr); |
| +} |
| + |
| +GamingSeat::~GamingSeat() { |
| + gamepad_provider_->RemoveGamepadObserver(this); |
| + delegate_->OnGamingSeatDestroying(this); |
| + // Disconnect all the gamepads. |
| + for (auto& entry : gamepads_) { |
| + entry.second->OnRemoved(); |
| + } |
| + |
| + WMHelper::GetInstance()->RemoveFocusObserver(this); |
| +} |
| + |
| +void GamingSeat::OnWindowFocused(aura::Window* gained_focus, |
| + aura::Window* lost_focus) { |
| + Surface* target = nullptr; |
| + if (gained_focus) { |
| + target = Surface::AsSurface(gained_focus); |
| + if (!target) { |
|
reveman
2017/06/05 22:48:04
hm, why is this scope needed?
jkwang
2017/06/06 20:03:08
To be honest, I don't know. I am reusing the same
|
| + 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); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// GamingSeat, Override functions for GamepadObserver. |
| +void GamingSeat::OnGamepadDevicesUpdated() { |
| + std::vector<ui::InputDevice> gamepad_devices = |
|
reveman
2017/06/05 22:48:04
hm, would be nice if GetGamepadDevices could retur
jkwang
2017/06/06 20:03:09
Yes, it should! I have added it to my bug. But I w
|
| + gamepad_provider_->GetGamepadDevices(); |
| + |
| + std::map<int, GamepadDelegate*> old_gamepad_map; |
|
reveman
2017/06/05 22:48:04
nit: old_gamepads and I would prefer new_gamepads
jkwang
2017/06/06 20:03:08
Done.
|
| + old_gamepad_map.swap(gamepads_); |
| + |
| + // Copy the "still connected gamepads". |
| + for (auto& device : gamepad_devices) { |
| + auto ite = old_gamepad_map.find(device.id); |
|
reveman
2017/06/05 22:48:04
nit: s/ite/it/
jkwang
2017/06/06 20:03:09
Done.
|
| + if (ite != old_gamepad_map.end()) { |
| + gamepads_[device.id] = ite->second; |
| + old_gamepad_map.erase(ite); |
| + } |
| + } |
| + |
| + // Send event for the disconected gamepads. |
|
reveman
2017/06/05 22:48:04
nit: s/Send event for the/Remove each/
nit: s/disc
jkwang
2017/06/06 20:03:09
Done.
|
| + for (auto& entry : old_gamepad_map) { |
|
reveman
2017/06/05 22:48:04
nit: no need for {}
jkwang
2017/06/06 20:03:07
Done.
|
| + entry.second->OnRemoved(); |
| + } |
| + |
| + // Connect new connected gamepads if there is a slot. |
| + for (auto& device : gamepad_devices) { |
| + if (gamepads_.size() >= kMaxGamepadCount) { |
|
reveman
2017/06/05 22:48:04
nit: no need for {}
why do we need this kMaxGamep
jkwang
2017/06/06 20:03:08
/device/gamepad support up to 4 gamepads. So andro
|
| + break; |
| + } |
| + if (gamepads_.find(device.id) == gamepads_.end()) { |
|
reveman
2017/06/05 22:48:04
nit: no need for {}
jkwang
2017/06/06 20:03:09
Done.
|
| + gamepads_[device.id] = delegate_->GamepadAdded(); |
| + } |
| + } |
| +} |
| + |
| +void GamingSeat::OnGamepadEvent(const ui::GamepadEvent& event) { |
| + auto ite = gamepads_.find(event.device_id()); |
|
reveman
2017/06/05 22:48:04
nit: s/ite/it/
jkwang
2017/06/06 20:03:09
Done.
|
| + if (ite == gamepads_.end()) |
|
reveman
2017/06/05 22:48:04
DCHECK after removing kMaxGamepadCount if possible
jkwang
2017/06/06 20:03:08
Done.
|
| + 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 GamingSeat::Enable(bool enable) { |
|
reveman
2017/06/05 22:48:04
Do we need this function? Your only calling it fro
jkwang
2017/06/06 20:03:08
Done.
|
| + if (enable) { |
| + gamepad_provider_->AddGamepadObserver(this); |
| + OnGamepadDevicesUpdated(); |
| + } else { |
| + gamepad_provider_->RemoveGamepadObserver(this); |
| + } |
| +} |
| + |
| +} // namespace exo |