| 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/gaming_seat_ozone.h" |
| 6 #include "components/exo/gamepad_delegate.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 constexpr int kMaxGamepadCount = 4; |
| 14 } // namespace |
| 15 |
| 16 namespace exo { |
| 17 |
| 18 GamingSeatOzone::GamingSeatOzone(GamingSeatDelegate* delegate) |
| 19 : delegate_(delegate), |
| 20 gamepad_provider_(ui::GamepadProviderOzone::GetInstance()) { |
| 21 auto* helper = WMHelper::GetInstance(); |
| 22 helper->AddFocusObserver(this); |
| 23 OnWindowFocused(helper->GetFocusedWindow(), nullptr); |
| 24 } |
| 25 |
| 26 GamingSeatOzone::~GamingSeatOzone() { |
| 27 gamepad_provider_->RemoveGamepadObserver(this); |
| 28 delegate_->OnGamingSeatDestroying(this); |
| 29 // Disconnect all the gamepads. |
| 30 for (auto& entry : gamepads_) { |
| 31 entry.second->OnRemoved(); |
| 32 } |
| 33 |
| 34 WMHelper::GetInstance()->RemoveFocusObserver(this); |
| 35 } |
| 36 |
| 37 void GamingSeatOzone::OnGamepadDevicesUpdated() { |
| 38 std::vector<ui::InputDevice> gamepad_devices = |
| 39 gamepad_provider_->GetGamepadDevices(); |
| 40 |
| 41 std::map<int, GamepadDelegate*> old_gamepad_map; |
| 42 old_gamepad_map.swap(gamepads_); |
| 43 |
| 44 // Copy the "still connected gamepads". |
| 45 for (auto& device : gamepad_devices) { |
| 46 auto ite = old_gamepad_map.find(device.id); |
| 47 if (ite != old_gamepad_map.end()) { |
| 48 gamepads_[device.id] = ite->second; |
| 49 old_gamepad_map.erase(ite); |
| 50 } |
| 51 } |
| 52 |
| 53 // Send event for the disconected gamepads. |
| 54 for (auto& entry : old_gamepad_map) { |
| 55 entry.second->OnRemoved(); |
| 56 } |
| 57 |
| 58 // Connect new connected gamepads if there is a slot. |
| 59 for (auto& device : gamepad_devices) { |
| 60 if (gamepads_.size() >= kMaxGamepadCount) { |
| 61 break; |
| 62 } |
| 63 if (gamepads_.find(device.id) == gamepads_.end()) { |
| 64 gamepads_[device.id] = delegate_->GamepadAdded(); |
| 65 } |
| 66 } |
| 67 } |
| 68 |
| 69 void GamingSeatOzone::OnGamepadEvent(const ui::GamepadEvent& event) { |
| 70 auto ite = gamepads_.find(event.device_id()); |
| 71 if (ite == gamepads_.end()) |
| 72 return; |
| 73 |
| 74 switch (event.type()) { |
| 75 case ui::GamepadEventType::BUTTON: |
| 76 ite->second->OnButton(event.code(), event.value(), event.value()); |
| 77 break; |
| 78 case ui::GamepadEventType::AXIS: |
| 79 ite->second->OnAxis(event.code(), event.value()); |
| 80 break; |
| 81 case ui::GamepadEventType::FRAME: |
| 82 ite->second->OnFrame(); |
| 83 break; |
| 84 } |
| 85 } |
| 86 |
| 87 void GamingSeatOzone::OnWindowFocused(aura::Window* gained_focus, |
| 88 aura::Window* lost_focus) { |
| 89 Surface* target = nullptr; |
| 90 if (gained_focus) { |
| 91 target = Surface::AsSurface(gained_focus); |
| 92 if (!target) { |
| 93 aura::Window* top_level_window = gained_focus->GetToplevelWindow(); |
| 94 if (top_level_window) |
| 95 target = ShellSurface::GetMainSurface(top_level_window); |
| 96 } |
| 97 } |
| 98 |
| 99 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); |
| 100 Enable(focused); |
| 101 } |
| 102 |
| 103 void GamingSeatOzone::Enable(bool enable) { |
| 104 if (enable) { |
| 105 gamepad_provider_->AddGamepadObserver(this); |
| 106 OnGamepadDevicesUpdated(); |
| 107 } else { |
| 108 gamepad_provider_->RemoveGamepadObserver(this); |
| 109 } |
| 110 } |
| 111 |
| 112 } // namespace exo |
| OLD | NEW |