PowerComponent.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Created by Tobias Hieta on 25/03/15.
  3. //
  4. #include "PowerComponent.h"
  5. #include "player/PlayerComponent.h"
  6. #include "input/InputComponent.h"
  7. #ifdef Q_OS_MAC
  8. #include "PowerComponentMac.h"
  9. #elif KONVERGO_OPENELEC
  10. #include "PowerComponentOE.h"
  11. #elif USE_X11POWER
  12. #include "PowerComponentX11.h"
  13. #elif defined(Q_OS_WIN32)
  14. #include "PowerComponentWin.h"
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////////////////
  17. PowerComponent& PowerComponent::Get()
  18. {
  19. #ifdef Q_OS_MAC
  20. static PowerComponentMac instance;
  21. return instance;
  22. #elif KONVERGO_OPENELEC
  23. static PowerComponentOE instance;
  24. return instance;
  25. #elif USE_X11POWER
  26. static PowerComponentX11 instance;
  27. return instance;
  28. #elif defined(Q_OS_WIN32)
  29. static PowerComponentWin instance;
  30. return instance;
  31. #else
  32. QLOG_WARN() << "Could not find a power component matching this platform. OS screensaver control disabled.";
  33. static PowerComponent instance;
  34. return instance;
  35. #endif
  36. }
  37. /////////////////////////////////////////////////////////////////////////////////////////
  38. bool PowerComponent::componentInitialize()
  39. {
  40. PlayerComponent* player = &PlayerComponent::Get();
  41. if (!player)
  42. return false;
  43. connect(player, &PlayerComponent::playing, this, &PowerComponent::playbackStarted);
  44. connect(player, &PlayerComponent::playbackEnded, this, &PowerComponent::playbackEnded);
  45. return true;
  46. }
  47. /////////////////////////////////////////////////////////////////////////////////////////
  48. void PowerComponent::setFullscreenState(bool fullscreen)
  49. {
  50. m_fullscreenState = fullscreen;
  51. redecideScreeensaverState();
  52. }
  53. /////////////////////////////////////////////////////////////////////////////////////////
  54. void PowerComponent::redecideScreeensaverState()
  55. {
  56. bool enableOsScreensaver = !m_fullscreenState && !m_videoPlaying;
  57. if (m_currentScreensaverEnabled != enableOsScreensaver)
  58. {
  59. m_currentScreensaverEnabled = enableOsScreensaver;
  60. if (enableOsScreensaver)
  61. {
  62. QLOG_DEBUG() << "Enabling OS screensaver";
  63. doEnableScreensaver();
  64. emit screenSaverEnabled();
  65. }
  66. else
  67. {
  68. QLOG_DEBUG() << "Disabling OS screensaver";
  69. doDisableScreensaver();
  70. emit screenSaverDisabled();
  71. }
  72. }
  73. }
  74. /////////////////////////////////////////////////////////////////////////////////////////
  75. void PowerComponent::playbackStarted()
  76. {
  77. m_videoPlaying = true;
  78. redecideScreeensaverState();
  79. }
  80. /////////////////////////////////////////////////////////////////////////////////////////
  81. void PowerComponent::playbackEnded()
  82. {
  83. m_videoPlaying = false;
  84. redecideScreeensaverState();
  85. }
  86. /////////////////////////////////////////////////////////////////////////////////////////
  87. void PowerComponent::componentPostInitialize()
  88. {
  89. InputComponent::Get().registerHostCommand("poweroff", this, "PowerOff");
  90. InputComponent::Get().registerHostCommand("reboot", this, "Reboot");
  91. InputComponent::Get().registerHostCommand("suspend", this, "Suspend");
  92. }