1 /* 2 * Copyright (c) 2021-2025 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 "parcel.h" 20 21 namespace OHOS { 22 namespace MMI { 23 class InputEvent : public Parcelable { 24 public: 25 /** 26 * Unknown action. It is usually used as the initial value. 27 * 28 * @since 9 29 */ 30 static constexpr int32_t ACTION_UNKNOWN = 0; 31 32 /** 33 * Cancel the action. It indicates that a consecutive input event is cancelled. 34 * 35 * @since 9 36 */ 37 static constexpr int32_t ACTION_CANCEL = 1; 38 39 /** 40 * The actual type of the current input event is the basic type (InputEvent type). 41 * 42 * @since 9 43 */ 44 static constexpr int32_t EVENT_TYPE_BASE = 0X00000000; 45 46 /** 47 * The actual type of the current input event is KeyEvent or its derived class. 48 * 49 * @since 9 50 */ 51 static constexpr int32_t EVENT_TYPE_KEY = 0X00010000; 52 53 /** 54 * The actual type of the current input event is PointerEvent or its derived class. 55 * 56 * @since 9 57 */ 58 static constexpr int32_t EVENT_TYPE_POINTER = 0X00020000; 59 60 /** 61 * The actual type of the current input event is AxisEvent or its derived class. 62 * 63 * @since 9 64 */ 65 static constexpr int32_t EVENT_TYPE_AXIS = 0X00030000; 66 67 /** 68 * The actual type of the current input event is FingerprintEvent or its derived class. 69 * 70 * @since 12 71 */ 72 static constexpr int32_t EVENT_TYPE_FINGERPRINT = 0X00040000; 73 74 /** 75 * The multimodal input service sends input events to the interceptor and listener. This is the default value. 76 * 77 * @since 9 78 */ 79 static constexpr uint32_t EVENT_FLAG_NONE = 0x00000000; 80 81 /** 82 * The multimodal input service does not intercept the input event. 83 * 84 * @since 9 85 */ 86 static constexpr uint32_t EVENT_FLAG_NO_INTERCEPT = 0x00000001; 87 88 /** 89 * The multimodal input service does not listen for the input event. 90 * 91 * @since 9 92 */ 93 static constexpr uint32_t EVENT_FLAG_NO_MONITOR = 0x00000002; 94 95 /** 96 * The multimodal input event from simulation. 97 * 98 * @since 10 99 */ 100 static constexpr uint32_t EVENT_FLAG_SIMULATE = 0x00000004; 101 102 /** 103 * The multimodal input service hide pointer. 104 * 105 * @since 12 106 */ 107 static constexpr uint32_t EVENT_FLAG_HIDE_POINTER = 0x00000008; 108 109 static constexpr uint32_t EVENT_FLAG_RAW_POINTER_MOVEMENT = 0x00000010; 110 static constexpr uint32_t EVENT_FLAG_TOUCHPAD_POINTER = 0x00000020; 111 static constexpr uint32_t EVENT_FLAG_PRIVACY_MODE = 0x00000040; 112 static constexpr uint32_t EVENT_FLAG_ACCESSIBILITY = 0x00000100; 113 114 /** 115 * The multimodal input event from navigation window. 116 * 117 * @since 12 118 */ 119 static constexpr uint32_t EVENT_FLAG_SIMULATE_NAVIGATION = 0x00000200; 120 121 static constexpr uint32_t EVENT_FLAG_GENERATE_FROM_REAL = 0x00000400; 122 123 static constexpr uint32_t EVENT_FLAG_SHOW_CUSOR_WITH_TOUCH = 0x00000600; 124 125 static constexpr uint32_t EVENT_FLAG_VIRTUAL_TOUCHPAD_POINTER = 0x00001000; 126 127 /** 128 * The multimodal input event for device enter focus flag. 129 * 130 * @since 19 131 */ 132 static constexpr uint32_t EVENT_FLAG_KEYBOARD_ENTER_FOCUS = 0x00002000; 133 /** 134 * The multimodal input event for device exit focus flag. 135 * 136 * @since 19 137 */ 138 static constexpr uint32_t EVENT_FLAG_KEYBOARD_EXIT_FOCUS = 0x00004000; 139 140 static constexpr uint32_t EVENT_FLAG_GESTURE_SUPPLEMENT = 0x00008000; 141 142 static constexpr uint32_t EVENT_FLAG_KEYBOARD_ESCAPE = 0x00080000; 143 144 static constexpr uint32_t EVENT_FLAG_UITEST = 0x00010000; 145 146 static constexpr uint32_t EVENT_FLAG_DISABLE_PULL_THROW = 0x00020000; 147 148 /** 149 * Indicates an unknown input source type. It is usually used as the initial value. 150 * 151 * @since 9 152 */ 153 static constexpr int32_t SOURCE_TYPE_UNKNOWN = 0; 154 155 /** 156 * Indicates that the input source generates events similar to mouse cursor movement, 157 * button press and release, and wheel scrolling. 158 * 159 * @since 9 160 */ 161 static constexpr int32_t SOURCE_TYPE_MOUSE = 1; 162 163 /** 164 * Indicates that the input source generates a touchscreen multi-touch event. 165 * 166 * @since 9 167 */ 168 static constexpr int32_t SOURCE_TYPE_TOUCHSCREEN = 2; 169 170 /** 171 * Indicates that the input source generates a touchpad multi-touch event. 172 * 173 * @since 9 174 */ 175 static constexpr int32_t SOURCE_TYPE_TOUCHPAD = 3; 176 177 /** 178 * Indicates joystick-like events generated by the input source, such as button pressing, button lifting, 179 * and wheel scrolling. 180 * 181 * @since 9 182 */ 183 static constexpr int32_t SOURCE_TYPE_JOYSTICK = 4; 184 185 /** 186 * Indicates that the input source generates a fingerprint event. 187 * 188 * @since 12 189 */ 190 static constexpr int32_t SOURCE_TYPE_FINGERPRINT = 5; 191 192 /** 193 * Indicates that the input source generates a crown event. 194 * 195 * @since 12 196 */ 197 static constexpr int32_t SOURCE_TYPE_CROWN = 6; 198 199 /** 200 * Indicates that the input source generates left and right hand event. 201 * 202 * @since 16 203 */ 204 static constexpr int32_t SOURCE_TYPE_MSDP_HAND_OPTINON = 7; 205 206 /** 207 * Indicates that the input source generates a x-key event. 208 * 209 * @since 16 210 */ 211 static constexpr int32_t SOURCE_TYPE_X_KEY = 8; 212 213 public: 214 /** 215 * Copy constructor function for InputEvent 216 * 217 * @since 9 218 */ 219 InputEvent(const InputEvent &other); 220 221 /** 222 * Virtual destructor of InputEvent 223 * 224 * @since 9 225 */ 226 virtual ~InputEvent(); 227 228 virtual InputEvent& operator=(const InputEvent& other) = delete; 229 DISALLOW_MOVE(InputEvent); 230 231 /** 232 * Create InputEvent object 233 * 234 * @since 9 235 */ 236 static std::shared_ptr<InputEvent> Create(); 237 238 /** 239 * @brief Converts an input event type into a string. 240 * @param Indicates the input event type. 241 * @return Returns the string converted from the input event type. 242 * @since 9 243 */ 244 static const char* EventTypeToString(int32_t eventType); 245 246 /** 247 * @brief Resets an input event to the initial state. 248 * @return void 249 * @since 9 250 */ 251 virtual void Reset(); 252 253 virtual std::string ToString(); 254 255 /** 256 * @brief Obtains the unique ID of an input event. 257 * @return Returns the unique ID of the input event. 258 * @since 9 259 */ 260 int32_t GetId() const; 261 262 /** 263 * @brief Sets the unique ID of an input event. 264 * @param id Indicates the unique ID. 265 * @return void 266 * @since 9 267 */ 268 void SetId(int32_t id); 269 270 /** 271 * @brief Updates the unique ID of an input event. 272 * @return void 273 * @since 9 274 */ 275 void UpdateId(); 276 277 /** 278 * @brief Obtains the time when the action for this input event occurs. 279 * @return Returns the time when the action for this input event occurs. 280 * @since 9 281 */ 282 int64_t GetActionTime() const; 283 284 /** 285 * @brief Sets the time when the action for this input event occurs. 286 * @param actionTime Indicates the time when the action for this input event occurs. 287 * @return void 288 * @since 9 289 */ 290 void SetActionTime(int64_t actionTime); 291 292 /** 293 * @brief Get the time when sensor perceive the event. 294 * @param sensorTime Indicates the time when sensor event occurs. 295 * @return void 296 * @since 9 297 */ 298 void SetSensorInputTime(uint64_t sensorTime); 299 300 /** 301 * @brief Set the time for sensor when the action for this input event occurs. 302 * @return Returns the time when sensor perceive the event. 303 * @since 9 304 */ 305 uint64_t GetSensorInputTime() const; 306 307 /** 308 * @brief Obtains the action for this input event. 309 * @return Returns the action for this input event. 310 * @since 9 311 */ 312 int32_t GetAction() const; 313 314 /** 315 * @brief Sets the action for this input event. 316 * @param action Indicates the action for the input event. 317 * @return void 318 * @since 9 319 */ 320 void SetAction(int32_t action); 321 322 /** 323 * @brief Obtains the time when the action for the first input event in a series of related input events occurs. 324 * @return Returns the time when the action for the first input event occurs. 325 * @since 9 326 */ 327 int64_t GetActionStartTime() const; 328 329 /** 330 * @brief Sets the time when the action for the first input event in a series of related input events occurs. 331 * @param time Indicates the time when the action for the first input event occurs. 332 * @return void 333 * @since 9 334 */ 335 void SetActionStartTime(int64_t time); 336 337 /** 338 * @brief Obtains the unique ID of the device that generates this input event. 339 * @return Returns the unique ID of the device. 340 * @since 9 341 */ 342 int32_t GetDeviceId() const; 343 344 /** 345 * @brief Sets the unique ID of the device that generates this input event. 346 * @param deviceId Indicates the unique ID of the device. 347 * @return void 348 * @since 9 349 */ 350 void SetDeviceId(int32_t deviceId); 351 352 /** 353 * @brief Obtains the source type of this event. 354 * @return Returns the source type. 355 * @since 9 356 */ 357 int32_t GetSourceType() const; 358 359 /** 360 * @brief Sets the source type for this event. 361 * @param sourceType Indicates the source type to set. 362 * @return void 363 * @since 9 364 */ 365 void SetSourceType(int32_t sourceType); 366 367 /** 368 * @brief Dumps the source type of this pointer input event as a string. 369 * @return Returns the pointer to the string. 370 * @since 9 371 */ 372 const char* DumpSourceType() const; 373 374 /** 375 * @brief Obtains the ID of the target display for an input event. 376 * @return Returns the ID of the target display. 377 * @since 9 378 */ 379 int32_t GetTargetDisplayId() const; 380 381 /** 382 * @brief Sets the ID of the target screen for an input event. 383 * @param displayId Indicates the ID of the target display. 384 * @return void 385 * @since 9 386 */ 387 void SetTargetDisplayId(int32_t displayId); 388 389 /** 390 * @brief Obtains the ID of the target window for an input event. 391 * @return Returns the ID of the target window. 392 * @since 9 393 */ 394 int32_t GetTargetWindowId() const; 395 396 /** 397 * @brief Sets the ID of the target window for an input event. 398 * @param windowId Indicates the ID of the target window. 399 * @return void 400 * @since 9 401 */ 402 void SetTargetWindowId(int32_t windowId); 403 404 /** 405 * @brief Obtains the ID of the agent window for an input event. 406 * @return Returns the ID of the agent window. 407 * @since 9 408 */ 409 int32_t GetAgentWindowId() const; 410 411 /** 412 * @brief Sets the ID of the agent window for an input event. 413 * @param windowId Indicates the ID of the agent window. 414 * @return void 415 * @since 9 416 */ 417 void SetAgentWindowId(int32_t windowId); 418 419 /** 420 * @brief Obtains the type of the this input event. 421 * @return Returns the type of the this input event. 422 * @since 9 423 */ 424 int32_t GetEventType() const; 425 426 /** 427 * @brief Obtains all flags of an input event. 428 * @return Returns the flags of the input event. 429 * @since 9 430 */ 431 uint32_t GetFlag() const; 432 433 /** 434 * @brief Checks whether a flag has been set for an input event. 435 * @param flag Indicates the flag of the input event. 436 * @return Returns <b>true</b> if a flag has been set; returns <b>false</b> otherwise. 437 * @since 9 438 */ 439 bool HasFlag(uint32_t flag); 440 441 /** 442 * @brief Checks whether a flag the same as expected for an input event. 443 * @param flag Indicates the flag of the input event. 444 * @return Returns <b>true</b> if a flag the same as expected; returns <b>false</b> otherwise. 445 * @since 19 446 */ 447 bool IsFlag(uint32_t flag); 448 449 /** 450 * @brief Adds a flag for an input event. 451 * @param flag Indicates the flag of the input event. 452 * @return void 453 * @since 9 454 */ 455 void AddFlag(uint32_t flag); 456 457 /** 458 * @brief Clears all flags of an input event. 459 * @return void 460 * @since 9 461 */ 462 void ClearFlag(); 463 464 /** 465 * @brief Clears all flags of an input event. 466 * @param flag Indicates the flag of the input event. 467 * @return void 468 * @since 12 469 */ 470 void ClearFlag(uint32_t flag); 471 472 /** 473 * @brief Marks an input event as completed. 474 * This method can only be called once. 475 * @return void 476 * @since 9 477 */ 478 void MarkProcessed(); 479 480 /** 481 * @brief Sets the callback invoked when an input event is processed. 482 * This method is not available for third-party applications. 483 * @return void 484 * @since 9 485 */ 486 void SetProcessedCallback(std::function<void(int32_t, int64_t)> callback); 487 488 /** 489 * @brief Sets the extra data of an input event. 490 * @param data the extra data. 491 * @param length data length in bytes. 492 * @return void 493 * @since 10 494 */ 495 void SetExtraData(const std::shared_ptr<const uint8_t[]> data, uint32_t length); 496 497 /** 498 * @brief Obtains the extra data of an input event. 499 * @param data the extra data. 500 * @param length data length in bytes. 501 * @return void 502 * @since 10 503 */ 504 void GetExtraData(std::shared_ptr<const uint8_t[]> &data, uint32_t &length) const; 505 506 /** 507 * @brief Checks whether the "Processed feedback" of this event is enabled or not. 508 * @return Returns <b>true</b> if we need a "Processed feedback" for this event; 509 * returns <b>false</b> otherwise. 510 * @since 12 511 */ 512 bool IsMarkEnabled() const; 513 514 /** 515 * @brief Sets the markEnabled_ field of an input event. 516 * @param markEnabled Indicates whether we need a "Processed feedback" or not for this event. 517 * @return void 518 * @since 12 519 */ 520 void SetMarkEnabled(bool markEnabled); 521 522 /** 523 * @brief Converts a input event action into a short string. 524 * @param Indicates the input event action. 525 * @return Returns the string converted from the input action. 526 * @since 12 527 */ 528 static std::string_view ActionToShortStr(int32_t action); 529 public: 530 /** 531 * @brief Writes data to a <b>Parcel</b> object. 532 * @param out Indicates the object into which data will be written. 533 * @return Returns <b>true</b> if the data is successfully written; returns <b>false</b> otherwise. 534 * @since 9 535 */ 536 bool WriteToParcel(Parcel &out) const; 537 bool Marshalling(Parcel &out) const; 538 539 /** 540 * @brief Reads data from a <b>Parcel</b> object. 541 * @param in Indicates the object from which data will be read. 542 * @return Returns <b>true</b> if the data is successfully read; returns <b>false</b> otherwise. 543 * @since 9 544 */ 545 bool ReadFromParcel(Parcel &in); 546 static InputEvent *Unmarshalling(Parcel &in); 547 548 protected: 549 /** 550 * @brief Constructs an input event object by using the specified input event type. Generally, this method 551 * is used to construct a base class object when constructing a derived class object. 552 * @since 9 553 */ 554 explicit InputEvent(int32_t eventType); 555 556 private: 557 int32_t eventType_ { -1 }; 558 int32_t id_ { -1 }; 559 int64_t actionTime_ { -1 }; 560 uint64_t sensorInputTime_ { 0 }; 561 int32_t action_ { -1 }; 562 int64_t actionStartTime_ { -1 }; 563 int32_t deviceId_ { -1 }; 564 int32_t sourceType_ { SOURCE_TYPE_UNKNOWN }; 565 int32_t targetDisplayId_ { -1 }; 566 int32_t targetWindowId_ { -1 }; 567 int32_t agentWindowId_ { -1 }; 568 uint32_t bitwise_ { 0 }; 569 bool markEnabled_ { true }; 570 std::shared_ptr<const uint8_t[]> extraData_; 571 uint32_t extraDataLength_ { 0 }; 572 std::function<void(int32_t, int64_t)> processedCallback_; 573 }; 574 } // namespace MMI 575 } // namespace OHOS 576 #endif // INPUT_EVENT_H 577