-
Notifications
You must be signed in to change notification settings - Fork 21
/
controller.h
533 lines (506 loc) · 18.4 KB
/
controller.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QObject>
#include <QQuickItem>
#include <liboxide.h>
using namespace codes::eeems::oxide1;
using namespace Oxide::Sentry;
enum State { Normal, PowerSaving };
enum BatteryState { BatteryUnknown, BatteryCharging, BatteryDischarging, BatteryNotPresent };
enum ChargerState { ChargerUnknown, ChargerConnected, ChargerNotConnected, ChargerNotPresent };
enum WifiState { WifiUnknown, WifiOff, WifiDisconnected, WifiOffline, WifiOnline};
class Controller : public QObject {
Q_OBJECT
Q_PROPERTY(bool powerOffInhibited READ powerOffInhibited NOTIFY powerOffInhibitedChanged)
Q_PROPERTY(bool sleepInhibited READ sleepInhibited NOTIFY sleepInhibitedChanged)
Q_PROPERTY(bool firstLaunch READ firstLaunch WRITE setFirstLaunch NOTIFY firstLaunchChanged)
Q_PROPERTY(bool telemetry READ telemetry WRITE setTelemetry NOTIFY telemetryChanged)
Q_PROPERTY(bool applicationUsage READ applicationUsage WRITE setApplicationUsage NOTIFY applicationUsageChanged)
Q_PROPERTY(bool crashReport READ crashReport WRITE setCrashReport NOTIFY crashReportChanged)
public:
Controller(QObject* parent)
: QObject(parent), confirmPin() {
clockTimer = new QTimer(root);
auto bus = QDBusConnection::systemBus();
qDebug() << "Waiting for tarnish to start up...";
while(!bus.interface()->registeredServiceNames().value().contains(OXIDE_SERVICE)){
struct timespec args{
.tv_sec = 1,
.tv_nsec = 0,
}, res;
nanosleep(&args, &res);
}
api = new General(OXIDE_SERVICE, OXIDE_SERVICE_PATH, bus, this);
qDebug() << "Requesting system API...";
QDBusObjectPath path = api->requestAPI("system");
if(path.path() == "/"){
qDebug() << "Unable to get system API";
throw "";
}
systemApi = new System(OXIDE_SERVICE, path.path(), bus, this);
connect(systemApi, &System::sleepInhibitedChanged, this, &Controller::sleepInhibitedChanged);
connect(systemApi, &System::powerOffInhibitedChanged, this, &Controller::powerOffInhibitedChanged);
connect(systemApi, &System::deviceSuspending, this, &Controller::deviceSuspending);
qDebug() << "Requesting power API...";
path = api->requestAPI("power");
if(path.path() == "/"){
qDebug() << "Unable to get power API";
throw "";
}
powerApi = new Power(OXIDE_SERVICE, path.path(), bus, this);
connect(powerApi, &Power::batteryLevelChanged, this, &Controller::batteryLevelChanged);
connect(powerApi, &Power::batteryStateChanged, this, &Controller::batteryStateChanged);
connect(powerApi, &Power::chargerStateChanged, this, &Controller::chargerStateChanged);
connect(powerApi, &Power::stateChanged, this, &Controller::powerStateChanged);
connect(powerApi, &Power::batteryAlert, this, &Controller::batteryAlert);
connect(powerApi, &Power::batteryWarning, this, &Controller::batteryWarning);
connect(powerApi, &Power::chargerWarning, this, &Controller::chargerWarning);
qDebug() << "Requesting wifi API...";
path = api->requestAPI("wifi");
if(path.path() == "/"){
qDebug() << "Unable to get wifi API";
throw "";
}
wifiApi = new Wifi(OXIDE_SERVICE, path.path(), bus, this);
connect(wifiApi, &Wifi::disconnected, this, &Controller::disconnected);
connect(wifiApi, &Wifi::networkConnected, this, &Controller::networkConnected);
connect(wifiApi, &Wifi::stateChanged, this, &Controller::wifiStateChanged);
connect(wifiApi, &Wifi::rssiChanged, this, &Controller::wifiRssiChanged);
qDebug() << "Requesting apps API...";
path = api->requestAPI("apps");
if(path.path() == "/"){
qDebug() << "Unable to get apps API";
throw "";
}
appsApi = new Apps(OXIDE_SERVICE, path.path(), bus, this);
QSettings settings;
if(QFile::exists(settings.fileName())){
qDebug() << "Importing old settings";
settings.sync();
if(settings.contains("pin")){
qDebug() << "Importing old pin";
sharedSettings.set_pin(settings.value("pin").toString());
}
if(settings.contains("onLogin")){
qDebug() << "Importing old onLogin";
sharedSettings.set_onLogin(settings.value("onLogin").toString());
}
if(settings.contains("onFailedLogin")){
qDebug() << "Importing old onFailedLogin";
sharedSettings.set_onFailedLogin(settings.value("onFailedLogin").toString());
}
settings.clear();
settings.sync();
QFile::remove(settings.fileName());
sharedSettings.sync();
}
connect(&sharedSettings, &Oxide::SharedSettings::firstLaunchChanged, this, &Controller::firstLaunchChanged);
connect(&sharedSettings, &Oxide::SharedSettings::telemetryChanged, this, &Controller::telemetryChanged);
connect(&sharedSettings, &Oxide::SharedSettings::applicationUsageChanged, this, &Controller::applicationUsageChanged);
connect(&sharedSettings, &Oxide::SharedSettings::crashReportChanged, this, &Controller::crashReportChanged);
}
~Controller(){
if(clockTimer->isActive()){
clockTimer->stop();
}
}
bool firstLaunch(){ return sharedSettings.firstLaunch(); }
void setFirstLaunch(bool firstLaunch){ sharedSettings.set_firstLaunch(firstLaunch); }
bool telemetry(){ return sharedSettings.telemetry(); }
void setTelemetry(bool telemetry){ sharedSettings.set_telemetry(telemetry); }
bool applicationUsage(){ return sharedSettings.applicationUsage(); }
void setApplicationUsage(bool applicationUsage){ sharedSettings.set_applicationUsage(applicationUsage); }
bool crashReport(){ return sharedSettings.crashReport(); }
void setCrashReport(bool crashReport){ sharedSettings.set_crashReport(crashReport); }
Q_INVOKABLE void startup(){
if(!getBatteryUI() || !getWifiUI() || !getClockUI() || !getStateControllerUI()){
QTimer::singleShot(100, this, &Controller::startup);
return;
}
qDebug() << "Running controller startup";
batteryLevelChanged(powerApi->batteryLevel());
batteryStateChanged(powerApi->batteryState());
chargerStateChanged(powerApi->chargerState());
powerStateChanged(powerApi->state());
wifiStateChanged(wifiApi->state());
wifiRssiChanged(wifiApi->rssi());
clockUI->setProperty("text", QTime::currentTime().toString("h:mm a"));
auto currentTime = QTime::currentTime();
QTime nextTime = currentTime.addSecs(60 - currentTime.second());
clockTimer->setInterval(currentTime.msecsTo(nextTime)); // nearest minute
QObject::connect(clockTimer , &QTimer::timeout, this, &Controller::updateClock);
clockTimer->start();
if(firstLaunch()){
qDebug() << "First launch";
QTimer::singleShot(100, [this]{
stateControllerUI->setProperty("state", "telemetry");
});
return;
}
// There is no PIN configuration
if(!sharedSettings.has_pin()){
qDebug() << "No Pin";
QTimer::singleShot(100, [this]{
stateControllerUI->setProperty("state", xochitlPin().isEmpty() ? "pinPrompt" : "import");
});
return;
}
// There is a PIN set
if(hasPin()){
qDebug() << "Prompting for PIN";
QTimer::singleShot(100, [this]{
setState("loaded");
});
return;
}
// PIN is set explicitly to a blank value
qDebug() << "No pin set";
QTimer::singleShot(100, [this]{
setState("noPin");
qApp->processEvents(QEventLoop::ExcludeUserInputEvents, 100);
previousApplication();
QTimer::singleShot(100, [this]{
setState("loading");
});
});
}
void launchStartupApp(){
QDBusObjectPath path = appsApi->startupApplication();
if(path.path() == "/"){
path = appsApi->getApplicationPath("codes.eeems.oxide");
}
if(path.path() == "/"){
O_WARNING("Unable to find startup application to launch.");
return;
}
Application app(OXIDE_SERVICE, path.path(), QDBusConnection::systemBus());
app.launch();
}
bool hasPin(){ return sharedSettings.has_pin() && storedPin().length(); }
void previousApplication(){
if(!appsApi->previousApplication()){
launchStartupApp();
}
}
Q_INVOKABLE void suspend(){
if(!sleepInhibited()){
systemApi->suspend().waitForFinished();
}
}
Q_INVOKABLE void powerOff(){
if(!powerOffInhibited()){
systemApi->powerOff().waitForFinished();
}
}
Q_INVOKABLE void reboot(){
if(!powerOffInhibited()){
systemApi->reboot().waitForFinished();
}
}
Q_INVOKABLE bool submitPin(QString pin){
if(pin.length() != 4){
return false;
}
qDebug() << "Checking PIN";
auto state = this->state();
if(state == "loaded" && pin == storedPin()){
qDebug() << "PIN matches!";
QTimer::singleShot(200, [this]{
onLogin();
if(getPinEntryUI()){
pinEntryUI->setProperty("message", "");
pinEntryUI->setProperty("pin", "");
}
setState("loading");
qApp->processEvents(QEventLoop::ExcludeUserInputEvents, 500);
QTimer::singleShot(200, [this]{
deviceSuspending();
previousApplication();
});
});
return true;
}else if(state == "loaded"){
qDebug() << "PIN doesn't match!";
O_DEBUG(pin << "!=" << storedPin());
onFailedLogin();
return false;
}
if(state == "prompt"){
confirmPin = pin;
QTimer::singleShot(200, [this]{
setState("confirmPin");
});
return true;
}
if(state != "confirmPin"){
return false;
}
if(pin != confirmPin){
qDebug() << "PIN doesn't match!";
setState("prompt");
return false;
}
qDebug() << "PIN matches!";
setStoredPin(pin);
QTimer::singleShot(200, [this]{
qApp->processEvents(QEventLoop::ExcludeUserInputEvents, 100);
setState("loading");
previousApplication();
});
return true;
}
Q_INVOKABLE void importPin(){
qDebug() << "Importing PIN from Xochitl";
setStoredPin(xochitlPin());
if(!storedPin().isEmpty()){
removeXochitlPin();
}
setState("loaded");
}
Q_INVOKABLE void clearPin(){
qDebug() << "Clearing PIN";
setStoredPin("");
startup();
}
Q_INVOKABLE void breadcrumb(QString category, QString message, QString type = "default"){
#ifdef SENTRY
sentry_breadcrumb(category.toStdString().c_str(), message.toStdString().c_str(), type.toStdString().c_str());
#else
Q_UNUSED(category);
Q_UNUSED(message);
Q_UNUSED(type);
#endif
}
bool sleepInhibited(){ return systemApi->sleepInhibited(); }
bool powerOffInhibited(){ return systemApi->powerOffInhibited(); }
QString state() {
if(!getStateControllerUI()){
return "loading";
}
return stateControllerUI->property("state").toString();
}
void setState(QString state){
if(!getStateControllerUI()){
throw "Unable to find state controller";
}
stateControllerUI->setProperty("state", state);
}
QString storedPin() {
if(!sharedSettings.has_pin()){
O_DEBUG("Does not have pin and storedPin was called");
return "";
}
return sharedSettings.pin();
}
void setStoredPin(QString pin) {
sharedSettings.set_pin(pin);
sharedSettings.sync();
}
void setRoot(QObject* root){ this->root = root; }
signals:
void sleepInhibitedChanged(bool);
void powerOffInhibitedChanged(bool);
void firstLaunchChanged(bool);
void telemetryChanged(bool);
void applicationUsageChanged(bool);
void crashReportChanged(bool);
private slots:
void deviceSuspending(){
if(getPinEntryUI()){
pinEntryUI->setProperty("message", "");
pinEntryUI->setProperty("pin", "");
}
if(state() == "confirmPin"){
confirmPin = "";
setState("pinPrompt");
}else{
setState("loading");
}
}
void updateClock(){
if(!getClockUI()){
return;
}
clockUI->setProperty("text", QTime::currentTime().toString("h:mm a"));
if(clockTimer->interval() != 60 * 1000){
clockTimer->setInterval(60 * 1000); // 1 minute
}
}
void disconnected(){
wifiStateChanged(wifiApi->state());
}
void networkConnected(){
wifiStateChanged(wifiApi->state());
}
void wifiStateChanged(int state){
if(!getWifiUI()){
return;
}
switch(state){
case WifiOff:
wifiUI->setProperty("state", "down");
break;
case WifiDisconnected:
wifiUI->setProperty("state", "up");
wifiUI->setProperty("connected", false);
break;
case WifiOffline:
wifiUI->setProperty("state", "up");
wifiUI->setProperty("connected", true);
break;
case WifiOnline:
wifiUI->setProperty("state", "up");
wifiUI->setProperty("connected", true);
wifiUI->setProperty("rssi", wifiApi->rssi());
break;
case WifiUnknown:
default:
wifiUI->setProperty("state", "unkown");
}
}
void wifiRssiChanged(int rssi){
if(!getWifiUI()){
return;
}
if(wifiApi->state() != WifiOnline){
rssi = -100;
}
wifiUI->setProperty("rssi", rssi);
}
void batteryLevelChanged(int level){
if(!getBatteryUI()){
return;
}
batteryUI->setProperty("level", level);
}
void batteryStateChanged(int state){
if(!getBatteryUI()){
return;
}
if(state != BatteryNotPresent){
batteryUI->setProperty("present", true);
}
switch(state){
case ChargerConnected:
batteryUI->setProperty("connected", true);
break;
case ChargerNotConnected:
case ChargerNotPresent:
batteryUI->setProperty("connected", false);
break;
case ChargerUnknown:
default:
batteryUI->setProperty("connected", false);
}
}
void chargerStateChanged(int state){
if(!getBatteryUI()){
return;
}
if(state != BatteryNotPresent){
batteryUI->setProperty("present", true);
}
switch(state){
case ChargerConnected:
batteryUI->setProperty("connected", true);
break;
case ChargerNotConnected:
case ChargerNotPresent:
batteryUI->setProperty("connected", false);
break;
case ChargerUnknown:
default:
batteryUI->setProperty("connected", false);
}
}
void powerStateChanged(int state){
Q_UNUSED(state);
// TODO handle requested battery state
}
void batteryAlert(){
if(!getBatteryUI()){
return;
}
batteryUI->setProperty("alert", true);
}
void batteryWarning(){
if(!getBatteryUI()){
return;
}
batteryUI->setProperty("warning", true);
}
void chargerWarning(){
// TODO handle charger
}
void onLogin(){
if(!sharedSettings.has_onLogin()){
return;
}
auto path = sharedSettings.onLogin();
if(!QFile::exists(path)){
O_WARNING("onLogin script does not exist" << path);
return;
}
if(!QFileInfo(path).isExecutable()){
O_WARNING("onLogin script is not executable" << path);
return;
}
QProcess::execute(path, QStringList());
}
void onFailedLogin(){
if(!sharedSettings.has_onFailedLogin()){
return;
}
auto path = sharedSettings.onFailedLogin();
if(!QFile::exists(path)){
O_WARNING("onFailedLogin script does not exist" << path);
return;
}
if(!QFileInfo(path).isExecutable()){
O_WARNING("onFailedLogin script is not executable" << path);
return;
}
QProcess::execute(path, QStringList());
}
private:
QString confirmPin;
General* api;
System* systemApi;
codes::eeems::oxide1::Power* powerApi;
Wifi* wifiApi;
Apps* appsApi;
QTimer* clockTimer = nullptr;
QObject* root = nullptr;
QObject* batteryUI = nullptr;
QObject* wifiUI = nullptr;
QObject* clockUI = nullptr;
QObject* stateControllerUI = nullptr;
QObject* pinEntryUI = nullptr;
int tarnishPid() { return api->tarnishPid(); }
QObject* getBatteryUI() {
batteryUI = root->findChild<QObject*>("batteryLevel");
return batteryUI;
}
QObject* getWifiUI() {
wifiUI = root->findChild<QObject*>("wifiState");
return wifiUI;
}
QObject* getClockUI() {
clockUI = root->findChild<QObject*>("clock");
return clockUI;
}
QObject* getStateControllerUI(){
stateControllerUI = root->findChild<QObject*>("stateController");
return stateControllerUI;
}
QObject* getPinEntryUI(){
pinEntryUI = root->findChild<QObject*>("pinEntry");
return pinEntryUI;
}
static QString xochitlPin(){ return xochitlSettings.passcode(); }
static void removeXochitlPin(){
xochitlSettings.remove("Passcode");
xochitlSettings.sync();
}
};
#endif // CONTROLLER_H