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