KonvergoWindow.cpp 23 KB

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