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 #define LOG_TAG "InputListener"
18 
19 //#define LOG_NDEBUG 0
20 
21 #include "InputListener.h"
22 
23 #include <android/log.h>
24 
25 namespace android {
26 
27 // --- NotifyConfigurationChangedArgs ---
28 
NotifyConfigurationChangedArgs(nsecs_t eventTime)29 NotifyConfigurationChangedArgs::NotifyConfigurationChangedArgs(nsecs_t eventTime) :
30         eventTime(eventTime) {
31 }
32 
NotifyConfigurationChangedArgs(const NotifyConfigurationChangedArgs & other)33 NotifyConfigurationChangedArgs::NotifyConfigurationChangedArgs(
34         const NotifyConfigurationChangedArgs& other) :
35         eventTime(other.eventTime) {
36 }
37 
notify(const sp<InputListenerInterface> & listener) const38 void NotifyConfigurationChangedArgs::notify(const sp<InputListenerInterface>& listener) const {
39     listener->notifyConfigurationChanged(this);
40 }
41 
42 
43 // --- NotifyKeyArgs ---
44 
NotifyKeyArgs(nsecs_t eventTime,int32_t deviceId,uint32_t source,uint32_t policyFlags,int32_t action,int32_t flags,int32_t keyCode,int32_t scanCode,int32_t metaState,nsecs_t downTime)45 NotifyKeyArgs::NotifyKeyArgs(nsecs_t eventTime, int32_t deviceId, uint32_t source,
46         uint32_t policyFlags,
47         int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
48         int32_t metaState, nsecs_t downTime) :
49         eventTime(eventTime), deviceId(deviceId), source(source), policyFlags(policyFlags),
50         action(action), flags(flags), keyCode(keyCode), scanCode(scanCode),
51         metaState(metaState), downTime(downTime) {
52 }
53 
NotifyKeyArgs(const NotifyKeyArgs & other)54 NotifyKeyArgs::NotifyKeyArgs(const NotifyKeyArgs& other) :
55         eventTime(other.eventTime), deviceId(other.deviceId), source(other.source),
56         policyFlags(other.policyFlags),
57         action(other.action), flags(other.flags),
58         keyCode(other.keyCode), scanCode(other.scanCode),
59         metaState(other.metaState), downTime(other.downTime) {
60 }
61 
notify(const sp<InputListenerInterface> & listener) const62 void NotifyKeyArgs::notify(const sp<InputListenerInterface>& listener) const {
63     listener->notifyKey(this);
64 }
65 
66 
67 // --- NotifyMotionArgs ---
68 
NotifyMotionArgs(nsecs_t eventTime,int32_t deviceId,uint32_t source,uint32_t policyFlags,int32_t action,int32_t actionButton,int32_t flags,int32_t metaState,int32_t buttonState,int32_t edgeFlags,int32_t displayId,uint32_t deviceTimestamp,uint32_t pointerCount,const PointerProperties * pointerProperties,const PointerCoords * pointerCoords,float xPrecision,float yPrecision,nsecs_t downTime)69 NotifyMotionArgs::NotifyMotionArgs(nsecs_t eventTime, int32_t deviceId, uint32_t source,
70         uint32_t policyFlags,
71         int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
72         int32_t buttonState, int32_t edgeFlags, int32_t displayId, uint32_t deviceTimestamp,
73         uint32_t pointerCount,
74         const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
75         float xPrecision, float yPrecision, nsecs_t downTime) :
76         eventTime(eventTime), deviceId(deviceId), source(source), policyFlags(policyFlags),
77         action(action), actionButton(actionButton),
78         flags(flags), metaState(metaState), buttonState(buttonState),
79         edgeFlags(edgeFlags), displayId(displayId), deviceTimestamp(deviceTimestamp),
80         pointerCount(pointerCount),
81         xPrecision(xPrecision), yPrecision(yPrecision), downTime(downTime) {
82     for (uint32_t i = 0; i < pointerCount; i++) {
83         this->pointerProperties[i].copyFrom(pointerProperties[i]);
84         this->pointerCoords[i].copyFrom(pointerCoords[i]);
85     }
86 }
87 
NotifyMotionArgs(const NotifyMotionArgs & other)88 NotifyMotionArgs::NotifyMotionArgs(const NotifyMotionArgs& other) :
89         eventTime(other.eventTime), deviceId(other.deviceId), source(other.source),
90         policyFlags(other.policyFlags),
91         action(other.action), actionButton(other.actionButton), flags(other.flags),
92         metaState(other.metaState), buttonState(other.buttonState),
93         edgeFlags(other.edgeFlags), displayId(other.displayId),
94         deviceTimestamp(other.deviceTimestamp), pointerCount(other.pointerCount),
95         xPrecision(other.xPrecision), yPrecision(other.yPrecision), downTime(other.downTime) {
96     for (uint32_t i = 0; i < pointerCount; i++) {
97         pointerProperties[i].copyFrom(other.pointerProperties[i]);
98         pointerCoords[i].copyFrom(other.pointerCoords[i]);
99     }
100 }
101 
notify(const sp<InputListenerInterface> & listener) const102 void NotifyMotionArgs::notify(const sp<InputListenerInterface>& listener) const {
103     listener->notifyMotion(this);
104 }
105 
106 
107 // --- NotifySwitchArgs ---
108 
NotifySwitchArgs(nsecs_t eventTime,uint32_t policyFlags,uint32_t switchValues,uint32_t switchMask)109 NotifySwitchArgs::NotifySwitchArgs(nsecs_t eventTime, uint32_t policyFlags,
110         uint32_t switchValues, uint32_t switchMask) :
111         eventTime(eventTime), policyFlags(policyFlags),
112         switchValues(switchValues), switchMask(switchMask) {
113 }
114 
NotifySwitchArgs(const NotifySwitchArgs & other)115 NotifySwitchArgs::NotifySwitchArgs(const NotifySwitchArgs& other) :
116         eventTime(other.eventTime), policyFlags(other.policyFlags),
117         switchValues(other.switchValues), switchMask(other.switchMask) {
118 }
119 
notify(const sp<InputListenerInterface> & listener) const120 void NotifySwitchArgs::notify(const sp<InputListenerInterface>& listener) const {
121     listener->notifySwitch(this);
122 }
123 
124 
125 // --- NotifyDeviceResetArgs ---
126 
NotifyDeviceResetArgs(nsecs_t eventTime,int32_t deviceId)127 NotifyDeviceResetArgs::NotifyDeviceResetArgs(nsecs_t eventTime, int32_t deviceId) :
128         eventTime(eventTime), deviceId(deviceId) {
129 }
130 
NotifyDeviceResetArgs(const NotifyDeviceResetArgs & other)131 NotifyDeviceResetArgs::NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other) :
132         eventTime(other.eventTime), deviceId(other.deviceId) {
133 }
134 
notify(const sp<InputListenerInterface> & listener) const135 void NotifyDeviceResetArgs::notify(const sp<InputListenerInterface>& listener) const {
136     listener->notifyDeviceReset(this);
137 }
138 
139 
140 // --- QueuedInputListener ---
141 
QueuedInputListener(const sp<InputListenerInterface> & innerListener)142 QueuedInputListener::QueuedInputListener(const sp<InputListenerInterface>& innerListener) :
143         mInnerListener(innerListener) {
144 }
145 
~QueuedInputListener()146 QueuedInputListener::~QueuedInputListener() {
147     size_t count = mArgsQueue.size();
148     for (size_t i = 0; i < count; i++) {
149         delete mArgsQueue[i];
150     }
151 }
152 
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)153 void QueuedInputListener::notifyConfigurationChanged(
154         const NotifyConfigurationChangedArgs* args) {
155     mArgsQueue.push(new NotifyConfigurationChangedArgs(*args));
156 }
157 
notifyKey(const NotifyKeyArgs * args)158 void QueuedInputListener::notifyKey(const NotifyKeyArgs* args) {
159     mArgsQueue.push(new NotifyKeyArgs(*args));
160 }
161 
notifyMotion(const NotifyMotionArgs * args)162 void QueuedInputListener::notifyMotion(const NotifyMotionArgs* args) {
163     mArgsQueue.push(new NotifyMotionArgs(*args));
164 }
165 
notifySwitch(const NotifySwitchArgs * args)166 void QueuedInputListener::notifySwitch(const NotifySwitchArgs* args) {
167     mArgsQueue.push(new NotifySwitchArgs(*args));
168 }
169 
notifyDeviceReset(const NotifyDeviceResetArgs * args)170 void QueuedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
171     mArgsQueue.push(new NotifyDeviceResetArgs(*args));
172 }
173 
flush()174 void QueuedInputListener::flush() {
175     size_t count = mArgsQueue.size();
176     for (size_t i = 0; i < count; i++) {
177         NotifyArgs* args = mArgsQueue[i];
178         args->notify(mInnerListener);
179         delete args;
180     }
181     mArgsQueue.clear();
182 }
183 
184 
185 } // namespace android
186