InputReader.h revision 12d6baa9b832f16a28f048ed5ffab75a76ed9c41
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2010 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifndef _UI_INPUT_READER_H
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define _UI_INPUT_READER_H
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "EventHub.h"
21de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski#include "PointerControllerInterface.h"
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "InputListener.h"
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <input/Input.h>
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <input/VelocityControl.h>
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <input/VelocityTracker.h>
27c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn#include <ui/DisplayInfo.h>
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/KeyedVector.h>
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/threads.h>
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Timers.h>
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/RefBase.h>
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/String8.h>
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/BitSet.h>
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stddef.h>
36da96e137bcc8191c584ada7b5de31eaae92f244fJeff Sharkey#include <unistd.h>
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Maximum supported size of a vibration pattern.
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Must be at least 2.
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define MAX_VIBRATE_PATTERN_SIZE 100
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Maximum allowable delay value in a vibration pattern before
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// which the delay will be truncated.
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass InputDevice;
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass InputMapper;
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Describes how coordinates are mapped on a physical display.
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See com.android.server.display.DisplayViewport.
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct DisplayViewport {
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t displayId; // -1 if invalid
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t orientation;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t logicalLeft;
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t logicalTop;
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t logicalRight;
6143a17654cf4bfe7f1ec22bd8b7b32daccdf27c09Joe Onorato    int32_t logicalBottom;
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t physicalLeft;
63c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    int32_t physicalTop;
64c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    int32_t physicalRight;
6560d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    int32_t physicalBottom;
667f9f99ea11051614a7727dfb9f9578b518e76e3cXavier Ducrohet    int32_t deviceWidth;
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t deviceHeight;
6860d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn
6960d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    DisplayViewport() :
7060d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn            displayId(ADISPLAY_ID_NONE), orientation(DISPLAY_ORIENTATION_0),
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            logicalLeft(0), logicalTop(0), logicalRight(0), logicalBottom(0),
72896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat            physicalLeft(0), physicalTop(0), physicalRight(0), physicalBottom(0),
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            deviceWidth(0), deviceHeight(0) {
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool operator==(const DisplayViewport& other) const {
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return displayId == other.displayId
78896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat                && orientation == other.orientation
7960d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn                && logicalLeft == other.logicalLeft
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && logicalTop == other.logicalTop
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && logicalRight == other.logicalRight
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && logicalBottom == other.logicalBottom
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && physicalLeft == other.physicalLeft
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && physicalTop == other.physicalTop
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && physicalRight == other.physicalRight
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && physicalBottom == other.physicalBottom
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && deviceWidth == other.deviceWidth
8860d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn                && deviceHeight == other.deviceHeight;
89c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    }
90c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
91c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    bool operator!=(const DisplayViewport& other) const {
92c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        return !(*this == other);
9348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    }
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline bool isValid() const {
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return displayId >= 0;
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void setNonDisplayViewport(int32_t width, int32_t height) {
10060d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn        displayId = ADISPLAY_ID_NONE;
10160d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn        orientation = DISPLAY_ORIENTATION_0;
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        logicalLeft = 0;
1037df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba        logicalTop = 0;
10460d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn        logicalRight = width;
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        logicalBottom = height;
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        physicalLeft = 0;
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        physicalTop = 0;
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        physicalRight = width;
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        physicalBottom = height;
110c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        deviceWidth = width;
111c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        deviceHeight = height;
112c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    }
113c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn};
114c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
115c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn/*
11648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad * Input reader configuration.
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Specifies various options that modify the behavior of the input reader.
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct InputReaderConfiguration {
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Describes changes that have occurred.
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    enum {
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The pointer speed changed.
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        CHANGE_POINTER_SPEED = 1 << 0,
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The pointer gesture control changed.
12760d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn        CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The display size or orientation changed.
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        CHANGE_DISPLAY_INFO = 1 << 2,
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The visible touches option changed.
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        CHANGE_SHOW_TOUCHES = 1 << 3,
13460d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The keyboard layouts must be reloaded.
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The device name alias supplied by the may have changed for some devices.
139c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        CHANGE_DEVICE_ALIAS = 1 << 5,
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The location calibration matrix changed.
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // All devices must be reopened.
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        CHANGE_MUST_REOPEN = 1 << 31,
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Gets the amount of time to disable virtual keys after the screen is touched
14960d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    // in order to filter out accidental virtual key presses due to swiping gestures
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // or taps near the edge of the display.  May be 0 to disable the feature.
15155fc850cf992cdcb0993cb109d2f716613c0dbddKenny Root    nsecs_t virtualKeyQuietTime;
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The excluded device names for the platform.
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Devices with these names will be ignored.
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Vector<String8> excludedDeviceNames;
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Velocity control parameters for mouse pointer movements.
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    VelocityControlParameters pointerVelocityControlParameters;
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Velocity control parameters for mouse wheel movements.
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    VelocityControlParameters wheelVelocityControlParameters;
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // True if pointer gestures are enabled.
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool pointerGesturesEnabled;
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Quiet time between certain pointer gesture transitions.
16760d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    // Time to allow for all fingers or buttons to settle into a stable state before
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // starting a new gesture.
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nsecs_t pointerGestureQuietInterval;
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The minimum speed that a pointer must travel for us to consider switching the active
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // touch pointer to it during a drag.  This threshold is set to avoid switching due
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // to noise from a finger resting on the touch pad (perhaps just pressing it down).
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float pointerGestureDragMinSwitchSpeed; // in pixels per second
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Tap gesture delay time.
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The time between down and up must be less than this to be considered a tap.
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nsecs_t pointerGestureTapInterval;
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Tap drag gesture delay time.
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The time between the previous tap's up and the next down must be less than
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // this to be considered a drag.  Otherwise, the previous tap is finished and a
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // new tap begins.
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Note that the previous tap will be held down for this entire duration so this
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // interval must be shorter than the long press timeout.
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nsecs_t pointerGestureTapDragInterval;
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The distance in pixels that the pointer is allowed to move from initial down
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // to up and still be called a tap.
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float pointerGestureTapSlop; // in pixels
19255fc850cf992cdcb0993cb109d2f716613c0dbddKenny Root
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Time after the first touch points go down to settle on an initial centroid.
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // This is intended to be enough time to handle cases where the user puts down two
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // fingers at almost but not quite exactly the same time.
19655fc850cf992cdcb0993cb109d2f716613c0dbddKenny Root    nsecs_t pointerGestureMultitouchSettleInterval;
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // at least two pointers have moved at least this far from their starting place.
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float pointerGestureMultitouchMinDistance; // in pixels
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The transition from PRESS to SWIPE gesture mode can only occur when the
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // cosine of the angle between the two vectors is greater than or equal to than this value
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // which indicates that the vectors are oriented in the same direction.
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // (In exactly opposite directions, the cosine is -1.0.)
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float pointerGestureSwipeTransitionAngleCosine;
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The transition from PRESS to SWIPE gesture mode can only occur when the
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // fingers are no more than this far apart relative to the diagonal size of
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // the touch pad.  For example, a ratio of 0.5 means that the fingers must be
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // no more than half the diagonal size of the touch pad apart.
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float pointerGestureSwipeMaxWidthRatio;
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The gesture movement speed factor relative to the size of the display.
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Movement speed applies when the fingers are moving in the same direction.
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Without acceleration, a full swipe of the touch pad diagonal in movement mode
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // will cover this portion of the display diagonal.
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float pointerGestureMovementSpeedRatio;
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The gesture zoom speed factor relative to the size of the display.
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Zoom speed applies when the fingers are mostly moving relative to each other
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // to execute a scale gesture or similar.
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // will cover this portion of the display diagonal.
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float pointerGestureZoomSpeedRatio;
227896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // True to show the location of touches on the touch screen as spots.
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool showTouches;
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    InputReaderConfiguration() :
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            virtualKeyQuietTime(0),
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGesturesEnabled(true),
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
23741a5ed7cd9f56d955a797a485c11ae5e7ccfb094Jozef BABJAK            pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureTapInterval(150 * 1000000LL), // 150 ms
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureTapSlop(10.0f), // 10 pixels
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureMultitouchMinDistance(15), // 15 pixels
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureSwipeMaxWidthRatio(0.25f),
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureMovementSpeedRatio(0.8f),
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pointerGestureZoomSpeedRatio(0.3f),
24760d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn            showTouches(false) { }
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2497df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba    bool getDisplayInfo(bool external, DisplayViewport* outViewport) const;
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void setDisplayInfo(bool external, const DisplayViewport& viewport);
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DisplayViewport mInternalDisplay;
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DisplayViewport mExternalDisplay;
2557df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba};
2567df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct TouchAffineTransformation {
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float x_scale;
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float x_ymix;
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float x_offset;
2627df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba    float y_xmix;
2637df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba    float y_scale;
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    float y_offset;
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    TouchAffineTransformation() :
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
270de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski
271de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski    void applyTo(float& x, float& y) const;
272de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski};
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Input reader policy interface.
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The input reader policy is used by the input reader to interact with the Window Manager
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * and other system components.
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The actual implementation is partially supported by callbacks into the DVM
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * via JNI.  This interface is also mocked in the unit tests.
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * These methods must NOT re-enter the input reader since they may be called while
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * holding the input reader lock.
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass InputReaderPolicyInterface : public virtual RefBase {
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprotected:
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    InputReaderPolicyInterface() { }
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual ~InputReaderPolicyInterface() { }
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Gets the input reader configuration. */
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Notifies the input reader policy that some input devices have changed
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and provides information about all current input devices.
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Gets the keyboard layout for a particular input device. */
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const InputDeviceIdentifier& identifier) = 0;
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
30960d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Gets the affine calibration associated with the specified device. */
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual TouchAffineTransformation getTouchAffineTransformation(
313896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat            const String8& inputDeviceDescriptor) = 0;
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
315c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
316c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
317c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn/* Processes raw input events and sends cooked event data to an input listener. */
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass InputReaderInterface : public virtual RefBase {
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprotected:
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    InputReaderInterface() { }
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual ~InputReaderInterface() { }
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Dumps the state of the input reader.
32560d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn     *
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This method may be called on any thread (usually by the input manager). */
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void dump(String8& dump) = 0;
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Called by the heatbeat to ensures that the reader has not deadlocked. */
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void monitor() = 0;
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Runs a single iteration of the processing loop.
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Nominally reads and processes one incoming message from the EventHub.
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This method should be called on the input reader thread.
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void loopOnce() = 0;
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Gets information about all input devices.
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This method may be called on any thread (usually by the input manager).
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Query current input state. */
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int32_t scanCode) = 0;
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int32_t keyCode) = 0;
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int32_t sw) = 0;
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Determine whether physical keys exist for the given framework-domain key codes. */
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Requests that a reconfiguration of all input devices.
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The changes flag is a bitfield that indicates what has changed and whether
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the input devices must all be reopened. */
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void requestRefreshConfiguration(uint32_t changes) = 0;
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Controls the vibrator of a particular input device. */
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ssize_t repeat, int32_t token) = 0;
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* Internal interface used by individual input devices to access global input device state
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * and parameters maintained by the input reader.
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass InputReaderContext {
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    InputReaderContext() { }
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual ~InputReaderContext() { }
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void updateGlobalMetaState() = 0;
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual int32_t getGlobalMetaState() = 0;
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual bool shouldDropVirtualKey(nsecs_t now,
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void fadePointer() = 0;
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void requestTimeoutAtTime(nsecs_t when) = 0;
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual int32_t bumpGeneration() = 0;
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual InputReaderPolicyInterface* getPolicy() = 0;
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual InputListenerInterface* getListener() = 0;
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual EventHubInterface* getEventHub() = 0;
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* The input reader reads raw event data from the event hub and processes it into input events
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * that it sends to the input listener.  Some functions of the input reader, such as early
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * event filtering in low power states, are controlled by a separate policy object.
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The InputReader owns a collection of InputMappers.  Most of the work it does happens
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * on the input reader thread but the InputReader can receive queries from other system
40160d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn * components running on arbitrary threads.  To keep things manageable, the InputReader
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * uses a single Mutex to guard its state.  The Mutex may be held while calling into the
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * EventHub or the InputReaderPolicy but it is never held while calling into the
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * InputListener.
405896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat */
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass InputReader : public InputReaderInterface {
407c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackbornpublic:
408c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    InputReader(const sp<EventHubInterface>& eventHub,
409c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn            const sp<InputReaderPolicyInterface>& policy,
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const sp<InputListenerInterface>& listener);
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual ~InputReader();
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void dump(String8& dump);
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void monitor();
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void loopOnce();
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int32_t scanCode);
42260d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int32_t keyCode);
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int32_t sw);
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void requestRefreshConfiguration(uint32_t changes);
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ssize_t repeat, int32_t token);
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual void cancelVibrate(int32_t deviceId, int32_t token);
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprotected:
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // These members are protected so they can be instrumented by test cases.
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const InputDeviceIdentifier& identifier, uint32_t classes);
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    class ContextImpl : public InputReaderContext {
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        InputReader* mReader;
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public:
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ContextImpl(InputReader* reader);
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual void updateGlobalMetaState();
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual int32_t getGlobalMetaState();
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual void disableVirtualKeysUntil(nsecs_t time);
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual bool shouldDropVirtualKey(nsecs_t now,
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                InputDevice* device, int32_t keyCode, int32_t scanCode);
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual void fadePointer();
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual void requestTimeoutAtTime(nsecs_t when);
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual int32_t bumpGeneration();
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual InputReaderPolicyInterface* getPolicy();
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual InputListenerInterface* getListener();
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        virtual EventHubInterface* getEventHub();
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } mContext;
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    friend class ContextImpl;
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Mutex mLock;
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Condition mReaderIsAliveCondition;
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<EventHubInterface> mEventHub;
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<InputReaderPolicyInterface> mPolicy;
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<QueuedInputListener> mQueuedListener;
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    InputReaderConfiguration mConfig;
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The event queue.
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static const int EVENT_BUFFER_SIZE = 256;
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    KeyedVector<int32_t, InputDevice*> mDevices;
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // low-level input event decoding and device management
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void processEventsLocked(const RawEvent* rawEvents, size_t count);
48160d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void addDeviceLocked(nsecs_t when, int32_t deviceId);
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void removeDeviceLocked(nsecs_t when, int32_t deviceId);
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
485896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    void timeoutExpiredLocked(nsecs_t when);
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
487c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    void handleConfigurationChangedLocked(nsecs_t when);
488c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
489c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    int32_t mGlobalMetaState;
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void updateGlobalMetaStateLocked();
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t getGlobalMetaStateLocked();
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void fadePointerLocked();
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
495c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    int32_t mGeneration;
49660d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    int32_t bumpGenerationLocked();
497c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nsecs_t mDisableVirtualKeysTimeout;
501896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    void disableVirtualKeysUntilLocked(nsecs_t time);
50260d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    bool shouldDropVirtualKeyLocked(nsecs_t now,
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            InputDevice* device, int32_t keyCode, int32_t scanCode);
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nsecs_t mNextTimeout;
506896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    void requestTimeoutAtTimeLocked(nsecs_t when);
507c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
508c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    uint32_t mConfigurationChangesToRefresh;
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void refreshConfigurationLocked(uint32_t changes);
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // state queries
512896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
51360d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            GetStateFunc getStateFunc);
515c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const int32_t* keyCodes, uint8_t* outFlags);
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
520c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn/* Reads raw events from the event hub and processes them, endlessly. */
521c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackbornclass InputReaderThread : public Thread {
522c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackbornpublic:
523c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    InputReaderThread(const sp<InputReaderInterface>& reader);
524c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    virtual ~InputReaderThread();
525c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
526c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackbornprivate:
527c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    sp<InputReaderInterface> mReader;
528c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
529c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    virtual bool threadLoop();
530c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn};
531c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
532c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
533c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn/* Represents the state of a single input device. */
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass InputDevice {
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
5372ba4efff8bece69f95401768e48db1b20afd077dNarayan Kamath            controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
5382ba4efff8bece69f95401768e48db1b20afd077dNarayan Kamath    ~InputDevice();
5392ba4efff8bece69f95401768e48db1b20afd077dNarayan Kamath
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline InputReaderContext* getContext() { return mContext; }
5412d19d202bdf6c16b8e01b73f3a742b2670bff907Narayan Kamath    inline int32_t getId() const { return mId; }
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline int32_t getControllerNumber() const { return mControllerNumber; }
543a0545dd54fdab5b284b509b7121515f15f4d728aAshok Bhat    inline int32_t getGeneration() const { return mGeneration; }
544a0545dd54fdab5b284b509b7121515f15f4d728aAshok Bhat    inline const String8& getName() const { return mIdentifier.name; }
545a0545dd54fdab5b284b509b7121515f15f4d728aAshok Bhat    inline const String8& getDescriptor() { return mIdentifier.descriptor; }
546a0545dd54fdab5b284b509b7121515f15f4d728aAshok Bhat    inline uint32_t getClasses() const { return mClasses; }
547a0545dd54fdab5b284b509b7121515f15f4d728aAshok Bhat    inline uint32_t getSources() const { return mSources; }
548a0545dd54fdab5b284b509b7121515f15f4d728aAshok Bhat
549896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    inline bool isExternal() { return mIsExternal; }
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline void setExternal(bool external) { mIsExternal = external; }
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline bool isIgnored() { return mMappers.isEmpty(); }
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void dump(String8& dump);
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void addMapper(InputMapper* mapper);
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void reset(nsecs_t when);
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void process(const RawEvent* rawEvents, size_t count);
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void timeoutExpired(nsecs_t when);
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
56560d7db4c3e3d60060e7ac021445ea1f510b7a1fbDianne Hackborn    bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const int32_t* keyCodes, uint8_t* outFlags);
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void cancelVibrate(int32_t token);
569c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t getMetaState();
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void fadePointer();
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void bumpGeneration();
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void notifyReset(nsecs_t when);
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline const PropertyMap& getConfiguration() { return mConfiguration; }
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool hasKey(int32_t code) {
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return getEventHub()->hasScanCode(mId, code);
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool hasAbsoluteAxis(int32_t code) {
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        RawAbsoluteAxisInfo info;
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return info.valid;
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool isKeyPressed(int32_t code) {
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t getAbsoluteAxisValue(int32_t code) {
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int32_t value;
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        getEventHub()->getAbsoluteAxisValue(mId, code, &value);
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return value;
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
601896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhatprivate:
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    InputReaderContext* mContext;
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mId;
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mGeneration;
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mControllerNumber;
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    InputDeviceIdentifier mIdentifier;
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 mAlias;
608b2a3dd88a53cc8c6d19f6dc8ec4f3d6c4abd9b54The Android Open Source Project    uint32_t mClasses;
609b2a3dd88a53cc8c6d19f6dc8ec4f3d6c4abd9b54The Android Open Source Project
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Vector<InputMapper*> mMappers;
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
612f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn    uint32_t mSources;
6137df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba    bool mIsExternal;
6147df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba    bool mDropUntilNextSync;
6157df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba
6167df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba    typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
6177df3625d5bb28d11cce9ac23429f5e3c6ebac030Martin Kosiba    int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
618f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn
619f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn    PropertyMap mConfiguration;
620f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn};
6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
62248d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
62348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad/* Keeps track of the state of mouse or touch pad buttons. */
62448d22323ce39f9aab003dce74456889b6414af55Mårten Kongstadclass CursorButtonAccumulator {
62548d22323ce39f9aab003dce74456889b6414af55Mårten Kongstadpublic:
62648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    CursorButtonAccumulator();
62748d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    void reset(InputDevice* device);
62848d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
62948d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    void process(const RawEvent* rawEvent);
63048d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
63148d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    uint32_t getButtonState() const;
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6333a19833750de7f8872ad44892054e409b2d77614Kenny Rootprivate:
6343a19833750de7f8872ad44892054e409b2d77614Kenny Root    bool mBtnLeft;
6353a19833750de7f8872ad44892054e409b2d77614Kenny Root    bool mBtnRight;
6363a19833750de7f8872ad44892054e409b2d77614Kenny Root    bool mBtnMiddle;
6373a19833750de7f8872ad44892054e409b2d77614Kenny Root    bool mBtnBack;
6383a19833750de7f8872ad44892054e409b2d77614Kenny Root    bool mBtnSide;
6393a19833750de7f8872ad44892054e409b2d77614Kenny Root    bool mBtnForward;
6403a19833750de7f8872ad44892054e409b2d77614Kenny Root    bool mBtnExtra;
6413a19833750de7f8872ad44892054e409b2d77614Kenny Root    bool mBtnTask;
6423a19833750de7f8872ad44892054e409b2d77614Kenny Root
6433a19833750de7f8872ad44892054e409b2d77614Kenny Root    void clearButtons();
6443a19833750de7f8872ad44892054e409b2d77614Kenny Root};
6453a19833750de7f8872ad44892054e409b2d77614Kenny Root
6463a19833750de7f8872ad44892054e409b2d77614Kenny Root
6473a19833750de7f8872ad44892054e409b2d77614Kenny Root/* Keeps track of cursor movements. */
6483a19833750de7f8872ad44892054e409b2d77614Kenny Root
6493a19833750de7f8872ad44892054e409b2d77614Kenny Rootclass CursorMotionAccumulator {
6503a19833750de7f8872ad44892054e409b2d77614Kenny Rootpublic:
6513a19833750de7f8872ad44892054e409b2d77614Kenny Root    CursorMotionAccumulator();
6523a19833750de7f8872ad44892054e409b2d77614Kenny Root    void reset(InputDevice* device);
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void process(const RawEvent* rawEvent);
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void finishSync();
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline int32_t getRelativeX() const { return mRelX; }
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline int32_t getRelativeY() const { return mRelY; }
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mRelX;
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mRelY;
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void clearRelativeAxes();
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* Keeps track of cursor scrolling motions. */
66911bce123b91cee70380bffa3fffa3c9d19ef3ebdNarayan Kamath
67011bce123b91cee70380bffa3fffa3c9d19ef3ebdNarayan Kamathclass CursorScrollAccumulator {
67111bce123b91cee70380bffa3fffa3c9d19ef3ebdNarayan Kamathpublic:
67211bce123b91cee70380bffa3fffa3c9d19ef3ebdNarayan Kamath    CursorScrollAccumulator();
67311bce123b91cee70380bffa3fffa3c9d19ef3ebdNarayan Kamath    void configure(InputDevice* device);
67411bce123b91cee70380bffa3fffa3c9d19ef3ebdNarayan Kamath    void reset(InputDevice* device);
67511bce123b91cee70380bffa3fffa3c9d19ef3ebdNarayan Kamath
67611bce123b91cee70380bffa3fffa3c9d19ef3ebdNarayan Kamath    void process(const RawEvent* rawEvent);
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void finishSync();
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline int32_t getRelativeX() const { return mRelX; }
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline int32_t getRelativeY() const { return mRelY; }
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline int32_t getRelativeVWheel() const { return mRelWheel; }
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline int32_t getRelativeHWheel() const { return mRelHWheel; }
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
68869cb87576ba163b61bb0e6477a3b7c57a9b11d40Dianne Hackborn    bool mHaveRelWheel;
68969cb87576ba163b61bb0e6477a3b7c57a9b11d40Dianne Hackborn    bool mHaveRelHWheel;
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mRelX;
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mRelY;
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mRelWheel;
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int32_t mRelHWheel;
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void clearRelativeAxes();
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* Keeps track of the state of touch, stylus and tool buttons. */
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass TouchButtonAccumulator {
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
703896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    TouchButtonAccumulator();
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void configure(InputDevice* device);
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void reset(InputDevice* device);
706896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void process(const RawEvent* rawEvent);
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint32_t getButtonState() const;
710896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    int32_t getToolType() const;
711896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    bool isToolActive() const;
712896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    bool isHovering() const;
713896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    bool hasStylus() const;
714896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat
715896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhatprivate:
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mHaveBtnTouch;
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mHaveStylus;
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
71955fc850cf992cdcb0993cb109d2f716613c0dbddKenny Root    bool mBtnTouch;
72055fc850cf992cdcb0993cb109d2f716613c0dbddKenny Root    bool mBtnStylus;
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnStylus2;
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnToolFinger;
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnToolPen;
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnToolRubber;
7250d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn    bool mBtnToolBrush;
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnToolPencil;
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnToolAirbrush;
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnToolMouse;
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnToolLens;
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool mBtnToolDoubleTap;
7310d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn    bool mBtnToolTripleTap;
732896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    bool mBtnToolQuadTap;
733896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void clearButtons();
73552b999f0721b53e9c6e18a4bd664e89aeb65b2d5Alan Viverette};
73652b999f0721b53e9c6e18a4bd664e89aeb65b2d5Alan Viverette
73752b999f0721b53e9c6e18a4bd664e89aeb65b2d5Alan Viverette
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* Raw axis information from the driver. */
739896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhatstruct RawPointerAxes {
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo x;
7419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo y;
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo pressure;
743896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    RawAbsoluteAxisInfo touchMajor;
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo touchMinor;
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo toolMajor;
7469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo toolMinor;
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo orientation;
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo distance;
7499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo tiltX;
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo tiltY;
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo trackingId;
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawAbsoluteAxisInfo slot;
753de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski
754de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski    RawPointerAxes();
755de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski    void clear();
756de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski};
757de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski
7589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* Raw data for a collection of pointers including a pointer id mapping table. */
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct RawPointerData {
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct Pointer {
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        uint32_t id;
76382e1ee93eece8fb0aec6acc3ef4ee7b1c86feec7Dianne Hackborn        int32_t x;
76482e1ee93eece8fb0aec6acc3ef4ee7b1c86feec7Dianne Hackborn        int32_t y;
76582e1ee93eece8fb0aec6acc3ef4ee7b1c86feec7Dianne Hackborn        int32_t pressure;
76682e1ee93eece8fb0aec6acc3ef4ee7b1c86feec7Dianne Hackborn        int32_t touchMajor;
76782e1ee93eece8fb0aec6acc3ef4ee7b1c86feec7Dianne Hackborn        int32_t touchMinor;
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int32_t toolMajor;
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int32_t toolMinor;
770896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat        int32_t orientation;
771896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat        int32_t distance;
772896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat        int32_t tiltX;
773896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat        int32_t tiltY;
774896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat        int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bool isHovering;
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
777896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint32_t pointerCount;
779896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    Pointer pointers[MAX_POINTERS];
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    BitSet32 hoveringIdBits, touchingIdBits;
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint32_t idToIndex[MAX_POINTER_ID + 1];
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    RawPointerData();
784042ad633bc68bdda2bb0c50216706d73575a5992Jon Miranda    void clear();
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void copyFrom(const RawPointerData& other);
78648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    void getCentroidOfTouchingPointers(float* outX, float* outY) const;
7879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline void markIdBit(uint32_t id, bool isHovering) {
789896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat        if (isHovering) {
790c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn            hoveringIdBits.markBit(id);
791c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        } else {
792896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat            touchingIdBits.markBit(id);
793c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        }
794896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    }
795896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat
796896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    inline void clearIdBits() {
797c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        hoveringIdBits.clear();
798c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        touchingIdBits.clear();
799c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    }
800c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
801896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    inline const Pointer& pointerForId(uint32_t id) const {
802c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn        return pointers[idToIndex[id]];
803c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn    }
804c3b91fd26a940f8cee54888f91b490cb1768b03cDianne Hackborn
8059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    inline bool isHovering(uint32_t pointerIndex) {
8069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return pointers[pointerIndex].isHovering;
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* Cooked data for a collection of pointers including a pointer id mapping table. */
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct CookedPointerData {
813    uint32_t pointerCount;
814    PointerProperties pointerProperties[MAX_POINTERS];
815    PointerCoords pointerCoords[MAX_POINTERS];
816    BitSet32 hoveringIdBits, touchingIdBits;
817    uint32_t idToIndex[MAX_POINTER_ID + 1];
818
819    CookedPointerData();
820    void clear();
821    void copyFrom(const CookedPointerData& other);
822
823    inline const PointerCoords& pointerCoordsForId(uint32_t id) const {
824        return pointerCoords[idToIndex[id]];
825    }
826
827    inline bool isHovering(uint32_t pointerIndex) {
828        return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id);
829    }
830};
831
832
833/* Keeps track of the state of single-touch protocol. */
834class SingleTouchMotionAccumulator {
835public:
836    SingleTouchMotionAccumulator();
837
838    void process(const RawEvent* rawEvent);
839    void reset(InputDevice* device);
840
841    inline int32_t getAbsoluteX() const { return mAbsX; }
842    inline int32_t getAbsoluteY() const { return mAbsY; }
843    inline int32_t getAbsolutePressure() const { return mAbsPressure; }
844    inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; }
845    inline int32_t getAbsoluteDistance() const { return mAbsDistance; }
846    inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; }
847    inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; }
848
849private:
850    int32_t mAbsX;
851    int32_t mAbsY;
852    int32_t mAbsPressure;
853    int32_t mAbsToolWidth;
854    int32_t mAbsDistance;
855    int32_t mAbsTiltX;
856    int32_t mAbsTiltY;
857
858    void clearAbsoluteAxes();
859};
860
861
862/* Keeps track of the state of multi-touch protocol. */
863class MultiTouchMotionAccumulator {
864public:
865    class Slot {
866    public:
867        inline bool isInUse() const { return mInUse; }
868        inline int32_t getX() const { return mAbsMTPositionX; }
869        inline int32_t getY() const { return mAbsMTPositionY; }
870        inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
871        inline int32_t getTouchMinor() const {
872            return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; }
873        inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
874        inline int32_t getToolMinor() const {
875            return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; }
876        inline int32_t getOrientation() const { return mAbsMTOrientation; }
877        inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
878        inline int32_t getPressure() const { return mAbsMTPressure; }
879        inline int32_t getDistance() const { return mAbsMTDistance; }
880        inline int32_t getToolType() const;
881
882    private:
883        friend class MultiTouchMotionAccumulator;
884
885        bool mInUse;
886        bool mHaveAbsMTTouchMinor;
887        bool mHaveAbsMTWidthMinor;
888        bool mHaveAbsMTToolType;
889
890        int32_t mAbsMTPositionX;
891        int32_t mAbsMTPositionY;
892        int32_t mAbsMTTouchMajor;
893        int32_t mAbsMTTouchMinor;
894        int32_t mAbsMTWidthMajor;
895        int32_t mAbsMTWidthMinor;
896        int32_t mAbsMTOrientation;
897        int32_t mAbsMTTrackingId;
898        int32_t mAbsMTPressure;
899        int32_t mAbsMTDistance;
900        int32_t mAbsMTToolType;
901
902        Slot();
903        void clear();
904    };
905
906    MultiTouchMotionAccumulator();
907    ~MultiTouchMotionAccumulator();
908
909    void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
910    void reset(InputDevice* device);
911    void process(const RawEvent* rawEvent);
912    void finishSync();
913    bool hasStylus() const;
914
915    inline size_t getSlotCount() const { return mSlotCount; }
916    inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
917
918private:
919    int32_t mCurrentSlot;
920    Slot* mSlots;
921    size_t mSlotCount;
922    bool mUsingSlotsProtocol;
923    bool mHaveStylus;
924
925    void clearSlots(int32_t initialSlot);
926};
927
928
929/* An input mapper transforms raw input events into cooked event data.
930 * A single input device can have multiple associated input mappers in order to interpret
931 * different classes of events.
932 *
933 * InputMapper lifecycle:
934 * - create
935 * - configure with 0 changes
936 * - reset
937 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
938 * - reset
939 * - destroy
940 */
941class InputMapper {
942public:
943    InputMapper(InputDevice* device);
944    virtual ~InputMapper();
945
946    inline InputDevice* getDevice() { return mDevice; }
947    inline int32_t getDeviceId() { return mDevice->getId(); }
948    inline const String8 getDeviceName() { return mDevice->getName(); }
949    inline InputReaderContext* getContext() { return mContext; }
950    inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
951    inline InputListenerInterface* getListener() { return mContext->getListener(); }
952    inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
953
954    virtual uint32_t getSources() = 0;
955    virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
956    virtual void dump(String8& dump);
957    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
958    virtual void reset(nsecs_t when);
959    virtual void process(const RawEvent* rawEvent) = 0;
960    virtual void timeoutExpired(nsecs_t when);
961
962    virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
963    virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
964    virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
965    virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
966            const int32_t* keyCodes, uint8_t* outFlags);
967    virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
968            int32_t token);
969    virtual void cancelVibrate(int32_t token);
970
971    virtual int32_t getMetaState();
972
973    virtual void fadePointer();
974
975protected:
976    InputDevice* mDevice;
977    InputReaderContext* mContext;
978
979    status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
980    void bumpGeneration();
981
982    static void dumpRawAbsoluteAxisInfo(String8& dump,
983            const RawAbsoluteAxisInfo& axis, const char* name);
984};
985
986
987class SwitchInputMapper : public InputMapper {
988public:
989    SwitchInputMapper(InputDevice* device);
990    virtual ~SwitchInputMapper();
991
992    virtual uint32_t getSources();
993    virtual void process(const RawEvent* rawEvent);
994
995    virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
996
997private:
998    uint32_t mUpdatedSwitchValues;
999    uint32_t mUpdatedSwitchMask;
1000
1001    void processSwitch(int32_t switchCode, int32_t switchValue);
1002    void sync(nsecs_t when);
1003};
1004
1005
1006class VibratorInputMapper : public InputMapper {
1007public:
1008    VibratorInputMapper(InputDevice* device);
1009    virtual ~VibratorInputMapper();
1010
1011    virtual uint32_t getSources();
1012    virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1013    virtual void process(const RawEvent* rawEvent);
1014
1015    virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1016            int32_t token);
1017    virtual void cancelVibrate(int32_t token);
1018    virtual void timeoutExpired(nsecs_t when);
1019    virtual void dump(String8& dump);
1020
1021private:
1022    bool mVibrating;
1023    nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE];
1024    size_t mPatternSize;
1025    ssize_t mRepeat;
1026    int32_t mToken;
1027    ssize_t mIndex;
1028    nsecs_t mNextStepTime;
1029
1030    void nextStep();
1031    void stopVibrating();
1032};
1033
1034
1035class KeyboardInputMapper : public InputMapper {
1036public:
1037    KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType);
1038    virtual ~KeyboardInputMapper();
1039
1040    virtual uint32_t getSources();
1041    virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1042    virtual void dump(String8& dump);
1043    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1044    virtual void reset(nsecs_t when);
1045    virtual void process(const RawEvent* rawEvent);
1046
1047    virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1048    virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1049    virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1050            const int32_t* keyCodes, uint8_t* outFlags);
1051
1052    virtual int32_t getMetaState();
1053
1054private:
1055    struct KeyDown {
1056        int32_t keyCode;
1057        int32_t scanCode;
1058    };
1059
1060    uint32_t mSource;
1061    int32_t mKeyboardType;
1062
1063    int32_t mOrientation; // orientation for dpad keys
1064
1065    Vector<KeyDown> mKeyDowns; // keys that are down
1066    int32_t mMetaState;
1067    nsecs_t mDownTime; // time of most recent key down
1068
1069    int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
1070
1071    struct LedState {
1072        bool avail; // led is available
1073        bool on;    // we think the led is currently on
1074    };
1075    LedState mCapsLockLedState;
1076    LedState mNumLockLedState;
1077    LedState mScrollLockLedState;
1078
1079    // Immutable configuration parameters.
1080    struct Parameters {
1081        bool hasAssociatedDisplay;
1082        bool orientationAware;
1083    } mParameters;
1084
1085    void configureParameters();
1086    void dumpParameters(String8& dump);
1087
1088    bool isKeyboardOrGamepadKey(int32_t scanCode);
1089
1090    void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
1091            uint32_t policyFlags);
1092
1093    ssize_t findKeyDown(int32_t scanCode);
1094
1095    void resetLedState();
1096    void initializeLedState(LedState& ledState, int32_t led);
1097    void updateLedState(bool reset);
1098    void updateLedStateForModifier(LedState& ledState, int32_t led,
1099            int32_t modifier, bool reset);
1100};
1101
1102
1103class CursorInputMapper : public InputMapper {
1104public:
1105    CursorInputMapper(InputDevice* device);
1106    virtual ~CursorInputMapper();
1107
1108    virtual uint32_t getSources();
1109    virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1110    virtual void dump(String8& dump);
1111    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1112    virtual void reset(nsecs_t when);
1113    virtual void process(const RawEvent* rawEvent);
1114
1115    virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1116
1117    virtual void fadePointer();
1118
1119private:
1120    // Amount that trackball needs to move in order to generate a key event.
1121    static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
1122
1123    // Immutable configuration parameters.
1124    struct Parameters {
1125        enum Mode {
1126            MODE_POINTER,
1127            MODE_NAVIGATION,
1128        };
1129
1130        Mode mode;
1131        bool hasAssociatedDisplay;
1132        bool orientationAware;
1133    } mParameters;
1134
1135    CursorButtonAccumulator mCursorButtonAccumulator;
1136    CursorMotionAccumulator mCursorMotionAccumulator;
1137    CursorScrollAccumulator mCursorScrollAccumulator;
1138
1139    int32_t mSource;
1140    float mXScale;
1141    float mYScale;
1142    float mXPrecision;
1143    float mYPrecision;
1144
1145    float mVWheelScale;
1146    float mHWheelScale;
1147
1148    // Velocity controls for mouse pointer and wheel movements.
1149    // The controls for X and Y wheel movements are separate to keep them decoupled.
1150    VelocityControl mPointerVelocityControl;
1151    VelocityControl mWheelXVelocityControl;
1152    VelocityControl mWheelYVelocityControl;
1153
1154    int32_t mOrientation;
1155
1156    sp<PointerControllerInterface> mPointerController;
1157
1158    int32_t mButtonState;
1159    nsecs_t mDownTime;
1160
1161    void configureParameters();
1162    void dumpParameters(String8& dump);
1163
1164    void sync(nsecs_t when);
1165};
1166
1167
1168class TouchInputMapper : public InputMapper {
1169public:
1170    TouchInputMapper(InputDevice* device);
1171    virtual ~TouchInputMapper();
1172
1173    virtual uint32_t getSources();
1174    virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1175    virtual void dump(String8& dump);
1176    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1177    virtual void reset(nsecs_t when);
1178    virtual void process(const RawEvent* rawEvent);
1179
1180    virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
1181    virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
1182    virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1183            const int32_t* keyCodes, uint8_t* outFlags);
1184
1185    virtual void fadePointer();
1186    virtual void timeoutExpired(nsecs_t when);
1187
1188protected:
1189    CursorButtonAccumulator mCursorButtonAccumulator;
1190    CursorScrollAccumulator mCursorScrollAccumulator;
1191    TouchButtonAccumulator mTouchButtonAccumulator;
1192
1193    struct VirtualKey {
1194        int32_t keyCode;
1195        int32_t scanCode;
1196        uint32_t flags;
1197
1198        // computed hit box, specified in touch screen coords based on known display size
1199        int32_t hitLeft;
1200        int32_t hitTop;
1201        int32_t hitRight;
1202        int32_t hitBottom;
1203
1204        inline bool isHit(int32_t x, int32_t y) const {
1205            return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
1206        }
1207    };
1208
1209    // Input sources and device mode.
1210    uint32_t mSource;
1211
1212    enum DeviceMode {
1213        DEVICE_MODE_DISABLED, // input is disabled
1214        DEVICE_MODE_DIRECT, // direct mapping (touchscreen)
1215        DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad)
1216        DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation)
1217        DEVICE_MODE_POINTER, // pointer mapping (pointer)
1218    };
1219    DeviceMode mDeviceMode;
1220
1221    // The reader's configuration.
1222    InputReaderConfiguration mConfig;
1223
1224    // Immutable configuration parameters.
1225    struct Parameters {
1226        enum DeviceType {
1227            DEVICE_TYPE_TOUCH_SCREEN,
1228            DEVICE_TYPE_TOUCH_PAD,
1229            DEVICE_TYPE_TOUCH_NAVIGATION,
1230            DEVICE_TYPE_POINTER,
1231        };
1232
1233        DeviceType deviceType;
1234        bool hasAssociatedDisplay;
1235        bool associatedDisplayIsExternal;
1236        bool orientationAware;
1237        bool hasButtonUnderPad;
1238
1239        enum GestureMode {
1240            GESTURE_MODE_POINTER,
1241            GESTURE_MODE_SPOTS,
1242        };
1243        GestureMode gestureMode;
1244
1245        bool wake;
1246    } mParameters;
1247
1248    // Immutable calibration parameters in parsed form.
1249    struct Calibration {
1250        // Size
1251        enum SizeCalibration {
1252            SIZE_CALIBRATION_DEFAULT,
1253            SIZE_CALIBRATION_NONE,
1254            SIZE_CALIBRATION_GEOMETRIC,
1255            SIZE_CALIBRATION_DIAMETER,
1256            SIZE_CALIBRATION_BOX,
1257            SIZE_CALIBRATION_AREA,
1258        };
1259
1260        SizeCalibration sizeCalibration;
1261
1262        bool haveSizeScale;
1263        float sizeScale;
1264        bool haveSizeBias;
1265        float sizeBias;
1266        bool haveSizeIsSummed;
1267        bool sizeIsSummed;
1268
1269        // Pressure
1270        enum PressureCalibration {
1271            PRESSURE_CALIBRATION_DEFAULT,
1272            PRESSURE_CALIBRATION_NONE,
1273            PRESSURE_CALIBRATION_PHYSICAL,
1274            PRESSURE_CALIBRATION_AMPLITUDE,
1275        };
1276
1277        PressureCalibration pressureCalibration;
1278        bool havePressureScale;
1279        float pressureScale;
1280
1281        // Orientation
1282        enum OrientationCalibration {
1283            ORIENTATION_CALIBRATION_DEFAULT,
1284            ORIENTATION_CALIBRATION_NONE,
1285            ORIENTATION_CALIBRATION_INTERPOLATED,
1286            ORIENTATION_CALIBRATION_VECTOR,
1287        };
1288
1289        OrientationCalibration orientationCalibration;
1290
1291        // Distance
1292        enum DistanceCalibration {
1293            DISTANCE_CALIBRATION_DEFAULT,
1294            DISTANCE_CALIBRATION_NONE,
1295            DISTANCE_CALIBRATION_SCALED,
1296        };
1297
1298        DistanceCalibration distanceCalibration;
1299        bool haveDistanceScale;
1300        float distanceScale;
1301
1302        enum CoverageCalibration {
1303            COVERAGE_CALIBRATION_DEFAULT,
1304            COVERAGE_CALIBRATION_NONE,
1305            COVERAGE_CALIBRATION_BOX,
1306        };
1307
1308        CoverageCalibration coverageCalibration;
1309
1310        inline void applySizeScaleAndBias(float* outSize) const {
1311            if (haveSizeScale) {
1312                *outSize *= sizeScale;
1313            }
1314            if (haveSizeBias) {
1315                *outSize += sizeBias;
1316            }
1317            if (*outSize < 0) {
1318                *outSize = 0;
1319            }
1320        }
1321    } mCalibration;
1322
1323    // Affine location transformation/calibration
1324    struct TouchAffineTransformation mAffineTransform;
1325
1326    // Raw pointer axis information from the driver.
1327    RawPointerAxes mRawPointerAxes;
1328
1329    // Raw pointer sample data.
1330    RawPointerData mCurrentRawPointerData;
1331    RawPointerData mLastRawPointerData;
1332
1333    // Cooked pointer sample data.
1334    CookedPointerData mCurrentCookedPointerData;
1335    CookedPointerData mLastCookedPointerData;
1336
1337    // Button state.
1338    int32_t mCurrentButtonState;
1339    int32_t mLastButtonState;
1340
1341    // Scroll state.
1342    int32_t mCurrentRawVScroll;
1343    int32_t mCurrentRawHScroll;
1344
1345    // Id bits used to differentiate fingers, stylus and mouse tools.
1346    BitSet32 mCurrentFingerIdBits; // finger or unknown
1347    BitSet32 mLastFingerIdBits;
1348    BitSet32 mCurrentStylusIdBits; // stylus or eraser
1349    BitSet32 mLastStylusIdBits;
1350    BitSet32 mCurrentMouseIdBits; // mouse or lens
1351    BitSet32 mLastMouseIdBits;
1352
1353    // True if we sent a HOVER_ENTER event.
1354    bool mSentHoverEnter;
1355
1356    // The time the primary pointer last went down.
1357    nsecs_t mDownTime;
1358
1359    // The pointer controller, or null if the device is not a pointer.
1360    sp<PointerControllerInterface> mPointerController;
1361
1362    Vector<VirtualKey> mVirtualKeys;
1363
1364    virtual void configureParameters();
1365    virtual void dumpParameters(String8& dump);
1366    virtual void configureRawPointerAxes();
1367    virtual void dumpRawPointerAxes(String8& dump);
1368    virtual void configureSurface(nsecs_t when, bool* outResetNeeded);
1369    virtual void dumpSurface(String8& dump);
1370    virtual void configureVirtualKeys();
1371    virtual void dumpVirtualKeys(String8& dump);
1372    virtual void parseCalibration();
1373    virtual void resolveCalibration();
1374    virtual void dumpCalibration(String8& dump);
1375    virtual void dumpAffineTransformation(String8& dump);
1376    virtual bool hasStylus() const = 0;
1377    virtual void updateAffineTransformation();
1378
1379    virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
1380
1381private:
1382    // The current viewport.
1383    // The components of the viewport are specified in the display's rotated orientation.
1384    DisplayViewport mViewport;
1385
1386    // The surface orientation, width and height set by configureSurface().
1387    // The width and height are derived from the viewport but are specified
1388    // in the natural orientation.
1389    // The surface origin specifies how the surface coordinates should be translated
1390    // to align with the logical display coordinate space.
1391    // The orientation may be different from the viewport orientation as it specifies
1392    // the rotation of the surface coordinates required to produce the viewport's
1393    // requested orientation, so it will depend on whether the device is orientation aware.
1394    int32_t mSurfaceWidth;
1395    int32_t mSurfaceHeight;
1396    int32_t mSurfaceLeft;
1397    int32_t mSurfaceTop;
1398    int32_t mSurfaceOrientation;
1399
1400    // Translation and scaling factors, orientation-independent.
1401    float mXTranslate;
1402    float mXScale;
1403    float mXPrecision;
1404
1405    float mYTranslate;
1406    float mYScale;
1407    float mYPrecision;
1408
1409    float mGeometricScale;
1410
1411    float mPressureScale;
1412
1413    float mSizeScale;
1414
1415    float mOrientationScale;
1416
1417    float mDistanceScale;
1418
1419    bool mHaveTilt;
1420    float mTiltXCenter;
1421    float mTiltXScale;
1422    float mTiltYCenter;
1423    float mTiltYScale;
1424
1425    // Oriented motion ranges for input device info.
1426    struct OrientedRanges {
1427        InputDeviceInfo::MotionRange x;
1428        InputDeviceInfo::MotionRange y;
1429        InputDeviceInfo::MotionRange pressure;
1430
1431        bool haveSize;
1432        InputDeviceInfo::MotionRange size;
1433
1434        bool haveTouchSize;
1435        InputDeviceInfo::MotionRange touchMajor;
1436        InputDeviceInfo::MotionRange touchMinor;
1437
1438        bool haveToolSize;
1439        InputDeviceInfo::MotionRange toolMajor;
1440        InputDeviceInfo::MotionRange toolMinor;
1441
1442        bool haveOrientation;
1443        InputDeviceInfo::MotionRange orientation;
1444
1445        bool haveDistance;
1446        InputDeviceInfo::MotionRange distance;
1447
1448        bool haveTilt;
1449        InputDeviceInfo::MotionRange tilt;
1450
1451        OrientedRanges() {
1452            clear();
1453        }
1454
1455        void clear() {
1456            haveSize = false;
1457            haveTouchSize = false;
1458            haveToolSize = false;
1459            haveOrientation = false;
1460            haveDistance = false;
1461            haveTilt = false;
1462        }
1463    } mOrientedRanges;
1464
1465    // Oriented dimensions and precision.
1466    float mOrientedXPrecision;
1467    float mOrientedYPrecision;
1468
1469    struct CurrentVirtualKeyState {
1470        bool down;
1471        bool ignored;
1472        nsecs_t downTime;
1473        int32_t keyCode;
1474        int32_t scanCode;
1475    } mCurrentVirtualKey;
1476
1477    // Scale factor for gesture or mouse based pointer movements.
1478    float mPointerXMovementScale;
1479    float mPointerYMovementScale;
1480
1481    // Scale factor for gesture based zooming and other freeform motions.
1482    float mPointerXZoomScale;
1483    float mPointerYZoomScale;
1484
1485    // The maximum swipe width.
1486    float mPointerGestureMaxSwipeWidth;
1487
1488    struct PointerDistanceHeapElement {
1489        uint32_t currentPointerIndex : 8;
1490        uint32_t lastPointerIndex : 8;
1491        uint64_t distance : 48; // squared distance
1492    };
1493
1494    enum PointerUsage {
1495        POINTER_USAGE_NONE,
1496        POINTER_USAGE_GESTURES,
1497        POINTER_USAGE_STYLUS,
1498        POINTER_USAGE_MOUSE,
1499    };
1500    PointerUsage mPointerUsage;
1501
1502    struct PointerGesture {
1503        enum Mode {
1504            // No fingers, button is not pressed.
1505            // Nothing happening.
1506            NEUTRAL,
1507
1508            // No fingers, button is not pressed.
1509            // Tap detected.
1510            // Emits DOWN and UP events at the pointer location.
1511            TAP,
1512
1513            // Exactly one finger dragging following a tap.
1514            // Pointer follows the active finger.
1515            // Emits DOWN, MOVE and UP events at the pointer location.
1516            //
1517            // Detect double-taps when the finger goes up while in TAP_DRAG mode.
1518            TAP_DRAG,
1519
1520            // Button is pressed.
1521            // Pointer follows the active finger if there is one.  Other fingers are ignored.
1522            // Emits DOWN, MOVE and UP events at the pointer location.
1523            BUTTON_CLICK_OR_DRAG,
1524
1525            // Exactly one finger, button is not pressed.
1526            // Pointer follows the active finger.
1527            // Emits HOVER_MOVE events at the pointer location.
1528            //
1529            // Detect taps when the finger goes up while in HOVER mode.
1530            HOVER,
1531
1532            // Exactly two fingers but neither have moved enough to clearly indicate
1533            // whether a swipe or freeform gesture was intended.  We consider the
1534            // pointer to be pressed so this enables clicking or long-pressing on buttons.
1535            // Pointer does not move.
1536            // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate.
1537            PRESS,
1538
1539            // Exactly two fingers moving in the same direction, button is not pressed.
1540            // Pointer does not move.
1541            // Emits DOWN, MOVE and UP events with a single pointer coordinate that
1542            // follows the midpoint between both fingers.
1543            SWIPE,
1544
1545            // Two or more fingers moving in arbitrary directions, button is not pressed.
1546            // Pointer does not move.
1547            // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow
1548            // each finger individually relative to the initial centroid of the finger.
1549            FREEFORM,
1550
1551            // Waiting for quiet time to end before starting the next gesture.
1552            QUIET,
1553        };
1554
1555        // Time the first finger went down.
1556        nsecs_t firstTouchTime;
1557
1558        // The active pointer id from the raw touch data.
1559        int32_t activeTouchId; // -1 if none
1560
1561        // The active pointer id from the gesture last delivered to the application.
1562        int32_t activeGestureId; // -1 if none
1563
1564        // Pointer coords and ids for the current and previous pointer gesture.
1565        Mode currentGestureMode;
1566        BitSet32 currentGestureIdBits;
1567        uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1];
1568        PointerProperties currentGestureProperties[MAX_POINTERS];
1569        PointerCoords currentGestureCoords[MAX_POINTERS];
1570
1571        Mode lastGestureMode;
1572        BitSet32 lastGestureIdBits;
1573        uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1];
1574        PointerProperties lastGestureProperties[MAX_POINTERS];
1575        PointerCoords lastGestureCoords[MAX_POINTERS];
1576
1577        // Time the pointer gesture last went down.
1578        nsecs_t downTime;
1579
1580        // Time when the pointer went down for a TAP.
1581        nsecs_t tapDownTime;
1582
1583        // Time when the pointer went up for a TAP.
1584        nsecs_t tapUpTime;
1585
1586        // Location of initial tap.
1587        float tapX, tapY;
1588
1589        // Time we started waiting for quiescence.
1590        nsecs_t quietTime;
1591
1592        // Reference points for multitouch gestures.
1593        float referenceTouchX;    // reference touch X/Y coordinates in surface units
1594        float referenceTouchY;
1595        float referenceGestureX;  // reference gesture X/Y coordinates in pixels
1596        float referenceGestureY;
1597
1598        // Distance that each pointer has traveled which has not yet been
1599        // subsumed into the reference gesture position.
1600        BitSet32 referenceIdBits;
1601        struct Delta {
1602            float dx, dy;
1603        };
1604        Delta referenceDeltas[MAX_POINTER_ID + 1];
1605
1606        // Describes how touch ids are mapped to gesture ids for freeform gestures.
1607        uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1];
1608
1609        // A velocity tracker for determining whether to switch active pointers during drags.
1610        VelocityTracker velocityTracker;
1611
1612        void reset() {
1613            firstTouchTime = LLONG_MIN;
1614            activeTouchId = -1;
1615            activeGestureId = -1;
1616            currentGestureMode = NEUTRAL;
1617            currentGestureIdBits.clear();
1618            lastGestureMode = NEUTRAL;
1619            lastGestureIdBits.clear();
1620            downTime = 0;
1621            velocityTracker.clear();
1622            resetTap();
1623            resetQuietTime();
1624        }
1625
1626        void resetTap() {
1627            tapDownTime = LLONG_MIN;
1628            tapUpTime = LLONG_MIN;
1629        }
1630
1631        void resetQuietTime() {
1632            quietTime = LLONG_MIN;
1633        }
1634    } mPointerGesture;
1635
1636    struct PointerSimple {
1637        PointerCoords currentCoords;
1638        PointerProperties currentProperties;
1639        PointerCoords lastCoords;
1640        PointerProperties lastProperties;
1641
1642        // True if the pointer is down.
1643        bool down;
1644
1645        // True if the pointer is hovering.
1646        bool hovering;
1647
1648        // Time the pointer last went down.
1649        nsecs_t downTime;
1650
1651        void reset() {
1652            currentCoords.clear();
1653            currentProperties.clear();
1654            lastCoords.clear();
1655            lastProperties.clear();
1656            down = false;
1657            hovering = false;
1658            downTime = 0;
1659        }
1660    } mPointerSimple;
1661
1662    // The pointer and scroll velocity controls.
1663    VelocityControl mPointerVelocityControl;
1664    VelocityControl mWheelXVelocityControl;
1665    VelocityControl mWheelYVelocityControl;
1666
1667    void sync(nsecs_t when);
1668
1669    bool consumeRawTouches(nsecs_t when, uint32_t policyFlags);
1670    void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
1671            int32_t keyEventAction, int32_t keyEventFlags);
1672
1673    void dispatchTouches(nsecs_t when, uint32_t policyFlags);
1674    void dispatchHoverExit(nsecs_t when, uint32_t policyFlags);
1675    void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags);
1676    void cookPointerData();
1677
1678    void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage);
1679    void abortPointerUsage(nsecs_t when, uint32_t policyFlags);
1680
1681    void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout);
1682    void abortPointerGestures(nsecs_t when, uint32_t policyFlags);
1683    bool preparePointerGestures(nsecs_t when,
1684            bool* outCancelPreviousGesture, bool* outFinishPreviousGesture,
1685            bool isTimeout);
1686
1687    void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags);
1688    void abortPointerStylus(nsecs_t when, uint32_t policyFlags);
1689
1690    void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags);
1691    void abortPointerMouse(nsecs_t when, uint32_t policyFlags);
1692
1693    void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
1694            bool down, bool hovering);
1695    void abortPointerSimple(nsecs_t when, uint32_t policyFlags);
1696
1697    // Dispatches a motion event.
1698    // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the
1699    // method will take care of setting the index and transmuting the action to DOWN or UP
1700    // it is the first / last pointer to go down / up.
1701    void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
1702            int32_t action, int32_t flags, int32_t metaState, int32_t buttonState,
1703            int32_t edgeFlags,
1704            const PointerProperties* properties, const PointerCoords* coords,
1705            const uint32_t* idToIndex, BitSet32 idBits,
1706            int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime);
1707
1708    // Updates pointer coords and properties for pointers with specified ids that have moved.
1709    // Returns true if any of them changed.
1710    bool updateMovedPointers(const PointerProperties* inProperties,
1711            const PointerCoords* inCoords, const uint32_t* inIdToIndex,
1712            PointerProperties* outProperties, PointerCoords* outCoords,
1713            const uint32_t* outIdToIndex, BitSet32 idBits) const;
1714
1715    bool isPointInsideSurface(int32_t x, int32_t y);
1716    const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y);
1717
1718    void assignPointerIds();
1719};
1720
1721
1722class SingleTouchInputMapper : public TouchInputMapper {
1723public:
1724    SingleTouchInputMapper(InputDevice* device);
1725    virtual ~SingleTouchInputMapper();
1726
1727    virtual void reset(nsecs_t when);
1728    virtual void process(const RawEvent* rawEvent);
1729
1730protected:
1731    virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
1732    virtual void configureRawPointerAxes();
1733    virtual bool hasStylus() const;
1734
1735private:
1736    SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
1737};
1738
1739
1740class MultiTouchInputMapper : public TouchInputMapper {
1741public:
1742    MultiTouchInputMapper(InputDevice* device);
1743    virtual ~MultiTouchInputMapper();
1744
1745    virtual void reset(nsecs_t when);
1746    virtual void process(const RawEvent* rawEvent);
1747
1748protected:
1749    virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
1750    virtual void configureRawPointerAxes();
1751    virtual bool hasStylus() const;
1752
1753private:
1754    MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
1755
1756    // Specifies the pointer id bits that are in use, and their associated tracking id.
1757    BitSet32 mPointerIdBits;
1758    int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
1759};
1760
1761
1762class JoystickInputMapper : public InputMapper {
1763public:
1764    JoystickInputMapper(InputDevice* device);
1765    virtual ~JoystickInputMapper();
1766
1767    virtual uint32_t getSources();
1768    virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1769    virtual void dump(String8& dump);
1770    virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
1771    virtual void reset(nsecs_t when);
1772    virtual void process(const RawEvent* rawEvent);
1773
1774private:
1775    struct Axis {
1776        RawAbsoluteAxisInfo rawAxisInfo;
1777        AxisInfo axisInfo;
1778
1779        bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
1780
1781        float scale;   // scale factor from raw to normalized values
1782        float offset;  // offset to add after scaling for normalization
1783        float highScale;  // scale factor from raw to normalized values of high split
1784        float highOffset; // offset to add after scaling for normalization of high split
1785
1786        float min;        // normalized inclusive minimum
1787        float max;        // normalized inclusive maximum
1788        float flat;       // normalized flat region size
1789        float fuzz;       // normalized error tolerance
1790        float resolution; // normalized resolution in units/mm
1791
1792        float filter;  // filter out small variations of this size
1793        float currentValue; // current value
1794        float newValue; // most recent value
1795        float highCurrentValue; // current value of high split
1796        float highNewValue; // most recent value of high split
1797
1798        void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
1799                bool explicitlyMapped, float scale, float offset,
1800                float highScale, float highOffset,
1801                float min, float max, float flat, float fuzz, float resolution) {
1802            this->rawAxisInfo = rawAxisInfo;
1803            this->axisInfo = axisInfo;
1804            this->explicitlyMapped = explicitlyMapped;
1805            this->scale = scale;
1806            this->offset = offset;
1807            this->highScale = highScale;
1808            this->highOffset = highOffset;
1809            this->min = min;
1810            this->max = max;
1811            this->flat = flat;
1812            this->fuzz = fuzz;
1813            this->resolution = resolution;
1814            this->filter = 0;
1815            resetValue();
1816        }
1817
1818        void resetValue() {
1819            this->currentValue = 0;
1820            this->newValue = 0;
1821            this->highCurrentValue = 0;
1822            this->highNewValue = 0;
1823        }
1824    };
1825
1826    // Axes indexed by raw ABS_* axis index.
1827    KeyedVector<int32_t, Axis> mAxes;
1828
1829    void sync(nsecs_t when, bool force);
1830
1831    bool haveAxis(int32_t axisId);
1832    void pruneAxes(bool ignoreExplicitlyMappedAxes);
1833    bool filterAxes(bool force);
1834
1835    static bool hasValueChangedSignificantly(float filter,
1836            float newValue, float currentValue, float min, float max);
1837    static bool hasMovedNearerToValueWithinFilteredRange(float filter,
1838            float newValue, float currentValue, float thresholdValue);
1839
1840    static bool isCenteredAxis(int32_t axis);
1841    static int32_t getCompatAxis(int32_t axis);
1842
1843    static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
1844    static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
1845            float value);
1846};
1847
1848} // namespace android
1849
1850#endif // _UI_INPUT_READER_H
1851