kodi
MouseStat.h
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 #pragma once
10 
11 #include "windowing/XBMC_events.h"
12 
15 
16 #define XBMC_BUTTON(X) (1 << ((X)-1))
17 #define XBMC_BUTTON_LEFT 1
18 #define XBMC_BUTTON_MIDDLE 2
19 #define XBMC_BUTTON_RIGHT 3
20 #define XBMC_BUTTON_WHEELUP 4
21 #define XBMC_BUTTON_WHEELDOWN 5
22 #define XBMC_BUTTON_X1 6
23 #define XBMC_BUTTON_X2 7
24 #define XBMC_BUTTON_X3 8
25 #define XBMC_BUTTON_X4 9
26 #define XBMC_BUTTON_LMASK XBMC_BUTTON(XBMC_BUTTON_LEFT)
27 #define XBMC_BUTTON_MMASK XBMC_BUTTON(XBMC_BUTTON_MIDDLE)
28 #define XBMC_BUTTON_RMASK XBMC_BUTTON(XBMC_BUTTON_RIGHT)
29 #define XBMC_BUTTON_X1MASK XBMC_BUTTON(XBMC_BUTTON_X1)
30 #define XBMC_BUTTON_X2MASK XBMC_BUTTON(XBMC_BUTTON_X2)
31 #define XBMC_BUTTON_X3MASK XBMC_BUTTON(XBMC_BUTTON_X3)
32 #define XBMC_BUTTON_X4MASK XBMC_BUTTON(XBMC_BUTTON_X4)
33 
34 #define MOUSE_MINIMUM_MOVEMENT 2
35 #define MOUSE_DOUBLE_CLICK_LENGTH 500L
36 #define MOUSE_ACTIVE_LENGTH 5000L
37 
38 #define MOUSE_MAX_BUTTON 7
39 
40 enum MOUSE_STATE
41 {
43  MOUSE_STATE_NORMAL = 1,
45  MOUSE_STATE_FOCUS,
47  MOUSE_STATE_DRAG,
49  MOUSE_STATE_CLICK
50 };
51 
52 enum MOUSE_BUTTON
53 {
54  MOUSE_LEFT_BUTTON = 0,
55  MOUSE_RIGHT_BUTTON,
56  MOUSE_MIDDLE_BUTTON,
57  MOUSE_EXTRA_BUTTON1,
58  MOUSE_EXTRA_BUTTON2,
59  MOUSE_EXTRA_BUTTON3,
60  MOUSE_EXTRA_BUTTON4
61 };
62 
63 enum class HoldAction
64 {
66  NONE,
68  DRAG_START,
70  DRAG,
72  DRAG_END
73 };
74 
76 struct MouseState
77 {
79  int x;
81  int y;
83  int16_t dx;
85  int16_t dy;
87  int8_t dz;
89  bool button[MOUSE_MAX_BUTTON];
91  bool active;
92 };
93 
95 {
96  int x;
97  int y;
98 };
99 
100 class CAction;
101 
103 {
104 public:
105  CMouseStat();
106  virtual ~CMouseStat();
107 
108  void Initialize();
109  void HandleEvent(const XBMC_Event& newEvent);
110  void SetResolution(int maxX, int maxY, float speedX, float speedY);
111  bool IsActive();
112  bool IsEnabled() const;
113 
114  void SetActive(bool active = true);
115  void SetState(MOUSE_STATE state) { m_pointerState = state; }
116  void SetEnabled(bool enabled = true);
117  MOUSE_STATE GetState() const { return m_pointerState; }
118  uint32_t GetKey() const;
119 
120  HoldAction GetHold(int ButtonID) const;
121  inline int GetX(void) const { return m_mouseState.x; }
122  inline int GetY(void) const { return m_mouseState.y; }
123  inline int GetDX(void) const { return m_mouseState.dx; }
124  inline int GetDY(void) const { return m_mouseState.dy; }
125  MousePosition GetPosition() { return MousePosition{m_mouseState.x, m_mouseState.y}; }
126 
127 private:
145  class CButtonState
146  {
147  public:
150  enum BUTTON_ACTION
151  {
152  MB_NONE = 0,
153  MB_SHORT_CLICK,
154  MB_LONG_CLICK,
155  MB_DOUBLE_CLICK,
156  MB_DRAG_START,
157  MB_DRAG,
158  MB_DRAG_END
159  };
160 
161  CButtonState();
162 
172  BUTTON_ACTION Update(unsigned int time, int x, int y, bool down);
173 
174  private:
177  static const unsigned int click_confines = 5;
178 
180  static const unsigned int short_click_time = 1000;
181 
183  static const unsigned int double_click_time = 500;
184 
185  bool InClickRange(int x, int y) const;
186 
187  enum BUTTON_STATE
188  {
189  STATE_RELEASED = 0,
190  STATE_IN_CLICK,
191  STATE_IN_DOUBLE_CLICK,
192  STATE_IN_DOUBLE_IGNORE,
193  STATE_IN_DRAG
194  };
195 
196  BUTTON_STATE m_state;
197  unsigned int m_time;
198  int m_x;
199  int m_y;
200  };
201 
208  bool MovedPastThreshold() const;
209 
210  // state of the mouse
211  MOUSE_STATE m_pointerState{MOUSE_STATE_NORMAL};
212  MouseState m_mouseState{};
213  bool m_mouseEnabled;
214  CButtonState m_buttonState[MOUSE_MAX_BUTTON];
215 
216  int m_maxX{0};
217  int m_maxY{0};
218  float m_speedX{0.0f};
219  float m_speedY{0.0f};
220 
221  // active/click timers
222  unsigned int m_lastActiveTime;
223 
224  bool bClick[MOUSE_MAX_BUTTON]{};
225  bool bDoubleClick[MOUSE_MAX_BUTTON]{};
226  HoldAction m_hold[MOUSE_MAX_BUTTON]{};
227  bool bLongClick[MOUSE_MAX_BUTTON]{};
228 
229  uint32_t m_Key;
230 };
231 
int16_t dx
Definition: MouseStat.h:83
int x
Definition: MouseStat.h:79
Definition: MouseStat.h:94
Class encapsulating information regarding a particular user action to be sent to windows and controls...
Definition: Action.h:21
int16_t dy
Definition: MouseStat.h:85
int y
Definition: MouseStat.h:81
Definition: XBMC_events.h:117
bool active
Definition: MouseStat.h:91
Definition: MouseStat.h:102
Holds everything we know about the current state of the mouse.
Definition: MouseStat.h:76
int8_t dz
Definition: MouseStat.h:87