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_COMMAND_HANDLER_H 17 #define KEY_COMMAND_HANDLER_H 18 19 #include <chrono> 20 #include <condition_variable> 21 #include <functional> 22 #include <fstream> 23 #include <map> 24 #include <mutex> 25 #include <set> 26 #include <thread> 27 #include <vector> 28 29 #include "nocopyable.h" 30 31 #include "i_input_event_handler.h" 32 #include "key_event.h" 33 #include "struct_multimodal.h" 34 35 namespace OHOS { 36 namespace MMI { 37 struct Ability { 38 std::string bundleName; 39 std::string abilityName; 40 std::string action; 41 std::string type; 42 std::string deviceId; 43 std::string uri; 44 std::vector<std::string> entities; 45 std::map<std::string, std::string> params; 46 }; 47 48 struct ShortcutKey { 49 std::set<int32_t> preKeys; 50 int32_t finalKey { -1 }; 51 int32_t keyDownDuration { 0 }; 52 int32_t triggerType { KeyEvent::KEY_ACTION_DOWN }; 53 int32_t timerId { -1 }; 54 Ability ability; 55 void Print() const; 56 }; 57 58 struct SequenceKey { 59 int32_t keyCode { -1 }; 60 int32_t keyAction { 0 }; 61 int64_t actionTime { 0 }; 62 int64_t delay { 0 }; 63 bool operator!=(const SequenceKey &sequenceKey) 64 { 65 return (keyCode != sequenceKey.keyCode) || (keyAction != sequenceKey.keyAction); 66 } 67 }; 68 69 struct Sequence { 70 std::vector<SequenceKey> sequenceKeys; 71 int64_t abilityStartDelay { 0 }; 72 int32_t timerId { -1 }; 73 Ability ability; 74 }; 75 76 class KeyCommandHandler : public IInputEventHandler { 77 public: 78 KeyCommandHandler() = default; 79 DISALLOW_COPY_AND_MOVE(KeyCommandHandler); 80 ~KeyCommandHandler() = default; 81 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 82 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 83 #endif // OHOS_BUILD_ENABLE_KEYBOARD 84 #ifdef OHOS_BUILD_ENABLE_POINTER 85 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 86 #endif // OHOS_BUILD_ENABLE_POINTER 87 #ifdef OHOS_BUILD_ENABLE_TOUCH 88 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 89 #endif // OHOS_BUILD_ENABLE_TOUCH 90 bool OnHandleEvent(const std::shared_ptr<KeyEvent> keyEvent); 91 private: 92 void Print(); 93 void PrintSeq(); 94 bool ParseConfig(); 95 bool ParseJson(const std::string &configFile); 96 void LaunchAbility(const ShortcutKey &key); 97 void LaunchAbility(const Sequence &sequence); 98 bool IsKeyMatch(const ShortcutKey &shortcutKey, const std::shared_ptr<KeyEvent> &key); 99 bool IsRepeatKeyEvent(const SequenceKey &sequenceKey); 100 bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent, const ShortcutKey &shortcutKey); 101 bool HandleKeyDown(ShortcutKey &shortcutKey); 102 bool HandleKeyCancel(ShortcutKey &shortcutKey); 103 bool HandleSequence(Sequence& sequence, bool &isLaunchAbility); 104 bool HandleSequences(const std::shared_ptr<KeyEvent> keyEvent); 105 bool HandleShortKeys(const std::shared_ptr<KeyEvent> keyEvent); 106 bool AddSequenceKey(const std::shared_ptr<KeyEvent> keyEvent); 107 void RemoveSubscribedTimer(int32_t keyCode); 108 void HandleSpecialKeys(int32_t keyCode, int32_t keyAction); 109 void InterruptTimers(); ResetLastMatchedKey()110 void ResetLastMatchedKey() 111 { 112 lastMatchedKey_.preKeys.clear(); 113 lastMatchedKey_.finalKey = -1; 114 lastMatchedKey_.timerId = -1; 115 lastMatchedKey_.keyDownDuration = 0; 116 } ResetSequenceKeys()117 void ResetSequenceKeys() 118 { 119 keys_.clear(); 120 filterSequences_.clear(); 121 } 122 bool SkipFinalKey(const int32_t keyCode, const std::shared_ptr<KeyEvent> &key); 123 124 private: 125 ShortcutKey lastMatchedKey_; 126 std::map<std::string, ShortcutKey> shortcutKeys_; 127 std::vector<Sequence> sequences_; 128 std::vector<Sequence> filterSequences_; 129 std::vector<SequenceKey> keys_; 130 bool isParseConfig_ { false }; 131 std::map<int32_t, int32_t> specialKeys_; 132 std::map<int32_t, std::list<int32_t>> specialTimers_; 133 }; 134 } // namespace MMI 135 } // namespace OHOS 136 #endif // KEY_COMMAND_HANDLER_H