1/*
2 * Copyright (c) 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#include "input_event_callback.h"
17
18#include "global.h"
19
20namespace OHOS {
21namespace MiscServices {
22uint32_t InputEventCallback::keyState_ = static_cast<uint32_t>(0);
23bool InputEventCallback::isKeyHandled_ = false;
24const std::map<int32_t, uint8_t> MASK_MAP{
25    { MMI::KeyEvent::KEYCODE_SHIFT_LEFT, KeyboardEvent::SHIFT_LEFT_MASK },
26    { MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, KeyboardEvent::SHIFT_RIGHT_MASK },
27    { MMI::KeyEvent::KEYCODE_CTRL_LEFT, KeyboardEvent::CTRL_LEFT_MASK },
28    { MMI::KeyEvent::KEYCODE_CTRL_RIGHT, KeyboardEvent::CTRL_RIGHT_MASK },
29    { MMI::KeyEvent::KEYCODE_CAPS_LOCK, KeyboardEvent::CAPS_MASK },
30};
31
32void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
33{
34    auto keyCode = keyEvent->GetKeyCode();
35    auto keyAction = keyEvent->GetKeyAction();
36    auto currKey = MASK_MAP.find(keyCode);
37    if (currKey == MASK_MAP.end()) {
38        IMSA_HILOGD("key code is unknown.");
39        keyState_ = 0;
40        return;
41    }
42
43    if (keyAction == MMI::KeyEvent::KEY_ACTION_DOWN) {
44        IMSA_HILOGD("key pressed down.");
45        keyState_ = static_cast<uint32_t>(keyState_ | currKey->second);
46        if (keyCode == MMI::KeyEvent::KEYCODE_CAPS_LOCK) {
47            if (keyHandler_ != nullptr) {
48                int32_t ret = keyHandler_(keyState_);
49                IMSA_HILOGI("handle key event ret: %{public}d.", ret);
50            }
51            isKeyHandled_ = true;
52            return;
53        }
54        isKeyHandled_ = false;
55        return;
56    }
57
58    if (keyAction == MMI::KeyEvent::KEY_ACTION_UP) {
59        if (keyHandler_ != nullptr && !isKeyHandled_) {
60            int32_t ret = keyHandler_(keyState_);
61            IMSA_HILOGI("handle key event ret: %{public}d.", ret);
62        }
63        isKeyHandled_ = true;
64        keyState_ = static_cast<uint32_t>(keyState_ & ~currKey->second);
65    }
66}
67
68void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
69{
70}
71
72void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
73{
74}
75
76void InputEventCallback::SetKeyHandle(KeyHandle handle)
77{
78    keyHandler_ = std::move(handle);
79}
80
81void InputEventCallback::TriggerSwitch()
82{
83    auto state = KeyboardEvent::META_MASK | KeyboardEvent::SPACE_MASK;
84    int32_t ret = keyHandler_(state);
85    IMSA_HILOGI("handle combinationkey ret: %{public}d.", ret);
86}
87} // namespace MiscServices
88} // namespace OHOS