| 1 |
/* |
| 2 |
* Copyright (C) 2012-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 "input/touch/ITouchInputHandler.h" |
| 12 |
#include "input/touch/TouchTypes.h" |
| 13 |
#include "threads/CriticalSection.h" |
| 14 |
#include "threads/Timer.h" |
| 15 |
|
| 16 |
#include <array> |
| 17 |
#include <memory> |
| 18 |
#include <set> |
| 19 |
|
| 20 |
class IGenericTouchGestureDetector; |
| 21 |
|
| 22 |
/*! |
| 23 |
* \ingroup touch_generic |
| 24 |
* \brief Generic implementation of ITouchInputHandler to handle low level (raw) |
| 25 |
* touch events and translate them into touch actions which are passed |
| 26 |
* on to the registered ITouchActionHandler implementation. |
| 27 |
* |
| 28 |
* The generic implementation supports single a double touch and hold |
| 29 |
* actions and basic gesture recognition for panning, swiping, pinching/zooming |
| 30 |
* and rotating. |
| 31 |
* |
| 32 |
* \sa ITouchInputHandler |
| 33 |
*/ |
| 34 |
class CGenericTouchInputHandler : public ITouchInputHandler, private ITimerCallback |
| 35 |
{ |
| 36 |
public: |
| 37 |
/*! |
| 38 |
\brief Get an instance of the touch input manager |
| 39 |
*/ |
| 40 |
static CGenericTouchInputHandler& GetInstance(); |
| 41 |
static constexpr int MAX_POINTERS = 2; |
| 42 |
|
| 43 |
// implementation of ITouchInputHandler |
| 44 |
bool HandleTouchInput(TouchInput event, |
| 45 |
float x, |
| 46 |
float y, |
| 47 |
int64_t time, |
| 48 |
int32_t pointer = 0, |
| 49 |
float size = 0.0f) override; |
| 50 |
bool UpdateTouchPointer( |
| 51 |
int32_t pointer, float x, float y, int64_t time, float size = 0.0f) override; |
| 52 |
|
| 53 |
private: |
| 54 |
// private construction, and no assignments; use the provided singleton methods |
| 55 |
CGenericTouchInputHandler(); |
| 56 |
~CGenericTouchInputHandler() override; |
| 57 |
CGenericTouchInputHandler(const CGenericTouchInputHandler&) = delete; |
| 58 |
CGenericTouchInputHandler const& operator=(CGenericTouchInputHandler const&) = delete; |
| 59 |
|
| 60 |
typedef enum |
| 61 |
{ |
| 62 |
TouchGestureUnknown = 0, |
| 63 |
// only primary pointer active but stationary so far |
| 64 |
TouchGestureSingleTouch, |
| 65 |
// primary pointer active but stationary for a certain time |
| 66 |
TouchGestureSingleTouchHold, |
| 67 |
// primary pointer moving |
| 68 |
TouchGesturePan, |
| 69 |
// at least two pointers active but stationary so far |
| 70 |
TouchGestureMultiTouchStart, |
| 71 |
// at least two pointers active but stationary for a certain time |
| 72 |
TouchGestureMultiTouchHold, |
| 73 |
// at least two pointers active and moving |
| 74 |
TouchGestureMultiTouch, |
| 75 |
// all but primary pointer have been lifted |
| 76 |
TouchGestureMultiTouchDone |
| 77 |
} TouchGestureState; |
| 78 |
|
| 79 |
// implementation of ITimerCallback |
| 80 |
void OnTimeout() override; |
| 81 |
|
| 82 |
void saveLastTouch(); |
| 83 |
void setGestureState(TouchGestureState gestureState) |
| 84 |
{ |
| 85 |
m_gestureStateOld = m_gestureState; |
| 86 |
m_gestureState = gestureState; |
| 87 |
} |
| 88 |
void triggerDetectors(TouchInput event, int32_t pointer); |
| 89 |
float AdjustPointerSize(float size); |
| 90 |
|
| 91 |
CCriticalSection m_critical; |
| 92 |
std::unique_ptr<CTimer> m_holdTimer; |
| 93 |
std::array<Pointer, MAX_POINTERS> m_pointers; |
| 94 |
std::set<std::unique_ptr<IGenericTouchGestureDetector>> m_detectors; |
| 95 |
|
| 96 |
TouchGestureState m_gestureState = TouchGestureUnknown; |
| 97 |
TouchGestureState m_gestureStateOld = TouchGestureUnknown; |
| 98 |
}; |