• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 #include "preferences.h"
31 #include "preferences_errno.h"
32 #include "preferences_helper.h"
33 #include "window_info.h"
34 
35 #include "i_input_event_handler.h"
36 #include "key_event.h"
37 #include "struct_multimodal.h"
38 
39 namespace OHOS {
40 namespace MMI {
41 enum KeyCommandType : int32_t {
42     TYPE_SHORTKEY = 0,
43     TYPE_SEQUENCE = 1,
44     TYPE_FINGERSCENE = 2,
45     TYPE_REPEAT_KEY = 3,
46     TYPE_MULTI_FINGERS = 4,
47 };
48 
49 enum class KnuckleType : int32_t {
50     KNUCKLE_TYPE_SINGLE = 0,
51     KNUCKLE_TYPE_DOUBLE = 1,
52 };
53 
54 enum class NotifyType : int32_t {
55     CANCEL,
56     INCONSISTENTGESTURE,
57     REGIONGESTURE,
58     LETTERGESTURE,
59     OTHER
60 };
61 
62 struct Ability {
63     std::string bundleName;
64     std::string abilityName;
65     std::string action;
66     std::string type;
67     std::string deviceId;
68     std::string uri;
69     std::string abilityType;
70     std::vector<std::string> entities;
71     std::map<std::string, std::string> params;
72 };
73 
74 struct ShortcutKey {
75     std::set<int32_t> preKeys;
76     std::string businessId;
77     std::string statusConfig;
78     bool statusConfigValue { true };
79     int32_t finalKey { -1 };
80     int32_t keyDownDuration { 0 };
81     int32_t triggerType { KeyEvent::KEY_ACTION_DOWN };
82     int32_t timerId { -1 };
83     Ability ability;
84     void Print() const;
85 };
86 
87 struct SequenceKey {
88     int32_t keyCode { -1 };
89     int32_t keyAction { 0 };
90     int64_t actionTime { 0 };
91     int64_t delay { 0 };
92     bool operator!=(const SequenceKey &sequenceKey)
93     {
94         return (keyCode != sequenceKey.keyCode) || (keyAction != sequenceKey.keyAction);
95     }
96 };
97 
98 struct ExcludeKey {
99     int32_t keyCode { -1 };
100     int32_t keyAction { -1 };
101     int64_t delay { 0 };
102 };
103 
104 struct Sequence {
105     std::vector<SequenceKey> sequenceKeys;
106     std::string statusConfig;
107     bool statusConfigValue { true };
108     int64_t abilityStartDelay { 0 };
109     int32_t timerId { -1 };
110     Ability ability;
111     friend std::ostream& operator<<(std::ostream&, const Sequence&);
112 };
113 
114 struct TwoFingerGesture {
115     inline static constexpr auto MAX_TOUCH_NUM = 2;
116     bool active = false;
117     int32_t timerId = -1;
118     int64_t abilityStartDelay = 0;
119     Ability ability;
120     struct {
121         int32_t id { 0 };
122         int32_t x { 0 };
123         int32_t y { 0 };
124         int64_t downTime { 0 };
125     } touches[MAX_TOUCH_NUM];
126 };
127 
128 struct KnuckleGesture {
129     std::shared_ptr<PointerEvent> lastPointerDownEvent { nullptr };
130     bool state { false };
131     int64_t lastPointerUpTime { 0 };
132     int64_t downToPrevUpTime { 0 };
133     float doubleClickDistance { 0.0f };
134     Ability ability;
135     struct {
136         int32_t id { 0 };
137         int32_t x { 0 };
138         int32_t y { 0 };
139     } lastDownPointer;
140 };
141 
142 struct RepeatKey {
143     int32_t keyCode { -1 };
144     int32_t keyAction { 0 };
145     int32_t times { 0 };
146     int64_t actionTime { 0 };
147     int64_t delay { 0 };
148     std::string statusConfig;
149     bool statusConfigValue { true };
150     Ability ability;
151 };
152 
153 struct MultiFingersTap {
154     Ability ability;
155 };
156 
157 struct KnuckleSwitch {
158     std::string statusConfig { "" };
159     bool statusConfigValue { false };
160 };
161 
162 class KeyCommandHandler final : public IInputEventHandler {
163 public:
164     KeyCommandHandler() = default;
165     DISALLOW_COPY_AND_MOVE(KeyCommandHandler);
166     ~KeyCommandHandler() override = default;
167     int32_t UpdateSettingsXml(const std::string &businessId, int32_t delay);
168     int32_t EnableCombineKey(bool enable);
169     KnuckleGesture GetSingleKnuckleGesture() const;
170     KnuckleGesture GetDoubleKnuckleGesture() const;
171     void Dump(int32_t fd, const std::vector<std::string> &args);
172     void PrintGestureInfo(int32_t fd);
173     std::string ConvertKeyActionToString(int32_t keyAction);
174 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
175     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
176 #endif // OHOS_BUILD_ENABLE_KEYBOARD
177 #ifdef OHOS_BUILD_ENABLE_POINTER
178     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
179 #endif // OHOS_BUILD_ENABLE_POINTER
180 #ifdef OHOS_BUILD_ENABLE_TOUCH
181     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
182     void HandlePointerActionDownEvent(const std::shared_ptr<PointerEvent> touchEvent);
183     void HandlePointerActionMoveEvent(const std::shared_ptr<PointerEvent> touchEvent);
184     void HandlePointerActionUpEvent(const std::shared_ptr<PointerEvent> touchEvent);
185 #endif // OHOS_BUILD_ENABLE_TOUCH
186     void SetKnuckleDoubleTapIntervalTime(int64_t interval);
187     void SetKnuckleDoubleTapDistance(float distance);
188     bool GetKnuckleSwitchValue();
189     bool CheckInputMethodArea(const std::shared_ptr<PointerEvent> touchEvent);
190 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
191     bool OnHandleEvent(const std::shared_ptr<KeyEvent> keyEvent);
192     int32_t SetIsFreezePowerKey(const std::string pageName);
193 #endif // OHOS_BUILD_ENABLE_KEYBOARD
194 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
195     bool OnHandleEvent(const std::shared_ptr<PointerEvent> pointerEvent);
196 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
197     void InitKeyObserver();
198 private:
199     void Print();
200     void PrintSeq();
201     void PrintExcludeKeys();
202     bool ParseConfig();
203     bool ParseExcludeConfig();
204     bool ParseJson(const std::string &configFile);
205     bool ParseExcludeJson(const std::string &configFile);
206     void ParseRepeatKeyMaxCount();
207     void ParseStatusConfigObserver();
208     void LaunchAbility(const Ability &ability);
209     void LaunchAbility(const Ability &ability, int64_t delay);
210     void LaunchAbility(const ShortcutKey &key);
211     void LaunchAbility(const Sequence &sequence);
212     void LaunchRepeatKeyAbility(const RepeatKey &item, bool &isLaunched, const std::shared_ptr<KeyEvent> keyEvent);
213     bool IsKeyMatch(const ShortcutKey &shortcutKey, const std::shared_ptr<KeyEvent> &key);
214     bool IsRepeatKeyEvent(const SequenceKey &sequenceKey);
215     bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent, const ShortcutKey &shortcutKey);
216     bool HandleKeyDown(ShortcutKey &shortcutKey);
217     bool HandleKeyCancel(ShortcutKey &shortcutKey);
218     bool PreHandleEvent(const std::shared_ptr<KeyEvent> key);
219     bool HandleEvent(const std::shared_ptr<KeyEvent> key);
220     bool HandleKeyUpCancel(const RepeatKey &item, const std::shared_ptr<KeyEvent> keyEvent);
221     bool HandleRepeatKeyCount(const RepeatKey &item, const std::shared_ptr<KeyEvent> keyEvent);
222     void HandleRepeatKeyOwnCount(const RepeatKey &item);
223     bool HandleRepeatKey(const RepeatKey& item, bool &isLaunchAbility, const std::shared_ptr<KeyEvent> keyEvent);
224     bool HandleRepeatKeys(const std::shared_ptr<KeyEvent> keyEvent);
225     bool HandleRepeatKeyAbility(const RepeatKey &item, bool &isLaunched,
226         const std::shared_ptr<KeyEvent> keyEvent, bool isMaxTimes);
227     bool HandleSequence(Sequence& sequence, bool &isLaunchAbility);
228     bool HandleNormalSequence(Sequence& sequence, bool &isLaunchAbility);
229     bool HandleMatchedSequence(Sequence& sequence, bool &isLaunchAbility);
230     bool HandleScreenLocked(Sequence& sequence, bool &isLaunchAbility);
231     bool IsActiveSequenceRepeating(std::shared_ptr<KeyEvent> keyEvent) const;
232     void MarkActiveSequence(bool active);
233     bool HandleSequences(const std::shared_ptr<KeyEvent> keyEvent);
234     bool HandleShortKeys(const std::shared_ptr<KeyEvent> keyEvent);
235     bool HandleConsumedKeyEvent(const std::shared_ptr<KeyEvent> keyEvent);
236     bool HandleMulFingersTap(const std::shared_ptr<PointerEvent> pointerEvent);
237     bool AddSequenceKey(const std::shared_ptr<KeyEvent> keyEvent);
238     std::shared_ptr<KeyEvent> CreateKeyEvent(int32_t keyCode, int32_t keyAction, bool isPressed);
239     bool IsEnableCombineKey(const std::shared_ptr<KeyEvent> key);
240     bool IsExcludeKey(const std::shared_ptr<KeyEvent> key);
241     void RemoveSubscribedTimer(int32_t keyCode);
242     void HandleSpecialKeys(int32_t keyCode, int32_t keyAction);
243     void InterruptTimers();
244     void HandlePointerVisibleKeys(const std::shared_ptr<KeyEvent> &keyEvent);
245     int32_t GetKeyDownDurationFromXml(const std::string &businessId);
246     void SendKeyEvent();
247     bool CheckSpecialRepeatKey(RepeatKey& item, const std::shared_ptr<KeyEvent> keyEvent);
248     template <class T>
249     void CreateStatusConfigObserver(T& item);
ResetLastMatchedKey()250     void ResetLastMatchedKey()
251     {
252         lastMatchedKey_.preKeys.clear();
253         lastMatchedKey_.finalKey = -1;
254         lastMatchedKey_.timerId = -1;
255         lastMatchedKey_.keyDownDuration = 0;
256     }
ResetCurrentLaunchAbilityKey()257     void ResetCurrentLaunchAbilityKey()
258     {
259         currentLaunchAbilityKey_.preKeys.clear();
260         currentLaunchAbilityKey_.finalKey = -1;
261         currentLaunchAbilityKey_.timerId = -1;
262         currentLaunchAbilityKey_.keyDownDuration = 0;
263     }
264 
ResetSequenceKeys()265     void ResetSequenceKeys()
266     {
267         keys_.clear();
268         filterSequences_.clear();
269     }
270     bool SkipFinalKey(const int32_t keyCode, const std::shared_ptr<KeyEvent> &key);
271 #ifdef OHOS_BUILD_ENABLE_TOUCH
272     void OnHandleTouchEvent(const std::shared_ptr<PointerEvent> touchEvent);
273 #endif // OHOS_BUILD_ENABLE_TOUCH
274 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
275     void StartTwoFingerGesture();
276     void StopTwoFingerGesture();
277     bool CheckTwoFingerGestureAction() const;
278 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
279 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
280     void HandleFingerGestureDownEvent(const std::shared_ptr<PointerEvent> touchEvent);
281     void HandleFingerGestureUpEvent(const std::shared_ptr<PointerEvent> touchEvent);
282     void HandleKnuckleGestureDownEvent(const std::shared_ptr<PointerEvent> touchEvent);
283     void HandleKnuckleGestureUpEvent(const std::shared_ptr<PointerEvent> touchEvent);
284     std::pair<int32_t, int32_t> CalcDrawCoordinate(const DisplayInfo& displayInfo,
285         PointerEvent::PointerItem pointerItem);
286     void SingleKnuckleGestureProcesser(const std::shared_ptr<PointerEvent> touchEvent);
287     void DoubleKnuckleGestureProcesser(const std::shared_ptr<PointerEvent> touchEvent);
288     void ReportKnuckleScreenCapture(const std::shared_ptr<PointerEvent> touchEvent);
289     void KnuckleGestureProcessor(std::shared_ptr<PointerEvent> touchEvent,
290         KnuckleGesture &knuckleGesture, KnuckleType type);
291     void UpdateKnuckleGestureInfo(const std::shared_ptr<PointerEvent> touchEvent, KnuckleGesture &knuckleGesture);
292     void AdjustTimeIntervalConfigIfNeed(int64_t intervalTime);
293     void AdjustDistanceConfigIfNeed(float distance);
294     bool CheckKnuckleCondition(std::shared_ptr<PointerEvent> touchEvent);
295 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
296 #ifdef OHOS_BUILD_ENABLE_TOUCH
297     int32_t ConvertVPToPX(int32_t vp) const;
298 #endif // OHOS_BUILD_ENABLE_TOUCH
299 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
300     void HandleKnuckleGestureEvent(std::shared_ptr<PointerEvent> touchEvent);
301     void HandleKnuckleGestureTouchDown(std::shared_ptr<PointerEvent> touchEvent);
302     void HandleKnuckleGestureTouchMove(std::shared_ptr<PointerEvent> touchEvent);
303     void HandleKnuckleGestureTouchUp(std::shared_ptr<PointerEvent> touchEvent);
304     void ProcessKnuckleGestureTouchUp(NotifyType type);
305     void ResetKnuckleGesture();
306     std::string GesturePointsToStr() const;
307     bool IsValidAction(int32_t action);
308     void ReportIfNeed();
309     void ReportRegionGesture();
310     void ReportLetterGesture();
311     void ReportGestureInfo();
312     bool IsMatchedAbility(std::vector<float> gesturePoints_, float gestureLastX, float gestureLastY);
313 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
314     void CheckAndUpdateTappingCountAtDown(std::shared_ptr<PointerEvent> touchEvent);
315 
316 private:
317     Sequence matchedSequence_;
318     ShortcutKey lastMatchedKey_;
319     ShortcutKey currentLaunchAbilityKey_;
320     std::map<std::string, ShortcutKey> shortcutKeys_;
321     std::vector<Sequence> sequences_;
322     std::vector<ExcludeKey> excludeKeys_;
323     std::vector<Sequence> filterSequences_;
324     std::vector<SequenceKey> keys_;
325     std::vector<RepeatKey> repeatKeys_;
326     std::vector<std::string> businessIds_;
327     bool isParseConfig_ { false };
328     bool isParseExcludeConfig_ { false };
329     std::map<int32_t, int32_t> specialKeys_;
330     std::map<int32_t, std::list<int32_t>> specialTimers_;
331     std::map<int32_t, int32_t> repeatKeyMaxTimes_;
332     std::map<std::string, int32_t> repeatKeyTimerIds_;
333     std::map<std::string, int32_t> repeatKeyCountMap_;
334     TwoFingerGesture twoFingerGesture_;
335     KnuckleGesture singleKnuckleGesture_;
336     KnuckleGesture doubleKnuckleGesture_;
337     MultiFingersTap threeFingersTap_;
338     bool isTimeConfig_ { false };
339     bool isDistanceConfig_ { false };
340     bool isKnuckleSwitchConfig_ { false };
341     struct KnuckleSwitch knuckleSwitch_;
342     int32_t checkAdjustIntervalTimeCount_ { 0 };
343     int32_t checkAdjustDistanceCount_ { 0 };
344     int64_t downToPrevUpTimeConfig_ { 0 };
345     float downToPrevDownDistanceConfig_ { 0.0f };
346     float distanceDefaultConfig_ { 0.0f };
347     float distanceLongConfig_ { 0.0f };
348     bool enableCombineKey_ { true };
349     RepeatKey repeatKey_;
350     int32_t maxCount_ { 0 };
351     int32_t count_ { 0 };
352     int32_t repeatTimerId_ { -1 };
353     int32_t knuckleCount_ { 0 };
354     int32_t sosDelayTimerId_ { -1 };
355     int64_t downActionTime_ { 0 };
356     int64_t lastDownActionTime_ { 0 };
357     int64_t lastVolumeDownActionTime_ { 0 };
358     int64_t upActionTime_ { 0 };
359     int32_t launchAbilityCount_ { 0 };
360     int64_t intervalTime_ { 120000 };
361     std::atomic<bool> isFreezePowerKey_ { false };
362     bool isDownStart_ { false };
363     bool isKeyCancel_ { false };
364     bool sequenceOccurred_ { false };
365     bool isHandleSequence_ { false };
366     bool isParseMaxCount_ { false };
367     bool isParseStatusConfig_ { false };
368     bool isDoubleClick_ { false };
369     int32_t lastKeyEventCode_ { -1 };
370     int32_t screenRecordingSuccessCount_ { 0 };
371     std::string sessionKey_ { };
372     bool isStartBase_ { false };
373 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
374     bool isGesturing_ { false };
375     bool isLetterGesturing_ { false };
376     bool isLastGestureSucceed_ { false };
377     float gestureLastX_ { 0.0f };
378     float gestureLastY_ { 0.0f };
379     float gestureTrackLength_ { 0.0f };
380     std::vector<float> gesturePoints_;
381     std::vector<int64_t> gestureTimeStamps_;
382     int64_t drawOFailTimestamp_ { 0 };
383     int64_t drawOSuccTimestamp_ { 0 };
384     Direction lastDirection_ { DIRECTION0 };
385 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
386     int64_t lastDownTime_ { 0 };
387     int64_t previousUpTime_ { 0 };
388     int32_t tappingCount_ { 0 };
389     std::map<int32_t, int64_t> lastPointerDownTime_;
390     std::mutex mutex_;
391     int64_t walletLaunchDelayTimes_ { 0 };
392 };
393 } // namespace MMI
394 } // namespace OHOS
395 #endif // KEY_COMMAND_HANDLER_H