• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _UI_INPUT_LISTENER_H
18 #define _UI_INPUT_LISTENER_H
19 
20 #include <vector>
21 
22 #include <input/Input.h>
23 #include <input/InputDevice.h>
24 #include <input/TouchVideoFrame.h>
25 
26 namespace android {
27 
28 class InputListenerInterface;
29 
30 
31 /* Superclass of all input event argument objects */
32 struct NotifyArgs {
33     int32_t id;
34     nsecs_t eventTime;
35 
NotifyArgsNotifyArgs36     inline NotifyArgs() : id(0), eventTime(0) {}
37 
NotifyArgsNotifyArgs38     inline explicit NotifyArgs(int32_t id, nsecs_t eventTime) : id(id), eventTime(eventTime) {}
39 
~NotifyArgsNotifyArgs40     virtual ~NotifyArgs() { }
41 
42     virtual void notify(InputListenerInterface& listener) const = 0;
43 };
44 
45 
46 /* Describes a configuration change event. */
47 struct NotifyConfigurationChangedArgs : public NotifyArgs {
48 
NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs49     inline NotifyConfigurationChangedArgs() { }
50 
51     bool operator==(const NotifyConfigurationChangedArgs& rhs) const;
52 
53     NotifyConfigurationChangedArgs(int32_t id, nsecs_t eventTime);
54 
55     NotifyConfigurationChangedArgs(const NotifyConfigurationChangedArgs& other);
56 
~NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs57     virtual ~NotifyConfigurationChangedArgs() { }
58 
59     void notify(InputListenerInterface& listener) const override;
60 };
61 
62 
63 /* Describes a key event. */
64 struct NotifyKeyArgs : public NotifyArgs {
65     int32_t deviceId;
66     uint32_t source;
67     int32_t displayId;
68     uint32_t policyFlags;
69     int32_t action;
70     int32_t flags;
71     int32_t keyCode;
72     int32_t scanCode;
73     int32_t metaState;
74     nsecs_t downTime;
75     nsecs_t readTime;
76 
NotifyKeyArgsNotifyKeyArgs77     inline NotifyKeyArgs() { }
78 
79     NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId,
80                   uint32_t source, int32_t displayId, uint32_t policyFlags, int32_t action,
81                   int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
82                   nsecs_t downTime);
83 
84     bool operator==(const NotifyKeyArgs& rhs) const;
85 
86     NotifyKeyArgs(const NotifyKeyArgs& other);
87 
~NotifyKeyArgsNotifyKeyArgs88     virtual ~NotifyKeyArgs() { }
89 
90     void notify(InputListenerInterface& listener) const override;
91 };
92 
93 
94 /* Describes a motion event. */
95 struct NotifyMotionArgs : public NotifyArgs {
96     int32_t deviceId;
97     uint32_t source;
98     int32_t displayId;
99     uint32_t policyFlags;
100     int32_t action;
101     int32_t actionButton;
102     int32_t flags;
103     int32_t metaState;
104     int32_t buttonState;
105     /**
106      * Classification of the current touch gesture
107      */
108     MotionClassification classification;
109     int32_t edgeFlags;
110 
111     uint32_t pointerCount;
112     PointerProperties pointerProperties[MAX_POINTERS];
113     PointerCoords pointerCoords[MAX_POINTERS];
114     float xPrecision;
115     float yPrecision;
116     /**
117      * Mouse cursor position when this event is reported relative to the origin of the specified
118      * display. Only valid if this is a mouse event (originates from a mouse or from a trackpad in
119      * gestures enabled mode.
120      */
121     float xCursorPosition;
122     float yCursorPosition;
123     nsecs_t downTime;
124     nsecs_t readTime;
125     std::vector<TouchVideoFrame> videoFrames;
126 
NotifyMotionArgsNotifyMotionArgs127     inline NotifyMotionArgs() { }
128 
129     NotifyMotionArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId,
130                      uint32_t source, int32_t displayId, uint32_t policyFlags, int32_t action,
131                      int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState,
132                      MotionClassification classification, int32_t edgeFlags, uint32_t pointerCount,
133                      const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
134                      float xPrecision, float yPrecision, float xCursorPosition,
135                      float yCursorPosition, nsecs_t downTime,
136                      const std::vector<TouchVideoFrame>& videoFrames);
137 
138     NotifyMotionArgs(const NotifyMotionArgs& other);
139 
~NotifyMotionArgsNotifyMotionArgs140     virtual ~NotifyMotionArgs() { }
141 
142     bool operator==(const NotifyMotionArgs& rhs) const;
143 
144     void notify(InputListenerInterface& listener) const override;
145 
146     std::string dump() const;
147 };
148 
149 /* Describes a sensor event. */
150 struct NotifySensorArgs : public NotifyArgs {
151     int32_t deviceId;
152     uint32_t source;
153     InputDeviceSensorType sensorType;
154     InputDeviceSensorAccuracy accuracy;
155     bool accuracyChanged;
156     nsecs_t hwTimestamp;
157     std::vector<float> values;
158 
NotifySensorArgsNotifySensorArgs159     inline NotifySensorArgs() {}
160 
161     NotifySensorArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
162                      InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
163                      bool accuracyChanged, nsecs_t hwTimestamp, std::vector<float> values);
164 
165     NotifySensorArgs(const NotifySensorArgs& other);
166 
167     bool operator==(const NotifySensorArgs rhs) const;
168 
~NotifySensorArgsNotifySensorArgs169     ~NotifySensorArgs() override {}
170 
171     void notify(InputListenerInterface& listener) const override;
172 };
173 
174 /* Describes a switch event. */
175 struct NotifySwitchArgs : public NotifyArgs {
176     uint32_t policyFlags;
177     uint32_t switchValues;
178     uint32_t switchMask;
179 
NotifySwitchArgsNotifySwitchArgs180     inline NotifySwitchArgs() { }
181 
182     NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags, uint32_t switchValues,
183                      uint32_t switchMask);
184 
185     NotifySwitchArgs(const NotifySwitchArgs& other);
186 
187     bool operator==(const NotifySwitchArgs rhs) const;
188 
~NotifySwitchArgsNotifySwitchArgs189     virtual ~NotifySwitchArgs() { }
190 
191     void notify(InputListenerInterface& listener) const override;
192 };
193 
194 
195 /* Describes a device reset event, such as when a device is added,
196  * reconfigured, or removed. */
197 struct NotifyDeviceResetArgs : public NotifyArgs {
198     int32_t deviceId;
199 
NotifyDeviceResetArgsNotifyDeviceResetArgs200     inline NotifyDeviceResetArgs() { }
201 
202     NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId);
203 
204     NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other);
205 
206     bool operator==(const NotifyDeviceResetArgs& rhs) const;
207 
~NotifyDeviceResetArgsNotifyDeviceResetArgs208     virtual ~NotifyDeviceResetArgs() { }
209 
210     void notify(InputListenerInterface& listener) const override;
211 };
212 
213 /* Describes a change in the state of Pointer Capture. */
214 struct NotifyPointerCaptureChangedArgs : public NotifyArgs {
215     // The sequence number of the Pointer Capture request, if enabled.
216     PointerCaptureRequest request;
217 
NotifyPointerCaptureChangedArgsNotifyPointerCaptureChangedArgs218     inline NotifyPointerCaptureChangedArgs() {}
219 
220     NotifyPointerCaptureChangedArgs(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
221 
222     NotifyPointerCaptureChangedArgs(const NotifyPointerCaptureChangedArgs& other);
223 
224     bool operator==(const NotifyPointerCaptureChangedArgs& rhs) const;
225 
~NotifyPointerCaptureChangedArgsNotifyPointerCaptureChangedArgs226     virtual ~NotifyPointerCaptureChangedArgs() {}
227 
228     void notify(InputListenerInterface& listener) const override;
229 };
230 
231 /* Describes a vibrator state event. */
232 struct NotifyVibratorStateArgs : public NotifyArgs {
233     int32_t deviceId;
234     bool isOn;
235 
NotifyVibratorStateArgsNotifyVibratorStateArgs236     inline NotifyVibratorStateArgs() {}
237 
238     NotifyVibratorStateArgs(int32_t id, nsecs_t eventTIme, int32_t deviceId, bool isOn);
239 
240     NotifyVibratorStateArgs(const NotifyVibratorStateArgs& other);
241 
242     bool operator==(const NotifyVibratorStateArgs rhs) const;
243 
~NotifyVibratorStateArgsNotifyVibratorStateArgs244     virtual ~NotifyVibratorStateArgs() {}
245 
246     void notify(InputListenerInterface& listener) const override;
247 };
248 
249 /*
250  * The interface used by the InputReader to notify the InputListener about input events.
251  */
252 class InputListenerInterface {
253 public:
InputListenerInterface()254     InputListenerInterface() { }
255     InputListenerInterface(const InputListenerInterface&) = delete;
256     InputListenerInterface& operator=(const InputListenerInterface&) = delete;
~InputListenerInterface()257     virtual ~InputListenerInterface() { }
258 
259     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) = 0;
260     virtual void notifyKey(const NotifyKeyArgs* args) = 0;
261     virtual void notifyMotion(const NotifyMotionArgs* args) = 0;
262     virtual void notifySwitch(const NotifySwitchArgs* args) = 0;
263     virtual void notifySensor(const NotifySensorArgs* args) = 0;
264     virtual void notifyVibratorState(const NotifyVibratorStateArgs* args) = 0;
265     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) = 0;
266     virtual void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) = 0;
267 };
268 
269 /*
270  * An implementation of the listener interface that queues up and defers dispatch
271  * of decoded events until flushed.
272  */
273 class QueuedInputListener : public InputListenerInterface {
274 
275 public:
276     explicit QueuedInputListener(InputListenerInterface& innerListener);
277 
278     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
279     virtual void notifyKey(const NotifyKeyArgs* args) override;
280     virtual void notifyMotion(const NotifyMotionArgs* args) override;
281     virtual void notifySwitch(const NotifySwitchArgs* args) override;
282     virtual void notifySensor(const NotifySensorArgs* args) override;
283     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
284     void notifyVibratorState(const NotifyVibratorStateArgs* args) override;
285     void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override;
286 
287     void flush();
288 
289 private:
290     InputListenerInterface& mInnerListener;
291     std::vector<std::unique_ptr<NotifyArgs>> mArgsQueue;
292 };
293 
294 } // namespace android
295 
296 #endif // _UI_INPUT_LISTENER_H
297