-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbswitchchecker.cpp
156 lines (150 loc) · 4.99 KB
/
bbswitchchecker.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
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
#include "bbswitchchecker.hpp"
#include <QByteArray>
#include <QStringList>
#include "log.h"
#include <QProcess>
bbswitchChecker::bbswitchChecker(QObject *parent) : QObject(parent)
{
rexp = QRegExp(".*(\\[[ 0-9.]*\\]) (.*)");
connect(&f, SIGNAL(readyRead()), this, SLOT(check()));
connect(&t, SIGNAL(timeout()), this, SLOT(check()));
connect(&tRestart, SIGNAL(timeout()), this, SLOT(check()));
t.start(1000);
tRestart.setInterval(1000 * 3600);
tRestart.start();
f.setFileName("/var/log/messages");
QTimer::singleShot(0, this, SIGNAL(check()));
}
void bbswitchChecker::check()
{
bool shouldBe = false;
bool foundBbinfo = false;
bool unknow = false;
QString mode;
if (this->sender() == &f)
Log::addLog(QString() + "ow: trigger by file");
else if (this->sender() == &tRestart)
Log::addLog(QString() + "ow: trigger by tempoRestart");
if (f.isOpen())
{
QByteArray b;
b = f.readLine();
if (b.size() == 0 && this->sender() == &tRestart)
{
Log::addLog(QString() + "ow: file is open, but no log since 1m, try re-open...");
f.close();
QTimer::singleShot(0, this, SIGNAL(check()));
}
else if (b.size() != 0)
tRestart.start();
while (b.length())
{
int pos = rexp.indexIn(b.trimmed());
if (pos > -1)
{
QByteArray line = rexp.cap(2).toUtf8();
Log::addLog(QString() + "lm: " + line);
if (line == "bbswitch: disabling discrete graphics")
{
shouldBe = false;
foundBbinfo = true;
unknow = false;
}
if (line == "bbswitch: Succesfully loaded. Discrete card 0000:01:00.0 is on" ||
line == "bbswitch: Unloaded. Discrete card 0000:01:00.0 is on")
{
shouldBe = true;
foundBbinfo = true;
unknow = false;
}
else if (line == "bbswitch: Succesfully loaded. Discrete card 0000:01:00.0 is off" ||
line == "bbswitch: Unloaded. Discrete card 0000:01:00.0 is off")
{
shouldBe = false;
foundBbinfo = true;
unknow = false;
}
else if (line == "bbswitch: device 0000:01:00.0 is in use by driver 'nvidia', refusing OFF")
{
shouldBe = true;
foundBbinfo = true;
unknow = false;
}
else if (line == "bbswitch: enabling discrete graphics")
{
shouldBe = true;
foundBbinfo = true;
unknow = false;
}
if (line == "pci 0000:01:00.0: Refused to change power state, currently in D0")
{
unknow = true;
mode = "D0";
}
else if (line == "pci 0000:01:00.0: Refused to change power state, currently in D3Cold")
{
unknow = true;
mode = "D3Cold";
}
else if (line == "OUT OF SLEEP, NVIDIA CARD SHOULD BE OFF")
{
unknow = true;
shouldBe = false;
mode = "Wake Off";
}
else if (line == "OUT OF SLEEP, NVIDIA CARD SHOULD BE ON")
{
unknow = true;
shouldBe = false;
mode = "Wake On";
}
else if (line == "nouveau 0000:01:00.0: DRM: resuming object tree..." ||
line == "nouveau 0000:01:00.0: DRM: resuming fence...")
{
shouldBe = true;
foundBbinfo = true;
unknow = false;
}
else if (line == "nouveau 0000:01:00.0: DRM: suspending object tree..." ||
line == "nouveau 0000:01:00.0: DRM: suspending fence...")
{
shouldBe = false;
foundBbinfo = true;
unknow = false;
}
else if (line.contains("bbswitch_proc_write"))
{
unknow = true;
foundBbinfo = false;
shouldBe = false;
mode = "D0";
Log::addLog("ow:INFO:reloading bbswitch module");
QProcess::execute("sudo modprobe -r bbswitch");
QProcess::execute("sudo modprobe bbswitch");
}
}
b = f.readLine();
}
if (unknow)
emit unknowState(foundBbinfo, shouldBe, mode);
else if (foundBbinfo)
emit newState(shouldBe);
}
else
{
Log::addLog("ow:INFO:re-opening syslog file!");
f.open(QFile::ReadOnly);
if (!f.isOpen())
// Warning
Log::addLog("ow:ERROR:optimus_watcher can't open syslog!");
else
{
unsigned int pos;
pos = f.size() - 2000;
if (pos > 0)
f.seek(pos);
emit unknowState(false, false, "??");
QTimer::singleShot(0, this, SIGNAL(check()));
}
}
}