| 1 |
/* |
| 2 |
* Copyright (C) 2016-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 "EventLockHandle.h" |
| 12 |
#include "EventPollHandle.h" |
| 13 |
#include "peripherals/PeripheralTypes.h" |
| 14 |
#include "threads/CriticalSection.h" |
| 15 |
#include "threads/Event.h" |
| 16 |
#include "threads/Thread.h" |
| 17 |
|
| 18 |
#include <chrono> |
| 19 |
#include <set> |
| 20 |
|
| 21 |
namespace PERIPHERALS |
| 22 |
{ |
| 23 |
class IEventScannerCallback; |
| 24 |
|
| 25 |
/*! |
| 26 |
* \ingroup peripherals |
| 27 |
* |
| 28 |
* \brief Class to scan for peripheral events |
| 29 |
* |
| 30 |
* By default, a rate of 60 Hz is used. A client can obtain control over when |
| 31 |
* input is handled by registering for a polling handle. |
| 32 |
*/ |
| 33 |
class CEventScanner : public IEventPollCallback, public IEventLockCallback, protected CThread |
| 34 |
{ |
| 35 |
public: |
| 36 |
explicit CEventScanner(IEventScannerCallback& callback); |
| 37 |
|
| 38 |
~CEventScanner() override = default; |
| 39 |
|
| 40 |
void Start(); |
| 41 |
void Stop(); |
| 42 |
|
| 43 |
EventPollHandlePtr RegisterPollHandle(); |
| 44 |
|
| 45 |
/*! |
| 46 |
* \brief Acquire a lock that prevents event processing while held |
| 47 |
*/ |
| 48 |
EventLockHandlePtr RegisterLock(); |
| 49 |
|
| 50 |
// implementation of IEventPollCallback |
| 51 |
void Activate(CEventPollHandle& handle) override; |
| 52 |
void Deactivate(CEventPollHandle& handle) override; |
| 53 |
void HandleEvents(bool bWait) override; |
| 54 |
void Release(CEventPollHandle& handle) override; |
| 55 |
|
| 56 |
// implementation of IEventLockCallback |
| 57 |
void ReleaseLock(CEventLockHandle& handle) override; |
| 58 |
|
| 59 |
protected: |
| 60 |
// implementation of CThread |
| 61 |
void Process() override; |
| 62 |
|
| 63 |
private: |
| 64 |
std::chrono::milliseconds GetScanIntervalMs() const; |
| 65 |
|
| 66 |
// Construction parameters |
| 67 |
IEventScannerCallback& m_callback; |
| 68 |
|
| 69 |
// Event parameters |
| 70 |
std::set<void*> m_activeHandles; |
| 71 |
std::set<void*> m_activeLocks; |
| 72 |
CEvent m_scanEvent; |
| 73 |
CEvent m_scanFinishedEvent; |
| 74 |
mutable CCriticalSection m_handleMutex; |
| 75 |
CCriticalSection m_lockMutex; |
| 76 |
CCriticalSection m_pollMutex; // Prevent two poll handles from polling at once |
| 77 |
}; |
| 78 |
} // namespace PERIPHERALS |