1 /*
2 * Copyright (c) 2025 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 #include <iostream>
17 #include <thread>
18 #include <chrono>
19 #include "ability_manager_client.h"
20 #include "system_ability_definition.h"
21 #include "x_key_event_processor.h"
22 #include "input_event_handler.h"
23 #include "setting_datashare.h"
24 #include "pointer_event.h"
25 #include "account_manager.h"
26 #include "timer_manager.h"
27
28 #undef MMI_LOG_DOMAIN
29 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
30 #undef MMI_LOG_TAG
31 #define MMI_LOG_TAG "XKeyEventProcessor"
32
33 namespace OHOS {
34 namespace MMI {
35 #ifdef OHOS_BUILD_ENABLE_X_KEY
36 namespace {
37 const std::string X_KEY_DOUBLE_CLICK_ENABLE_KEY = "double_click_enable_status";
38 const std::string DOUBLE_CLICK_ENABLE_STATUS = "0";
39 const std::string X_KEY_APP_BUNDLE_NAME = "";
40 const std::string X_KEY_APP_ABILITY_NAME = "";
41 const std::string SETTINGS_DATA_SECURE_PRE_URI =
42 "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_";
43 const std::string SETTINGS_DATA_SECURE_POST_URI = "?Proxy=true";
44 const int32_t X_KEY_DOUBLE_CLICK_ENABLE_COUNT = 2;
45 constexpr int32_t DOUBLE_CLICK_DELAY { 300 };
46 constexpr int32_t LONG_PRESS_DELAY { 500 };
47
48 constexpr int32_t X_KEY_DOWN { 0 };
49 constexpr int32_t X_KEY_UP { 1 };
50 constexpr int32_t SINGLE_CLICK { 2 };
51 constexpr int32_t DOUBLE_CLICK { 3 };
52 constexpr int32_t LONG_PRESS { 4 };
53 }
54
XKeyEventProcessor()55 XKeyEventProcessor::XKeyEventProcessor()
56 {}
57
~XKeyEventProcessor()58 XKeyEventProcessor::~XKeyEventProcessor()
59 {}
60
IsXKeyEvent(struct libinput_event * event)61 bool XKeyEventProcessor::IsXKeyEvent(struct libinput_event* event)
62 {
63 CALL_DEBUG_ENTER;
64 CHKPF(event);
65 auto device = libinput_event_get_device(event);
66 CHKPF(device);
67 std::string name = libinput_device_get_name(device);
68 if (X_KEY_SOURCE_KEY != name) {
69 MMI_HILOGD("Not X-key");
70 return false;
71 }
72 return true;
73 }
74
HandleXKeyEvent(struct libinput_event * event)75 int32_t XKeyEventProcessor::HandleXKeyEvent(struct libinput_event* event)
76 {
77 CALL_DEBUG_ENTER;
78 CHKPR(event, ERROR_NULL_POINTER);
79 auto device = libinput_event_get_device(event);
80 CHKPR(device, PARAM_INPUT_INVALID);
81 std::string name = libinput_device_get_name(device);
82 if (X_KEY_SOURCE_KEY == name) {
83 return AnalyseKeyEvent(event);
84 }
85 return PARAM_INPUT_INVALID;
86 }
87
AnalyseKeyEvent(struct libinput_event * event)88 int32_t XKeyEventProcessor::AnalyseKeyEvent(struct libinput_event* event)
89 {
90 CALL_DEBUG_ENTER;
91 CHKPR(event, ERROR_NULL_POINTER);
92 struct libinput_event_keyboard* keyEvent = libinput_event_get_keyboard_event(event);
93 CHKPR(keyEvent, ERROR_NULL_POINTER);
94 auto keyCode = libinput_event_keyboard_get_key(keyEvent);
95 int32_t keyState = libinput_event_keyboard_get_key_state(keyEvent);
96 MMI_HILOGD("keyCode:%{private}d, keyState:%{private}d", keyCode, keyState);
97 int32_t keyAction = keyState == 0 ? KeyEvent::KEY_ACTION_UP : KeyEvent::KEY_ACTION_DOWN;
98 if (KeyEvent::KEY_ACTION_DOWN == keyAction) {
99 InterceptXKeyDown();
100 } else {
101 InterceptXKeyUp();
102 }
103 return ERR_OK;
104 }
105
InterceptXKeyDown()106 void XKeyEventProcessor::InterceptXKeyDown()
107 {
108 handledLongPress_ = false;
109 HandleQuickAccessMenu(X_KEY_DOWN);
110
111 if (pressCount_ == 0) {
112 StartLongPressTimer();
113 }
114
115 pressCount_++;
116 }
117
StartLongPressTimer()118 void XKeyEventProcessor::StartLongPressTimer()
119 {
120 MMI_HILOGD("start long press timer.");
121 longPressTimerId_ = TimerMgr->AddTimer(LONG_PRESS_DELAY, 1, [this] () {
122 if (this->pressCount_ == 1 && !this->handledLongPress_) {
123 HandleQuickAccessMenu(LONG_PRESS);
124 MMI_HILOGI("X-key is long press.");
125 }
126 }, "XKeyEventProcessor");
127 }
128
InterceptXKeyUp()129 void XKeyEventProcessor::InterceptXKeyUp()
130 {
131 handledLongPress_ = true;
132 HandleQuickAccessMenu(X_KEY_UP);
133 if (pressCount_ == 1) {
134 if (IsRemoveDelaySingleClick()) {
135 MMI_HILOGI("X-key is single click after remove delayed click.");
136 HandleQuickAccessMenu(SINGLE_CLICK);
137 return;
138 }
139 StartSingleClickTimer();
140 } else if (pressCount_ == X_KEY_DOUBLE_CLICK_ENABLE_COUNT) {
141 HandleQuickAccessMenu(DOUBLE_CLICK);
142 MMI_HILOGI("X-key is double click.");
143 }
144 }
145
IsRemoveDelaySingleClick()146 bool XKeyEventProcessor::IsRemoveDelaySingleClick()
147 {
148 std::string value = DOUBLE_CLICK_ENABLE_STATUS;
149 int32_t userId = ACCOUNT_MGR->GetCurrentAccountSetting().GetAccountId();
150 std::string uri = SETTINGS_DATA_SECURE_PRE_URI + std::to_string(userId) + SETTINGS_DATA_SECURE_POST_URI;
151 MMI_HILOGI("settings data uri:%{public}s", uri.c_str());
152 SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
153 .SettingDataShare::GetStringValue(X_KEY_DOUBLE_CLICK_ENABLE_KEY, value, uri);
154 MMI_HILOGI("double click enable state:%{public}s", value.c_str());
155 return value == DOUBLE_CLICK_ENABLE_STATUS;
156 }
157
StartSingleClickTimer()158 void XKeyEventProcessor::StartSingleClickTimer()
159 {
160 MMI_HILOGD("start single click timer.");
161 singleClickTimerId_ = TimerMgr->AddTimer(DOUBLE_CLICK_DELAY, 1, [this] () {
162 if (this->pressCount_ == 1) {
163 HandleQuickAccessMenu(SINGLE_CLICK);
164 MMI_HILOGI("X-key is single click.");
165 }
166 }, "XKeyEventProcessor");
167 }
168
RemoveTimer()169 void XKeyEventProcessor::RemoveTimer()
170 {
171 if (singleClickTimerId_ != -1) {
172 TimerMgr->RemoveTimer(singleClickTimerId_);
173 singleClickTimerId_ = -1;
174 }
175 if (longPressTimerId_ != -1) {
176 TimerMgr->RemoveTimer(longPressTimerId_);
177 longPressTimerId_ = -1;
178 }
179 }
180
ResetCount()181 void XKeyEventProcessor::ResetCount()
182 {
183 MMI_HILOGD("reset press count");
184 pressCount_ = 0;
185 }
186
HandleQuickAccessMenu(int32_t xKeyEventType)187 int32_t XKeyEventProcessor::HandleQuickAccessMenu(int32_t xKeyEventType)
188 {
189 if (X_KEY_DOWN != xKeyEventType && X_KEY_UP != xKeyEventType) {
190 StartXKeyIfNeeded(xKeyEventType);
191 ResetCount();
192 RemoveTimer();
193 }
194 auto pointerEvent = PointerEvent::Create();
195 CHKPR(pointerEvent, ERROR_NULL_POINTER);
196 pointerEvent->SetPointerAction(xKeyEventType);
197 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_X_KEY);
198 #if (defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)) && defined(OHOS_BUILD_ENABLE_MONITOR)
199 auto eventMonitorHandler_ = InputHandler->GetMonitorHandler();
200 if (eventMonitorHandler_ != nullptr) {
201 eventMonitorHandler_->OnHandleEvent(pointerEvent);
202 }
203 #endif // (OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH) && OHOS_BUILD_ENABLE_MONITOR
204 return RET_OK;
205 }
206
StartXKeyIfNeeded(int32_t xKeyEventType)207 void XKeyEventProcessor::StartXKeyIfNeeded(int32_t xKeyEventType)
208 {
209 if (!isStartedXKey_) {
210 isStartedXKey_ = true;
211 MMI_HILOGI("start x-key.");
212 AAFwk::Want want;
213 want.SetElementName(X_KEY_APP_BUNDLE_NAME, X_KEY_APP_ABILITY_NAME);
214 want.SetParam("xKeyEventType", xKeyEventType);
215 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
216 if (err != ERR_OK) {
217 MMI_HILOGI("start ability fail.");
218 }
219 }
220 }
221 #endif // OHOS_BUILD_ENABLE_X_KEY
222 } // namespace MMI
223 } // namespace OHOS