DisplayComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include "DisplayComponent.h"
  2. #include "DisplayManager.h"
  3. #include "settings/SettingsComponent.h"
  4. #include <QGuiApplication>
  5. #include <QWindow>
  6. #include <QDebug>
  7. #include <math.h>
  8. #ifdef Q_OS_MAC
  9. #include "osx/DisplayManagerOSX.h"
  10. #elif defined(TARGET_RPI)
  11. #include "rpi/DisplayManagerRPI.h"
  12. #elif defined(USE_X11XRANDR)
  13. #include "x11/DisplayManagerX11.h"
  14. #elif defined(Q_OS_WIN)
  15. #include "win/DisplayManagerWin.h"
  16. #endif
  17. #include "dummy/DisplayManagerDummy.h"
  18. #include "input/InputComponent.h"
  19. ///////////////////////////////////////////////////////////////////////////////////////////////////
  20. DisplayComponent::DisplayComponent(QObject* parent) : ComponentBase(parent), m_initTimer(this)
  21. {
  22. m_displayManager = nullptr;
  23. m_lastVideoMode = -1;
  24. m_lastDisplay = -1;
  25. m_applicationWindow = nullptr;
  26. }
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. DisplayComponent::~DisplayComponent()
  29. {
  30. }
  31. ///////////////////////////////////////////////////////////////////////////////////////////////////
  32. bool DisplayComponent::initializeDisplayManager()
  33. {
  34. m_initTimer.setSingleShot(false);
  35. bool res = false;
  36. if (m_displayManager)
  37. res = m_displayManager->initialize();
  38. emit refreshRateChanged();
  39. return res;
  40. }
  41. ///////////////////////////////////////////////////////////////////////////////////////////////////
  42. bool DisplayComponent::componentInitialize()
  43. {
  44. #if 0
  45. m_displayManager = new DisplayManagerDummy(this);
  46. #elif defined(Q_OS_MAC)
  47. m_displayManager = new DisplayManagerOSX(this);
  48. #elif defined(TARGET_RPI)
  49. m_displayManager = new DisplayManagerRPI(this);
  50. #elif defined(USE_X11XRANDR)
  51. m_displayManager = new DisplayManagerX11(this);
  52. #elif defined(Q_OS_WIN)
  53. m_displayManager = new DisplayManagerWin(this);
  54. #endif
  55. if (initializeDisplayManager())
  56. {
  57. QGuiApplication* app = (QGuiApplication*)QGuiApplication::instance();
  58. connect(app, SIGNAL(screenAdded(QScreen*)), this, SLOT(monitorChange()));
  59. connect(app, SIGNAL(screenRemoved(QScreen*)), this, SLOT(monitorChange()));
  60. for(QScreen *screen : app->screens())
  61. {
  62. connect(screen, SIGNAL(refreshRateChanged(qreal)), this, SLOT(monitorChange()));
  63. connect(screen, SIGNAL(geometryChanged(QRect)), this, SLOT(monitorChange()));
  64. }
  65. #ifdef TARGET_RPI
  66. // The firmware doesn't always make the best decision. Hope we do better.
  67. qInfo() << "Trying to switch to best display mode.";
  68. switchToBestOverallVideoMode(0);
  69. #endif
  70. return true;
  71. }
  72. return false;
  73. }
  74. //////////////////////////////////////////////////////////////////////////////////////////////////
  75. void DisplayComponent::monitorChange()
  76. {
  77. qInfo() << "Monitor change detected.";
  78. if (!m_initTimer.isSingleShot())
  79. {
  80. m_initTimer.setSingleShot(true);
  81. m_initTimer.singleShot(1000, this, SLOT(initializeDisplayManager()));
  82. }
  83. }
  84. //////////////////////////////////////////////////////////////////////////////////////////////////
  85. bool DisplayComponent::switchToBestVideoMode(float frameRate)
  86. {
  87. initializeDisplayManager();
  88. if (!m_displayManager)
  89. return false;
  90. int currentDisplay = getApplicationDisplay();
  91. if (currentDisplay < 0)
  92. {
  93. qInfo() << "Not switching rate - current display not found.";
  94. return false;
  95. }
  96. int currentMode = m_displayManager->getCurrentDisplayMode(currentDisplay);
  97. if (m_lastVideoMode < 0)
  98. {
  99. m_lastVideoMode = currentMode;
  100. m_lastDisplay = currentDisplay;
  101. }
  102. qDebug() << "Current display:" << currentDisplay << "mode:" << currentMode;
  103. DMMatchMediaInfo matchInfo(frameRate, false);
  104. int bestmode = m_displayManager->findBestMatch(currentDisplay, matchInfo);
  105. if (bestmode >= 0)
  106. {
  107. if (bestmode != currentMode)
  108. {
  109. qDebug()
  110. << "Best video matching mode is "
  111. << m_displayManager->m_displays[currentDisplay]->m_videoModes[bestmode]->getPrettyName()
  112. << "on display" << currentDisplay;
  113. if (!m_displayManager->setDisplayMode(currentDisplay, bestmode))
  114. {
  115. qInfo() << "Mode switching failed.";
  116. return false;
  117. }
  118. return true;
  119. }
  120. qInfo() << "No better video mode than the currently active one found.";
  121. }
  122. else
  123. {
  124. qDebug() << "No video mode found as better match.";
  125. }
  126. return false;
  127. }
  128. //////////////////////////////////////////////////////////////////////////////////////////////////
  129. bool DisplayComponent::switchToBestOverallVideoMode(int display)
  130. {
  131. initializeDisplayManager();
  132. if (!m_displayManager || !m_displayManager->isValidDisplay(display))
  133. return false;
  134. if (!SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "hdmi_poweron").toBool())
  135. {
  136. qInfo() << "Switching to best mode disabled.";
  137. return false;
  138. }
  139. int bestmode = m_displayManager->findBestMode(display);
  140. if (bestmode < 0)
  141. return false;
  142. qInfo() << "We think mode" << bestmode << "is the best mode.";
  143. if (bestmode == m_displayManager->getCurrentDisplayMode(display))
  144. {
  145. qInfo() << "This mode is the currently active one. Not switching.";
  146. return false;
  147. }
  148. if (!m_displayManager->setDisplayMode(display, bestmode))
  149. {
  150. qInfo() << "Switching mode failed.";
  151. return false;
  152. }
  153. qInfo() << "Switching mode successful.";
  154. return true;
  155. }
  156. //////////////////////////////////////////////////////////////////////////////////////////////////
  157. double DisplayComponent::currentRefreshRate()
  158. {
  159. if (!m_displayManager)
  160. return 0;
  161. int currentDisplay = getApplicationDisplay();
  162. if (currentDisplay < 0)
  163. return 0;
  164. int mode = m_displayManager->getCurrentDisplayMode(currentDisplay);
  165. if (mode < 0)
  166. return 0;
  167. return m_displayManager->m_displays[currentDisplay]->m_videoModes[mode]->m_refreshRate;
  168. }
  169. //////////////////////////////////////////////////////////////////////////////////////////////////
  170. bool DisplayComponent::restorePreviousVideoMode()
  171. {
  172. initializeDisplayManager();
  173. if (!m_displayManager)
  174. return false;
  175. if (!m_displayManager->isValidDisplayMode(m_lastDisplay, m_lastVideoMode))
  176. return false;
  177. bool ret = true;
  178. if (m_displayManager->getCurrentDisplayMode(m_lastDisplay) != m_lastVideoMode)
  179. {
  180. qDebug()
  181. << "Restoring VideoMode to"
  182. << m_displayManager->m_displays[m_lastDisplay]->m_videoModes[m_lastVideoMode]->getPrettyName()
  183. << "on display" << m_lastDisplay;
  184. ret = m_displayManager->setDisplayMode(m_lastDisplay, m_lastVideoMode);
  185. }
  186. m_lastVideoMode = -1;
  187. m_lastDisplay = -1;
  188. return ret;
  189. }
  190. //////////////////////////////////////////////////////////////////////////////////////////////////
  191. int DisplayComponent::getApplicationDisplay(bool silent)
  192. {
  193. QWindow* activeWindow = m_applicationWindow;
  194. int display = -1;
  195. if (activeWindow && m_displayManager)
  196. {
  197. if (!silent)
  198. {
  199. qInfo() << "Looking for a display at:" << activeWindow->geometry()
  200. << "(center:" << activeWindow->geometry().center() << ")";
  201. }
  202. display = m_displayManager->getDisplayFromPoint(activeWindow->geometry().center());
  203. }
  204. if (!silent)
  205. {
  206. qInfo() << "Display index:" << display;
  207. }
  208. return display;
  209. }
  210. /////////////////////////////////////////////////////////////////////////////////////////
  211. QString DisplayComponent::displayName(int display)
  212. {
  213. if (display < 0)
  214. return "(not found)";
  215. QString id = QString("#%0 ").arg(display);
  216. if (m_displayManager->isValidDisplay(display))
  217. return id + m_displayManager->m_displays[display]->m_name;
  218. else
  219. return id + "(not valid)";
  220. }
  221. /////////////////////////////////////////////////////////////////////////////////////////
  222. QString DisplayComponent::modePretty(int display, int mode)
  223. {
  224. if (mode < 0)
  225. return "(not found)";
  226. QString id = QString("#%0 ").arg(mode);
  227. if (m_displayManager->isValidDisplayMode(display, mode))
  228. return id + m_displayManager->m_displays[display]->m_videoModes[mode]->getPrettyName();
  229. else
  230. return id + "(not valid)";
  231. }
  232. /////////////////////////////////////////////////////////////////////////////////////////
  233. QString DisplayComponent::debugInformation()
  234. {
  235. QString debugInfo;
  236. QTextStream stream(&debugInfo);
  237. stream << "Display\n";
  238. if (!m_displayManager)
  239. {
  240. stream << " (no DisplayManager initialized)\n";
  241. }
  242. else
  243. {
  244. int display = getApplicationDisplay(true);
  245. int mode = display < 0 ? -1 : m_displayManager->getCurrentDisplayMode(display);
  246. stream << " Current screen: " << displayName(display) << "\n";
  247. if (display >= 0)
  248. stream << " Current mode: " << modePretty(display, mode) << "\n";
  249. if (m_displayManager->isValidDisplayMode(m_lastDisplay, m_lastVideoMode))
  250. {
  251. stream << " Switch back on screen: " << displayName(m_lastDisplay) << "\n";
  252. stream << " Switch back to mode: " << modePretty(m_lastDisplay, m_lastVideoMode) << "\n";
  253. }
  254. }
  255. stream << "\n";
  256. stream.flush();
  257. return debugInfo;
  258. }
  259. ///////////////////////////////////////////////////////////////////////////////////////////////////
  260. static float modeDistance(const DMVideoMode& m1, const DMVideoMode& m2)
  261. {
  262. if (m1.m_height == m2.m_height &&
  263. m1.m_width == m2.m_width &&
  264. m1.m_bitsPerPixel == m2.m_bitsPerPixel &&
  265. m1.m_interlaced == m2.m_interlaced)
  266. return fabs(m1.m_refreshRate - m2.m_refreshRate);
  267. return -1;
  268. }
  269. /////////////////////////////////////////////////////////////////////////////////////////
  270. void DisplayComponent::switchCommand(QString command)
  271. {
  272. if (!m_displayManager)
  273. {
  274. qCritical() << "Display manager not set";
  275. return;
  276. }
  277. if (!initializeDisplayManager())
  278. {
  279. qCritical() << "Could not reinitialize display manager";
  280. return;
  281. }
  282. int currentDisplay = getApplicationDisplay();
  283. if (currentDisplay < 0)
  284. {
  285. qCritical() << "Current display not found";
  286. return;
  287. }
  288. int id = m_displayManager->getCurrentDisplayMode(currentDisplay);
  289. if (id < 0)
  290. {
  291. qCritical() << "Current mode not found";
  292. return;
  293. }
  294. DMVideoMode currentMode = *m_displayManager->m_displays[currentDisplay]->m_videoModes[id];
  295. DMVideoMode mode = currentMode;
  296. int bestMode = -1; // if -1, select it by using the mode variable above
  297. for(QString a : command.split(" "))
  298. {
  299. a = a.trimmed();
  300. if (a == "p")
  301. {
  302. mode.m_interlaced = false;
  303. }
  304. else if (a == "i")
  305. {
  306. mode.m_interlaced = true;
  307. }
  308. else if (a.endsWith("hz"))
  309. {
  310. a = a.mid(0, a.size() - 2);
  311. bool ok;
  312. float rate = a.toFloat(&ok);
  313. if (ok)
  314. mode.m_refreshRate = rate;
  315. }
  316. else if (a.startsWith("mode="))
  317. {
  318. a = a.mid(5);
  319. bool ok;
  320. int newId = a.toInt(&ok);
  321. if (ok && m_displayManager->isValidDisplayMode(currentDisplay, newId))
  322. bestMode = newId;
  323. }
  324. else if (a.indexOf("x") >= 0)
  325. {
  326. QStringList sub = a.split("x");
  327. if (sub.size() != 2)
  328. continue;
  329. bool ok;
  330. int w = sub[0].toInt(&ok);
  331. if (!ok)
  332. continue;
  333. int h = sub[1].toInt(&ok);
  334. if (!ok)
  335. continue;
  336. mode.m_width = w;
  337. mode.m_height = h;
  338. }
  339. }
  340. qInfo() << "Current mode:" << currentMode.getPrettyName();
  341. if (bestMode < 0)
  342. {
  343. qInfo() << "Mode requested by command:" << mode.getPrettyName();
  344. for(auto cur : m_displayManager->m_displays[currentDisplay]->m_videoModes)
  345. {
  346. // "Score" according to what was requested
  347. float dCur = modeDistance(*cur, mode);
  348. if (dCur < 0)
  349. continue;
  350. if (bestMode < 0)
  351. {
  352. bestMode = cur->m_id;
  353. }
  354. else
  355. {
  356. // "Score" according to best mode
  357. float dBest = modeDistance(*m_displayManager->m_displays[currentDisplay]->m_videoModes[bestMode],
  358. mode);
  359. if (dCur < dBest)
  360. bestMode = cur->m_id;
  361. }
  362. }
  363. }
  364. if (bestMode >= 0)
  365. {
  366. qInfo() << "Found mode to switch to:" << m_displayManager->m_displays[currentDisplay]->m_videoModes[bestMode]->getPrettyName();
  367. if (m_displayManager->setDisplayMode(currentDisplay, bestMode))
  368. {
  369. m_lastDisplay = m_lastVideoMode = -1;
  370. }
  371. else
  372. {
  373. qInfo() << "Switching failed.";
  374. }
  375. return;
  376. }
  377. qInfo() << "Requested mode not found.";
  378. }
  379. /////////////////////////////////////////////////////////////////////////////////////////
  380. void DisplayComponent::componentPostInitialize()
  381. {
  382. InputComponent::Get().registerHostCommand("switch", this, "switchCommand");
  383. if (m_displayManager)
  384. InputComponent::Get().registerHostCommand("recreateRpiUI", m_displayManager, "resetRendering");
  385. }