PowerComponent.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. connect(player, &PlayerComponent::playing, this, &PowerComponent::playbackStarted);
  42. connect(player, &PlayerComponent::playbackEnded, this, &PowerComponent::playbackEnded);
  43. return true;
  44. }
  45. /////////////////////////////////////////////////////////////////////////////////////////
  46. void PowerComponent::setFullscreenState(bool fullscreen)
  47. {
  48. m_fullscreenState = fullscreen;
  49. redecideScreeensaverState();
  50. }
  51. /////////////////////////////////////////////////////////////////////////////////////////
  52. void PowerComponent::redecideScreeensaverState()
  53. {
  54. bool enableOsScreensaver = !m_fullscreenState && !m_videoPlaying;
  55. if (m_currentScreensaverEnabled != enableOsScreensaver)
  56. {
  57. m_currentScreensaverEnabled = enableOsScreensaver;
  58. if (enableOsScreensaver)
  59. {
  60. QLOG_DEBUG() << "Enabling OS screensaver";
  61. doEnableScreensaver();
  62. emit screenSaverEnabled();
  63. }
  64. else
  65. {
  66. QLOG_DEBUG() << "Disabling OS screensaver";
  67. doDisableScreensaver();
  68. emit screenSaverDisabled();
  69. }
  70. }
  71. }
  72. /////////////////////////////////////////////////////////////////////////////////////////
  73. void PowerComponent::playbackStarted()
  74. {
  75. m_videoPlaying = true;
  76. redecideScreeensaverState();
  77. }
  78. /////////////////////////////////////////////////////////////////////////////////////////
  79. void PowerComponent::playbackEnded()
  80. {
  81. m_videoPlaying = false;
  82. redecideScreeensaverState();
  83. }
  84. /////////////////////////////////////////////////////////////////////////////////////////
  85. void PowerComponent::componentPostInitialize()
  86. {
  87. InputComponent::Get().registerHostCommand("poweroff", this, "PowerOff");
  88. InputComponent::Get().registerHostCommand("reboot", this, "Reboot");
  89. InputComponent::Get().registerHostCommand("suspend", this, "Suspend");
  90. }