• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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::vector<std::string> entities;
50     std::map<std::string, std::string> params;
51 };
52 
53 struct ShortcutKey {
54     std::set<int32_t> preKeys;
55     std::string businessId;
56     int32_t finalKey { -1 };
57     int32_t keyDownDuration { 0 };
58     int32_t triggerType { KeyEvent::KEY_ACTION_DOWN };
59     int32_t timerId { -1 };
60     Ability ability;
61     void Print() const;
62 };
63 
64 struct SequenceKey {
65     int32_t keyCode { -1 };
66     int32_t keyAction { 0 };
67     int64_t actionTime { 0 };
68     int64_t delay { 0 };
69     bool operator!=(const SequenceKey &sequenceKey)
70     {
71         return (keyCode != sequenceKey.keyCode) || (keyAction != sequenceKey.keyAction);
72     }
73 };
74 
75 struct Sequence {
76     std::vector<SequenceKey> sequenceKeys;
77     int64_t abilityStartDelay { 0 };
78     int32_t timerId { -1 };
79     Ability ability;
80 };
81 
82 struct TwoFingerGesture {
83     inline static constexpr auto MAX_TOUCH_NUM = 2;
84     bool active = false;
85     int32_t timerId = -1;
86     int64_t abilityStartDelay = 0;
87     Ability ability;
88     struct {
89         int32_t id { 0 };
90         int32_t x { 0 };
91         int32_t y { 0 };
92     } touches[MAX_TOUCH_NUM];
93 };
94 
95 struct KnuckleGesture {
96     std::shared_ptr<PointerEvent> lastPointerDownEvent { nullptr };
97     int32_t state { 0 };
98     int64_t lastPointerUpTime { 0 };
99     int64_t downToPrevUpTime { 0 };
100     int32_t timerId { -1 };
101     Ability ability;
102 };
103 
104 class KeyCommandHandler final : public IInputEventHandler {
105 public:
106     KeyCommandHandler() = default;
107     DISALLOW_COPY_AND_MOVE(KeyCommandHandler);
108     ~KeyCommandHandler() override = default;
109     int32_t UpdateSettingsXml(const std::string &businessId, int32_t delay);
110     KnuckleGesture GetSingleKnuckleGesture();
111     KnuckleGesture GetDoubleKnuckleGesture();
112 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
113     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
114 #endif // OHOS_BUILD_ENABLE_KEYBOARD
115 #ifdef OHOS_BUILD_ENABLE_POINTER
116     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
117 #endif // OHOS_BUILD_ENABLE_POINTER
118 #ifdef OHOS_BUILD_ENABLE_TOUCH
119     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
120     void HandlePointerActionDownEvent(const std::shared_ptr<PointerEvent> &touchEvent);
121     void HandlePointerActionMoveEvent(const std::shared_ptr<PointerEvent> &touchEvent);
122     void HandlePointerActionUpEvent(const std::shared_ptr<PointerEvent> &touchEvent);
123 #endif // OHOS_BUILD_ENABLE_TOUCH
124     bool OnHandleEvent(const std::shared_ptr<KeyEvent> keyEvent);
125 private:
126     void Print();
127     void PrintSeq();
128     bool ParseConfig();
129     bool ParseJson(const std::string &configFile);
130     void LaunchAbility(const Ability &ability, int64_t delay);
131     void LaunchAbility(const ShortcutKey &key);
132     void LaunchAbility(const Sequence &sequence);
133     bool IsKeyMatch(const ShortcutKey &shortcutKey, const std::shared_ptr<KeyEvent> &key);
134     bool IsRepeatKeyEvent(const SequenceKey &sequenceKey);
135     bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent, const ShortcutKey &shortcutKey);
136     bool HandleKeyDown(ShortcutKey &shortcutKey);
137     bool HandleKeyCancel(ShortcutKey &shortcutKey);
138     bool HandleSequence(Sequence& sequence, bool &isLaunchAbility);
139     bool HandleSequences(const std::shared_ptr<KeyEvent> keyEvent);
140     bool HandleShortKeys(const std::shared_ptr<KeyEvent> keyEvent);
141     bool HandleConsumedKeyEvent(const std::shared_ptr<KeyEvent> keyEvent);
142     bool AddSequenceKey(const std::shared_ptr<KeyEvent> keyEvent);
143     void RemoveSubscribedTimer(int32_t keyCode);
144     void HandleSpecialKeys(int32_t keyCode, int32_t keyAction);
145     void InterruptTimers();
146     int32_t GetKeyDownDurationFromXml(const std::string &businessId);
ResetLastMatchedKey()147     void ResetLastMatchedKey()
148     {
149         lastMatchedKey_.preKeys.clear();
150         lastMatchedKey_.finalKey = -1;
151         lastMatchedKey_.timerId = -1;
152         lastMatchedKey_.keyDownDuration = 0;
153     }
ResetCurrentLaunchAbilityKey()154     void ResetCurrentLaunchAbilityKey()
155     {
156         currentLaunchAbilityKey_.preKeys.clear();
157         currentLaunchAbilityKey_.finalKey = -1;
158         currentLaunchAbilityKey_.timerId = -1;
159         currentLaunchAbilityKey_.keyDownDuration = 0;
160     }
ResetSequenceKeys()161     void ResetSequenceKeys()
162     {
163         keys_.clear();
164         filterSequences_.clear();
165     }
166     bool SkipFinalKey(const int32_t keyCode, const std::shared_ptr<KeyEvent> &key);
167     void OnHandleTouchEvent(const std::shared_ptr<PointerEvent>& touchEvent);
168     void StartTwoFingerGesture();
169     void StopTwoFingerGesture();
170 #ifdef OHOS_BUILD_ENABLE_TOUCH
171     void HandleFingerGestureDownEvent(const std::shared_ptr<PointerEvent> &touchEvent);
172     void HandleFingerGestureUpEvent(const std::shared_ptr<PointerEvent> &touchEvent);
173     void HandleKnuckleGestureDownEvent(const std::shared_ptr<PointerEvent> &touchEvent);
174     void HandleKnuckleGestureUpEvent(const std::shared_ptr<PointerEvent> &touchEvent);
175     void KnuckleGestureProcesser(const std::shared_ptr<PointerEvent> &touchEvent, KnuckleGesture &knuckleGesture);
176     void SingleKnuckleGestureProcesser(const std::shared_ptr<PointerEvent> &touchEvent);
177     void DoubleKnuckleGestureProcesser(const std::shared_ptr<PointerEvent> &touchEvent);
178 #endif // OHOS_BUILD_ENABLE_TOUCH
179 
180 private:
181     ShortcutKey lastMatchedKey_;
182     ShortcutKey currentLaunchAbilityKey_;
183     std::map<std::string, ShortcutKey> shortcutKeys_;
184     std::vector<Sequence> sequences_;
185     std::vector<Sequence> filterSequences_;
186     std::vector<SequenceKey> keys_;
187     std::vector<std::string> businessIds_;
188     bool isParseConfig_ { false };
189     std::map<int32_t, int32_t> specialKeys_;
190     std::map<int32_t, std::list<int32_t>> specialTimers_;
191     TwoFingerGesture twoFingerGesture_;
192     KnuckleGesture singleKnuckleGesture_;
193     KnuckleGesture doubleKnuckleGesture_;
194     bool isKnuckleState_ { false };
195 };
196 } // namespace MMI
197 } // namespace OHOS
198 #endif // KEY_COMMAND_HANDLER_H