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 namespace OHOS {
19 namespace MMI {
MouseDeviceState()20 MouseDeviceState::MouseDeviceState()
21 {
22 mouseCoord_ = {0, 0};
23 }
24
~MouseDeviceState()25 MouseDeviceState::~MouseDeviceState() {}
26
GetMouseCoordsX() const27 int32_t MouseDeviceState::GetMouseCoordsX() const
28 {
29 return mouseCoord_.globleX;
30 }
31
GetMouseCoordsY() const32 int32_t MouseDeviceState::GetMouseCoordsY() const
33 {
34 return mouseCoord_.globleY;
35 }
36
SetMouseCoords(const int32_t x,const int32_t y)37 void MouseDeviceState::SetMouseCoords(const int32_t x, const int32_t y)
38 {
39 mouseCoord_.globleX = x;
40 mouseCoord_.globleY = y;
41 }
42
IsLeftBtnPressed()43 bool MouseDeviceState::IsLeftBtnPressed()
44 {
45 std::lock_guard<std::mutex> lock(mu_);
46 auto iter = mouseBtnState_.find(LIBINPUT_LEFT_BUTTON_CODE);
47 if (iter == mouseBtnState_.end()) {
48 return false;
49 }
50 if (iter->second > 0) {
51 return true;
52 }
53 return false;
54 }
55
GetPressedButtons(std::vector<int32_t> & pressedButtons)56 void MouseDeviceState::GetPressedButtons(std::vector<int32_t>& pressedButtons)
57 {
58 std::lock_guard<std::mutex> lock(mu_);
59 for (const auto &item : mouseBtnState_) {
60 if (item.second > 0) {
61 pressedButtons.push_back(LibinputChangeToPointer(item.first));
62 }
63 }
64 }
65
GetMouseBtnState()66 std::map<uint32_t, int32_t> MouseDeviceState::GetMouseBtnState()
67 {
68 return mouseBtnState_;
69 }
70
MouseBtnStateCounts(uint32_t btnCode,const BUTTON_STATE btnState)71 void MouseDeviceState::MouseBtnStateCounts(uint32_t btnCode, const BUTTON_STATE btnState)
72 {
73 std::lock_guard<std::mutex> lock(mu_);
74 std::map<uint32_t, int32_t>::iterator iter = mouseBtnState_.find(btnCode);
75 if (iter == mouseBtnState_.end()) {
76 mouseBtnState_.insert(std::make_pair(btnCode, ((btnState == BUTTON_STATE_PRESSED) ? 1 : 0)));
77 return;
78 }
79 ChangeMouseState(btnState, iter->second);
80 }
81
LibinputChangeToPointer(const uint32_t keyValue)82 int32_t MouseDeviceState::LibinputChangeToPointer(const uint32_t keyValue)
83 {
84 auto it = mapLibinputChangeToPointer.find(keyValue);
85 if (it == mapLibinputChangeToPointer.end()) {
86 return PointerEvent::BUTTON_NONE;
87 }
88 return it->second;
89 }
90
ChangeMouseState(const BUTTON_STATE btnState,int32_t & btnStateCount)91 void MouseDeviceState::ChangeMouseState(const BUTTON_STATE btnState, int32_t &btnStateCount)
92 {
93 if (btnState == BUTTON_STATE_PRESSED) {
94 btnStateCount++;
95 } else if (btnState == BUTTON_STATE_RELEASED) {
96 btnStateCount--;
97 }
98 if (btnStateCount > mouseBtnMax) {
99 btnStateCount = mouseBtnMax;
100 } else if (btnStateCount < 0) {
101 btnStateCount = 0;
102 }
103 }
104 } // namespace MMI
105 } // namespace OHOS