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 #include "mouse_device_state.h"
17
18 #include "define_multimodal.h"
19
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "MouseDeviceState" };
24 } // namespace
25
MouseDeviceState()26 MouseDeviceState::MouseDeviceState()
27 {
28 mouseCoord_ = { 0, 0 };
29 }
30
~MouseDeviceState()31 MouseDeviceState::~MouseDeviceState() {}
32
GetMouseCoordsX() const33 int32_t MouseDeviceState::GetMouseCoordsX() const
34 {
35 return mouseCoord_.physicalX;
36 }
37
GetMouseCoordsY() const38 int32_t MouseDeviceState::GetMouseCoordsY() const
39 {
40 return mouseCoord_.physicalY;
41 }
42
SetMouseCoords(int32_t x,int32_t y)43 void MouseDeviceState::SetMouseCoords(int32_t x, int32_t y)
44 {
45 mouseCoord_.physicalX = x;
46 mouseCoord_.physicalY = y;
47 }
48
IsLeftBtnPressed()49 bool MouseDeviceState::IsLeftBtnPressed()
50 {
51 auto iter = mouseBtnState_.find(LIBINPUT_LEFT_BUTTON_CODE);
52 if (iter == mouseBtnState_.end()) {
53 return false;
54 }
55 return iter->second > 0;
56 }
57
GetPressedButtons(std::vector<int32_t> & pressedButtons)58 void MouseDeviceState::GetPressedButtons(std::vector<int32_t>& pressedButtons)
59 {
60 for (const auto &item : mouseBtnState_) {
61 if (item.second > 0) {
62 pressedButtons.push_back(LibinputChangeToPointer(item.first));
63 }
64 }
65 }
66
MouseBtnStateCounts(uint32_t btnCode,const BUTTON_STATE btnState)67 void MouseDeviceState::MouseBtnStateCounts(uint32_t btnCode, const BUTTON_STATE btnState)
68 {
69 std::map<uint32_t, int32_t>::iterator iter = mouseBtnState_.find(btnCode);
70 if (iter == mouseBtnState_.end()) {
71 auto ret = mouseBtnState_.insert(std::make_pair(btnCode, ((btnState == BUTTON_STATE_PRESSED) ? 1 : 0)));
72 if (!ret.second) {
73 MMI_HILOGE("Insert value failed, btnCode:%{public}d", btnCode);
74 }
75 return;
76 }
77 ChangeMouseState(btnState, iter->second);
78 }
79
LibinputChangeToPointer(const uint32_t keyValue)80 int32_t MouseDeviceState::LibinputChangeToPointer(const uint32_t keyValue)
81 {
82 auto it = mapLibinputChangeToPointer.find(keyValue);
83 if (it == mapLibinputChangeToPointer.end()) {
84 return PointerEvent::BUTTON_NONE;
85 }
86 return it->second;
87 }
88
ChangeMouseState(const BUTTON_STATE btnState,int32_t & btnStateCount)89 void MouseDeviceState::ChangeMouseState(const BUTTON_STATE btnState, int32_t &btnStateCount)
90 {
91 if (btnState == BUTTON_STATE_PRESSED) {
92 btnStateCount++;
93 } else if (btnState == BUTTON_STATE_RELEASED) {
94 btnStateCount--;
95 }
96 if (btnStateCount > mouseBtnMax) {
97 btnStateCount = mouseBtnMax;
98 } else if (btnStateCount < 0) {
99 btnStateCount = 0;
100 }
101 }
102 } // namespace MMI
103 } // namespace OHOS