-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathApp.xaml.cpp
More file actions
259 lines (236 loc) · 8.88 KB
/
Copy pathApp.xaml.cpp
File metadata and controls
259 lines (236 loc) · 8.88 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
//
// App.xaml.cpp
// Implementation of the App class.
//
#include "pch.h"
#include <Utils.hpp>
#include "MoonlightWelcome.xaml.h"
#include "Pages\StreamPage.xaml.h"
using namespace moonlight_xbox_dx;
using namespace Platform;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
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::Interop;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
App::App()
{
InitializeComponent();
RequiresPointerMode = Windows::UI::Xaml::ApplicationRequiresPointerMode::WhenRequested;
Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
Resuming += ref new EventHandler<Object^>(this, &App::OnResuming);
displayRequest = ref new Windows::System::Display::DisplayRequest();
}
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used when the application is launched to open a specific file, to display
/// search results, and so forth.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e)
{
// #if _DEBUG
// if (IsDebuggerPresent())
// {
// DebugSettings->EnableFrameRateCounter = true;
// }
// #endif
moonlight_xbox_dx::Utils::Log("Hello from Moonlight!\n");
auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == nullptr)
{
// Create a Frame to act as the navigation context and associate it with
// a SuspensionManager key
rootFrame = ref new Frame();
rootFrame->NavigationFailed += ref new Windows::UI::Xaml::Navigation::NavigationFailedEventHandler(this, &App::OnNavigationFailed);
// Place the frame in the current Window
Window::Current->Content = rootFrame;
}
if (rootFrame->Content == nullptr)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame->Navigate(TypeName(HostSelectorPage::typeid), e->Arguments);
}
if (m_menuPage == nullptr)
{
m_menuPage = dynamic_cast<HostSelectorPage^>(rootFrame->Content);
}
// Ensure the current window is active
Window::Current->Activate();
//Start the state
auto state = GetApplicationState();
auto that = this;
state->Init().then([that](){
that->m_stateLoaded = true;
that->m_menuPage->OnStateLoaded();
});
displayRequest->RequestActive();
}
namespace {
// Parsed from a protocol activation, e.g. moonlight:?host=192.168.1.10&appName=Desktop&launchOnExit=retropass:
struct ProtocolLaunchRequest {
bool hasTarget = false;
std::wstring host;
int appId = -1;
std::wstring appName;
bool resume = false;
bool hasLaunchOnExit = false;
Platform::String^ launchOnExit;
};
// Flag-style parameters count as enabled when present without a value
bool IsTruthyParam(Platform::String^ value) {
if (value == nullptr || value->IsEmpty()) return true;
return _wcsicmp(value->Data(), L"true") == 0 || _wcsicmp(value->Data(), L"1") == 0 || _wcsicmp(value->Data(), L"yes") == 0;
}
ProtocolLaunchRequest ParseProtocolUri(Windows::Foundation::Uri^ uri) {
ProtocolLaunchRequest request;
Windows::Foundation::WwwFormUrlDecoder^ query = nullptr;
try {
query = uri->QueryParsed;
} catch (...) {
moonlight_xbox_dx::Utils::Log("Protocol activation: failed to parse query string\n");
return request;
}
if (query == nullptr) return request;
for (unsigned int i = 0; i < query->Size; i++) {
auto entry = query->GetAt(i);
if (entry == nullptr || entry->Name == nullptr) continue;
const wchar_t* name = entry->Name->Data();
Platform::String^ value = entry->Value;
bool hasValue = value != nullptr && !value->IsEmpty();
if (_wcsicmp(name, L"host") == 0 && hasValue) {
request.host = value->Data();
request.hasTarget = true;
} else if (_wcsicmp(name, L"appId") == 0 && hasValue) {
request.appId = (int)wcstol(value->Data(), nullptr, 10);
request.hasTarget = true;
} else if (_wcsicmp(name, L"appName") == 0 && hasValue) {
request.appName = value->Data();
request.hasTarget = true;
} else if (_wcsicmp(name, L"desktop") == 0) {
if (IsTruthyParam(value)) {
request.appName = L"Desktop";
request.hasTarget = true;
}
} else if (_wcsicmp(name, L"resume") == 0) {
if (IsTruthyParam(value)) {
request.resume = true;
request.hasTarget = true;
}
} else if (_wcsicmp(name, L"launchOnExit") == 0 && hasValue) {
// Return URI provided by the launching frontend (e.g. "retropass:"), passed through as-is
request.launchOnExit = value;
request.hasLaunchOnExit = true;
}
}
return request;
}
}
/// <summary>
/// Invoked when the application is activated through a URI scheme (moonlight:).
/// </summary>
void App::OnActivated(Windows::ApplicationModel::Activation::IActivatedEventArgs^ e)
{
if (e->Kind != Windows::ApplicationModel::Activation::ActivationKind::Protocol) {
return;
}
auto protocolArgs = dynamic_cast<Windows::ApplicationModel::Activation::ProtocolActivatedEventArgs^>(e);
if (protocolArgs == nullptr || protocolArgs->Uri == nullptr) {
return;
}
moonlight_xbox_dx::Utils::Logf("Protocol activation: %S\n", protocolArgs->Uri->AbsoluteUri->Data());
ProtocolLaunchRequest request = ParseProtocolUri(protocolArgs->Uri);
auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
bool isColdStart = (rootFrame == nullptr);
if (rootFrame == nullptr)
{
rootFrame = ref new Frame();
rootFrame->NavigationFailed += ref new Windows::UI::Xaml::Navigation::NavigationFailedEventHandler(this, &App::OnNavigationFailed);
Window::Current->Content = rootFrame;
}
// Never interrupt an active streaming session, just bring the window to the foreground
if (dynamic_cast<StreamPage^>(rootFrame->Content) != nullptr) {
moonlight_xbox_dx::Utils::Log("Protocol activation ignored: a stream is currently active\n");
Window::Current->Activate();
return;
}
auto state = GetApplicationState();
state->pendingProtocolHostSelect = request.hasTarget;
state->pendingProtocolHost = request.host;
state->pendingProtocolAppId = request.appId;
state->pendingProtocolAppName = request.appName;
state->pendingProtocolResume = request.resume;
if (request.hasLaunchOnExit) {
state->launchOnExitUri = request.launchOnExit;
}
rootFrame->Navigate(TypeName(HostSelectorPage::typeid));
rootFrame->BackStack->Clear();
m_menuPage = dynamic_cast<HostSelectorPage^>(rootFrame->Content);
Window::Current->Activate();
if (isColdStart) {
displayRequest->RequestActive();
}
auto that = this;
if (!m_stateLoaded) {
state->Init().then([that]() {
that->m_stateLoaded = true;
that->m_menuPage->OnStateLoaded();
});
} else {
m_menuPage->OnStateLoaded();
}
}
/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
{
(void) sender; // Unused parameter
(void) e; // Unused parameter
displayRequest->RequestRelease();
}
/// <summary>
/// Invoked when application execution is being resumed.
/// </summary>
/// <param name="sender">The source of the resume request.</param>
/// <param name="args">Details about the resume request.</param>
void App::OnResuming(Object ^sender, Object ^args)
{
(void) sender; // Unused parameter
(void) args; // Unused parameter
displayRequest->RequestActive();
}
/// <summary>
/// Invoked when Navigation to a certain page fails
/// </summary>
/// <param name="sender">The Frame which failed navigation</param>
/// <param name="e">Details about the navigation failure</param>
void App::OnNavigationFailed(Platform::Object ^sender, Windows::UI::Xaml::Navigation::NavigationFailedEventArgs ^e)
{
e->Handled = true;
Windows::UI::Xaml::Controls::ContentDialog^ dialog = ref new Windows::UI::Xaml::Controls::ContentDialog();
dialog->Content = e->Exception.ToString();
dialog->CloseButtonText = L"OK";
dialog->ShowAsync();
//throw ref new FailureException("Failed to load Page " + e->SourcePageType.Name);
}