Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [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 | c08b0db | 2022-09-10 00:57:15 +0000 | [diff] [blame] | 17 | #pragma once |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 18 | |
Robert Carr | 2c358bf | 2018-08-08 15:58:15 -0700 | [diff] [blame] | 19 | #pragma GCC system_header |
| 20 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Native input transport. |
| 23 | * |
| 24 | * The InputChannel provides a mechanism for exchanging InputMessage structures across processes. |
| 25 | * |
| 26 | * The InputPublisher and InputConsumer each handle one end-point of an input channel. |
| 27 | * The InputPublisher is used by the input dispatcher to send events to the application. |
| 28 | * The InputConsumer is used by the application to receive events from the input dispatcher. |
| 29 | */ |
| 30 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 31 | #include <string> |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 32 | #include <unordered_map> |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 33 | |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 34 | #include <android-base/chrono_utils.h> |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 35 | #include <android-base/result.h> |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 36 | #include <android-base/unique_fd.h> |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 37 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 38 | #include <android/os/InputChannelCore.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 39 | #include <binder/IBinder.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 40 | #include <input/Input.h> |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 41 | #include <input/InputVerifier.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 42 | #include <sys/stat.h> |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 43 | #include <ui/Transform.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 44 | #include <utils/BitSet.h> |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 45 | #include <utils/Errors.h> |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 46 | #include <utils/Timers.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 47 | |
| 48 | namespace android { |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 49 | class Parcel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Intermediate representation used to send input events and related signals. |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 53 | * |
| 54 | * Note that this structure is used for IPCs so its layout must be identical |
| 55 | * on 64 and 32 bit processes. This is tested in StructLayout_test.cpp. |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 56 | * |
| 57 | * Since the struct must be aligned to an 8-byte boundary, there could be uninitialized bytes |
| 58 | * in-between the defined fields. This padding data should be explicitly accounted for by adding |
| 59 | * "empty" fields into the struct. This data is memset to zero before sending the struct across |
| 60 | * the socket. Adding the explicit fields ensures that the memset is not optimized away by the |
| 61 | * compiler. When a new field is added to the struct, the corresponding change |
| 62 | * in StructLayout_test should be made. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 63 | */ |
| 64 | struct InputMessage { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 65 | enum class Type : uint32_t { |
| 66 | KEY, |
| 67 | MOTION, |
| 68 | FINISHED, |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 69 | FOCUS, |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 70 | CAPTURE, |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 71 | DRAG, |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 72 | TIMELINE, |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 73 | TOUCH_MODE, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 74 | |
| 75 | ftl_last = TOUCH_MODE |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct Header { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 79 | Type type; // 4 bytes |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 80 | uint32_t seq; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 81 | } header; |
| 82 | |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 83 | // For keys and motions, rely on the fact that std::array takes up exactly as much space |
| 84 | // as the underlying data. This is not guaranteed by C++, but it simplifies the conversions. |
| 85 | static_assert(sizeof(std::array<uint8_t, 32>) == 32); |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 86 | |
| 87 | // For bool values, rely on the fact that they take up exactly one byte. This is not guaranteed |
| 88 | // by C++ and is implementation-dependent, but it simplifies the conversions. |
| 89 | static_assert(sizeof(bool) == 1); |
| 90 | |
| 91 | // Body *must* be 8 byte aligned. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 92 | union Body { |
| 93 | struct Key { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 94 | int32_t eventId; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 95 | uint32_t empty1; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 96 | nsecs_t eventTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 97 | int32_t deviceId; |
| 98 | int32_t source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 99 | int32_t displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 100 | std::array<uint8_t, 32> hmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 101 | int32_t action; |
| 102 | int32_t flags; |
| 103 | int32_t keyCode; |
| 104 | int32_t scanCode; |
| 105 | int32_t metaState; |
| 106 | int32_t repeatCount; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 107 | uint32_t empty2; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 108 | nsecs_t downTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 109 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 110 | inline size_t size() const { return sizeof(Key); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 111 | } key; |
| 112 | |
| 113 | struct Motion { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 114 | int32_t eventId; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 115 | uint32_t pointerCount; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 116 | nsecs_t eventTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 117 | int32_t deviceId; |
| 118 | int32_t source; |
Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 119 | int32_t displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 120 | std::array<uint8_t, 32> hmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 121 | int32_t action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 122 | int32_t actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 123 | int32_t flags; |
| 124 | int32_t metaState; |
| 125 | int32_t buttonState; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 126 | MotionClassification classification; // base type: uint8_t |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 127 | uint8_t empty2[3]; // 3 bytes to fill gap created by classification |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 128 | int32_t edgeFlags; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 129 | nsecs_t downTime __attribute__((aligned(8))); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 130 | float dsdx; // Begin window transform |
| 131 | float dtdx; // |
| 132 | float dtdy; // |
| 133 | float dsdy; // |
| 134 | float tx; // |
| 135 | float ty; // End window transform |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 136 | float xPrecision; |
| 137 | float yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 138 | float xCursorPosition; |
| 139 | float yCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 140 | float dsdxRaw; // Begin raw transform |
| 141 | float dtdxRaw; // |
| 142 | float dtdyRaw; // |
| 143 | float dsdyRaw; // |
| 144 | float txRaw; // |
| 145 | float tyRaw; // End raw transform |
Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 146 | /** |
| 147 | * The "pointers" field must be the last field of the struct InputMessage. |
| 148 | * When we send the struct InputMessage across the socket, we are not |
| 149 | * writing the entire "pointers" array, but only the pointerCount portion |
| 150 | * of it as an optimization. Adding a field after "pointers" would break this. |
| 151 | */ |
Michael Wright | b03f103 | 2015-05-14 16:29:13 +0100 | [diff] [blame] | 152 | struct Pointer { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 153 | PointerProperties properties; |
| 154 | PointerCoords coords; |
Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 155 | } pointers[MAX_POINTERS] __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 156 | |
| 157 | int32_t getActionId() const { |
| 158 | uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) |
| 159 | >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 160 | return pointers[index].properties.id; |
| 161 | } |
| 162 | |
| 163 | inline size_t size() const { |
| 164 | return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS |
| 165 | + sizeof(Pointer) * pointerCount; |
| 166 | } |
| 167 | } motion; |
| 168 | |
| 169 | struct Finished { |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 170 | bool handled; |
| 171 | uint8_t empty[7]; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 172 | nsecs_t consumeTime; // The time when the event was consumed by the receiving end |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 173 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 174 | inline size_t size() const { return sizeof(Finished); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 175 | } finished; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 176 | |
| 177 | struct Focus { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 178 | int32_t eventId; |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 179 | // The following 2 fields take up 4 bytes total |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 180 | bool hasFocus; |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 181 | uint8_t empty[3]; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 182 | |
| 183 | inline size_t size() const { return sizeof(Focus); } |
| 184 | } focus; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 185 | |
| 186 | struct Capture { |
| 187 | int32_t eventId; |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 188 | bool pointerCaptureEnabled; |
| 189 | uint8_t empty[3]; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 190 | |
| 191 | inline size_t size() const { return sizeof(Capture); } |
| 192 | } capture; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 193 | |
| 194 | struct Drag { |
| 195 | int32_t eventId; |
| 196 | float x; |
| 197 | float y; |
| 198 | bool isExiting; |
| 199 | uint8_t empty[3]; |
| 200 | |
| 201 | inline size_t size() const { return sizeof(Drag); } |
| 202 | } drag; |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 203 | |
| 204 | struct Timeline { |
| 205 | int32_t eventId; |
| 206 | uint32_t empty; |
| 207 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 208 | |
| 209 | inline size_t size() const { return sizeof(Timeline); } |
| 210 | } timeline; |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 211 | |
| 212 | struct TouchMode { |
| 213 | int32_t eventId; |
| 214 | // The following 2 fields take up 4 bytes total |
| 215 | bool isInTouchMode; |
| 216 | uint8_t empty[3]; |
| 217 | |
| 218 | inline size_t size() const { return sizeof(TouchMode); } |
| 219 | } touchMode; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 220 | } __attribute__((aligned(8))) body; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 221 | |
| 222 | bool isValid(size_t actualSize) const; |
| 223 | size_t size() const; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 224 | void getSanitizedCopy(InputMessage* msg) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | /* |
| 228 | * An input channel consists of a local unix domain socket used to send and receive |
| 229 | * input messages across processes. Each channel has a descriptive name for debugging purposes. |
| 230 | * |
| 231 | * Each endpoint has its own InputChannel object that specifies its file descriptor. |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 232 | * For parceling, this relies on android::os::InputChannelCore, defined in aidl. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 233 | * |
| 234 | * The input channel is closed when all references to it are released. |
| 235 | */ |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 236 | class InputChannel : private android::os::InputChannelCore { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 237 | public: |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 238 | static std::unique_ptr<InputChannel> create(android::os::InputChannelCore&& parceledChannel); |
| 239 | ~InputChannel(); |
| 240 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Create a pair of input channels. |
| 243 | * The two returned input channels are equivalent, and are labeled as "server" and "client" |
| 244 | * for convenience. The two input channels share the same token. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 245 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 246 | * Return OK on success. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 247 | */ |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 248 | static status_t openInputChannelPair(const std::string& name, |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 249 | std::unique_ptr<InputChannel>& outServerChannel, |
| 250 | std::unique_ptr<InputChannel>& outClientChannel); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 251 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 252 | inline std::string getName() const { return name; } |
| 253 | inline int getFd() const { return fd.get(); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 254 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 255 | /* Send a message to the other endpoint. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 256 | * |
| 257 | * If the channel is full then the message is guaranteed not to have been sent at all. |
| 258 | * Try again after the consumer has sent a finished signal indicating that it has |
| 259 | * consumed some of the pending messages from the channel. |
| 260 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 261 | * Return OK on success. |
| 262 | * Return WOULD_BLOCK if the channel is full. |
| 263 | * Return DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 264 | * Other errors probably indicate that the channel is broken. |
| 265 | */ |
| 266 | status_t sendMessage(const InputMessage* msg); |
| 267 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 268 | /* Receive a message sent by the other endpoint. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 269 | * |
| 270 | * If there is no message present, try again after poll() indicates that the fd |
| 271 | * is readable. |
| 272 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 273 | * Return OK on success. |
| 274 | * Return WOULD_BLOCK if there is no message present. |
| 275 | * Return DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 276 | * Other errors probably indicate that the channel is broken. |
| 277 | */ |
Paul Ramirez | 10bae11 | 2024-06-18 21:33:33 +0000 | [diff] [blame] | 278 | android::base::Result<InputMessage> receiveMessage(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 279 | |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 280 | /* Tells whether there is a message in the channel available to be received. |
| 281 | * |
| 282 | * This is only a performance hint and may return false negative results. Clients should not |
| 283 | * rely on availability of the message based on the return value. |
| 284 | */ |
| 285 | bool probablyHasInput() const; |
| 286 | |
Egor Pasko | 5a67a56 | 2024-01-16 16:46:45 +0100 | [diff] [blame] | 287 | /* Wait until there is a message in the channel. |
| 288 | * |
| 289 | * The |timeout| specifies how long to block waiting for an input event to appear. Negative |
| 290 | * values are not allowed. |
| 291 | * |
| 292 | * In some cases returning before timeout expiration can happen without a message available. |
| 293 | * This could happen after the channel was closed on the other side. Another possible reason |
| 294 | * is incorrect setup of the channel. |
| 295 | */ |
| 296 | void waitForMessage(std::chrono::milliseconds timeout) const; |
| 297 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 298 | /* Return a new object that has a duplicate of this channel's fd. */ |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 299 | std::unique_ptr<InputChannel> dup() const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 300 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 301 | void copyTo(android::os::InputChannelCore& outChannel) const; |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 302 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 303 | /** |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 304 | * Similar to "copyTo", but it takes ownership of the provided InputChannel (and after this is |
| 305 | * called, it destroys it). |
| 306 | * @param from the InputChannel that should be converted to InputChannelCore |
| 307 | * @param outChannel the pre-allocated InputChannelCore to which to transfer the 'from' channel |
| 308 | */ |
| 309 | static void moveChannel(std::unique_ptr<InputChannel> from, |
| 310 | android::os::InputChannelCore& outChannel); |
| 311 | |
| 312 | /** |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 313 | * The connection token is used to identify the input connection, i.e. |
| 314 | * the pair of input channels that were created simultaneously. Input channels |
| 315 | * are always created in pairs, and the token can be used to find the server-side |
| 316 | * input channel from the client-side input channel, and vice versa. |
| 317 | * |
| 318 | * Do not use connection token to check equality of a specific input channel object |
| 319 | * to another, because two different (client and server) input channels will share the |
| 320 | * same connection token. |
| 321 | * |
| 322 | * Return the token that identifies this connection. |
| 323 | */ |
| 324 | sp<IBinder> getConnectionToken() const; |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 325 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 326 | private: |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 327 | static std::unique_ptr<InputChannel> create(const std::string& name, |
| 328 | android::base::unique_fd fd, sp<IBinder> token); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 329 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 330 | InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | /* |
| 334 | * Publishes input events to an input channel. |
| 335 | */ |
| 336 | class InputPublisher { |
| 337 | public: |
| 338 | /* Creates a publisher associated with an input channel. */ |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 339 | explicit InputPublisher(const std::shared_ptr<InputChannel>& channel); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 340 | |
| 341 | /* Destroys the publisher and releases its input channel. */ |
| 342 | ~InputPublisher(); |
| 343 | |
| 344 | /* Gets the underlying input channel. */ |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 345 | inline InputChannel& getChannel() const { return *mChannel; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 346 | |
| 347 | /* Publishes a key event to the input channel. |
| 348 | * |
| 349 | * Returns OK on success. |
| 350 | * Returns WOULD_BLOCK if the channel is full. |
| 351 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 352 | * Returns BAD_VALUE if seq is 0. |
| 353 | * Other errors probably indicate that the channel is broken. |
| 354 | */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 355 | status_t publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 356 | ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac, |
| 357 | int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, |
| 358 | int32_t metaState, int32_t repeatCount, nsecs_t downTime, |
| 359 | nsecs_t eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 360 | |
| 361 | /* Publishes a motion event to the input channel. |
| 362 | * |
| 363 | * Returns OK on success. |
| 364 | * Returns WOULD_BLOCK if the channel is full. |
| 365 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 366 | * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS. |
| 367 | * Other errors probably indicate that the channel is broken. |
| 368 | */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 369 | status_t publishMotionEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 370 | ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac, |
| 371 | int32_t action, int32_t actionButton, int32_t flags, |
| 372 | int32_t edgeFlags, int32_t metaState, int32_t buttonState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 373 | MotionClassification classification, const ui::Transform& transform, |
| 374 | float xPrecision, float yPrecision, float xCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 375 | float yCursorPosition, const ui::Transform& rawTransform, |
| 376 | nsecs_t downTime, nsecs_t eventTime, uint32_t pointerCount, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 377 | const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 378 | const PointerCoords* pointerCoords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 379 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 380 | /* Publishes a focus event to the input channel. |
| 381 | * |
| 382 | * Returns OK on success. |
| 383 | * Returns WOULD_BLOCK if the channel is full. |
| 384 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 385 | * Other errors probably indicate that the channel is broken. |
| 386 | */ |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 387 | status_t publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 388 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 389 | /* Publishes a capture event to the input channel. |
| 390 | * |
| 391 | * Returns OK on success. |
| 392 | * Returns WOULD_BLOCK if the channel is full. |
| 393 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 394 | * Other errors probably indicate that the channel is broken. |
| 395 | */ |
| 396 | status_t publishCaptureEvent(uint32_t seq, int32_t eventId, bool pointerCaptureEnabled); |
| 397 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 398 | /* Publishes a drag event to the input channel. |
| 399 | * |
| 400 | * Returns OK on success. |
| 401 | * Returns WOULD_BLOCK if the channel is full. |
| 402 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 403 | * Other errors probably indicate that the channel is broken. |
| 404 | */ |
| 405 | status_t publishDragEvent(uint32_t seq, int32_t eventId, float x, float y, bool isExiting); |
| 406 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 407 | /* Publishes a touch mode event to the input channel. |
| 408 | * |
| 409 | * Returns OK on success. |
| 410 | * Returns WOULD_BLOCK if the channel is full. |
| 411 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 412 | * Other errors probably indicate that the channel is broken. |
| 413 | */ |
| 414 | status_t publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode); |
| 415 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 416 | struct Finished { |
| 417 | uint32_t seq; |
| 418 | bool handled; |
| 419 | nsecs_t consumeTime; |
| 420 | }; |
| 421 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 422 | struct Timeline { |
| 423 | int32_t inputEventId; |
| 424 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 425 | }; |
| 426 | |
| 427 | typedef std::variant<Finished, Timeline> ConsumerResponse; |
| 428 | /* Receive a signal from the consumer in reply to the original dispatch signal. |
| 429 | * If a signal was received, returns a Finished or a Timeline object. |
| 430 | * The InputConsumer should return a Finished object for every InputMessage that it is sent |
| 431 | * to confirm that it has been processed and that the InputConsumer is responsive. |
| 432 | * If several InputMessages are sent to InputConsumer, it's possible to receive Finished |
| 433 | * events out of order for those messages. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 434 | * |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 435 | * The Timeline object is returned whenever the receiving end has processed a graphical frame |
| 436 | * and is returning the timeline of the frame. Not all input events will cause a Timeline |
| 437 | * object to be returned, and there is not guarantee about when it will arrive. |
| 438 | * |
| 439 | * If an object of Finished is returned, the returned sequence number is never 0 unless the |
| 440 | * operation failed. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 441 | * |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 442 | * Returned error codes: |
| 443 | * OK on success. |
| 444 | * WOULD_BLOCK if there is no signal present. |
| 445 | * DEAD_OBJECT if the channel's peer has been closed. |
| 446 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 447 | */ |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 448 | android::base::Result<ConsumerResponse> receiveConsumerResponse(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 449 | |
| 450 | private: |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 451 | std::shared_ptr<InputChannel> mChannel; |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 452 | InputVerifier mInputVerifier; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 455 | } // namespace android |