Debian Codegraph @

xbmc/windowing/wayland/InputProcessorTouch.cpp

raw
1 /*
2 * Copyright (C) 2017-2018 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 "InputProcessorTouch.h"
10
11 #include "input/touch/generic/GenericTouchInputHandler.h"
12
13 using namespace KODI::WINDOWING::WAYLAND;
14
15 CInputProcessorTouch::CInputProcessorTouch(wayland::surface_t const& surface)
16 : m_surface{surface}
17 {
18 }
19
20 void CInputProcessorTouch::OnTouchDown(CSeat* seat,
21 std::uint32_t serial,
22 std::uint32_t time,
23 const wayland::surface_t& surface,
24 std::int32_t id,
25 double x,
26 double y)
27 {
28 if (surface != m_surface)
29 {
30 return;
31 }
32
33 // Find free Kodi pointer number
34 int kodiPointer{-1};
35 // Not optimal, but irrelevant for the small number of iterations
36 for (int testPointer{0}; testPointer < CGenericTouchInputHandler::MAX_POINTERS; testPointer++)
37 {
38 if (std::all_of(m_touchPoints.cbegin(), m_touchPoints.cend(),
39 [=](decltype(m_touchPoints)::value_type const& pair)
40 {
41 return (pair.second.kodiPointerNumber != testPointer);
42 }))
43 {
44 kodiPointer = testPointer;
45 break;
46 }
47 }
48
49 if (kodiPointer != -1)
50 {
51 auto it = m_touchPoints.emplace(std::piecewise_construct, std::forward_as_tuple(id), std::forward_as_tuple(time, kodiPointer, x * m_coordinateScale, y * m_coordinateScale, 0.0f)).first;
52 SendTouchPointEvent(TouchInputDown, it->second);
53 }
54 }
55
56 void CInputProcessorTouch::OnTouchUp(CSeat* seat, std::uint32_t serial, std::uint32_t time, std::int32_t id)
57 {
58 auto it = m_touchPoints.find(id);
59 if (it != m_touchPoints.end())
60 {
61 auto& point = it->second;
62 point.lastEventTime = time;
63 SendTouchPointEvent(TouchInputUp, point);
64 m_touchPoints.erase(it);
65 }
66 }
67
68 void CInputProcessorTouch::OnTouchMotion(CSeat* seat, std::uint32_t time, std::int32_t id, double x, double y)
69 {
70 auto it = m_touchPoints.find(id);
71 if (it != m_touchPoints.end())
72 {
73 auto& point = it->second;
74 point.x = x * m_coordinateScale;
75 point.y = y * m_coordinateScale;
76 point.lastEventTime = time;
77 SendTouchPointEvent(TouchInputMove, point);
78 }
79 }
80
81 void CInputProcessorTouch::OnTouchCancel(CSeat* seat)
82 {
83 AbortTouches();
84 }
85
86 void CInputProcessorTouch::OnTouchShape(CSeat* seat, std::int32_t id, double major, double minor)
87 {
88 auto it = m_touchPoints.find(id);
89 if (it != m_touchPoints.end())
90 {
91 auto& point = it->second;
92 // Kodi only supports size without shape, so use average of both axes
93 point.size = ((major + minor) / 2.0) * m_coordinateScale;
94 UpdateTouchPoint(point);
95 }
96 }
97
98 CInputProcessorTouch::~CInputProcessorTouch() noexcept
99 {
100 AbortTouches();
101 }
102
103 void CInputProcessorTouch::AbortTouches()
104 {
105 // TouchInputAbort aborts for all pointers, so it does not matter which is specified
106 if (!m_touchPoints.empty())
107 {
108 SendTouchPointEvent(TouchInputAbort, m_touchPoints.begin()->second);
109 }
110 m_touchPoints.clear();
111 }
112
113 void CInputProcessorTouch::SendTouchPointEvent(TouchInput event, const TouchPoint& point)
114 {
115 if (event == TouchInputMove)
116 {
117 for (auto const& point : m_touchPoints)
118 {
119 // Contrary to the docs, this must be called before HandleTouchInput or the
120 // position will not be updated and gesture detection will not work
121 UpdateTouchPoint(point.second);
122 }
123 }
124 CGenericTouchInputHandler::GetInstance().HandleTouchInput(event, point.x, point.y, point.lastEventTime * INT64_C(1000000), point.kodiPointerNumber, point.size);
125 }
126
127 void CInputProcessorTouch::UpdateTouchPoint(const TouchPoint& point)
128 {
129 CGenericTouchInputHandler::GetInstance().UpdateTouchPointer(point.kodiPointerNumber, point.x, point.y, point.lastEventTime * INT64_C(1000000), point.size);
130 }