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