Debian Codegraph @

xbmc/input/touch/generic/GenericTouchActionHandler.cpp

raw
1 /*
2 * Copyright (C) 2013-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 "GenericTouchActionHandler.h"
10
11 #include "ServiceBroker.h"
12 #include "application/AppInboundProtocol.h"
13 #include "guilib/GUIComponent.h"
14 #include "guilib/GUIWindowManager.h"
15 #include "input/actions/ActionIDs.h"
16
17 #include <cmath>
18
19 using namespace KODI::MESSAGING;
20
21 CGenericTouchActionHandler& CGenericTouchActionHandler::GetInstance()
22 {
23 static CGenericTouchActionHandler sTouchAction;
24 return sTouchAction;
25 }
26
27 void CGenericTouchActionHandler::OnTouchAbort()
28 {
29 sendEvent(ACTION_GESTURE_ABORT, 0.0f, 0.0f);
30 }
31
32 bool CGenericTouchActionHandler::OnSingleTouchStart(float x, float y)
33 {
34 focusControl(x, y);
35
36 return true;
37 }
38
39 bool CGenericTouchActionHandler::OnSingleTouchHold(float x, float y)
40 {
41 return true;
42 }
43
44 bool CGenericTouchActionHandler::OnSingleTouchMove(
45 float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
46 {
47 return true;
48 }
49
50 bool CGenericTouchActionHandler::OnSingleTouchEnd(float x, float y)
51 {
52 return true;
53 }
54
55 bool CGenericTouchActionHandler::OnMultiTouchDown(float x, float y, int32_t pointer)
56 {
57 return true;
58 }
59
60 bool CGenericTouchActionHandler::OnMultiTouchHold(float x, float y, int32_t pointers /* = 2 */)
61 {
62 return true;
63 }
64
65 bool CGenericTouchActionHandler::OnMultiTouchMove(float x,
66 float y,
67 float offsetX,
68 float offsetY,
69 float velocityX,
70 float velocityY,
71 int32_t pointer)
72 {
73 return true;
74 }
75
76 bool CGenericTouchActionHandler::OnMultiTouchUp(float x, float y, int32_t pointer)
77 {
78 return true;
79 }
80
81 bool CGenericTouchActionHandler::OnTouchGestureStart(float x, float y)
82 {
83 sendEvent(ACTION_GESTURE_BEGIN, x, y, 0.0f, 0.0f);
84
85 return true;
86 }
87
88 bool CGenericTouchActionHandler::OnTouchGesturePan(
89 float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
90 {
91 sendEvent(ACTION_GESTURE_PAN, x, y, offsetX, offsetY, velocityX, velocityY);
92
93 return true;
94 }
95
96 bool CGenericTouchActionHandler::OnTouchGestureEnd(
97 float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
98 {
99 sendEvent(ACTION_GESTURE_END, velocityX, velocityY, x, y, offsetX, offsetY);
100
101 return true;
102 }
103
104 void CGenericTouchActionHandler::OnTap(float x, float y, int32_t pointers /* = 1 */)
105 {
106 if (pointers <= 0 || pointers > 10)
107 return;
108
109 sendEvent(ACTION_TOUCH_TAP, x, y, 0.0f, 0.0f, 0.0f, 0.0f, pointers);
110 }
111
112 void CGenericTouchActionHandler::OnLongPress(float x, float y, int32_t pointers /* = 1 */)
113 {
114 if (pointers <= 0 || pointers > 10)
115 return;
116
117 sendEvent(ACTION_TOUCH_LONGPRESS, x, y, 0.0f, 0.0f, 0.0f, 0.0f, pointers);
118 }
119
120 void CGenericTouchActionHandler::OnSwipe(TouchMoveDirection direction,
121 float xDown,
122 float yDown,
123 float xUp,
124 float yUp,
125 float velocityX,
126 float velocityY,
127 int32_t pointers /* = 1 */)
128 {
129 if (pointers <= 0 || pointers > 10)
130 return;
131
132 int actionId = 0;
133 if (direction == TouchMoveDirectionLeft)
134 actionId = ACTION_GESTURE_SWIPE_LEFT;
135 else if (direction == TouchMoveDirectionRight)
136 actionId = ACTION_GESTURE_SWIPE_RIGHT;
137 else if (direction == TouchMoveDirectionUp)
138 actionId = ACTION_GESTURE_SWIPE_UP;
139 else if (direction == TouchMoveDirectionDown)
140 actionId = ACTION_GESTURE_SWIPE_DOWN;
141 else
142 return;
143
144 sendEvent(actionId, xUp, yUp, velocityX, velocityY, xDown, yDown, pointers);
145 }
146
147 void CGenericTouchActionHandler::OnZoomPinch(float centerX, float centerY, float zoomFactor)
148 {
149 sendEvent(ACTION_GESTURE_ZOOM, centerX, centerY, zoomFactor, 0.0f);
150 }
151
152 void CGenericTouchActionHandler::OnRotate(float centerX, float centerY, float angle)
153 {
154 sendEvent(ACTION_GESTURE_ROTATE, centerX, centerY, angle, 0.0f);
155 }
156
157 int CGenericTouchActionHandler::QuerySupportedGestures(float x, float y)
158 {
159 CGUIMessage msg(GUI_MSG_GESTURE_NOTIFY, 0, 0, static_cast<int>(std::round(x)),
160 static_cast<int>(std::round(y)));
161 if (!CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg))
162 return 0;
163
164 int result = 0;
165 if (msg.GetPointer())
166 {
167 int* p = static_cast<int*>(msg.GetPointer());
168 msg.SetPointer(nullptr);
169 result = *p;
170 delete p;
171 }
172 return result;
173 }
174
175 void CGenericTouchActionHandler::sendEvent(int actionId,
176 float x,
177 float y,
178 float x2 /* = 0.0f */,
179 float y2 /* = 0.0f */,
180 float x3,
181 float y3,
182 int pointers /* = 1 */)
183 {
184 XBMC_Event newEvent{};
185 newEvent.type = XBMC_TOUCH;
186
187 newEvent.touch.action = actionId;
188 newEvent.touch.x = x;
189 newEvent.touch.y = y;
190 newEvent.touch.x2 = x2;
191 newEvent.touch.y2 = y2;
192 newEvent.touch.x3 = x3;
193 newEvent.touch.y3 = y3;
194 newEvent.touch.pointers = pointers;
195
196 std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort();
197 if (appPort)
198 appPort->OnEvent(newEvent);
199 }
200
201 void CGenericTouchActionHandler::focusControl(float x, float y)
202 {
203 XBMC_Event newEvent{};
204 newEvent.type = XBMC_SETFOCUS;
205
206 newEvent.focus.x = static_cast<int>(std::round(x));
207 newEvent.focus.y = static_cast<int>(std::round(y));
208
209 std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort();
210 if (appPort)
211 appPort->OnEvent(newEvent);
212 }