• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_SHORTCUT_MANAGER_H
17 #define KEY_SHORTCUT_MANAGER_H
18 
19 #include <cJSON.h>
20 
21 #include "key_command_handler.h"
22 #include "key_option.h"
23 
24 namespace OHOS {
25 namespace MMI {
26 enum KeyShortcutError : int32_t {
27     KEY_SHORTCUT_ERROR_BASE = -1,
28     KEY_SHORTCUT_ERROR_CONFIG = (KEY_SHORTCUT_ERROR_BASE - 1),
29     KEY_SHORTCUT_ERROR_COMBINATION_KEY = (KEY_SHORTCUT_ERROR_BASE - 2),
30     KEY_SHORTCUT_ERROR_TAKEN = (KEY_SHORTCUT_ERROR_BASE - 3),
31 };
32 
33 class KeyShortcutManager final {
34 public:
35     static inline constexpr uint32_t SHORTCUT_MODIFIER_ALT { 0x1 };
36     static inline constexpr uint32_t SHORTCUT_MODIFIER_SHIFT { SHORTCUT_MODIFIER_ALT << 1U };
37     static inline constexpr uint32_t SHORTCUT_MODIFIER_CTRL { SHORTCUT_MODIFIER_ALT << 2U };
38     static inline constexpr uint32_t SHORTCUT_MODIFIER_LOGO { SHORTCUT_MODIFIER_ALT << 3U };
39     static inline constexpr uint32_t SHORTCUT_MODIFIER_MASK {
40         SHORTCUT_MODIFIER_ALT | SHORTCUT_MODIFIER_SHIFT | SHORTCUT_MODIFIER_CTRL | SHORTCUT_MODIFIER_LOGO };
41     static inline constexpr int32_t SHORTCUT_PURE_MODIFIERS { -1 };
42     static inline constexpr int32_t MOUSE_BUTTON_LEFT { 25001 };
43     static inline constexpr int32_t MOUSE_BUTTON_RIGHT { MOUSE_BUTTON_LEFT + 1 };
44 
45     enum ShortcutTriggerType : int32_t {
46         SHORTCUT_TRIGGER_TYPE_DOWN = 0,
47         SHORTCUT_TRIGGER_TYPE_UP,
48     };
49 
50     struct SystemShortcutKey {
51         std::set<int32_t> modifiers;
52         int32_t finalKey;
53         int32_t longPressTime; // ms
54         ShortcutTriggerType triggerType;
55         int32_t session;
56         std::function<void(std::shared_ptr<KeyEvent>)> callback;
57     };
58 
59     struct HotKey {
60         std::set<int32_t> modifiers;
61         int32_t finalKey;
62         int32_t longPressTime; // ms
63         int32_t session;
64         std::function<void(std::shared_ptr<KeyEvent>)> callback;
65     };
66 
67     KeyShortcutManager();
68     ~KeyShortcutManager() = default;
69     DISALLOW_COPY_AND_MOVE(KeyShortcutManager);
70 
71     int32_t RegisterSystemKey(const SystemShortcutKey &key);
72     void UnregisterSystemKey(int32_t shortcutId);
73     int32_t RegisterHotKey(const HotKey &key);
74     void UnregisterHotKey(int32_t shortcutId);
75     bool HandleEvent(std::shared_ptr<KeyEvent> keyEvent);
76     void ResetAll();
77     int32_t GetAllSystemHotkeys(std::vector<std::unique_ptr<KeyOption>> &sysKeys);
78 
79     bool HaveShortcutConsumed(std::shared_ptr<KeyEvent> keyEvent);
80     void UpdateShortcutConsumed(std::shared_ptr<KeyEvent> keyEvent);
81     void MarkShortcutConsumed(const ShortcutKey &shortcut);
82     void MarkShortcutConsumed(const KeyOption &shortcut);
83     void ResetCheckState();
84     bool IsCheckUpShortcut(const std::shared_ptr<KeyEvent> &keyEvent);
85 
86     static std::shared_ptr<KeyShortcutManager> GetInstance();
87     static bool IsModifier(int32_t keyCode);
88 
89 private:
90     struct SystemKey {
91         uint32_t modifiers;
92         int32_t finalKey;
93 
94         bool operator<(const SystemKey &other) const;
95     };
96 
97     struct ExceptionalSystemKey {
98         std::set<int32_t> preKeys;
99         int32_t finalKey;
100         int32_t longPressTime; // ms
101         ShortcutTriggerType triggerType;
102 
103         bool operator<(const ExceptionalSystemKey &other) const;
104     };
105 
106     struct KeyShortcut {
107         uint32_t modifiers;
108         int32_t finalKey;
109         int32_t longPressTime; // ms
110         ShortcutTriggerType triggerType;
111         int32_t session;
112         std::function<void(std::shared_ptr<KeyEvent>)> callback;
113     };
114 
115     struct SystemHotkey {
116         std::set<int32_t> preKeys;
117         int32_t finalKey;
118 
119         bool operator<(const SystemHotkey &other) const;
120     };
121 
122     void LoadSystemKeys();
123     void ReadSystemKeys(const std::string &cfgPath);
124     int32_t ReadSystemKey(cJSON *jsonSysKey);
125     int32_t AddSystemKey(const std::set<int32_t> &preKeys, int32_t finalKey);
126     void LoadExceptionalSystemKeys();
127     void ReadExceptionalSystemKeys(const std::string &cfgPath);
128     int32_t ReadExceptionalSystemKey(cJSON *jsonSysKey);
129     void AddExceptionalSystemKey(const ExceptionalSystemKey &sysKey);
130     std::string FormatModifiers(const std::set<int32_t> &modifiers) const;
131     int32_t GenerateId() const;
132     bool IsExceptionalSystemKey(const ExceptionalSystemKey &sysKey) const;
133     bool CheckSystemKey(const SystemShortcutKey &key, KeyShortcut &shortcut) const;
134     bool IsValid(const ShortcutTriggerType triggerType) const;
135     bool IsReservedSystemKey(const KeyShortcut &shortcut) const;
136     bool CheckGlobalKey(const HotKey &key, KeyShortcut &shortcut) const;
137     bool HaveRegisteredGlobalKey(const KeyShortcut &key) const;
138     std::string FormatPressedKeys(std::shared_ptr<KeyEvent> keyEvent) const;
139     std::set<int32_t> GetForegroundPids() const;
140     bool HandleKeyDown(std::shared_ptr<KeyEvent> keyEvent);
141     bool HandleKeyUp(std::shared_ptr<KeyEvent> keyEvent);
142     bool HandleKeyCancel(std::shared_ptr<KeyEvent> keyEvent);
143     bool CheckCombination(std::shared_ptr<KeyEvent> keyEvent, const KeyShortcut &shortcut) const;
144     bool CheckPureModifiers(std::shared_ptr<KeyEvent> keyEvent, const KeyShortcut &shortcut) const;
145     bool CheckModifiers(std::shared_ptr<KeyEvent> keyEvent, const KeyShortcut &shortcut) const;
146     void TriggerDown(std::shared_ptr<KeyEvent> keyEvent, int32_t shortcutId, const KeyShortcut &shortcut);
147     void RunShortcut(std::shared_ptr<KeyEvent> keyEvent, int32_t shortcutId);
148     void TriggerUp(std::shared_ptr<KeyEvent> keyEvent, int32_t shortcutId, const KeyShortcut &shortcut);
149     void ResetTriggering(std::shared_ptr<KeyEvent> keyEvent);
150     bool WillResetOnKeyDown(int32_t keyCode, const KeyShortcut &shortcut) const;
151     bool WillResetOnKeyUp(int32_t keyCode, const KeyShortcut &shortcut) const;
152     void ResetTriggering(int32_t shortcutId);
153     void LoadHotkeys();
154     void ReadHotkeys(const std::string &cfgPath);
155     int32_t ReadHotkey(cJSON *jsonSysKey);
156     int32_t AddHotkey(const std::set<int32_t> &preKeys, int32_t finalKey);
157 
158     std::set<int32_t> shortcutConsumed_;
159     std::set<SystemKey> systemKeys_;
160     std::set<ExceptionalSystemKey> exceptSysKeys_;
161     std::map<int32_t, KeyShortcut> shortcuts_;
162     std::map<int32_t, int32_t> triggering_;
163     bool isCheckShortcut_ { true };
164     static const std::map<int32_t, uint32_t> modifiers_;
165     static std::mutex mutex_;
166     static std::shared_ptr<KeyShortcutManager> instance_;
167     std::set<SystemHotkey> hotkeys_;
168 };
169 
170 #define KEY_SHORTCUT_MGR KeyShortcutManager::GetInstance()
171 } // namespace MMI
172 } // namespace OHOS
173 #endif // KEY_SHORTCUT_MANAGER_H
174