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(const int32_t x,const int32_t y)43 void MouseDeviceState::SetMouseCoords(const int32_t x, const 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
GetMouseBtnState() const67 std::map<uint32_t, int32_t> MouseDeviceState::GetMouseBtnState() const
68 {
69 return mouseBtnState_;
70 }
71
MouseBtnStateCounts(uint32_t btnCode,const BUTTON_STATE btnState)72 void MouseDeviceState::MouseBtnStateCounts(uint32_t btnCode, const BUTTON_STATE btnState)
73 {
74 std::map<uint32_t, int32_t>::iterator iter = mouseBtnState_.find(btnCode);
75 if (iter == mouseBtnState_.end()) {
76 auto ret = mouseBtnState_.insert(std::make_pair(btnCode, ((btnState == BUTTON_STATE_PRESSED) ? 1 : 0)));
77 if (!ret.second) {
78 MMI_HILOGE("Insert value failed, btnCode:%{public}d", btnCode);
79 }
80 return;
81 }
82 ChangeMouseState(btnState, iter->second);
83 }
84
LibinputChangeToPointer(const uint32_t keyValue)85 int32_t MouseDeviceState::LibinputChangeToPointer(const uint32_t keyValue)
86 {
87 auto it = mapLibinputChangeToPointer.find(keyValue);
88 if (it == mapLibinputChangeToPointer.end()) {
89 return PointerEvent::BUTTON_NONE;
90 }
91 return it->second;
92 }
93
ChangeMouseState(const BUTTON_STATE btnState,int32_t & btnStateCount)94 void MouseDeviceState::ChangeMouseState(const BUTTON_STATE btnState, int32_t &btnStateCount)
95 {
96 if (btnState == BUTTON_STATE_PRESSED) {
97 btnStateCount++;
98 } else if (btnState == BUTTON_STATE_RELEASED) {
99 btnStateCount--;
100 }
101 if (btnStateCount > mouseBtnMax) {
102 btnStateCount = mouseBtnMax;
103 } else if (btnStateCount < 0) {
104 btnStateCount = 0;
105 }
106 }
107 } // namespace MMI
108 } // namespace OHOS