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 // | |
|
reveman
2017/06/08 00:21:44
remove "//" from this line
| |
| 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 exo { | |
| 13 | |
| 14 //////////////////////////////////////////////////////////////////////////////// | |
| 15 // GamingSeat, plublic: | |
|
reveman
2017/06/08 00:21:44
public:
| |
| 16 | |
| 17 GamingSeat::GamingSeat(GamingSeatDelegate* delegate, | |
| 18 base::SingleThreadTaskRunner* task_runner) | |
| 19 : delegate_(delegate) { | |
| 20 auto* helper = WMHelper::GetInstance(); | |
| 21 helper->AddFocusObserver(this); | |
| 22 OnWindowFocused(helper->GetFocusedWindow(), nullptr); | |
| 23 } | |
| 24 | |
| 25 GamingSeat::~GamingSeat() { | |
| 26 ui::GamepadProviderOzone::GetInstance()->RemoveGamepadObserver(this); | |
| 27 delegate_->OnGamingSeatDestroying(this); | |
| 28 // Disconnect all the gamepads. | |
| 29 for (auto& entry : gamepads_) { | |
|
reveman
2017/06/08 00:21:44
nit: no need for {}
| |
| 30 entry.second->OnRemoved(); | |
| 31 } | |
| 32 | |
| 33 WMHelper::GetInstance()->RemoveFocusObserver(this); | |
| 34 } | |
| 35 | |
| 36 //////////////////////////////////////////////////////////////////////////////// | |
| 37 // WMHelper::FocusObserver overrides: | |
| 38 | |
| 39 void GamingSeat::OnWindowFocused(aura::Window* gained_focus, | |
| 40 aura::Window* lost_focus) { | |
| 41 Surface* target = nullptr; | |
| 42 if (gained_focus) { | |
| 43 target = Surface::AsSurface(gained_focus); | |
| 44 if (!target) { | |
| 45 aura::Window* top_level_window = gained_focus->GetToplevelWindow(); | |
| 46 if (top_level_window) | |
| 47 target = ShellSurface::GetMainSurface(top_level_window); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); | |
| 52 if (focused) { | |
| 53 ui::GamepadProviderOzone::GetInstance()->AddGamepadObserver(this); | |
| 54 OnGamepadDevicesUpdated(); | |
| 55 } else { | |
| 56 ui::GamepadProviderOzone::GetInstance()->RemoveGamepadObserver(this); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 //////////////////////////////////////////////////////////////////////////////// | |
| 61 // ui::GamepadObserver overrides: | |
| 62 | |
| 63 void GamingSeat::OnGamepadDevicesUpdated() { | |
| 64 std::vector<ui::InputDevice> gamepad_devices = | |
| 65 ui::GamepadProviderOzone::GetInstance()->GetGamepadDevices(); | |
| 66 | |
| 67 base::flat_map<int, GamepadDelegate*> new_gamepads; | |
| 68 | |
| 69 // Copy the "still connected gamepads". | |
| 70 for (auto& device : gamepad_devices) { | |
| 71 auto it = gamepads_.find(device.id); | |
| 72 if (it != gamepads_.end()) { | |
| 73 new_gamepads[device.id] = it->second; | |
| 74 gamepads_.erase(it); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 // Remove each disconected gamepad. | |
| 79 for (auto& entry : gamepads_) | |
| 80 entry.second->OnRemoved(); | |
| 81 | |
| 82 // Add each new connected gamepad. | |
| 83 for (auto& device : gamepad_devices) { | |
| 84 if (new_gamepads.find(device.id) == new_gamepads.end()) | |
| 85 new_gamepads[device.id] = delegate_->GamepadAdded(); | |
| 86 } | |
| 87 | |
| 88 new_gamepads.swap(gamepads_); | |
| 89 } | |
| 90 | |
| 91 void GamingSeat::OnGamepadEvent(const ui::GamepadEvent& event) { | |
| 92 auto it = gamepads_.find(event.device_id()); | |
| 93 if (it == gamepads_.end()) | |
| 94 return; | |
| 95 | |
| 96 switch (event.type()) { | |
| 97 case ui::GamepadEventType::BUTTON: | |
| 98 it->second->OnButton(event.code(), event.value(), event.value()); | |
| 99 break; | |
| 100 case ui::GamepadEventType::AXIS: | |
| 101 it->second->OnAxis(event.code(), event.value()); | |
| 102 break; | |
| 103 case ui::GamepadEventType::FRAME: | |
| 104 it->second->OnFrame(); | |
| 105 break; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 } // namespace exo | |
| OLD | NEW |