KonvergoWindow.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include "KonvergoWindow.h"
  2. #include <QTimer>
  3. #include <QJsonObject>
  4. #include <QScreen>
  5. #include <QQuickItem>
  6. #include <QGuiApplication>
  7. #include <QMessageBox>
  8. #include <QPushButton>
  9. #include "core/Version.h"
  10. #include "system/UpdaterComponent.h"
  11. #include "input/InputKeyboard.h"
  12. #include "settings/SettingsComponent.h"
  13. #include "settings/SettingsSection.h"
  14. #include "system/SystemComponent.h"
  15. #include "player/PlayerComponent.h"
  16. #include "player/PlayerQuickItem.h"
  17. #include "display/DisplayComponent.h"
  18. #include "QsLog.h"
  19. #include "utils/Utils.h"
  20. #include "Globals.h"
  21. #include "EventFilter.h"
  22. ///////////////////////////////////////////////////////////////////////////////////////////////////
  23. class ScopedDecrementer
  24. {
  25. Q_DISABLE_COPY(ScopedDecrementer)
  26. int* m_value;
  27. public:
  28. ScopedDecrementer(int* value) : m_value(value) {}
  29. ~ScopedDecrementer() { (*m_value)--; }
  30. };
  31. ///////////////////////////////////////////////////////////////////////////////////////////////////
  32. KonvergoWindow::KonvergoWindow(QWindow* parent) :
  33. QQuickWindow(parent),
  34. m_debugLayer(false),
  35. m_ignoreFullscreenSettingsChange(0),
  36. m_showedUpdateDialog(false),
  37. m_osxPresentationOptions(0)
  38. {
  39. // NSWindowCollectionBehaviorFullScreenPrimary is only set on OSX if Qt::WindowFullscreenButtonHint is set on the window.
  40. setFlags(flags() | Qt::WindowFullscreenButtonHint);
  41. m_infoTimer = new QTimer(this);
  42. m_infoTimer->setInterval(1000);
  43. m_webDesktopMode = (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "webMode").toString() == "desktop");
  44. installEventFilter(new EventFilter(this));
  45. connect(m_infoTimer, &QTimer::timeout, this, &KonvergoWindow::updateDebugInfo);
  46. InputComponent::Get().registerHostCommand("close", this, "close");
  47. InputComponent::Get().registerHostCommand("toggleDebug", this, "toggleDebug");
  48. InputComponent::Get().registerHostCommand("reload", this, "reloadWeb");
  49. InputComponent::Get().registerHostCommand("fullscreen", this, "toggleFullscreen");
  50. InputComponent::Get().registerHostCommand("minimize", this, "minimizeWindow");
  51. InputComponent::Get().registerHostCommand("switchMode", this, "toggleWebMode");
  52. #ifdef TARGET_RPI
  53. // On RPI, we use dispmanx layering - the video is on a layer below Konvergo,
  54. // and during playback the Konvergo window is partially transparent. The OSD
  55. // will be visible on top of the video as part of the Konvergo window.
  56. setColor(QColor("transparent"));
  57. #else
  58. setColor(QColor("#000000"));
  59. #endif
  60. QRect loadedGeo = loadGeometry();
  61. connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_MAIN), &SettingsSection::valuesUpdated,
  62. this, &KonvergoWindow::updateMainSectionSettings);
  63. connect(this, &KonvergoWindow::visibilityChanged,
  64. this, &KonvergoWindow::onVisibilityChanged);
  65. connect(this, &KonvergoWindow::screenChanged,
  66. this, &KonvergoWindow::updateCurrentScreen, Qt::QueuedConnection);
  67. connect(this, &KonvergoWindow::xChanged,
  68. this, &KonvergoWindow::updateCurrentScreen, Qt::QueuedConnection);
  69. connect(this, &KonvergoWindow::yChanged,
  70. this, &KonvergoWindow::updateCurrentScreen, Qt::QueuedConnection);
  71. connect(this, &KonvergoWindow::visibilityChanged,
  72. this, &KonvergoWindow::updateCurrentScreen, Qt::QueuedConnection);
  73. connect(this, &KonvergoWindow::windowStateChanged,
  74. this, &KonvergoWindow::updateCurrentScreen, Qt::QueuedConnection);
  75. connect(this, &KonvergoWindow::enableVideoWindowSignal,
  76. this, &KonvergoWindow::enableVideoWindow, Qt::QueuedConnection);
  77. connect(&PlayerComponent::Get(), &PlayerComponent::windowVisible,
  78. this, &KonvergoWindow::playerWindowVisible, Qt::QueuedConnection);
  79. // this is using old syntax because ... reasons. QQuickCloseEvent is not public class
  80. connect(this, SIGNAL(closing(QQuickCloseEvent*)), this, SLOT(closingWindow()));
  81. connect(qApp, &QCoreApplication::aboutToQuit, this, &KonvergoWindow::closingWindow);
  82. connect(&UpdaterComponent::Get(), &UpdaterComponent::downloadComplete,
  83. this, &KonvergoWindow::showUpdateDialog);
  84. #ifdef Q_OS_MAC
  85. m_osxPresentationOptions = 0;
  86. #endif
  87. #ifdef KONVERGO_OPENELEC
  88. setVisibility(QWindow::FullScreen);
  89. #else
  90. updateWindowState(false);
  91. #endif
  92. updateScreens();
  93. connect(qApp, &QGuiApplication::screenAdded, this, &KonvergoWindow::onScreenAdded);
  94. connect(qApp, &QGuiApplication::screenRemoved, this, &KonvergoWindow::onScreenRemoved);
  95. emit enableVideoWindowSignal();
  96. }
  97. /////////////////////////////////////////////////////////////////////////////////////////
  98. void KonvergoWindow::showUpdateDialog()
  99. {
  100. if (m_webDesktopMode && !m_showedUpdateDialog)
  101. {
  102. QVariantHash updateInfo = UpdaterComponent::Get().updateInfo();
  103. QString currentVersion = Version::GetCanonicalVersionString().split("-")[0];
  104. QString newVersion = updateInfo["version"].toString().split("-")[0];
  105. QMessageBox* message = new QMessageBox(nullptr);
  106. message->setIcon(QMessageBox::Information);
  107. message->setWindowModality(Qt::ApplicationModal);
  108. message->setWindowTitle("Update found!");
  109. message->setText("An update to Plex Media Player was found");
  110. auto infoText = QString("You are currently running version %0\nDo you wish to install version %1 now?")
  111. .arg(currentVersion)
  112. .arg(newVersion);
  113. message->setInformativeText(infoText);
  114. auto details = QString("ChangeLog for version %0\n\nNew:\n%1\n\nFixed:\n%2")
  115. .arg(newVersion)
  116. .arg(updateInfo["new"].toString())
  117. .arg(updateInfo["fixed"].toString());
  118. message->setDetailedText(details);
  119. auto updateNow = message->addButton("Install Now", QMessageBox::AcceptRole);
  120. auto updateLater = message->addButton("Install on Next Restart", QMessageBox::RejectRole);
  121. message->setDefaultButton(updateNow);
  122. m_showedUpdateDialog = true;
  123. connect(message, &QMessageBox::buttonClicked, [=](QAbstractButton* button)
  124. {
  125. if (button == updateNow)
  126. UpdaterComponent::Get().doUpdate();
  127. else if (button == updateLater)
  128. message->close();
  129. message->deleteLater();
  130. });
  131. message->show();
  132. }
  133. }
  134. /////////////////////////////////////////////////////////////////////////////////////////
  135. void KonvergoWindow::closingWindow()
  136. {
  137. if (!SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool())
  138. saveGeometry();
  139. qApp->quit();
  140. }
  141. ///////////////////////////////////////////////////////////////////////////////////////////////////
  142. KonvergoWindow::~KonvergoWindow()
  143. {
  144. DisplayComponent::Get().setApplicationWindow(nullptr);
  145. }
  146. ///////////////////////////////////////////////////////////////////////////////////////////////////
  147. bool KonvergoWindow::fitsInScreens(const QRect& rc)
  148. {
  149. for(QScreen *screen : QGuiApplication::screens())
  150. {
  151. if (screen->virtualGeometry().isValid() && screen->virtualGeometry().contains(rc))
  152. return true;
  153. }
  154. return false;
  155. }
  156. ///////////////////////////////////////////////////////////////////////////////////////////////////
  157. void KonvergoWindow::saveGeometry()
  158. {
  159. QLOG_DEBUG() << "Window state when saving geometry:" << visibility();
  160. QRect rc = geometry();
  161. // lets make sure we are not saving something craycray
  162. if (rc.size().width() < windowMinSize().width() || rc.size().height() < windowMinSize().height())
  163. return;
  164. if (!fitsInScreens(rc))
  165. return;
  166. QLOG_DEBUG() << "Saving window geometry:" << rc;
  167. QVariantMap map = {{"x", rc.x()}, {"y", rc.y()},
  168. {"width", rc.width()}, {"height", rc.height()}};
  169. SettingsComponent::Get().setValue(SETTINGS_SECTION_STATE, "geometry", map);
  170. QScreen *curScreen = screen();
  171. SettingsComponent::Get().setValue(SETTINGS_SECTION_STATE, "lastUsedScreen", curScreen ? curScreen->name() : "");
  172. }
  173. ///////////////////////////////////////////////////////////////////////////////////////////////////
  174. QRect KonvergoWindow::loadGeometry()
  175. {
  176. QRect rc = loadGeometryRect();
  177. QScreen* myScreen = loadLastScreen();
  178. if (!myScreen)
  179. myScreen = screen();
  180. QRect nsize = rc;
  181. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool())
  182. {
  183. QLOG_DEBUG() << "Load FullScreen geo...";
  184. if (myScreen)
  185. {
  186. // On OSX we need to set the geometry to the size we want when we
  187. // return from fullscreen otherwise when we exit fullscreen it
  188. // will stay small or big. On Windows we need to set it to max
  189. // resolution for the screen (i.e. fullscreen) otherwise it will
  190. // just scale the webcontent to the minimum size we have defined
  191. //
  192. #ifndef Q_OS_MAC
  193. nsize = myScreen->geometry();
  194. #endif
  195. setGeometry(nsize);
  196. setScreen(myScreen);
  197. }
  198. }
  199. else
  200. {
  201. setGeometry(nsize);
  202. saveGeometry();
  203. }
  204. return nsize;
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////////////////////////
  207. QRect KonvergoWindow::loadGeometryRect()
  208. {
  209. // if we dont have anything, default to 720p in the middle of the screen
  210. QScreen *curScreen = screen();
  211. QRect defaultRect = QRect(0, 0, WEBUI_SIZE.width(), WEBUI_SIZE.height());
  212. if (curScreen)
  213. {
  214. defaultRect = QRect((curScreen->geometry().width() - WEBUI_SIZE.width()) / 2,
  215. (curScreen->geometry().height() - WEBUI_SIZE.height()) / 2,
  216. WEBUI_SIZE.width(), WEBUI_SIZE.height());
  217. }
  218. QVariantMap map = SettingsComponent::Get().value(SETTINGS_SECTION_STATE, "geometry").toMap();
  219. if (map.isEmpty())
  220. return defaultRect;
  221. QRect rc(map["x"].toInt(), map["y"].toInt(), map["width"].toInt(), map["height"].toInt());
  222. QLOG_DEBUG() << "Restoring geo:" << rc;
  223. if (!rc.isValid() || rc.isEmpty())
  224. {
  225. QLOG_DEBUG() << "Geo bad, going for defaults";
  226. return defaultRect;
  227. }
  228. QSize minsz = windowMinSize();
  229. // Clamp to min size if we have really small values in there
  230. if (rc.size().width() < minsz.width())
  231. rc.setWidth(minsz.width());
  232. if (rc.size().height() < minsz.height())
  233. rc.setHeight(minsz.height());
  234. // also make sure we are not putting windows outside the screen somewhere
  235. if (!fitsInScreens(rc))
  236. {
  237. QLOG_DEBUG() << "Could not fit stored geo into current screens";
  238. return defaultRect;
  239. }
  240. return rc;
  241. }
  242. ///////////////////////////////////////////////////////////////////////////////////////////////////
  243. void KonvergoWindow::enableVideoWindow()
  244. {
  245. PlayerComponent::Get().setWindow(this);
  246. DisplayComponent::Get().setApplicationWindow(this);
  247. }
  248. ///////////////////////////////////////////////////////////////////////////////////////////////////
  249. void KonvergoWindow::setFullScreen(bool enable)
  250. {
  251. QLOG_DEBUG() << "setting fullscreen = " << enable;
  252. SettingsComponent::Get().setValue(SETTINGS_SECTION_MAIN, "fullscreen", enable);
  253. }
  254. ///////////////////////////////////////////////////////////////////////////////////////////////////
  255. void KonvergoWindow::toggleWebMode()
  256. {
  257. if (!m_webDesktopMode)
  258. SettingsComponent::Get().setValue(SETTINGS_SECTION_MAIN, "webMode", "desktop");
  259. else
  260. SettingsComponent::Get().setValue(SETTINGS_SECTION_MAIN, "webMode", "tv");
  261. }
  262. ///////////////////////////////////////////////////////////////////////////////////////////////////
  263. void KonvergoWindow::setAlwaysOnTop(bool enable)
  264. {
  265. QLOG_DEBUG() << "setting always on top = " << enable;
  266. // Update the settings value.
  267. SettingsComponent::Get().setValue(SETTINGS_SECTION_MAIN, "alwaysOnTop", enable);
  268. }
  269. ///////////////////////////////////////////////////////////////////////////////////////////////////
  270. void KonvergoWindow::playerWindowVisible(bool visible)
  271. {
  272. // adjust webengineview transparecy depending on player visibility
  273. QQuickItem *web = findChild<QQuickItem *>("web");
  274. if (web)
  275. web->setProperty("backgroundColor", visible ? "transparent" : "#000000");
  276. #ifdef Q_OS_MAC
  277. // On OSX, initializing VideoTooolbox (hardware decoder API) will mysteriously
  278. // show the hidden mouse pointer again. The VTDecompressionSessionCreate API
  279. // function does this, and we have no influence over its behavior.
  280. if (visible && !SystemComponent::Get().cursorVisible())
  281. {
  282. // "Refresh" it. (There doesn't seem to be a nicer way, and we have to do
  283. // this on the Cocoa level too.)
  284. SystemComponent::Get().setCursorVisibility(true);
  285. SystemComponent::Get().setCursorVisibility(false);
  286. }
  287. #endif
  288. }
  289. ///////////////////////////////////////////////////////////////////////////////////////////////////
  290. void KonvergoWindow::updateMainSectionSettings(const QVariantMap& values)
  291. {
  292. // update mouse visibility if needed
  293. if (values.find("disablemouse") != values.end())
  294. {
  295. SystemComponent::Get().setCursorVisibility(!SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "disablemouse").toBool());
  296. }
  297. if (values.contains("alwaysOnTop"))
  298. updateWindowState();
  299. if (values.contains("fullscreen") && !m_ignoreFullscreenSettingsChange)
  300. updateWindowState();
  301. if (values.contains("webMode"))
  302. {
  303. InputComponent::Get().cancelAutoRepeat();
  304. bool oldDesktopMode = m_webDesktopMode;
  305. bool newDesktopMode = (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "webMode").toString() == "desktop");
  306. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "layout").toString() != "auto")
  307. {
  308. if (oldDesktopMode != newDesktopMode)
  309. {
  310. PlayerComponent::Get().stop();
  311. m_webDesktopMode = newDesktopMode;
  312. emit webDesktopModeChanged();
  313. emit webUrlChanged();
  314. }
  315. }
  316. else
  317. {
  318. if (oldDesktopMode != newDesktopMode)
  319. {
  320. QTimer::singleShot(0, [this, newDesktopMode]
  321. {
  322. PlayerComponent::Get().stop();
  323. m_webDesktopMode = newDesktopMode;
  324. emit webDesktopModeChanged();
  325. emit webUrlChanged();
  326. if (m_webDesktopMode)
  327. SystemComponent::Get().setCursorVisibility(true);
  328. });
  329. }
  330. }
  331. }
  332. if (values.contains("startupurl"))
  333. emit webUrlChanged();
  334. if (values.contains("forceFSScreen"))
  335. updateForcedScreen();
  336. }
  337. ///////////////////////////////////////////////////////////////////////////////////////////////////
  338. void KonvergoWindow::updateForcedScreen()
  339. {
  340. QString screenName = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "forceFSScreen").toString();
  341. if (screenName.isEmpty())
  342. return;
  343. for (QScreen* scr : QGuiApplication::screens())
  344. {
  345. if (scr->name() == screenName)
  346. {
  347. QLOG_DEBUG() << "Forcing screen to" << scr->name();
  348. setScreen(scr);
  349. setGeometry(scr->geometry());
  350. setVisibility(QWindow::FullScreen);
  351. InputComponent::Get().cancelAutoRepeat();
  352. return;
  353. }
  354. }
  355. }
  356. ///////////////////////////////////////////////////////////////////////////////////////////////////
  357. void KonvergoWindow::updateWindowState(bool saveGeo)
  358. {
  359. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool() || SystemComponent::Get().isOpenELEC() || SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "forceAlwaysFS").toBool())
  360. {
  361. // if we were go from windowed to fullscreen
  362. // we want to store our current windowed position
  363. if (!isFullScreen() && saveGeo)
  364. saveGeometry();
  365. setVisibility(QWindow::FullScreen);
  366. // When fullscreening explicitly, we might have to move the window to a
  367. // different screen, as Qt will fullscreen to the current screen.
  368. QTimer::singleShot(200, [=]
  369. {
  370. updateForcedScreen();
  371. });
  372. }
  373. else
  374. {
  375. setVisibility(QWindow::Windowed);
  376. loadGeometry();
  377. Qt::WindowFlags forceOnTopFlags = Qt::WindowStaysOnTopHint;
  378. #ifdef Q_WS_X11
  379. forceOnTopFlags = forceOnTopFlags | Qt::X11BypassWindowManagerHint;
  380. #endif
  381. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "alwaysOnTop").toBool())
  382. setFlags(flags() | forceOnTopFlags);
  383. else
  384. setFlags(flags() &~ forceOnTopFlags);
  385. }
  386. InputComponent::Get().cancelAutoRepeat();
  387. }
  388. ///////////////////////////////////////////////////////////////////////////////////////////////////
  389. QScreen* KonvergoWindow::findCurrentScreen()
  390. {
  391. // Return the screen that contains most of the window. Quite possible that
  392. // screen() would be sufficient, at least once the Qt bug returning a wrong
  393. // QScreen on Windows is fixed.
  394. QScreen *best = nullptr;
  395. qint64 bestArea = 0;
  396. for(QScreen* screen : qApp->screens())
  397. {
  398. QRect areaRC = screen->geometry().intersected(geometry());
  399. qint64 area = areaRC.width() * (qint64)areaRC.height();
  400. if (!best || area > bestArea)
  401. {
  402. best = screen;
  403. bestArea = area;
  404. }
  405. }
  406. return best ? best : screen();
  407. }
  408. ///////////////////////////////////////////////////////////////////////////////////////////////////
  409. void KonvergoWindow::onVisibilityChanged(QWindow::Visibility visibility)
  410. {
  411. QLOG_DEBUG() << "QWindow visibility set to" << visibility;
  412. #ifdef Q_OS_WIN32
  413. if (visibility == QWindow::Windowed)
  414. {
  415. QScreen* realScreen = findCurrentScreen();
  416. if (realScreen && realScreen->geometry() == geometry())
  417. {
  418. QLOG_WARN() << "winging it!";
  419. setScreen(realScreen);
  420. setVisibility(QWindow::FullScreen);
  421. return;
  422. }
  423. }
  424. #endif
  425. if (visibility == QWindow::Windowed && SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "forceAlwaysFS").toBool())
  426. {
  427. QLOG_WARN() << "Forcing re-entering fullscreen because of forceAlwaysFS setting!";
  428. updateForcedScreen(); // if a specific screen is forced, try to move the window there
  429. setVisibility(QWindow::FullScreen);
  430. return;
  431. }
  432. if (visibility == QWindow::FullScreen || visibility == QWindow::Windowed)
  433. {
  434. m_ignoreFullscreenSettingsChange++;
  435. ScopedDecrementer decrement(&m_ignoreFullscreenSettingsChange);
  436. bool fs = visibility == QWindow::FullScreen;
  437. SettingsComponent::Get().setValue(SETTINGS_SECTION_MAIN, "fullscreen", fs);
  438. SystemComponent::Get().setCursorVisibility(false);
  439. }
  440. InputComponent::Get().cancelAutoRepeat();
  441. }
  442. /////////////////////////////////////////////////////////////////////////////////////////
  443. void KonvergoWindow::focusOutEvent(QFocusEvent * ev)
  444. {
  445. #ifdef Q_OS_WIN32
  446. // Do this to workaround DWM compositor bugs with fullscreened OpenGL applications.
  447. // The compositor will not properly redraw anything when focusing other windows.
  448. if (visibility() == QWindow::FullScreen && SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "minimizeOnDefocus").toBool())
  449. {
  450. QLOG_DEBUG() << "minimizing window";
  451. showMinimized();
  452. }
  453. #endif
  454. QQuickWindow::focusOutEvent(ev);
  455. }
  456. /////////////////////////////////////////////////////////////////////////////////////////
  457. void KonvergoWindow::RegisterClass()
  458. {
  459. qmlRegisterType<KonvergoWindow>("Konvergo", 1, 0, "KonvergoWindow");
  460. }
  461. /////////////////////////////////////////////////////////////////////////////////////////
  462. void KonvergoWindow::updateDebugInfo()
  463. {
  464. if (m_systemDebugInfo.size() == 0)
  465. m_systemDebugInfo = SystemComponent::Get().debugInformation();
  466. m_debugInfo = m_systemDebugInfo;
  467. m_debugInfo += DisplayComponent::Get().debugInformation();
  468. PlayerQuickItem* video = findChild<PlayerQuickItem*>("video");
  469. if (video)
  470. m_debugInfo += video->debugInfo();
  471. QString infoString;
  472. QDebug info(&infoString);
  473. info << "Qt windowing info:\n";
  474. info << " FS: " << visibility() << "\n";
  475. info << " Geo: " << geometry() << "\n";
  476. for (QScreen* scr : QGuiApplication::screens())
  477. {
  478. info << " Screen" << scr->name() << scr->geometry() << "\n";
  479. }
  480. #ifdef Q_OS_WIN32
  481. HMONITOR mon = MonitorFromWindow((HWND)winId(), MONITOR_DEFAULTTONEAREST);
  482. MONITORINFO moninfo = {};
  483. moninfo.cbSize = sizeof(moninfo);
  484. RECT winrc;
  485. if (GetMonitorInfo(mon, &moninfo) &&GetWindowRect((HWND)winId(), &winrc))
  486. {
  487. RECT rc = moninfo.rcMonitor;
  488. info << " Win32 window" << QString("%1/%2 %3x%4").arg(rc.left).arg(rc.top).arg(rc.right).arg(rc.bottom) << QString("%1/%2 %3x%4").arg(winrc.left).arg(winrc.top).arg(winrc.right).arg(winrc.bottom) << "\n";
  489. }
  490. #endif
  491. info << "\n";
  492. m_debugInfo += infoString;
  493. m_videoInfo = PlayerComponent::Get().videoInformation();
  494. emit debugInfoChanged();
  495. }
  496. /////////////////////////////////////////////////////////////////////////////////////////
  497. void KonvergoWindow::toggleDebug()
  498. {
  499. if (property("showDebugLayer").toBool())
  500. {
  501. m_infoTimer->stop();
  502. setProperty("showDebugLayer", false);
  503. }
  504. else
  505. {
  506. m_infoTimer->start();
  507. updateDebugInfo();
  508. setProperty("showDebugLayer", true);
  509. }
  510. }
  511. /////////////////////////////////////////////////////////////////////////////////////////
  512. void KonvergoWindow::resizeEvent(QResizeEvent* event)
  513. {
  514. QLOG_DEBUG() << "resize event:" << event->size();
  515. // This next block was added at some point to workaround a problem with
  516. // resizing on windows. Unfortunately it broke the desktop client behavior
  517. // and when retried on Windows 10 with Qt5.7 the original bug seems to be
  518. // gone. I'll keep this code around until such a time that we dont get any
  519. // complaints about it.
  520. //
  521. #if 0
  522. // This next block should never really be needed in a prefect world...
  523. // Unfortunatly this is an imperfect world and on windows sometimes what
  524. // would happen on startup is that we got a resize event that would make
  525. // the window much smaller than fullscreen.
  526. //
  527. if (isFullScreen())
  528. {
  529. QSize fsSize = screen()->size();
  530. if (event->size().width() < fsSize.width() || event->size().height() < fsSize.height())
  531. {
  532. QLOG_DEBUG() << "Ignoring resize event when in fullscreen...";
  533. return;
  534. }
  535. }
  536. #endif
  537. QQuickWindow::resizeEvent(event);
  538. }
  539. /////////////////////////////////////////////////////////////////////////////////////////
  540. QScreen* KonvergoWindow::loadLastScreen()
  541. {
  542. QString screenName = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "forceFSScreen").toString();
  543. if (screenName.isEmpty())
  544. screenName = SettingsComponent::Get().value(SETTINGS_SECTION_STATE, "lastUsedScreen").toString();
  545. if (screenName.isEmpty())
  546. return nullptr;
  547. for (QScreen* scr : QGuiApplication::screens())
  548. {
  549. if (scr->name() == screenName)
  550. return scr;
  551. }
  552. QLOG_DEBUG() << "Tried to find screen:" << screenName << "but it was not present";
  553. return nullptr;
  554. }
  555. /////////////////////////////////////////////////////////////////////////////////////////
  556. QString KonvergoWindow::webUrl()
  557. {
  558. return SettingsComponent::Get().getWebClientUrl(m_webDesktopMode);
  559. }
  560. /////////////////////////////////////////////////////////////////////////////////////////
  561. void KonvergoWindow::updateScreens()
  562. {
  563. QScreen* windowScreen = findCurrentScreen();
  564. QString screenName = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "forceFSScreen").toString();
  565. QVariantList settingList;
  566. QVariantMap defentry;
  567. defentry["value"] = "";
  568. defentry["title"] = "Auto";
  569. settingList << defentry;
  570. bool currentPresent = false;
  571. int num = 0;
  572. for(QScreen* screen : qApp->screens())
  573. {
  574. QRect rc = screen->geometry();
  575. bool active = screen == windowScreen;
  576. QVariantMap entry;
  577. entry["value"] = screen->name();
  578. entry["title"] =
  579. QString("%1,%2 %3x%4").arg(rc.left()).arg(rc.top()).arg(rc.right()).arg(rc.bottom()) +
  580. " (" + screen->name() + ")" +
  581. (active ? " *" : "");
  582. settingList << entry;
  583. bool selected = screen->name() == screenName;
  584. if (selected)
  585. currentPresent = true;
  586. QLOG_DEBUG() << "Screen" << (num++) << screen << screen->geometry()
  587. << screen->virtualGeometry() << "active:" << active
  588. << "selected:" << selected;
  589. }
  590. if (!currentPresent && !screenName.isEmpty())
  591. {
  592. QVariantMap entry;
  593. entry["value"] = screenName;
  594. entry["title"] = "[Disconnected: " + screenName + "]";
  595. settingList << entry;
  596. }
  597. SettingsComponent::Get().updatePossibleValues(SETTINGS_SECTION_MAIN, "forceFSScreen", settingList);
  598. m_currentScreenName = windowScreen ? windowScreen->name() : "";
  599. }
  600. /////////////////////////////////////////////////////////////////////////////////////////
  601. void KonvergoWindow::onScreenAdded(QScreen *screen)
  602. {
  603. updateScreens();
  604. // The timer is out of fear for chaotic mid-change states.
  605. QTimer::singleShot(200, [this]
  606. {
  607. updateForcedScreen();
  608. });
  609. }
  610. /////////////////////////////////////////////////////////////////////////////////////////
  611. void KonvergoWindow::onScreenRemoved(QScreen *screen)
  612. {
  613. updateScreens();
  614. }
  615. /////////////////////////////////////////////////////////////////////////////////////////
  616. void KonvergoWindow::updateCurrentScreen()
  617. {
  618. QScreen* current = findCurrentScreen();
  619. QString currentName = current ? current->name() : "";
  620. if (currentName != m_currentScreenName)
  621. updateScreens();
  622. }