Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "InputReader" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log debug messages for each raw event received from the EventHub. |
| 22 | #define DEBUG_RAW_EVENTS 0 |
| 23 | |
| 24 | // Log debug messages about touch screen filtering hacks. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 25 | #define DEBUG_HACKS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | |
| 27 | // Log debug messages about virtual key processing. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 28 | #define DEBUG_VIRTUAL_KEYS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | |
| 30 | // Log debug messages about pointers. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 31 | #define DEBUG_POINTERS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 33 | // Log debug messages about pointer assignment calculations. |
| 34 | #define DEBUG_POINTER_ASSIGNMENT 0 |
| 35 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 36 | #include "InputReader.h" |
| 37 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 38 | #include <cutils/log.h> |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 39 | #include <ui/Keyboard.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 40 | #include <ui/VirtualKeyMap.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | |
| 42 | #include <stddef.h> |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 43 | #include <stdlib.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 44 | #include <unistd.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 45 | #include <errno.h> |
| 46 | #include <limits.h> |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 47 | #include <math.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 49 | #define INDENT " " |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 50 | #define INDENT2 " " |
| 51 | #define INDENT3 " " |
| 52 | #define INDENT4 " " |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 53 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 54 | namespace android { |
| 55 | |
| 56 | // --- Static Functions --- |
| 57 | |
| 58 | template<typename T> |
| 59 | inline static T abs(const T& value) { |
| 60 | return value < 0 ? - value : value; |
| 61 | } |
| 62 | |
| 63 | template<typename T> |
| 64 | inline static T min(const T& a, const T& b) { |
| 65 | return a < b ? a : b; |
| 66 | } |
| 67 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 68 | template<typename T> |
| 69 | inline static void swap(T& a, T& b) { |
| 70 | T temp = a; |
| 71 | a = b; |
| 72 | b = temp; |
| 73 | } |
| 74 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 75 | inline static float avg(float x, float y) { |
| 76 | return (x + y) / 2; |
| 77 | } |
| 78 | |
| 79 | inline static float pythag(float x, float y) { |
| 80 | return sqrtf(x * x + y * y); |
| 81 | } |
| 82 | |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 83 | inline static int32_t signExtendNybble(int32_t value) { |
| 84 | return value >= 8 ? value - 16 : value; |
| 85 | } |
| 86 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 87 | static inline const char* toString(bool value) { |
| 88 | return value ? "true" : "false"; |
| 89 | } |
| 90 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 91 | static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation, |
| 92 | const int32_t map[][4], size_t mapSize) { |
| 93 | if (orientation != DISPLAY_ORIENTATION_0) { |
| 94 | for (size_t i = 0; i < mapSize; i++) { |
| 95 | if (value == map[i][0]) { |
| 96 | return map[i][orientation]; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | return value; |
| 101 | } |
| 102 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 103 | static const int32_t keyCodeRotationMap[][4] = { |
| 104 | // key codes enumerated counter-clockwise with the original (unrotated) key first |
| 105 | // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 106 | { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT }, |
| 107 | { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN }, |
| 108 | { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT }, |
| 109 | { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP }, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 110 | }; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 111 | static const size_t keyCodeRotationMapSize = |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 112 | sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]); |
| 113 | |
| 114 | int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 115 | return rotateValueUsingRotationMap(keyCode, orientation, |
| 116 | keyCodeRotationMap, keyCodeRotationMapSize); |
| 117 | } |
| 118 | |
| 119 | static const int32_t edgeFlagRotationMap[][4] = { |
| 120 | // edge flags enumerated counter-clockwise with the original (unrotated) edge flag first |
| 121 | // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation |
| 122 | { AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT, |
| 123 | AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT }, |
| 124 | { AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP, |
| 125 | AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM }, |
| 126 | { AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT, |
| 127 | AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT }, |
| 128 | { AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM, |
| 129 | AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP }, |
| 130 | }; |
| 131 | static const size_t edgeFlagRotationMapSize = |
| 132 | sizeof(edgeFlagRotationMap) / sizeof(edgeFlagRotationMap[0]); |
| 133 | |
| 134 | static int32_t rotateEdgeFlag(int32_t edgeFlag, int32_t orientation) { |
| 135 | return rotateValueUsingRotationMap(edgeFlag, orientation, |
| 136 | edgeFlagRotationMap, edgeFlagRotationMapSize); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 139 | static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) { |
| 140 | return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0; |
| 141 | } |
| 142 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 143 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 144 | // --- InputReader --- |
| 145 | |
| 146 | InputReader::InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 147 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 148 | const sp<InputDispatcherInterface>& dispatcher) : |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 149 | mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher), |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 150 | mGlobalMetaState(0), mDisableVirtualKeysTimeout(-1) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 151 | configureExcludedDevices(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 152 | updateGlobalMetaState(); |
| 153 | updateInputConfiguration(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | InputReader::~InputReader() { |
| 157 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 158 | delete mDevices.valueAt(i); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void InputReader::loopOnce() { |
| 163 | RawEvent rawEvent; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 164 | mEventHub->getEvent(& rawEvent); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 165 | |
| 166 | #if DEBUG_RAW_EVENTS |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 167 | LOGD("Input event: device=%d type=0x%x scancode=%d keycode=%d value=%d", |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 168 | rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode, |
| 169 | rawEvent.value); |
| 170 | #endif |
| 171 | |
| 172 | process(& rawEvent); |
| 173 | } |
| 174 | |
| 175 | void InputReader::process(const RawEvent* rawEvent) { |
| 176 | switch (rawEvent->type) { |
| 177 | case EventHubInterface::DEVICE_ADDED: |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 178 | addDevice(rawEvent->deviceId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 179 | break; |
| 180 | |
| 181 | case EventHubInterface::DEVICE_REMOVED: |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 182 | removeDevice(rawEvent->deviceId); |
| 183 | break; |
| 184 | |
| 185 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 186 | handleConfigurationChanged(rawEvent->when); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 187 | break; |
| 188 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 189 | default: |
| 190 | consumeEvent(rawEvent); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 195 | void InputReader::addDevice(int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 196 | String8 name = mEventHub->getDeviceName(deviceId); |
| 197 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 198 | |
| 199 | InputDevice* device = createDevice(deviceId, name, classes); |
| 200 | device->configure(); |
| 201 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 202 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 203 | LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 204 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 205 | LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 206 | device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 209 | bool added = false; |
| 210 | { // acquire device registry writer lock |
| 211 | RWLock::AutoWLock _wl(mDeviceRegistryLock); |
| 212 | |
| 213 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 214 | if (deviceIndex < 0) { |
| 215 | mDevices.add(deviceId, device); |
| 216 | added = true; |
| 217 | } |
| 218 | } // release device registry writer lock |
| 219 | |
| 220 | if (! added) { |
| 221 | LOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
| 222 | delete device; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 223 | return; |
| 224 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 227 | void InputReader::removeDevice(int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 228 | bool removed = false; |
| 229 | InputDevice* device = NULL; |
| 230 | { // acquire device registry writer lock |
| 231 | RWLock::AutoWLock _wl(mDeviceRegistryLock); |
| 232 | |
| 233 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 234 | if (deviceIndex >= 0) { |
| 235 | device = mDevices.valueAt(deviceIndex); |
| 236 | mDevices.removeItemsAt(deviceIndex, 1); |
| 237 | removed = true; |
| 238 | } |
| 239 | } // release device registry writer lock |
| 240 | |
| 241 | if (! removed) { |
| 242 | LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 246 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 247 | LOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 248 | device->getId(), device->getName().string()); |
| 249 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 250 | LOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 251 | device->getId(), device->getName().string(), device->getSources()); |
| 252 | } |
| 253 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 254 | device->reset(); |
| 255 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 256 | delete device; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 259 | InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) { |
| 260 | InputDevice* device = new InputDevice(this, deviceId, name); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 261 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 262 | // External devices. |
| 263 | if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { |
| 264 | device->setExternal(true); |
| 265 | } |
| 266 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 267 | // Switch-like devices. |
| 268 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 269 | device->addMapper(new SwitchInputMapper(device)); |
| 270 | } |
| 271 | |
| 272 | // Keyboard-like devices. |
| 273 | uint32_t keyboardSources = 0; |
| 274 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 275 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
| 276 | keyboardSources |= AINPUT_SOURCE_KEYBOARD; |
| 277 | } |
| 278 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 279 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 280 | } |
| 281 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
| 282 | keyboardSources |= AINPUT_SOURCE_DPAD; |
| 283 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 284 | if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { |
| 285 | keyboardSources |= AINPUT_SOURCE_GAMEPAD; |
| 286 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 287 | |
| 288 | if (keyboardSources != 0) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 289 | device->addMapper(new KeyboardInputMapper(device, keyboardSources, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 292 | // Cursor-like devices. |
| 293 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 294 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 297 | // Touchscreens and touchpad devices. |
| 298 | if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 299 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 300 | } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 301 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 304 | // Joystick-like devices. |
| 305 | if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { |
| 306 | device->addMapper(new JoystickInputMapper(device)); |
| 307 | } |
| 308 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 309 | return device; |
| 310 | } |
| 311 | |
| 312 | void InputReader::consumeEvent(const RawEvent* rawEvent) { |
| 313 | int32_t deviceId = rawEvent->deviceId; |
| 314 | |
| 315 | { // acquire device registry reader lock |
| 316 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 317 | |
| 318 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 319 | if (deviceIndex < 0) { |
| 320 | LOGW("Discarding event for unknown deviceId %d.", deviceId); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 325 | if (device->isIgnored()) { |
| 326 | //LOGD("Discarding event for ignored deviceId %d.", deviceId); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | device->process(rawEvent); |
| 331 | } // release device registry reader lock |
| 332 | } |
| 333 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 334 | void InputReader::handleConfigurationChanged(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 335 | // Reset global meta state because it depends on the list of all configured devices. |
| 336 | updateGlobalMetaState(); |
| 337 | |
| 338 | // Update input configuration. |
| 339 | updateInputConfiguration(); |
| 340 | |
| 341 | // Enqueue configuration changed. |
| 342 | mDispatcher->notifyConfigurationChanged(when); |
| 343 | } |
| 344 | |
| 345 | void InputReader::configureExcludedDevices() { |
| 346 | Vector<String8> excludedDeviceNames; |
| 347 | mPolicy->getExcludedDeviceNames(excludedDeviceNames); |
| 348 | |
| 349 | for (size_t i = 0; i < excludedDeviceNames.size(); i++) { |
| 350 | mEventHub->addExcludedDevice(excludedDeviceNames[i]); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void InputReader::updateGlobalMetaState() { |
| 355 | { // acquire state lock |
| 356 | AutoMutex _l(mStateLock); |
| 357 | |
| 358 | mGlobalMetaState = 0; |
| 359 | |
| 360 | { // acquire device registry reader lock |
| 361 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 362 | |
| 363 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 364 | InputDevice* device = mDevices.valueAt(i); |
| 365 | mGlobalMetaState |= device->getMetaState(); |
| 366 | } |
| 367 | } // release device registry reader lock |
| 368 | } // release state lock |
| 369 | } |
| 370 | |
| 371 | int32_t InputReader::getGlobalMetaState() { |
| 372 | { // acquire state lock |
| 373 | AutoMutex _l(mStateLock); |
| 374 | |
| 375 | return mGlobalMetaState; |
| 376 | } // release state lock |
| 377 | } |
| 378 | |
| 379 | void InputReader::updateInputConfiguration() { |
| 380 | { // acquire state lock |
| 381 | AutoMutex _l(mStateLock); |
| 382 | |
| 383 | int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH; |
| 384 | int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS; |
| 385 | int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV; |
| 386 | { // acquire device registry reader lock |
| 387 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 388 | |
| 389 | InputDeviceInfo deviceInfo; |
| 390 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 391 | InputDevice* device = mDevices.valueAt(i); |
| 392 | device->getDeviceInfo(& deviceInfo); |
| 393 | uint32_t sources = deviceInfo.getSources(); |
| 394 | |
| 395 | if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 396 | touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER; |
| 397 | } |
| 398 | if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) { |
| 399 | navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL; |
| 400 | } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) { |
| 401 | navigationConfig = InputConfiguration::NAVIGATION_DPAD; |
| 402 | } |
| 403 | if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 404 | keyboardConfig = InputConfiguration::KEYBOARD_QWERTY; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 407 | } // release device registry reader lock |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 408 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 409 | mInputConfiguration.touchScreen = touchScreenConfig; |
| 410 | mInputConfiguration.keyboard = keyboardConfig; |
| 411 | mInputConfiguration.navigation = navigationConfig; |
| 412 | } // release state lock |
| 413 | } |
| 414 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 415 | void InputReader::disableVirtualKeysUntil(nsecs_t time) { |
| 416 | mDisableVirtualKeysTimeout = time; |
| 417 | } |
| 418 | |
| 419 | bool InputReader::shouldDropVirtualKey(nsecs_t now, |
| 420 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 421 | if (now < mDisableVirtualKeysTimeout) { |
| 422 | LOGI("Dropping virtual key from device %s because virtual keys are " |
| 423 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 424 | device->getName().string(), |
| 425 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 426 | keyCode, scanCode); |
| 427 | return true; |
| 428 | } else { |
| 429 | return false; |
| 430 | } |
| 431 | } |
| 432 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 433 | void InputReader::fadePointer() { |
| 434 | { // acquire device registry reader lock |
| 435 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 436 | |
| 437 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 438 | InputDevice* device = mDevices.valueAt(i); |
| 439 | device->fadePointer(); |
| 440 | } |
| 441 | } // release device registry reader lock |
| 442 | } |
| 443 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 444 | void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) { |
| 445 | { // acquire state lock |
| 446 | AutoMutex _l(mStateLock); |
| 447 | |
| 448 | *outConfiguration = mInputConfiguration; |
| 449 | } // release state lock |
| 450 | } |
| 451 | |
| 452 | status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) { |
| 453 | { // acquire device registry reader lock |
| 454 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 455 | |
| 456 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 457 | if (deviceIndex < 0) { |
| 458 | return NAME_NOT_FOUND; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 461 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 462 | if (device->isIgnored()) { |
| 463 | return NAME_NOT_FOUND; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 464 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 465 | |
| 466 | device->getDeviceInfo(outDeviceInfo); |
| 467 | return OK; |
| 468 | } // release device registy reader lock |
| 469 | } |
| 470 | |
| 471 | void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) { |
| 472 | outDeviceIds.clear(); |
| 473 | |
| 474 | { // acquire device registry reader lock |
| 475 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 476 | |
| 477 | size_t numDevices = mDevices.size(); |
| 478 | for (size_t i = 0; i < numDevices; i++) { |
| 479 | InputDevice* device = mDevices.valueAt(i); |
| 480 | if (! device->isIgnored()) { |
| 481 | outDeviceIds.add(device->getId()); |
| 482 | } |
| 483 | } |
| 484 | } // release device registy reader lock |
| 485 | } |
| 486 | |
| 487 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 488 | int32_t keyCode) { |
| 489 | return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState); |
| 490 | } |
| 491 | |
| 492 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 493 | int32_t scanCode) { |
| 494 | return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState); |
| 495 | } |
| 496 | |
| 497 | int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) { |
| 498 | return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState); |
| 499 | } |
| 500 | |
| 501 | int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code, |
| 502 | GetStateFunc getStateFunc) { |
| 503 | { // acquire device registry reader lock |
| 504 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 505 | |
| 506 | int32_t result = AKEY_STATE_UNKNOWN; |
| 507 | if (deviceId >= 0) { |
| 508 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 509 | if (deviceIndex >= 0) { |
| 510 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 511 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 512 | result = (device->*getStateFunc)(sourceMask, code); |
| 513 | } |
| 514 | } |
| 515 | } else { |
| 516 | size_t numDevices = mDevices.size(); |
| 517 | for (size_t i = 0; i < numDevices; i++) { |
| 518 | InputDevice* device = mDevices.valueAt(i); |
| 519 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 520 | result = (device->*getStateFunc)(sourceMask, code); |
| 521 | if (result >= AKEY_STATE_DOWN) { |
| 522 | return result; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | return result; |
| 528 | } // release device registy reader lock |
| 529 | } |
| 530 | |
| 531 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 532 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 533 | memset(outFlags, 0, numCodes); |
| 534 | return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
| 535 | } |
| 536 | |
| 537 | bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 538 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 539 | { // acquire device registry reader lock |
| 540 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 541 | bool result = false; |
| 542 | if (deviceId >= 0) { |
| 543 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 544 | if (deviceIndex >= 0) { |
| 545 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 546 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 547 | result = device->markSupportedKeyCodes(sourceMask, |
| 548 | numCodes, keyCodes, outFlags); |
| 549 | } |
| 550 | } |
| 551 | } else { |
| 552 | size_t numDevices = mDevices.size(); |
| 553 | for (size_t i = 0; i < numDevices; i++) { |
| 554 | InputDevice* device = mDevices.valueAt(i); |
| 555 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 556 | result |= device->markSupportedKeyCodes(sourceMask, |
| 557 | numCodes, keyCodes, outFlags); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | return result; |
| 562 | } // release device registy reader lock |
| 563 | } |
| 564 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 565 | void InputReader::dump(String8& dump) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 566 | mEventHub->dump(dump); |
| 567 | dump.append("\n"); |
| 568 | |
| 569 | dump.append("Input Reader State:\n"); |
| 570 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 571 | { // acquire device registry reader lock |
| 572 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 573 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 574 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 575 | mDevices.valueAt(i)->dump(dump); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 576 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 577 | } // release device registy reader lock |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 580 | |
| 581 | // --- InputReaderThread --- |
| 582 | |
| 583 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 584 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 585 | } |
| 586 | |
| 587 | InputReaderThread::~InputReaderThread() { |
| 588 | } |
| 589 | |
| 590 | bool InputReaderThread::threadLoop() { |
| 591 | mReader->loopOnce(); |
| 592 | return true; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | // --- InputDevice --- |
| 597 | |
| 598 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) : |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 599 | mContext(context), mId(id), mName(name), mSources(0), mIsExternal(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | InputDevice::~InputDevice() { |
| 603 | size_t numMappers = mMappers.size(); |
| 604 | for (size_t i = 0; i < numMappers; i++) { |
| 605 | delete mMappers[i]; |
| 606 | } |
| 607 | mMappers.clear(); |
| 608 | } |
| 609 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 610 | void InputDevice::dump(String8& dump) { |
| 611 | InputDeviceInfo deviceInfo; |
| 612 | getDeviceInfo(& deviceInfo); |
| 613 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 614 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 615 | deviceInfo.getName().string()); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 616 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 617 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 618 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 619 | |
| 620 | const KeyedVector<int32_t, InputDeviceInfo::MotionRange> ranges = deviceInfo.getMotionRanges(); |
| 621 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 622 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 623 | for (size_t i = 0; i < ranges.size(); i++) { |
| 624 | int32_t axis = ranges.keyAt(i); |
| 625 | const char* label = getAxisLabel(axis); |
| 626 | char name[32]; |
| 627 | if (label) { |
| 628 | strncpy(name, label, sizeof(name)); |
| 629 | name[sizeof(name) - 1] = '\0'; |
| 630 | } else { |
| 631 | snprintf(name, sizeof(name), "%d", axis); |
| 632 | } |
| 633 | const InputDeviceInfo::MotionRange& range = ranges.valueAt(i); |
| 634 | dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 635 | name, range.min, range.max, range.flat, range.fuzz); |
| 636 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | size_t numMappers = mMappers.size(); |
| 640 | for (size_t i = 0; i < numMappers; i++) { |
| 641 | InputMapper* mapper = mMappers[i]; |
| 642 | mapper->dump(dump); |
| 643 | } |
| 644 | } |
| 645 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 646 | void InputDevice::addMapper(InputMapper* mapper) { |
| 647 | mMappers.add(mapper); |
| 648 | } |
| 649 | |
| 650 | void InputDevice::configure() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 651 | if (! isIgnored()) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 652 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 655 | mSources = 0; |
| 656 | |
| 657 | size_t numMappers = mMappers.size(); |
| 658 | for (size_t i = 0; i < numMappers; i++) { |
| 659 | InputMapper* mapper = mMappers[i]; |
| 660 | mapper->configure(); |
| 661 | mSources |= mapper->getSources(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 665 | void InputDevice::reset() { |
| 666 | size_t numMappers = mMappers.size(); |
| 667 | for (size_t i = 0; i < numMappers; i++) { |
| 668 | InputMapper* mapper = mMappers[i]; |
| 669 | mapper->reset(); |
| 670 | } |
| 671 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 672 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 673 | void InputDevice::process(const RawEvent* rawEvent) { |
| 674 | size_t numMappers = mMappers.size(); |
| 675 | for (size_t i = 0; i < numMappers; i++) { |
| 676 | InputMapper* mapper = mMappers[i]; |
| 677 | mapper->process(rawEvent); |
| 678 | } |
| 679 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 680 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 681 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
| 682 | outDeviceInfo->initialize(mId, mName); |
| 683 | |
| 684 | size_t numMappers = mMappers.size(); |
| 685 | for (size_t i = 0; i < numMappers; i++) { |
| 686 | InputMapper* mapper = mMappers[i]; |
| 687 | mapper->populateDeviceInfo(outDeviceInfo); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 692 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 693 | } |
| 694 | |
| 695 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 696 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 697 | } |
| 698 | |
| 699 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 700 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 701 | } |
| 702 | |
| 703 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 704 | int32_t result = AKEY_STATE_UNKNOWN; |
| 705 | size_t numMappers = mMappers.size(); |
| 706 | for (size_t i = 0; i < numMappers; i++) { |
| 707 | InputMapper* mapper = mMappers[i]; |
| 708 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 709 | result = (mapper->*getStateFunc)(sourceMask, code); |
| 710 | if (result >= AKEY_STATE_DOWN) { |
| 711 | return result; |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | return result; |
| 716 | } |
| 717 | |
| 718 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 719 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 720 | bool result = false; |
| 721 | size_t numMappers = mMappers.size(); |
| 722 | for (size_t i = 0; i < numMappers; i++) { |
| 723 | InputMapper* mapper = mMappers[i]; |
| 724 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 725 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 726 | } |
| 727 | } |
| 728 | return result; |
| 729 | } |
| 730 | |
| 731 | int32_t InputDevice::getMetaState() { |
| 732 | int32_t result = 0; |
| 733 | size_t numMappers = mMappers.size(); |
| 734 | for (size_t i = 0; i < numMappers; i++) { |
| 735 | InputMapper* mapper = mMappers[i]; |
| 736 | result |= mapper->getMetaState(); |
| 737 | } |
| 738 | return result; |
| 739 | } |
| 740 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 741 | void InputDevice::fadePointer() { |
| 742 | size_t numMappers = mMappers.size(); |
| 743 | for (size_t i = 0; i < numMappers; i++) { |
| 744 | InputMapper* mapper = mMappers[i]; |
| 745 | mapper->fadePointer(); |
| 746 | } |
| 747 | } |
| 748 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 749 | |
| 750 | // --- InputMapper --- |
| 751 | |
| 752 | InputMapper::InputMapper(InputDevice* device) : |
| 753 | mDevice(device), mContext(device->getContext()) { |
| 754 | } |
| 755 | |
| 756 | InputMapper::~InputMapper() { |
| 757 | } |
| 758 | |
| 759 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 760 | info->addSource(getSources()); |
| 761 | } |
| 762 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 763 | void InputMapper::dump(String8& dump) { |
| 764 | } |
| 765 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 766 | void InputMapper::configure() { |
| 767 | } |
| 768 | |
| 769 | void InputMapper::reset() { |
| 770 | } |
| 771 | |
| 772 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 773 | return AKEY_STATE_UNKNOWN; |
| 774 | } |
| 775 | |
| 776 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 777 | return AKEY_STATE_UNKNOWN; |
| 778 | } |
| 779 | |
| 780 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 781 | return AKEY_STATE_UNKNOWN; |
| 782 | } |
| 783 | |
| 784 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 785 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | int32_t InputMapper::getMetaState() { |
| 790 | return 0; |
| 791 | } |
| 792 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 793 | void InputMapper::fadePointer() { |
| 794 | } |
| 795 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 796 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 797 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 798 | if (axis.valid) { |
| 799 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n", |
| 800 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz); |
| 801 | } else { |
| 802 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 803 | } |
| 804 | } |
| 805 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 806 | |
| 807 | // --- SwitchInputMapper --- |
| 808 | |
| 809 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 810 | InputMapper(device) { |
| 811 | } |
| 812 | |
| 813 | SwitchInputMapper::~SwitchInputMapper() { |
| 814 | } |
| 815 | |
| 816 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 817 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 821 | switch (rawEvent->type) { |
| 822 | case EV_SW: |
| 823 | processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value); |
| 824 | break; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 829 | getDispatcher()->notifySwitch(when, switchCode, switchValue, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 833 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 834 | } |
| 835 | |
| 836 | |
| 837 | // --- KeyboardInputMapper --- |
| 838 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 839 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 840 | uint32_t sources, int32_t keyboardType) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 841 | InputMapper(device), mSources(sources), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 842 | mKeyboardType(keyboardType) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 843 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 847 | } |
| 848 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 849 | void KeyboardInputMapper::initializeLocked() { |
| 850 | mLocked.metaState = AMETA_NONE; |
| 851 | mLocked.downTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | uint32_t KeyboardInputMapper::getSources() { |
| 855 | return mSources; |
| 856 | } |
| 857 | |
| 858 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 859 | InputMapper::populateDeviceInfo(info); |
| 860 | |
| 861 | info->setKeyboardType(mKeyboardType); |
| 862 | } |
| 863 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 864 | void KeyboardInputMapper::dump(String8& dump) { |
| 865 | { // acquire lock |
| 866 | AutoMutex _l(mLock); |
| 867 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 868 | dumpParameters(dump); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 869 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
| 870 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size()); |
| 871 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState); |
| 872 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime); |
| 873 | } // release lock |
| 874 | } |
| 875 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 876 | |
| 877 | void KeyboardInputMapper::configure() { |
| 878 | InputMapper::configure(); |
| 879 | |
| 880 | // Configure basic parameters. |
| 881 | configureParameters(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 882 | |
| 883 | // Reset LEDs. |
| 884 | { |
| 885 | AutoMutex _l(mLock); |
| 886 | resetLedStateLocked(); |
| 887 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | void KeyboardInputMapper::configureParameters() { |
| 891 | mParameters.orientationAware = false; |
| 892 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 893 | mParameters.orientationAware); |
| 894 | |
| 895 | mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1; |
| 896 | } |
| 897 | |
| 898 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 899 | dump.append(INDENT3 "Parameters:\n"); |
| 900 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 901 | mParameters.associatedDisplayId); |
| 902 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 903 | toString(mParameters.orientationAware)); |
| 904 | } |
| 905 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 906 | void KeyboardInputMapper::reset() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 907 | for (;;) { |
| 908 | int32_t keyCode, scanCode; |
| 909 | { // acquire lock |
| 910 | AutoMutex _l(mLock); |
| 911 | |
| 912 | // Synthesize key up event on reset if keys are currently down. |
| 913 | if (mLocked.keyDowns.isEmpty()) { |
| 914 | initializeLocked(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 915 | resetLedStateLocked(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 916 | break; // done |
| 917 | } |
| 918 | |
| 919 | const KeyDown& keyDown = mLocked.keyDowns.top(); |
| 920 | keyCode = keyDown.keyCode; |
| 921 | scanCode = keyDown.scanCode; |
| 922 | } // release lock |
| 923 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 924 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 925 | processKey(when, false, keyCode, scanCode, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 929 | getContext()->updateGlobalMetaState(); |
| 930 | } |
| 931 | |
| 932 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 933 | switch (rawEvent->type) { |
| 934 | case EV_KEY: { |
| 935 | int32_t scanCode = rawEvent->scanCode; |
| 936 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 937 | processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode, |
| 938 | rawEvent->flags); |
| 939 | } |
| 940 | break; |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 946 | return scanCode < BTN_MOUSE |
| 947 | || scanCode >= KEY_OK |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 948 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 951 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 952 | int32_t scanCode, uint32_t policyFlags) { |
| 953 | int32_t newMetaState; |
| 954 | nsecs_t downTime; |
| 955 | bool metaStateChanged = false; |
| 956 | |
| 957 | { // acquire lock |
| 958 | AutoMutex _l(mLock); |
| 959 | |
| 960 | if (down) { |
| 961 | // Rotate key codes according to orientation if needed. |
| 962 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 963 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 964 | int32_t orientation; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 965 | if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 966 | NULL, NULL, & orientation)) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 967 | orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | keyCode = rotateKeyCode(keyCode, orientation); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 973 | // Add key down. |
| 974 | ssize_t keyDownIndex = findKeyDownLocked(scanCode); |
| 975 | if (keyDownIndex >= 0) { |
| 976 | // key repeat, be sure to use same keycode as before in case of rotation |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 977 | keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 978 | } else { |
| 979 | // key down |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 980 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 981 | && mContext->shouldDropVirtualKey(when, |
| 982 | getDevice(), keyCode, scanCode)) { |
| 983 | return; |
| 984 | } |
| 985 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 986 | mLocked.keyDowns.push(); |
| 987 | KeyDown& keyDown = mLocked.keyDowns.editTop(); |
| 988 | keyDown.keyCode = keyCode; |
| 989 | keyDown.scanCode = scanCode; |
| 990 | } |
| 991 | |
| 992 | mLocked.downTime = when; |
| 993 | } else { |
| 994 | // Remove key down. |
| 995 | ssize_t keyDownIndex = findKeyDownLocked(scanCode); |
| 996 | if (keyDownIndex >= 0) { |
| 997 | // key up, be sure to use same keycode as before in case of rotation |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 998 | keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 999 | mLocked.keyDowns.removeAt(size_t(keyDownIndex)); |
| 1000 | } else { |
| 1001 | // key was not actually down |
| 1002 | LOGI("Dropping key up from device %s because the key was not down. " |
| 1003 | "keyCode=%d, scanCode=%d", |
| 1004 | getDeviceName().string(), keyCode, scanCode); |
| 1005 | return; |
| 1006 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1009 | int32_t oldMetaState = mLocked.metaState; |
| 1010 | newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 1011 | if (oldMetaState != newMetaState) { |
| 1012 | mLocked.metaState = newMetaState; |
| 1013 | metaStateChanged = true; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1014 | updateLedStateLocked(false); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1015 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1016 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1017 | downTime = mLocked.downTime; |
| 1018 | } // release lock |
| 1019 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1020 | // Key down on external an keyboard should wake the device. |
| 1021 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 1022 | // For internal keyboards, the key layout file should specify the policy flags for |
| 1023 | // each wake key individually. |
| 1024 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1025 | if (down && getDevice()->isExternal() |
| 1026 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 1027 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1028 | } |
| 1029 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1030 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1031 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1034 | if (down && !isMetaKey(keyCode)) { |
| 1035 | getContext()->fadePointer(); |
| 1036 | } |
| 1037 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1038 | getDispatcher()->notifyKey(when, getDeviceId(), mSources, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1039 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 1040 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1043 | ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) { |
| 1044 | size_t n = mLocked.keyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1045 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1046 | if (mLocked.keyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1047 | return i; |
| 1048 | } |
| 1049 | } |
| 1050 | return -1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1053 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1054 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 1055 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1056 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1057 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1058 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1059 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1060 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1061 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1062 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1063 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 1064 | } |
| 1065 | |
| 1066 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1067 | { // acquire lock |
| 1068 | AutoMutex _l(mLock); |
| 1069 | return mLocked.metaState; |
| 1070 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1073 | void KeyboardInputMapper::resetLedStateLocked() { |
| 1074 | initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL); |
| 1075 | initializeLedStateLocked(mLocked.numLockLedState, LED_NUML); |
| 1076 | initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL); |
| 1077 | |
| 1078 | updateLedStateLocked(true); |
| 1079 | } |
| 1080 | |
| 1081 | void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) { |
| 1082 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 1083 | ledState.on = false; |
| 1084 | } |
| 1085 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1086 | void KeyboardInputMapper::updateLedStateLocked(bool reset) { |
| 1087 | updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1088 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1089 | updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1090 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1091 | updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1092 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState, |
| 1096 | int32_t led, int32_t modifier, bool reset) { |
| 1097 | if (ledState.avail) { |
| 1098 | bool desiredState = (mLocked.metaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1099 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1100 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 1101 | ledState.on = desiredState; |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1106 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1107 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1108 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1109 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1110 | InputMapper(device) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1111 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1114 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1117 | uint32_t CursorInputMapper::getSources() { |
| 1118 | return mSources; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1121 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1122 | InputMapper::populateDeviceInfo(info); |
| 1123 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1124 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 1125 | float minX, minY, maxX, maxY; |
| 1126 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1127 | info->addMotionRange(AMOTION_EVENT_AXIS_X, minX, maxX, 0.0f, 0.0f); |
| 1128 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, minY, maxY, 0.0f, 0.0f); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1129 | } |
| 1130 | } else { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1131 | info->addMotionRange(AMOTION_EVENT_AXIS_X, -1.0f, 1.0f, 0.0f, mXScale); |
| 1132 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, -1.0f, 1.0f, 0.0f, mYScale); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1133 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1134 | info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, 0.0f, 1.0f, 0.0f, 0.0f); |
| 1135 | |
| 1136 | if (mHaveVWheel) { |
| 1137 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, -1.0f, 1.0f, 0.0f, 0.0f); |
| 1138 | } |
| 1139 | if (mHaveHWheel) { |
| 1140 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, -1.0f, 1.0f, 0.0f, 0.0f); |
| 1141 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1144 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1145 | { // acquire lock |
| 1146 | AutoMutex _l(mLock); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1147 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1148 | dumpParameters(dump); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1149 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 1150 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1151 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 1152 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1153 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel)); |
| 1154 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel)); |
| 1155 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 1156 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1157 | dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down)); |
| 1158 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime); |
| 1159 | } // release lock |
| 1160 | } |
| 1161 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1162 | void CursorInputMapper::configure() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1163 | InputMapper::configure(); |
| 1164 | |
| 1165 | // Configure basic parameters. |
| 1166 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1167 | |
| 1168 | // Configure device mode. |
| 1169 | switch (mParameters.mode) { |
| 1170 | case Parameters::MODE_POINTER: |
| 1171 | mSources = AINPUT_SOURCE_MOUSE; |
| 1172 | mXPrecision = 1.0f; |
| 1173 | mYPrecision = 1.0f; |
| 1174 | mXScale = 1.0f; |
| 1175 | mYScale = 1.0f; |
| 1176 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 1177 | break; |
| 1178 | case Parameters::MODE_NAVIGATION: |
| 1179 | mSources = AINPUT_SOURCE_TRACKBALL; |
| 1180 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 1181 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 1182 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 1183 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 1184 | break; |
| 1185 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1186 | |
| 1187 | mVWheelScale = 1.0f; |
| 1188 | mHWheelScale = 1.0f; |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1189 | |
| 1190 | mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL); |
| 1191 | mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1194 | void CursorInputMapper::configureParameters() { |
| 1195 | mParameters.mode = Parameters::MODE_POINTER; |
| 1196 | String8 cursorModeString; |
| 1197 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 1198 | if (cursorModeString == "navigation") { |
| 1199 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 1200 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
| 1201 | LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
| 1202 | } |
| 1203 | } |
| 1204 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1205 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1206 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1207 | mParameters.orientationAware); |
| 1208 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1209 | mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER |
| 1210 | || mParameters.orientationAware ? 0 : -1; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1213 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1214 | dump.append(INDENT3 "Parameters:\n"); |
| 1215 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1216 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1217 | |
| 1218 | switch (mParameters.mode) { |
| 1219 | case Parameters::MODE_POINTER: |
| 1220 | dump.append(INDENT4 "Mode: pointer\n"); |
| 1221 | break; |
| 1222 | case Parameters::MODE_NAVIGATION: |
| 1223 | dump.append(INDENT4 "Mode: navigation\n"); |
| 1224 | break; |
| 1225 | default: |
| 1226 | assert(false); |
| 1227 | } |
| 1228 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1229 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1230 | toString(mParameters.orientationAware)); |
| 1231 | } |
| 1232 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1233 | void CursorInputMapper::initializeLocked() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1234 | mAccumulator.clear(); |
| 1235 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1236 | mLocked.down = false; |
| 1237 | mLocked.downTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1240 | void CursorInputMapper::reset() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1241 | for (;;) { |
| 1242 | { // acquire lock |
| 1243 | AutoMutex _l(mLock); |
| 1244 | |
| 1245 | if (! mLocked.down) { |
| 1246 | initializeLocked(); |
| 1247 | break; // done |
| 1248 | } |
| 1249 | } // release lock |
| 1250 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1251 | // Synthesize button up event on reset. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1252 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1253 | mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1254 | mAccumulator.btnMouse = false; |
| 1255 | sync(when); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1258 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1259 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1260 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1261 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1262 | switch (rawEvent->type) { |
| 1263 | case EV_KEY: |
| 1264 | switch (rawEvent->scanCode) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1265 | case BTN_LEFT: |
| 1266 | case BTN_RIGHT: |
| 1267 | case BTN_MIDDLE: |
| 1268 | case BTN_SIDE: |
| 1269 | case BTN_EXTRA: |
| 1270 | case BTN_FORWARD: |
| 1271 | case BTN_BACK: |
| 1272 | case BTN_TASK: |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1273 | mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE; |
| 1274 | mAccumulator.btnMouse = rawEvent->value != 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1275 | // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and |
| 1276 | // we need to ensure that we report the up/down promptly. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1277 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1278 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1279 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1280 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1281 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1282 | case EV_REL: |
| 1283 | switch (rawEvent->scanCode) { |
| 1284 | case REL_X: |
| 1285 | mAccumulator.fields |= Accumulator::FIELD_REL_X; |
| 1286 | mAccumulator.relX = rawEvent->value; |
| 1287 | break; |
| 1288 | case REL_Y: |
| 1289 | mAccumulator.fields |= Accumulator::FIELD_REL_Y; |
| 1290 | mAccumulator.relY = rawEvent->value; |
| 1291 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1292 | case REL_WHEEL: |
| 1293 | mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL; |
| 1294 | mAccumulator.relWheel = rawEvent->value; |
| 1295 | break; |
| 1296 | case REL_HWHEEL: |
| 1297 | mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL; |
| 1298 | mAccumulator.relHWheel = rawEvent->value; |
| 1299 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1300 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1301 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1302 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1303 | case EV_SYN: |
| 1304 | switch (rawEvent->scanCode) { |
| 1305 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1306 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1307 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1308 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1309 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1310 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1313 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1314 | uint32_t fields = mAccumulator.fields; |
| 1315 | if (fields == 0) { |
| 1316 | return; // no new state changes, so nothing to do |
| 1317 | } |
| 1318 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1319 | int32_t motionEventAction; |
| 1320 | int32_t motionEventEdgeFlags; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1321 | PointerCoords pointerCoords; |
| 1322 | nsecs_t downTime; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1323 | float vscroll, hscroll; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1324 | { // acquire lock |
| 1325 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1326 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1327 | bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE; |
| 1328 | |
| 1329 | if (downChanged) { |
| 1330 | if (mAccumulator.btnMouse) { |
Jeff Brown | 1c9d06e | 2011-01-14 17:24:16 -0800 | [diff] [blame] | 1331 | if (!mLocked.down) { |
| 1332 | mLocked.down = true; |
| 1333 | mLocked.downTime = when; |
| 1334 | } else { |
| 1335 | downChanged = false; |
| 1336 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1337 | } else { |
Jeff Brown | 1c9d06e | 2011-01-14 17:24:16 -0800 | [diff] [blame] | 1338 | if (mLocked.down) { |
| 1339 | mLocked.down = false; |
| 1340 | } else { |
| 1341 | downChanged = false; |
| 1342 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1343 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1344 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1345 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1346 | downTime = mLocked.downTime; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1347 | float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f; |
| 1348 | float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1349 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1350 | if (downChanged) { |
| 1351 | motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1352 | } else if (mLocked.down || mPointerController == NULL) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1353 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1354 | } else { |
| 1355 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1356 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1357 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1358 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1359 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1360 | // Rotate motion based on display orientation if needed. |
| 1361 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
| 1362 | int32_t orientation; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1363 | if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 1364 | NULL, NULL, & orientation)) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1365 | orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | float temp; |
| 1369 | switch (orientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1370 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1371 | temp = deltaX; |
| 1372 | deltaX = deltaY; |
| 1373 | deltaY = -temp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1374 | break; |
| 1375 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1376 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1377 | deltaX = -deltaX; |
| 1378 | deltaY = -deltaY; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1379 | break; |
| 1380 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1381 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1382 | temp = deltaX; |
| 1383 | deltaX = -deltaY; |
| 1384 | deltaY = temp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1385 | break; |
| 1386 | } |
| 1387 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1388 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 1389 | pointerCoords.clear(); |
| 1390 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1391 | motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 1392 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1393 | if (mPointerController != NULL) { |
| 1394 | mPointerController->move(deltaX, deltaY); |
| 1395 | if (downChanged) { |
| 1396 | mPointerController->setButtonState(mLocked.down ? POINTER_BUTTON_1 : 0); |
| 1397 | } |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 1398 | float x, y; |
| 1399 | mPointerController->getPosition(&x, &y); |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1400 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 1401 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1402 | |
| 1403 | if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1404 | float minX, minY, maxX, maxY; |
| 1405 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
| 1406 | if (x <= minX) { |
| 1407 | motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT; |
| 1408 | } else if (x >= maxX) { |
| 1409 | motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT; |
| 1410 | } |
| 1411 | if (y <= minY) { |
| 1412 | motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP; |
| 1413 | } else if (y >= maxY) { |
| 1414 | motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM; |
| 1415 | } |
| 1416 | } |
| 1417 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1418 | } else { |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1419 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 1420 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1423 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, mLocked.down ? 1.0f : 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1424 | |
| 1425 | if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1426 | vscroll = mAccumulator.relWheel; |
| 1427 | } else { |
| 1428 | vscroll = 0; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1429 | } |
| 1430 | if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1431 | hscroll = mAccumulator.relHWheel; |
| 1432 | } else { |
| 1433 | hscroll = 0; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1434 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1435 | if (hscroll != 0 || vscroll != 0) { |
| 1436 | mPointerController->unfade(); |
| 1437 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1438 | } // release lock |
| 1439 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1440 | // Moving an external trackball or mouse should wake the device. |
| 1441 | // We don't do this for internal cursor devices to prevent them from waking up |
| 1442 | // the device in your pocket. |
| 1443 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1444 | uint32_t policyFlags = 0; |
| 1445 | if (getDevice()->isExternal()) { |
| 1446 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1447 | } |
| 1448 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1449 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1450 | int32_t pointerId = 0; |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1451 | getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags, |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1452 | motionEventAction, 0, metaState, motionEventEdgeFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1453 | 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 1454 | |
| 1455 | mAccumulator.clear(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1456 | |
| 1457 | if (vscroll != 0 || hscroll != 0) { |
| 1458 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 1459 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 1460 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1461 | getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags, |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1462 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 1463 | 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 1464 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1467 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 1468 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 1469 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1470 | } else { |
| 1471 | return AKEY_STATE_UNKNOWN; |
| 1472 | } |
| 1473 | } |
| 1474 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1475 | void CursorInputMapper::fadePointer() { |
| 1476 | { // acquire lock |
| 1477 | AutoMutex _l(mLock); |
| 1478 | mPointerController->fade(); |
| 1479 | } // release lock |
| 1480 | } |
| 1481 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1482 | |
| 1483 | // --- TouchInputMapper --- |
| 1484 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1485 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
| 1486 | InputMapper(device) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1487 | mLocked.surfaceOrientation = -1; |
| 1488 | mLocked.surfaceWidth = -1; |
| 1489 | mLocked.surfaceHeight = -1; |
| 1490 | |
| 1491 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | TouchInputMapper::~TouchInputMapper() { |
| 1495 | } |
| 1496 | |
| 1497 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1498 | return mSources; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1502 | InputMapper::populateDeviceInfo(info); |
| 1503 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1504 | { // acquire lock |
| 1505 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1506 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1507 | // Ensure surface information is up to date so that orientation changes are |
| 1508 | // noticed immediately. |
| 1509 | configureSurfaceLocked(); |
| 1510 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1511 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mLocked.orientedRanges.x); |
| 1512 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mLocked.orientedRanges.y); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1513 | |
| 1514 | if (mLocked.orientedRanges.havePressure) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1515 | info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1516 | mLocked.orientedRanges.pressure); |
| 1517 | } |
| 1518 | |
| 1519 | if (mLocked.orientedRanges.haveSize) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1520 | info->addMotionRange(AMOTION_EVENT_AXIS_SIZE, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1521 | mLocked.orientedRanges.size); |
| 1522 | } |
| 1523 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1524 | if (mLocked.orientedRanges.haveTouchSize) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1525 | info->addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MAJOR, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1526 | mLocked.orientedRanges.touchMajor); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1527 | info->addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MINOR, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1528 | mLocked.orientedRanges.touchMinor); |
| 1529 | } |
| 1530 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1531 | if (mLocked.orientedRanges.haveToolSize) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1532 | info->addMotionRange(AMOTION_EVENT_AXIS_TOOL_MAJOR, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1533 | mLocked.orientedRanges.toolMajor); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1534 | info->addMotionRange(AMOTION_EVENT_AXIS_TOOL_MINOR, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1535 | mLocked.orientedRanges.toolMinor); |
| 1536 | } |
| 1537 | |
| 1538 | if (mLocked.orientedRanges.haveOrientation) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1539 | info->addMotionRange(AMOTION_EVENT_AXIS_ORIENTATION, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1540 | mLocked.orientedRanges.orientation); |
| 1541 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1542 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1545 | void TouchInputMapper::dump(String8& dump) { |
| 1546 | { // acquire lock |
| 1547 | AutoMutex _l(mLock); |
| 1548 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1549 | dumpParameters(dump); |
| 1550 | dumpVirtualKeysLocked(dump); |
| 1551 | dumpRawAxes(dump); |
| 1552 | dumpCalibration(dump); |
| 1553 | dumpSurfaceLocked(dump); |
Jeff Brown | 511ee5f | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 1554 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1555 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale); |
| 1556 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale); |
| 1557 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision); |
| 1558 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision); |
| 1559 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale); |
| 1560 | dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale); |
| 1561 | dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias); |
| 1562 | dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale); |
| 1563 | dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias); |
| 1564 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale); |
| 1565 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale); |
| 1566 | dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1567 | } // release lock |
| 1568 | } |
| 1569 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1570 | void TouchInputMapper::initializeLocked() { |
| 1571 | mCurrentTouch.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1572 | mLastTouch.clear(); |
| 1573 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1574 | |
| 1575 | for (uint32_t i = 0; i < MAX_POINTERS; i++) { |
| 1576 | mAveragingTouchFilter.historyStart[i] = 0; |
| 1577 | mAveragingTouchFilter.historyEnd[i] = 0; |
| 1578 | } |
| 1579 | |
| 1580 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1581 | |
| 1582 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1583 | |
| 1584 | mLocked.orientedRanges.havePressure = false; |
| 1585 | mLocked.orientedRanges.haveSize = false; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1586 | mLocked.orientedRanges.haveTouchSize = false; |
| 1587 | mLocked.orientedRanges.haveToolSize = false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1588 | mLocked.orientedRanges.haveOrientation = false; |
| 1589 | } |
| 1590 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1591 | void TouchInputMapper::configure() { |
| 1592 | InputMapper::configure(); |
| 1593 | |
| 1594 | // Configure basic parameters. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1595 | configureParameters(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1596 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1597 | // Configure sources. |
| 1598 | switch (mParameters.deviceType) { |
| 1599 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 1600 | mSources = AINPUT_SOURCE_TOUCHSCREEN; |
| 1601 | break; |
| 1602 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 1603 | mSources = AINPUT_SOURCE_TOUCHPAD; |
| 1604 | break; |
| 1605 | default: |
| 1606 | assert(false); |
| 1607 | } |
| 1608 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1609 | // Configure absolute axis information. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1610 | configureRawAxes(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1611 | |
| 1612 | // Prepare input device calibration. |
| 1613 | parseCalibration(); |
| 1614 | resolveCalibration(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1615 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1616 | { // acquire lock |
| 1617 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1618 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1619 | // Configure surface dimensions and orientation. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1620 | configureSurfaceLocked(); |
| 1621 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1624 | void TouchInputMapper::configureParameters() { |
| 1625 | mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents(); |
| 1626 | mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents(); |
| 1627 | mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents(); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1628 | mParameters.virtualKeyQuietTime = getPolicy()->getVirtualKeyQuietTime(); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1629 | |
| 1630 | String8 deviceTypeString; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 1631 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1632 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 1633 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 1634 | if (deviceTypeString == "touchScreen") { |
| 1635 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 1636 | } else if (deviceTypeString != "touchPad") { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1637 | LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
| 1638 | } |
| 1639 | } |
| 1640 | bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 1641 | |
| 1642 | mParameters.orientationAware = isTouchScreen; |
| 1643 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 1644 | mParameters.orientationAware); |
| 1645 | |
| 1646 | mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1649 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1650 | dump.append(INDENT3 "Parameters:\n"); |
| 1651 | |
| 1652 | switch (mParameters.deviceType) { |
| 1653 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 1654 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 1655 | break; |
| 1656 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 1657 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 1658 | break; |
| 1659 | default: |
| 1660 | assert(false); |
| 1661 | } |
| 1662 | |
| 1663 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1664 | mParameters.associatedDisplayId); |
| 1665 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1666 | toString(mParameters.orientationAware)); |
| 1667 | |
| 1668 | dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1669 | toString(mParameters.useBadTouchFilter)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1670 | dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1671 | toString(mParameters.useAveragingTouchFilter)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1672 | dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1673 | toString(mParameters.useJumpyTouchFilter)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1676 | void TouchInputMapper::configureRawAxes() { |
| 1677 | mRawAxes.x.clear(); |
| 1678 | mRawAxes.y.clear(); |
| 1679 | mRawAxes.pressure.clear(); |
| 1680 | mRawAxes.touchMajor.clear(); |
| 1681 | mRawAxes.touchMinor.clear(); |
| 1682 | mRawAxes.toolMajor.clear(); |
| 1683 | mRawAxes.toolMinor.clear(); |
| 1684 | mRawAxes.orientation.clear(); |
| 1685 | } |
| 1686 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1687 | void TouchInputMapper::dumpRawAxes(String8& dump) { |
| 1688 | dump.append(INDENT3 "Raw Axes:\n"); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1689 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X"); |
| 1690 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y"); |
| 1691 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure"); |
| 1692 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor"); |
| 1693 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor"); |
| 1694 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor"); |
| 1695 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor"); |
| 1696 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1697 | } |
| 1698 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1699 | bool TouchInputMapper::configureSurfaceLocked() { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1700 | // Ensure we have valid X and Y axes. |
| 1701 | if (!mRawAxes.x.valid || !mRawAxes.y.valid) { |
| 1702 | LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! " |
| 1703 | "The device will be inoperable.", getDeviceName().string()); |
| 1704 | return false; |
| 1705 | } |
| 1706 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1707 | // Update orientation and dimensions if needed. |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1708 | int32_t orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1709 | int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1; |
| 1710 | int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1711 | |
| 1712 | if (mParameters.associatedDisplayId >= 0) { |
| 1713 | bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 1714 | bool wantOrientation = mParameters.orientationAware; |
| 1715 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1716 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1717 | if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 1718 | wantSize ? &width : NULL, wantSize ? &height : NULL, |
| 1719 | wantOrientation ? &orientation : NULL)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1720 | return false; |
| 1721 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1724 | bool orientationChanged = mLocked.surfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1725 | if (orientationChanged) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1726 | mLocked.surfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1729 | bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1730 | if (sizeChanged) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1731 | LOGI("Device reconfigured: id=%d, name='%s', display size is now %dx%d", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1732 | getDeviceId(), getDeviceName().string(), width, height); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1733 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1734 | mLocked.surfaceWidth = width; |
| 1735 | mLocked.surfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1736 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1737 | // Configure X and Y factors. |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1738 | mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1); |
| 1739 | mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1); |
| 1740 | mLocked.xPrecision = 1.0f / mLocked.xScale; |
| 1741 | mLocked.yPrecision = 1.0f / mLocked.yScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1742 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1743 | configureVirtualKeysLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1744 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1745 | // Scale factor for terms that are not oriented in a particular axis. |
| 1746 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 1747 | // by choosing an average. |
| 1748 | mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1749 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1750 | // Size of diagonal axis. |
| 1751 | float diagonalSize = pythag(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1752 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1753 | // TouchMajor and TouchMinor factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1754 | if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) { |
| 1755 | mLocked.orientedRanges.haveTouchSize = true; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1756 | mLocked.orientedRanges.touchMajor.min = 0; |
| 1757 | mLocked.orientedRanges.touchMajor.max = diagonalSize; |
| 1758 | mLocked.orientedRanges.touchMajor.flat = 0; |
| 1759 | mLocked.orientedRanges.touchMajor.fuzz = 0; |
| 1760 | mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor; |
| 1761 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1762 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1763 | // ToolMajor and ToolMinor factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1764 | mLocked.toolSizeLinearScale = 0; |
| 1765 | mLocked.toolSizeLinearBias = 0; |
| 1766 | mLocked.toolSizeAreaScale = 0; |
| 1767 | mLocked.toolSizeAreaBias = 0; |
| 1768 | if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) { |
| 1769 | if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) { |
| 1770 | if (mCalibration.haveToolSizeLinearScale) { |
| 1771 | mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1772 | } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1773 | mLocked.toolSizeLinearScale = float(min(width, height)) |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1774 | / mRawAxes.toolMajor.maxValue; |
| 1775 | } |
| 1776 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1777 | if (mCalibration.haveToolSizeLinearBias) { |
| 1778 | mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias; |
| 1779 | } |
| 1780 | } else if (mCalibration.toolSizeCalibration == |
| 1781 | Calibration::TOOL_SIZE_CALIBRATION_AREA) { |
| 1782 | if (mCalibration.haveToolSizeLinearScale) { |
| 1783 | mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale; |
| 1784 | } else { |
| 1785 | mLocked.toolSizeLinearScale = min(width, height); |
| 1786 | } |
| 1787 | |
| 1788 | if (mCalibration.haveToolSizeLinearBias) { |
| 1789 | mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias; |
| 1790 | } |
| 1791 | |
| 1792 | if (mCalibration.haveToolSizeAreaScale) { |
| 1793 | mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale; |
| 1794 | } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
| 1795 | mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue; |
| 1796 | } |
| 1797 | |
| 1798 | if (mCalibration.haveToolSizeAreaBias) { |
| 1799 | mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1800 | } |
| 1801 | } |
| 1802 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1803 | mLocked.orientedRanges.haveToolSize = true; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1804 | mLocked.orientedRanges.toolMajor.min = 0; |
| 1805 | mLocked.orientedRanges.toolMajor.max = diagonalSize; |
| 1806 | mLocked.orientedRanges.toolMajor.flat = 0; |
| 1807 | mLocked.orientedRanges.toolMajor.fuzz = 0; |
| 1808 | mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor; |
| 1809 | } |
| 1810 | |
| 1811 | // Pressure factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1812 | mLocked.pressureScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1813 | if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) { |
| 1814 | RawAbsoluteAxisInfo rawPressureAxis; |
| 1815 | switch (mCalibration.pressureSource) { |
| 1816 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 1817 | rawPressureAxis = mRawAxes.pressure; |
| 1818 | break; |
| 1819 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 1820 | rawPressureAxis = mRawAxes.touchMajor; |
| 1821 | break; |
| 1822 | default: |
| 1823 | rawPressureAxis.clear(); |
| 1824 | } |
| 1825 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1826 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 1827 | || mCalibration.pressureCalibration |
| 1828 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 1829 | if (mCalibration.havePressureScale) { |
| 1830 | mLocked.pressureScale = mCalibration.pressureScale; |
| 1831 | } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) { |
| 1832 | mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | mLocked.orientedRanges.havePressure = true; |
| 1837 | mLocked.orientedRanges.pressure.min = 0; |
| 1838 | mLocked.orientedRanges.pressure.max = 1.0; |
| 1839 | mLocked.orientedRanges.pressure.flat = 0; |
| 1840 | mLocked.orientedRanges.pressure.fuzz = 0; |
| 1841 | } |
| 1842 | |
| 1843 | // Size factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1844 | mLocked.sizeScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1845 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1846 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) { |
| 1847 | if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
| 1848 | mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | mLocked.orientedRanges.haveSize = true; |
| 1853 | mLocked.orientedRanges.size.min = 0; |
| 1854 | mLocked.orientedRanges.size.max = 1.0; |
| 1855 | mLocked.orientedRanges.size.flat = 0; |
| 1856 | mLocked.orientedRanges.size.fuzz = 0; |
| 1857 | } |
| 1858 | |
| 1859 | // Orientation |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1860 | mLocked.orientationScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1861 | if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1862 | if (mCalibration.orientationCalibration |
| 1863 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
| 1864 | if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) { |
| 1865 | mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue; |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | mLocked.orientedRanges.orientation.min = - M_PI_2; |
| 1870 | mLocked.orientedRanges.orientation.max = M_PI_2; |
| 1871 | mLocked.orientedRanges.orientation.flat = 0; |
| 1872 | mLocked.orientedRanges.orientation.fuzz = 0; |
| 1873 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | if (orientationChanged || sizeChanged) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1877 | // Compute oriented surface dimensions, precision, scales and ranges. |
| 1878 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 1879 | // unit less than the total width or height of surface. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1880 | switch (mLocked.surfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1881 | case DISPLAY_ORIENTATION_90: |
| 1882 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1883 | mLocked.orientedSurfaceWidth = mLocked.surfaceHeight; |
| 1884 | mLocked.orientedSurfaceHeight = mLocked.surfaceWidth; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1885 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1886 | mLocked.orientedXPrecision = mLocked.yPrecision; |
| 1887 | mLocked.orientedYPrecision = mLocked.xPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1888 | |
| 1889 | mLocked.orientedRanges.x.min = 0; |
| 1890 | mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue) |
| 1891 | * mLocked.yScale; |
| 1892 | mLocked.orientedRanges.x.flat = 0; |
| 1893 | mLocked.orientedRanges.x.fuzz = mLocked.yScale; |
| 1894 | |
| 1895 | mLocked.orientedRanges.y.min = 0; |
| 1896 | mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue) |
| 1897 | * mLocked.xScale; |
| 1898 | mLocked.orientedRanges.y.flat = 0; |
| 1899 | mLocked.orientedRanges.y.fuzz = mLocked.xScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1900 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1901 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1902 | default: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1903 | mLocked.orientedSurfaceWidth = mLocked.surfaceWidth; |
| 1904 | mLocked.orientedSurfaceHeight = mLocked.surfaceHeight; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1905 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1906 | mLocked.orientedXPrecision = mLocked.xPrecision; |
| 1907 | mLocked.orientedYPrecision = mLocked.yPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1908 | |
| 1909 | mLocked.orientedRanges.x.min = 0; |
| 1910 | mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue) |
| 1911 | * mLocked.xScale; |
| 1912 | mLocked.orientedRanges.x.flat = 0; |
| 1913 | mLocked.orientedRanges.x.fuzz = mLocked.xScale; |
| 1914 | |
| 1915 | mLocked.orientedRanges.y.min = 0; |
| 1916 | mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue) |
| 1917 | * mLocked.yScale; |
| 1918 | mLocked.orientedRanges.y.flat = 0; |
| 1919 | mLocked.orientedRanges.y.fuzz = mLocked.yScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1920 | break; |
| 1921 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | return true; |
| 1925 | } |
| 1926 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1927 | void TouchInputMapper::dumpSurfaceLocked(String8& dump) { |
| 1928 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth); |
| 1929 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight); |
| 1930 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1931 | } |
| 1932 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1933 | void TouchInputMapper::configureVirtualKeysLocked() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1934 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1935 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1936 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1937 | mLocked.virtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1938 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1939 | if (virtualKeyDefinitions.size() == 0) { |
| 1940 | return; |
| 1941 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1942 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1943 | mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size()); |
| 1944 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1945 | int32_t touchScreenLeft = mRawAxes.x.minValue; |
| 1946 | int32_t touchScreenTop = mRawAxes.y.minValue; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 1947 | int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1; |
| 1948 | int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1949 | |
| 1950 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1951 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1952 | virtualKeyDefinitions[i]; |
| 1953 | |
| 1954 | mLocked.virtualKeys.add(); |
| 1955 | VirtualKey& virtualKey = mLocked.virtualKeys.editTop(); |
| 1956 | |
| 1957 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 1958 | int32_t keyCode; |
| 1959 | uint32_t flags; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1960 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1961 | & keyCode, & flags)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1962 | LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
| 1963 | virtualKey.scanCode); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1964 | mLocked.virtualKeys.pop(); // drop the key |
| 1965 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1966 | } |
| 1967 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1968 | virtualKey.keyCode = keyCode; |
| 1969 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1970 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1971 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 1972 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 1973 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1974 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1975 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
| 1976 | * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft; |
| 1977 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
| 1978 | * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft; |
| 1979 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
| 1980 | * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop; |
| 1981 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
| 1982 | * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) { |
| 1987 | if (!mLocked.virtualKeys.isEmpty()) { |
| 1988 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 1989 | |
| 1990 | for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) { |
| 1991 | const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i); |
| 1992 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 1993 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 1994 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 1995 | virtualKey.hitLeft, virtualKey.hitRight, |
| 1996 | virtualKey.hitTop, virtualKey.hitBottom); |
| 1997 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1998 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2001 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2002 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2003 | Calibration& out = mCalibration; |
| 2004 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2005 | // Touch Size |
| 2006 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT; |
| 2007 | String8 touchSizeCalibrationString; |
| 2008 | if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) { |
| 2009 | if (touchSizeCalibrationString == "none") { |
| 2010 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE; |
| 2011 | } else if (touchSizeCalibrationString == "geometric") { |
| 2012 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC; |
| 2013 | } else if (touchSizeCalibrationString == "pressure") { |
| 2014 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE; |
| 2015 | } else if (touchSizeCalibrationString != "default") { |
| 2016 | LOGW("Invalid value for touch.touchSize.calibration: '%s'", |
| 2017 | touchSizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2021 | // Tool Size |
| 2022 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT; |
| 2023 | String8 toolSizeCalibrationString; |
| 2024 | if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) { |
| 2025 | if (toolSizeCalibrationString == "none") { |
| 2026 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE; |
| 2027 | } else if (toolSizeCalibrationString == "geometric") { |
| 2028 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC; |
| 2029 | } else if (toolSizeCalibrationString == "linear") { |
| 2030 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR; |
| 2031 | } else if (toolSizeCalibrationString == "area") { |
| 2032 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA; |
| 2033 | } else if (toolSizeCalibrationString != "default") { |
| 2034 | LOGW("Invalid value for touch.toolSize.calibration: '%s'", |
| 2035 | toolSizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2039 | out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"), |
| 2040 | out.toolSizeLinearScale); |
| 2041 | out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"), |
| 2042 | out.toolSizeLinearBias); |
| 2043 | out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"), |
| 2044 | out.toolSizeAreaScale); |
| 2045 | out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"), |
| 2046 | out.toolSizeAreaBias); |
| 2047 | out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"), |
| 2048 | out.toolSizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2049 | |
| 2050 | // Pressure |
| 2051 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 2052 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2053 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2054 | if (pressureCalibrationString == "none") { |
| 2055 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 2056 | } else if (pressureCalibrationString == "physical") { |
| 2057 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 2058 | } else if (pressureCalibrationString == "amplitude") { |
| 2059 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 2060 | } else if (pressureCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2061 | LOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2062 | pressureCalibrationString.string()); |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT; |
| 2067 | String8 pressureSourceString; |
| 2068 | if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) { |
| 2069 | if (pressureSourceString == "pressure") { |
| 2070 | out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE; |
| 2071 | } else if (pressureSourceString == "touch") { |
| 2072 | out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH; |
| 2073 | } else if (pressureSourceString != "default") { |
| 2074 | LOGW("Invalid value for touch.pressure.source: '%s'", |
| 2075 | pressureSourceString.string()); |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 2080 | out.pressureScale); |
| 2081 | |
| 2082 | // Size |
| 2083 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 2084 | String8 sizeCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2085 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2086 | if (sizeCalibrationString == "none") { |
| 2087 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 2088 | } else if (sizeCalibrationString == "normalized") { |
| 2089 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED; |
| 2090 | } else if (sizeCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2091 | LOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2092 | sizeCalibrationString.string()); |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | // Orientation |
| 2097 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 2098 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2099 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2100 | if (orientationCalibrationString == "none") { |
| 2101 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 2102 | } else if (orientationCalibrationString == "interpolated") { |
| 2103 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2104 | } else if (orientationCalibrationString == "vector") { |
| 2105 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2106 | } else if (orientationCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2107 | LOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2108 | orientationCalibrationString.string()); |
| 2109 | } |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | void TouchInputMapper::resolveCalibration() { |
| 2114 | // Pressure |
| 2115 | switch (mCalibration.pressureSource) { |
| 2116 | case Calibration::PRESSURE_SOURCE_DEFAULT: |
| 2117 | if (mRawAxes.pressure.valid) { |
| 2118 | mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE; |
| 2119 | } else if (mRawAxes.touchMajor.valid) { |
| 2120 | mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH; |
| 2121 | } |
| 2122 | break; |
| 2123 | |
| 2124 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 2125 | if (! mRawAxes.pressure.valid) { |
| 2126 | LOGW("Calibration property touch.pressure.source is 'pressure' but " |
| 2127 | "the pressure axis is not available."); |
| 2128 | } |
| 2129 | break; |
| 2130 | |
| 2131 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 2132 | if (! mRawAxes.touchMajor.valid) { |
| 2133 | LOGW("Calibration property touch.pressure.source is 'touch' but " |
| 2134 | "the touchMajor axis is not available."); |
| 2135 | } |
| 2136 | break; |
| 2137 | |
| 2138 | default: |
| 2139 | break; |
| 2140 | } |
| 2141 | |
| 2142 | switch (mCalibration.pressureCalibration) { |
| 2143 | case Calibration::PRESSURE_CALIBRATION_DEFAULT: |
| 2144 | if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) { |
| 2145 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 2146 | } else { |
| 2147 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 2148 | } |
| 2149 | break; |
| 2150 | |
| 2151 | default: |
| 2152 | break; |
| 2153 | } |
| 2154 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2155 | // Tool Size |
| 2156 | switch (mCalibration.toolSizeCalibration) { |
| 2157 | case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2158 | if (mRawAxes.toolMajor.valid) { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2159 | mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2160 | } else { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2161 | mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2162 | } |
| 2163 | break; |
| 2164 | |
| 2165 | default: |
| 2166 | break; |
| 2167 | } |
| 2168 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2169 | // Touch Size |
| 2170 | switch (mCalibration.touchSizeCalibration) { |
| 2171 | case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2172 | if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2173 | && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) { |
| 2174 | mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2175 | } else { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2176 | mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2177 | } |
| 2178 | break; |
| 2179 | |
| 2180 | default: |
| 2181 | break; |
| 2182 | } |
| 2183 | |
| 2184 | // Size |
| 2185 | switch (mCalibration.sizeCalibration) { |
| 2186 | case Calibration::SIZE_CALIBRATION_DEFAULT: |
| 2187 | if (mRawAxes.toolMajor.valid) { |
| 2188 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED; |
| 2189 | } else { |
| 2190 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 2191 | } |
| 2192 | break; |
| 2193 | |
| 2194 | default: |
| 2195 | break; |
| 2196 | } |
| 2197 | |
| 2198 | // Orientation |
| 2199 | switch (mCalibration.orientationCalibration) { |
| 2200 | case Calibration::ORIENTATION_CALIBRATION_DEFAULT: |
| 2201 | if (mRawAxes.orientation.valid) { |
| 2202 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
| 2203 | } else { |
| 2204 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 2205 | } |
| 2206 | break; |
| 2207 | |
| 2208 | default: |
| 2209 | break; |
| 2210 | } |
| 2211 | } |
| 2212 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2213 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 2214 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2215 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2216 | // Touch Size |
| 2217 | switch (mCalibration.touchSizeCalibration) { |
| 2218 | case Calibration::TOUCH_SIZE_CALIBRATION_NONE: |
| 2219 | dump.append(INDENT4 "touch.touchSize.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2220 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2221 | case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC: |
| 2222 | dump.append(INDENT4 "touch.touchSize.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2223 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2224 | case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE: |
| 2225 | dump.append(INDENT4 "touch.touchSize.calibration: pressure\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2226 | break; |
| 2227 | default: |
| 2228 | assert(false); |
| 2229 | } |
| 2230 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2231 | // Tool Size |
| 2232 | switch (mCalibration.toolSizeCalibration) { |
| 2233 | case Calibration::TOOL_SIZE_CALIBRATION_NONE: |
| 2234 | dump.append(INDENT4 "touch.toolSize.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2235 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2236 | case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC: |
| 2237 | dump.append(INDENT4 "touch.toolSize.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2238 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2239 | case Calibration::TOOL_SIZE_CALIBRATION_LINEAR: |
| 2240 | dump.append(INDENT4 "touch.toolSize.calibration: linear\n"); |
| 2241 | break; |
| 2242 | case Calibration::TOOL_SIZE_CALIBRATION_AREA: |
| 2243 | dump.append(INDENT4 "touch.toolSize.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2244 | break; |
| 2245 | default: |
| 2246 | assert(false); |
| 2247 | } |
| 2248 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2249 | if (mCalibration.haveToolSizeLinearScale) { |
| 2250 | dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n", |
| 2251 | mCalibration.toolSizeLinearScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2254 | if (mCalibration.haveToolSizeLinearBias) { |
| 2255 | dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n", |
| 2256 | mCalibration.toolSizeLinearBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2259 | if (mCalibration.haveToolSizeAreaScale) { |
| 2260 | dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n", |
| 2261 | mCalibration.toolSizeAreaScale); |
| 2262 | } |
| 2263 | |
| 2264 | if (mCalibration.haveToolSizeAreaBias) { |
| 2265 | dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n", |
| 2266 | mCalibration.toolSizeAreaBias); |
| 2267 | } |
| 2268 | |
| 2269 | if (mCalibration.haveToolSizeIsSummed) { |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 2270 | dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n", |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2271 | toString(mCalibration.toolSizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | // Pressure |
| 2275 | switch (mCalibration.pressureCalibration) { |
| 2276 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2277 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2278 | break; |
| 2279 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2280 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2281 | break; |
| 2282 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2283 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2284 | break; |
| 2285 | default: |
| 2286 | assert(false); |
| 2287 | } |
| 2288 | |
| 2289 | switch (mCalibration.pressureSource) { |
| 2290 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2291 | dump.append(INDENT4 "touch.pressure.source: pressure\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2292 | break; |
| 2293 | case Calibration::PRESSURE_SOURCE_TOUCH: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2294 | dump.append(INDENT4 "touch.pressure.source: touch\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2295 | break; |
| 2296 | case Calibration::PRESSURE_SOURCE_DEFAULT: |
| 2297 | break; |
| 2298 | default: |
| 2299 | assert(false); |
| 2300 | } |
| 2301 | |
| 2302 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2303 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 2304 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | // Size |
| 2308 | switch (mCalibration.sizeCalibration) { |
| 2309 | case Calibration::SIZE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2310 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2311 | break; |
| 2312 | case Calibration::SIZE_CALIBRATION_NORMALIZED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2313 | dump.append(INDENT4 "touch.size.calibration: normalized\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2314 | break; |
| 2315 | default: |
| 2316 | assert(false); |
| 2317 | } |
| 2318 | |
| 2319 | // Orientation |
| 2320 | switch (mCalibration.orientationCalibration) { |
| 2321 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2322 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2323 | break; |
| 2324 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2325 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2326 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2327 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 2328 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 2329 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2330 | default: |
| 2331 | assert(false); |
| 2332 | } |
| 2333 | } |
| 2334 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2335 | void TouchInputMapper::reset() { |
| 2336 | // Synthesize touch up event if touch is currently down. |
| 2337 | // This will also take care of finishing virtual key processing if needed. |
| 2338 | if (mLastTouch.pointerCount != 0) { |
| 2339 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2340 | mCurrentTouch.clear(); |
| 2341 | syncTouch(when, true); |
| 2342 | } |
| 2343 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2344 | { // acquire lock |
| 2345 | AutoMutex _l(mLock); |
| 2346 | initializeLocked(); |
| 2347 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2348 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2349 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2353 | // Preprocess pointer data. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2354 | if (mParameters.useBadTouchFilter) { |
| 2355 | if (applyBadTouchFilter()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2356 | havePointerIds = false; |
| 2357 | } |
| 2358 | } |
| 2359 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2360 | if (mParameters.useJumpyTouchFilter) { |
| 2361 | if (applyJumpyTouchFilter()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2362 | havePointerIds = false; |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | if (! havePointerIds) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2367 | calculatePointerIds(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2368 | } |
| 2369 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2370 | TouchData temp; |
| 2371 | TouchData* savedTouch; |
| 2372 | if (mParameters.useAveragingTouchFilter) { |
| 2373 | temp.copyFrom(mCurrentTouch); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2374 | savedTouch = & temp; |
| 2375 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2376 | applyAveragingTouchFilter(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2377 | } else { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2378 | savedTouch = & mCurrentTouch; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2379 | } |
| 2380 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2381 | uint32_t policyFlags = 0; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2382 | if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2383 | // Hide the pointer on an initial down. |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2384 | getContext()->fadePointer(); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2385 | |
| 2386 | // Initial downs on external touch devices should wake the device. |
| 2387 | // We don't do this for internal touch screens to prevent them from waking |
| 2388 | // up in your pocket. |
| 2389 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2390 | if (getDevice()->isExternal()) { |
| 2391 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2392 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2393 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2394 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2395 | // Process touches and virtual keys. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2396 | TouchResult touchResult = consumeOffScreenTouches(when, policyFlags); |
| 2397 | if (touchResult == DISPATCH_TOUCH) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2398 | detectGestures(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2399 | dispatchTouches(when, policyFlags); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2400 | } |
| 2401 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2402 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2403 | if (touchResult == DROP_STROKE) { |
| 2404 | mLastTouch.clear(); |
| 2405 | } else { |
| 2406 | mLastTouch.copyFrom(*savedTouch); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2407 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2410 | TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches( |
| 2411 | nsecs_t when, uint32_t policyFlags) { |
| 2412 | int32_t keyEventAction, keyEventFlags; |
| 2413 | int32_t keyCode, scanCode, downTime; |
| 2414 | TouchResult touchResult; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 2415 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2416 | { // acquire lock |
| 2417 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2418 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2419 | // Update surface size and orientation, including virtual key positions. |
| 2420 | if (! configureSurfaceLocked()) { |
| 2421 | return DROP_STROKE; |
| 2422 | } |
| 2423 | |
| 2424 | // Check for virtual key press. |
| 2425 | if (mLocked.currentVirtualKey.down) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2426 | if (mCurrentTouch.pointerCount == 0) { |
| 2427 | // Pointer went up while virtual key was down. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2428 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2429 | #if DEBUG_VIRTUAL_KEYS |
| 2430 | LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2431 | mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2432 | #endif |
| 2433 | keyEventAction = AKEY_EVENT_ACTION_UP; |
| 2434 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 2435 | touchResult = SKIP_TOUCH; |
| 2436 | goto DispatchVirtualKey; |
| 2437 | } |
| 2438 | |
| 2439 | if (mCurrentTouch.pointerCount == 1) { |
| 2440 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2441 | int32_t y = mCurrentTouch.pointers[0].y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2442 | const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y); |
| 2443 | if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2444 | // Pointer is still within the space of the virtual key. |
| 2445 | return SKIP_TOUCH; |
| 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | // Pointer left virtual key area or another pointer also went down. |
| 2450 | // Send key cancellation and drop the stroke so subsequent motions will be |
| 2451 | // considered fresh downs. This is useful when the user swipes away from the |
| 2452 | // virtual key area into the main display surface. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2453 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2454 | #if DEBUG_VIRTUAL_KEYS |
| 2455 | LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2456 | mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2457 | #endif |
| 2458 | keyEventAction = AKEY_EVENT_ACTION_UP; |
| 2459 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 2460 | | AKEY_EVENT_FLAG_CANCELED; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2461 | |
| 2462 | // Check whether the pointer moved inside the display area where we should |
| 2463 | // start a new stroke. |
| 2464 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2465 | int32_t y = mCurrentTouch.pointers[0].y; |
| 2466 | if (isPointInsideSurfaceLocked(x, y)) { |
| 2467 | mLastTouch.clear(); |
| 2468 | touchResult = DISPATCH_TOUCH; |
| 2469 | } else { |
| 2470 | touchResult = DROP_STROKE; |
| 2471 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2472 | } else { |
| 2473 | if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) { |
| 2474 | // Pointer just went down. Handle off-screen touches, if needed. |
| 2475 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2476 | int32_t y = mCurrentTouch.pointers[0].y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2477 | if (! isPointInsideSurfaceLocked(x, y)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2478 | // If exactly one pointer went down, check for virtual key hit. |
| 2479 | // Otherwise we will drop the entire stroke. |
| 2480 | if (mCurrentTouch.pointerCount == 1) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2481 | const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2482 | if (virtualKey) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2483 | if (mContext->shouldDropVirtualKey(when, getDevice(), |
| 2484 | virtualKey->keyCode, virtualKey->scanCode)) { |
| 2485 | return DROP_STROKE; |
| 2486 | } |
| 2487 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2488 | mLocked.currentVirtualKey.down = true; |
| 2489 | mLocked.currentVirtualKey.downTime = when; |
| 2490 | mLocked.currentVirtualKey.keyCode = virtualKey->keyCode; |
| 2491 | mLocked.currentVirtualKey.scanCode = virtualKey->scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2492 | #if DEBUG_VIRTUAL_KEYS |
| 2493 | LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2494 | mLocked.currentVirtualKey.keyCode, |
| 2495 | mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2496 | #endif |
| 2497 | keyEventAction = AKEY_EVENT_ACTION_DOWN; |
| 2498 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM |
| 2499 | | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 2500 | touchResult = SKIP_TOUCH; |
| 2501 | goto DispatchVirtualKey; |
| 2502 | } |
| 2503 | } |
| 2504 | return DROP_STROKE; |
| 2505 | } |
| 2506 | } |
| 2507 | return DISPATCH_TOUCH; |
| 2508 | } |
| 2509 | |
| 2510 | DispatchVirtualKey: |
| 2511 | // Collect remaining state needed to dispatch virtual key. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2512 | keyCode = mLocked.currentVirtualKey.keyCode; |
| 2513 | scanCode = mLocked.currentVirtualKey.scanCode; |
| 2514 | downTime = mLocked.currentVirtualKey.downTime; |
| 2515 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2516 | |
| 2517 | // Dispatch virtual key. |
| 2518 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | 0eaf393 | 2010-10-01 14:55:30 -0700 | [diff] [blame] | 2519 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2520 | getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 2521 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 2522 | return touchResult; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2525 | void TouchInputMapper::detectGestures(nsecs_t when) { |
| 2526 | // Disable all virtual key touches that happen within a short time interval of the |
| 2527 | // most recent touch. The idea is to filter out stray virtual key presses when |
| 2528 | // interacting with the touch screen. |
| 2529 | // |
| 2530 | // Problems we're trying to solve: |
| 2531 | // |
| 2532 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 2533 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 2534 | // triggers a virtual key. |
| 2535 | // |
| 2536 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 2537 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 2538 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 2539 | // is displayed. |
| 2540 | if (mParameters.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) { |
| 2541 | mContext->disableVirtualKeysUntil(when + mParameters.virtualKeyQuietTime); |
| 2542 | } |
| 2543 | } |
| 2544 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2545 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
| 2546 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 2547 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2548 | if (currentPointerCount == 0 && lastPointerCount == 0) { |
| 2549 | return; // nothing to do! |
| 2550 | } |
| 2551 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2552 | BitSet32 currentIdBits = mCurrentTouch.idBits; |
| 2553 | BitSet32 lastIdBits = mLastTouch.idBits; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2554 | |
| 2555 | if (currentIdBits == lastIdBits) { |
| 2556 | // No pointer id changes so this is a move event. |
| 2557 | // The dispatcher takes care of batching moves so we don't have to deal with that here. |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 2558 | int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2559 | dispatchTouch(when, policyFlags, & mCurrentTouch, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2560 | currentIdBits, -1, currentPointerCount, motionEventAction); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2561 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2562 | // There may be pointers going up and pointers going down and pointers moving |
| 2563 | // all at the same time. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2564 | BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value); |
| 2565 | BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value); |
| 2566 | BitSet32 activeIdBits(lastIdBits.value); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2567 | uint32_t pointerCount = lastPointerCount; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2568 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2569 | // Produce an intermediate representation of the touch data that consists of the |
| 2570 | // old location of pointers that have just gone up and the new location of pointers that |
| 2571 | // have just moved but omits the location of pointers that have just gone down. |
| 2572 | TouchData interimTouch; |
| 2573 | interimTouch.copyFrom(mLastTouch); |
| 2574 | |
| 2575 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
| 2576 | bool moveNeeded = false; |
| 2577 | while (!moveIdBits.isEmpty()) { |
| 2578 | uint32_t moveId = moveIdBits.firstMarkedBit(); |
| 2579 | moveIdBits.clearBit(moveId); |
| 2580 | |
| 2581 | int32_t oldIndex = mLastTouch.idToIndex[moveId]; |
| 2582 | int32_t newIndex = mCurrentTouch.idToIndex[moveId]; |
| 2583 | if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) { |
| 2584 | interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex]; |
| 2585 | moveNeeded = true; |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | // Dispatch pointer up events using the interim pointer locations. |
| 2590 | while (!upIdBits.isEmpty()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2591 | uint32_t upId = upIdBits.firstMarkedBit(); |
| 2592 | upIdBits.clearBit(upId); |
| 2593 | BitSet32 oldActiveIdBits = activeIdBits; |
| 2594 | activeIdBits.clearBit(upId); |
| 2595 | |
| 2596 | int32_t motionEventAction; |
| 2597 | if (activeIdBits.isEmpty()) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 2598 | motionEventAction = AMOTION_EVENT_ACTION_UP; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2599 | } else { |
Jeff Brown | 00ba884 | 2010-07-16 15:01:56 -0700 | [diff] [blame] | 2600 | motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2601 | } |
| 2602 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2603 | dispatchTouch(when, policyFlags, &interimTouch, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2604 | oldActiveIdBits, upId, pointerCount, motionEventAction); |
| 2605 | pointerCount -= 1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2608 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 2609 | // Although applications receive new locations as part of individual pointer up |
| 2610 | // events, they do not generally handle them except when presented in a move event. |
| 2611 | if (moveNeeded) { |
| 2612 | dispatchTouch(when, policyFlags, &mCurrentTouch, |
| 2613 | activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE); |
| 2614 | } |
| 2615 | |
| 2616 | // Dispatch pointer down events using the new pointer locations. |
| 2617 | while (!downIdBits.isEmpty()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2618 | uint32_t downId = downIdBits.firstMarkedBit(); |
| 2619 | downIdBits.clearBit(downId); |
| 2620 | BitSet32 oldActiveIdBits = activeIdBits; |
| 2621 | activeIdBits.markBit(downId); |
| 2622 | |
| 2623 | int32_t motionEventAction; |
| 2624 | if (oldActiveIdBits.isEmpty()) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 2625 | motionEventAction = AMOTION_EVENT_ACTION_DOWN; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2626 | mDownTime = when; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2627 | } else { |
Jeff Brown | 00ba884 | 2010-07-16 15:01:56 -0700 | [diff] [blame] | 2628 | motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2629 | } |
| 2630 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2631 | pointerCount += 1; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2632 | dispatchTouch(when, policyFlags, &mCurrentTouch, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2633 | activeIdBits, downId, pointerCount, motionEventAction); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2634 | } |
| 2635 | } |
| 2636 | } |
| 2637 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2638 | void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2639 | TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2640 | int32_t motionEventAction) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2641 | int32_t pointerIds[MAX_POINTERS]; |
| 2642 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2643 | int32_t motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2644 | float xPrecision, yPrecision; |
| 2645 | |
| 2646 | { // acquire lock |
| 2647 | AutoMutex _l(mLock); |
| 2648 | |
| 2649 | // Walk through the the active pointers and map touch screen coordinates (TouchData) into |
| 2650 | // display coordinates (PointerCoords) and adjust for display orientation. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2651 | for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2652 | uint32_t id = idBits.firstMarkedBit(); |
| 2653 | idBits.clearBit(id); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2654 | uint32_t inIndex = touch->idToIndex[id]; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2655 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2656 | const PointerData& in = touch->pointers[inIndex]; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2657 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2658 | // ToolMajor and ToolMinor |
| 2659 | float toolMajor, toolMinor; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2660 | switch (mCalibration.toolSizeCalibration) { |
| 2661 | case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2662 | toolMajor = in.toolMajor * mLocked.geometricScale; |
| 2663 | if (mRawAxes.toolMinor.valid) { |
| 2664 | toolMinor = in.toolMinor * mLocked.geometricScale; |
| 2665 | } else { |
| 2666 | toolMinor = toolMajor; |
| 2667 | } |
| 2668 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2669 | case Calibration::TOOL_SIZE_CALIBRATION_LINEAR: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2670 | toolMajor = in.toolMajor != 0 |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2671 | ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2672 | : 0; |
| 2673 | if (mRawAxes.toolMinor.valid) { |
| 2674 | toolMinor = in.toolMinor != 0 |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2675 | ? in.toolMinor * mLocked.toolSizeLinearScale |
| 2676 | + mLocked.toolSizeLinearBias |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2677 | : 0; |
| 2678 | } else { |
| 2679 | toolMinor = toolMajor; |
| 2680 | } |
| 2681 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2682 | case Calibration::TOOL_SIZE_CALIBRATION_AREA: |
| 2683 | if (in.toolMajor != 0) { |
| 2684 | float diameter = sqrtf(in.toolMajor |
| 2685 | * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias); |
| 2686 | toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias; |
| 2687 | } else { |
| 2688 | toolMajor = 0; |
| 2689 | } |
| 2690 | toolMinor = toolMajor; |
| 2691 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2692 | default: |
| 2693 | toolMajor = 0; |
| 2694 | toolMinor = 0; |
| 2695 | break; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2696 | } |
| 2697 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2698 | if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2699 | toolMajor /= pointerCount; |
| 2700 | toolMinor /= pointerCount; |
| 2701 | } |
| 2702 | |
| 2703 | // Pressure |
| 2704 | float rawPressure; |
| 2705 | switch (mCalibration.pressureSource) { |
| 2706 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 2707 | rawPressure = in.pressure; |
| 2708 | break; |
| 2709 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 2710 | rawPressure = in.touchMajor; |
| 2711 | break; |
| 2712 | default: |
| 2713 | rawPressure = 0; |
| 2714 | } |
| 2715 | |
| 2716 | float pressure; |
| 2717 | switch (mCalibration.pressureCalibration) { |
| 2718 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 2719 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 2720 | pressure = rawPressure * mLocked.pressureScale; |
| 2721 | break; |
| 2722 | default: |
| 2723 | pressure = 1; |
| 2724 | break; |
| 2725 | } |
| 2726 | |
| 2727 | // TouchMajor and TouchMinor |
| 2728 | float touchMajor, touchMinor; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2729 | switch (mCalibration.touchSizeCalibration) { |
| 2730 | case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2731 | touchMajor = in.touchMajor * mLocked.geometricScale; |
| 2732 | if (mRawAxes.touchMinor.valid) { |
| 2733 | touchMinor = in.touchMinor * mLocked.geometricScale; |
| 2734 | } else { |
| 2735 | touchMinor = touchMajor; |
| 2736 | } |
| 2737 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2738 | case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2739 | touchMajor = toolMajor * pressure; |
| 2740 | touchMinor = toolMinor * pressure; |
| 2741 | break; |
| 2742 | default: |
| 2743 | touchMajor = 0; |
| 2744 | touchMinor = 0; |
| 2745 | break; |
| 2746 | } |
| 2747 | |
| 2748 | if (touchMajor > toolMajor) { |
| 2749 | touchMajor = toolMajor; |
| 2750 | } |
| 2751 | if (touchMinor > toolMinor) { |
| 2752 | touchMinor = toolMinor; |
| 2753 | } |
| 2754 | |
| 2755 | // Size |
| 2756 | float size; |
| 2757 | switch (mCalibration.sizeCalibration) { |
| 2758 | case Calibration::SIZE_CALIBRATION_NORMALIZED: { |
| 2759 | float rawSize = mRawAxes.toolMinor.valid |
| 2760 | ? avg(in.toolMajor, in.toolMinor) |
| 2761 | : in.toolMajor; |
| 2762 | size = rawSize * mLocked.sizeScale; |
| 2763 | break; |
| 2764 | } |
| 2765 | default: |
| 2766 | size = 0; |
| 2767 | break; |
| 2768 | } |
| 2769 | |
| 2770 | // Orientation |
| 2771 | float orientation; |
| 2772 | switch (mCalibration.orientationCalibration) { |
| 2773 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 2774 | orientation = in.orientation * mLocked.orientationScale; |
| 2775 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2776 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 2777 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 2778 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 2779 | if (c1 != 0 || c2 != 0) { |
| 2780 | orientation = atan2f(c1, c2) * 0.5f; |
Jeff Brown | c3451d4 | 2011-02-15 19:13:20 -0800 | [diff] [blame] | 2781 | float scale = 1.0f + pythag(c1, c2) / 16.0f; |
| 2782 | touchMajor *= scale; |
| 2783 | touchMinor /= scale; |
| 2784 | toolMajor *= scale; |
| 2785 | toolMinor /= scale; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2786 | } else { |
| 2787 | orientation = 0; |
| 2788 | } |
| 2789 | break; |
| 2790 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2791 | default: |
| 2792 | orientation = 0; |
| 2793 | } |
| 2794 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2795 | // X and Y |
| 2796 | // Adjust coords for surface orientation. |
| 2797 | float x, y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2798 | switch (mLocked.surfaceOrientation) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2799 | case DISPLAY_ORIENTATION_90: |
| 2800 | x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale; |
| 2801 | y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2802 | orientation -= M_PI_2; |
| 2803 | if (orientation < - M_PI_2) { |
| 2804 | orientation += M_PI; |
| 2805 | } |
| 2806 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2807 | case DISPLAY_ORIENTATION_180: |
| 2808 | x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale; |
| 2809 | y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2810 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2811 | case DISPLAY_ORIENTATION_270: |
| 2812 | x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale; |
| 2813 | y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2814 | orientation += M_PI_2; |
| 2815 | if (orientation > M_PI_2) { |
| 2816 | orientation -= M_PI; |
| 2817 | } |
| 2818 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2819 | default: |
| 2820 | x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale; |
| 2821 | y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale; |
| 2822 | break; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2825 | // Write output coords. |
| 2826 | PointerCoords& out = pointerCoords[outIndex]; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 2827 | out.clear(); |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 2828 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2829 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 2830 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 2831 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 2832 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 2833 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 2834 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 2835 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 2836 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2837 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2838 | pointerIds[outIndex] = int32_t(id); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2839 | |
| 2840 | if (id == changedId) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2841 | motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2842 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2843 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2844 | |
| 2845 | // Check edge flags by looking only at the first pointer since the flags are |
| 2846 | // global to the event. |
| 2847 | if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2848 | uint32_t inIndex = touch->idToIndex[pointerIds[0]]; |
| 2849 | const PointerData& in = touch->pointers[inIndex]; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 2850 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2851 | if (in.x <= mRawAxes.x.minValue) { |
| 2852 | motionEventEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_LEFT, |
| 2853 | mLocked.surfaceOrientation); |
| 2854 | } else if (in.x >= mRawAxes.x.maxValue) { |
| 2855 | motionEventEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_RIGHT, |
| 2856 | mLocked.surfaceOrientation); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2857 | } |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2858 | if (in.y <= mRawAxes.y.minValue) { |
| 2859 | motionEventEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_TOP, |
| 2860 | mLocked.surfaceOrientation); |
| 2861 | } else if (in.y >= mRawAxes.y.maxValue) { |
| 2862 | motionEventEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_BOTTOM, |
| 2863 | mLocked.surfaceOrientation); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2864 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2865 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2866 | |
| 2867 | xPrecision = mLocked.orientedXPrecision; |
| 2868 | yPrecision = mLocked.orientedYPrecision; |
| 2869 | } // release lock |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2870 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2871 | getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2872 | motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2873 | pointerCount, pointerIds, pointerCoords, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2874 | xPrecision, yPrecision, mDownTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2877 | bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 2878 | return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue |
| 2879 | && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2880 | } |
| 2881 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2882 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked( |
| 2883 | int32_t x, int32_t y) { |
| 2884 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 2885 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 2886 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2887 | |
| 2888 | #if DEBUG_VIRTUAL_KEYS |
| 2889 | LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
| 2890 | "left=%d, top=%d, right=%d, bottom=%d", |
| 2891 | x, y, |
| 2892 | virtualKey.keyCode, virtualKey.scanCode, |
| 2893 | virtualKey.hitLeft, virtualKey.hitTop, |
| 2894 | virtualKey.hitRight, virtualKey.hitBottom); |
| 2895 | #endif |
| 2896 | |
| 2897 | if (virtualKey.isHit(x, y)) { |
| 2898 | return & virtualKey; |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | return NULL; |
| 2903 | } |
| 2904 | |
| 2905 | void TouchInputMapper::calculatePointerIds() { |
| 2906 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 2907 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
| 2908 | |
| 2909 | if (currentPointerCount == 0) { |
| 2910 | // No pointers to assign. |
| 2911 | mCurrentTouch.idBits.clear(); |
| 2912 | } else if (lastPointerCount == 0) { |
| 2913 | // All pointers are new. |
| 2914 | mCurrentTouch.idBits.clear(); |
| 2915 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 2916 | mCurrentTouch.pointers[i].id = i; |
| 2917 | mCurrentTouch.idToIndex[i] = i; |
| 2918 | mCurrentTouch.idBits.markBit(i); |
| 2919 | } |
| 2920 | } else if (currentPointerCount == 1 && lastPointerCount == 1) { |
| 2921 | // Only one pointer and no change in count so it must have the same id as before. |
| 2922 | uint32_t id = mLastTouch.pointers[0].id; |
| 2923 | mCurrentTouch.pointers[0].id = id; |
| 2924 | mCurrentTouch.idToIndex[id] = 0; |
| 2925 | mCurrentTouch.idBits.value = BitSet32::valueForBit(id); |
| 2926 | } else { |
| 2927 | // General case. |
| 2928 | // We build a heap of squared euclidean distances between current and last pointers |
| 2929 | // associated with the current and last pointer indices. Then, we find the best |
| 2930 | // match (by distance) for each current pointer. |
| 2931 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 2932 | |
| 2933 | uint32_t heapSize = 0; |
| 2934 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 2935 | currentPointerIndex++) { |
| 2936 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 2937 | lastPointerIndex++) { |
| 2938 | int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x |
| 2939 | - mLastTouch.pointers[lastPointerIndex].x; |
| 2940 | int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y |
| 2941 | - mLastTouch.pointers[lastPointerIndex].y; |
| 2942 | |
| 2943 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 2944 | |
| 2945 | // Insert new element into the heap (sift up). |
| 2946 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 2947 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 2948 | heap[heapSize].distance = distance; |
| 2949 | heapSize += 1; |
| 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | // Heapify |
| 2954 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 2955 | startIndex -= 1; |
| 2956 | for (uint32_t parentIndex = startIndex; ;) { |
| 2957 | uint32_t childIndex = parentIndex * 2 + 1; |
| 2958 | if (childIndex >= heapSize) { |
| 2959 | break; |
| 2960 | } |
| 2961 | |
| 2962 | if (childIndex + 1 < heapSize |
| 2963 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 2964 | childIndex += 1; |
| 2965 | } |
| 2966 | |
| 2967 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 2968 | break; |
| 2969 | } |
| 2970 | |
| 2971 | swap(heap[parentIndex], heap[childIndex]); |
| 2972 | parentIndex = childIndex; |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | #if DEBUG_POINTER_ASSIGNMENT |
| 2977 | LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize); |
| 2978 | for (size_t i = 0; i < heapSize; i++) { |
| 2979 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 2980 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 2981 | heap[i].distance); |
| 2982 | } |
| 2983 | #endif |
| 2984 | |
| 2985 | // Pull matches out by increasing order of distance. |
| 2986 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 2987 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 2988 | // It also tracks the used pointer id bits. |
| 2989 | BitSet32 matchedLastBits(0); |
| 2990 | BitSet32 matchedCurrentBits(0); |
| 2991 | BitSet32 usedIdBits(0); |
| 2992 | bool first = true; |
| 2993 | for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) { |
| 2994 | for (;;) { |
| 2995 | if (first) { |
| 2996 | // The first time through the loop, we just consume the root element of |
| 2997 | // the heap (the one with smallest distance). |
| 2998 | first = false; |
| 2999 | } else { |
| 3000 | // Previous iterations consumed the root element of the heap. |
| 3001 | // Pop root element off of the heap (sift down). |
| 3002 | heapSize -= 1; |
| 3003 | assert(heapSize > 0); |
| 3004 | |
| 3005 | // Sift down. |
| 3006 | heap[0] = heap[heapSize]; |
| 3007 | for (uint32_t parentIndex = 0; ;) { |
| 3008 | uint32_t childIndex = parentIndex * 2 + 1; |
| 3009 | if (childIndex >= heapSize) { |
| 3010 | break; |
| 3011 | } |
| 3012 | |
| 3013 | if (childIndex + 1 < heapSize |
| 3014 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 3015 | childIndex += 1; |
| 3016 | } |
| 3017 | |
| 3018 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 3019 | break; |
| 3020 | } |
| 3021 | |
| 3022 | swap(heap[parentIndex], heap[childIndex]); |
| 3023 | parentIndex = childIndex; |
| 3024 | } |
| 3025 | |
| 3026 | #if DEBUG_POINTER_ASSIGNMENT |
| 3027 | LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize); |
| 3028 | for (size_t i = 0; i < heapSize; i++) { |
| 3029 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 3030 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 3031 | heap[i].distance); |
| 3032 | } |
| 3033 | #endif |
| 3034 | } |
| 3035 | |
| 3036 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 3037 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 3038 | |
| 3039 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 3040 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 3041 | |
| 3042 | matchedCurrentBits.markBit(currentPointerIndex); |
| 3043 | matchedLastBits.markBit(lastPointerIndex); |
| 3044 | |
| 3045 | uint32_t id = mLastTouch.pointers[lastPointerIndex].id; |
| 3046 | mCurrentTouch.pointers[currentPointerIndex].id = id; |
| 3047 | mCurrentTouch.idToIndex[id] = currentPointerIndex; |
| 3048 | usedIdBits.markBit(id); |
| 3049 | |
| 3050 | #if DEBUG_POINTER_ASSIGNMENT |
| 3051 | LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
| 3052 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 3053 | #endif |
| 3054 | break; |
| 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | // Assign fresh ids to new pointers. |
| 3059 | if (currentPointerCount > lastPointerCount) { |
| 3060 | for (uint32_t i = currentPointerCount - lastPointerCount; ;) { |
| 3061 | uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit(); |
| 3062 | uint32_t id = usedIdBits.firstUnmarkedBit(); |
| 3063 | |
| 3064 | mCurrentTouch.pointers[currentPointerIndex].id = id; |
| 3065 | mCurrentTouch.idToIndex[id] = currentPointerIndex; |
| 3066 | usedIdBits.markBit(id); |
| 3067 | |
| 3068 | #if DEBUG_POINTER_ASSIGNMENT |
| 3069 | LOGD("calculatePointerIds - assigned: cur=%d, id=%d", |
| 3070 | currentPointerIndex, id); |
| 3071 | #endif |
| 3072 | |
| 3073 | if (--i == 0) break; // done |
| 3074 | matchedCurrentBits.markBit(currentPointerIndex); |
| 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | // Fix id bits. |
| 3079 | mCurrentTouch.idBits = usedIdBits; |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | /* Special hack for devices that have bad screen data: if one of the |
| 3084 | * points has moved more than a screen height from the last position, |
| 3085 | * then drop it. */ |
| 3086 | bool TouchInputMapper::applyBadTouchFilter() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3087 | uint32_t pointerCount = mCurrentTouch.pointerCount; |
| 3088 | |
| 3089 | // Nothing to do if there are no points. |
| 3090 | if (pointerCount == 0) { |
| 3091 | return false; |
| 3092 | } |
| 3093 | |
| 3094 | // Don't do anything if a finger is going down or up. We run |
| 3095 | // here before assigning pointer IDs, so there isn't a good |
| 3096 | // way to do per-finger matching. |
| 3097 | if (pointerCount != mLastTouch.pointerCount) { |
| 3098 | return false; |
| 3099 | } |
| 3100 | |
| 3101 | // We consider a single movement across more than a 7/16 of |
| 3102 | // the long size of the screen to be bad. This was a magic value |
| 3103 | // determined by looking at the maximum distance it is feasible |
| 3104 | // to actually move in one sample. |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 3105 | int32_t maxDeltaY = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) * 7 / 16; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3106 | |
| 3107 | // XXX The original code in InputDevice.java included commented out |
| 3108 | // code for testing the X axis. Note that when we drop a point |
| 3109 | // we don't actually restore the old X either. Strange. |
| 3110 | // The old code also tries to track when bad points were previously |
| 3111 | // detected but it turns out that due to the placement of a "break" |
| 3112 | // at the end of the loop, we never set mDroppedBadPoint to true |
| 3113 | // so it is effectively dead code. |
| 3114 | // Need to figure out if the old code is busted or just overcomplicated |
| 3115 | // but working as intended. |
| 3116 | |
| 3117 | // Look through all new points and see if any are farther than |
| 3118 | // acceptable from all previous points. |
| 3119 | for (uint32_t i = pointerCount; i-- > 0; ) { |
| 3120 | int32_t y = mCurrentTouch.pointers[i].y; |
| 3121 | int32_t closestY = INT_MAX; |
| 3122 | int32_t closestDeltaY = 0; |
| 3123 | |
| 3124 | #if DEBUG_HACKS |
| 3125 | LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y); |
| 3126 | #endif |
| 3127 | |
| 3128 | for (uint32_t j = pointerCount; j-- > 0; ) { |
| 3129 | int32_t lastY = mLastTouch.pointers[j].y; |
| 3130 | int32_t deltaY = abs(y - lastY); |
| 3131 | |
| 3132 | #if DEBUG_HACKS |
| 3133 | LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d", |
| 3134 | j, lastY, deltaY); |
| 3135 | #endif |
| 3136 | |
| 3137 | if (deltaY < maxDeltaY) { |
| 3138 | goto SkipSufficientlyClosePoint; |
| 3139 | } |
| 3140 | if (deltaY < closestDeltaY) { |
| 3141 | closestDeltaY = deltaY; |
| 3142 | closestY = lastY; |
| 3143 | } |
| 3144 | } |
| 3145 | |
| 3146 | // Must not have found a close enough match. |
| 3147 | #if DEBUG_HACKS |
| 3148 | LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d", |
| 3149 | i, y, closestY, closestDeltaY, maxDeltaY); |
| 3150 | #endif |
| 3151 | |
| 3152 | mCurrentTouch.pointers[i].y = closestY; |
| 3153 | return true; // XXX original code only corrects one point |
| 3154 | |
| 3155 | SkipSufficientlyClosePoint: ; |
| 3156 | } |
| 3157 | |
| 3158 | // No change. |
| 3159 | return false; |
| 3160 | } |
| 3161 | |
| 3162 | /* Special hack for devices that have bad screen data: drop points where |
| 3163 | * the coordinate value for one axis has jumped to the other pointer's location. |
| 3164 | */ |
| 3165 | bool TouchInputMapper::applyJumpyTouchFilter() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3166 | uint32_t pointerCount = mCurrentTouch.pointerCount; |
| 3167 | if (mLastTouch.pointerCount != pointerCount) { |
| 3168 | #if DEBUG_HACKS |
| 3169 | LOGD("JumpyTouchFilter: Different pointer count %d -> %d", |
| 3170 | mLastTouch.pointerCount, pointerCount); |
| 3171 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 3172 | LOGD(" Pointer %d (%d, %d)", i, |
| 3173 | mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y); |
| 3174 | } |
| 3175 | #endif |
| 3176 | |
| 3177 | if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) { |
| 3178 | if (mLastTouch.pointerCount == 1 && pointerCount == 2) { |
| 3179 | // Just drop the first few events going from 1 to 2 pointers. |
| 3180 | // They're bad often enough that they're not worth considering. |
| 3181 | mCurrentTouch.pointerCount = 1; |
| 3182 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 3183 | |
| 3184 | #if DEBUG_HACKS |
| 3185 | LOGD("JumpyTouchFilter: Pointer 2 dropped"); |
| 3186 | #endif |
| 3187 | return true; |
| 3188 | } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) { |
| 3189 | // The event when we go from 2 -> 1 tends to be messed up too |
| 3190 | mCurrentTouch.pointerCount = 2; |
| 3191 | mCurrentTouch.pointers[0] = mLastTouch.pointers[0]; |
| 3192 | mCurrentTouch.pointers[1] = mLastTouch.pointers[1]; |
| 3193 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 3194 | |
| 3195 | #if DEBUG_HACKS |
| 3196 | for (int32_t i = 0; i < 2; i++) { |
| 3197 | LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i, |
| 3198 | mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y); |
| 3199 | } |
| 3200 | #endif |
| 3201 | return true; |
| 3202 | } |
| 3203 | } |
| 3204 | // Reset jumpy points dropped on other transitions or if limit exceeded. |
| 3205 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
| 3206 | |
| 3207 | #if DEBUG_HACKS |
| 3208 | LOGD("JumpyTouchFilter: Transition - drop limit reset"); |
| 3209 | #endif |
| 3210 | return false; |
| 3211 | } |
| 3212 | |
| 3213 | // We have the same number of pointers as last time. |
| 3214 | // A 'jumpy' point is one where the coordinate value for one axis |
| 3215 | // has jumped to the other pointer's location. No need to do anything |
| 3216 | // else if we only have one pointer. |
| 3217 | if (pointerCount < 2) { |
| 3218 | return false; |
| 3219 | } |
| 3220 | |
| 3221 | if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame^] | 3222 | int jumpyEpsilon = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) / JUMPY_EPSILON_DIVISOR; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3223 | |
| 3224 | // We only replace the single worst jumpy point as characterized by pointer distance |
| 3225 | // in a single axis. |
| 3226 | int32_t badPointerIndex = -1; |
| 3227 | int32_t badPointerReplacementIndex = -1; |
| 3228 | int32_t badPointerDistance = INT_MIN; // distance to be corrected |
| 3229 | |
| 3230 | for (uint32_t i = pointerCount; i-- > 0; ) { |
| 3231 | int32_t x = mCurrentTouch.pointers[i].x; |
| 3232 | int32_t y = mCurrentTouch.pointers[i].y; |
| 3233 | |
| 3234 | #if DEBUG_HACKS |
| 3235 | LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y); |
| 3236 | #endif |
| 3237 | |
| 3238 | // Check if a touch point is too close to another's coordinates |
| 3239 | bool dropX = false, dropY = false; |
| 3240 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 3241 | if (i == j) { |
| 3242 | continue; |
| 3243 | } |
| 3244 | |
| 3245 | if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) { |
| 3246 | dropX = true; |
| 3247 | break; |
| 3248 | } |
| 3249 | |
| 3250 | if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) { |
| 3251 | dropY = true; |
| 3252 | break; |
| 3253 | } |
| 3254 | } |
| 3255 | if (! dropX && ! dropY) { |
| 3256 | continue; // not jumpy |
| 3257 | } |
| 3258 | |
| 3259 | // Find a replacement candidate by comparing with older points on the |
| 3260 | // complementary (non-jumpy) axis. |
| 3261 | int32_t distance = INT_MIN; // distance to be corrected |
| 3262 | int32_t replacementIndex = -1; |
| 3263 | |
| 3264 | if (dropX) { |
| 3265 | // X looks too close. Find an older replacement point with a close Y. |
| 3266 | int32_t smallestDeltaY = INT_MAX; |
| 3267 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 3268 | int32_t deltaY = abs(y - mLastTouch.pointers[j].y); |
| 3269 | if (deltaY < smallestDeltaY) { |
| 3270 | smallestDeltaY = deltaY; |
| 3271 | replacementIndex = j; |
| 3272 | } |
| 3273 | } |
| 3274 | distance = abs(x - mLastTouch.pointers[replacementIndex].x); |
| 3275 | } else { |
| 3276 | // Y looks too close. Find an older replacement point with a close X. |
| 3277 | int32_t smallestDeltaX = INT_MAX; |
| 3278 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 3279 | int32_t deltaX = abs(x - mLastTouch.pointers[j].x); |
| 3280 | if (deltaX < smallestDeltaX) { |
| 3281 | smallestDeltaX = deltaX; |
| 3282 | replacementIndex = j; |
| 3283 | } |
| 3284 | } |
| 3285 | distance = abs(y - mLastTouch.pointers[replacementIndex].y); |
| 3286 | } |
| 3287 | |
| 3288 | // If replacing this pointer would correct a worse error than the previous ones |
| 3289 | // considered, then use this replacement instead. |
| 3290 | if (distance > badPointerDistance) { |
| 3291 | badPointerIndex = i; |
| 3292 | badPointerReplacementIndex = replacementIndex; |
| 3293 | badPointerDistance = distance; |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | // Correct the jumpy pointer if one was found. |
| 3298 | if (badPointerIndex >= 0) { |
| 3299 | #if DEBUG_HACKS |
| 3300 | LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)", |
| 3301 | badPointerIndex, |
| 3302 | mLastTouch.pointers[badPointerReplacementIndex].x, |
| 3303 | mLastTouch.pointers[badPointerReplacementIndex].y); |
| 3304 | #endif |
| 3305 | |
| 3306 | mCurrentTouch.pointers[badPointerIndex].x = |
| 3307 | mLastTouch.pointers[badPointerReplacementIndex].x; |
| 3308 | mCurrentTouch.pointers[badPointerIndex].y = |
| 3309 | mLastTouch.pointers[badPointerReplacementIndex].y; |
| 3310 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 3311 | return true; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
| 3316 | return false; |
| 3317 | } |
| 3318 | |
| 3319 | /* Special hack for devices that have bad screen data: aggregate and |
| 3320 | * compute averages of the coordinate data, to reduce the amount of |
| 3321 | * jitter seen by applications. */ |
| 3322 | void TouchInputMapper::applyAveragingTouchFilter() { |
| 3323 | for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) { |
| 3324 | uint32_t id = mCurrentTouch.pointers[currentIndex].id; |
| 3325 | int32_t x = mCurrentTouch.pointers[currentIndex].x; |
| 3326 | int32_t y = mCurrentTouch.pointers[currentIndex].y; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3327 | int32_t pressure; |
| 3328 | switch (mCalibration.pressureSource) { |
| 3329 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 3330 | pressure = mCurrentTouch.pointers[currentIndex].pressure; |
| 3331 | break; |
| 3332 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 3333 | pressure = mCurrentTouch.pointers[currentIndex].touchMajor; |
| 3334 | break; |
| 3335 | default: |
| 3336 | pressure = 1; |
| 3337 | break; |
| 3338 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3339 | |
| 3340 | if (mLastTouch.idBits.hasBit(id)) { |
| 3341 | // Pointer was down before and is still down now. |
| 3342 | // Compute average over history trace. |
| 3343 | uint32_t start = mAveragingTouchFilter.historyStart[id]; |
| 3344 | uint32_t end = mAveragingTouchFilter.historyEnd[id]; |
| 3345 | |
| 3346 | int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x; |
| 3347 | int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y; |
| 3348 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 3349 | |
| 3350 | #if DEBUG_HACKS |
| 3351 | LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld", |
| 3352 | id, distance); |
| 3353 | #endif |
| 3354 | |
| 3355 | if (distance < AVERAGING_DISTANCE_LIMIT) { |
| 3356 | // Increment end index in preparation for recording new historical data. |
| 3357 | end += 1; |
| 3358 | if (end > AVERAGING_HISTORY_SIZE) { |
| 3359 | end = 0; |
| 3360 | } |
| 3361 | |
| 3362 | // If the end index has looped back to the start index then we have filled |
| 3363 | // the historical trace up to the desired size so we drop the historical |
| 3364 | // data at the start of the trace. |
| 3365 | if (end == start) { |
| 3366 | start += 1; |
| 3367 | if (start > AVERAGING_HISTORY_SIZE) { |
| 3368 | start = 0; |
| 3369 | } |
| 3370 | } |
| 3371 | |
| 3372 | // Add the raw data to the historical trace. |
| 3373 | mAveragingTouchFilter.historyStart[id] = start; |
| 3374 | mAveragingTouchFilter.historyEnd[id] = end; |
| 3375 | mAveragingTouchFilter.historyData[end].pointers[id].x = x; |
| 3376 | mAveragingTouchFilter.historyData[end].pointers[id].y = y; |
| 3377 | mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure; |
| 3378 | |
| 3379 | // Average over all historical positions in the trace by total pressure. |
| 3380 | int32_t averagedX = 0; |
| 3381 | int32_t averagedY = 0; |
| 3382 | int32_t totalPressure = 0; |
| 3383 | for (;;) { |
| 3384 | int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x; |
| 3385 | int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y; |
| 3386 | int32_t historicalPressure = mAveragingTouchFilter.historyData[start] |
| 3387 | .pointers[id].pressure; |
| 3388 | |
| 3389 | averagedX += historicalX * historicalPressure; |
| 3390 | averagedY += historicalY * historicalPressure; |
| 3391 | totalPressure += historicalPressure; |
| 3392 | |
| 3393 | if (start == end) { |
| 3394 | break; |
| 3395 | } |
| 3396 | |
| 3397 | start += 1; |
| 3398 | if (start > AVERAGING_HISTORY_SIZE) { |
| 3399 | start = 0; |
| 3400 | } |
| 3401 | } |
| 3402 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3403 | if (totalPressure != 0) { |
| 3404 | averagedX /= totalPressure; |
| 3405 | averagedY /= totalPressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3406 | |
| 3407 | #if DEBUG_HACKS |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3408 | LOGD("AveragingTouchFilter: Pointer id %d - " |
| 3409 | "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure, |
| 3410 | averagedX, averagedY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3411 | #endif |
| 3412 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3413 | mCurrentTouch.pointers[currentIndex].x = averagedX; |
| 3414 | mCurrentTouch.pointers[currentIndex].y = averagedY; |
| 3415 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3416 | } else { |
| 3417 | #if DEBUG_HACKS |
| 3418 | LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id); |
| 3419 | #endif |
| 3420 | } |
| 3421 | } else { |
| 3422 | #if DEBUG_HACKS |
| 3423 | LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id); |
| 3424 | #endif |
| 3425 | } |
| 3426 | |
| 3427 | // Reset pointer history. |
| 3428 | mAveragingTouchFilter.historyStart[id] = 0; |
| 3429 | mAveragingTouchFilter.historyEnd[id] = 0; |
| 3430 | mAveragingTouchFilter.historyData[0].pointers[id].x = x; |
| 3431 | mAveragingTouchFilter.historyData[0].pointers[id].y = y; |
| 3432 | mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure; |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3437 | { // acquire lock |
| 3438 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3439 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3440 | if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3441 | return AKEY_STATE_VIRTUAL; |
| 3442 | } |
| 3443 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3444 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 3445 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 3446 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3447 | if (virtualKey.keyCode == keyCode) { |
| 3448 | return AKEY_STATE_UP; |
| 3449 | } |
| 3450 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3451 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3452 | |
| 3453 | return AKEY_STATE_UNKNOWN; |
| 3454 | } |
| 3455 | |
| 3456 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3457 | { // acquire lock |
| 3458 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3459 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3460 | if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3461 | return AKEY_STATE_VIRTUAL; |
| 3462 | } |
| 3463 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3464 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 3465 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 3466 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3467 | if (virtualKey.scanCode == scanCode) { |
| 3468 | return AKEY_STATE_UP; |
| 3469 | } |
| 3470 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3471 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3472 | |
| 3473 | return AKEY_STATE_UNKNOWN; |
| 3474 | } |
| 3475 | |
| 3476 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 3477 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3478 | { // acquire lock |
| 3479 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3480 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3481 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 3482 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 3483 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3484 | |
| 3485 | for (size_t i = 0; i < numCodes; i++) { |
| 3486 | if (virtualKey.keyCode == keyCodes[i]) { |
| 3487 | outFlags[i] = 1; |
| 3488 | } |
| 3489 | } |
| 3490 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3491 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3492 | |
| 3493 | return true; |
| 3494 | } |
| 3495 | |
| 3496 | |
| 3497 | // --- SingleTouchInputMapper --- |
| 3498 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3499 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 3500 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3501 | initialize(); |
| 3502 | } |
| 3503 | |
| 3504 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 3505 | } |
| 3506 | |
| 3507 | void SingleTouchInputMapper::initialize() { |
| 3508 | mAccumulator.clear(); |
| 3509 | |
| 3510 | mDown = false; |
| 3511 | mX = 0; |
| 3512 | mY = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3513 | mPressure = 0; // default to 0 for devices that don't report pressure |
| 3514 | mToolWidth = 0; // default to 0 for devices that don't report tool width |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | void SingleTouchInputMapper::reset() { |
| 3518 | TouchInputMapper::reset(); |
| 3519 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3520 | initialize(); |
| 3521 | } |
| 3522 | |
| 3523 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
| 3524 | switch (rawEvent->type) { |
| 3525 | case EV_KEY: |
| 3526 | switch (rawEvent->scanCode) { |
| 3527 | case BTN_TOUCH: |
| 3528 | mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH; |
| 3529 | mAccumulator.btnTouch = rawEvent->value != 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3530 | // Don't sync immediately. Wait until the next SYN_REPORT since we might |
| 3531 | // not have received valid position information yet. This logic assumes that |
| 3532 | // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3533 | break; |
| 3534 | } |
| 3535 | break; |
| 3536 | |
| 3537 | case EV_ABS: |
| 3538 | switch (rawEvent->scanCode) { |
| 3539 | case ABS_X: |
| 3540 | mAccumulator.fields |= Accumulator::FIELD_ABS_X; |
| 3541 | mAccumulator.absX = rawEvent->value; |
| 3542 | break; |
| 3543 | case ABS_Y: |
| 3544 | mAccumulator.fields |= Accumulator::FIELD_ABS_Y; |
| 3545 | mAccumulator.absY = rawEvent->value; |
| 3546 | break; |
| 3547 | case ABS_PRESSURE: |
| 3548 | mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE; |
| 3549 | mAccumulator.absPressure = rawEvent->value; |
| 3550 | break; |
| 3551 | case ABS_TOOL_WIDTH: |
| 3552 | mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH; |
| 3553 | mAccumulator.absToolWidth = rawEvent->value; |
| 3554 | break; |
| 3555 | } |
| 3556 | break; |
| 3557 | |
| 3558 | case EV_SYN: |
| 3559 | switch (rawEvent->scanCode) { |
| 3560 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3561 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3562 | break; |
| 3563 | } |
| 3564 | break; |
| 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | void SingleTouchInputMapper::sync(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3569 | uint32_t fields = mAccumulator.fields; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3570 | if (fields == 0) { |
| 3571 | return; // no new state changes, so nothing to do |
| 3572 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3573 | |
| 3574 | if (fields & Accumulator::FIELD_BTN_TOUCH) { |
| 3575 | mDown = mAccumulator.btnTouch; |
| 3576 | } |
| 3577 | |
| 3578 | if (fields & Accumulator::FIELD_ABS_X) { |
| 3579 | mX = mAccumulator.absX; |
| 3580 | } |
| 3581 | |
| 3582 | if (fields & Accumulator::FIELD_ABS_Y) { |
| 3583 | mY = mAccumulator.absY; |
| 3584 | } |
| 3585 | |
| 3586 | if (fields & Accumulator::FIELD_ABS_PRESSURE) { |
| 3587 | mPressure = mAccumulator.absPressure; |
| 3588 | } |
| 3589 | |
| 3590 | if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3591 | mToolWidth = mAccumulator.absToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | mCurrentTouch.clear(); |
| 3595 | |
| 3596 | if (mDown) { |
| 3597 | mCurrentTouch.pointerCount = 1; |
| 3598 | mCurrentTouch.pointers[0].id = 0; |
| 3599 | mCurrentTouch.pointers[0].x = mX; |
| 3600 | mCurrentTouch.pointers[0].y = mY; |
| 3601 | mCurrentTouch.pointers[0].pressure = mPressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3602 | mCurrentTouch.pointers[0].touchMajor = 0; |
| 3603 | mCurrentTouch.pointers[0].touchMinor = 0; |
| 3604 | mCurrentTouch.pointers[0].toolMajor = mToolWidth; |
| 3605 | mCurrentTouch.pointers[0].toolMinor = mToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3606 | mCurrentTouch.pointers[0].orientation = 0; |
| 3607 | mCurrentTouch.idToIndex[0] = 0; |
| 3608 | mCurrentTouch.idBits.markBit(0); |
| 3609 | } |
| 3610 | |
| 3611 | syncTouch(when, true); |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3612 | |
| 3613 | mAccumulator.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3614 | } |
| 3615 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3616 | void SingleTouchInputMapper::configureRawAxes() { |
| 3617 | TouchInputMapper::configureRawAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3618 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3619 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x); |
| 3620 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y); |
| 3621 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure); |
| 3622 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | |
| 3626 | // --- MultiTouchInputMapper --- |
| 3627 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3628 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
| 3629 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3630 | initialize(); |
| 3631 | } |
| 3632 | |
| 3633 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 3634 | } |
| 3635 | |
| 3636 | void MultiTouchInputMapper::initialize() { |
| 3637 | mAccumulator.clear(); |
| 3638 | } |
| 3639 | |
| 3640 | void MultiTouchInputMapper::reset() { |
| 3641 | TouchInputMapper::reset(); |
| 3642 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3643 | initialize(); |
| 3644 | } |
| 3645 | |
| 3646 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
| 3647 | switch (rawEvent->type) { |
| 3648 | case EV_ABS: { |
| 3649 | uint32_t pointerIndex = mAccumulator.pointerCount; |
| 3650 | Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex]; |
| 3651 | |
| 3652 | switch (rawEvent->scanCode) { |
| 3653 | case ABS_MT_POSITION_X: |
| 3654 | pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X; |
| 3655 | pointer->absMTPositionX = rawEvent->value; |
| 3656 | break; |
| 3657 | case ABS_MT_POSITION_Y: |
| 3658 | pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y; |
| 3659 | pointer->absMTPositionY = rawEvent->value; |
| 3660 | break; |
| 3661 | case ABS_MT_TOUCH_MAJOR: |
| 3662 | pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR; |
| 3663 | pointer->absMTTouchMajor = rawEvent->value; |
| 3664 | break; |
| 3665 | case ABS_MT_TOUCH_MINOR: |
| 3666 | pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR; |
| 3667 | pointer->absMTTouchMinor = rawEvent->value; |
| 3668 | break; |
| 3669 | case ABS_MT_WIDTH_MAJOR: |
| 3670 | pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR; |
| 3671 | pointer->absMTWidthMajor = rawEvent->value; |
| 3672 | break; |
| 3673 | case ABS_MT_WIDTH_MINOR: |
| 3674 | pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR; |
| 3675 | pointer->absMTWidthMinor = rawEvent->value; |
| 3676 | break; |
| 3677 | case ABS_MT_ORIENTATION: |
| 3678 | pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION; |
| 3679 | pointer->absMTOrientation = rawEvent->value; |
| 3680 | break; |
| 3681 | case ABS_MT_TRACKING_ID: |
| 3682 | pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID; |
| 3683 | pointer->absMTTrackingId = rawEvent->value; |
| 3684 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3685 | case ABS_MT_PRESSURE: |
| 3686 | pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE; |
| 3687 | pointer->absMTPressure = rawEvent->value; |
| 3688 | break; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3689 | } |
| 3690 | break; |
| 3691 | } |
| 3692 | |
| 3693 | case EV_SYN: |
| 3694 | switch (rawEvent->scanCode) { |
| 3695 | case SYN_MT_REPORT: { |
| 3696 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 3697 | uint32_t pointerIndex = mAccumulator.pointerCount; |
| 3698 | |
| 3699 | if (mAccumulator.pointers[pointerIndex].fields) { |
| 3700 | if (pointerIndex == MAX_POINTERS) { |
| 3701 | LOGW("MultiTouch device driver returned more than maximum of %d pointers.", |
| 3702 | MAX_POINTERS); |
| 3703 | } else { |
| 3704 | pointerIndex += 1; |
| 3705 | mAccumulator.pointerCount = pointerIndex; |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | mAccumulator.pointers[pointerIndex].clear(); |
| 3710 | break; |
| 3711 | } |
| 3712 | |
| 3713 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3714 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3715 | break; |
| 3716 | } |
| 3717 | break; |
| 3718 | } |
| 3719 | } |
| 3720 | |
| 3721 | void MultiTouchInputMapper::sync(nsecs_t when) { |
| 3722 | static const uint32_t REQUIRED_FIELDS = |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3723 | Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3724 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3725 | uint32_t inCount = mAccumulator.pointerCount; |
| 3726 | uint32_t outCount = 0; |
| 3727 | bool havePointerIds = true; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3728 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3729 | mCurrentTouch.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3730 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3731 | for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3732 | const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex]; |
| 3733 | uint32_t fields = inPointer.fields; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3734 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3735 | if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3736 | // Some drivers send empty MT sync packets without X / Y to indicate a pointer up. |
| 3737 | // Drop this finger. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3738 | continue; |
| 3739 | } |
| 3740 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3741 | PointerData& outPointer = mCurrentTouch.pointers[outCount]; |
| 3742 | outPointer.x = inPointer.absMTPositionX; |
| 3743 | outPointer.y = inPointer.absMTPositionY; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3744 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3745 | if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) { |
| 3746 | if (inPointer.absMTPressure <= 0) { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3747 | // Some devices send sync packets with X / Y but with a 0 pressure to indicate |
| 3748 | // a pointer going up. Drop this finger. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3749 | continue; |
| 3750 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3751 | outPointer.pressure = inPointer.absMTPressure; |
| 3752 | } else { |
| 3753 | // Default pressure to 0 if absent. |
| 3754 | outPointer.pressure = 0; |
| 3755 | } |
| 3756 | |
| 3757 | if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) { |
| 3758 | if (inPointer.absMTTouchMajor <= 0) { |
| 3759 | // Some devices send sync packets with X / Y but with a 0 touch major to indicate |
| 3760 | // a pointer going up. Drop this finger. |
| 3761 | continue; |
| 3762 | } |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3763 | outPointer.touchMajor = inPointer.absMTTouchMajor; |
| 3764 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3765 | // Default touch area to 0 if absent. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3766 | outPointer.touchMajor = 0; |
| 3767 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3768 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3769 | if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) { |
| 3770 | outPointer.touchMinor = inPointer.absMTTouchMinor; |
| 3771 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3772 | // Assume touch area is circular. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3773 | outPointer.touchMinor = outPointer.touchMajor; |
| 3774 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3775 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3776 | if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) { |
| 3777 | outPointer.toolMajor = inPointer.absMTWidthMajor; |
| 3778 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3779 | // Default tool area to 0 if absent. |
| 3780 | outPointer.toolMajor = 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3781 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3782 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3783 | if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) { |
| 3784 | outPointer.toolMinor = inPointer.absMTWidthMinor; |
| 3785 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3786 | // Assume tool area is circular. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3787 | outPointer.toolMinor = outPointer.toolMajor; |
| 3788 | } |
| 3789 | |
| 3790 | if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) { |
| 3791 | outPointer.orientation = inPointer.absMTOrientation; |
| 3792 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3793 | // Default orientation to vertical if absent. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3794 | outPointer.orientation = 0; |
| 3795 | } |
| 3796 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3797 | // Assign pointer id using tracking id if available. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3798 | if (havePointerIds) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3799 | if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) { |
| 3800 | uint32_t id = uint32_t(inPointer.absMTTrackingId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3801 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3802 | if (id > MAX_POINTER_ID) { |
| 3803 | #if DEBUG_POINTERS |
| 3804 | LOGD("Pointers: Ignoring driver provided pointer id %d because " |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3805 | "it is larger than max supported id %d", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3806 | id, MAX_POINTER_ID); |
| 3807 | #endif |
| 3808 | havePointerIds = false; |
| 3809 | } |
| 3810 | else { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3811 | outPointer.id = id; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3812 | mCurrentTouch.idToIndex[id] = outCount; |
| 3813 | mCurrentTouch.idBits.markBit(id); |
| 3814 | } |
| 3815 | } else { |
| 3816 | havePointerIds = false; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3817 | } |
| 3818 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3819 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3820 | outCount += 1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3821 | } |
| 3822 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3823 | mCurrentTouch.pointerCount = outCount; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3824 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3825 | syncTouch(when, havePointerIds); |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3826 | |
| 3827 | mAccumulator.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3830 | void MultiTouchInputMapper::configureRawAxes() { |
| 3831 | TouchInputMapper::configureRawAxes(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3832 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3833 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x); |
| 3834 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y); |
| 3835 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor); |
| 3836 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor); |
| 3837 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor); |
| 3838 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor); |
| 3839 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation); |
| 3840 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3841 | } |
| 3842 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3843 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3844 | // --- JoystickInputMapper --- |
| 3845 | |
| 3846 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 3847 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | JoystickInputMapper::~JoystickInputMapper() { |
| 3851 | } |
| 3852 | |
| 3853 | uint32_t JoystickInputMapper::getSources() { |
| 3854 | return AINPUT_SOURCE_JOYSTICK; |
| 3855 | } |
| 3856 | |
| 3857 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 3858 | InputMapper::populateDeviceInfo(info); |
| 3859 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3860 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 3861 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3862 | info->addMotionRange(axis.axisInfo.axis, axis.min, axis.max, axis.flat, axis.fuzz); |
| 3863 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 3864 | info->addMotionRange(axis.axisInfo.highAxis, axis.min, axis.max, axis.flat, axis.fuzz); |
| 3865 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | void JoystickInputMapper::dump(String8& dump) { |
| 3870 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 3871 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3872 | dump.append(INDENT3 "Axes:\n"); |
| 3873 | size_t numAxes = mAxes.size(); |
| 3874 | for (size_t i = 0; i < numAxes; i++) { |
| 3875 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3876 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3877 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3878 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3879 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3880 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3881 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3882 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 3883 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 3884 | if (label) { |
| 3885 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 3886 | } else { |
| 3887 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 3888 | axis.axisInfo.splitValue); |
| 3889 | } |
| 3890 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 3891 | dump.append(" (invert)"); |
| 3892 | } |
| 3893 | |
| 3894 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 3895 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 3896 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 3897 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 3898 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3899 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n", |
| 3900 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
| 3901 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | void JoystickInputMapper::configure() { |
| 3906 | InputMapper::configure(); |
| 3907 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3908 | // Collect all axes. |
| 3909 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
| 3910 | RawAbsoluteAxisInfo rawAxisInfo; |
| 3911 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo); |
| 3912 | if (rawAxisInfo.valid) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3913 | // Map axis. |
| 3914 | AxisInfo axisInfo; |
| 3915 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3916 | if (!explicitlyMapped) { |
| 3917 | // Axis is not explicitly mapped, will choose a generic axis later. |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3918 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 3919 | axisInfo.axis = -1; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3920 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3921 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3922 | // Apply flat override. |
| 3923 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 3924 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 3925 | |
| 3926 | // Calculate scaling factors and limits. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3927 | Axis axis; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3928 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 3929 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 3930 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 3931 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 3932 | scale, 0.0f, highScale, 0.0f, |
| 3933 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 3934 | } else if (isCenteredAxis(axisInfo.axis)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3935 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 3936 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3937 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 3938 | scale, offset, scale, offset, |
| 3939 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3940 | } else { |
| 3941 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3942 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 3943 | scale, 0.0f, scale, 0.0f, |
| 3944 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 3948 | // in axis values up front. |
| 3949 | axis.filter = axis.flat * 0.25f; |
| 3950 | |
| 3951 | mAxes.add(abs, axis); |
| 3952 | } |
| 3953 | } |
| 3954 | |
| 3955 | // If there are too many axes, start dropping them. |
| 3956 | // Prefer to keep explicitly mapped axes. |
| 3957 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
| 3958 | LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.", |
| 3959 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 3960 | pruneAxes(true); |
| 3961 | pruneAxes(false); |
| 3962 | } |
| 3963 | |
| 3964 | // Assign generic axis ids to remaining axes. |
| 3965 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 3966 | size_t numAxes = mAxes.size(); |
| 3967 | for (size_t i = 0; i < numAxes; i++) { |
| 3968 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3969 | if (axis.axisInfo.axis < 0) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3970 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 3971 | && haveAxis(nextGenericAxisId)) { |
| 3972 | nextGenericAxisId += 1; |
| 3973 | } |
| 3974 | |
| 3975 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3976 | axis.axisInfo.axis = nextGenericAxisId; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3977 | nextGenericAxisId += 1; |
| 3978 | } else { |
| 3979 | LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " |
| 3980 | "have already been assigned to other axes.", |
| 3981 | getDeviceName().string(), mAxes.keyAt(i)); |
| 3982 | mAxes.removeItemsAt(i--); |
| 3983 | numAxes -= 1; |
| 3984 | } |
| 3985 | } |
| 3986 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3987 | } |
| 3988 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3989 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3990 | size_t numAxes = mAxes.size(); |
| 3991 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 3992 | const Axis& axis = mAxes.valueAt(i); |
| 3993 | if (axis.axisInfo.axis == axisId |
| 3994 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 3995 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3996 | return true; |
| 3997 | } |
| 3998 | } |
| 3999 | return false; |
| 4000 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4001 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4002 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 4003 | size_t i = mAxes.size(); |
| 4004 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 4005 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 4006 | continue; |
| 4007 | } |
| 4008 | LOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
| 4009 | getDeviceName().string(), mAxes.keyAt(i)); |
| 4010 | mAxes.removeItemsAt(i); |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 4015 | switch (axis) { |
| 4016 | case AMOTION_EVENT_AXIS_X: |
| 4017 | case AMOTION_EVENT_AXIS_Y: |
| 4018 | case AMOTION_EVENT_AXIS_Z: |
| 4019 | case AMOTION_EVENT_AXIS_RX: |
| 4020 | case AMOTION_EVENT_AXIS_RY: |
| 4021 | case AMOTION_EVENT_AXIS_RZ: |
| 4022 | case AMOTION_EVENT_AXIS_HAT_X: |
| 4023 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 4024 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 4025 | case AMOTION_EVENT_AXIS_RUDDER: |
| 4026 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4027 | return true; |
| 4028 | default: |
| 4029 | return false; |
| 4030 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4031 | } |
| 4032 | |
| 4033 | void JoystickInputMapper::reset() { |
| 4034 | // Recenter all axes. |
| 4035 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4036 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4037 | size_t numAxes = mAxes.size(); |
| 4038 | for (size_t i = 0; i < numAxes; i++) { |
| 4039 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 4040 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | sync(when, true /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4044 | |
| 4045 | InputMapper::reset(); |
| 4046 | } |
| 4047 | |
| 4048 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 4049 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4050 | case EV_ABS: { |
| 4051 | ssize_t index = mAxes.indexOfKey(rawEvent->scanCode); |
| 4052 | if (index >= 0) { |
| 4053 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 4054 | float newValue, highNewValue; |
| 4055 | switch (axis.axisInfo.mode) { |
| 4056 | case AxisInfo::MODE_INVERT: |
| 4057 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 4058 | * axis.scale + axis.offset; |
| 4059 | highNewValue = 0.0f; |
| 4060 | break; |
| 4061 | case AxisInfo::MODE_SPLIT: |
| 4062 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 4063 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 4064 | * axis.scale + axis.offset; |
| 4065 | highNewValue = 0.0f; |
| 4066 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 4067 | newValue = 0.0f; |
| 4068 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 4069 | * axis.highScale + axis.highOffset; |
| 4070 | } else { |
| 4071 | newValue = 0.0f; |
| 4072 | highNewValue = 0.0f; |
| 4073 | } |
| 4074 | break; |
| 4075 | default: |
| 4076 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 4077 | highNewValue = 0.0f; |
| 4078 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4079 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 4080 | axis.newValue = newValue; |
| 4081 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4082 | } |
| 4083 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4084 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4085 | |
| 4086 | case EV_SYN: |
| 4087 | switch (rawEvent->scanCode) { |
| 4088 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4089 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4090 | break; |
| 4091 | } |
| 4092 | break; |
| 4093 | } |
| 4094 | } |
| 4095 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4096 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 4097 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4098 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | int32_t metaState = mContext->getGlobalMetaState(); |
| 4102 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4103 | PointerCoords pointerCoords; |
| 4104 | pointerCoords.clear(); |
| 4105 | |
| 4106 | size_t numAxes = mAxes.size(); |
| 4107 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 4108 | const Axis& axis = mAxes.valueAt(i); |
| 4109 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 4110 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 4111 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 4112 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4113 | } |
| 4114 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 4115 | // Moving a joystick axis should not wake the devide because joysticks can |
| 4116 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 4117 | // button will likely wake the device. |
| 4118 | // TODO: Use the input device configuration to control this behavior more finely. |
| 4119 | uint32_t policyFlags = 0; |
| 4120 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4121 | int32_t pointerId = 0; |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 4122 | getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4123 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4124 | 1, &pointerId, &pointerCoords, 0, 0, 0); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4125 | } |
| 4126 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 4127 | bool JoystickInputMapper::filterAxes(bool force) { |
| 4128 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4129 | size_t numAxes = mAxes.size(); |
| 4130 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 4131 | Axis& axis = mAxes.editValueAt(i); |
| 4132 | if (force || hasValueChangedSignificantly(axis.filter, |
| 4133 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 4134 | axis.currentValue = axis.newValue; |
| 4135 | atLeastOneSignificantChange = true; |
| 4136 | } |
| 4137 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 4138 | if (force || hasValueChangedSignificantly(axis.filter, |
| 4139 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 4140 | axis.highCurrentValue = axis.highNewValue; |
| 4141 | atLeastOneSignificantChange = true; |
| 4142 | } |
| 4143 | } |
| 4144 | } |
| 4145 | return atLeastOneSignificantChange; |
| 4146 | } |
| 4147 | |
| 4148 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 4149 | float filter, float newValue, float currentValue, float min, float max) { |
| 4150 | if (newValue != currentValue) { |
| 4151 | // Filter out small changes in value unless the value is converging on the axis |
| 4152 | // bounds or center point. This is intended to reduce the amount of information |
| 4153 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 4154 | if (fabs(newValue - currentValue) > filter |
| 4155 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 4156 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 4157 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 4158 | return true; |
| 4159 | } |
| 4160 | } |
| 4161 | return false; |
| 4162 | } |
| 4163 | |
| 4164 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 4165 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 4166 | float newDistance = fabs(newValue - thresholdValue); |
| 4167 | if (newDistance < filter) { |
| 4168 | float oldDistance = fabs(currentValue - thresholdValue); |
| 4169 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4170 | return true; |
| 4171 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4172 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 4173 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 4174 | } |
| 4175 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4176 | } // namespace android |