1 /*
2  * Copyright (C) 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 #define private public
17 #define protected public
18 #include "pointer_event.h"
19 #undef private
20 #undef protected
21 #include "mock_pointer_event.h"
22 
23 namespace OHOS {
24 namespace MMI {
PointerItem()25 PointerEvent::PointerItem::PointerItem()
26 {}
27 
~PointerItem()28 PointerEvent::PointerItem::~PointerItem()
29 {}
30 
SetPointerId(int32_t pointerId)31 void PointerEvent::PointerItem::SetPointerId(int32_t pointerId)
32 {
33     pointerId_ = pointerId;
34 }
35 
GetPointerId() const36 int32_t PointerEvent::PointerItem::GetPointerId() const
37 {
38     return pointerId_;
39 }
40 
SetDownTime(int64_t downTime)41 void PointerEvent::PointerItem::SetDownTime(int64_t downTime)
42 {
43     downTime_ = downTime;
44 }
45 
GetDownTime() const46 int64_t PointerEvent::PointerItem::GetDownTime() const
47 {
48     return downTime_;
49 }
50 
SetPressed(bool pressed)51 void PointerEvent::PointerItem::SetPressed(bool pressed)
52 {
53     pressed_ = pressed;
54 }
55 
IsPressed() const56 bool PointerEvent::PointerItem::IsPressed() const
57 {
58     return pressed_;
59 }
60 
SetDisplayX(int32_t x)61 void PointerEvent::PointerItem::SetDisplayX(int32_t x)
62 {
63     displayX_ = x;
64 }
65 
GetDisplayX() const66 int32_t PointerEvent::PointerItem::GetDisplayX() const
67 {
68     return displayX_;
69 }
70 
SetDisplayY(int32_t y)71 void PointerEvent::PointerItem::SetDisplayY(int32_t y)
72 {
73     displayY_ = y;
74 }
75 
GetDisplayY() const76 int32_t PointerEvent::PointerItem::GetDisplayY() const
77 {
78     return displayY_;
79 }
80 
SetWindowX(int32_t x)81 void PointerEvent::PointerItem::SetWindowX(int32_t x)
82 {
83     windowX_ = x;
84 }
85 
GetWindowX() const86 int32_t PointerEvent::PointerItem::GetWindowX() const
87 {
88     return windowX_;
89 }
90 
SetWindowY(int32_t y)91 void PointerEvent::PointerItem::SetWindowY(int32_t y)
92 {
93     windowY_ = y;
94 }
95 
GetWindowY() const96 int32_t PointerEvent::PointerItem::GetWindowY() const
97 {
98     return windowY_;
99 }
100 
SetWidth(int32_t width)101 void PointerEvent::PointerItem::SetWidth(int32_t width)
102 {
103     width_ = width;
104 }
105 
GetWidth() const106 int32_t PointerEvent::PointerItem::GetWidth() const
107 {
108     return width_;
109 }
110 
SetHeight(int32_t height)111 void PointerEvent::PointerItem::SetHeight(int32_t height)
112 {
113     height_ = height;
114 }
115 
GetHeight() const116 int32_t PointerEvent::PointerItem::GetHeight() const
117 {
118     return height_;
119 }
120 
SetPressure(double pressure)121 void PointerEvent::PointerItem::SetPressure(double pressure)
122 {
123     pressure_ = pressure;
124 }
125 
GetPressure() const126 double PointerEvent::PointerItem::GetPressure() const
127 {
128     return pressure_;
129 }
130 
SetDeviceId(int32_t deviceId)131 void PointerEvent::PointerItem::SetDeviceId(int32_t deviceId)
132 {
133     deviceId_ = deviceId;
134 }
135 
GetDeviceId() const136 int32_t PointerEvent::PointerItem::GetDeviceId() const
137 {
138     return deviceId_;
139 }
140 
PointerEvent(int32_t eventType)141 PointerEvent::PointerEvent(int32_t eventType) : InputEvent(eventType)
142 {}
143 
PointerEvent(const PointerEvent & other)144 PointerEvent::PointerEvent(const PointerEvent& other)
145     : InputEvent(other),
146       pointerId_(other.pointerId_),
147       pointers_(other.pointers_),
148       pressedButtons_(other.pressedButtons_),
149       sourceType_(other.sourceType_),
150       pointerAction_(other.pointerAction_),
151       buttonId_(other.buttonId_),
152       axes_(other.axes_),
153       axisValues_(other.axisValues_),
154       pressedKeys_(other.pressedKeys_)
155 {}
156 
~PointerEvent()157 PointerEvent::~PointerEvent()
158 {
159     pointers_.clear();
160     pressedButtons_.clear();
161     pressedKeys_.clear();
162 }
163 
Create()164 std::shared_ptr<PointerEvent> PointerEvent::Create()
165 {
166     return std::make_shared<PointerEvent>(InputEvent::EVENT_TYPE_POINTER);
167 }
168 
GetSourceType() const169 int32_t PointerEvent::GetSourceType() const
170 {
171     return sourceType_;
172 }
173 
SetSourceType(int32_t sourceType)174 void PointerEvent::SetSourceType(int32_t sourceType)
175 {
176     sourceType_ = sourceType;
177 }
178 
GetPointerAction() const179 int32_t PointerEvent::GetPointerAction() const
180 {
181     return pointerAction_;
182 }
183 
SetPointerAction(int32_t pointerAction)184 void PointerEvent::SetPointerAction(int32_t pointerAction)
185 {
186     pointerAction_ = pointerAction;
187 }
188 
GetPointerId() const189 int32_t PointerEvent::GetPointerId() const
190 {
191     return pointerId_;
192 }
193 
SetPointerId(int32_t pointerId)194 void PointerEvent::SetPointerId(int32_t pointerId)
195 {
196     pointerId_ = pointerId;
197 }
198 
GetPointerItem(int32_t pointerId,PointerItem & pointerItem)199 bool PointerEvent::GetPointerItem(int32_t pointerId, PointerItem& pointerItem)
200 {
201     for (auto& item : pointers_) {
202         if (item.GetPointerId() == pointerId) {
203             pointerItem = item;
204             return true;
205         }
206     }
207     return false;
208 }
209 
RemovePointerItem(int32_t pointerId)210 void PointerEvent::RemovePointerItem(int32_t pointerId)
211 {
212     for (auto it = pointers_.begin(); it != pointers_.end(); it++) {
213         if (it->GetPointerId() == pointerId) {
214             pointers_.erase(it);
215             break;
216         }
217     }
218 }
219 
AddPointerItem(PointerItem & pointerItem)220 void PointerEvent::AddPointerItem(PointerItem& pointerItem)
221 {
222     pointers_.push_back(pointerItem);
223 }
224 
GetPointerIds() const225 std::vector<int32_t> PointerEvent::GetPointerIds() const
226 {
227     std::vector<int32_t> pointerIdList;
228 
229     for (auto& item : pointers_) {
230         pointerIdList.push_back(item.GetPointerId());
231     }
232 
233     return pointerIdList;
234 }
235 
Reset()236 void PointerEvent::Reset()
237 {
238 }
239 
SetButtonId(int32_t buttonId)240 void PointerEvent::SetButtonId(int32_t buttonId)
241 {
242     buttonId_ = buttonId;
243 }
244 
SetButtonPressed(int32_t buttonId)245 void PointerEvent::SetButtonPressed(int32_t buttonId)
246 {
247     pressedButtons_.insert(buttonId);
248 }
249 
UpdatePointerItem(int32_t pointerId,PointerItem & pointerItem)250 void PointerEvent::UpdatePointerItem(int32_t pointerId, PointerItem &pointerItem)
251 {
252     for (auto &item : pointers_) {
253         if (item.GetPointerId() == pointerId) {
254             item = pointerItem;
255             return;
256         }
257     }
258     pointers_.push_back(pointerItem);
259 }
260 } // namespace MMI
261 } // namespace OHOS