PowerComponentWin.cpp 1.8 KB

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