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 #include <utils/RefBase.h> 26 27 namespace android { 28 29 class InputListenerInterface; 30 31 32 /* Superclass of all input event argument objects */ 33 struct NotifyArgs { 34 int32_t id; 35 nsecs_t eventTime; 36 NotifyArgsNotifyArgs37 inline NotifyArgs() : id(0), eventTime(0) {} 38 NotifyArgsNotifyArgs39 inline explicit NotifyArgs(int32_t id, nsecs_t eventTime) : id(id), eventTime(eventTime) {} 40 ~NotifyArgsNotifyArgs41 virtual ~NotifyArgs() { } 42 43 virtual void notify(const sp<InputListenerInterface>& listener) const = 0; 44 }; 45 46 47 /* Describes a configuration change event. */ 48 struct NotifyConfigurationChangedArgs : public NotifyArgs { 49 NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs50 inline NotifyConfigurationChangedArgs() { } 51 52 bool operator==(const NotifyConfigurationChangedArgs& rhs) const; 53 54 NotifyConfigurationChangedArgs(int32_t id, nsecs_t eventTime); 55 56 NotifyConfigurationChangedArgs(const NotifyConfigurationChangedArgs& other); 57 ~NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs58 virtual ~NotifyConfigurationChangedArgs() { } 59 60 virtual void notify(const sp<InputListenerInterface>& listener) const; 61 }; 62 63 64 /* Describes a key event. */ 65 struct NotifyKeyArgs : public NotifyArgs { 66 int32_t deviceId; 67 uint32_t source; 68 int32_t displayId; 69 uint32_t policyFlags; 70 int32_t action; 71 int32_t flags; 72 int32_t keyCode; 73 int32_t scanCode; 74 int32_t metaState; 75 nsecs_t downTime; 76 nsecs_t readTime; 77 NotifyKeyArgsNotifyKeyArgs78 inline NotifyKeyArgs() { } 79 80 NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, 81 uint32_t source, int32_t displayId, uint32_t policyFlags, int32_t action, 82 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, 83 nsecs_t downTime); 84 85 bool operator==(const NotifyKeyArgs& rhs) const; 86 87 NotifyKeyArgs(const NotifyKeyArgs& other); 88 ~NotifyKeyArgsNotifyKeyArgs89 virtual ~NotifyKeyArgs() { } 90 91 virtual void notify(const sp<InputListenerInterface>& listener) const; 92 }; 93 94 95 /* Describes a motion event. */ 96 struct NotifyMotionArgs : public NotifyArgs { 97 int32_t deviceId; 98 uint32_t source; 99 int32_t displayId; 100 uint32_t policyFlags; 101 int32_t action; 102 int32_t actionButton; 103 int32_t flags; 104 int32_t metaState; 105 int32_t buttonState; 106 /** 107 * Classification of the current touch gesture 108 */ 109 MotionClassification classification; 110 int32_t edgeFlags; 111 112 uint32_t pointerCount; 113 PointerProperties pointerProperties[MAX_POINTERS]; 114 PointerCoords pointerCoords[MAX_POINTERS]; 115 float xPrecision; 116 float yPrecision; 117 /** 118 * Mouse cursor position when this event is reported relative to the origin of the specified 119 * display. Only valid if this is a mouse event (originates from a mouse or from a trackpad in 120 * gestures enabled mode. 121 */ 122 float xCursorPosition; 123 float yCursorPosition; 124 nsecs_t downTime; 125 nsecs_t readTime; 126 std::vector<TouchVideoFrame> videoFrames; 127 NotifyMotionArgsNotifyMotionArgs128 inline NotifyMotionArgs() { } 129 130 NotifyMotionArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId, 131 uint32_t source, int32_t displayId, uint32_t policyFlags, int32_t action, 132 int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState, 133 MotionClassification classification, int32_t edgeFlags, uint32_t pointerCount, 134 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords, 135 float xPrecision, float yPrecision, float xCursorPosition, 136 float yCursorPosition, nsecs_t downTime, 137 const std::vector<TouchVideoFrame>& videoFrames); 138 139 NotifyMotionArgs(const NotifyMotionArgs& other); 140 ~NotifyMotionArgsNotifyMotionArgs141 virtual ~NotifyMotionArgs() { } 142 143 bool operator==(const NotifyMotionArgs& rhs) const; 144 145 virtual void notify(const sp<InputListenerInterface>& listener) const; 146 }; 147 148 /* Describes a sensor event. */ 149 struct NotifySensorArgs : public NotifyArgs { 150 int32_t deviceId; 151 uint32_t source; 152 InputDeviceSensorType sensorType; 153 InputDeviceSensorAccuracy accuracy; 154 bool accuracyChanged; 155 nsecs_t hwTimestamp; 156 std::vector<float> values; 157 NotifySensorArgsNotifySensorArgs158 inline NotifySensorArgs() {} 159 160 NotifySensorArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, 161 InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy, 162 bool accuracyChanged, nsecs_t hwTimestamp, std::vector<float> values); 163 164 NotifySensorArgs(const NotifySensorArgs& other); 165 166 bool operator==(const NotifySensorArgs rhs) const; 167 ~NotifySensorArgsNotifySensorArgs168 ~NotifySensorArgs() override {} 169 170 void notify(const sp<InputListenerInterface>& listener) const override; 171 }; 172 173 /* Describes a switch event. */ 174 struct NotifySwitchArgs : public NotifyArgs { 175 uint32_t policyFlags; 176 uint32_t switchValues; 177 uint32_t switchMask; 178 NotifySwitchArgsNotifySwitchArgs179 inline NotifySwitchArgs() { } 180 181 NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags, uint32_t switchValues, 182 uint32_t switchMask); 183 184 NotifySwitchArgs(const NotifySwitchArgs& other); 185 186 bool operator==(const NotifySwitchArgs rhs) const; 187 ~NotifySwitchArgsNotifySwitchArgs188 virtual ~NotifySwitchArgs() { } 189 190 virtual void notify(const sp<InputListenerInterface>& listener) const; 191 }; 192 193 194 /* Describes a device reset event, such as when a device is added, 195 * reconfigured, or removed. */ 196 struct NotifyDeviceResetArgs : public NotifyArgs { 197 int32_t deviceId; 198 NotifyDeviceResetArgsNotifyDeviceResetArgs199 inline NotifyDeviceResetArgs() { } 200 201 NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId); 202 203 NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other); 204 205 bool operator==(const NotifyDeviceResetArgs& rhs) const; 206 ~NotifyDeviceResetArgsNotifyDeviceResetArgs207 virtual ~NotifyDeviceResetArgs() { } 208 209 virtual void notify(const sp<InputListenerInterface>& listener) const; 210 }; 211 212 /* Describes a change in the state of Pointer Capture. */ 213 struct NotifyPointerCaptureChangedArgs : public NotifyArgs { 214 bool enabled; 215 NotifyPointerCaptureChangedArgsNotifyPointerCaptureChangedArgs216 inline NotifyPointerCaptureChangedArgs() {} 217 218 NotifyPointerCaptureChangedArgs(int32_t id, nsecs_t eventTime, bool enabled); 219 220 NotifyPointerCaptureChangedArgs(const NotifyPointerCaptureChangedArgs& other); 221 222 bool operator==(const NotifyPointerCaptureChangedArgs& rhs) const; 223 ~NotifyPointerCaptureChangedArgsNotifyPointerCaptureChangedArgs224 virtual ~NotifyPointerCaptureChangedArgs() {} 225 226 virtual void notify(const sp<InputListenerInterface>& listener) const; 227 }; 228 229 /* Describes a vibrator state event. */ 230 struct NotifyVibratorStateArgs : public NotifyArgs { 231 int32_t deviceId; 232 bool isOn; 233 NotifyVibratorStateArgsNotifyVibratorStateArgs234 inline NotifyVibratorStateArgs() {} 235 236 NotifyVibratorStateArgs(int32_t id, nsecs_t eventTIme, int32_t deviceId, bool isOn); 237 238 NotifyVibratorStateArgs(const NotifyVibratorStateArgs& other); 239 240 bool operator==(const NotifyVibratorStateArgs rhs) const; 241 ~NotifyVibratorStateArgsNotifyVibratorStateArgs242 virtual ~NotifyVibratorStateArgs() {} 243 244 virtual void notify(const sp<InputListenerInterface>& listener) const; 245 }; 246 247 /* 248 * The interface used by the InputReader to notify the InputListener about input events. 249 */ 250 class InputListenerInterface : public virtual RefBase { 251 protected: InputListenerInterface()252 InputListenerInterface() { } ~InputListenerInterface()253 virtual ~InputListenerInterface() { } 254 255 public: 256 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) = 0; 257 virtual void notifyKey(const NotifyKeyArgs* args) = 0; 258 virtual void notifyMotion(const NotifyMotionArgs* args) = 0; 259 virtual void notifySwitch(const NotifySwitchArgs* args) = 0; 260 virtual void notifySensor(const NotifySensorArgs* args) = 0; 261 virtual void notifyVibratorState(const NotifyVibratorStateArgs* args) = 0; 262 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) = 0; 263 virtual void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) = 0; 264 }; 265 266 267 /* 268 * An implementation of the listener interface that queues up and defers dispatch 269 * of decoded events until flushed. 270 */ 271 class QueuedInputListener : public InputListenerInterface { 272 protected: 273 virtual ~QueuedInputListener(); 274 275 public: 276 explicit QueuedInputListener(const sp<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 sp<InputListenerInterface> mInnerListener; 291 std::vector<NotifyArgs*> mArgsQueue; 292 }; 293 294 } // namespace android 295 296 #endif // _UI_INPUT_LISTENER_H 297