KonvergoWindow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include "KonvergoWindow.h"
  2. #include <QTimer>
  3. #include <QJsonObject>
  4. #include <QScreen>
  5. #include <QQuickItem>
  6. #include <QGuiApplication>
  7. #include "input/InputKeyboard.h"
  8. #include "settings/SettingsComponent.h"
  9. #include "settings/SettingsSection.h"
  10. #include "system/SystemComponent.h"
  11. #include "player/PlayerComponent.h"
  12. #include "display/DisplayComponent.h"
  13. #include "QsLog.h"
  14. #include "power/PowerComponent.h"
  15. #include "utils/Utils.h"
  16. #include "KonvergoEngine.h"
  17. ///////////////////////////////////////////////////////////////////////////////////////////////////
  18. bool MouseEventFilter::eventFilter(QObject* watched, QEvent* event)
  19. {
  20. SystemComponent& system = SystemComponent::Get();
  21. // ignore mouse events if mouse is disabled
  22. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "disablemouse").toBool() &&
  23. ((event->type() == QEvent::MouseMove) ||
  24. (event->type() == QEvent::MouseButtonPress) ||
  25. (event->type() == QEvent::MouseButtonRelease) ||
  26. (event->type() == QEvent::MouseButtonDblClick)))
  27. {
  28. return true;
  29. }
  30. if (event->type() == QEvent::KeyPress)
  31. {
  32. // In konvergo we intercept all keyboard events and translate them
  33. // into web client actions. We need to do this so that we can remap
  34. // keyboard buttons to different events.
  35. //
  36. QKeyEvent* kevent = dynamic_cast<QKeyEvent*>(event);
  37. if (kevent)
  38. {
  39. system.setCursorVisibility(false);
  40. if (kevent->spontaneous())
  41. {
  42. InputKeyboard::Get().keyPress(QKeySequence(kevent->key() | kevent->modifiers()));
  43. return true;
  44. }
  45. }
  46. }
  47. else if (event->type() == QEvent::MouseMove)
  48. {
  49. system.setCursorVisibility(true);
  50. }
  51. else if (event->type() == QEvent::Wheel)
  52. {
  53. return true;
  54. }
  55. else if (event->type() == QEvent::MouseButtonPress)
  56. {
  57. // ignore right clicks that would show context menu
  58. QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
  59. if ((mouseEvent) && (mouseEvent->button() == Qt::RightButton))
  60. return true;
  61. }
  62. return QObject::eventFilter(watched, event);
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////////////////////////
  65. KonvergoWindow::KonvergoWindow(QWindow* parent) : QQuickWindow(parent), m_debugLayer(false)
  66. {
  67. // NSWindowCollectionBehaviorFullScreenPrimary is only set on OSX if Qt::WindowFullscreenButtonHint is set on the window.
  68. setFlags(flags() | Qt::WindowFullscreenButtonHint);
  69. m_eventFilter = new MouseEventFilter(this);
  70. installEventFilter(m_eventFilter);
  71. m_infoTimer = new QTimer(this);
  72. m_infoTimer->setInterval(1000);
  73. connect(m_infoTimer, &QTimer::timeout, this, &KonvergoWindow::updateDebugInfo);
  74. #ifdef TARGET_RPI
  75. // On RPI, we use dispmanx layering - the video is on a layer below Konvergo,
  76. // and during playback the Konvergo window is partially transparent. The OSD
  77. // will be visible on top of the video as part of the Konvergo window.
  78. setColor(QColor("transparent"));
  79. #else
  80. setColor(QColor("black"));
  81. #endif
  82. loadGeometry();
  83. connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_MAIN), &SettingsSection::valuesUpdated,
  84. this, &KonvergoWindow::updateMainSectionSettings);
  85. connect(this, &KonvergoWindow::visibilityChanged,
  86. this, &KonvergoWindow::onVisibilityChanged);
  87. connect(this, &KonvergoWindow::enableVideoWindowSignal,
  88. this, &KonvergoWindow::enableVideoWindow, Qt::QueuedConnection);
  89. // connect(QGuiApplication::desktop(), &QDesktopWidget::screenCountChanged,
  90. // this, &KonvergoWindow::onScreenCountChanged);
  91. connect(&PlayerComponent::Get(), &PlayerComponent::windowVisible,
  92. this, &KonvergoWindow::playerWindowVisible);
  93. connect(&PlayerComponent::Get(), &PlayerComponent::playbackStarting,
  94. this, &KonvergoWindow::playerPlaybackStarting);
  95. // this is using old syntax because ... reasons. QQuickCloseEvent is not public class
  96. connect(this, SIGNAL(closing(QQuickCloseEvent*)), this, SLOT(closingWindow()));
  97. // make sure that we handle some of the host commands that can be emitted
  98. connect(&InputComponent::Get(), &InputComponent::receivedHostCommand,
  99. this, &KonvergoWindow::handleHostCommand);
  100. connect(qApp, &QCoreApplication::aboutToQuit, this, &KonvergoWindow::saveGeometry);
  101. #ifdef Q_OS_MAC
  102. // this is such a hack. But I could not get it to enter into fullscreen
  103. // mode if I didn't trigger this after a while.
  104. //
  105. QTimer::singleShot(500, [=]() {
  106. updateFullscreenState();
  107. });
  108. #else
  109. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fakefullscreen").toBool())
  110. updateFullscreenState();
  111. else if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool() || SystemComponent::Get().isOpenELEC())
  112. setWindowState(Qt::WindowFullScreen);
  113. #endif
  114. emit enableVideoWindowSignal();
  115. }
  116. /////////////////////////////////////////////////////////////////////////////////////////
  117. void KonvergoWindow::closingWindow()
  118. {
  119. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool() == false)
  120. saveGeometry();
  121. qApp->quit();
  122. }
  123. ///////////////////////////////////////////////////////////////////////////////////////////////////
  124. KonvergoWindow::~KonvergoWindow()
  125. {
  126. removeEventFilter(m_eventFilter);
  127. }
  128. ///////////////////////////////////////////////////////////////////////////////////////////////////
  129. void KonvergoWindow::saveGeometry()
  130. {
  131. QRect rc = geometry();
  132. QJsonObject obj;
  133. obj.insert("x", rc.x());
  134. obj.insert("y", rc.y());
  135. obj.insert("width", rc.width());
  136. obj.insert("height", rc.height());
  137. SettingsComponent::Get().setValue(SETTINGS_SECTION_STATE, "geometry", obj.toVariantMap());
  138. }
  139. ///////////////////////////////////////////////////////////////////////////////////////////////////
  140. void KonvergoWindow::loadGeometry()
  141. {
  142. QRect rc = loadGeometryRect();
  143. if (rc.isValid())
  144. setGeometry(rc);
  145. }
  146. ///////////////////////////////////////////////////////////////////////////////////////////////////
  147. QRect KonvergoWindow::loadGeometryRect()
  148. {
  149. QJsonObject obj = QJsonObject::fromVariantMap(SettingsComponent::Get().value(SETTINGS_SECTION_STATE, "geometry").toMap());
  150. if (obj.isEmpty())
  151. return QRect();
  152. QRect rc(obj["x"].toInt(), obj["y"].toInt(), obj["width"].toInt(), obj["height"].toInt());
  153. if (rc.width() < 1280)
  154. rc.setWidth(1280);
  155. if (rc.height() < 720)
  156. rc.setHeight(720);
  157. // make sure our poisition is contained in one of the current screens
  158. foreach (QScreen *screen, QGuiApplication::screens())
  159. {
  160. if (screen->availableGeometry().contains(rc))
  161. return rc;
  162. }
  163. // otherwise default to center of current screen
  164. return QRect((screen()->geometry().width() - geometry().width()) / 2,
  165. (screen()->geometry().height() - geometry().height()) / 2,
  166. geometry().width(),
  167. geometry().height());
  168. }
  169. ///////////////////////////////////////////////////////////////////////////////////////////////////
  170. void KonvergoWindow::enableVideoWindow()
  171. {
  172. PlayerComponent::Get().setWindow(this);
  173. }
  174. ///////////////////////////////////////////////////////////////////////////////////////////////////
  175. void KonvergoWindow::setFullScreen(bool enable)
  176. {
  177. QLOG_DEBUG() << "setting fullscreen = " << enable;
  178. SettingsComponent::Get().setValue(SETTINGS_SECTION_MAIN, "fullscreen", enable);
  179. }
  180. ///////////////////////////////////////////////////////////////////////////////////////////////////
  181. void KonvergoWindow::playerWindowVisible(bool visible)
  182. {
  183. // adjust webengineview transparecy depending on player visibility
  184. QQuickItem *web = findChild<QQuickItem *>("web");
  185. if (web)
  186. web->setProperty("backgroundColor", visible ? "transparent" : "#111111");
  187. }
  188. ///////////////////////////////////////////////////////////////////////////////////////////////////
  189. void KonvergoWindow::updateMainSectionSettings(const QVariantMap& values)
  190. {
  191. // update mouse visibility if needed
  192. if (values.find("disablemouse") != values.end())
  193. {
  194. SystemComponent::Get().setCursorVisibility(!SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "disablemouse").toBool());
  195. }
  196. if (values.find("fullscreen") == values.end())
  197. return;
  198. updateFullscreenState();
  199. }
  200. ///////////////////////////////////////////////////////////////////////////////////////////////////
  201. void KonvergoWindow::updateFullscreenState()
  202. {
  203. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool() || SystemComponent::Get().isOpenELEC())
  204. {
  205. // if we were go from windowed to fullscreen
  206. // we want to stor our current windowed position
  207. if (!isFullScreen())
  208. saveGeometry();
  209. setVisibility(QWindow::FullScreen);
  210. }
  211. else
  212. {
  213. setVisibility(QWindow::Windowed);
  214. loadGeometry();
  215. }
  216. }
  217. ///////////////////////////////////////////////////////////////////////////////////////////////////
  218. void KonvergoWindow::onVisibilityChanged(QWindow::Visibility visibility)
  219. {
  220. QLOG_DEBUG() << (visibility == QWindow::FullScreen ? "FullScreen" : "Windowed") << "visbility set to " << visibility;
  221. if (visibility == QWindow::Windowed)
  222. loadGeometry();
  223. if (visibility == QWindow::FullScreen)
  224. PowerComponent::Get().setFullscreenState(true);
  225. else if (visibility == QWindow::Windowed)
  226. PowerComponent::Get().setFullscreenState(false);
  227. }
  228. /////////////////////////////////////////////////////////////////////////////////////////
  229. void KonvergoWindow::focusOutEvent(QFocusEvent * ev)
  230. {
  231. #ifdef Q_OS_WIN32
  232. // Do this to workaround DWM compositor bugs with fullscreened OpenGL applications.
  233. // The compositor will not properly redraw anything when focusing other windows.
  234. if (visibility() == QWindow::FullScreen)
  235. showMinimized();
  236. #endif
  237. }
  238. /////////////////////////////////////////////////////////////////////////////////////////
  239. void KonvergoWindow::playerPlaybackStarting()
  240. {
  241. #if defined(Q_OS_MAC)
  242. // On OSX, initializing VideoTooolbox (hardware decoder API) will mysteriously
  243. // show the hidden mouse pointer again. The VTDecompressionSessionCreate API
  244. // function does this, and we have no influence over its behavior. To make sure
  245. // the cursor is gone again when starting playback, listen to the player's
  246. // playbackStarting signal, at which point decoder initialization is guaranteed
  247. // to be completed. Then we just have to set the cursor again on the Cocoa level.
  248. if (QGuiApplication::overrideCursor())
  249. QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
  250. #endif
  251. }
  252. /////////////////////////////////////////////////////////////////////////////////////////
  253. void KonvergoWindow::RegisterClass()
  254. {
  255. qmlRegisterType<KonvergoWindow>("Konvergo", 1, 0, "KonvergoWindow");
  256. }
  257. /////////////////////////////////////////////////////////////////////////////////////////
  258. void KonvergoWindow::onScreenCountChanged(int newCount)
  259. {
  260. updateFullscreenState();
  261. }
  262. /////////////////////////////////////////////////////////////////////////////////////////
  263. void KonvergoWindow::updateDebugInfo()
  264. {
  265. if (m_debugInfo.size() == 0)
  266. m_debugInfo = SystemComponent::Get().debugInformation();
  267. m_videoInfo = PlayerComponent::Get().videoInformation();
  268. emit debugInfoChanged();
  269. }
  270. /////////////////////////////////////////////////////////////////////////////////////////
  271. void KonvergoWindow::handleHostCommand(QString hostCommand)
  272. {
  273. QLOG_DEBUG() << "Got command:" << hostCommand;
  274. QString arguments = "";
  275. int arguments_start = hostCommand.indexOf(":");
  276. if (arguments_start > 0)
  277. {
  278. arguments = hostCommand.mid(arguments_start + 1);
  279. hostCommand = hostCommand.mid(0, arguments_start);
  280. }
  281. if (hostCommand == "fullscreen")
  282. {
  283. SettingsComponent::Get().setValue(SETTINGS_SECTION_MAIN, "fullscreen", !SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool());
  284. }
  285. else if (hostCommand == "close")
  286. {
  287. close();
  288. }
  289. else if (hostCommand == "player")
  290. {
  291. PlayerComponent::Get().userCommand(arguments);
  292. }
  293. else if (hostCommand == "toggleDebug")
  294. {
  295. if (property("showDebugLayer").toBool())
  296. {
  297. m_infoTimer->stop();
  298. setProperty("showDebugLayer", false);
  299. }
  300. else
  301. {
  302. m_infoTimer->start();
  303. updateDebugInfo();
  304. setProperty("showDebugLayer", true);
  305. }
  306. }
  307. else if (hostCommand == "recreateRpiUi")
  308. {
  309. DisplayManager* display_manager = DisplayComponent::Get().getDisplayManager();
  310. if (display_manager)
  311. display_manager->resetRendering();
  312. }
  313. else if (hostCommand == "reload")
  314. {
  315. emit reloadWebClient();
  316. }
  317. else if (hostCommand == "crash!")
  318. {
  319. *(volatile int*)0=0;
  320. }
  321. else
  322. {
  323. QLOG_WARN() << "unknown host command" << hostCommand;
  324. }
  325. }