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 16 #ifndef KEY_SUBSCRIBER_HANDLER_H 17 #define KEY_SUBSCRIBER_HANDLER_H 18 19 #include <algorithm> 20 #include <list> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <set> 25 #include <thread> 26 27 #include "i_input_event_handler.h" 28 #include "key_event.h" 29 #include "key_option.h" 30 #include "uds_server.h" 31 32 namespace OHOS { 33 namespace MMI { 34 class KeySubscriberHandler final : public IInputEventHandler { 35 public: 36 KeySubscriberHandler() = default; 37 DISALLOW_COPY_AND_MOVE(KeySubscriberHandler); 38 ~KeySubscriberHandler() = default; 39 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 40 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 41 bool IsKeyEventSubscribed(int32_t keyCode, int32_t trrigerType); 42 #endif // OHOS_BUILD_ENABLE_KEYBOARD 43 #ifdef OHOS_BUILD_ENABLE_POINTER 44 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 45 #endif // OHOS_BUILD_ENABLE_POINTER 46 #ifdef OHOS_BUILD_ENABLE_TOUCH 47 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 48 #endif // OHOS_BUILD_ENABLE_TOUCH 49 int32_t SubscribeKeyEvent(SessionPtr sess, int32_t subscribeId, 50 const std::shared_ptr<KeyOption> keyOption); 51 int32_t UnsubscribeKeyEvent(SessionPtr sess, int32_t subscribeId); 52 void RemoveSubscriberKeyUpTimer(int32_t keyCode); 53 void Dump(int32_t fd, const std::vector<std::string> &args); 54 private: 55 struct Subscriber { SubscriberSubscriber56 Subscriber(int32_t id, SessionPtr sess, std::shared_ptr<KeyOption> keyOption) 57 : id_(id), sess_(sess), keyOption_(keyOption), timerId_(-1) {} 58 int32_t id_ { -1 }; 59 SessionPtr sess_ { nullptr }; 60 std::shared_ptr<KeyOption> keyOption_ { nullptr }; 61 int32_t timerId_ { -1 }; 62 std::shared_ptr<KeyEvent> keyEvent_ { nullptr }; 63 }; 64 void InsertSubScriber(std::shared_ptr<Subscriber> subs); 65 66 private: 67 bool OnSubscribeKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 68 bool HandleKeyDown(const std::shared_ptr<KeyEvent> &keyEvent); 69 bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent); 70 bool HandleKeyCancel(const std::shared_ptr<KeyEvent> &keyEvent); 71 bool IsPreKeysMatch(const std::set<int32_t> &preKeys, const std::vector<int32_t> &pressedKeys) const; 72 void NotifySubscriber(std::shared_ptr<KeyEvent> keyEvent, 73 const std::shared_ptr<Subscriber> &subscriber); 74 bool AddTimer(const std::shared_ptr<Subscriber> &subscriber, const std::shared_ptr<KeyEvent> &keyEvent); 75 void ClearTimer(const std::shared_ptr<Subscriber> &subscriber); 76 void OnTimer(const std::shared_ptr<Subscriber> subscriber); 77 void OnSessionDelete(SessionPtr sess); 78 bool InitSessionDeleteCallback(); 79 bool CloneKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 80 void RemoveKeyCode(int32_t keyCode, std::vector<int32_t> &keyCodes); 81 bool IsRepeatedKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 82 bool IsNotifyPowerKeySubsciber(int32_t keyCode, const std::vector<int32_t> &keyCodes); 83 void HandleKeyUpWithDelay(std::shared_ptr<KeyEvent> keyEvent, const std::shared_ptr<Subscriber> &subscriber); 84 void PrintKeyUpLog(const std::shared_ptr<Subscriber> &subscriber); 85 86 private: 87 std::list<std::shared_ptr<Subscriber>> subscribers_ {}; 88 bool callbackInitialized_ { false }; 89 bool hasEventExecuting_ { false }; 90 std::shared_ptr<KeyEvent> keyEvent_ { nullptr }; 91 }; 92 } // namespace MMI 93 } // namespace OHOS 94 #endif // KEY_SUBSCRIBER_HANDLER_H 95