1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef INPUT_EVENT_H 17 #define INPUT_EVENT_H 18 19 #include <functional> 20 #include <memory> 21 #include <mutex> 22 23 #include "nocopyable.h" 24 #include "parcel.h" 25 26 namespace OHOS { 27 namespace MMI { 28 class InputEvent { 29 public: 30 /** 31 * Unknown action. It is usually used as the initial value. 32 * 33 * @since 9 34 */ 35 static constexpr int32_t ACTION_UNKNOWN = 0; 36 37 /** 38 * Cancel the action. It indicates that a consecutive input event is cancelled. 39 * 40 * @since 9 41 */ 42 static constexpr int32_t ACTION_CANCEL = 1; 43 44 /** 45 * The actual type of the current input event is the basic type (InputEvent type). 46 * 47 * @since 9 48 */ 49 static constexpr int32_t EVENT_TYPE_BASE = 0X00000000; 50 51 /** 52 * The actual type of the current input event is KeyEvent or its derived class. 53 * 54 * @since 9 55 */ 56 static constexpr int32_t EVENT_TYPE_KEY = 0X00010000; 57 58 /** 59 * The actual type of the current input event is PointerEvent or its derived class. 60 * 61 * @since 9 62 */ 63 static constexpr int32_t EVENT_TYPE_POINTER = 0X00020000; 64 65 /** 66 * The actual type of the current input event is AxisEvent or its derived class. 67 * 68 * @since 9 69 */ 70 static constexpr int32_t EVENT_TYPE_AXIS = 0X00030000; 71 72 /** 73 * The multimodal input service sends input events to the interceptor and listener. This is the default value. 74 * 75 * @since 9 76 */ 77 static constexpr uint32_t EVENT_FLAG_NONE = 0x00000000; 78 79 /** 80 * The multimodal input service does not intercept the input event. 81 * 82 * @since 9 83 */ 84 static constexpr uint32_t EVENT_FLAG_NO_INTERCEPT = 0x00000001; 85 86 /** 87 * The multimodal input service does not listen for the input event. 88 * 89 * @since 9 90 */ 91 static constexpr uint32_t EVENT_FLAG_NO_MONITOR = 0x00000002; 92 93 /** 94 * The multimodal input event from simulation. 95 * 96 * @since 10 97 */ 98 static constexpr uint32_t EVENT_FLAG_SIMULATE = 0x00000004; 99 100 public: 101 /** 102 * Copy constructor function for InputEvent 103 * 104 * @since 9 105 */ 106 InputEvent(const InputEvent& other); 107 108 /** 109 * Virtual destructor of InputEvent 110 * 111 * @since 9 112 */ 113 virtual ~InputEvent(); 114 115 virtual InputEvent& operator=(const InputEvent& other) = delete; 116 DISALLOW_MOVE(InputEvent); 117 118 /** 119 * Create InputEvent object 120 * 121 * @since 9 122 */ 123 static std::shared_ptr<InputEvent> Create(); 124 125 /** 126 * @brief Converts an input event type into a string. 127 * @param Indicates the input event type. 128 * @return Returns the string converted from the input event type. 129 * @since 9 130 */ 131 static const char* EventTypeToString(int32_t eventType); 132 133 /** 134 * @brief Resets an input event to the initial state. 135 * @return void 136 * @since 9 137 */ 138 virtual void Reset(); 139 140 /** 141 * @brief Obtains the unique ID of an input event. 142 * @return Returns the unique ID of the input event. 143 * @since 9 144 */ 145 int32_t GetId() const; 146 147 /** 148 * @brief Sets the unique ID of an input event. 149 * @param id Indicates the unique ID. 150 * @return void 151 * @since 9 152 */ 153 void SetId(int32_t id); 154 155 /** 156 * @brief Updates the unique ID of an input event. 157 * @return void 158 * @since 9 159 */ 160 void UpdateId(); 161 162 /** 163 * @brief Obtains the time when the action for this input event occurs. 164 * @return Returns the time when the action for this input event occurs. 165 * @since 9 166 */ 167 int64_t GetActionTime() const; 168 169 /** 170 * @brief Sets the time when the action for this input event occurs. 171 * @param actionTime Indicates the time when the action for this input event occurs. 172 * @return void 173 * @since 9 174 */ 175 void SetActionTime(int64_t actionTime); 176 177 /** 178 * @brief Get the time when sensor perceive the event. 179 * @param sensorTime Indicates the time when sensor event occurs. 180 * @return void 181 * @since 9 182 */ 183 void SetSensorInputTime(uint64_t sensorTime); 184 185 /** 186 * @brief Set the time for sensor when the action for this input event occurs. 187 * @return Returns the time when sensor perceive the event. 188 * @since 9 189 */ 190 uint64_t GetSensorInputTime(); 191 192 /** 193 * @brief Obtains the action for this input event. 194 * @return Returns the action for this input event. 195 * @since 9 196 */ 197 int32_t GetAction() const; 198 199 /** 200 * @brief Sets the action for this input event. 201 * @param action Indicates the action for the input event. 202 * @return void 203 * @since 9 204 */ 205 void SetAction(int32_t action); 206 207 /** 208 * @brief Obtains the time when the action for the first input event in a series of related input events occurs. 209 * @return Returns the time when the action for the first input event occurs. 210 * @since 9 211 */ 212 int64_t GetActionStartTime() const; 213 214 /** 215 * @brief Sets the time when the action for the first input event in a series of related input events occurs. 216 * @param time Indicates the time when the action for the first input event occurs. 217 * @return void 218 * @since 9 219 */ 220 void SetActionStartTime(int64_t time); 221 222 /** 223 * @brief Obtains the unique ID of the device that generates this input event. 224 * @return Returns the unique ID of the device. 225 * @since 9 226 */ 227 int32_t GetDeviceId() const; 228 229 /** 230 * @brief Sets the unique ID of the device that generates this input event. 231 * @param deviceId Indicates the unique ID of the device. 232 * @return void 233 * @since 9 234 */ 235 void SetDeviceId(int32_t deviceId); 236 237 /** 238 * @brief Obtains the ID of the target display for an input event. 239 * @return Returns the ID of the target display. 240 * @since 9 241 */ 242 int32_t GetTargetDisplayId() const; 243 244 /** 245 * @brief Sets the ID of the target screen for an input event. 246 * @param displayId Indicates the ID of the target display. 247 * @return void 248 * @since 9 249 */ 250 void SetTargetDisplayId(int32_t displayId); 251 252 /** 253 * @brief Obtains the ID of the target window for an input event. 254 * @return Returns the ID of the target window. 255 * @since 9 256 */ 257 int32_t GetTargetWindowId() const; 258 259 /** 260 * @brief Sets the ID of the target window for an input event. 261 * @param windowId Indicates the ID of the target window. 262 * @return void 263 * @since 9 264 */ 265 void SetTargetWindowId(int32_t windowId); 266 267 /** 268 * @brief Obtains the ID of the agent window for an input event. 269 * @return Returns the ID of the agent window. 270 * @since 9 271 */ 272 int32_t GetAgentWindowId() const; 273 274 /** 275 * @brief Sets the ID of the agent window for an input event. 276 * @param windowId Indicates the ID of the agent window. 277 * @return void 278 * @since 9 279 */ 280 void SetAgentWindowId(int32_t windowId); 281 282 /** 283 * @brief Obtains the type of the this input event. 284 * @return Returns the type of the this input event. 285 * @since 9 286 */ 287 int32_t GetEventType() const; 288 289 /** 290 * @brief Obtains all flags of an input event. 291 * @return Returns the flags of the input event. 292 * @since 9 293 */ 294 uint32_t GetFlag() const; 295 296 /** 297 * @brief Checks whether a flag has been set for an input event. 298 * @param flag Indicates the flag of the input event. 299 * @return Returns <b>true</b> if a flag has been set; returns <b>false</b> otherwise. 300 * @since 9 301 */ 302 bool HasFlag(uint32_t flag); 303 304 /** 305 * @brief Adds a flag for an input event. 306 * @param flag Indicates the flag of the input event. 307 * @return void 308 * @since 9 309 */ 310 void AddFlag(uint32_t flag); 311 312 /** 313 * @brief Clears all flags of an input event. 314 * @return void 315 * @since 9 316 */ 317 void ClearFlag(); 318 319 /** 320 * @brief Marks an input event as completed. 321 * This method can only be called once. 322 * @return void 323 * @since 9 324 */ 325 void MarkProcessed(); 326 327 /** 328 * @brief Sets the callback invoked when an input event is processed. 329 * This method is not available for third-party applications. 330 * @return void 331 * @since 9 332 */ 333 void SetProcessedCallback(std::function<void(int32_t, int64_t)> callback); 334 335 /** 336 * @brief Sets the extra data of an input event. 337 * @param data the extra data. 338 * @param length data length in bytes. 339 * @return void 340 * @since 10 341 */ 342 void SetExtraData(const std::shared_ptr<const uint8_t[]> data, uint32_t length); 343 344 /** 345 * @brief Obtains the extra data of an input event. 346 * @param data the extra data. 347 * @param length data length in bytes. 348 * @return void 349 * @since 10 350 */ 351 void GetExtraData(std::shared_ptr<const uint8_t[]> &data, uint32_t &length) const; 352 353 public: 354 /** 355 * @brief Writes data to a <b>Parcel</b> object. 356 * @param out Indicates the object into which data will be written. 357 * @return Returns <b>true</b> if the data is successfully written; returns <b>false</b> otherwise. 358 * @since 9 359 */ 360 bool WriteToParcel(Parcel &out) const; 361 362 /** 363 * @brief Reads data from a <b>Parcel</b> object. 364 * @param in Indicates the object from which data will be read. 365 * @return Returns <b>true</b> if the data is successfully read; returns <b>false</b> otherwise. 366 * @since 9 367 */ 368 bool ReadFromParcel(Parcel &in); 369 370 protected: 371 /** 372 * @brief Constructs an input event object by using the specified input event type. Generally, this method 373 * is used to construct a base class object when constructing a derived class object. 374 * @since 9 375 */ 376 explicit InputEvent(int32_t eventType); 377 378 private: 379 int32_t eventType_; 380 int32_t id_; 381 int64_t actionTime_; 382 uint64_t sensorInputTime_ { 0 }; 383 int32_t action_; 384 int64_t actionStartTime_; 385 int32_t deviceId_; 386 int32_t targetDisplayId_; 387 int32_t targetWindowId_; 388 int32_t agentWindowId_; 389 uint32_t bitwise_; 390 std::function<void(int32_t, int64_t)> processedCallback_; 391 std::shared_ptr<const uint8_t[]> extraData_; 392 uint32_t extraDataLength_ { 0 }; 393 }; 394 } // namespace MMI 395 } // namespace OHOS 396 #endif // INPUT_EVENT_H