PowerComponentWin.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <windows.h>
  2. #include "PowerComponentWin.h"
  3. #include "powrprof.h"
  4. #include "utils/Utils.h"
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////
  6. PowerComponentWin::PowerComponentWin() : PowerComponent(nullptr), m_hasPrivileges(false)
  7. {
  8. m_hasPrivileges = WinUtils::getPowerManagementPrivileges();
  9. }
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////
  11. void PowerComponentWin::doDisableScreensaver()
  12. {
  13. SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);
  14. }
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////
  16. void PowerComponentWin::doEnableScreensaver()
  17. {
  18. SetThreadExecutionState(ES_CONTINUOUS);
  19. }
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////
  21. bool PowerComponentWin::Suspend()
  22. {
  23. return SetSuspendState(false, true, false) == TRUE;
  24. }
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////
  26. bool PowerComponentWin::Reboot()
  27. {
  28. return InitiateShutdownW(NULL, NULL, 0, SHUTDOWN_INSTALL_UPDATES | SHUTDOWN_RESTART,
  29. SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED) == ERROR_SUCCESS;
  30. }
  31. ///////////////////////////////////////////////////////////////////////////////////////////////////
  32. bool PowerComponentWin::PowerOff()
  33. {
  34. DWORD shutdownFlags = SHUTDOWN_INSTALL_UPDATES | SHUTDOWN_POWEROFF;
  35. if (QSysInfo::WindowsVersion >= QSysInfo::WV_WINDOWS8)
  36. shutdownFlags |= SHUTDOWN_HYBRID;
  37. return InitiateShutdownW(NULL, NULL, 0, shutdownFlags,
  38. SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED) == ERROR_SUCCESS;
  39. }