DisplayComponent.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "QsLog.h"
  2. #include "DisplayComponent.h"
  3. #include "DisplayManager.h"
  4. #include "settings/SettingsComponent.h"
  5. #include <QGuiApplication>
  6. #include <QWindow>
  7. #ifdef Q_OS_MAC
  8. #include "osx/DisplayManagerOSX.h"
  9. #elif defined(TARGET_RPI)
  10. #include "rpi/DisplayManagerRPI.h"
  11. #elif defined(USE_X11XRANDR)
  12. #include "x11/DisplayManagerX11.h"
  13. #elif defined(Q_OS_WIN)
  14. #include "win/DisplayManagerWin.h"
  15. #endif
  16. #include "dummy/DisplayManagerDummy.h"
  17. ///////////////////////////////////////////////////////////////////////////////////////////////////
  18. DisplayComponent::DisplayComponent(QObject* parent) : ComponentBase(parent), m_initTimer(this)
  19. {
  20. m_displayManager = NULL;
  21. m_lastVideoMode = -1;
  22. m_lastDisplay = -1;
  23. }
  24. ///////////////////////////////////////////////////////////////////////////////////////////////////
  25. DisplayComponent::~DisplayComponent()
  26. {
  27. }
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. bool DisplayComponent::initializeDisplayManager()
  30. {
  31. m_initTimer.setSingleShot(false);
  32. bool res = false;
  33. if (m_displayManager)
  34. res = m_displayManager->initialize();
  35. emit refreshRateChanged();
  36. return res;
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////////////////////////
  39. bool DisplayComponent::componentInitialize()
  40. {
  41. #if 0
  42. m_displayManager = new DisplayManagerDummy(this);
  43. #elif defined(Q_OS_MAC)
  44. m_displayManager = new DisplayManagerOSX(this);
  45. #elif defined(TARGET_RPI)
  46. m_displayManager = new DisplayManagerRPI(this);
  47. #elif defined(USE_X11XRANDR)
  48. m_displayManager = new DisplayManagerX11(this);
  49. #elif defined(Q_OS_WIN)
  50. m_displayManager = new DisplayManagerWin(this);
  51. #endif
  52. if (initializeDisplayManager())
  53. {
  54. QGuiApplication* app = (QGuiApplication*)QGuiApplication::instance();
  55. connect(app, SIGNAL(screenAdded(QScreen*)), this, SLOT(monitorChange()));
  56. connect(app, SIGNAL(screenRemoved(QScreen*)), this, SLOT(monitorChange()));
  57. foreach (QScreen *screen, app->screens())
  58. {
  59. connect(screen, SIGNAL(refreshRateChanged(qreal)), this, SLOT(monitorChange()));
  60. connect(screen, SIGNAL(geometryChanged(QRect)), this, SLOT(monitorChange()));
  61. }
  62. #ifdef TARGET_RPI
  63. // The firmware doesn't always make the best decision. Hope we do better.
  64. QLOG_INFO() << "Trying to switch to best display mode.";
  65. switchToBestOverallVideoMode(0);
  66. #endif
  67. return true;
  68. }
  69. return false;
  70. }
  71. //////////////////////////////////////////////////////////////////////////////////////////////////
  72. void DisplayComponent::monitorChange()
  73. {
  74. QLOG_INFO() << "Monitor change detected.";
  75. if (!m_initTimer.isSingleShot())
  76. {
  77. m_initTimer.setSingleShot(true);
  78. m_initTimer.singleShot(1000, this, SLOT(initializeDisplayManager()));
  79. }
  80. }
  81. //////////////////////////////////////////////////////////////////////////////////////////////////
  82. bool DisplayComponent::switchToBestVideoMode(float frameRate)
  83. {
  84. if (!m_displayManager)
  85. return false;
  86. int currentDisplay = getApplicationDisplay();
  87. if (currentDisplay < 0)
  88. {
  89. QLOG_INFO() << "Not switching rate - current display not found.";
  90. return false;
  91. }
  92. int currentMode = m_displayManager->getCurrentDisplayMode(currentDisplay);
  93. if (m_lastVideoMode < 0)
  94. {
  95. m_lastVideoMode = currentMode;
  96. m_lastDisplay = currentDisplay;
  97. }
  98. QLOG_DEBUG() << "Current display:" << currentDisplay << "mode:" << currentMode;
  99. DMMatchMediaInfo matchInfo(frameRate, false);
  100. int bestmode = m_displayManager->findBestMatch(currentDisplay, matchInfo);
  101. if (bestmode >= 0)
  102. {
  103. if (bestmode != currentMode)
  104. {
  105. QLOG_DEBUG()
  106. << "Best video matching mode is "
  107. << m_displayManager->displays[currentDisplay]->videoModes[bestmode]->getPrettyName()
  108. << "on display" << currentDisplay;
  109. m_displayManager->setDisplayMode(currentDisplay, bestmode);
  110. return true;
  111. }
  112. QLOG_INFO() << "No better video mode than the currently active one found.";
  113. }
  114. else
  115. {
  116. QLOG_DEBUG() << "No video mode found as better match.";
  117. }
  118. return false;
  119. }
  120. //////////////////////////////////////////////////////////////////////////////////////////////////
  121. bool DisplayComponent::switchToBestOverallVideoMode(int display)
  122. {
  123. if (!m_displayManager || !m_displayManager->isValidDisplay(display))
  124. return false;
  125. if (!SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "hdmi_poweron").toBool())
  126. {
  127. QLOG_INFO() << "Switching to best mode disabled.";
  128. return false;
  129. }
  130. int bestmode = m_displayManager->findBestMode(display);
  131. if (bestmode < 0)
  132. return false;
  133. QLOG_INFO() << "We think mode" << bestmode << "is the best mode.";
  134. if (bestmode == m_displayManager->getCurrentDisplayMode(display))
  135. {
  136. QLOG_INFO() << "This mode is the currently active one. Not switching.";
  137. return false;
  138. }
  139. if (!m_displayManager->setDisplayMode(display, bestmode))
  140. {
  141. QLOG_INFO() << "Switching mode failed.";
  142. return false;
  143. }
  144. QLOG_INFO() << "Switching mode successful.";
  145. return true;
  146. }
  147. //////////////////////////////////////////////////////////////////////////////////////////////////
  148. double DisplayComponent::currentRefreshRate()
  149. {
  150. if (!m_displayManager)
  151. return 0;
  152. int currentDisplay = getApplicationDisplay();
  153. if (currentDisplay < 0)
  154. return 0;
  155. int mode = m_displayManager->getCurrentDisplayMode(currentDisplay);
  156. if (mode < 0)
  157. return 0;
  158. return m_displayManager->displays[currentDisplay]->videoModes[mode]->refreshRate;
  159. }
  160. //////////////////////////////////////////////////////////////////////////////////////////////////
  161. bool DisplayComponent::restorePreviousVideoMode()
  162. {
  163. if (!m_displayManager)
  164. return false;
  165. if (!m_displayManager->isValidDisplayMode(m_lastDisplay, m_lastVideoMode))
  166. return false;
  167. bool ret = true;
  168. if (m_displayManager->getCurrentDisplayMode(m_lastDisplay) != m_lastVideoMode)
  169. {
  170. QLOG_DEBUG()
  171. << "Restoring VideoMode to"
  172. << m_displayManager->displays[m_lastDisplay]->videoModes[m_lastVideoMode]->getPrettyName()
  173. << "on display" << m_lastDisplay;
  174. ret = m_displayManager->setDisplayMode(m_lastDisplay, m_lastVideoMode);
  175. }
  176. m_lastVideoMode = -1;
  177. m_lastDisplay = -1;
  178. return ret;
  179. }
  180. //////////////////////////////////////////////////////////////////////////////////////////////////
  181. int DisplayComponent::getApplicationDisplay()
  182. {
  183. QWindow* activeWindow = QGuiApplication::focusWindow();
  184. int display = -1;
  185. if (activeWindow && m_displayManager)
  186. {
  187. QLOG_DEBUG() << "Looking for a display at:" << activeWindow->geometry()
  188. << "(center:" << activeWindow->geometry().center() << ")";
  189. display = m_displayManager->getDisplayFromPoint(activeWindow->geometry().center());
  190. }
  191. if (display < 0)
  192. {
  193. QLOG_WARN() << "Unable to locate current display.";
  194. }
  195. else
  196. {
  197. QLOG_DEBUG() << "Display found:" << display;
  198. }
  199. return display;
  200. }