/
mythdisplay.cpp
1103 lines (994 loc) · 38.3 KB
/
mythdisplay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Qt
#include <QTimer>
#include <QThread>
#include <QApplication>
#include <QElapsedTimer>
#include <QWindow>
// MythTV
#include "mythlogging.h"
#include "compat.h"
#include "mythcorecontext.h"
#include "mythuihelper.h"
#include "mythdisplay.h"
#include "mythegl.h"
#include "mythmainwindow.h"
#ifdef Q_OS_ANDROID
#include "mythdisplayandroid.h"
#endif
#if defined(Q_OS_MAC)
#include "mythdisplayosx.h"
#endif
#ifdef USING_X11
#include "mythdisplayx11.h"
#endif
#ifdef USING_DRM
#include "mythdisplaydrm.h"
#endif
#if defined(Q_OS_WIN)
#include "mythdisplaywindows.h"
#endif
#ifdef USING_MMAL
#include "mythdisplayrpi.h"
#endif
#define LOC QString("Display: ")
/*! \class MythDisplay
*
* MythDisplay is a reference counted, singleton class.
*
* Retrieve a reference to the MythDisplay object by calling AcquireRelease().
*
* A valid pointer is always returned.
*
* Release the reference by calling AcquireRelease(false).
*
* \note There is no locking in MythDisplay. It should never be used from anywhere
* other than the main/UI thread.
*
* \bug When using Xinerama/virtual screens, we get the correct screen at startup
* and we successfully move to another screen at the first attempt. Subsequently
* trying to move back to the original screen does not work. This appears to be
* an issue with MythMainWindow relying on MythUIHelper for screen coordinates
* that are not updated and/or subsequent window movements moving the window
* into the wrong screen. Much of the MythUIHelper code needs to move into MythDisplay.
*
* \todo Complete handling of screen changes. We need to handle several cases.
* Firstly, when the main window is moved (dragged) to a new screen. This generally
* works but need to check if all display details are updated (VideoOutWindow is
* currently hooked up to the CurrentScreenChanged signal - so video window sizing
* should work without issue). Various other parameters need updating though e.g.
* refresh rates, supported rates etc
* Secondly, when a new screen is added and the user has configured a multiscreen
* setup - in which case we might want to move the main window to the new screen.
* There are various complications here. Currently switching windows is triggered
* from the settings screens - which initiate a teardown of painter/render/UI etc.
* We cannot do that from random parts of the UI or during video playback.
*/
MythDisplay* MythDisplay::AcquireRelease(bool Acquire)
{
static QMutex s_lock(QMutex::Recursive);
static MythDisplay* s_display = nullptr;
QMutexLocker locker(&s_lock);
if (Acquire)
{
if (s_display)
{
s_display->IncrRef();
}
else
{
#ifdef USING_X11
if (MythDisplayX11::IsAvailable())
s_display = new MythDisplayX11();
#endif
#ifdef USING_MMAL
if (!s_display)
s_display = new MythDisplayRPI();
#endif
#ifdef USING_DRM
// this will only work by validating the screen's serial number
if (!s_display)
s_display = new MythDisplayDRM();
#endif
#if defined(Q_OS_MAC)
if (!s_display)
s_display = new MythDisplayOSX();
#endif
#ifdef Q_OS_ANDROID
if (!s_display)
s_display = new MythDisplayAndroid();
#endif
#if defined(Q_OS_WIN)
if (!s_display)
s_display = new MythDisplayWindows();
#endif
if (!s_display)
s_display = new MythDisplay();
}
}
else
{
if (s_display)
if (s_display->DecrRef() == 0)
s_display = nullptr;
}
return s_display;
}
QStringList MythDisplay::GetDescription(void)
{
QStringList result;
bool spanall = false;
int screencount = MythDisplay::GetScreenCount();
MythDisplay* display = MythDisplay::AcquireRelease();
if (MythDisplay::SpanAllScreens() && screencount > 1)
{
spanall = true;
result.append(tr("Spanning %1 screens").arg(screencount));
result.append(tr("Total bounds") + QString("\t: %1x%2")
.arg(display->GetScreenBounds().width())
.arg(display->GetScreenBounds().height()));
result.append("");
}
QScreen *current = display->GetCurrentScreen();
QList<QScreen*> screens = QGuiApplication::screens();
bool first = true;
for (auto *screen : qAsConst(screens))
{
if (!first)
result.append("");
first = false;
QString id = QString("(%1)").arg(screen->manufacturer());
if (screen == current && !spanall)
result.append(tr("Current screen %1 %2:").arg(screen->name()).arg(id));
else
result.append(tr("Screen %1 %2:").arg(screen->name()).arg(id));
result.append(tr("Size") + QString("\t\t: %1mmx%2mm")
.arg(screen->physicalSize().width()).arg(screen->physicalSize().height()));
if (screen == current)
{
QString source;
double aspect = display->GetAspectRatio(source);
result.append(tr("Aspect ratio") + QString("\t: %1 (%2)")
.arg(aspect, 0, 'f', 3).arg(source));
if (!spanall)
{
result.append(tr("Current mode") + QString("\t: %1x%2@%3Hz")
.arg(display->GetResolution().width()).arg(display->GetResolution().height())
.arg(display->GetRefreshRate(), 0, 'f', 2));
}
}
}
MythDisplay::AcquireRelease(false);
return result;
}
MythDisplay::MythDisplay()
: ReferenceCounter("Display")
{
m_screen = GetDesiredScreen();
DebugScreen(m_screen, "Using");
if (m_screen)
{
connect(m_screen, &QScreen::geometryChanged, this, &MythDisplay::GeometryChanged);
connect(m_screen, &QScreen::physicalDotsPerInchChanged, this, &MythDisplay::PhysicalDPIChanged);
}
auto *guiapp = qobject_cast<QGuiApplication *>(QCoreApplication::instance());
if (guiapp == nullptr)
return;
connect(guiapp, &QGuiApplication::screenRemoved, this, &MythDisplay::ScreenRemoved);
connect(guiapp, &QGuiApplication::screenAdded, this, &MythDisplay::ScreenAdded);
connect(guiapp, &QGuiApplication::primaryScreenChanged, this, &MythDisplay::PrimaryScreenChanged);
}
MythDisplay::~MythDisplay()
{
LOG(VB_GENERAL, LOG_INFO, LOC + "Deleting");
}
/*! \brief Set the QWidget and QWindow in use.
*
* Certain platform implementations need to know the QWidget and/or QWindow
* to access display information. We also connect to the QWindow::screenChanged
* signal so that we are informed when the window has been moved into a new screen.
*
* \note We typically call this twice; once before MythMainWindow is shown and
* once after. This is because we need to try and ensure we are using the correct
* screen before showing the window, and hence have the correct geometry etc, and
* once after because the QWindow is sometimes not created until after the widget
* is shown - and we cannot connect the screenChanged signal until we know the window
* handle.
*/
void MythDisplay::SetWidget(QWidget *MainWindow)
{
QWidget* oldwidget = m_widget;
m_widget = MainWindow;
if (!m_modeComplete)
UpdateCurrentMode();
QWindow* oldwindow = m_window;
if (m_widget)
m_window = m_widget->windowHandle();
else
m_window = nullptr;
if (m_widget && (m_widget != oldwidget))
LOG(VB_GENERAL, LOG_INFO, LOC + "Have main widget");
if (m_window && (m_window != oldwindow))
{
LOG(VB_GENERAL, LOG_INFO, LOC + "Have main window");
connect(m_window, &QWindow::screenChanged, this, &MythDisplay::ScreenChanged, Qt::UniqueConnection);
QScreen *desired = GetDesiredScreen();
// If we have changed the video mode for the old screen then reset
// it to the default/desktop mode
SwitchToDesktop();
// Ensure we completely re-initialise when the new screen is set
m_initialised = false;
if (desired != m_screen)
DebugScreen(desired, "Moving to");
m_window->setScreen(desired);
// WaitForNewScreen doesn't work as intended. It successfully filters
// out unwanted screenChanged signals after moving screens - but always
//times out. This just delays startup by 500ms - so ignore on startup as it isn't needed.
if (!m_firstScreenChange)
WaitForNewScreen();
m_firstScreenChange = false;
InitScreenBounds();
return;
}
}
int MythDisplay::GetScreenCount(void)
{
return QGuiApplication::screens().size();
}
double MythDisplay::GetPixelAspectRatio(void)
{
if (m_physicalSize.isEmpty() || m_resolution.isEmpty())
return 1.0;
return (m_physicalSize.width() / static_cast<double>(m_resolution.width())) /
(m_physicalSize.height() / static_cast<double>(m_resolution.height()));
}
QSize MythDisplay::GetGUIResolution(void)
{
return m_guiMode.Resolution();
}
QRect MythDisplay::GetScreenBounds(void)
{
return m_screenBounds;
}
/*! \brief Return a pointer to the screen to use.
*
* This function looks at the users screen preference, and will return
* that screen if possible. If not, i.e. the screen isn't plugged in,
* then this function returns the system's primary screen.
*
* Note: There is no special case here for the case of MythTV spanning
* all screens, as all screen have access to the virtual desktop
* attributes. The check for spanning screens must be made when the
* screen size/geometry accessed, and the proper physical/virtual
* size/geometry retrieved.
*/
QScreen* MythDisplay::GetCurrentScreen(void)
{
return m_screen;
}
QScreen *MythDisplay::GetDesiredScreen(void)
{
QScreen* newscreen = nullptr;
// If geometry is overriden at the command line level, try and determine
// which screen that applies to (if any).
// N.B. So many potential issues here e.g. should the geometry override be
// ignored after first use? (as it will continue to override the screen
// regardless of changes to screen preference).
if (MythUIHelper::IsGeometryOverridden())
{
// this matches the check in MythMainWindow
bool windowed = GetMythDB()->GetBoolSetting("RunFrontendInWindow", false) &&
!MythMainWindow::WindowIsAlwaysFullscreen();
QRect override = MythUIHelper::GetGeometryOverride();
// When windowed, we use topleft as a best guess as to which screen we belong in.
// When fullscreen, Qt appears to use the reverse - though this may be
// the window manager rather than Qt. So could be wrong.
QPoint point = windowed ? override.topLeft() : override.bottomRight();
for (QScreen *screen : QGuiApplication::screens())
{
if (screen->geometry().contains(point))
{
newscreen = screen;
LOG(VB_GENERAL, LOG_INFO, LOC + QString(
"Geometry override places window in screen '%1'").arg(newscreen->name()));
break;
}
}
}
// If spanning all screens, then always use the primary display
if (!newscreen && MythDisplay::SpanAllScreens())
{
LOG(VB_GENERAL, LOG_INFO, LOC + "Using primary screen for multiscreen");
newscreen = QGuiApplication::primaryScreen();
}
QString name = gCoreContext->GetSetting("XineramaScreen", nullptr);
// Lookup by name
if (!newscreen)
{
for (QScreen *screen : QGuiApplication::screens())
{
if (!name.isEmpty() && name == screen->name())
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Found screen '%1'").arg(name));
newscreen = screen;
}
}
}
// No name match. These were previously numbers.
if (!newscreen)
{
bool ok = false;
int screen_num = name.toInt(&ok);
QList<QScreen *>screens = QGuiApplication::screens();
if (ok && (screen_num >= 0) && (screen_num < screens.size()))
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Found screen number %1 (%2)")
.arg(name).arg(screens[screen_num]->name()));
newscreen = screens[screen_num];
}
}
// For anything else, return the primary screen.
if (!newscreen)
{
QScreen *primary = QGuiApplication::primaryScreen();
if (name.isEmpty() && primary)
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Defaulting to primary screen (%1)")
.arg(primary->name()));
}
else if (name != "-1" && primary)
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Screen '%1' not found, defaulting to primary screen (%2)")
.arg(name).arg(primary->name()));
}
newscreen = primary;
}
return newscreen;
}
/*! \brief The actual screen in use has changed. We must use it.
*/
void MythDisplay::ScreenChanged(QScreen *qScreen)
{
if (qScreen == m_screen)
return;
if (m_screen)
disconnect(m_screen, nullptr, this, nullptr);
DebugScreen(qScreen, "Changed to");
m_screen = qScreen;
connect(m_screen, &QScreen::geometryChanged, this, &MythDisplay::GeometryChanged);
connect(m_screen, &QScreen::physicalDotsPerInchChanged, this, &MythDisplay::PhysicalDPIChanged);
Initialise();
emit CurrentScreenChanged(qScreen);
}
void MythDisplay::PhysicalDPIChanged(qreal DPI)
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Qt screen pixel ratio changed to %1")
.arg(DPI, 2, 'f', 2, '0'));
emit CurrentDPIChanged(DPI);
}
void MythDisplay::PrimaryScreenChanged(QScreen* qScreen)
{
DebugScreen(qScreen, "New primary");
}
void MythDisplay::ScreenAdded(QScreen* qScreen)
{
DebugScreen(qScreen, "New");
emit ScreenCountChanged(QGuiApplication::screens().size());
}
void MythDisplay::ScreenRemoved(QScreen* qScreen)
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Screen '%1' removed").arg(qScreen->name()));
emit ScreenCountChanged(QGuiApplication::screens().size());
}
void MythDisplay::GeometryChanged(const QRect &Geo)
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("New screen geometry: %1x%2+%3+%4")
.arg(Geo.width()).arg(Geo.height()).arg(Geo.left()).arg(Geo.top()));
}
/*! \brief Retrieve screen details.
*
* This is the final fallback when no other platform specifics are available
* It is usually accurate apart from the refresh rate - which is often
* rounded down.
*/
void MythDisplay::UpdateCurrentMode(void)
{
// Certain platform implementations do not have a window to access at startup
// and hence use this implementation. Flag the status as incomplete to ensure
// we try to retrieve the full details at the first opportunity.
m_modeComplete = false;
m_edid = MythEDID();
QScreen *screen = GetCurrentScreen();
if (!screen)
{
m_refreshRate = 60.0;
m_physicalSize = QSize(0, 0);
m_resolution = QSize(1920, 1080);
return;
}
m_refreshRate = screen->refreshRate();
m_resolution = screen->size();
m_physicalSize = QSize(static_cast<int>(screen->physicalSize().width()),
static_cast<int>(screen->physicalSize().height()));
}
/// \brief Return true if the MythTV windows should span all screens.
bool MythDisplay::SpanAllScreens(void)
{
return gCoreContext->GetSetting("XineramaScreen", nullptr) == "-1";
}
QString MythDisplay::GetExtraScreenInfo(QScreen *qScreen)
{
QString mfg = qScreen->manufacturer();
if (mfg.isEmpty())
mfg = "Unknown";
QString model = qScreen->model();
if (model.isEmpty())
model = "Unknown";
return QString("(Make: %1 Model: %2)").arg(mfg).arg(model);
}
void MythDisplay::DebugScreen(QScreen *qScreen, const QString &Message)
{
if (!qScreen)
return;
QRect geom = qScreen->geometry();
QString extra = GetExtraScreenInfo(qScreen);
LOG(VB_GENERAL, LOG_INFO, LOC + QString("%1 screen '%2' %3")
.arg(Message).arg(qScreen->name()).arg(extra));
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Qt screen pixel ratio: %1")
.arg(qScreen->devicePixelRatio(), 2, 'f', 2, '0'));
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Geometry: %1x%2+%3+%4 Size(Qt): %5mmx%6mm")
.arg(geom.width()).arg(geom.height()).arg(geom.left()).arg(geom.top())
.arg(qScreen->physicalSize().width()).arg(qScreen->physicalSize().height()));
if (qScreen->virtualGeometry() != geom)
{
geom = qScreen->virtualGeometry();
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Total virtual geometry: %1x%2+%3+%4")
.arg(geom.width()).arg(geom.height()).arg(geom.left()).arg(geom.top()));
}
}
void MythDisplay::Initialise(void)
{
m_videoModes.clear();
m_overrideVideoModes.clear();
UpdateCurrentMode();
InitScreenBounds();
// Set the desktop mode - which is the mode at startup. We must always return
// the screen to this mode.
if (!m_initialised)
{
// Only ever set this once or after a screen change
m_initialised = true;
m_desktopMode = MythDisplayMode(m_resolution, m_physicalSize, -1.0, m_refreshRate);
LOG(VB_GENERAL, LOG_NOTICE, LOC + QString("Desktop video mode: %1x%2 %3Hz")
.arg(m_resolution.width()).arg(m_resolution.height()).arg(m_refreshRate, 0, 'f', 3));
if (m_edid.Valid())
{
if (m_edid.IsSRGB())
LOG(VB_GENERAL, LOG_NOTICE, LOC + "Display is using sRGB colourspace");
else
LOG(VB_GENERAL, LOG_NOTICE, LOC + "Display has custom colourspace");
}
}
// Set the gui mode from database settings
int pixelwidth = m_resolution.width();
int pixelheight = m_resolution.height();
int mmwidth = m_physicalSize.width();
int mmheight = m_physicalSize.height();
double refreshrate = m_refreshRate;
double aspectratio = 0.0;
GetMythDB()->GetResolutionSetting("GuiVidMode", pixelwidth, pixelheight, aspectratio, refreshrate);
GetMythDB()->GetResolutionSetting("DisplaySize", mmwidth, mmheight);
m_guiMode = MythDisplayMode(pixelwidth, pixelheight, mmwidth, mmheight, -1.0, refreshrate);
// Set default video mode
pixelwidth = pixelheight = 0;
GetMythDB()->GetResolutionSetting("TVVidMode", pixelwidth, pixelheight, aspectratio, refreshrate);
m_videoMode = MythDisplayMode(pixelwidth, pixelheight, mmwidth, mmheight, aspectratio, refreshrate);
// Initialise video override modes
for (int i = 0; true; ++i)
{
int iw = 0;
int ih = 0;
int ow = 0;
int oh = 0;
double iaspect = 0.0;
double oaspect = 0.0;
double irate = 0.0;
double orate = 0.0;
GetMythDB()->GetResolutionSetting("VidMode", iw, ih, iaspect, irate, i);
GetMythDB()->GetResolutionSetting("TVVidMode", ow, oh, oaspect, orate, i);
if (!(iw || ih || !qFuzzyIsNull(irate)) || !(ih && ow && oh))
break;
uint64_t key = MythDisplayMode::CalcKey(QSize(iw, ih), irate);
MythDisplayMode scr(QSize(ow, oh), QSize(mmwidth, mmheight), oaspect, orate);
m_overrideVideoModes[key] = scr;
}
}
/*! \brief Get screen size from Qt while respecting the user's multiscreen settings
*
* If the windowing system environment has multiple screens, then use
* QScreen::virtualSize() to get the size of the virtual desktop.
* Otherwise QScreen::size() or QScreen::availableSize() will provide
* the size of an individual screen.
*/
void MythDisplay::InitScreenBounds(void)
{
QList<QScreen*> screens = QGuiApplication::screens();
for (auto *screen : screens)
{
QRect dim = screen->geometry();
QString extra = MythDisplay::GetExtraScreenInfo(screen);
LOG(VB_GUI, LOG_INFO, LOC + QString("Screen %1: %2x%3 %4")
.arg(screen->name()).arg(dim.width()).arg(dim.height()).arg(extra));
}
QScreen *primary = QGuiApplication::primaryScreen();
LOG(VB_GUI, LOG_INFO, LOC +QString("Primary screen: %1.").arg(primary->name()));
int numScreens = MythDisplay::GetScreenCount();
QSize dim = primary->virtualSize();
LOG(VB_GUI, LOG_INFO, LOC + QString("Total desktop dim: %1x%2, over %3 screen[s].")
.arg(dim.width()).arg(dim.height()).arg(numScreens));
if (MythDisplay::SpanAllScreens())
{
LOG(VB_GUI, LOG_INFO, LOC + QString("Using entire desktop."));
m_screenBounds = primary->virtualGeometry();
return;
}
if (GetMythDB()->GetBoolSetting("RunFrontendInWindow", false))
{
LOG(VB_GUI, LOG_INFO, LOC + "Running in a window");
// This doesn't include the area occupied by the
// Windows taskbar, or the Mac OS X menu bar and Dock
m_screenBounds = m_screen->availableGeometry();
}
else
{
m_screenBounds = m_screen->geometry();
}
LOG(VB_GUI, LOG_INFO, LOC + QString("Using screen %1: %2x%3 at %4+%5")
.arg(m_screen->name()).arg(m_screenBounds.width()).arg(m_screenBounds.height())
.arg(m_screenBounds.left()).arg(m_screenBounds.top()));
}
/*! \brief Check whether the next mode is larger in size than the current mode.
*
* This is used to allow the caller to force an update of the main window to ensure
* the window is fully resized and is in no way clipped. Not an issue if the next
* mode is smaller.
*/
bool MythDisplay::NextModeIsLarger(QSize Size)
{
return Size.width() > m_resolution.width() || Size.height() > m_resolution.height();
}
/*! \brief Return the screen to the original desktop video mode
*
* \note It is assume the app is exiting once this is called.
*/
void MythDisplay::SwitchToDesktop()
{
MythDisplayMode current(m_resolution, m_physicalSize, -1.0, m_refreshRate);
if (current == m_desktopMode)
return;
SwitchToVideoMode(m_desktopMode.Resolution(), m_desktopMode.RefreshRate());
}
/** \brief Switches to the resolution and refresh rate defined in the
* database for the specified video resolution and frame rate.
*/
bool MythDisplay::SwitchToVideo(QSize Size, double Rate)
{
if (!m_modeComplete)
UpdateCurrentMode();
MythDisplayMode next = m_videoMode;
MythDisplayMode current(m_resolution, m_physicalSize, -1.0, m_refreshRate);
double targetrate = 0.0;
double aspectoverride = 0.0;
// try to find video override mode
uint64_t key = MythDisplayMode::FindBestScreen(m_overrideVideoModes,
Size.width(), Size.height(), Rate);
if (key != 0)
{
next = m_overrideVideoModes[key];
if (next.AspectRatio() > 0.0)
aspectoverride = next.AspectRatio();
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Found custom screen override %1x%2 Aspect %3")
.arg(next.Width()).arg(next.Height()).arg(aspectoverride));
}
// If requested refresh rate is 0, attempt to match video fps
if (qFuzzyIsNull(next.RefreshRate()))
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Trying to match best refresh rate %1Hz")
.arg(Rate, 0, 'f', 3));
next.SetRefreshRate(Rate);
}
// need to change video mode?
MythDisplayMode::FindBestMatch(GetVideoModes(), next, targetrate);
if ((next == current) && (MythDisplayMode::CompareRates(current.RefreshRate(), targetrate)))
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Using current mode %1x%2@%3Hz")
.arg(m_resolution.width()).arg(m_resolution.height()).arg(m_refreshRate));
return true;
}
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Trying mode %1x%2@%3Hz")
.arg(next.Width()).arg(next.Height()).arg(next.RefreshRate(), 0, 'f', 3));
if (!SwitchToVideoMode(next.Resolution(), targetrate))
{
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to change mode to %1x%2@%3Hz")
.arg(next.Width()).arg(next.Height()).arg(next.RefreshRate(), 0, 'f', 3));
return false;
}
if (next.Resolution() != m_resolution)
WaitForScreenChange();
// N.B. We used a computed aspect ratio unless overridden
m_aspectRatioOverride = aspectoverride > 0.0 ? aspectoverride : 0.0;
UpdateCurrentMode();
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Switched to %1x%2@%3Hz for video %4x%5")
.arg(m_resolution.width()).arg(m_resolution.height())
.arg(m_refreshRate, 0, 'f', 3).arg(Size.width()).arg(Size.height()));
PauseForModeSwitch();
return true;
}
/** \brief Switches to the GUI resolution.
*/
bool MythDisplay::SwitchToGUI(bool Wait)
{
if (!m_modeComplete)
UpdateCurrentMode();
// If the current resolution is the same as the GUI resolution then do nothing
// as refresh rate should not be critical for the GUI.
if (m_resolution == m_guiMode.Resolution())
{
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Using %1x%2@%3Hz for GUI")
.arg(m_resolution.width()).arg(m_resolution.height()).arg(m_refreshRate));
return true;
}
if (!SwitchToVideoMode(m_guiMode.Resolution(), m_guiMode.RefreshRate()))
{
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to change mode to %1x%2@%3Hz")
.arg(m_guiMode.Width()).arg(m_guiMode.Height()).arg(m_guiMode.RefreshRate(), 0, 'f', 3));
return false;
}
if (Wait && (m_resolution != m_guiMode.Resolution()))
WaitForScreenChange();
UpdateCurrentMode();
m_aspectRatioOverride = 0.0;
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Switched to %1x%2@%3Hz")
.arg(m_resolution.width()).arg(m_resolution.height()).arg(m_refreshRate, 0, 'f', 3));
return true;
}
double MythDisplay::GetRefreshRate(void) const
{
return m_refreshRate;
}
int MythDisplay::GetRefreshInterval(int Fallback) const
{
if (m_refreshRate > 20.0 && m_refreshRate < 200.0)
return static_cast<int>(lround(1000000.0 / m_refreshRate));
if (Fallback > 33000) // ~30Hz
Fallback /= 2;
return Fallback;
}
std::vector<double> MythDisplay::GetRefreshRates(QSize Size)
{
auto targetrate = static_cast<double>(NAN);
const MythDisplayMode mode(Size, QSize(0, 0), -1.0, 0.0);
const vector<MythDisplayMode>& modes = GetVideoModes();
int match = MythDisplayMode::FindBestMatch(modes, mode, targetrate);
if (match < 0)
return std::vector<double>();
return modes[static_cast<size_t>(match)].RefreshRates();
}
bool MythDisplay::SwitchToVideoMode(QSize /*Size*/, double /*Framerate*/)
{
return false;
}
const vector<MythDisplayMode> &MythDisplay::GetVideoModes(void)
{
return m_videoModes;
}
/** \brief Returns current screen aspect ratio.
*
* For the vast majority of cases we should be using the physical display size to
* compute the actual aspect ratio.
*
* This is currently overridden by:-
* - a custom, user specified aspect ratio for a given display mode. This feature
* is considered legacy as I don't believe it should be needed anymore,
* complicates both the code and the user 'experience' and offers very
* limited values for the override (4:3 and 16:9).
* - a general override for setups where the aspect ratio is not correctly detected.
* This generally means exotic multiscreen setups and displays with invalid and/or
* misleading EDIDs.
*
* If no override is specified and a valid physical aspect ratio cannot be
* calculated, then we fallback to the screen resolution (i.e. assume square pixels)
* and finally a guess at 16:9.
*/
double MythDisplay::GetAspectRatio(QString &Source, bool IgnoreModeOverride)
{
auto valid = [](double Aspect) { return (Aspect > 0.1 && Aspect < 10.0); };
// Override for this video mode
// Is this behaviour still needed?
if (!IgnoreModeOverride && valid(m_aspectRatioOverride))
{
Source = tr("Video mode override");
return m_aspectRatioOverride;
}
// General override for invalid/misleading EDIDs or multiscreen setups
// New default of -1.0 equates to square pixels for modern displays
bool multiscreen = MythDisplay::SpanAllScreens() && GetScreenCount() > 1;
double override = gCoreContext->GetFloatSettingOnHost("XineramaMonitorAspectRatio",
gCoreContext->GetHostName(), -1.0);
// Zero (not valid) indicates auto
if (valid(override))
{
Source = tr("Override");
return override;
}
// Auto for multiscreen is a best guess
if (multiscreen)
{
double aspect = EstimateVirtualAspectRatio();
if (valid(aspect))
{
Source = tr("Multiscreen estimate");
return aspect;
}
}
double calculated = m_resolution.isEmpty() ? 0.0 :
static_cast<double>(m_resolution.width()) / m_resolution.height();
double detected = m_physicalSize.isEmpty() ? 0.0 :
static_cast<double>(m_physicalSize.width()) / m_physicalSize.height();
// Assume pixel aspect ratio is 1 (square pixels)
if (valid(calculated))
{
if ((override < 0.0) || !valid(detected))
{
Source = tr("Square pixels");
return calculated;
}
}
// Based on actual physical size if available
if (valid(detected))
{
Source = tr("Detected");
return detected;
}
// the aspect ratio of last resort
Source = tr("Guessed");
return 16.0 / 9.0;
}
MythEDID& MythDisplay::GetEDID(void)
{
return m_edid;
}
/*! \brief Estimate the overall display aspect ratio for multi screen setups.
*
* \note This will only work where screens are configured either as a grid (e.g. 2x2)
* or as a single 'row' or 'column' of screens. Likewise it will fail if the aspect
* ratios of the displays are not similar and may not work if the display
* resolutions are significantly different (in a grid type setup).
*
* \note Untested with a grid layout - anyone have a card with 4 outputs?
*/
double MythDisplay::EstimateVirtualAspectRatio(void)
{
auto sortscreens = [](const QScreen* First, const QScreen* Second)
{
if (First->geometry().left() < Second->geometry().left())
return true;
if (First->geometry().top() < Second->geometry().top())
return true;
return false;
};
// default
double result = 16.0 / 9.0;
QList<QScreen*> screens;
if (m_screen)
screens = m_screen->virtualSiblings();
if (screens.empty())
return result;
// N.B. This sorting may not be needed
std::sort(screens.begin(), screens.end(), sortscreens);
QList<double> aspectratios;
QSize totalresolution;
int lasttop = 0;
int lastleft = 0;
int rows = 1;
int columns = 1;
for (auto it = screens.constBegin() ; it != screens.constEnd(); ++it)
{
QRect geom = (*it)->geometry();
totalresolution += geom.size();
LOG(VB_PLAYBACK, LOG_DEBUG, LOC + QString("%1x%2+%3+%4 %5")
.arg(geom.width()).arg(geom.height()).arg(geom.left()).arg(geom.top())
.arg((*it)->physicalSize().width() / (*it)->physicalSize().height()));
if (lastleft < geom.left())
{
columns++;
lastleft = geom.left();
}
if (lasttop < geom.top())
{
rows++;
lasttop = geom.top();
lastleft = 0;
}
aspectratios << (*it)->physicalSize().width() / (*it)->physicalSize().height();
}
// If all else fails, use the total resolution and assume pixel aspect ratio
// equals display aspect ratio
if (!totalresolution.isEmpty())
result = static_cast<double>(totalresolution.width()) / totalresolution.height();
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Screen layout: %1x%2").arg(rows).arg(columns));
if (rows == columns)
{
LOG(VB_GENERAL, LOG_DEBUG, LOC + "Grid layout");
}
else if (rows == 1 && columns > 1)
{
LOG(VB_GENERAL, LOG_DEBUG, LOC + "Horizontal layout");
}
else if (columns == 1 && rows > 1)
{
LOG(VB_GENERAL, LOG_DEBUG, LOC + "Vertical layout");
}
else
{
LOG(VB_GENERAL, LOG_INFO,
LOC + QString("Unsupported layout - defaulting to %1 (%2/%3)")
.arg(result).arg(totalresolution.width()).arg(totalresolution.height()));
return result;
}
// validate aspect ratios - with a little fuzzyness
double aspectratio = 0.0;
double average = 0.0;
int count = 1;
for (auto it2 = aspectratios.constBegin() ; it2 != aspectratios.constEnd(); ++it2, ++count)
{
aspectratio += *it2;
average = aspectratio / count;
if (qAbs(*it2 - average) > 0.1)
{
LOG(VB_GENERAL, LOG_INFO, LOC +
QString("Inconsistent aspect ratios - defaulting to %1 (%2/%3)")
.arg(result).arg(totalresolution.width()).arg(totalresolution.height()));
return result;
}
}
aspectratio = (average * columns) / rows;
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Estimated aspect ratio: %1")
.arg(aspectratio));
return aspectratio;
}
QSize MythDisplay::GetResolution(void)
{
return m_resolution;
}
QSize MythDisplay::GetPhysicalSize(void)
{
return m_physicalSize;
}
void MythDisplay::WaitForScreenChange(void)
{
// Some implementations may have their own mechanism for ensuring the mode
// is updated before continuing
if (!m_waitForModeChanges)
return;
LOG(VB_GENERAL, LOG_INFO, LOC + "Waiting for resolution change");
QEventLoop loop;
QTimer timer;
timer.setSingleShot(true);
connect(&timer, &QTimer::timeout, [](){ LOG(VB_GENERAL, LOG_WARNING, LOC + "Timed out wating for screen change"); });
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
QObject::connect(m_screen, &QScreen::geometryChanged, &loop, &QEventLoop::quit);
// 500ms maximum wait
timer.start(500);
loop.exec();
}
void MythDisplay::WaitForNewScreen(void)
{
// N.B. This isn't working as intended as it always times out rather than
// exiting deliberately. It does however somehow filter out unwanted screenChanged
// events that otherwise often put the widget in the wrong screen.
// Needs more investigation - but for now it works:)
if (!m_widget || (m_widget && !m_widget->windowHandle()))
return;
LOG(VB_GENERAL, LOG_INFO, LOC + "Waiting for new screen");
QEventLoop loop;
QTimer timer;
timer.setSingleShot(true);
connect(&timer, &QTimer::timeout, [](){ LOG(VB_GENERAL, LOG_WARNING, LOC + "Timed out waiting for new screen"); });