• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MOUSE_DEVICE_STATE_H
16 #define MOUSE_DEVICE_STATE_H
17 
18 #include <map>
19 #include <mutex>
20 #include "pointer_event.h"
21 #include "struct_multimodal.h"
22 #include "singleton.h"
23 
24 namespace OHOS {
25 namespace MMI {
26 class MouseDeviceState : public DelayedSingleton<MouseDeviceState> {
27 public:
28     const int32_t mouseBtnMax = 8; // 鼠标按键最多为8个
29     enum LIBINPUT_BUTTON_CODE {
30         LIBINPUT_LEFT_BUTTON_CODE = 272,
31         LIBINPUT_RIGHT_BUTTON_CODE,
32         LIBINPUT_MIDDLE_BUTTON_CODE,
33         LIBINPUT_SIDE_BUTTON_CODE,
34         LIBINPUT_EXTRA_BUTTON_CODE,
35         LIBINPUT_FORWARD_BUTTON_CODE,
36         LIBINPUT_BACK_BUTTON_CODE,
37         LIBINPUT_TASK_BUTTON_CODE
38     };
39     struct MouseDeviceCoords {
40         int32_t globleX;
41         int32_t globleY;
42     };
43     const std::map<uint32_t, int32_t> mapLibinputChangeToPointer = {
44         {LIBINPUT_LEFT_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_LEFT},
45         {LIBINPUT_RIGHT_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_RIGHT},
46         {LIBINPUT_MIDDLE_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_MIDDLE},
47     };
48 public:
49     MouseDeviceState();
50     ~MouseDeviceState();
51 
52     int32_t GetMouseCoordsX() const;
53     int32_t GetMouseCoordsY() const;
54     void SetMouseCoords(const int32_t x, const int32_t y);
55     bool IsLeftBtnPressed();
56     void GetPressedButtons(std::vector<int32_t>& pressedButtons);
57     std::map<uint32_t, int32_t> GetMouseBtnState();
58     void MouseBtnStateCounts(uint32_t btnCode, const BUTTON_STATE btnState);
59 
60 private:
61     int32_t LibinputChangeToPointer(const uint32_t keyValue);
62     void ChangeMouseState(const BUTTON_STATE btnState, int32_t& btnStateCount);
63 
64 private:
65     std::mutex mu_;
66     MouseDeviceCoords mouseCoord_;
67     std::map<uint32_t, int32_t> mouseBtnState_;
68 };
69 } // namespace MMI
70 } // namespace OHOS
71 
72 #define MouseState OHOS::MMI::MouseDeviceState::GetInstance()
73 #endif // MOUSE_DEVICE_STATE_H