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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/exo/gaming_seat_ozone.cc
diff --git a/components/exo/gaming_seat_ozone.cc b/components/exo/gaming_seat_ozone.cc
new file mode 100644
index 0000000000000000000000000000000000000000..14304de87dd1c700c75516adccd96b3c9b183b9f
--- /dev/null
+++ b/components/exo/gaming_seat_ozone.cc
@@ -0,0 +1,115 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+#include "components/exo/gamepad_delegate.h"
+#include "components/exo/gaming_seat.h"
+#include "components/exo/gaming_seat_delegate.h"
+#include "components/exo/shell_surface.h"
+#include "components/exo/surface.h"
+#include "ui/events/ozone/gamepad/gamepad_provider_ozone.h"
+
+namespace {
+
+// This const is the max gamepad count supported by gaming seat.
+// TODO(jkwang) Check if we should remove this constraint when ozone backend is
+// default.
+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.
+
+} // namespace
+
+namespace exo {
+
+GamingSeat::GamingSeat(GamingSeatDelegate* delegate,
+ base::SingleThreadTaskRunner* task_runner)
+ : delegate_(delegate),
+ gamepad_provider_(ui::GamepadProviderOzone::GetInstance()) {
+ auto* helper = WMHelper::GetInstance();
+ helper->AddFocusObserver(this);
+ OnWindowFocused(helper->GetFocusedWindow(), nullptr);
+}
+
+GamingSeat::~GamingSeat() {
+ gamepad_provider_->RemoveGamepadObserver(this);
+ delegate_->OnGamingSeatDestroying(this);
+ // Disconnect all the gamepads.
+ for (auto& entry : gamepads_) {
+ entry.second->OnRemoved();
+ }
+
+ WMHelper::GetInstance()->RemoveFocusObserver(this);
+}
+
+void GamingSeat::OnWindowFocused(aura::Window* gained_focus,
+ aura::Window* lost_focus) {
+ Surface* target = nullptr;
+ if (gained_focus) {
+ target = Surface::AsSurface(gained_focus);
+ if (!target) {
+ aura::Window* top_level_window = gained_focus->GetToplevelWindow();
+ if (top_level_window)
+ target = ShellSurface::GetMainSurface(top_level_window);
+ }
+ }
+
+ bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target);
+ if (focused) {
+ gamepad_provider_->AddGamepadObserver(this);
+ OnGamepadDevicesUpdated();
+ } else {
+ gamepad_provider_->RemoveGamepadObserver(this);
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// GamingSeat, Override functions for GamepadObserver.
+void GamingSeat::OnGamepadDevicesUpdated() {
+ std::vector<ui::InputDevice> gamepad_devices =
+ gamepad_provider_->GetGamepadDevices();
+
+ base::flat_map<int, GamepadDelegate*> new_gamepads;
+
+ // Copy the "still connected gamepads".
+ for (auto& device : gamepad_devices) {
+ auto it = gamepads_.find(device.id);
+ if (it != gamepads_.end()) {
+ new_gamepads[device.id] = it->second;
+ gamepads_.erase(it);
+ }
+ }
+
+ // Remove each disconected gamepad.
+ for (auto& entry : gamepads_)
+ entry.second->OnRemoved();
+
+ // Connect new connected gamepads if there is a slot.
+ for (auto& device : gamepad_devices) {
+ if (new_gamepads.size() >= kMaxGamepadCount)
+ break;
+
+ if (new_gamepads.find(device.id) == new_gamepads.end())
+ new_gamepads[device.id] = delegate_->GamepadAdded();
+ }
+
+ new_gamepads.swap(gamepads_);
+}
+
+void GamingSeat::OnGamepadEvent(const ui::GamepadEvent& event) {
+ auto it = gamepads_.find(event.device_id());
+ if (it == gamepads_.end())
+ return;
+
+ switch (event.type()) {
+ case ui::GamepadEventType::BUTTON:
+ it->second->OnButton(event.code(), event.value(), event.value());
+ break;
+ case ui::GamepadEventType::AXIS:
+ it->second->OnAxis(event.code(), event.value());
+ break;
+ case ui::GamepadEventType::FRAME:
+ it->second->OnFrame();
+ break;
+ }
+}
+
+} // namespace exo

Powered by Google App Engine
This is Rietveld 408576698