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