1 /* 2 * Copyright (C) 2021 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 FM_IMMS_PROJECT_INPUTMETHODABILITY_H 17 #define FM_IMMS_PROJECT_INPUTMETHODABILITY_H 18 19 #include <thread> 20 #include "js_input_method_engine_listener.h" 21 #include "js_keyboard_delegate_listener.h" 22 #include "iremote_object.h" 23 #include "i_input_control_channel.h" 24 #include "i_input_method_core.h" 25 #include "i_input_data_channel.h" 26 #include "i_input_method_agent.h" 27 #include "input_method_core_stub.h" 28 #include "input_control_channel_proxy.h" 29 #include "input_attribute.h" 30 #include "message_handler.h" 31 #include "input_channel.h" 32 #include "input_data_channel_proxy.h" 33 #include "message.h" 34 #include "input_method_system_ability_proxy.h" 35 36 namespace OHOS { 37 namespace MiscServices { 38 class JsInputMethodEngineListener; 39 class MessageHandler; 40 class InputMethodAbility : public RefBase { 41 public: 42 InputMethodAbility(); 43 ~InputMethodAbility(); 44 static sptr<InputMethodAbility> GetInstance(); 45 sptr<IInputMethodCore> OnConnect(); 46 bool InsertText(const std::string text); 47 void setImeListener(sptr<JsInputMethodEngineListener> &imeListener); 48 void setKdListener(sptr<JsKeyboardDelegateListener> &kdListener); 49 void DeleteForward(int32_t length); 50 void DeleteBackward(int32_t length); 51 void HideKeyboardSelf(); 52 std::u16string GetTextBeforeCursor(int32_t number); 53 std::u16string GetTextAfterCursor(int32_t number); 54 void SendFunctionKey(int32_t funcKey); 55 void MoveCursor(int32_t keyCode); 56 bool DispatchKeyEvent(int32_t keyCode, int32_t keyStatus); 57 void SetCallingWindow(uint32_t windowId); 58 int32_t GetEnterKeyType(); 59 int32_t GetInputPattern(); 60 void StopInput(); 61 62 private: 63 std::thread workThreadHandler; 64 MessageHandler *msgHandler; 65 bool mSupportPhysicalKbd = false; 66 InputAttribute *editorAttribute; 67 int32_t displyId = 0; 68 sptr<IRemoteObject> startInputToken; 69 InputChannel *writeInputChannel; 70 bool stop_; 71 int32_t KEYBOARD_HIDE = 1; 72 int32_t KEYBOARD_SHOW = 2; 73 bool isBindClient = false; 74 75 std::mutex controlChannelLock_; 76 std::shared_ptr<InputControlChannelProxy> controlChannel_ = nullptr; 77 void SetCoreAndAgent(); 78 79 std::mutex dataChannelLock_; 80 std::shared_ptr<InputDataChannelProxy> dataChannel_ = nullptr; 81 sptr<JsInputMethodEngineListener> imeListener_; 82 sptr<JsKeyboardDelegateListener> kdListener_; 83 static std::mutex instanceLock_; 84 static sptr<InputMethodAbility> instance_; 85 86 struct ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 87 sptr<JsInputMethodEngineListener> listener{ nullptr }; 88 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 89 }; 90 sptr<ServiceDeathRecipient> deathRecipientPtr_{ nullptr }; 91 sptr<InputMethodSystemAbilityProxy> mImms; 92 sptr<InputMethodSystemAbilityProxy> GetImsaProxy(); 93 94 void SetInputDataChannel(sptr<IRemoteObject> &object); 95 std::shared_ptr<InputDataChannelProxy> GetInputDataChannel(); 96 void SetInputControlChannel(sptr<IRemoteObject> &object); 97 std::shared_ptr<InputControlChannelProxy> GetInputControlChannel(); 98 99 void Initialize(); 100 void WorkThread(); 101 void OnInitialInput(Message *msg); 102 void OnStartInput(Message *msg); 103 void OnStopInput(Message *msg); 104 void OnSetKeyboardType(Message *msg); 105 void OnShowKeyboard(Message *msg); 106 void OnHideKeyboard(Message *msg); 107 void OnInitInputControlChannel(Message *msg); 108 109 void OnCursorUpdate(Message *msg); 110 void OnSelectionChange(Message *msg); 111 112 void InitialInputWindow(); 113 void ShowInputWindow(); 114 void DissmissInputWindow(); 115 }; 116 } 117 } 118 #endif // FM_IMMS_PROJECT_INPUTMETHODABILITY_H