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/TouchVideoFrame.h>
24 #include <utils/RefBase.h>
25 #include <utils/Vector.h>
26 
27 namespace android {
28 
29 class InputListenerInterface;
30 
31 
32 /* Superclass of all input event argument objects */
33 struct NotifyArgs {
34     uint32_t sequenceNum;
35     nsecs_t eventTime;
36 
NotifyArgsNotifyArgs37     inline NotifyArgs() : sequenceNum(0), eventTime(0) { }
38 
NotifyArgsNotifyArgs39     inline explicit NotifyArgs(uint32_t sequenceNum, nsecs_t eventTime) :
40             sequenceNum(sequenceNum), eventTime(eventTime) { }
41 
~NotifyArgsNotifyArgs42     virtual ~NotifyArgs() { }
43 
44     virtual void notify(const sp<InputListenerInterface>& listener) const = 0;
45 };
46 
47 
48 /* Describes a configuration change event. */
49 struct NotifyConfigurationChangedArgs : public NotifyArgs {
50 
NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs51     inline NotifyConfigurationChangedArgs() { }
52 
53     bool operator==(const NotifyConfigurationChangedArgs& rhs) const;
54 
55     NotifyConfigurationChangedArgs(uint32_t sequenceNum, nsecs_t eventTime);
56 
57     NotifyConfigurationChangedArgs(const NotifyConfigurationChangedArgs& other);
58 
~NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs59     virtual ~NotifyConfigurationChangedArgs() { }
60 
61     virtual void notify(const sp<InputListenerInterface>& listener) const;
62 };
63 
64 
65 /* Describes a key event. */
66 struct NotifyKeyArgs : public NotifyArgs {
67     int32_t deviceId;
68     uint32_t source;
69     int32_t displayId;
70     uint32_t policyFlags;
71     int32_t action;
72     int32_t flags;
73     int32_t keyCode;
74     int32_t scanCode;
75     int32_t metaState;
76     nsecs_t downTime;
77 
NotifyKeyArgsNotifyKeyArgs78     inline NotifyKeyArgs() { }
79 
80     NotifyKeyArgs(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
81             int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
82             int32_t scanCode, int32_t metaState, nsecs_t downTime);
83 
84     bool operator==(const NotifyKeyArgs& rhs) const;
85 
86     NotifyKeyArgs(const NotifyKeyArgs& other);
87 
~NotifyKeyArgsNotifyKeyArgs88     virtual ~NotifyKeyArgs() { }
89 
90     virtual void notify(const sp<InputListenerInterface>& listener) const;
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      * A timestamp in the input device's time base, not the platform's.
112      * The units are microseconds since the last reset.
113      * This can only be compared to other device timestamps from the same device.
114      * This value will overflow after a little over an hour.
115      */
116     uint32_t deviceTimestamp;
117     uint32_t pointerCount;
118     PointerProperties pointerProperties[MAX_POINTERS];
119     PointerCoords pointerCoords[MAX_POINTERS];
120     float xPrecision;
121     float yPrecision;
122     nsecs_t downTime;
123     std::vector<TouchVideoFrame> videoFrames;
124 
NotifyMotionArgsNotifyMotionArgs125     inline NotifyMotionArgs() { }
126 
127     NotifyMotionArgs(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
128             int32_t displayId, uint32_t policyFlags,
129             int32_t action, int32_t actionButton, int32_t flags,
130             int32_t metaState, int32_t buttonState, MotionClassification classification,
131             int32_t edgeFlags, uint32_t deviceTimestamp, uint32_t pointerCount,
132             const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
133             float xPrecision, float yPrecision, nsecs_t downTime,
134             const std::vector<TouchVideoFrame>& videoFrames);
135 
136     NotifyMotionArgs(const NotifyMotionArgs& other);
137 
~NotifyMotionArgsNotifyMotionArgs138     virtual ~NotifyMotionArgs() { }
139 
140     bool operator==(const NotifyMotionArgs& rhs) const;
141 
142     virtual void notify(const sp<InputListenerInterface>& listener) const;
143 };
144 
145 
146 /* Describes a switch event. */
147 struct NotifySwitchArgs : public NotifyArgs {
148     uint32_t policyFlags;
149     uint32_t switchValues;
150     uint32_t switchMask;
151 
NotifySwitchArgsNotifySwitchArgs152     inline NotifySwitchArgs() { }
153 
154     NotifySwitchArgs(uint32_t sequenceNum, nsecs_t eventTime, uint32_t policyFlags,
155             uint32_t switchValues, uint32_t switchMask);
156 
157     NotifySwitchArgs(const NotifySwitchArgs& other);
158 
159     bool operator==(const NotifySwitchArgs rhs) const;
160 
~NotifySwitchArgsNotifySwitchArgs161     virtual ~NotifySwitchArgs() { }
162 
163     virtual void notify(const sp<InputListenerInterface>& listener) const;
164 };
165 
166 
167 /* Describes a device reset event, such as when a device is added,
168  * reconfigured, or removed. */
169 struct NotifyDeviceResetArgs : public NotifyArgs {
170     int32_t deviceId;
171 
NotifyDeviceResetArgsNotifyDeviceResetArgs172     inline NotifyDeviceResetArgs() { }
173 
174     NotifyDeviceResetArgs(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
175 
176     NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other);
177 
178     bool operator==(const NotifyDeviceResetArgs& rhs) const;
179 
~NotifyDeviceResetArgsNotifyDeviceResetArgs180     virtual ~NotifyDeviceResetArgs() { }
181 
182     virtual void notify(const sp<InputListenerInterface>& listener) const;
183 };
184 
185 
186 /*
187  * The interface used by the InputReader to notify the InputListener about input events.
188  */
189 class InputListenerInterface : public virtual RefBase {
190 protected:
InputListenerInterface()191     InputListenerInterface() { }
~InputListenerInterface()192     virtual ~InputListenerInterface() { }
193 
194 public:
195     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) = 0;
196     virtual void notifyKey(const NotifyKeyArgs* args) = 0;
197     virtual void notifyMotion(const NotifyMotionArgs* args) = 0;
198     virtual void notifySwitch(const NotifySwitchArgs* args) = 0;
199     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) = 0;
200 };
201 
202 
203 /*
204  * An implementation of the listener interface that queues up and defers dispatch
205  * of decoded events until flushed.
206  */
207 class QueuedInputListener : public InputListenerInterface {
208 protected:
209     virtual ~QueuedInputListener();
210 
211 public:
212     explicit QueuedInputListener(const sp<InputListenerInterface>& innerListener);
213 
214     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args);
215     virtual void notifyKey(const NotifyKeyArgs* args);
216     virtual void notifyMotion(const NotifyMotionArgs* args);
217     virtual void notifySwitch(const NotifySwitchArgs* args);
218     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
219 
220     void flush();
221 
222 private:
223     sp<InputListenerInterface> mInnerListener;
224     std::vector<NotifyArgs*> mArgsQueue;
225 };
226 
227 } // namespace android
228 
229 #endif // _UI_INPUT_LISTENER_H
230