Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 #include "components/exo/gamepad_delegate.h" | |
| 6 #include "components/exo/gaming_seat.h" | |
| 7 #include "components/exo/gaming_seat_delegate.h" | |
| 8 #include "components/exo/shell_surface.h" | |
| 9 #include "components/exo/surface.h" | |
| 10 #include "ui/events/ozone/gamepad/gamepad_provider_ozone.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // This const is the max gamepad count supported by gaming seat. | |
| 15 // TODO(jkwang) Check if we should remove this constraint when ozone backend is | |
| 16 // default. | |
| 17 const int kMaxGamepadCount = 4; | |
|
reveman
2017/06/06 21:58:55
can we just not have this in the first place? I'm
jkwang
2017/06/07 18:03:19
The code below, yes. But the android counter part
reveman
2017/06/07 20:29:45
Let's not have code here that makes assumptions ab
jkwang
2017/06/07 22:01:45
Done.
| |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 21 namespace exo { | |
| 22 | |
| 23 GamingSeat::GamingSeat(GamingSeatDelegate* delegate, | |
| 24 base::SingleThreadTaskRunner* task_runner) | |
| 25 : delegate_(delegate), | |
| 26 gamepad_provider_(ui::GamepadProviderOzone::GetInstance()) { | |
| 27 auto* helper = WMHelper::GetInstance(); | |
| 28 helper->AddFocusObserver(this); | |
| 29 OnWindowFocused(helper->GetFocusedWindow(), nullptr); | |
| 30 } | |
| 31 | |
| 32 GamingSeat::~GamingSeat() { | |
| 33 gamepad_provider_->RemoveGamepadObserver(this); | |
| 34 delegate_->OnGamingSeatDestroying(this); | |
| 35 // Disconnect all the gamepads. | |
| 36 for (auto& entry : gamepads_) { | |
| 37 entry.second->OnRemoved(); | |
| 38 } | |
| 39 | |
| 40 WMHelper::GetInstance()->RemoveFocusObserver(this); | |
| 41 } | |
| 42 | |
| 43 void GamingSeat::OnWindowFocused(aura::Window* gained_focus, | |
| 44 aura::Window* lost_focus) { | |
| 45 Surface* target = nullptr; | |
| 46 if (gained_focus) { | |
| 47 target = Surface::AsSurface(gained_focus); | |
| 48 if (!target) { | |
| 49 aura::Window* top_level_window = gained_focus->GetToplevelWindow(); | |
| 50 if (top_level_window) | |
| 51 target = ShellSurface::GetMainSurface(top_level_window); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); | |
| 56 if (focused) { | |
| 57 gamepad_provider_->AddGamepadObserver(this); | |
| 58 OnGamepadDevicesUpdated(); | |
| 59 } else { | |
| 60 gamepad_provider_->RemoveGamepadObserver(this); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 //////////////////////////////////////////////////////////////////////////////// | |
| 65 // GamingSeat, Override functions for GamepadObserver. | |
| 66 void GamingSeat::OnGamepadDevicesUpdated() { | |
| 67 std::vector<ui::InputDevice> gamepad_devices = | |
| 68 gamepad_provider_->GetGamepadDevices(); | |
| 69 | |
| 70 base::flat_map<int, GamepadDelegate*> new_gamepads; | |
| 71 | |
| 72 // Copy the "still connected gamepads". | |
| 73 for (auto& device : gamepad_devices) { | |
| 74 auto it = gamepads_.find(device.id); | |
| 75 if (it != gamepads_.end()) { | |
| 76 new_gamepads[device.id] = it->second; | |
| 77 gamepads_.erase(it); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 // Remove each disconected gamepad. | |
| 82 for (auto& entry : gamepads_) | |
| 83 entry.second->OnRemoved(); | |
| 84 | |
| 85 // Connect new connected gamepads if there is a slot. | |
| 86 for (auto& device : gamepad_devices) { | |
| 87 if (new_gamepads.size() >= kMaxGamepadCount) | |
| 88 break; | |
| 89 | |
| 90 if (new_gamepads.find(device.id) == new_gamepads.end()) | |
| 91 new_gamepads[device.id] = delegate_->GamepadAdded(); | |
| 92 } | |
| 93 | |
| 94 new_gamepads.swap(gamepads_); | |
| 95 } | |
| 96 | |
| 97 void GamingSeat::OnGamepadEvent(const ui::GamepadEvent& event) { | |
| 98 auto it = gamepads_.find(event.device_id()); | |
| 99 if (it == gamepads_.end()) | |
| 100 return; | |
| 101 | |
| 102 switch (event.type()) { | |
| 103 case ui::GamepadEventType::BUTTON: | |
| 104 it->second->OnButton(event.code(), event.value(), event.value()); | |
| 105 break; | |
| 106 case ui::GamepadEventType::AXIS: | |
| 107 it->second->OnAxis(event.code(), event.value()); | |
| 108 break; | |
| 109 case ui::GamepadEventType::FRAME: | |
| 110 it->second->OnFrame(); | |
| 111 break; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 } // namespace exo | |
| OLD | NEW |