1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef KEY_SUBSCRIBER_HANDLER_H 16 #define KEY_SUBSCRIBER_HANDLER_H 17 18 #include <algorithm> 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <set> 24 #include <thread> 25 26 #include "i_input_event_handler.h" 27 #include "key_event.h" 28 #include "key_option.h" 29 #include "uds_server.h" 30 31 namespace OHOS { 32 namespace MMI { 33 class KeySubscriberHandler : public IInputEventHandler { 34 public: 35 KeySubscriberHandler() = default; 36 DISALLOW_COPY_AND_MOVE(KeySubscriberHandler); 37 ~KeySubscriberHandler() = default; 38 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 39 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 40 #endif // OHOS_BUILD_ENABLE_KEYBOARD 41 #ifdef OHOS_BUILD_ENABLE_POINTER 42 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 43 #endif // OHOS_BUILD_ENABLE_POINTER 44 #ifdef OHOS_BUILD_ENABLE_TOUCH 45 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 46 #endif // OHOS_BUILD_ENABLE_TOUCH 47 int32_t SubscribeKeyEvent(SessionPtr sess, int32_t subscribeId, 48 const std::shared_ptr<KeyOption> keyOption); 49 int32_t UnsubscribeKeyEvent(SessionPtr sess, int32_t subscribeId); 50 void RemoveSubscriberKeyUpTimer(int32_t keyCode); 51 void Dump(int32_t fd, const std::vector<std::string> &args); 52 private: 53 struct Subscriber { SubscriberSubscriber54 Subscriber(int32_t id, SessionPtr sess, std::shared_ptr<KeyOption> keyOption) 55 : id_(id), sess_(sess), keyOption_(keyOption), timerId_(-1) {} 56 int32_t id_ { -1 }; 57 SessionPtr sess_ { nullptr }; 58 std::shared_ptr<KeyOption> keyOption_ { nullptr }; 59 int32_t timerId_ { -1 }; 60 std::shared_ptr<KeyEvent> keyEvent_ { nullptr }; 61 }; 62 void InsertSubScriber(std::shared_ptr<Subscriber> subs); 63 64 private: 65 bool OnSubscribeKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 66 bool HandleKeyDown(const std::shared_ptr<KeyEvent> &keyEvent); 67 bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent); 68 bool HandleKeyCancel(const std::shared_ptr<KeyEvent> &keyEvent); 69 bool IsPreKeysMatch(const std::set<int32_t> &preKeys, const std::vector<int32_t> &pressedKeys) const; 70 void NotifySubscriber(std::shared_ptr<KeyEvent> keyEvent, 71 const std::shared_ptr<Subscriber> &subscriber); 72 bool AddTimer(const std::shared_ptr<Subscriber> &subscriber, const std::shared_ptr<KeyEvent> &keyEvent); 73 void ClearTimer(const std::shared_ptr<Subscriber> &subscriber); 74 void OnTimer(const std::shared_ptr<Subscriber> subscriber); 75 void OnSessionDelete(SessionPtr sess); 76 bool InitSessionDeleteCallback(); 77 bool CloneKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 78 void RemoveKeyCode(int32_t keyCode, std::vector<int32_t> &keyCodes); 79 bool IsRepeatedKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 80 bool IsNotifyPowerKeySubsciber(int32_t keyCode, const std::vector<int32_t> &keyCodes); 81 void HandleKeyUpWithDelay(std::shared_ptr<KeyEvent> keyEvent, const std::shared_ptr<Subscriber> &subscriber); 82 void PrintKeyUpLog(const std::shared_ptr<Subscriber> &subscriber); 83 84 private: 85 std::list<std::shared_ptr<Subscriber>> subscribers_ {}; 86 bool callbackInitialized_ { false }; 87 bool hasEventExecuting_ { false }; 88 std::shared_ptr<KeyEvent> keyEvent_ { nullptr }; 89 }; 90 } // namespace MMI 91 } // namespace OHOS 92 #endif // KEY_SUBSCRIBER_HANDLER_H 93