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 <input/Input.h>
21 #include <utils/RefBase.h>
22 #include <utils/Vector.h>
23 
24 namespace android {
25 
26 class InputListenerInterface;
27 
28 
29 /* Superclass of all input event argument objects */
30 struct NotifyArgs {
~NotifyArgsNotifyArgs31     virtual ~NotifyArgs() { }
32 
33     virtual void notify(const sp<InputListenerInterface>& listener) const = 0;
34 };
35 
36 
37 /* Describes a configuration change event. */
38 struct NotifyConfigurationChangedArgs : public NotifyArgs {
39     nsecs_t eventTime;
40 
NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs41     inline NotifyConfigurationChangedArgs() { }
42 
43     explicit NotifyConfigurationChangedArgs(nsecs_t eventTime);
44 
45     NotifyConfigurationChangedArgs(const NotifyConfigurationChangedArgs& other);
46 
~NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs47     virtual ~NotifyConfigurationChangedArgs() { }
48 
49     virtual void notify(const sp<InputListenerInterface>& listener) const;
50 };
51 
52 
53 /* Describes a key event. */
54 struct NotifyKeyArgs : public NotifyArgs {
55     nsecs_t eventTime;
56     int32_t deviceId;
57     uint32_t source;
58     uint32_t policyFlags;
59     int32_t action;
60     int32_t flags;
61     int32_t keyCode;
62     int32_t scanCode;
63     int32_t metaState;
64     nsecs_t downTime;
65 
NotifyKeyArgsNotifyKeyArgs66     inline NotifyKeyArgs() { }
67 
68     NotifyKeyArgs(nsecs_t eventTime, int32_t deviceId, uint32_t source, uint32_t policyFlags,
69             int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
70             int32_t metaState, nsecs_t downTime);
71 
72     NotifyKeyArgs(const NotifyKeyArgs& other);
73 
~NotifyKeyArgsNotifyKeyArgs74     virtual ~NotifyKeyArgs() { }
75 
76     virtual void notify(const sp<InputListenerInterface>& listener) const;
77 };
78 
79 
80 /* Describes a motion event. */
81 struct NotifyMotionArgs : public NotifyArgs {
82     nsecs_t eventTime;
83     int32_t deviceId;
84     uint32_t source;
85     uint32_t policyFlags;
86     int32_t action;
87     int32_t actionButton;
88     int32_t flags;
89     int32_t metaState;
90     int32_t buttonState;
91     int32_t edgeFlags;
92     int32_t displayId;
93     /**
94      * A timestamp in the input device's time base, not the platform's.
95      * The units are microseconds since the last reset.
96      * This can only be compared to other device timestamps from the same device.
97      * This value will overflow after a little over an hour.
98      */
99     uint32_t deviceTimestamp;
100     uint32_t pointerCount;
101     PointerProperties pointerProperties[MAX_POINTERS];
102     PointerCoords pointerCoords[MAX_POINTERS];
103     float xPrecision;
104     float yPrecision;
105     nsecs_t downTime;
106 
NotifyMotionArgsNotifyMotionArgs107     inline NotifyMotionArgs() { }
108 
109     NotifyMotionArgs(nsecs_t eventTime, int32_t deviceId, uint32_t source, uint32_t policyFlags,
110             int32_t action, int32_t actionButton, int32_t flags,
111             int32_t metaState, int32_t buttonState,
112             int32_t edgeFlags, int32_t displayId, uint32_t deviceTimestamp, uint32_t pointerCount,
113             const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
114             float xPrecision, float yPrecision, nsecs_t downTime);
115 
116     NotifyMotionArgs(const NotifyMotionArgs& other);
117 
~NotifyMotionArgsNotifyMotionArgs118     virtual ~NotifyMotionArgs() { }
119 
120     virtual void notify(const sp<InputListenerInterface>& listener) const;
121 };
122 
123 
124 /* Describes a switch event. */
125 struct NotifySwitchArgs : public NotifyArgs {
126     nsecs_t eventTime;
127     uint32_t policyFlags;
128     uint32_t switchValues;
129     uint32_t switchMask;
130 
NotifySwitchArgsNotifySwitchArgs131     inline NotifySwitchArgs() { }
132 
133     NotifySwitchArgs(nsecs_t eventTime, uint32_t policyFlags,
134             uint32_t switchValues, uint32_t switchMask);
135 
136     NotifySwitchArgs(const NotifySwitchArgs& other);
137 
~NotifySwitchArgsNotifySwitchArgs138     virtual ~NotifySwitchArgs() { }
139 
140     virtual void notify(const sp<InputListenerInterface>& listener) const;
141 };
142 
143 
144 /* Describes a device reset event, such as when a device is added,
145  * reconfigured, or removed. */
146 struct NotifyDeviceResetArgs : public NotifyArgs {
147     nsecs_t eventTime;
148     int32_t deviceId;
149 
NotifyDeviceResetArgsNotifyDeviceResetArgs150     inline NotifyDeviceResetArgs() { }
151 
152     NotifyDeviceResetArgs(nsecs_t eventTime, int32_t deviceId);
153 
154     NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other);
155 
~NotifyDeviceResetArgsNotifyDeviceResetArgs156     virtual ~NotifyDeviceResetArgs() { }
157 
158     virtual void notify(const sp<InputListenerInterface>& listener) const;
159 };
160 
161 
162 /*
163  * The interface used by the InputReader to notify the InputListener about input events.
164  */
165 class InputListenerInterface : public virtual RefBase {
166 protected:
InputListenerInterface()167     InputListenerInterface() { }
~InputListenerInterface()168     virtual ~InputListenerInterface() { }
169 
170 public:
171     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) = 0;
172     virtual void notifyKey(const NotifyKeyArgs* args) = 0;
173     virtual void notifyMotion(const NotifyMotionArgs* args) = 0;
174     virtual void notifySwitch(const NotifySwitchArgs* args) = 0;
175     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) = 0;
176 };
177 
178 
179 /*
180  * An implementation of the listener interface that queues up and defers dispatch
181  * of decoded events until flushed.
182  */
183 class QueuedInputListener : public InputListenerInterface {
184 protected:
185     virtual ~QueuedInputListener();
186 
187 public:
188     explicit QueuedInputListener(const sp<InputListenerInterface>& innerListener);
189 
190     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args);
191     virtual void notifyKey(const NotifyKeyArgs* args);
192     virtual void notifyMotion(const NotifyMotionArgs* args);
193     virtual void notifySwitch(const NotifySwitchArgs* args);
194     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
195 
196     void flush();
197 
198 private:
199     sp<InputListenerInterface> mInnerListener;
200     Vector<NotifyArgs*> mArgsQueue;
201 };
202 
203 } // namespace android
204 
205 #endif // _UI_INPUT_LISTENER_H
206