-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathAppPage.xaml.cpp
More file actions
383 lines (334 loc) · 14.2 KB
/
Copy pathAppPage.xaml.cpp
File metadata and controls
383 lines (334 loc) · 14.2 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
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
#include "pch.h"
#include "AppPage.Xaml.h"
#include "Common\ModalDialog.xaml.h"
#include "HostSettingsPage.xaml.h"
#include "HostSelectorPage.xaml.h"
#include "State\MoonlightClient.h"
#include "StreamPage.xaml.h"
#include "Utils.hpp"
#include <algorithm>
#include <cmath>
#include <vector>
using namespace moonlight_xbox_dx;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace concurrency;
using namespace Windows::UI::Xaml::Hosting;
using namespace Windows::UI::Composition;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
static MoonlightApp^ GetAppById(MoonlightHost^ host, int appId) {
if (host == nullptr) {
return nullptr;
}
for (unsigned int i = 0; i < host->Apps->Size; ++i) {
auto app = host->Apps->GetAt(i);
if (app != nullptr && app->Id == appId) {
return app;
}
}
return nullptr;
}
AppPage::AppPage()
{
InitializeComponent();
Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()->SetDesiredBoundsMode(Windows::UI::ViewManagement::ApplicationViewBoundsMode::UseVisible);
this->Loaded += ref new Windows::UI::Xaml::RoutedEventHandler(this, &AppPage::OnLoaded);
this->Unloaded += ref new Windows::UI::Xaml::RoutedEventHandler(this, &AppPage::OnUnloaded);
}
void AppPage::OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) {
MoonlightHost^ mhost = dynamic_cast<MoonlightHost^>(e->Parameter);
if (mhost == nullptr) return;
host = mhost;
host->UpdateHostInfo(true);
host->UpdateApps();
// Start background polling for app running state and connectivity
continueAppFetch.store(true);
wasConnected.store(host->Connected);
Platform::WeakReference weakThis(this);
create_task([weakThis]() {
while (true) {
auto that = weakThis.Resolve<AppPage>();
if (that == nullptr) break;
if (!that->continueAppFetch.load()) break;
try {
if (that->host != nullptr) {
that->host->UpdateAppRunningStates();
if (that->wasConnected.load() && !that->host->Connected) {
that->wasConnected.store(false);
// Show the disconnect dialog only if page instance still exists (no visible-page checks)
Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
Windows::UI::Core::CoreDispatcherPriority::Normal,
ref new Windows::UI::Core::DispatchedHandler([weakThis]() {
auto inner = weakThis.Resolve<AppPage>();
if (inner == nullptr) return;
try {
auto dialog = ref new Windows::UI::Xaml::Controls::ContentDialog();
dialog->Title = Utils::StringFromStdString("Disconnected");
dialog->Content = Utils::StringFromStdString("Connection to host was lost.");
dialog->PrimaryButtonText = Utils::StringFromStdString("OK");
concurrency::create_task(::moonlight_xbox_dx::ModalDialog::ShowOnceAsync(dialog)).then([weakThis](Windows::UI::Xaml::Controls::ContentDialogResult result) {
auto that2 = weakThis.Resolve<AppPage>();
if (that2 == nullptr) return;
that2->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([that2]() {
try {
that2->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(HostSelectorPage::typeid));
} catch (const std::exception &e) {
Utils::Logf("[AppPage] Failed to navigate to HostSelectorPage after disconnect. Exception: %s\n", e.what());
} catch (...) {
Utils::Log("[AppPage] Failed to navigate to HostSelectorPage after disconnect. Unknown Exception.\n");
}
}));
});
} catch (const std::exception &e) {
Utils::Logf("[AppPage] Failed to show disconnect dialog. Exception: %s\n", e.what());
} catch (...) {
Utils::Log("[AppPage] Failed to show disconnect dialog. Unknown Exception.\n");
}
}));
}
else if (!that->wasConnected.load() && that->host->Connected) {
that->wasConnected.store(true);
}
}
} catch (const std::exception &e) {
Utils::Logf("[AppPage] Failed to poll app and host running state. Exception: %s\n", e.what());
} catch (...) {
Utils::Log("[AppPage] Failed to poll app and host running state. Unknown Exception.\n");
}
Sleep(3000);
}
});
if (host->AutostartID >= 0 && GetApplicationState()->shouldAutoConnect) {
GetApplicationState()->shouldAutoConnect = false;
Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
Windows::UI::Core::CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([this]() {
this->Connect(host->AutostartID);
}));
}
GetApplicationState()->shouldAutoConnect = false;
}
void AppPage::OnNavigatedFrom(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) {
continueAppFetch.store(false);
}
void AppPage::AppsGrid_ItemClick(Platform::Object ^ sender, Windows::UI::Xaml::Controls::ItemClickEventArgs ^ e) {
MoonlightApp ^ app = (MoonlightApp ^) e->ClickedItem;
this->currentApp = app;
if (this->host != nullptr) {
for (unsigned int i = 0; i < this->host->Apps->Size; ++i) {
auto candidate = this->host->Apps->GetAt(i);
if (candidate != nullptr && candidate->CurrentlyRunning && candidate->Id != app->Id) {
this->closeAndStartButton_Click(nullptr, nullptr);
return;
}
}
}
this->Connect(app->Id);
}
void AppPage::Connect(int appId) {
continueAppFetch.store(false);
MoonlightApp^ app = GetAppById(host, appId);
StreamConfiguration ^ config = ref new StreamConfiguration();
config->hostname = host->LastHostname;
config->appID = appId;
config->appName = app ? app->Name : "App";
config->width = host->Resolution->Width;
config->height = host->Resolution->Height;
config->bitrate = host->Bitrate;
config->FPS = host->FPS;
config->audioConfig = host->AudioConfig;
config->videoCodec = host->VideoCodec;
config->playAudioOnPC = host->PlayAudioOnPC;
config->enableHDR = host->EnableHDR;
config->enableSOPS = host->EnableSOPS;
config->framePacing = host->FramePacing;
config->enableStats = host->EnableStats;
config->enableGraphs = host->EnableGraphs;
config->idrInterval = host->IdrInterval;
if (config->enableHDR) {
host->VideoCodec = "HEVC (H.265)";
}
bool result = this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(StreamPage::typeid), config);
if (!result) {
printf("C");
}
}
void AppPage::AppsGrid_RightTapped(Platform::Object ^ sender, Windows::UI::Xaml::Input::RightTappedRoutedEventArgs ^ e) {
Utils::Log("AppPage::AppsGrid_RightTapped invoked\n");
FrameworkElement ^ senderElement = (FrameworkElement ^) e->OriginalSource;
FrameworkElement ^ anchor = senderElement;
if (senderElement != nullptr && senderElement->GetType()->FullName->Equals(GridViewItem::typeid->FullName)) {
auto gi = (GridViewItem ^) senderElement;
currentApp = (MoonlightApp ^)(gi->Content);
anchor = gi;
} else {
if (senderElement != nullptr) currentApp = (MoonlightApp ^)(senderElement->DataContext);
if (currentApp == nullptr && this->HostsGrid != nullptr && this->HostsGrid->SelectedIndex >= 0) {
currentApp = (MoonlightApp ^) this->HostsGrid->SelectedItem;
auto container = (GridViewItem ^) this->HostsGrid->ContainerFromIndex(this->HostsGrid->SelectedIndex);
if (container != nullptr)
anchor = container;
else
anchor = this->HostsGrid;
}
}
bool anyRunning = false;
MoonlightApp^ runningApp = nullptr;
if (this->host != nullptr) {
for (unsigned int i = 0; i < this->host->Apps->Size; ++i) {
auto candidate = this->host->Apps->GetAt(i);
if (candidate != nullptr && candidate->CurrentlyRunning) {
anyRunning = true;
runningApp = candidate;
break;
}
}
}
this->resumeAppButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
this->closeAppButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
this->closeAndStartButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
if (!anyRunning) {
this->resumeAppButton->Text = "Open App";
this->resumeAppButton->Visibility = Windows::UI::Xaml::Visibility::Visible;
} else {
if (currentApp != nullptr && currentApp->CurrentlyRunning) {
this->resumeAppButton->Text = "Resume App";
this->resumeAppButton->Visibility = Windows::UI::Xaml::Visibility::Visible;
this->closeAppButton->Visibility = Windows::UI::Xaml::Visibility::Visible;
} else {
if (currentApp != nullptr) {
this->closeAndStartButton->Visibility = Windows::UI::Xaml::Visibility::Visible;
}
}
}
if (anchor != nullptr) {
this->ActionsFlyout->ShowAt(anchor);
} else {
this->ActionsFlyout->ShowAt(this->HostsGrid);
}
}
void AppPage::resumeAppButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) {
this->Connect(this->currentApp->Id);
}
void AppPage::closeAndStartButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) {
if (this->currentApp == nullptr) {
return;
}
if (sender != nullptr) {
this->ExecuteCloseAndStart();
return;
}
auto dialog = ref new Windows::UI::Xaml::Controls::ContentDialog();
dialog->Title = Utils::StringFromStdString("Confirm");
dialog->Content = Utils::StringFromStdString("Close currently running app and connect?");
dialog->PrimaryButtonText = Utils::StringFromStdString("Yes");
dialog->CloseButtonText = Utils::StringFromStdString("Cancel");
Platform::WeakReference weakThis(this);
concurrency::create_task(::moonlight_xbox_dx::ModalDialog::ShowOnceAsync(dialog)).then([weakThis](Windows::UI::Xaml::Controls::ContentDialogResult result) {
try {
if (result != Windows::UI::Xaml::Controls::ContentDialogResult::Primary) {
return;
}
auto that = weakThis.Resolve<AppPage>();
if (that == nullptr) return;
that->ExecuteCloseAndStart();
} catch (const std::exception &e) {
Utils::Logf("closeAndStartButton_Click dialog task exception: %s\n", e.what());
} catch (...) {
Utils::Log("closeAndStartButton_Click dialog task unknown exception\n");
}
});
}
void AppPage::ExecuteCloseAndStart() {
auto that = this;
auto progressToken = ::moonlight_xbox_dx::ModalDialog::ShowProgressDialogToken(nullptr, Utils::StringFromStdString("Closing app..."));
moonlight_xbox_dx::Utils::Logf("AppPage::ExecuteCloseAndStart: progressToken=%llu\n", (unsigned long long)progressToken);
concurrency::create_task(concurrency::create_async([that, progressToken]() {
try {
MoonlightClient client;
auto ipAddr = Utils::PlatformStringToStdString(that->host->LastHostname);
int status = client.Connect(ipAddr.c_str());
if (status == 0) {
client.StopApp();
Sleep(1000);
}
} catch (...) {
}
that->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([that, progressToken]() {
try {
if (that->currentApp != nullptr) {
that->Connect(that->currentApp->Id);
}
::moonlight_xbox_dx::ModalDialog::HideDialogByToken(progressToken);
} catch (const std::exception &e) {
Utils::Logf("ExecuteCloseAndStart UI exception: %s\n", e.what());
} catch (...) {
Utils::Log("ExecuteCloseAndStart UI unknown exception\n");
}
}));
})).then([](concurrency::task<void> t) {
try {
t.get();
} catch (const std::exception &e) {
Utils::Logf("ExecuteCloseAndStart task exception: %s\n", e.what());
} catch (...) {
Utils::Log("ExecuteCloseAndStart unknown task exception\n");
}
});
}
void AppPage::closeAppButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) {
auto that = this;
auto progressToken = ::moonlight_xbox_dx::ModalDialog::ShowProgressDialogToken(Utils::StringFromStdString("Closing"), Utils::StringFromStdString("Closing app..."));
moonlight_xbox_dx::Utils::Logf("AppPage::closeAppButton_Click: progressToken=%llu\n", (unsigned long long)progressToken);
concurrency::create_task(concurrency::create_async([that, progressToken]() {
try {
MoonlightClient client;
auto ipAddr = Utils::PlatformStringToStdString(that->host->LastHostname);
int status = client.Connect(ipAddr.c_str());
if (status == 0) {
client.StopApp();
Sleep(1000);
}
} catch (...) {
}
that->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([that, progressToken]() {
try {
that->host->UpdateHostInfo(true);
that->host->UpdateAppRunningStates();
} catch (...) {
}
::moonlight_xbox_dx::ModalDialog::HideDialogByToken(progressToken);
}));
}));
}
void AppPage::backButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) {
this->Frame->GoBack();
}
void AppPage::settingsButton_Click(Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e) {
bool result = this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(HostSettingsPage::typeid), Host);
}
void AppPage::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
if (this->Frame->CanGoBack) {
this->Frame->GoBack();
args->Handled = true;
}
}
void AppPage::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, &AppPage::OnBackRequested);
}
void AppPage::OnUnloaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) {
auto navigation = Windows::UI::Core::SystemNavigationManager::GetForCurrentView();
navigation->BackRequested -= m_back_cookie;
// stop background polling loop
continueAppFetch.store(false);
}