Debian Codegraph @

xbmc/input/mouse/MouseStat.cpp

raw
1 /*
2 * Copyright (C) 2005-2024 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "MouseStat.h"
10
11 #include "ServiceBroker.h"
12 #include "input/keyboard/KeyIDs.h"
13 #include "utils/TimeUtils.h"
14 #include "windowing/WinSystem.h"
15
16 #include <algorithm>
17 #include <cstring>
18
19 CMouseStat::CMouseStat()
20 {
21 SetEnabled();
22 m_Key = KEY_MOUSE_NOOP;
23 }
24
25 CMouseStat::~CMouseStat() = default;
26
27 void CMouseStat::Initialize()
28 {
29 // Set the default resolution (PAL)
30 SetResolution(720, 576, 1, 1);
31 }
32
33 void CMouseStat::HandleEvent(const XBMC_Event& newEvent)
34 {
35 // Save the mouse position and the size of the last move
36 int dx, dy;
37 if (newEvent.type == XBMC_MOUSEMOTION)
38 {
39 dx = newEvent.motion.x - m_mouseState.x;
40 dy = newEvent.motion.y - m_mouseState.y;
41 }
42 else if (newEvent.type == XBMC_MOUSEBUTTONDOWN || newEvent.type == XBMC_MOUSEBUTTONUP)
43 {
44 dx = newEvent.button.x - m_mouseState.x;
45 dy = newEvent.button.y - m_mouseState.y;
46 }
47 else
48 {
49 return;
50 }
51 m_mouseState.dx = dx;
52 m_mouseState.dy = dy;
53 m_mouseState.x = std::max(0, std::min(m_maxX, m_mouseState.x + dx));
54 m_mouseState.y = std::max(0, std::min(m_maxY, m_mouseState.y + dy));
55
56 // Fill in the public members
57 if (newEvent.type == XBMC_MOUSEBUTTONDOWN)
58 {
59 if (newEvent.button.button == XBMC_BUTTON_LEFT)
60 m_mouseState.button[MOUSE_LEFT_BUTTON] = true;
61 if (newEvent.button.button == XBMC_BUTTON_RIGHT)
62 m_mouseState.button[MOUSE_RIGHT_BUTTON] = true;
63 if (newEvent.button.button == XBMC_BUTTON_MIDDLE)
64 m_mouseState.button[MOUSE_MIDDLE_BUTTON] = true;
65 if (newEvent.button.button == XBMC_BUTTON_X1)
66 m_mouseState.button[MOUSE_EXTRA_BUTTON1] = true;
67 if (newEvent.button.button == XBMC_BUTTON_X2)
68 m_mouseState.button[MOUSE_EXTRA_BUTTON2] = true;
69 if (newEvent.button.button == XBMC_BUTTON_X3)
70 m_mouseState.button[MOUSE_EXTRA_BUTTON3] = true;
71 if (newEvent.button.button == XBMC_BUTTON_X4)
72 m_mouseState.button[MOUSE_EXTRA_BUTTON4] = true;
73 if (newEvent.button.button == XBMC_BUTTON_WHEELUP)
74 m_mouseState.dz = 1;
75 if (newEvent.button.button == XBMC_BUTTON_WHEELDOWN)
76 m_mouseState.dz = -1;
77 }
78 else if (newEvent.type == XBMC_MOUSEBUTTONUP)
79 {
80 if (newEvent.button.button == XBMC_BUTTON_LEFT)
81 m_mouseState.button[MOUSE_LEFT_BUTTON] = false;
82 if (newEvent.button.button == XBMC_BUTTON_RIGHT)
83 m_mouseState.button[MOUSE_RIGHT_BUTTON] = false;
84 if (newEvent.button.button == XBMC_BUTTON_MIDDLE)
85 m_mouseState.button[MOUSE_MIDDLE_BUTTON] = false;
86 if (newEvent.button.button == XBMC_BUTTON_X1)
87 m_mouseState.button[MOUSE_EXTRA_BUTTON1] = false;
88 if (newEvent.button.button == XBMC_BUTTON_X2)
89 m_mouseState.button[MOUSE_EXTRA_BUTTON2] = false;
90 if (newEvent.button.button == XBMC_BUTTON_X3)
91 m_mouseState.button[MOUSE_EXTRA_BUTTON3] = false;
92 if (newEvent.button.button == XBMC_BUTTON_X4)
93 m_mouseState.button[MOUSE_EXTRA_BUTTON4] = false;
94 if (newEvent.button.button == XBMC_BUTTON_WHEELUP)
95 m_mouseState.dz = 0;
96 if (newEvent.button.button == XBMC_BUTTON_WHEELDOWN)
97 m_mouseState.dz = 0;
98 }
99
100 // Now check the current message and the previous state to find out if
101 // this is a click, doubleclick, drag etc
102 uint32_t now = CTimeUtils::GetFrameTime();
103 bool bNothingDown = true;
104
105 for (int i = 0; i < MOUSE_MAX_BUTTON; i++)
106 {
107 bClick[i] = false;
108 bLongClick[i] = false;
109 bDoubleClick[i] = false;
110 m_hold[i] = HoldAction::NONE;
111
112 // CButtonState::Update does the hard work of checking the button state
113 // and spotting drags, doubleclicks etc
114 CButtonState::BUTTON_ACTION action =
115 m_buttonState[i].Update(now, m_mouseState.x, m_mouseState.y, m_mouseState.button[i]);
116 switch (action)
117 {
118 case CButtonState::MB_SHORT_CLICK:
119 bClick[i] = true;
120 bNothingDown = false;
121 break;
122 case CButtonState::MB_LONG_CLICK:
123 bLongClick[i] = true;
124 bNothingDown = false;
125 break;
126 case CButtonState::MB_DOUBLE_CLICK:
127 bDoubleClick[i] = true;
128 bNothingDown = false;
129 break;
130 case CButtonState::MB_DRAG_START:
131 m_hold[i] = HoldAction::DRAG_START;
132 bNothingDown = false;
133 break;
134 case CButtonState::MB_DRAG:
135 m_hold[i] = HoldAction::DRAG;
136 bNothingDown = false;
137 break;
138 case CButtonState::MB_DRAG_END:
139 m_hold[i] = HoldAction::DRAG_END;
140 bNothingDown = false;
141 break;
142 default:
143 break;
144 }
145 }
146
147 // Now work out what action ID to send to XBMC.
148
149 // ignore any mouse messages by default
150 m_Key = KEY_MOUSE_NOOP;
151
152 for (int button = 0; button < MOUSE_MAX_BUTTON; ++button)
153 {
154 // The bClick array is set true if CButtonState::Update spots a click
155 // i.e. a button down followed by a button up.
156 if (bClick[button])
157 m_Key = KEY_MOUSE_CLICK + button;
158 // The bDoubleClick array is set true if CButtonState::Update spots a
159 // button down within double_click_time (500ms) of the last click
160 else if (bDoubleClick[button])
161 m_Key = KEY_MOUSE_DOUBLE_CLICK + button;
162 else if (bLongClick[button])
163 m_Key = KEY_MOUSE_LONG_CLICK + button;
164
165 if (m_Key != KEY_MOUSE_NOOP)
166 break;
167 }
168
169 if (m_Key == KEY_MOUSE_NOOP)
170 {
171 // The m_hold array is set to the drag action
172 if (m_hold[MOUSE_LEFT_BUTTON] != HoldAction::NONE)
173 {
174 switch (m_hold[MOUSE_LEFT_BUTTON])
175 {
176 case HoldAction::DRAG:
177 m_Key = KEY_MOUSE_DRAG;
178 break;
179 case HoldAction::DRAG_START:
180 m_Key = KEY_MOUSE_DRAG_START;
181 break;
182 case HoldAction::DRAG_END:
183 m_Key = KEY_MOUSE_DRAG_END;
184 break;
185 default:
186 break;
187 }
188 }
189 else if (m_hold[MOUSE_RIGHT_BUTTON] != HoldAction::NONE)
190 {
191 switch (m_hold[MOUSE_RIGHT_BUTTON])
192 {
193 case HoldAction::DRAG:
194 m_Key = KEY_MOUSE_RDRAG;
195 break;
196 case HoldAction::DRAG_START:
197 m_Key = KEY_MOUSE_RDRAG_START;
198 break;
199 case HoldAction::DRAG_END:
200 m_Key = KEY_MOUSE_RDRAG_END;
201 break;
202 default:
203 break;
204 }
205 }
206
207 // dz is +1 on wheel up and -1 on wheel down
208 else if (m_mouseState.dz > 0)
209 m_Key = KEY_MOUSE_WHEEL_UP;
210 else if (m_mouseState.dz < 0)
211 m_Key = KEY_MOUSE_WHEEL_DOWN;
212
213 // Check for a mouse move that isn't a drag, ignoring messages with no movement at all
214 else if (newEvent.type == XBMC_MOUSEMOTION && (m_mouseState.dx || m_mouseState.dy))
215 m_Key = KEY_MOUSE_MOVE;
216 }
217
218 // activate the mouse pointer if we have an action or the mouse has moved far enough
219 if ((MovedPastThreshold() && m_Key == KEY_MOUSE_MOVE) ||
220 (m_Key != KEY_MOUSE_NOOP && m_Key != KEY_MOUSE_MOVE))
221 SetActive();
222
223 // reset the mouse state if nothing is held down
224 if (bNothingDown)
225 SetState(MOUSE_STATE_NORMAL);
226 }
227
228 void CMouseStat::SetResolution(int maxX, int maxY, float speedX, float speedY)
229 {
230 m_maxX = maxX;
231 m_maxY = maxY;
232
233 // speed is currently unused
234 m_speedX = speedX;
235 m_speedY = speedY;
236 }
237
238 void CMouseStat::SetActive(bool active /*=true*/)
239 {
240 m_lastActiveTime = CTimeUtils::GetFrameTime();
241 m_mouseState.active = active;
242 // we show the OS mouse if:
243 // 1. The mouse is active (it has been moved) AND
244 // 2. The XBMC mouse is disabled in settings AND
245 // 3. XBMC is not in fullscreen.
246 CWinSystemBase* winSystem = CServiceBroker::GetWinSystem();
247 if (winSystem)
248 winSystem->ShowOSMouse(m_mouseState.active && !IsEnabled() &&
249 !CServiceBroker::GetWinSystem()->IsFullScreen());
250 }
251
252 // IsActive - returns true if we have been active in the last MOUSE_ACTIVE_LENGTH period
253 bool CMouseStat::IsActive()
254 {
255 if (m_mouseState.active && (CTimeUtils::GetFrameTime() - m_lastActiveTime > MOUSE_ACTIVE_LENGTH))
256 SetActive(false);
257 return (m_mouseState.active && IsEnabled());
258 }
259
260 void CMouseStat::SetEnabled(bool enabled)
261 {
262 m_mouseEnabled = enabled;
263 SetActive(enabled);
264 }
265
266 // IsEnabled - returns true if mouse is enabled
267 bool CMouseStat::IsEnabled() const
268 {
269 return m_mouseEnabled;
270 }
271
272 bool CMouseStat::MovedPastThreshold() const
273 {
274 return (m_mouseState.dx * m_mouseState.dx + m_mouseState.dy * m_mouseState.dy >=
275 MOUSE_MINIMUM_MOVEMENT * MOUSE_MINIMUM_MOVEMENT);
276 }
277
278 uint32_t CMouseStat::GetKey() const
279 {
280 return m_Key;
281 }
282
283 HoldAction CMouseStat::GetHold(int ButtonID) const
284 {
285 switch (ButtonID)
286 {
287 case MOUSE_LEFT_BUTTON:
288 return m_hold[MOUSE_LEFT_BUTTON];
289 }
290 return HoldAction::NONE;
291 }
292
293 CMouseStat::CButtonState::CButtonState()
294 {
295 m_state = STATE_RELEASED;
296 m_time = 0;
297 m_x = 0;
298 m_y = 0;
299 }
300
301 bool CMouseStat::CButtonState::InClickRange(int x, int y) const
302 {
303 int dx = x - m_x;
304 int dy = y - m_y;
305 return (unsigned int)(dx * dx + dy * dy) <= click_confines * click_confines;
306 }
307
308 CMouseStat::CButtonState::BUTTON_ACTION CMouseStat::CButtonState::Update(unsigned int time,
309 int x,
310 int y,
311 bool down)
312 {
313 if (m_state == STATE_IN_DRAG)
314 {
315 if (down)
316 return MB_DRAG;
317 m_state = STATE_RELEASED;
318 return MB_DRAG_END;
319 }
320 else if (m_state == STATE_RELEASED)
321 {
322 if (down)
323 {
324 m_state = STATE_IN_CLICK;
325 m_time = time;
326 m_x = x;
327 m_y = y;
328 }
329 }
330 else if (m_state == STATE_IN_CLICK)
331 {
332 if (down)
333 {
334 if (!InClickRange(x, y))
335 { // beginning a drag
336 m_state = STATE_IN_DRAG;
337 return MB_DRAG_START;
338 }
339 }
340 else
341 { // button up
342 if (time - m_time < short_click_time)
343 { // single click
344 m_state = STATE_IN_DOUBLE_CLICK;
345 m_time = time; // double click time and positioning is measured from the
346 m_x = x; // end of a single click
347 m_y = y;
348 return MB_SHORT_CLICK;
349 }
350 else
351 { // long click
352 m_state = STATE_RELEASED;
353 return MB_LONG_CLICK;
354 }
355 }
356 }
357 else if (m_state == STATE_IN_DOUBLE_CLICK)
358 {
359 if (time - m_time > double_click_time || !InClickRange(x, y))
360 { // too long, or moved to much - reset to released state and re-update, as we may be starting a
361 // new click
362 m_state = STATE_RELEASED;
363 return Update(time, x, y, down);
364 }
365 if (down)
366 {
367 m_state = STATE_IN_DOUBLE_IGNORE;
368 return MB_DOUBLE_CLICK;
369 }
370 }
371 else if (m_state == STATE_IN_DOUBLE_IGNORE)
372 {
373 if (!down)
374 m_state = STATE_RELEASED;
375 }
376
377 return MB_NONE;
378 }