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