2
0

pwmfan.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman maldela@halloarsch.de
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License or (at your option) version 3 or any later version
  9. * accepted by the membership of KDE e.V. (or its successor approved
  10. * by the membership of KDE e.V.), which shall act as a proxy
  11. * defined in Section 14 of version 3 of the license.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "pwmfan.h"
  23. #include "hwmon.h"
  24. #include <QtCore/QTextStream>
  25. #include <QtCore/QTimer>
  26. #include <QtCore/QDir>
  27. #include <QtCore/QFile>
  28. #include <QtCore/QDebug>
  29. #include <KConfigCore/KConfigGroup>
  30. #include <KConfigCore/KSharedConfig>
  31. #include <KAuth/KAuthExecuteJob>
  32. #include <KI18n/KLocalizedString>
  33. #define MAX_ERRORS_FOR_RPM_ZERO 10
  34. namespace Fancontrol
  35. {
  36. PwmFan::PwmFan(Hwmon *parent, uint index) : Fan(parent, index),
  37. m_pwmStream(new QTextStream),
  38. m_modeStream(new QTextStream),
  39. m_temp(Q_NULLPTR),
  40. m_hasTemp(false),
  41. m_minTemp(0),
  42. m_maxTemp(100),
  43. m_minPwm(255),
  44. m_maxPwm(255),
  45. m_minStart(255),
  46. m_minStop(255),
  47. m_zeroRpm(0),
  48. m_testStatus(NotStarted)
  49. {
  50. connect(this, SIGNAL(tempChanged()), parent, SLOT(updateConfig()));
  51. connect(this, SIGNAL(hasTempChanged()), parent, SLOT(updateConfig()));
  52. connect(this, SIGNAL(minTempChanged()), parent, SLOT(updateConfig()));
  53. connect(this, SIGNAL(maxTempChanged()), parent, SLOT(updateConfig()));
  54. connect(this, SIGNAL(minPwmChanged()), parent, SLOT(updateConfig()));
  55. connect(this, SIGNAL(maxPwmChanged()), parent, SLOT(updateConfig()));
  56. connect(this, SIGNAL(minStartChanged()), parent, SLOT(updateConfig()));
  57. connect(this, SIGNAL(minStopChanged()), parent, SLOT(updateConfig()));
  58. if (QDir(parent->path()).isReadable())
  59. {
  60. QFile *pwmFile = new QFile(parent->path() + "/pwm" + QString::number(index), this);
  61. if (pwmFile->open(QFile::ReadWrite))
  62. {
  63. m_pwmStream->setDevice(pwmFile);
  64. *m_pwmStream >> m_pwm;
  65. }
  66. else if (pwmFile->open(QFile::ReadOnly))
  67. {
  68. m_pwmStream->setDevice(pwmFile);
  69. *m_pwmStream >> m_pwm;
  70. }
  71. else
  72. qWarning() << "Can't open pwmFile " << pwmFile->fileName();
  73. QFile *pwmModeFile = new QFile(parent->path() + "/pwm" + QString::number(index) + "_mode", this);
  74. if (pwmModeFile->open(QFile::ReadWrite))
  75. {
  76. m_modeStream->setDevice(pwmModeFile);
  77. *m_modeStream >> m_pwmMode;
  78. }
  79. else if (pwmModeFile->open(QFile::ReadOnly))
  80. {
  81. m_modeStream->setDevice(pwmModeFile);
  82. *m_modeStream >> m_pwmMode;
  83. }
  84. else
  85. qWarning() << "Can't open pwmModeFile " << pwmModeFile->fileName();
  86. }
  87. }
  88. PwmFan::~PwmFan()
  89. {
  90. delete m_pwmStream;
  91. delete m_modeStream;
  92. }
  93. void PwmFan::update()
  94. {
  95. Fan::update();
  96. m_pwmStream->seek(0);
  97. setPwm(m_pwmStream->readAll().toInt(), false);
  98. m_modeStream->seek(0);
  99. setPwmMode(m_modeStream->readAll().toInt(), false);
  100. }
  101. void PwmFan::reset()
  102. {
  103. Fan::reset();
  104. QIODevice *oldFile = m_pwmStream->device();
  105. delete m_pwmStream;
  106. delete oldFile;
  107. oldFile = m_modeStream->device();
  108. delete m_modeStream;
  109. delete oldFile;
  110. QFile *pwmFile = new QFile(m_parent->path() + "/pwm" + QString::number(m_index), this);
  111. if (pwmFile->open(QFile::ReadWrite))
  112. {
  113. m_pwmStream = new QTextStream(pwmFile);
  114. *m_pwmStream >> m_pwm;
  115. }
  116. else if (pwmFile->open(QFile::ReadOnly))
  117. {
  118. m_pwmStream = new QTextStream(pwmFile);
  119. *m_pwmStream >> m_pwm;
  120. }
  121. else
  122. qWarning() << "Can't open pwmFile " << pwmFile->fileName();
  123. QFile *pwmModeFile = new QFile(m_parent->path() + "/pwm" + QString::number(m_index) + "_mode", this);
  124. if (pwmModeFile->open(QFile::ReadWrite))
  125. {
  126. m_modeStream = new QTextStream(pwmModeFile);
  127. *m_modeStream >> m_pwmMode;
  128. }
  129. else if (pwmModeFile->open(QFile::ReadOnly))
  130. {
  131. m_modeStream = new QTextStream(pwmModeFile);
  132. *m_modeStream >> m_pwmMode;
  133. }
  134. else
  135. qWarning() << "Can't open pwmModeFile " << pwmModeFile->fileName();
  136. }
  137. bool PwmFan::setPwm(int pwm, bool write)
  138. {
  139. if (m_pwm != pwm)
  140. {
  141. m_pwm = pwm;
  142. emit pwmChanged();
  143. if (write)
  144. {
  145. setPwmMode(1);
  146. if (m_pwmStream->device()->isWritable())
  147. *m_pwmStream << pwm;
  148. else
  149. {
  150. KAuth::Action action("fancontrol.gui.helper.action");
  151. action.setHelperId("fancontrol.gui.helper");
  152. if (!action.isValid())
  153. {
  154. emit errorChanged(i18n("Action is invalid"));
  155. return false;
  156. }
  157. QVariantMap map;
  158. map["action"] = "write";
  159. map["filename"] = qobject_cast<QFile *>(m_pwmStream->device())->fileName();
  160. map["content"] = QString::number(pwm);
  161. action.setArguments(map);
  162. KAuth::ExecuteJob *job = action.execute();
  163. connect(job, SIGNAL(result(KJob*)), this, SLOT(handleSetPwmResult(KJob*)));
  164. job->start();
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. void PwmFan::handleSetPwmResult(KJob *job)
  171. {
  172. if (job->error())
  173. {
  174. if (job->error() == KAuth::ActionReply::HelperBusyError)
  175. {
  176. qDebug() << "Helper busy...";
  177. QTimer::singleShot(50, this, [this] (){ setPwm(m_pwm); });
  178. return;
  179. }
  180. emit errorChanged(i18n("Could not set pwm: ") + job->errorText());
  181. return;
  182. }
  183. update();
  184. }
  185. bool PwmFan::setPwmMode(int pwmMode, bool write)
  186. {
  187. if (m_pwmMode != pwmMode)
  188. {
  189. m_pwmMode = pwmMode;
  190. emit pwmModeChanged();
  191. if (write)
  192. {
  193. if (m_modeStream->device()->isWritable())
  194. *m_modeStream << pwmMode;
  195. else
  196. {
  197. KAuth::Action action("fancontrol.gui.helper.action");
  198. action.setHelperId("fancontrol.gui.helper");
  199. if (!action.isValid())
  200. {
  201. emit errorChanged(i18n("Action is invalid"));
  202. return false;
  203. }
  204. QVariantMap map;
  205. map["action"] = "write";
  206. map["filename"] = qobject_cast<QFile *>(m_modeStream->device())->fileName();
  207. map["content"] = QString::number(pwmMode);
  208. action.setArguments(map);
  209. KAuth::ExecuteJob *job = action.execute();
  210. connect(job, SIGNAL(result(KJob*)), this, SLOT(handleSetPwmModeResult(KJob*)));
  211. job->start();
  212. }
  213. }
  214. }
  215. return true;
  216. }
  217. void PwmFan::handleSetPwmModeResult(KJob *job)
  218. {
  219. if (job->error())
  220. {
  221. if (job->error() == KAuth::ActionReply::HelperBusyError)
  222. {
  223. qDebug() << "Helper busy...";
  224. QTimer::singleShot(50, this, [this] (){ setPwmMode(m_pwmMode); });
  225. return;
  226. }
  227. emit errorChanged(i18n("Could not set pwm mode: ") + job->errorText());
  228. return;
  229. }
  230. update();
  231. }
  232. bool PwmFan::test()
  233. {
  234. KAuth::Action action("fancontrol.gui.helper.action");
  235. action.setHelperId("fancontrol.gui.helper");
  236. if (!action.isValid())
  237. {
  238. emit errorChanged("Test action is invalid");
  239. return false;
  240. }
  241. KAuth::ExecuteJob *job = action.execute();
  242. connect(job, SIGNAL(result(KJob*)), this, SLOT(handleTestAuthReply(KJob*)));
  243. job->start();
  244. return true;
  245. }
  246. void PwmFan::handleTestAuthReply(KJob *job)
  247. {
  248. if (job->error())
  249. {
  250. if (job->error() == KAuth::ActionReply::HelperBusyError)
  251. {
  252. qDebug() << "Helper busy...";
  253. QTimer::singleShot(100, this, &PwmFan::test);
  254. return;
  255. }
  256. emit errorChanged(i18n("Authorization error: ") + job->errorText());
  257. m_testStatus = Error;
  258. emit testingChanged();
  259. return;
  260. }
  261. setPwm(255);
  262. m_testStatus = FindingStop1;
  263. emit testingChanged();
  264. QTimer::singleShot(500, this, SLOT(continueTest()));
  265. qDebug() << "Start testing...";
  266. }
  267. void PwmFan::abortTest()
  268. {
  269. if (m_testStatus >= FindingStop1 && m_testStatus <= FindingStart)
  270. {
  271. qDebug() << "Abort testing";
  272. disconnect(this, 0, this, SLOT(continueTest()));
  273. m_testStatus = Cancelled;
  274. emit testingChanged();
  275. setPwm(255);
  276. }
  277. }
  278. void PwmFan::continueTest()
  279. {
  280. KAuth::Action action("fancontrol.gui.helper.action");
  281. action.setHelperId("fancontrol.gui.helper");
  282. if (!action.isValid())
  283. {
  284. emit errorChanged("Test action is invalid");
  285. return;
  286. }
  287. if (action.status() != KAuth::Action::AuthorizedStatus)
  288. {
  289. m_testStatus = Error;
  290. emit testingChanged();
  291. return;
  292. }
  293. update();
  294. switch (m_testStatus)
  295. {
  296. case FindingStop1:
  297. if (m_rpm > 0)
  298. {
  299. setPwm(qMin(m_pwm * 0.95, m_pwm - 5.0));
  300. m_zeroRpm = 0;
  301. }
  302. else
  303. {
  304. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  305. {
  306. m_zeroRpm++;
  307. }
  308. else
  309. {
  310. m_testStatus = FindingStart;
  311. m_zeroRpm = 0;
  312. qDebug() << "Start finding start value...";
  313. }
  314. }
  315. QTimer::singleShot(500, this, SLOT(continueTest()));
  316. break;
  317. case FindingStart:
  318. if (m_rpm == 0)
  319. setPwm(m_pwm + 2);
  320. else
  321. {
  322. m_testStatus = FindingStop2;
  323. setMinStart(m_pwm);
  324. qDebug() << "Start finding stop value...";
  325. }
  326. QTimer::singleShot(1000, this, SLOT(continueTest()));
  327. break;
  328. case FindingStop2:
  329. if (m_rpm > 0)
  330. {
  331. setPwm(m_pwm - 1);
  332. m_zeroRpm = 0;
  333. QTimer::singleShot(1000, this, SLOT(continueTest()));
  334. }
  335. else
  336. {
  337. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  338. {
  339. m_zeroRpm++;
  340. QTimer::singleShot(500, this, SLOT(continueTest()));
  341. }
  342. else
  343. {
  344. m_testStatus = Finished;
  345. emit testingChanged();
  346. m_zeroRpm = 0;
  347. setMinStop(m_pwm + 5);
  348. qDebug() << "Finished testing!";
  349. }
  350. }
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. bool PwmFan::testing() const
  357. {
  358. return m_testStatus == FindingStop1 || m_testStatus == FindingStop2 || m_testStatus == FindingStart;
  359. }
  360. bool PwmFan::active() const
  361. {
  362. KConfigGroup active = KSharedConfig::openConfig("fancontrol-gui")->group("active");
  363. KConfigGroup localActive = active.group(m_parent->name());
  364. return localActive.readEntry("pwmfan" + QString::number(m_index), true);
  365. }
  366. void PwmFan::setActive(bool a)
  367. {
  368. KConfigGroup active = KSharedConfig::openConfig("fancontrol-gui")->group("active");
  369. KConfigGroup localActive = active.group(m_parent->name());
  370. if (a != localActive.readEntry("pwmfan" + QString::number(m_index), true))
  371. {
  372. localActive.writeEntry("pwmfan" + QString::number(m_index), a);
  373. emit activeChanged();
  374. }
  375. }
  376. }