| 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 |
#include "EventScanner.h" |
| 10 |
|
| 11 |
#include "peripherals/events/interfaces/IEventScannerCallback.h" |
| 12 |
#include "utils/log.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <mutex> |
| 16 |
|
| 17 |
using namespace PERIPHERALS; |
| 18 |
|
| 19 |
// Default event scan rate when no polling handles are held |
| 20 |
#define DEFAULT_SCAN_RATE_HZ 60 |
| 21 |
|
| 22 |
// Timeout when a polling handle is held but doesn't trigger scan. This reduces |
| 23 |
// input latency when the game is running at < 1/4 speed. |
| 24 |
#define WATCHDOG_TIMEOUT_MS 80 |
| 25 |
|
| 26 |
CEventScanner::CEventScanner(IEventScannerCallback& callback) |
| 27 |
: CThread("PeripEventScan"), m_callback(callback) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
void CEventScanner::Start() |
| 32 |
{ |
| 33 |
Create(); |
| 34 |
} |
| 35 |
|
| 36 |
void CEventScanner::Stop() |
| 37 |
{ |
| 38 |
StopThread(false); |
| 39 |
m_scanEvent.Set(); |
| 40 |
StopThread(true); |
| 41 |
} |
| 42 |
|
| 43 |
EventPollHandlePtr CEventScanner::RegisterPollHandle() |
| 44 |
{ |
| 45 |
EventPollHandlePtr handle(new CEventPollHandle(*this)); |
| 46 |
|
| 47 |
{ |
| 48 |
std::unique_lock<CCriticalSection> lock(m_handleMutex); |
| 49 |
m_activeHandles.insert(handle.get()); |
| 50 |
} |
| 51 |
|
| 52 |
CLog::Log(LOGDEBUG, "PERIPHERALS: Event poll handle registered"); |
| 53 |
|
| 54 |
return handle; |
| 55 |
} |
| 56 |
|
| 57 |
void CEventScanner::Activate(CEventPollHandle& handle) |
| 58 |
{ |
| 59 |
{ |
| 60 |
std::unique_lock<CCriticalSection> lock(m_handleMutex); |
| 61 |
m_activeHandles.insert(&handle); |
| 62 |
} |
| 63 |
|
| 64 |
CLog::Log(LOGDEBUG, "PERIPHERALS: Event poll handle activated"); |
| 65 |
} |
| 66 |
|
| 67 |
void CEventScanner::Deactivate(CEventPollHandle& handle) |
| 68 |
{ |
| 69 |
{ |
| 70 |
std::unique_lock<CCriticalSection> lock(m_handleMutex); |
| 71 |
m_activeHandles.erase(&handle); |
| 72 |
} |
| 73 |
|
| 74 |
CLog::Log(LOGDEBUG, "PERIPHERALS: Event poll handle deactivated"); |
| 75 |
} |
| 76 |
|
| 77 |
void CEventScanner::HandleEvents(bool bWait) |
| 78 |
{ |
| 79 |
if (bWait) |
| 80 |
{ |
| 81 |
std::unique_lock<CCriticalSection> lock(m_pollMutex); |
| 82 |
|
| 83 |
m_scanFinishedEvent.Reset(); |
| 84 |
m_scanEvent.Set(); |
| 85 |
m_scanFinishedEvent.Wait(); |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
m_scanEvent.Set(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void CEventScanner::Release(CEventPollHandle& handle) |
| 94 |
{ |
| 95 |
{ |
| 96 |
std::unique_lock<CCriticalSection> lock(m_handleMutex); |
| 97 |
m_activeHandles.erase(&handle); |
| 98 |
} |
| 99 |
|
| 100 |
CLog::Log(LOGDEBUG, "PERIPHERALS: Event poll handle released"); |
| 101 |
} |
| 102 |
|
| 103 |
EventLockHandlePtr CEventScanner::RegisterLock() |
| 104 |
{ |
| 105 |
EventLockHandlePtr handle(new CEventLockHandle(*this)); |
| 106 |
|
| 107 |
{ |
| 108 |
std::unique_lock<CCriticalSection> lock(m_lockMutex); |
| 109 |
m_activeLocks.insert(handle.get()); |
| 110 |
} |
| 111 |
|
| 112 |
CLog::Log(LOGDEBUG, "PERIPHERALS: Event lock handle registered"); |
| 113 |
|
| 114 |
return handle; |
| 115 |
} |
| 116 |
|
| 117 |
void CEventScanner::ReleaseLock(CEventLockHandle& handle) |
| 118 |
{ |
| 119 |
{ |
| 120 |
std::unique_lock<CCriticalSection> lock(m_lockMutex); |
| 121 |
m_activeLocks.erase(&handle); |
| 122 |
} |
| 123 |
|
| 124 |
CLog::Log(LOGDEBUG, "PERIPHERALS: Event lock handle released"); |
| 125 |
} |
| 126 |
|
| 127 |
void CEventScanner::Process() |
| 128 |
{ |
| 129 |
auto nextScan = std::chrono::steady_clock::now(); |
| 130 |
|
| 131 |
while (!m_bStop) |
| 132 |
{ |
| 133 |
{ |
| 134 |
std::unique_lock<CCriticalSection> lock(m_lockMutex); |
| 135 |
if (m_activeLocks.empty()) |
| 136 |
m_callback.ProcessEvents(); |
| 137 |
} |
| 138 |
|
| 139 |
m_scanFinishedEvent.Set(); |
| 140 |
|
| 141 |
auto now = std::chrono::steady_clock::now(); |
| 142 |
auto scanIntervalMs = GetScanIntervalMs(); |
| 143 |
|
| 144 |
// Handle wrap-around |
| 145 |
if (now < nextScan) |
| 146 |
nextScan = now; |
| 147 |
|
| 148 |
while (nextScan <= now) |
| 149 |
nextScan += scanIntervalMs; |
| 150 |
|
| 151 |
auto waitTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(nextScan - now); |
| 152 |
|
| 153 |
if (!m_bStop && waitTimeMs.count() > 0) |
| 154 |
m_scanEvent.Wait(waitTimeMs); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
std::chrono::milliseconds CEventScanner::GetScanIntervalMs() const |
| 159 |
{ |
| 160 |
bool bHasActiveHandle; |
| 161 |
|
| 162 |
{ |
| 163 |
std::unique_lock<CCriticalSection> lock(m_handleMutex); |
| 164 |
bHasActiveHandle = !m_activeHandles.empty(); |
| 165 |
} |
| 166 |
|
| 167 |
if (!bHasActiveHandle) |
| 168 |
{ |
| 169 |
// this truncates to 16 (from 16.666) should it round up to 17 using std::nearbyint or should we use nanoseconds? |
| 170 |
return std::chrono::milliseconds(static_cast<uint32_t>(1000.0 / DEFAULT_SCAN_RATE_HZ)); |
| 171 |
} |
| 172 |
else |
| 173 |
return std::chrono::milliseconds(WATCHDOG_TIMEOUT_MS); |
| 174 |
} |