-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecision.cpp
More file actions
209 lines (190 loc) · 6.34 KB
/
decision.cpp
File metadata and controls
209 lines (190 loc) · 6.34 KB
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
#include "decision.hpp"
#include <QDebug>
#include <QApplication>
#include <QUrl>
#include "log.h"
#include <QProcess>
QProcess pglx;
QProcess pprocs;
decision::decision(QObject *parent) : QObject(parent)
{
QStringList l;
QAction *tmp;
l << "glxgears" << "-info";
pglx.setProgram("/usr/bin/primusrun");
pglx.setArguments(l);
pprocs.setProgram("/home/rholk/sbin/nvprocess");
pglx.setProcessChannelMode(QProcess::MergedChannels);
pglx.setStandardOutputFile("/tmp/glxgears", QProcess::Append);
on = QIcon(":/nvidia-icon.png");
off = QIcon(":/nvidia-icon-gray.png");
unknow = QIcon(":/nvidia-icon-orange.png");
this->autoHandle = m.addAction("auto correct");
this->processes = m.insertSeparator(this->autoHandle);
tmp = m.addAction("--Purge ALL--", this, SLOT(purge()));
m.insertAction(this->processes, tmp);
this->purgeaction = new QMenu("Processes");
this->purgeaction->setProperty("lol", "toto");
m.insertMenu(tmp, this->purgeaction);
this->processes = m.insertSeparator(this->autoHandle);
this->autoHandle->setCheckable(true);
this->autoHandle->setChecked(true);
this->cleanBnet = m.addAction("Clean Bnet", this, SIGNAL(bnetClean()));
this->cleanBnet->setCheckable(false);
this->glx = m.addAction("glxgears", this, SLOT(glxgears()));
this->glx->setCheckable(false);
this->exit = m.addAction("exit",QApplication::instance(), SLOT(quit()));
this->poll.setInterval(2000);
this->purgeaction->setEnabled(false);
connect(&(this->poll), SIGNAL(timeout()),
this, SLOT(getprocesses()));
connect(&pprocs, SIGNAL(finished(int)),
this, SLOT(analyze(int)));
}
void decision::purge()
{
QMap<QAction*, unsigned int>::iterator it = this->_processesActions.begin();
while (it != this->_processesActions.end())
{
QString cmd;
cmd = QString() + "sudo kill -9 " + QString::number(it.value());
this->showNotif("Optimus", "Killing " + this->_processes[it.value()] + "\n" + cmd, QSystemTrayIcon::Information, 5000);
QProcess::startDetached(cmd);
it++;
}
}
void decision::kill()
{
QAction* act = dynamic_cast<QAction*>(this->sender());
if (this->_processesActions.contains(act))
{
QString cmd;
cmd = QString() + "sudo kill -9 " + QString::number(this->_processesActions[act]);
this->showNotif("Optimus", "Killing " + this->_processes[this->_processesActions[act]] + "\n" + cmd, QSystemTrayIcon::Information, 5000);
QProcess::startDetached(cmd);
}
}
void decision::analyze(int)
{
QMap<unsigned int, QString> procs;
while (true)
{
QByteArray sl = pprocs.readLine().trimmed();
if (sl.size() == 0)
break ;
QList<QByteArray> slist = sl.split(';');
if (slist.size() >= 2)
procs[QString(slist[0]).toUInt()] = slist[1];
}
for (QMap<unsigned int, QString>::iterator it = procs.begin(); it != procs.end(); it++)
{
if (this->_processes.contains(it.key()) == false)
{
// adding
Log::addLog(QString() + "ow:INFO:Adding new process:" + QString::number(it.key()) + " " + it.value());
this->_processes[it.key()] = it.value();
QAction * act = new QAction(it.value());
connect(act, SIGNAL(triggered(bool)),
this, SLOT(kill()));
qDebug() << "Inserting" << act << "before" << this->purgeaction;
this->purgeaction->insertAction(NULL, act);
this->_processesActions[act] = it.key();
}
}
QMap<QAction*, unsigned int>::iterator it = this->_processesActions.begin();
while (it != this->_processesActions.end())
{
if (procs.contains(it.value()) == false)
{
Log::addLog(QString() + "ow:INFO:removing old process: " + QString::number(it.value()));
delete it.key();
_processes.remove(it.value());
_processesActions.remove(it.key());
it = this->_processesActions.begin();
}
else
it++;
}
if (this->purgeaction->actions().size())
this->purgeaction->setEnabled(true);
else
this->purgeaction->setEnabled(false);
}
void decision::getprocesses()
{
if (pprocs.state() != QProcess::Running)
pprocs.start();
}
void decision::glxgears()
{
if (pglx.state() == QProcess::Running)
pglx.kill();
else
pglx.start();
}
void decision::newState(bool st)
{
if (st)
{
this->poll.start();
this->_ic->setIcon(on);
system("sudo touch /tmp/nvidiaunsafe");
}
else
{
this->poll.stop();
this->_ic->setIcon(off);
system("sudo rm /tmp/nvidiaunsafe");
}
this->_ic->show();
}
void decision::_tryCorrect(bool bbinfo, bool shouldbe, QString pciReport)
{
if (pciReport == "D0" && bbinfo && shouldbe == false && this->autoHandle->isChecked())
{
Log::addLog("ow:INFO:on then off because of D0 glitch");
emit poweron();
emit poweroff();
}
else if (pciReport == "Wake Off" && this->autoHandle->isChecked())
{
Log::addLog("ow:INFO:on then off because of wakeup off");
emit poweron();
emit poweroff();
}
else if (pciReport == "Wake On" && this->autoHandle->isChecked())
{
Log::addLog("ow:INFO:on then off because of wakeup on");
QTimer::singleShot(2000, this, SIGNAL(poweron()));
QTimer::singleShot(2300, this, SIGNAL(poweroff()));
}
else if (pciReport == "??" && shouldbe == false && this->autoHandle->isChecked())
{
Log::addLog("ow:INFO:Try on then off");
emit poweron();
emit poweroff();
}
else
{
emit ask(bbinfo, shouldbe, pciReport);
}
}
void decision::unknowState(bool bbinfo, bool shouldbe, QString pciReport)
{
this->_ic->setIcon(unknow);
this->_ic->show();
this->_tryCorrect(bbinfo, shouldbe, pciReport);
system("sudo touch /tmp/nvidiaunsafe");
}
void decision::setSysTray(QSystemTrayIcon* ic)
{
this->_ic = ic;
this->_ic->setContextMenu(&m);
this->_ic->setIcon(unknow);
this->_ic->setVisible(true);
this->_ic->show();
}
void decision::showNotif(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int millisecondsTimeoutHint)
{
this->_ic->showMessage(title, message, icon, millisecondsTimeoutHint);
}