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