| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 17 | #ifndef _UI_INPUTREADER_INPUT_READER_H | 
|  | 18 | #define _UI_INPUTREADER_INPUT_READER_H | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 19 |  | 
|  | 20 | #include "EventHub.h" | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | #include "InputListener.h" | 
| Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 22 | #include "InputReaderBase.h" | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 23 | #include "InputReaderContext.h" | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 24 | #include "InputThread.h" | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 25 |  | 
| Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 26 | #include <PointerControllerInterface.h> | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 27 | #include <utils/Condition.h> | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 28 | #include <utils/Mutex.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 29 |  | 
| Nathaniel R. Lewis | 10793a6 | 2019-11-05 02:17:02 +0000 | [diff] [blame] | 30 | #include <unordered_map> | 
| Siarhei Vishniakou | d634392 | 2018-07-06 23:33:37 +0100 | [diff] [blame] | 31 | #include <vector> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 32 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 | namespace android { | 
|  | 34 |  | 
|  | 35 | class InputDevice; | 
|  | 36 | class InputMapper; | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 37 | struct StylusState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 38 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 | /* The input reader reads raw event data from the event hub and processes it into input events | 
|  | 40 | * that it sends to the input listener.  Some functions of the input reader, such as early | 
|  | 41 | * event filtering in low power states, are controlled by a separate policy object. | 
|  | 42 | * | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 43 | * The InputReader owns a collection of InputMappers. InputReader starts its own thread, where | 
|  | 44 | * most of the work happens, but the InputReader can receive queries from other system | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 45 | * components running on arbitrary threads.  To keep things manageable, the InputReader | 
|  | 46 | * uses a single Mutex to guard its state.  The Mutex may be held while calling into the | 
|  | 47 | * EventHub or the InputReaderPolicy but it is never held while calling into the | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 48 | * InputListener. All calls to InputListener must happen from InputReader's thread. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | */ | 
|  | 50 | class InputReader : public InputReaderInterface { | 
|  | 51 | public: | 
| Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 52 | InputReader(std::shared_ptr<EventHubInterface> eventHub, | 
|  | 53 | const sp<InputReaderPolicyInterface>& policy, | 
|  | 54 | const sp<InputListenerInterface>& listener); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 55 | virtual ~InputReader(); | 
|  | 56 |  | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 57 | virtual void dump(std::string& dump) override; | 
|  | 58 | virtual void monitor() override; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 59 |  | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 60 | virtual status_t start() override; | 
|  | 61 | virtual status_t stop() override; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 62 |  | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 63 | virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) override; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 |  | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 65 | virtual bool isInputDeviceEnabled(int32_t deviceId) override; | 
| Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 66 |  | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 67 | virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, | 
|  | 68 | int32_t scanCode) override; | 
|  | 69 | virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, | 
|  | 70 | int32_t keyCode) override; | 
|  | 71 | virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) override; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 72 |  | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 73 | virtual void toggleCapsLockState(int32_t deviceId) override; | 
| Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 74 |  | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 75 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes, | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 76 | const int32_t* keyCodes, uint8_t* outFlags) override; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 |  | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 78 | virtual void requestRefreshConfiguration(uint32_t changes) override; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize, | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 81 | ssize_t repeat, int32_t token) override; | 
|  | 82 | virtual void cancelVibrate(int32_t deviceId, int32_t token) override; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 |  | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 84 | virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) override; | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 85 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 86 | protected: | 
|  | 87 | // These members are protected so they can be instrumented by test cases. | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 88 | virtual std::shared_ptr<InputDevice> createDeviceLocked( | 
|  | 89 | int32_t deviceId, const InputDeviceIdentifier& identifier); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 90 |  | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 91 | // With each iteration of the loop, InputReader reads and processes one incoming message from | 
|  | 92 | // the EventHub. | 
|  | 93 | void loopOnce(); | 
|  | 94 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | class ContextImpl : public InputReaderContext { | 
|  | 96 | InputReader* mReader; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 97 | IdGenerator mIdGenerator; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 |  | 
|  | 99 | public: | 
| Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 100 | explicit ContextImpl(InputReader* reader); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 |  | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 102 | virtual void updateGlobalMetaState() override; | 
|  | 103 | virtual int32_t getGlobalMetaState() override; | 
|  | 104 | virtual void disableVirtualKeysUntil(nsecs_t time) override; | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 105 | virtual bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override; | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 106 | virtual void fadePointer() override; | 
| Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 107 | virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) override; | 
| Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 108 | virtual void requestTimeoutAtTime(nsecs_t when) override; | 
|  | 109 | virtual int32_t bumpGeneration() override; | 
|  | 110 | virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override; | 
|  | 111 | virtual void dispatchExternalStylusState(const StylusState& outState) override; | 
|  | 112 | virtual InputReaderPolicyInterface* getPolicy() override; | 
|  | 113 | virtual InputListenerInterface* getListener() override; | 
|  | 114 | virtual EventHubInterface* getEventHub() override; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 115 | virtual int32_t getNextId() override; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 116 | } mContext; | 
|  | 117 |  | 
|  | 118 | friend class ContextImpl; | 
|  | 119 |  | 
|  | 120 | private: | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 121 | std::unique_ptr<InputThread> mThread; | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 122 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 123 | Mutex mLock; | 
|  | 124 |  | 
|  | 125 | Condition mReaderIsAliveCondition; | 
|  | 126 |  | 
| Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 127 | // This could be unique_ptr, but due to the way InputReader tests are written, | 
|  | 128 | // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test | 
|  | 129 | // in parallel to passing it to the InputReader. | 
|  | 130 | std::shared_ptr<EventHubInterface> mEventHub; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | sp<InputReaderPolicyInterface> mPolicy; | 
|  | 132 | sp<QueuedInputListener> mQueuedListener; | 
|  | 133 |  | 
|  | 134 | InputReaderConfiguration mConfig; | 
|  | 135 |  | 
|  | 136 | // The event queue. | 
|  | 137 | static const int EVENT_BUFFER_SIZE = 256; | 
|  | 138 | RawEvent mEventBuffer[EVENT_BUFFER_SIZE]; | 
|  | 139 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 140 | // An input device can represent a collection of EventHub devices. This map provides a way | 
|  | 141 | // to lookup the input device instance from the EventHub device id. | 
|  | 142 | std::unordered_map<int32_t /*eventHubId*/, std::shared_ptr<InputDevice>> mDevices; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 143 |  | 
|  | 144 | // low-level input event decoding and device management | 
|  | 145 | void processEventsLocked(const RawEvent* rawEvents, size_t count); | 
|  | 146 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 147 | void addDeviceLocked(nsecs_t when, int32_t eventHubId); | 
|  | 148 | void removeDeviceLocked(nsecs_t when, int32_t eventHubId); | 
|  | 149 | void processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents, size_t count); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | void timeoutExpiredLocked(nsecs_t when); | 
|  | 151 |  | 
|  | 152 | void handleConfigurationChangedLocked(nsecs_t when); | 
|  | 153 |  | 
|  | 154 | int32_t mGlobalMetaState; | 
|  | 155 | void updateGlobalMetaStateLocked(); | 
|  | 156 | int32_t getGlobalMetaStateLocked(); | 
|  | 157 |  | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 158 | void notifyExternalStylusPresenceChanged(); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 159 | void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices); | 
| Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 160 | void dispatchExternalStylusState(const StylusState& state); | 
|  | 161 |  | 
| Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 162 | // The PointerController that is shared among all the input devices that need it. | 
|  | 163 | wp<PointerControllerInterface> mPointerController; | 
|  | 164 | sp<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId); | 
|  | 165 | void updatePointerDisplayLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | void fadePointerLocked(); | 
|  | 167 |  | 
|  | 168 | int32_t mGeneration; | 
|  | 169 | int32_t bumpGenerationLocked(); | 
|  | 170 |  | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 171 | int32_t mNextInputDeviceId; | 
|  | 172 | int32_t nextInputDeviceIdLocked(); | 
|  | 173 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 174 | void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 |  | 
|  | 176 | nsecs_t mDisableVirtualKeysTimeout; | 
|  | 177 | void disableVirtualKeysUntilLocked(nsecs_t time); | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 178 | bool shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 179 |  | 
|  | 180 | nsecs_t mNextTimeout; | 
|  | 181 | void requestTimeoutAtTimeLocked(nsecs_t when); | 
|  | 182 |  | 
|  | 183 | uint32_t mConfigurationChangesToRefresh; | 
|  | 184 | void refreshConfigurationLocked(uint32_t changes); | 
|  | 185 |  | 
|  | 186 | // state queries | 
|  | 187 | typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code); | 
|  | 188 | int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code, | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 189 | GetStateFunc getStateFunc); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 190 | bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes, | 
| Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 191 | const int32_t* keyCodes, uint8_t* outFlags); | 
| Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 192 |  | 
|  | 193 | // find an InputDevice from an InputDevice id | 
|  | 194 | InputDevice* findInputDevice(int32_t deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 195 | }; | 
|  | 196 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 197 | } // namespace android | 
|  | 198 |  | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 199 | #endif // _UI_INPUTREADER_INPUT_READER_H |