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