1 /* 2 * Copyright (C) 2021-2023 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 FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_METHOD_CONTROLLER_H 17 #define FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_METHOD_CONTROLLER_H 18 19 #include <atomic> 20 #include <chrono> 21 #include <condition_variable> 22 #include <ctime> 23 #include <mutex> 24 #include <thread> 25 #include <variant> 26 27 #include "block_queue.h" 28 #include "controller_listener.h" 29 #include "element_name.h" 30 #include "event_handler.h" 31 #include "global.h" 32 #include "iinput_method_agent.h" 33 #include "iinput_method_system_ability.h" 34 #include "ime_event_listener.h" 35 #include "input_client_info.h" 36 #include "input_method_property.h" 37 #include "input_method_status.h" 38 #include "input_method_utils.h" 39 #include "inputmethod_message_handler.h" 40 #include "ipc_skeleton.h" 41 #include "iremote_object.h" 42 #include "key_event.h" 43 #include "key_event_result_handler.h" 44 #include "msg_handler_callback_interface.h" 45 #include "panel_info.h" 46 #include "private_command_interface.h" 47 #include "visibility.h" 48 49 namespace OHOS { 50 namespace MiscServices { 51 class OnTextChangedListener : public virtual RefBase { 52 public: ~OnTextChangedListener()53 virtual ~OnTextChangedListener() {} 54 virtual void InsertText(const std::u16string &text) = 0; 55 virtual void DeleteForward(int32_t length) = 0; 56 virtual void DeleteBackward(int32_t length) = 0; 57 virtual void SendKeyEventFromInputMethod(const KeyEvent &event) = 0; 58 virtual void SendKeyboardStatus(const KeyboardStatus &keyboardStatus) = 0; NotifyPanelStatusInfo(const PanelStatusInfo & info)59 virtual void NotifyPanelStatusInfo(const PanelStatusInfo &info) 60 { 61 } NotifyKeyboardHeight(uint32_t height)62 virtual void NotifyKeyboardHeight(uint32_t height) 63 { 64 } 65 virtual void SendFunctionKey(const FunctionKey &functionKey) = 0; 66 virtual void SetKeyboardStatus(bool status) = 0; 67 virtual void MoveCursor(const Direction direction) = 0; 68 virtual void HandleSetSelection(int32_t start, int32_t end) = 0; 69 virtual void HandleExtendAction(int32_t action) = 0; 70 virtual void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) = 0; 71 virtual std::u16string GetLeftTextOfCursor(int32_t number) = 0; 72 virtual std::u16string GetRightTextOfCursor(int32_t number) = 0; 73 virtual int32_t GetTextIndexAtCursor() = 0; ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)74 virtual int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) 75 { 76 return ErrorCode::NO_ERROR; 77 } 78 /** 79 * @brief Set preview text. 80 * 81 * When InputMethod app sends request to set preview text, the function will be called. 82 * 83 * @param text Indicates the text to be previewed. 84 * @param range Indicates the range of text to be replaced. 85 * @return 86 * If success, please return 0. 87 * If parameter range check error, please return -1. 88 * If other failure, no specific requirement. 89 * @since 12 90 */ SetPreviewText(const std::u16string & text,const Range & range)91 virtual int32_t SetPreviewText(const std::u16string &text, const Range &range) 92 { 93 return ErrorCode::NO_ERROR; 94 } 95 /** 96 * @brief Finish text preview. 97 * 98 * When InputMethod app sends request to finish text preview, the function will be called. 99 * 100 * @since 12 101 */ FinishTextPreview()102 virtual void FinishTextPreview() 103 { 104 } OnDetach()105 virtual void OnDetach() 106 { 107 } 108 /** 109 * @brief Is listener from ts registration. 110 * 111 * If you use C/C++ interface, ignore this. 112 * 113 * @since 12 114 */ IsFromTs()115 virtual bool IsFromTs() 116 { 117 return false; 118 } GetEventHandler()119 virtual std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler() 120 { 121 return nullptr; 122 } 123 124 private: 125 friend class InputMethodController; 126 void InsertTextV2(const std::u16string &text); 127 void DeleteForwardV2(int32_t length); 128 void DeleteBackwardV2(int32_t length); 129 void SendKeyboardStatusV2(const KeyboardStatus &keyboardStatus); 130 void SendFunctionKeyV2(const FunctionKey &functionKey); 131 void MoveCursorV2(const Direction &direction); 132 void HandleExtendActionV2(int32_t action); 133 std::u16string GetLeftTextOfCursorV2(int32_t number); 134 std::u16string GetRightTextOfCursorV2(int32_t number); 135 int32_t GetTextIndexAtCursorV2(); 136 void SendKeyEventFromInputMethodV2(const KeyEvent &event); 137 void NotifyPanelStatusInfoV2(const PanelStatusInfo &info); 138 void NotifyKeyboardHeightV2(uint32_t height); 139 void SetKeyboardStatusV2(bool status); 140 void HandleSetSelectionV2(int32_t start, int32_t end); 141 void HandleSelectV2(int32_t keyCode, int32_t cursorMoveSkip); 142 int32_t ReceivePrivateCommandV2(const std::unordered_map<std::string, PrivateDataValue> &privateCommand); 143 int32_t SetPreviewTextV2(const std::u16string &text, const Range &range); 144 void FinishTextPreviewV2(); 145 void OnDetachV2(); 146 }; 147 using PrivateDataValue = std::variant<std::string, bool, int32_t>; 148 using KeyEventCallback = std::function<void(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed)>; 149 using WindowScaleCallback = std::function<int32_t(uint32_t windowId, CursorInfo &cursorInfo)>; 150 class InputMethodController : public RefBase, public PrivateCommandInterface { 151 public: 152 /** 153 * @brief Get the instance of InputMethodController. 154 * 155 * This function is used to get the instance of InputMethodController. 156 * 157 * @return The instance of InputMethodController. 158 * @since 6 159 */ 160 IMF_API static sptr<InputMethodController> GetInstance(); 161 162 /** 163 * @brief Show soft keyboard, set listener and bind IMSA with default states and attribute. 164 * 165 * This function is used to show soft keyboard, set listener and bind IMSA, 166 * default state is 'true', default attribute is 'InputAttribute::PATTERN_TEXT'. 167 * 168 * @param listener Indicates the listener in order to manipulate text. 169 * @param type Indicates the type of caller. 170 * @return Returns 0 for success, others for failure. 171 * @since 6 172 */ 173 IMF_API int32_t Attach(sptr<OnTextChangedListener> listener, ClientType type = ClientType::INNER_KIT); 174 175 /** 176 * @brief Set listener and bind IMSA with given states and default attribute. 177 * 178 * This function is used to set listener and bind IMSA, 179 * default attribute is 'InputAttribute::PATTERN_TEXT'. Show soft keyboard when state is true. 180 * 181 * @param listener Indicates the listener in order to manipulate text. 182 * @param isShowKeyboard Indicates the state, if you want to show soft keyboard, please pass in true. 183 * @param type Indicates the type of caller. 184 * @return Returns 0 for success, others for failure. 185 * @since 8 186 */ 187 IMF_API int32_t Attach( 188 sptr<OnTextChangedListener> listener, bool isShowKeyboard, ClientType type = ClientType::INNER_KIT); 189 190 /** 191 * @brief Set listener and bind IMSA with given states and attribute. 192 * 193 * This function is used to set listener and bind IMSA. 194 * Show soft keyboard when state is true, and customized attribute. 195 * 196 * @param listener Indicates the listener in order to manipulate text. 197 * @param isShowKeyboard Indicates the state, if you want to show soft keyboard, please pass in true. 198 * @param attribute Indicates the attribute, such as input pattern, enter eyType, input option. 199 * @param type Indicates the type of caller. 200 * @return Returns 0 for success, others for failure. 201 * @since 8 202 */ 203 IMF_API int32_t Attach(sptr<OnTextChangedListener> listener, bool isShowKeyboard, const InputAttribute &attribute, 204 ClientType type = ClientType::INNER_KIT); 205 206 /** 207 * @brief Set listener and bind IMSA with given states and textConfig. 208 * 209 * This function is used to set listener and bind IMSA. 210 * Show soft keyboard when state is true, and customized attribute. 211 * 212 * @param listener Indicates the listener in order to manipulate text. 213 * @param isShowKeyboard Indicates the state, if you want to show soft keyboard, please pass in true. 214 * @param textConfig Indicates the textConfig, such as input attribute, cursorInfo, range of text selection, 215 * windowId. 216 * @param type Indicates the type of caller. 217 * @return Returns 0 for success, others for failure. 218 * @since 10 219 */ 220 IMF_API int32_t Attach(sptr<OnTextChangedListener> listener, bool isShowKeyboard, const TextConfig &textConfig, 221 ClientType type = ClientType::INNER_KIT); 222 223 /** 224 * @brief Set listener and bind IMSA with given states and textConfig. 225 * 226 * This function is used to set listener and bind IMSA. 227 * Show soft keyboard when state is true, and customized attribute. 228 * 229 * @param listener Indicates the listener in order to manipulate text. 230 * @param attachOptions Indicates the attachOptions, if you want to show soft keyboard, 231 * please pass in true. 232 * @param textConfig Indicates the textConfig, such as input attribute, cursorInfo, range of 233 * text selection,windowId. 234 * @param type Indicates the type of caller. 235 * @return Returns 0 for success, others for failure. 236 * @since 16 237 */ 238 IMF_API int32_t Attach(sptr<OnTextChangedListener> listener, const AttachOptions &attachOptions, 239 const TextConfig &textConfig, ClientType type = ClientType::INNER_KIT); 240 /** 241 * @brief Show soft keyboard. 242 * 243 * This function is used to show soft keyboard of current client. 244 * 245 * @param type Indicates the type of caller. 246 * @return Returns 0 for success, others for failure. 247 * @since 6 248 */ 249 IMF_API int32_t ShowTextInput(ClientType type = ClientType::INNER_KIT); 250 /** 251 * @brief Show soft keyboard. 252 * 253 * This function is used to show soft keyboard of current client. 254 * 255 * @param attachOptions Indicates the attachOptions, such as requestKeyboardReason 256 * @return Returns 0 for success, others for failure. 257 * @since 16 258 */ 259 IMF_API int32_t ShowTextInput(const AttachOptions &attachOptions, ClientType type = ClientType::INNER_KIT); 260 /** 261 * @brief Hide soft keyboard. 262 * 263 * This function is used to hide soft keyboard of current client, and keep binding. 264 * 265 * @return Returns 0 for success, others for failure. 266 * @since 6 267 */ 268 IMF_API int32_t HideTextInput(); 269 270 /** 271 * @brief Hide current input method, clear text listener and unbind IMSA. 272 * 273 * This function is used to stop input, whick will set listener to nullptr, 274 * hide current soft keyboard and unbind IMSA. 275 * 276 * @return Returns 0 for success, others for failure. 277 * @since 6 278 */ 279 IMF_API int32_t Close(); 280 281 /** 282 * @brief A callback function when the cursor changes. 283 * 284 * This function is the callback when the cursor changes. 285 * 286 * @param cursorInfo Indicates the information of current cursor changes. 287 * @return Returns 0 for success, others for failure. 288 * @since 6 289 */ 290 IMF_API int32_t OnCursorUpdate(CursorInfo cursorInfo); 291 292 /** 293 * @brief Discard the typing text. 294 * 295 * @return Returns 0 for success, others for failure. 296 * @since 16 297 */ 298 IMF_API int32_t DiscardTypingText(); 299 300 /** 301 * @brief A callback function when the cursor changes. 302 * 303 * This function is the callback when the cursor changes. 304 * 305 * @param text Indicates the currently selected text. 306 * @param start Indicates the coordinates of the current start. 307 * @param end Indicates the coordinates of the current end. 308 * @return Returns 0 for success, others for failure. 309 * @since 6 310 */ 311 IMF_API int32_t OnSelectionChange(std::u16string text, int start, int end); 312 313 /** 314 * @brief Changing the configuration of soft keyboard. 315 * 316 * This function is used to change the configuration of soft keyboard. 317 * 318 * @param info Indicates the current configuration. 319 * @return Returns 0 for success, others for failure. 320 * @since 6 321 */ 322 IMF_API int32_t OnConfigurationChange(Configuration info); 323 IMF_API void SetControllerListener(std::shared_ptr<ControllerListener> controllerListener); 324 325 /** 326 * @brief Dispatch keyboard event. 327 * 328 * This function is used to Dispatch events of keyboard. 329 * 330 * @param keyEvent Indicates the events keyboard. 331 * @return Returns true for success otherwise for failure. 332 * @since 6 333 */ 334 /** 335 * @brief Dispatch keyboard event. 336 * 337 * This function is used to Dispatch events of keyboard. 338 * 339 * @param keyEvent Indicates the events keyboard. 340 * @param callback Indicates the consumption result of key event. 341 * @return Returns 0 for success, others for failure. 342 * @since 11 343 */ 344 IMF_API int32_t DispatchKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent, KeyEventCallback callback); 345 346 /** 347 * @brief List input methods. 348 * 349 * This function is used to list all of input methods. 350 * 351 * @param props Indicates the input methods that will be listed. 352 * @return Returns 0 for success, others for failure. 353 * @since 6 354 */ 355 IMF_API int32_t ListInputMethod(std::vector<Property> &props); 356 357 /** 358 * @brief List input methods. 359 * 360 * This function is used to list enabled or disabled input methods. 361 * 362 * @param props Indicates the input methods that will be listed. 363 * @param enable Indicates the state of input method. 364 * @return Returns 0 for success, others for failure. 365 * @since 6 366 */ 367 IMF_API int32_t ListInputMethod(bool enable, std::vector<Property> &props); 368 369 /** 370 * @brief List input method subtypes. 371 * 372 * This function is used to list specified input method subtypes. 373 * 374 * @param property Indicates the specified input method property. 375 * @param subProperties Indicates the subtypes of specified input method that will be listed. 376 * @return Returns 0 for success, others for failure. 377 * @since 6 378 */ 379 IMF_API int32_t ListInputMethodSubtype(const Property &property, std::vector<SubProperty> &subProperties); 380 381 /** 382 * @brief List current input method subtypes. 383 * 384 * This function is used to list current input method subtypes. 385 * 386 * @param subProperties Indicates the subtypes of current input method that will be listed. 387 * @return Returns 0 for success, others for failure. 388 * @since 6 389 */ 390 IMF_API int32_t ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProperties); 391 392 /** 393 * @brief Get enter key type. 394 * 395 * This function is used to get enter key type of current client. 396 * 397 * @param keyType Indicates the enter key type of current client that will be obtained, such as SEND, SEARCH... 398 * @return Returns 0 for success, others for failure. 399 * @since 6 400 */ 401 int32_t GetEnterKeyType(int32_t &keyType); 402 403 /** 404 * @brief Get input pattern. 405 * 406 * This function is used to get text input type of current client. 407 * 408 * @param inputPattern Indicates the text input type of current client that will be obtained, such as TEXT, URL... 409 * @return Returns 0 for success, others for failure. 410 * @since 6 411 */ 412 int32_t GetInputPattern(int32_t &inputPattern); 413 414 /** 415 * @brief Get text config. 416 * 417 * This function is used to get text config of current client. 418 * 419 * @param textConfig Indicates the text config of current client that will be obtained. 420 * @return Returns 0 for success, others for failure. 421 * @since 10 422 */ 423 int32_t GetTextConfig(TextTotalConfig &config); 424 425 /** 426 * @brief Get current input method property. 427 * 428 * This function is used to get current input method property. 429 * 430 * @return The property of current input method. 431 * @since 6 432 */ 433 IMF_API std::shared_ptr<Property> GetCurrentInputMethod(); 434 435 /** 436 * @brief Get current input method subtypes. 437 * 438 * This function is used to get current input method's current subtype. 439 * 440 * @return The subtypes of current input method. 441 * @since 6 442 */ 443 IMF_API std::shared_ptr<SubProperty> GetCurrentInputMethodSubtype(); 444 445 /** 446 * @brief Get default input method property. 447 * 448 * This function is used to get default input method property. 449 * 450 * @return The property of default input method. 451 * @since 10 452 */ 453 IMF_API int32_t GetDefaultInputMethod(std::shared_ptr<Property> &prop); 454 455 /** 456 * @brief get input method config ability. 457 * 458 * This function is used to get input method config ability. 459 * 460 * @return The info of input settings. 461 * @since 10 462 */ 463 IMF_API int32_t GetInputMethodConfig(AppExecFwk::ElementName &inputMethodConfig); 464 465 /** 466 * @brief Set calling window id. 467 * 468 * This function is used to set calling window id to input method. 469 * 470 * @param windowId Indicates the window id. 471 * @return Returns 0 for success, others for failure. 472 * @since 6 473 */ 474 IMF_API int32_t SetCallingWindow(uint32_t windowId); 475 476 /** 477 * @brief Switch input method or subtype. 478 * 479 * This function is used to switch input method or subtype. 480 * 481 * @param name Indicates the id of target input method. 482 * @param subName Optional parameter. Indicates the subtype of target input method. 483 * @return Returns 0 for success, others for failure. 484 * @since 11 485 */ 486 IMF_API int32_t SwitchInputMethod(SwitchTrigger trigger, const std::string &name, const std::string &subName = ""); 487 488 /** 489 * @brief Set simple keyboard mode. 490 * 491 * This function is used to set the simple keyboard mode. 492 * 493 * @param enable indicates enable simple keyboard or not. 494 * @return Returns 0 for success, others for failure. 495 * @since 20 496 */ 497 IMF_API int32_t SetSimpleKeyboardEnabled(bool enable); 498 499 /** 500 * @brief Show soft keyboard. 501 * 502 * This function is used to show soft keyboard of current client. 503 * 504 * @param type Indicates the type of caller. 505 * @return Returns 0 for success, others for failure. 506 * @since 6 507 */ 508 IMF_API int32_t ShowSoftKeyboard(ClientType type = ClientType::INNER_KIT); 509 510 /** 511 * @brief Hide soft keyboard. 512 * 513 * This function is used to hide soft keyboard of current client, and keep binding. 514 * 515 * @return Returns 0 for success, others for failure. 516 * @since 6 517 */ 518 IMF_API int32_t HideSoftKeyboard(); 519 520 /** 521 * @brief Stop current input session. 522 * 523 * This function is used to stop current input session. 524 * 525 * @return Returns 0 for success, others for failure. 526 * @since 6 527 */ 528 IMF_API int32_t StopInputSession(); 529 530 /** 531 * @brief Show input method setting extension dialog. 532 * 533 * This function is used to show input method setting extension dialog. 534 * 535 * @return Returns 0 for success, others for failure. 536 * @since 8 537 */ 538 IMF_API int32_t ShowOptionalInputMethod(); 539 540 // Deprecated innerkits with no permission check, kept for compatibility 541 542 /** 543 * @brief Show soft keyboard. 544 * 545 * This function is used to show soft keyboard of current client. 546 * 547 * @return Returns 0 for success, others for failure. 548 * @deprecated since 9 549 * @since 6 550 */ 551 IMF_API int32_t ShowCurrentInput(); 552 553 /** 554 * @brief Hide soft keyboard. 555 * 556 * This function is used to hide soft keyboard of current client, and keep binding. 557 * 558 * @return Returns 0 for success, others for failure. 559 * @deprecated since 9 560 * @since 6 561 */ 562 IMF_API int32_t HideCurrentInput(); 563 564 /** 565 * @brief Request to show input method. 566 * 567 * This function is used to request to show input method. 568 * 569 * @return Returns 0 for success, others for failure. 570 * @since 11 571 */ 572 IMF_API int32_t RequestShowInput(); 573 574 /** 575 * @brief Request to hide input method. 576 * 577 * This function is used to request to hide input method. 578 * 579 * @return Returns 0 for success, others for failure. 580 * @since 11 581 */ 582 IMF_API int32_t RequestHideInput(bool isFocusTriggered = false); 583 584 /** 585 * @brief Show input method setting extension dialog. 586 * 587 * This function is used to show input method setting extension dialog. 588 * 589 * @return Returns 0 for success, others for failure. 590 * @deprecated since 9 591 * @since 6 592 */ 593 IMF_API int32_t DisplayOptionalInputMethod(); 594 595 /** 596 * @brief Get attach status. 597 * 598 * This function is used to get status of attach. 599 * 600 * @return Returns true for attached otherwise for detached. 601 * @since 10 602 */ 603 IMF_API bool WasAttached(); 604 605 /** 606 * @brief Query current client bound status, get client windowId 607 * 608 * This function is used to Query current client bound status. 609 * 610 * @param isInputStart Indicates imf bound status 611 * @param callingWndId Indicates the windows id of calling client. 612 * @param requestKeyboardReason Indicates requestKeyboardReason for show keyboard 613 * @return Returns true for imf upon bound state, return false for unbound status. 614 */ 615 int32_t GetInputStartInfo(bool& isInputStart, uint32_t& callingWndId, int32_t& requestKeyboardReason); 616 617 /** 618 * @brief Set agent which will be used to communicate with IMA. 619 * 620 * This function is used to Set agent. 621 * 622 * @since 10 623 */ 624 void OnInputReady(sptr<IRemoteObject> agentObject, const BindImeInfo &imeInfo); 625 626 /** 627 * @brief Unbind IMC with Service. 628 * 629 * This function is unbind imc with service. 630 * 631 * @since 10 632 */ 633 void OnInputStop(bool isStopInactiveClient = false, sptr<IRemoteObject> proxy = nullptr); 634 void OnImeMirrorStop(sptr<IRemoteObject> object); 635 636 /** 637 * @brief Insert text. 638 * 639 * This function is used to insert text into editor. 640 * 641 * @param text Indicates the text which will be inserted. 642 * @return Returns 0 for success, others for failure. 643 * @since 10 644 */ 645 int32_t InsertText(const std::u16string &text); 646 647 /** 648 * @brief Move cursor. 649 * 650 * This function is used to move cursor according to the direction. 651 * 652 * @param direction Indicates the direction according to which the cursor will be moved. 653 * @return Returns 0 for success, others for failure. 654 * @since 10 655 */ 656 int32_t MoveCursor(Direction direction); 657 658 /** 659 * @brief Delete forward. 660 * 661 * This function is used to delete text at the left of cursor. 662 * 663 * @param length Indicates the length of deleted text. 664 * @return Returns 0 for success, others for failure. 665 * @since 10 666 */ 667 int32_t DeleteForward(int32_t length); 668 669 /** 670 * @brief Delete backward. 671 * 672 * This function is used to delete text at the right of cursor. 673 * 674 * @param length Indicates the length of deleted text. 675 * @return Returns 0 for success, others for failure. 676 * @since 10 677 */ 678 int32_t DeleteBackward(int32_t length); 679 680 /** 681 * @brief Get text at the left of cursor. 682 * 683 * This function is used to get text at the left of cursor. 684 * 685 * @param length Indicates the length of text. 686 * @param text Indicates the text which will be get. 687 * @return Returns 0 for success, others for failure. 688 * @since 10 689 */ 690 int32_t GetLeft(int32_t length, std::u16string &text); 691 692 /** 693 * @brief Get text at the right of cursor. 694 * 695 * This function is used to get text at the right of cursor. 696 * 697 * @param length Indicates the length of text. 698 * @param text Indicates the text which will be get. 699 * @return Returns 0 for success, others for failure. 700 * @since 10 701 */ 702 int32_t GetRight(int32_t length, std::u16string &text); 703 704 /** 705 * @brief Select text in editor by range. 706 * 707 * This function is used to select text in editor by range. 708 * 709 * @param start Indicates the beginning of the range. 710 * @param start Indicates the end of the range. 711 * @return Returns 0 for success, others for failure. 712 * @since 10 713 */ 714 void SelectByRange(int32_t start, int32_t end); 715 716 /** 717 * @brief Select text in editor by cursor movement. 718 * 719 * This function is used to select text in editor by cursor movement. 720 * 721 * @param direction Indicates the direction of cursor movement. 722 * @param cursorMoveSkip Indicates the skip of cursor movement. 723 * @return Returns 0 for success, others for failure. 724 * @since 10 725 */ 726 void SelectByMovement(int32_t direction, int32_t cursorMoveSkip); 727 728 /** 729 * @brief Handle extend action code. 730 * 731 * This function is used to handle extend action code. 732 * 733 * @param action Indicates the action code which will be handled. 734 * @return Returns 0 for success, others for failure. 735 * @since 10 736 */ 737 int32_t HandleExtendAction(int32_t action); 738 739 /** 740 * @brief Get the index number of text at cursor. 741 * 742 * This function is used to get the index number of text at cursor. 743 * 744 * @param index Indicates the index number of text at cursor. 745 * @return Returns 0 for success, others for failure. 746 * @since 10 747 */ 748 int32_t GetTextIndexAtCursor(int32_t &index); 749 750 /** 751 * @brief Send keyboard status. 752 * 753 * This function is used to send keyboard status to editor. 754 * 755 * @param status Indicates the status of keyboard. 756 * @since 10 757 */ 758 void SendKeyboardStatus(KeyboardStatus status); 759 760 /** 761 * @brief Send panel status info. 762 * 763 * This function is used to send panel status info to editor. 764 * Only notify the status info of soft keyboard(not contain candidate column) at present 765 * 766 * @param info Indicates the status info of panel. 767 * @since 11 768 */ 769 void NotifyPanelStatusInfo(const PanelStatusInfo &info); 770 771 /** 772 * @brief Send panel height. 773 * 774 * This function is used to send panel height to editor. 775 * 776 * @param info Indicates the panel height. 777 * @since 11 778 */ 779 void NotifyKeyboardHeight(uint32_t height); 780 781 /** 782 * @brief Send function key. 783 * 784 * This function is used to send function key to editor. 785 * 786 * @param functionKey Indicates the function key. 787 * @return Returns 0 for success, others for failure. 788 * @since 10 789 */ 790 int32_t SendFunctionKey(int32_t functionKey); 791 792 /** 793 * @brief Deactivate the input client. 794 * 795 * This function is used to deactivate the input client. 796 * 797 * @since 11 798 */ 799 void DeactivateClient(); 800 801 /** 802 * @brief Query whether an input type is supported. 803 * 804 * This function is used to query whether an input type is supported. 805 * 806 * @param type Indicates the input type being queried. 807 * @return Returns true for supported, false for not supported. 808 * @since 11 809 */ 810 IMF_API bool IsInputTypeSupported(InputType type); 811 812 /** 813 * @brief Start the input method which provides the specific input type. 814 * 815 * This function is used to start the input method which provides the specific input type. 816 * 817 * @param type Indicates the input type being specified. 818 * @return Returns 0 for success, others for failure. 819 * @since 11 820 */ 821 IMF_API int32_t StartInputType(InputType type); 822 823 /** 824 * @brief Start the input method which provides the specific input type. 825 * 826 * This function is used to start the input method which provides the specific input type. 827 * 828 * @param type Indicates the input type being specified. 829 * @return Returns 0 for success, others for failure. 830 * @since 21 831 */ 832 IMF_API int32_t StartInputTypeAsync(InputType type); 833 834 /** 835 * @brief Query whether the specific type panel is shown. 836 * 837 * This function is used to query whether the specific type panel is shown. 838 * 839 * @param panelInfo Indicates the info of the panel. 840 * @param isShown Indicates the state of the specific panel. 841 * @return Returns 0 for success, others for failure. 842 * @since 11 843 */ 844 IMF_API int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown); 845 int32_t UpdateListenEventFlag(uint32_t finalEventFlag, uint32_t eventFlag, bool isOn); 846 847 /** 848 * @brief Send private command to ime. 849 * 850 * This function is used to send private command to ime. 851 * 852 * @param privateCommand Indicates the private command which will be send. 853 * @return Returns 0 for success, others for failure. 854 * @since 12 855 */ 856 IMF_API int32_t SendPrivateCommand( 857 const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override; 858 859 /** 860 * @brief Receive private command from ime. 861 * 862 * This function is used to receive private command from ime. 863 * 864 * @param privateCommand Indicates the private command which send from ime. 865 * @return Returns 0 for success, others for failure. 866 * @since 12 867 */ 868 int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override; 869 870 /** 871 * @brief Set preview text. 872 * 873 * This function is used to set preview text. 874 * 875 * @param text Indicates the text to be previewed. 876 * @param range Indicates the range of text to be replaced. 877 * @return Returns 0 for success, others for failure. 878 * @since 12 879 */ 880 int32_t SetPreviewText(const std::string &text, const Range &range); 881 882 /** 883 * @brief Finish text preview. 884 * 885 * This function is used to finish text preview. 886 * 887 * @since 12 888 */ 889 int32_t FinishTextPreview(); 890 891 /** 892 * @brief Query whether an process id current inputmethod. 893 * 894 * This function is used to query whether an process id is inputmethod. 895 * 896 * @param type Indicates current process id. 897 * @return Returns true for current ime process id, false for not current ime process id. 898 * @since 12 899 */ 900 IMF_API bool IsCurrentImeByPid(int32_t pid); 901 902 /** 903 * @brief Reset controller. 904 * 905 * This function is used to reset controller. 906 * Do not call this interface unless you know what you are doing 907 * 908 * @since 12 909 */ 910 IMF_API void Reset(); 911 912 /** 913 * @brief Query whether the default input method is setting. 914 * 915 * This function is used to query whether the default input method is setting. 916 * Do not call this interface unless you know what you are doing 917 * 918 * @since 13 919 */ 920 IMF_API bool IsDefaultImeSet(); 921 922 /** 923 * @brief Enable the ime called bundleName. 924 * 925 * This function is used to enable the ime called bundleName. 926 * Do not call this interface unless you know what you are doing 927 * 928 * @since 13 929 */ 930 IMF_API int32_t EnableIme(const std::string &bundleName, const std::string &extensionName = "", 931 EnabledStatus status = EnabledStatus::BASIC_MODE); 932 933 /** 934 * @brief Send ArrayBuffer message to ime. 935 * 936 * This function is used to Send ArrayBuffer message to ime. 937 * 938 * @param arrayBuffer Indicates the ArrayBuffer message that will be send to the ime. 939 * The member msgId limit 256B, and member msgParam limit 128KB. 940 * @return Returns 0 for success, others for failure. 941 * @since 16 942 */ 943 IMF_API int32_t SendMessage(const ArrayBuffer &arrayBuffer); 944 int32_t RecvMessage(const ArrayBuffer &arrayBuffer); 945 946 /** 947 * @brief Regist message handler callback. 948 * 949 * This function is used to register a message handler to receive message from ime, or remove message handler. 950 * 951 * @param msgHandler Indicates the message handler callback. 952 * If nullptr will remove callback that was registered and trigger function OnTerminated. 953 * @return Returns 0 for success, others for failure. 954 * @since 16 955 */ 956 IMF_API int32_t RegisterMsgHandler(const std::shared_ptr<MsgHandlerCallbackInterface> &msgHandler = nullptr); 957 958 /** 959 * @brief Get ime state. 960 * 961 * This function is used to get it's enabled state by ime. 962 * 963 * @param status Indicates the enabled state 964 * @return Returns 0 for success, others for failure. 965 * @since 16 966 */ 967 IMF_API int32_t GetInputMethodState(EnabledStatus &status); 968 969 IMF_API void ReportBaseTextOperation(int32_t eventCode, int32_t errCode); 970 971 IMF_API void UpdateTextPreviewState(bool isSupport); 972 973 /** 974 * @brief Send Private Data. 975 * 976 * This function only available special service apply. 977 * 978 * @param privateCommand Indicates the private data information. 979 * @return Returns 0 for success, others for failure. 980 * @since 18 981 */ 982 IMF_API int32_t SendPrivateData(const std::unordered_map<std::string, PrivateDataValue> &privateCommand); 983 984 /** 985 * @brief Registration callbacks are used for coordinate transformation processing in window zooming scenarios. 986 * 987 * This function only available special service apply. 988 * 989 * @param callback Indicates the window zooming handle callback 990 * @return Returns 0 for success, others for failure. 991 * @since 18 992 */ 993 IMF_API int32_t RegisterWindowScaleCallbackHandler(WindowScaleCallback&& callback); 994 995 void HandleKeyEventResult(uint64_t cbId, bool consumeResult); 996 997 #ifdef OHOS_IMF_TEST 998 void SetImsaProxyForTest(sptr<IInputMethodSystemAbility> proxy); 999 #endif // OHOS_IMF_TEST 1000 private: 1001 InputMethodController(); 1002 ~InputMethodController(); 1003 1004 int32_t Initialize(); 1005 sptr<IInputMethodSystemAbility> GetSystemAbilityProxy(bool ifRetry = true); 1006 sptr<IInputMethodSystemAbility> TryGetSystemAbilityProxy(); 1007 void RemoveDeathRecipient(); 1008 int32_t StartInput( 1009 InputClientInfo &inputClientInfo, std::vector<sptr<IRemoteObject>> &agents, std::vector<BindImeInfo> &imeInfos); 1010 int32_t ShowInput( 1011 sptr<IInputClient> &client, ClientType type = ClientType::INNER_KIT, int32_t requestKeyboardReason = 0); 1012 int32_t HideInput(sptr<IInputClient> &client); 1013 int32_t ReleaseInput(sptr<IInputClient> &client); 1014 int32_t ListInputMethodCommon(InputMethodStatus status, std::vector<Property> &props); 1015 void ClearEditorCache(bool isNewEditor, sptr<OnTextChangedListener> lastListener); 1016 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 1017 void RestoreListenInfoInSaDied(); 1018 void RestoreClientInfoInSaDied(); 1019 int32_t RestoreListenEventFlag(); 1020 void SaveTextConfig(const TextConfig &textConfig); 1021 sptr<OnTextChangedListener> GetTextListener(); 1022 void SetTextListener(sptr<OnTextChangedListener> listener); 1023 bool IsEditable(); 1024 bool IsBound(); 1025 void SetAgent(const sptr<IRemoteObject> &agentObject, const std::string &bundleName); 1026 std::shared_ptr<IInputMethodAgent> GetAgent(); 1027 void PrintLogIfAceTimeout(int64_t start); 1028 void PrintKeyEventLog(); 1029 std::shared_ptr<MsgHandlerCallbackInterface> GetMsgHandlerCallback(); 1030 int32_t IsValidTextConfig(const TextConfig &textConfig); 1031 void SetBindImeInfo(const std::pair<int64_t, std::string> &imeInfo); 1032 std::pair<int64_t, std::string> GetBindImeInfo(); 1033 int32_t SetPreviewTextInner(const std::string &text, const Range &range); 1034 int32_t ShowTextInputInner(const AttachOptions &attachOptions, ClientType type); 1035 int32_t ShowSoftKeyboardInner(ClientType type); 1036 void ReportClientShow(int32_t eventCode, int32_t errCode, ClientType type); 1037 void GetWindowScaleCoordinate(uint32_t windowId, CursorInfo &cursorInfo); 1038 int32_t ResponseDataChannel( 1039 const sptr<IRemoteObject> &agentObject, uint64_t msgId, int32_t code, const ResponseData &data); 1040 void CalibrateImmersiveParam(InputAttribute &inputAttribute); 1041 void ClearAgentInfo(); 1042 int32_t SendRequestToAllAgents(std::function<int32_t(std::shared_ptr<IInputMethodAgent>)> task); 1043 int32_t SendRequestToImeMirrorAgent(std::function<int32_t(std::shared_ptr<IInputMethodAgent>)> task); 1044 void SetInputReady(const std::vector<sptr<IRemoteObject>> &agentObjects, const std::vector<BindImeInfo> &imeInfos); 1045 1046 friend class InputDataChannelServiceImpl; 1047 std::shared_ptr<ControllerListener> controllerListener_; 1048 std::mutex abilityLock_; 1049 sptr<IInputMethodSystemAbility> abilityManager_ = nullptr; 1050 sptr<InputDeathRecipient> deathRecipient_; 1051 1052 struct AgentInfo { 1053 sptr<IRemoteObject> agentObject = nullptr; 1054 std::shared_ptr<IInputMethodAgent> agent = nullptr; 1055 ImeType imeType = ImeType::NONE; 1056 }; 1057 std::mutex agentLock_; 1058 std::vector<AgentInfo> agentInfoList_; 1059 1060 std::mutex textListenerLock_; 1061 sptr<OnTextChangedListener> textListener_ = nullptr; 1062 std::atomic_bool isDiedAttached_{ false }; 1063 1064 std::mutex cursorInfoMutex_; 1065 CursorInfo cursorInfo_; 1066 1067 std::atomic_bool isTextNotified_{ false }; 1068 std::mutex editorContentLock_; 1069 std::u16string textString_; 1070 int selectOldBegin_ = 0; 1071 int selectOldEnd_ = 0; 1072 int selectNewBegin_ = 0; 1073 int selectNewEnd_ = 0; 1074 1075 static std::mutex instanceLock_; 1076 static sptr<InputMethodController> instance_; 1077 static std::shared_ptr<AppExecFwk::EventHandler> handler_; 1078 1079 static std::mutex logLock_; 1080 static int keyEventCountInPeriod_; 1081 static std::chrono::system_clock::time_point startLogTime_; 1082 1083 std::atomic_bool isEditable_{ false }; 1084 std::atomic_bool isBound_{ false }; 1085 std::atomic_bool bootCompleted_{ false }; 1086 1087 std::recursive_mutex clientInfoLock_; 1088 InputClientInfo clientInfo_; 1089 1090 static constexpr int CURSOR_DIRECTION_BASE_VALUE = 2011; 1091 std::atomic_bool isDiedRestoreListen_{ false }; 1092 1093 std::mutex textConfigLock_; 1094 TextConfig textConfig_; 1095 1096 struct KeyEventInfo { 1097 std::chrono::system_clock::time_point timestamp{}; 1098 std::shared_ptr<MMI::KeyEvent> keyEvent; 1099 bool operator==(const KeyEventInfo &info) const 1100 { 1101 return (timestamp == info.timestamp && keyEvent == info.keyEvent); 1102 } 1103 }; 1104 static constexpr int32_t MAX_WAIT_TIME = 5000; 1105 BlockQueue<KeyEventInfo> keyEventQueue_{ MAX_WAIT_TIME }; 1106 1107 std::mutex msgHandlerMutex_; 1108 std::shared_ptr<MsgHandlerCallbackInterface> msgHandler_ = nullptr; 1109 std::mutex bindImeInfoLock_; 1110 std::pair<int64_t, std::string> bindImeInfo_{ 0, "" }; // for hiSysEvent 1111 std::atomic_uint32_t sessionId_ { 0 }; 1112 1113 std::mutex windowScaleCallbackMutex_; 1114 WindowScaleCallback windowScaleCallback_ = nullptr; 1115 KeyEventResultHandler keyEventRetHandler_; 1116 }; 1117 } // namespace MiscServices 1118 } // namespace OHOS 1119 #endif // FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_METHOD_CONTROLLER_H 1120