-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screensaver.h
303 lines (254 loc) · 9.31 KB
/
Screensaver.h
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2022 Metrological
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "Module.h"
#include "EGLRender.h"
#include "IModel.h"
#include <simpleworker/SimpleWorker.h>
#include <virtualinput/virtualinput.h>
namespace Thunder {
namespace Plugin {
class Screensaver : public PluginHost::IPlugin, public PluginHost::IWeb, public PluginHost::JSONRPC {
private:
class InputServer {
public:
InputServer(const InputServer&) = delete;
InputServer& operator=(const InputServer&) = delete;
struct EXTERNAL ICallback {
virtual ~ICallback() = default;
virtual void Trigger() = 0;
}; // struct ICallback
InputServer();
~InputServer();
static void VirtualKeyboardCallback(keyactiontype type VARIABLE_IS_NOT_USED, unsigned int code VARIABLE_IS_NOT_USED)
{
Core::SafeSyncType<Core::CriticalSection> scopedLock(_adminLock);
if (_callback != nullptr) {
_callback->Trigger();
}
}
static void VirtualMouseCallback(mouseactiontype type VARIABLE_IS_NOT_USED, unsigned short button VARIABLE_IS_NOT_USED, signed short horizontal VARIABLE_IS_NOT_USED, signed short vertical VARIABLE_IS_NOT_USED)
{
Core::SafeSyncType<Core::CriticalSection> scopedLock(_adminLock);
if (_callback != nullptr) {
_callback->Trigger();
}
}
static void VirtualTouchScreenCallback(touchactiontype type VARIABLE_IS_NOT_USED, unsigned short index VARIABLE_IS_NOT_USED, unsigned short x VARIABLE_IS_NOT_USED, unsigned short y VARIABLE_IS_NOT_USED)
{
Core::SafeSyncType<Core::CriticalSection> scopedLock(_adminLock);
if (_callback != nullptr) {
_callback->Trigger();
}
}
void Callback(ICallback* callback)
{
Core::SafeSyncType<Core::CriticalSection> scopedLock(_adminLock);
ASSERT((callback == nullptr) ^ (_callback == nullptr));
_callback = callback;
}
void Connect(const string& connector);
void Disconnect();
private:
private:
static Core::CriticalSection _adminLock;
static ICallback* _callback;
void* _virtualinput;
}; // class InputServer
public:
Screensaver(const Screensaver&) = delete;
Screensaver& operator=(const Screensaver&) = delete;
Screensaver();
virtual ~Screensaver() override;
BEGIN_INTERFACE_MAP(Screensaver)
INTERFACE_ENTRY(PluginHost::IPlugin)
INTERFACE_ENTRY(PluginHost::IWeb)
INTERFACE_ENTRY(PluginHost::IDispatcher)
END_INTERFACE_MAP
private:
Core::ProxyType<Web::Response> PutMethod(Core::TextSegmentIterator& index, const Web::Request& request);
class InputSink : public InputServer::ICallback {
public:
InputSink() = delete;
InputSink(const InputSink&) = delete;
InputSink& operator=(const InputSink&) = delete;
InputSink(Screensaver& parent)
: _parent(parent)
{
}
~InputSink() = default;
void Trigger()
{
TRACE(Trace::Information, ("Input detected!"));
_parent.Trigger();
}
private:
Screensaver& _parent;
};
class Tick : public Core::IDispatch {
public:
Tick(Screensaver& parent)
: _parent(parent)
, _interval(0)
{
TRACE(Trace::Information, ("Created Tick %p", this));
}
void Schedule(const uint16_t interval)
{
_interval = interval;
Core::IWorkerPool::Instance()
.Schedule(
Core::Time::Now().Add(_interval),
Core::ProxyType<Core::IDispatch>(*this));
}
void Dispatch() override
{
_parent.Tack();
if (_parent.IsActive()) {
Schedule(_interval);
}
}
private:
Screensaver& _parent;
uint16_t _interval;
};
public:
class Config : public Core::JSON::Container {
public:
Config(const Config&) = delete;
Config& operator=(const Config&) = delete;
Config()
: Core::JSON::Container()
, Height(720)
, Width(1280)
, FPS(25)
, TimeOut(15 * 60) /* 15 minutes in s */
, FadeIn(0) /* milliseconds*/
, Instant(false)
, Interval(5) /* seconds; 0 = no off*/
, ReportFPS(false)
, Models()
{
Add(_T("height"), &Height);
Add(_T("width"), &Width);
Add(_T("fps"), &FPS);
Add(_T("timeout"), &TimeOut);
Add(_T("fadein"), &FadeIn);
Add(_T("instant"), &Instant);
Add(_T("interval"), &Interval);
Add(_T("reportfps"), &ReportFPS);
Add(_T("models"), &Models);
}
~Config()
{
}
public:
Core::JSON::DecUInt16 Height;
Core::JSON::DecUInt16 Width;
Core::JSON::DecUInt8 FPS;
Core::JSON::DecUInt16 TimeOut;
Core::JSON::DecUInt16 FadeIn;
Core::JSON::Boolean Instant;
Core::JSON::DecUInt8 Interval;
Core::JSON::Boolean ReportFPS;
Core::JSON::ArrayType<Graphics::ModelConfig> Models;
};
public:
// IPlugin methods
// -------------------------------------------------------------------------------------------------------
const string Initialize(PluginHost::IShell* service) override;
void Deinitialize(PluginHost::IShell* service) override;
string Information() const override;
// IWeb methods
// -------------------------------------------------------------------------------------------------------
void Inbound(Web::Request& request) override;
Core::ProxyType<Web::Response> Process(const Web::Request& request) override;
// JSON RPC
// -------------------------------------------------------------------------------------------------------
void JSONRPCRegister();
void JSONRPCUnregister();
uint32_t JSONRPCPause();
uint32_t JSONRPCResumed();
private:
void RenderUpdate();
inline uint32_t Pause()
{
_eglRender.Pause();
return Core::ERROR_NONE;
}
inline uint32_t Resume()
{
_eglRender.Resume();
_ticker->Schedule(_interval);
return Core::ERROR_NONE;
}
inline uint32_t Hide()
{
Trigger();
return Core::ERROR_NONE;
}
inline uint32_t Show()
{
_eglRender.Show();
_ticker->Schedule(_interval);
return Core::ERROR_NONE;
}
void Trigger()
{
if (Core::Time::Now().Add(_timeOut * 1000).Ticks() >= (_startTime + (Core::Time::TicksPerMillisecond * 1000 * 2))) {
TRACE(Trace::Information, ("Input detected!"));
_eglRender.Hide();
_startTime = Core::Time::Now().Add(_timeOut * 1000).Ticks();
_triggered = false;
} else {
TRACE(Trace::Information, ("Hey input! Cooldown!"));
}
}
bool Tack()
{
if ((_startTime - Core::Time::Now().Ticks() <= 0) && (_triggered == false)) {
_eglRender.Show();
_triggered = true;
}
if (_reportFPS == true) {
RenderUpdate();
}
return _triggered;
}
bool IsActive() const
{
return _eglRender.IsActive();
}
private:
uint8_t _skipURL;
Graphics::EGLRender _eglRender;
PluginHost::IShell* _service;
bool _triggered;
uint32_t _previousFrames;
uint64_t _previousTimeMS;
uint16_t _interval;
uint16_t _timeOut;
uint64_t _startTime;
bool _reportFPS;
InputSink _inputSink;
Core::ProxyType<Tick> _ticker;
InputServer _inputServer;
};
} // namespace Plugin
} // namespace Thunder