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 #ifndef MOUSE_DEVICE_STATE_H
17 #define MOUSE_DEVICE_STATE_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "nocopyable.h"
23 #include "singleton.h"
24 
25 #include "pointer_event.h"
26 #include "struct_multimodal.h"
27 
28 namespace OHOS {
29 namespace MMI {
30 class MouseDeviceState final {
31     DECLARE_DELAYED_SINGLETON(MouseDeviceState);
32 public:
33     const int32_t mouseBtnMax = 8;
34     enum LIBINPUT_BUTTON_CODE {
35         LIBINPUT_LEFT_BUTTON_CODE = 272,
36         LIBINPUT_RIGHT_BUTTON_CODE,
37         LIBINPUT_MIDDLE_BUTTON_CODE,
38         LIBINPUT_SIDE_BUTTON_CODE,
39         LIBINPUT_EXTRA_BUTTON_CODE,
40         LIBINPUT_FORWARD_BUTTON_CODE,
41         LIBINPUT_BACK_BUTTON_CODE,
42         LIBINPUT_TASK_BUTTON_CODE
43     };
44     struct MouseDeviceCoords {
45         int32_t physicalX { 0 };
46         int32_t physicalY { 0 };
47     };
48     const std::map<uint32_t, int32_t> mapLibinputChangeToPointer = {
49         { LIBINPUT_LEFT_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_LEFT },
50         { LIBINPUT_RIGHT_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_RIGHT },
51         { LIBINPUT_MIDDLE_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_MIDDLE },
52         { LIBINPUT_SIDE_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_SIDE },
53         { LIBINPUT_EXTRA_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_EXTRA },
54         { LIBINPUT_FORWARD_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_FORWARD },
55         { LIBINPUT_BACK_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_BACK },
56         { LIBINPUT_TASK_BUTTON_CODE, PointerEvent::MOUSE_BUTTON_TASK },
57     };
58 public:
59     DISALLOW_COPY_AND_MOVE(MouseDeviceState);
60     int32_t GetMouseCoordsX() const;
61     int32_t GetMouseCoordsY() const;
62     void SetMouseCoords(int32_t x, int32_t y);
63     bool IsLeftBtnPressed();
64     void GetPressedButtons(std::vector<int32_t>& pressedButtons);
65     void MouseBtnStateCounts(uint32_t btnCode, const BUTTON_STATE btnState);
66     int32_t LibinputChangeToPointer(const uint32_t keyValue);
67 
68 private:
69     void ChangeMouseState(const BUTTON_STATE btnState, int32_t& btnStateCount);
70 
71 private:
72     MouseDeviceCoords mouseCoord_;
73     std::map<uint32_t, int32_t> mouseBtnState_;
74 };
75 
76 #define MouseState ::OHOS::DelayedSingleton<MouseDeviceState>::GetInstance()
77 } // namespace MMI
78 } // namespace OHOS
79 #endif // MOUSE_DEVICE_STATE_H