KonvergoWindow.cpp 23 KB

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