OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #ifndef COMPONENTS_EXO_GAMEPAD_H_ | |
6 #define COMPONENTS_EXO_GAMEPAD_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/sequenced_task_runner.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "base/threading/thread.h" | |
15 #include "base/threading/thread_task_runner_handle.h" | |
16 #include "device/gamepad/gamepad_data_fetcher.h" | |
17 #include "ui/aura/client/focus_change_observer.h" | |
18 | |
19 namespace exo { | |
20 class GamepadDelegate; | |
21 | |
22 // This class represents one or more gamepads, it implements a background thread | |
23 // for polling gamepad devices and notifies the GamepadDelegate of any | |
24 // changes. | |
25 class Gamepad : public aura::client::FocusChangeObserver { | |
26 public: | |
27 // This class will post tasks to invoke the delegate on the thread runner | |
28 // which is associated with the thread that is creating this instance. | |
29 explicit Gamepad(GamepadDelegate* delegate); | |
30 // Allows test cases to specify a GamepadDataFetcher to provide mock gamepad | |
31 // data. | |
32 Gamepad(GamepadDelegate* delegate, device::GamepadDataFetcher* fetcher); | |
33 ~Gamepad() override; | |
34 | |
35 // Overridden aura::client::FocusChangeObserver: | |
36 void OnWindowFocused(aura::Window* gained_focus, | |
37 aura::Window* lost_focus) override; | |
38 | |
39 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.
| |
40 | |
41 private: | |
42 // 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.
| |
43 // Can only be called on the polling thread. | |
44 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.
| |
45 | |
46 // 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.
| |
47 // Can only be called on the polling thread. | |
48 void ResumePolling(); | |
reveman
2016/07/07 22:14:03
nit: add OnPollingThread suffix?
btw, would havin
denniskempin
2016/07/08 23:49:40
Done.
| |
49 | |
50 // Schedules a poll on the polling thread. Can only be called on the polling | |
51 // thread. | |
52 void ScheduleOnPoll(); | |
reveman
2016/07/07 22:14:02
nit: SchedulePollOnPollingThread
denniskempin
2016/07/08 23:49:40
Done.
| |
53 | |
54 // Polls devices for new data and posts delegate updates. | |
55 // Can only be called on the polling thread. | |
56 void OnPoll(); | |
reveman
2016/07/07 22:14:03
nit: PollOnPollingThread
denniskempin
2016/07/08 23:49:40
Done.
| |
57 | |
58 // Posts updates of gamepad data to delegate. Can only be called on the | |
59 // origin thread. | |
60 void PostGamepadChanges(const blink::WebGamepad new_pad); | |
61 | |
62 // The delegate instance that all events are dispatched to. | |
63 GamepadDelegate* const delegate_; | |
64 | |
65 // The current state of the gamepad represented by this instance. | |
66 blink::WebGamepad pad_state_; | |
67 | |
68 // Reference to the task runner of the thread that created this instance. | |
69 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; | |
70 | |
71 // 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.
| |
72 base::ThreadChecker origin_thread_checker_; | |
73 | |
74 // The thread on which the polling is executed. | |
75 std::unique_ptr<base::Thread> polling_thread_; | |
76 | |
77 // Externally owned gamepad data fetcher. | |
78 device::GamepadDataFetcher* external_fetcher_; | |
79 | |
80 // Internally owned gamepad data fetcher. | |
81 std::unique_ptr<device::GamepadDataFetcher> internal_fetcher_; | |
82 | |
83 // The current state of all gamepads. Only used by polling thread. | |
84 blink::WebGamepads state_; | |
85 | |
86 // True if a poll has been scheduled. Only used by polling thread. | |
87 bool has_poll_scheduled_{false}; | |
88 | |
89 // True if the polling thread is paused. Only used by polling thread. | |
90 bool is_paused_{true}; | |
91 | |
92 // ThreadChecker for the polling thread. | |
93 base::ThreadChecker polling_thread_checker_; | |
94 | |
95 // Callback to PostGamepadChanges using weak reference to this. | |
96 base::Callback<void(const blink::WebGamepad)> post_gamepad_changes_callback_; | |
97 | |
98 // Weak pointer factory to create the above weak pointer. | |
99 base::WeakPtrFactory<Gamepad> weak_factory_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(Gamepad); | |
102 }; | |
103 | |
104 } // namespace exo | |
105 | |
106 #endif // COMPONENTS_EXO_GAMEPAD_H_ | |
OLD | NEW |