Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: components/exo/gaming_seat_ozone.cc

Issue 2900773003: Allow gaming_seat to use ozone gamepad as back-end (Closed)
Patch Set: Add gaming_seat_ozone to exo Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {
reveman 2017/06/05 22:48:04 nit: blank line after this
jkwang 2017/06/06 20:03:09 Done.
13 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.
14 } // namespace
reveman 2017/06/05 22:48:04 nit: blank line before this
jkwang 2017/06/06 20:03:09 Done.
15
16 namespace exo {
17
18 base::Thread* GamingSeat::CreatePollingThreadIfNeeded() {
19 return nullptr;
20 }
21
22 GamingSeat::GamingSeat(GamingSeatDelegate* delegate,
23 base::Thread* polling_thread)
24 : delegate_(delegate),
25 gamepad_provider_(ui::GamepadProviderOzone::GetInstance()) {
26 DCHECK(!polling_thread);
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) {
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
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 Enable(focused);
57 }
58
59 ////////////////////////////////////////////////////////////////////////////////
60 // GamingSeat, Override functions for GamepadObserver.
61 void GamingSeat::OnGamepadDevicesUpdated() {
62 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
63 gamepad_provider_->GetGamepadDevices();
64
65 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.
66 old_gamepad_map.swap(gamepads_);
67
68 // Copy the "still connected gamepads".
69 for (auto& device : gamepad_devices) {
70 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.
71 if (ite != old_gamepad_map.end()) {
72 gamepads_[device.id] = ite->second;
73 old_gamepad_map.erase(ite);
74 }
75 }
76
77 // 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.
78 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.
79 entry.second->OnRemoved();
80 }
81
82 // Connect new connected gamepads if there is a slot.
83 for (auto& device : gamepad_devices) {
84 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
85 break;
86 }
87 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.
88 gamepads_[device.id] = delegate_->GamepadAdded();
89 }
90 }
91 }
92
93 void GamingSeat::OnGamepadEvent(const ui::GamepadEvent& event) {
94 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.
95 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.
96 return;
97
98 switch (event.type()) {
99 case ui::GamepadEventType::BUTTON:
100 ite->second->OnButton(event.code(), event.value(), event.value());
101 break;
102 case ui::GamepadEventType::AXIS:
103 ite->second->OnAxis(event.code(), event.value());
104 break;
105 case ui::GamepadEventType::FRAME:
106 ite->second->OnFrame();
107 break;
108 }
109 }
110
111 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.
112 if (enable) {
113 gamepad_provider_->AddGamepadObserver(this);
114 OnGamepadDevicesUpdated();
115 } else {
116 gamepad_provider_->RemoveGamepadObserver(this);
117 }
118 }
119
120 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698