PowerComponentX11.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <stdlib.h>
  2. #include "PowerComponentX11.h"
  3. #include "QsLog.h"
  4. ///////////////////////////////////////////////////////////////////////////////////////////////////
  5. PowerComponentX11::PowerComponentX11() : PowerComponent(0)
  6. {
  7. m_timer = new QTimer(this);
  8. connect(m_timer, &QTimer::timeout, this, &PowerComponentX11::onTimer);
  9. m_timer->setInterval(15 * 1000);
  10. }
  11. ///////////////////////////////////////////////////////////////////////////////////////////////////
  12. void PowerComponentX11::onTimer()
  13. {
  14. if (!m_process && !m_broken)
  15. {
  16. m_process = new QProcess(this);
  17. m_process->setProcessChannelMode(QProcess::ForwardedChannels);
  18. connect(m_process, (void (QProcess::*)(int,QProcess::ExitStatus))&QProcess::finished,
  19. this, &PowerComponentX11::onProcessFinished);
  20. connect(m_process, (void (QProcess::*)(QProcess::ProcessError))&QProcess::error,
  21. this, &PowerComponentX11::onProcessError);
  22. m_process->start("xdg-screensaver", {"reset"});
  23. }
  24. }
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////
  26. void PowerComponentX11::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
  27. {
  28. if (m_process)
  29. m_process->deleteLater();
  30. m_process = 0;
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////////////////////////
  33. void PowerComponentX11::onProcessError(QProcess::ProcessError error)
  34. {
  35. QLOG_ERROR() << "Disabling screensaver is not working. Make sure xdg-screensaver is installed.";
  36. m_broken = true;
  37. onProcessFinished(-1, QProcess::CrashExit);
  38. }
  39. ///////////////////////////////////////////////////////////////////////////////////////////////////
  40. void PowerComponentX11::doDisableScreensaver()
  41. {
  42. m_timer->start();
  43. }
  44. ///////////////////////////////////////////////////////////////////////////////////////////////////
  45. void PowerComponentX11::doEnableScreensaver()
  46. {
  47. m_timer->stop();
  48. }