Debian Codegraph @

xbmc/peripherals/devices/PeripheralMouse.cpp

raw
1 /*
2 * Copyright (C) 2017-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 "PeripheralMouse.h"
10
11 #include "games/GameServices.h"
12 #include "games/controllers/Controller.h"
13 #include "games/controllers/ControllerManager.h"
14 #include "input/InputManager.h"
15 #include "peripherals/Peripherals.h"
16
17 #include <mutex>
18 #include <sstream>
19
20 using namespace KODI;
21 using namespace PERIPHERALS;
22
23 CPeripheralMouse::CPeripheralMouse(CPeripherals& manager,
24 const PeripheralScanResult& scanResult,
25 CPeripheralBus* bus)
26 : CPeripheral(manager, scanResult, bus)
27 {
28 // Initialize CPeripheral
29 m_features.push_back(FEATURE_MOUSE);
30 }
31
32 CPeripheralMouse::~CPeripheralMouse(void)
33 {
34 m_manager.GetInputManager().UnregisterMouseDriverHandler(this);
35 }
36
37 bool CPeripheralMouse::InitialiseFeature(const PeripheralFeature feature)
38 {
39 bool bSuccess = false;
40
41 if (CPeripheral::InitialiseFeature(feature))
42 {
43 if (feature == FEATURE_MOUSE)
44 {
45 m_manager.GetInputManager().RegisterMouseDriverHandler(this);
46 }
47
48 bSuccess = true;
49 }
50
51 return bSuccess;
52 }
53
54 void CPeripheralMouse::RegisterMouseDriverHandler(MOUSE::IMouseDriverHandler* handler,
55 bool bPromiscuous)
56 {
57 std::unique_lock<CCriticalSection> lock(m_mutex);
58
59 MouseHandle handle{handler, bPromiscuous};
60 m_mouseHandlers.insert(m_mouseHandlers.begin(), handle);
61 }
62
63 void CPeripheralMouse::UnregisterMouseDriverHandler(MOUSE::IMouseDriverHandler* handler)
64 {
65 std::unique_lock<CCriticalSection> lock(m_mutex);
66
67 auto it =
68 std::find_if(m_mouseHandlers.begin(), m_mouseHandlers.end(),
69 [handler](const MouseHandle& handle) { return handle.handler == handler; });
70
71 if (it != m_mouseHandlers.end())
72 m_mouseHandlers.erase(it);
73 }
74
75 void CPeripheralMouse::SetLastActive(const CDateTime& lastActive)
76 {
77 // Update state
78 m_lastActive = lastActive;
79
80 // Update ancestor
81 CPeripheral::SetLastActive(lastActive);
82 }
83
84 GAME::ControllerPtr CPeripheralMouse::ControllerProfile() const
85 {
86 if (m_controllerProfile)
87 return m_controllerProfile;
88
89 return m_manager.GetControllerProfiles().GetDefaultMouse();
90 }
91
92 bool CPeripheralMouse::OnPosition(int x, int y)
93 {
94 std::unique_lock<CCriticalSection> lock(m_mutex);
95
96 bool bHandled = false;
97
98 // Process promiscuous handlers
99 for (const MouseHandle& handle : m_mouseHandlers)
100 {
101 if (handle.bPromiscuous)
102 handle.handler->OnPosition(x, y);
103 }
104
105 // Process handlers until one is handled
106 for (const MouseHandle& handle : m_mouseHandlers)
107 {
108 if (!handle.bPromiscuous)
109 {
110 bHandled = handle.handler->OnPosition(x, y);
111 if (bHandled)
112 break;
113 }
114 }
115
116 // Update state
117 if (bHandled)
118 SetLastActive(CDateTime::GetCurrentDateTime());
119
120 return bHandled;
121 }
122
123 bool CPeripheralMouse::OnButtonPress(MOUSE::BUTTON_ID button)
124 {
125 std::unique_lock<CCriticalSection> lock(m_mutex);
126
127 // Update state
128 SetLastActive(CDateTime::GetCurrentDateTime());
129
130 bool bHandled = false;
131
132 // Process promiscuous handlers
133 for (const MouseHandle& handle : m_mouseHandlers)
134 {
135 if (handle.bPromiscuous)
136 handle.handler->OnButtonPress(button);
137 }
138
139 // Process handlers until one is handled
140 for (const MouseHandle& handle : m_mouseHandlers)
141 {
142 if (!handle.bPromiscuous)
143 {
144 bHandled = handle.handler->OnButtonPress(button);
145 if (bHandled)
146 break;
147 }
148 }
149
150 return bHandled;
151 }
152
153 void CPeripheralMouse::OnButtonRelease(MOUSE::BUTTON_ID button)
154 {
155 std::unique_lock<CCriticalSection> lock(m_mutex);
156
157 for (const MouseHandle& handle : m_mouseHandlers)
158 handle.handler->OnButtonRelease(button);
159 }