-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathHostSettingsPage.xaml.cpp
More file actions
291 lines (256 loc) · 10.8 KB
/
Copy pathHostSettingsPage.xaml.cpp
File metadata and controls
291 lines (256 loc) · 10.8 KB
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
//
// HostSettingsPage.xaml.cpp
// Implementation of the HostSettingsPage class
//
#include "pch.h"
#include "HostSettingsPage.xaml.h"
#include "MoonlightSettings.xaml.h"
#include "Utils.hpp"
#include <gamingdeviceinformation.h>
#include <cmath> // sqrtf, lround
using namespace Windows::UI::Core;
using namespace moonlight_xbox_dx;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Graphics::Display::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::ViewManagement::Core;
HostSettingsPage::HostSettingsPage()
{
InitializeComponent();
Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()->SetDesiredBoundsMode(Windows::UI::ViewManagement::ApplicationViewBoundsMode::UseVisible);
this->Loaded += ref new Windows::UI::Xaml::RoutedEventHandler(this, &HostSettingsPage::OnLoaded);
this->Unloaded += ref new Windows::UI::Xaml::RoutedEventHandler(this, &HostSettingsPage::OnUnloaded);
}
void HostSettingsPage::OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) {
MoonlightHost^ mhost = dynamic_cast<MoonlightHost^>(e->Parameter);
if (mhost == nullptr)return;
GAMING_DEVICE_MODEL_INFORMATION info = {};
GetGamingDeviceModelInformation(&info);
host = mhost;
AvailableResolutions->Append(ref new ScreenResolution(1280, 720));
AvailableResolutions->Append(ref new ScreenResolution(1920, 1080));
//No 4K for Old Xbox One
if (!(info.vendorId == GAMING_DEVICE_VENDOR_ID_MICROSOFT && info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_ONE)) {
AvailableResolutions->Append(ref new ScreenResolution(2560, 1440));
AvailableResolutions->Append(ref new ScreenResolution(3840, 2160));
}
AvailableFPS->Append(30);
AvailableFPS->Append(60);
AvailableFPS->Append(120);
AvailableVideoCodecs->Append("H.264");
AvailableVideoCodecs->Append("HEVC (H.265)");
AvailableAudioConfigs->Append("Stereo");
AvailableAudioConfigs->Append("Surround 5.1");
AvailableAudioConfigs->Append("Surround 7.1");
AvailableFramePacing->Append("Immediate");
AvailableFramePacing->Append("Display-locked");
CurrentResolutionIndex = 0;
for (int i = 0; i < AvailableResolutions->Size; i++) {
if (host->Resolution->Width == AvailableResolutions->GetAt(i)->Width &&
host->Resolution->Height == AvailableResolutions->GetAt(i)->Height
) {
CurrentResolutionIndex = i;
break;
}
}
CurrentAppIndex = 0;
auto item = ref new ComboBoxItem();
item->Content = L"No App";
AutoStartSelector->Items->Append(item);
for (int i = 0; i < Host->Apps->Size; i++) {
auto item = ref new ComboBoxItem();
item->Content = Host->Apps->GetAt(i)->Name;
AutoStartSelector->Items->Append(item);
if (host->AutostartID == host->Apps->GetAt(i)->Id) {
CurrentAppIndex = i + 1;
}
}
AutoStartSelector->SelectedIndex = CurrentAppIndex;
// Set frame pacing selection based on saved preference
for (int i = 0; i < AvailableFramePacing->Size; i++) {
if (host->FramePacing == AvailableFramePacing->GetAt(i)) {
FramePacingComboBox->SelectedIndex = i;
break;
}
}
if (info.vendorId == GAMING_DEVICE_VENDOR_ID_MICROSOFT) {
// Old Xbox One can only use H264, remove from settings everything else
if (info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_ONE) {
CodecComboBox->IsEnabled = false;
CodecComboBox->SelectedIndex = 0;
}
// Disable HDR if console is not set to 4K
auto mode = HdmiDisplayInformation::GetForCurrentView()->GetCurrentDisplayMode();
auto height = mode->ResolutionHeightInRawPixels;
if (height < 2160) {
EnableHDRCheckbox->IsEnabled = false;
EnableHDRCheckbox->IsChecked = false;
EnableHDRCheckbox->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
HDR4KNote->Visibility = Windows::UI::Xaml::Visibility::Visible;
} else {
EnableHDRCheckbox->IsEnabled = true;
EnableHDRCheckbox->Visibility = Windows::UI::Xaml::Visibility::Visible;
HDR4KNote->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}
// Disable graphs at 4K on Xbox One
if (IsXboxOne() && height >= 2160) {
EnableGraphsCheckbox->IsEnabled = false;
EnableGraphsCheckbox->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
XboxOneGraphsNote->Visibility = Windows::UI::Xaml::Visibility::Visible;
} else {
EnableGraphsCheckbox->IsEnabled = true;
EnableGraphsCheckbox->Visibility = Windows::UI::Xaml::Visibility::Visible;
XboxOneGraphsNote->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}
}
if (host->IdrInterval > 0) {
EnableIdrIntervalCheckbox->IsChecked = true;
IdrIntervalSlider->Minimum = 2;
IdrIntervalSlider->IsEnabled = true;
} else {
EnableIdrIntervalCheckbox->IsChecked = false;
IdrIntervalSlider->Minimum = 0;
IdrIntervalSlider->Value = 0;
IdrIntervalSlider->IsEnabled = false;
}
}
void HostSettingsPage::backButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
GetApplicationState()->UpdateFile();
this->Frame->GoBack();
}
void HostSettingsPage::OnBackRequested(Platform::Object^ e, Windows::UI::Core::BackRequestedEventArgs^ args)
{
// UWP on Xbox One triggers a back request whenever the B
// button is pressed which can result in the app being
// suspended if unhandled
GetApplicationState()->UpdateFile();
this->Frame->GoBack();
args->Handled = true;
}
void HostSettingsPage::ResolutionSelector_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
auto selectedResolution = AvailableResolutions->GetAt(this->ResolutionSelector->SelectedIndex);
// Default to a new bitrate if a new resolution was chosen
if (selectedResolution->Width != host->Resolution->Width) {
host->Bitrate = getDefaultBitrate(selectedResolution->Width, selectedResolution->Height, host->FPS);
}
host->Resolution = selectedResolution;
}
void HostSettingsPage::FPSSelector_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
if (e->AddedItems->Size == 0) return;
int selectedFPS = (int)e->AddedItems->GetAt(0);
// Default to a new bitrate if a new FPS was chosen
if (selectedFPS != host->FPS) {
host->Bitrate = getDefaultBitrate(host->Resolution->Width, host->Resolution->Height, selectedFPS);
}
host->FPS = selectedFPS;
}
void HostSettingsPage::AutoStartSelector_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
int index = AutoStartSelector->SelectedIndex - 1;
if (index >= 0 && host->Apps->Size > index) {
host->AutostartID = host->Apps->GetAt(index)->Id;
}
else {
host->AutostartID = -1;
}
}
void HostSettingsPage::FramePacing_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
auto selectedFramePacing = AvailableFramePacing->GetAt(this->FramePacingComboBox->SelectedIndex);
if (selectedFramePacing == "Immediate") {
FramePacingImmediateDesc->Visibility = Windows::UI::Xaml::Visibility::Visible;
FramePacingDisplayLockedDesc->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
} else {
FramePacingImmediateDesc->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
FramePacingDisplayLockedDesc->Visibility = Windows::UI::Xaml::Visibility::Visible;
}
host->FramePacing = selectedFramePacing;
}
void HostSettingsPage::EnableIdrIntervalCheckbox_Checked(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) {
IdrIntervalSlider->IsEnabled = true;
if (IdrIntervalSlider->Value < 2) {
IdrIntervalSlider->Value = 10;
}
IdrIntervalSlider->Minimum = 2;
}
void HostSettingsPage::EnableIdrIntervalCheckbox_Unchecked(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) {
IdrIntervalSlider->IsEnabled = false;
IdrIntervalSlider->Minimum = 0;
IdrIntervalSlider->Value = 0;
}
void HostSettingsPage::GlobalSettingsOption_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(MoonlightSettings::typeid));
}
void HostSettingsPage::BitrateInput_KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
if (e->Key == Windows::System::VirtualKey::Enter) {
CoreInputView::GetForCurrentView()->TryHide();
}
}
void HostSettingsPage::OnLoaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
auto navigation = Windows::UI::Core::SystemNavigationManager::GetForCurrentView();
m_back_cookie = navigation->BackRequested += ref new EventHandler<BackRequestedEventArgs^>(this, &HostSettingsPage::OnBackRequested);
}
void HostSettingsPage::OnUnloaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
auto navigation = Windows::UI::Core::SystemNavigationManager::GetForCurrentView();
navigation->BackRequested -= m_back_cookie;
}
int HostSettingsPage::getDefaultBitrate(int width, int height, int fps)
{
// Don't scale bitrate linearly beyond 60 FPS. It's definitely not a linear
// bitrate increase for frame rate once we get to values that high.
float frameRateFactor = (fps <= 60 ? fps : (std::sqrtf(fps / 60.f) * 60.f)) / 30.f;
// TODO: Collect some empirical data to see if these defaults make sense.
// We're just using the values that the Shield used, as we have for years.
static const struct resTable {
int pixels;
float factor;
} resTable[] {
{ 1280 * 720, 5.0f },
{ 1920 * 1080, 10.0f },
{ 2560 * 1440, 20.0f },
{ 3840 * 2160, 40.0f },
{ -1, -1.0f },
};
// Calculate the resolution factor by linear interpolation of the resolution table
float resolutionFactor;
int pixels = width * height;
for (int i = 0;; i++) {
if (pixels == resTable[i].pixels) {
// We can bail immediately for exact matches
resolutionFactor = resTable[i].factor;
break;
}
else if (pixels < resTable[i].pixels) {
if (i == 0) {
// Never go below the lowest resolution entry
resolutionFactor = resTable[i].factor;
}
else {
// Interpolate between the entry greater than the chosen resolution (i) and the entry less than the chosen resolution (i-1)
resolutionFactor = ((float)(pixels - resTable[i-1].pixels) / (resTable[i].pixels - resTable[i-1].pixels)) * (resTable[i].factor - resTable[i-1].factor) + resTable[i-1].factor;
}
break;
}
else if (resTable[i].pixels == -1) {
// Never go above the highest resolution entry
resolutionFactor = resTable[i-1].factor;
break;
}
}
return std::lround(resolutionFactor * frameRateFactor) * 1000;
}