-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
98 lines (74 loc) · 2.38 KB
/
main.cpp
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
#include <QApplication>
#include <QSystemTrayIcon>
#include <QIcon>
#include <QMenu>
#include <QAction>
#include <QStyleFactory>
#include <QObject>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QProcess>
#include <QFileSystemWatcher>
#include <QDebug>
const QString menuStyles =
"QMenu {"
"background: #080808;"
"color: #d6d6d6;"
"}"
"QMenu::item:selected {"
"background: #242424;"
"}";
const QString bbswitch = "/proc/acpi/bbswitch";
QString read(QString filename) {
QFile file(filename);
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(nullptr, "error", file.errorString());
}
QTextStream in(&file);
return in.readAll();
}
bool isOn() {
return read(bbswitch).split(" ")[1].trimmed() != "OFF";
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QIcon iconOn(":/assets/on.png");
QIcon iconOff(":/assets/off.png");
bool on = isOn();
QSystemTrayIcon tray(on ? iconOn : iconOff);
qInfo() << "Started monitoring, current state:" << (on ? "ON" : "OFF");
QMenu menu;
menu.setStyleSheet(menuStyles);
auto toggleAction = new QAction("Toggle GPU");
auto checkAction = new QAction("Force-check status");
auto quitAction = new QAction("Quit");
QObject::connect(toggleAction, &QAction::triggered, [] {
bool on = isOn();
int exitCode = QProcess::execute("pkexec", QStringList() << "sh" << "-c" << QString("echo %1 >> %2 ").arg(on ? "OFF" : "ON", bbswitch));
if (exitCode != 0) {
QMessageBox::information(nullptr, "error", QString("Error toggling GPU.\nExit code: %1").arg(exitCode));
}
});
QObject::connect(checkAction, &QAction::triggered, [&tray, iconOn, iconOff] {
tray.setIcon(isOn() ? iconOn : iconOff);
});
QObject::connect(quitAction, &QAction::triggered, [] {
std::exit(0);
});
menu.addAction(toggleAction);
menu.addAction(checkAction);
menu.addAction(quitAction);
tray.setContextMenu(&menu);
tray.show();
QFileSystemWatcher watcher;
watcher.addPath(bbswitch);
QObject::connect(&watcher, &QFileSystemWatcher::fileChanged, [&tray, iconOn, iconOff] {
bool on = isOn();
qInfo() << "GPU has been turned" << (on ? "ON" : "OFF");
tray.setIcon(on ? iconOn : iconOff);
});
return a.exec();
}