Index: components/exo/gamepad.h |
diff --git a/components/exo/gamepad.h b/components/exo/gamepad.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1347af73685b7da6e77cdf10a478e27b07b6e33f |
--- /dev/null |
+++ b/components/exo/gamepad.h |
@@ -0,0 +1,106 @@ |
+// Copyright 2016 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. |
+ |
+#ifndef COMPONENTS_EXO_GAMEPAD_H_ |
+#define COMPONENTS_EXO_GAMEPAD_H_ |
+ |
+#include <memory> |
+ |
+#include "base/macros.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/sequenced_task_runner.h" |
+#include "base/synchronization/lock.h" |
+#include "base/threading/thread.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "device/gamepad/gamepad_data_fetcher.h" |
+#include "ui/aura/client/focus_change_observer.h" |
+ |
+namespace exo { |
+class GamepadDelegate; |
+ |
+// This class represents one or more gamepads, it implements a background thread |
+// for polling gamepad devices and notifies the GamepadDelegate of any |
+// changes. |
+class Gamepad : public aura::client::FocusChangeObserver { |
+ public: |
+ // This class will post tasks to invoke the delegate on the thread runner |
+ // which is associated with the thread that is creating this instance. |
+ explicit Gamepad(GamepadDelegate* delegate); |
+ // Allows test cases to specify a GamepadDataFetcher to provide mock gamepad |
+ // data. |
+ Gamepad(GamepadDelegate* delegate, device::GamepadDataFetcher* fetcher); |
+ ~Gamepad() override; |
+ |
+ // Overridden aura::client::FocusChangeObserver: |
+ void OnWindowFocused(aura::Window* gained_focus, |
+ aura::Window* lost_focus) override; |
+ |
+ bool is_paused() { return is_paused_; } |
reveman
2016/07/07 22:14:03
where is this used? please add const modifier if w
denniskempin
2016/07/08 23:49:40
Done.
|
+ |
+ private: |
+ // Pauses polling thread. Has no effect is the thread is already paused. |
reveman
2016/07/07 22:14:02
s/is/if/
denniskempin
2016/07/08 23:49:40
Done.
|
+ // Can only be called on the polling thread. |
+ void PausePolling(); |
reveman
2016/07/07 22:14:03
nit: Common style in chromium is to add a suffix "
denniskempin
2016/07/08 23:49:40
Done.
|
+ |
+ // Resume polling. Hs no effect if the thread is already active. |
reveman
2016/07/07 22:14:02
s/Hs/Has/
denniskempin
2016/07/08 23:49:40
Done.
|
+ // Can only be called on the polling thread. |
+ void ResumePolling(); |
reveman
2016/07/07 22:14:03
nit: add OnPollingThread suffix?
btw, would havin
denniskempin
2016/07/08 23:49:40
Done.
|
+ |
+ // Schedules a poll on the polling thread. Can only be called on the polling |
+ // thread. |
+ void ScheduleOnPoll(); |
reveman
2016/07/07 22:14:02
nit: SchedulePollOnPollingThread
denniskempin
2016/07/08 23:49:40
Done.
|
+ |
+ // Polls devices for new data and posts delegate updates. |
+ // Can only be called on the polling thread. |
+ void OnPoll(); |
reveman
2016/07/07 22:14:03
nit: PollOnPollingThread
denniskempin
2016/07/08 23:49:40
Done.
|
+ |
+ // Posts updates of gamepad data to delegate. Can only be called on the |
+ // origin thread. |
+ void PostGamepadChanges(const blink::WebGamepad new_pad); |
+ |
+ // The delegate instance that all events are dispatched to. |
+ GamepadDelegate* const delegate_; |
+ |
+ // The current state of the gamepad represented by this instance. |
+ blink::WebGamepad pad_state_; |
+ |
+ // Reference to the task runner of the thread that created this instance. |
+ scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
+ |
+ // ThreadChecker for the origin thread |
reveman
2016/07/07 22:14:03
nit: s/origin thread/origin thread./
denniskempin
2016/07/08 23:49:40
Done.
|
+ base::ThreadChecker origin_thread_checker_; |
+ |
+ // The thread on which the polling is executed. |
+ std::unique_ptr<base::Thread> polling_thread_; |
+ |
+ // Externally owned gamepad data fetcher. |
+ device::GamepadDataFetcher* external_fetcher_; |
+ |
+ // Internally owned gamepad data fetcher. |
+ std::unique_ptr<device::GamepadDataFetcher> internal_fetcher_; |
+ |
+ // The current state of all gamepads. Only used by polling thread. |
+ blink::WebGamepads state_; |
+ |
+ // True if a poll has been scheduled. Only used by polling thread. |
+ bool has_poll_scheduled_{false}; |
+ |
+ // True if the polling thread is paused. Only used by polling thread. |
+ bool is_paused_{true}; |
+ |
+ // ThreadChecker for the polling thread. |
+ base::ThreadChecker polling_thread_checker_; |
+ |
+ // Callback to PostGamepadChanges using weak reference to this. |
+ base::Callback<void(const blink::WebGamepad)> post_gamepad_changes_callback_; |
+ |
+ // Weak pointer factory to create the above weak pointer. |
+ base::WeakPtrFactory<Gamepad> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(Gamepad); |
+}; |
+ |
+} // namespace exo |
+ |
+#endif // COMPONENTS_EXO_GAMEPAD_H_ |