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 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 #include "preferences.h" 35 #include "preferences_impl.h" 36 #include "preferences_errno.h" 37 #include "preferences_helper.h" 38 #include "preferences_xml_utils.h" 39 40 namespace OHOS { 41 namespace MMI { 42 struct Ability { 43 std::string bundleName; 44 std::string abilityName; 45 std::string action; 46 std::string type; 47 std::string deviceId; 48 std::string uri; 49 std::string abilityType; 50 std::vector<std::string> entities; 51 std::map<std::string, std::string> params; 52 }; 53 54 struct ShortcutKey { 55 std::set<int32_t> preKeys; 56 std::string businessId; 57 std::string statusConfig; 58 bool statusConfigValue { true }; 59 int32_t finalKey { -1 }; 60 int32_t keyDownDuration { 0 }; 61 int32_t triggerType { KeyEvent::KEY_ACTION_DOWN }; 62 int32_t timerId { -1 }; 63 Ability ability; 64 void Print() const; 65 }; 66 67 struct SequenceKey { 68 int32_t keyCode { -1 }; 69 int32_t keyAction { 0 }; 70 int64_t actionTime { 0 }; 71 int64_t delay { 0 }; 72 bool operator!=(const SequenceKey &sequenceKey) 73 { 74 return (keyCode != sequenceKey.keyCode) || (keyAction != sequenceKey.keyAction); 75 } 76 }; 77 78 struct Sequence { 79 std::vector<SequenceKey> sequenceKeys; 80 std::string statusConfig; 81 bool statusConfigValue { true }; 82 int64_t abilityStartDelay { 0 }; 83 int32_t timerId { -1 }; 84 Ability ability; 85 }; 86 87 struct TwoFingerGesture { 88 inline static constexpr auto MAX_TOUCH_NUM = 2; 89 bool active = false; 90 int32_t timerId = -1; 91 int64_t abilityStartDelay = 0; 92 Ability ability; 93 struct { 94 int32_t id { 0 }; 95 int32_t x { 0 }; 96 int32_t y { 0 }; 97 } touches[MAX_TOUCH_NUM]; 98 }; 99 100 struct KnuckleGesture { 101 std::shared_ptr<PointerEvent> lastPointerDownEvent { nullptr }; 102 bool state { false }; 103 int64_t lastPointerUpTime { 0 }; 104 int64_t downToPrevUpTime { 0 }; 105 float doubleClickDistance { 0.0f }; 106 Ability ability; 107 struct { 108 int32_t id { 0 }; 109 int32_t x { 0 }; 110 int32_t y { 0 }; 111 } lastDownPointer; 112 }; 113 114 struct MultiFingersTap { 115 Ability ability; 116 }; 117 118 struct RepeatKey { 119 int32_t keyCode { -1 }; 120 int32_t keyAction { 0 }; 121 int32_t times { 0 }; 122 int64_t actionTime { 0 }; 123 int64_t delay { 0 }; 124 std::string statusConfig; 125 bool statusConfigValue { true }; 126 Ability ability; 127 }; 128 129 class KeyCommandHandler final : public IInputEventHandler { 130 public: 131 KeyCommandHandler() = default; 132 DISALLOW_COPY_AND_MOVE(KeyCommandHandler); 133 ~KeyCommandHandler() override = default; 134 int32_t UpdateSettingsXml(const std::string &businessId, int32_t delay); 135 int32_t EnableCombineKey(bool enable); 136 KnuckleGesture GetSingleKnuckleGesture(); 137 KnuckleGesture GetDoubleKnuckleGesture(); 138 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 139 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 140 #endif // OHOS_BUILD_ENABLE_KEYBOARD 141 #ifdef OHOS_BUILD_ENABLE_POINTER 142 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 143 #endif // OHOS_BUILD_ENABLE_POINTER 144 #ifdef OHOS_BUILD_ENABLE_TOUCH 145 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 146 void HandlePointerActionDownEvent(const std::shared_ptr<PointerEvent> touchEvent); 147 void HandlePointerActionMoveEvent(const std::shared_ptr<PointerEvent> touchEvent); 148 void HandlePointerActionUpEvent(const std::shared_ptr<PointerEvent> touchEvent); 149 void SetKnuckleDoubleTapIntervalTime(int64_t interval); 150 void SetKnuckleDoubleTapDistance(float distance); 151 #endif // OHOS_BUILD_ENABLE_TOUCH 152 bool OnHandleEvent(const std::shared_ptr<KeyEvent> keyEvent); 153 bool OnHandleEvent(const std::shared_ptr<PointerEvent> pointerEvent); 154 155 #ifdef UNIT_TEST 156 public: 157 #else 158 private: 159 #endif 160 void Print(); 161 void PrintSeq(); 162 bool ParseConfig(); 163 bool ParseJson(const std::string &configFile); 164 void ParseRepeatKeyMaxCount(); 165 void ParseStatusConfigObserver(); 166 void LaunchAbility(const Ability &ability); 167 void LaunchAbility(const Ability &ability, int64_t delay); 168 void LaunchAbility(const ShortcutKey &key); 169 void LaunchAbility(const Sequence &sequence); 170 bool IsKeyMatch(const ShortcutKey &shortcutKey, const std::shared_ptr<KeyEvent> &key); 171 bool IsRepeatKeyEvent(const SequenceKey &sequenceKey); 172 bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent, const ShortcutKey &shortcutKey); 173 bool HandleKeyDown(ShortcutKey &shortcutKey); 174 bool HandleKeyCancel(ShortcutKey &shortcutKey); 175 bool HandleEvent(const std::shared_ptr<KeyEvent> key); 176 bool HandleKeyUpCancel(const RepeatKey &item, const std::shared_ptr<KeyEvent> keyEvent); 177 bool HandleRepeatKeyCount(const RepeatKey &item, const std::shared_ptr<KeyEvent> keyEvent); 178 bool HandleRepeatKey(const RepeatKey& item, bool &isLaunchAbility, const std::shared_ptr<KeyEvent> keyEvent); 179 bool HandleRepeatKeys(const std::shared_ptr<KeyEvent> keyEvent); 180 bool HandleSequence(Sequence& sequence, bool &isLaunchAbility); 181 bool HandleSequences(const std::shared_ptr<KeyEvent> keyEvent); 182 bool HandleShortKeys(const std::shared_ptr<KeyEvent> keyEvent); 183 bool HandleConsumedKeyEvent(const std::shared_ptr<KeyEvent> keyEvent); 184 bool HandleMulFingersTap(const std::shared_ptr<PointerEvent> pointerEvent); 185 bool AddSequenceKey(const std::shared_ptr<KeyEvent> keyEvent); 186 std::shared_ptr<KeyEvent> CreateKeyEvent(int32_t keyCode, int32_t keyAction, bool isPressed); 187 bool IsEnableCombineKey(const std::shared_ptr<KeyEvent> key); 188 void RemoveSubscribedTimer(int32_t keyCode); 189 void HandleSpecialKeys(int32_t keyCode, int32_t keyAction); 190 void InterruptTimers(); 191 int32_t GetKeyDownDurationFromXml(const std::string &businessId); 192 void SendKeyEvent(); 193 template <class T> 194 void CreateStatusConfigObserver(T& item); ResetLastMatchedKey()195 void ResetLastMatchedKey() 196 { 197 lastMatchedKey_.preKeys.clear(); 198 lastMatchedKey_.finalKey = -1; 199 lastMatchedKey_.timerId = -1; 200 lastMatchedKey_.keyDownDuration = 0; 201 } ResetCurrentLaunchAbilityKey()202 void ResetCurrentLaunchAbilityKey() 203 { 204 currentLaunchAbilityKey_.preKeys.clear(); 205 currentLaunchAbilityKey_.finalKey = -1; 206 currentLaunchAbilityKey_.timerId = -1; 207 currentLaunchAbilityKey_.keyDownDuration = 0; 208 } 209 ResetSequenceKeys()210 void ResetSequenceKeys() 211 { 212 keys_.clear(); 213 filterSequences_.clear(); 214 } 215 bool SkipFinalKey(const int32_t keyCode, const std::shared_ptr<KeyEvent> &key); 216 void OnHandleTouchEvent(const std::shared_ptr<PointerEvent> touchEvent); 217 void StartTwoFingerGesture(); 218 void StopTwoFingerGesture(); 219 #ifdef OHOS_BUILD_ENABLE_TOUCH 220 void HandleFingerGestureDownEvent(const std::shared_ptr<PointerEvent> touchEvent); 221 void HandleFingerGestureUpEvent(const std::shared_ptr<PointerEvent> touchEvent); 222 void HandleKnuckleGestureDownEvent(const std::shared_ptr<PointerEvent> touchEvent); 223 void HandleKnuckleGestureUpEvent(const std::shared_ptr<PointerEvent> touchEvent); 224 void SingleKnuckleGestureProcesser(const std::shared_ptr<PointerEvent> touchEvent); 225 void DoubleKnuckleGestureProcesser(const std::shared_ptr<PointerEvent> touchEvent); 226 void ReportKnuckleDoubleClickEvent(const std::shared_ptr<PointerEvent> touchEvent, KnuckleGesture &knuckleGesture); 227 void ReportKnuckleScreenCapture(const std::shared_ptr<PointerEvent> touchEvent); 228 void KnuckleGestureProcessor(const std::shared_ptr<PointerEvent> touchEvent, KnuckleGesture &knuckleGesture); 229 void UpdateKnuckleGestureInfo(const std::shared_ptr<PointerEvent> touchEvent, KnuckleGesture &knuckleGesture); 230 void AdjustTimeIntervalConfigIfNeed(int64_t intervalTime); 231 void AdjustDistanceConfigIfNeed(float distance); 232 #endif // OHOS_BUILD_ENABLE_TOUCH 233 234 private: 235 ShortcutKey lastMatchedKey_; 236 ShortcutKey currentLaunchAbilityKey_; 237 std::map<std::string, ShortcutKey> shortcutKeys_; 238 std::vector<Sequence> sequences_; 239 std::vector<Sequence> filterSequences_; 240 std::vector<SequenceKey> keys_; 241 std::vector<RepeatKey> repeatKeys_; 242 std::vector<std::string> businessIds_; 243 bool isParseConfig_ { false }; 244 std::map<int32_t, int32_t> specialKeys_; 245 std::map<int32_t, std::list<int32_t>> specialTimers_; 246 TwoFingerGesture twoFingerGesture_; 247 KnuckleGesture singleKnuckleGesture_; 248 KnuckleGesture doubleKnuckleGesture_; 249 MultiFingersTap threeFingersTap_; 250 bool isKnuckleState_ { false }; 251 bool isTimeConfig_ { false }; 252 bool isDistanceConfig_ { false }; 253 int32_t checkAdjustIntervalTimeCount_ { 0 }; 254 int32_t checkAdjustDistanceCount_ { 0 }; 255 int64_t downToPrevUpTimeConfig_ { 0 }; 256 float downToPrevDownDistanceConfig_ { 0.0f }; 257 float distanceDefaultConfig_ { 0.0f }; 258 float distanceLongConfig_ { 0.0f }; 259 bool enableCombineKey_ { true }; 260 RepeatKey repeatKey_; 261 int32_t maxCount_ { 0 }; 262 int32_t count_ { 0 }; 263 int32_t repeatTimerId_ { -1 }; 264 int64_t downActionTime_ { 0 }; 265 int64_t upActionTime_ { 0 }; 266 int32_t launchAbilityCount_ { 0 }; 267 int64_t intervalTime_ { 120000 }; 268 bool isDownStart_ { false }; 269 bool isKeyCancel_ { false }; 270 bool isHandleSequence_ { false }; 271 bool isParseMaxCount_ { false }; 272 bool isParseStatusConfig_ { false }; 273 bool isDoubleClick_ { false }; 274 }; 275 } // namespace MMI 276 } // namespace OHOS 277 #endif // KEY_COMMAND_HANDLER_H