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