Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UWP/App.cpp
5665 views
1
#include "ppsspp_config.h"
2
3
#include "pch.h"
4
#include "App.h"
5
6
#include <mutex>
7
8
#include "Common/Net/HTTPClient.h"
9
#include "Common/Net/Resolve.h"
10
11
#include "Common/File/VFS/VFS.h"
12
#include "Common/File/VFS/DirectoryReader.h"
13
#include "Common/Data/Encoding/Utf8.h"
14
#include "Common/Input/InputState.h"
15
#include "Common/System/NativeApp.h"
16
#include "Common/System/System.h"
17
#include "Common/Log/LogManager.h"
18
#include "Core/System.h"
19
#include "Core/Config.h"
20
#include "Core/Core.h"
21
#include "UWPHelpers/LaunchItem.h"
22
#include <UWPUtil.h>
23
24
using namespace UWP;
25
26
using namespace winrt;
27
using namespace winrt::Windows::ApplicationModel;
28
using namespace winrt::Windows::ApplicationModel::Core;
29
using namespace winrt::Windows::ApplicationModel::Activation;
30
using namespace winrt::Windows::UI::Core;
31
using namespace winrt::Windows::UI::Input;
32
using namespace winrt::Windows::System;
33
using namespace winrt::Windows::Foundation;
34
using namespace winrt::Windows::Graphics::Display;
35
36
// The main function is only used to initialize our IFrameworkView class.
37
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
38
winrt::init_apartment();
39
CoreApplication::Run(winrt::make<Direct3DApplicationSource>());
40
return 0;
41
}
42
43
IFrameworkView Direct3DApplicationSource::CreateView() {
44
return winrt::make<App>();
45
}
46
47
App::App() :
48
m_windowClosed(false),
49
m_windowVisible(true)
50
{
51
}
52
53
void App::InitialPPSSPP() {
54
// Initial net
55
net::Init();
56
57
// Get install location
58
auto packageDirectory = Package::Current().InstalledPath();
59
const Path& exePath = Path(FromHString(packageDirectory));
60
g_VFS.Register("", new DirectoryReader(exePath / "Content"));
61
g_VFS.Register("", new DirectoryReader(exePath));
62
63
// Mount a filesystem
64
g_Config.flash0Directory = exePath / "assets/flash0";
65
66
// Prepare for initialization
67
std::wstring internalDataFolderW = std::wstring(winrt::Windows::Storage::ApplicationData::Current().LocalFolder().Path());
68
g_Config.internalDataDirectory = Path(internalDataFolderW);
69
g_Config.memStickDirectory = g_Config.internalDataDirectory;
70
71
// On Win32 it makes more sense to initialize the system directories here
72
// because the next place it was called was in the EmuThread, and it's too late by then.
73
CreateSysDirectories();
74
75
g_logManager.Init(&g_Config.bEnableLogging);
76
77
// Set the config path to local state by default
78
// it will be overrided by `NativeInit` if there is custom memStick
79
g_Config.SetSearchPath(GetSysDirectory(DIRECTORY_SYSTEM));
80
g_Config.Load();
81
82
if (g_Config.bFirstRun) {
83
// Clear `memStickDirectory` to show memory stick screen on first start
84
g_Config.memStickDirectory.clear();
85
}
86
87
// Since we don't have any async operation in `NativeInit`
88
// it's better to call it here
89
const char* argv[2] = { "fake", nullptr };
90
std::string cacheFolder = ConvertWStringToUTF8(
91
std::wstring(winrt::Windows::Storage::ApplicationData::Current().TemporaryFolder().Path())
92
);
93
// We will not be able to use `argv`
94
// since launch parameters usually handled by `OnActivated`
95
// and `OnActivated` will be invoked later, even after `PPSSPP_UWPMain(..)`
96
// so we are handling launch cases using `LaunchItem`
97
NativeInit(1, argv, "", "", cacheFolder.c_str());
98
99
// Override backend, `DIRECT3D11` is the only way for UWP apps
100
g_Config.iGPUBackend = (int)GPUBackend::DIRECT3D11;
101
102
// Calling `NativeInit` before will help us to deal with custom configs
103
// such as custom adapter, so it's better to initial render device here
104
m_deviceResources = std::make_shared<DX::DeviceResources>();
105
m_deviceResources->CreateWindowSizeDependentResources();
106
}
107
108
// The first method called when the IFrameworkView is being created.
109
void App::Initialize(const CoreApplicationView& applicationView) {
110
// Register event handlers for app lifecycle. This example includes Activated, so that we
111
// can make the CoreWindow active and start rendering on the window.
112
applicationView.Activated({ this, &App::OnActivated });
113
CoreApplication::Suspending({ this, &App::OnSuspending });
114
CoreApplication::Resuming({ this, &App::OnResuming });
115
}
116
117
// Called when the CoreWindow object is created (or re-created).
118
void App::SetWindow(const CoreWindow& window) {
119
window.SizeChanged({ this, &App::OnWindowSizeChanged });
120
window.VisibilityChanged({ this, &App::OnVisibilityChanged });
121
window.Closed({ this, &App::OnWindowClosed });
122
123
DisplayInformation currentDisplayInformation = DisplayInformation::GetForCurrentView();
124
125
currentDisplayInformation.DpiChanged({ this, &App::OnDpiChanged });
126
currentDisplayInformation.OrientationChanged({ this, &App::OnOrientationChanged });
127
DisplayInformation::DisplayContentsInvalidated({ this, &App::OnDisplayContentsInvalidated });
128
129
window.KeyDown({ this, &App::OnKeyDown });
130
window.KeyUp({ this, &App::OnKeyUp });
131
window.CharacterReceived({ this, &App::OnCharacterReceived });
132
133
window.PointerMoved({ this, &App::OnPointerMoved });
134
window.PointerEntered({ this, &App::OnPointerEntered });
135
window.PointerExited({ this, &App::OnPointerExited });
136
window.PointerPressed({ this, &App::OnPointerPressed });
137
window.PointerReleased({ this, &App::OnPointerReleased });
138
window.PointerCaptureLost({ this, &App::OnPointerCaptureLost });
139
window.PointerWheelChanged({ this, &App::OnPointerWheelChanged });
140
141
if (winrt::Windows::Foundation::Metadata::ApiInformation::IsTypePresent(L"Windows.Phone.UI.Input.HardwareButtons")) {
142
m_hardwareButtons.insert(HardwareButton::BACK);
143
}
144
145
if (winrt::Windows::System::Profile::AnalyticsInfo::VersionInfo().DeviceFamily() == L"Windows.Mobile") {
146
m_isPhone = true;
147
}
148
149
winrt::Windows::UI::Core::SystemNavigationManager::GetForCurrentView().BackRequested(
150
{ this, &App::App_BackRequested }
151
);
152
153
InitialPPSSPP();
154
}
155
156
bool App::HasBackButton() {
157
if (m_hardwareButtons.count(HardwareButton::BACK) != 0)
158
return true;
159
else
160
return false;
161
}
162
163
void App::App_BackRequested(const IInspectable& sender, const BackRequestedEventArgs& e) {
164
if (m_isPhone) {
165
e.Handled(m_main->OnHardwareButton(HardwareButton::BACK));
166
} else {
167
e.Handled(true);
168
}
169
}
170
171
void App::OnKeyDown(const CoreWindow& sender, const KeyEventArgs& args) {
172
m_main->OnKeyDown(args.KeyStatus().ScanCode, args.VirtualKey(), args.KeyStatus().RepeatCount);
173
}
174
175
void App::OnKeyUp(const CoreWindow& sender, const KeyEventArgs& args) {
176
m_main->OnKeyUp(args.KeyStatus().ScanCode, args.VirtualKey());
177
}
178
179
void App::OnCharacterReceived(const CoreWindow& sender, const CharacterReceivedEventArgs& args) {
180
m_main->OnCharacterReceived(args.KeyStatus().ScanCode, args.KeyCode());
181
}
182
183
void App::OnPointerMoved(const CoreWindow& sender, const PointerEventArgs& args) {
184
int pointerId = touchMap_.TouchId(args.CurrentPoint().PointerId());
185
if (pointerId < 0)
186
return;
187
float X = args.CurrentPoint().Position().X;
188
float Y = args.CurrentPoint().Position().Y;
189
int64_t timestamp = args.CurrentPoint().Timestamp();
190
m_main->OnTouchEvent(TouchInputFlags::MOVE, pointerId, X, Y, (double)timestamp);
191
}
192
193
void App::OnPointerEntered(const CoreWindow& sender, const PointerEventArgs& args) {
194
}
195
196
void App::OnPointerExited(const CoreWindow& sender, const PointerEventArgs& args) {
197
}
198
199
void App::OnPointerPressed(const CoreWindow& sender, const PointerEventArgs& args) {
200
int pointerId = touchMap_.TouchId(args.CurrentPoint().PointerId());
201
if (pointerId < 0)
202
pointerId = touchMap_.AddNewTouch(args.CurrentPoint().PointerId());
203
204
float X = args.CurrentPoint().Position().X;
205
float Y = args.CurrentPoint().Position().Y;
206
int64_t timestamp = args.CurrentPoint().Timestamp();
207
m_main->OnTouchEvent(TouchInputFlags::DOWN | TouchInputFlags::MOVE, pointerId, X, Y, (double)timestamp);
208
if (!m_isPhone) {
209
sender.SetPointerCapture();
210
}
211
}
212
213
void App::OnPointerReleased(const CoreWindow& sender, const PointerEventArgs& args) {
214
int pointerId = touchMap_.RemoveTouch(args.CurrentPoint().PointerId());
215
if (pointerId < 0)
216
return;
217
float X = args.CurrentPoint().Position().X;
218
float Y = args.CurrentPoint().Position().Y;
219
int64_t timestamp = args.CurrentPoint().Timestamp();
220
m_main->OnTouchEvent(TouchInputFlags::UP | TouchInputFlags::MOVE, pointerId, X, Y, (double)timestamp);
221
if (!m_isPhone) {
222
sender.ReleasePointerCapture();
223
}
224
}
225
226
void App::OnPointerCaptureLost(const CoreWindow& sender, const PointerEventArgs& args) {
227
}
228
229
void App::OnPointerWheelChanged(const CoreWindow& sender, const PointerEventArgs& args) {
230
int pointerId = 0; // irrelevant
231
float delta = (float)args.CurrentPoint().Properties().MouseWheelDelta();
232
m_main->OnMouseWheel(delta);
233
}
234
235
// Initializes scene resources, or loads a previously saved app state.
236
void App::Load(const hstring& entryPoint) {
237
if (m_main == nullptr) {
238
m_main = std::unique_ptr<PPSSPP_UWPMain>(new PPSSPP_UWPMain(this, m_deviceResources));
239
}
240
}
241
242
// This method is called after the window becomes active.
243
void App::Run() {
244
while (!m_windowClosed) {
245
if (m_windowVisible) {
246
CoreWindow::GetForCurrentThread().Dispatcher().ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
247
m_main->Render();
248
// TODO: Adopt some practices from m_deviceResources->Present();
249
} else {
250
CoreWindow::GetForCurrentThread().Dispatcher().ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
251
}
252
}
253
}
254
255
// Required for IFrameworkView.
256
// Terminate events do not cause Uninitialize to be called. It will be called if your IFrameworkView
257
// class is torn down while the app is in the foreground.
258
void App::Uninitialize() {
259
}
260
261
// Application lifecycle event handlers.
262
void App::OnActivated(const CoreApplicationView& applicationView, const IActivatedEventArgs& args) {
263
// Run() won't start until the CoreWindow is activated.
264
CoreWindow::GetForCurrentThread().Activate();
265
// On mobile, we force-enter fullscreen mode.
266
if (m_isPhone)
267
g_Config.bFullScreen = true;
268
269
if (g_Config.bFullScreen)
270
winrt::Windows::UI::ViewManagement::ApplicationView::GetForCurrentView().TryEnterFullScreenMode();
271
272
//Detect if app started or activated by launch item (file, uri)
273
DetectLaunchItem(args);
274
}
275
276
void App::OnSuspending(const IInspectable& sender, const SuspendingEventArgs& args) {
277
// Save app state asynchronously after requesting a deferral. Holding a deferral
278
// indicates that the application is busy performing suspending operations. Be
279
// aware that a deferral may not be held indefinitely. After about five seconds,
280
// the app will be forced to exit.
281
SuspendingDeferral deferral = args.SuspendingOperation().GetDeferral();
282
auto app = this;
283
284
std::thread([app, deferral]() {
285
g_Config.Save("App::OnSuspending");
286
app->m_deviceResources->Trim();
287
deferral.Complete();
288
}).detach();
289
}
290
291
void App::OnResuming(const IInspectable& sender, const IInspectable& args) {
292
// Restore any data or state that was unloaded on suspend. By default, data
293
// and state are persisted when resuming from suspend. Note that this event
294
// does not occur if the app was previously terminated.
295
296
// Insert your code here.
297
}
298
299
// Window event handlers.
300
void App::OnWindowSizeChanged(const CoreWindow& sender, const WindowSizeChangedEventArgs& args) {
301
auto view = winrt::Windows::UI::ViewManagement::ApplicationView::GetForCurrentView();
302
g_Config.bFullScreen = view.IsFullScreenMode();
303
304
float width = sender.Bounds().Width;
305
float height = sender.Bounds().Height;
306
float scale = m_deviceResources->GetDpi() / 96.0f;
307
308
m_deviceResources->SetLogicalSize(winrt::Windows::Foundation::Size(width, height));
309
if (m_main) {
310
m_main->CreateWindowSizeDependentResources();
311
}
312
313
PSP_CoreParameter().pixelWidth = (int)(width * scale);
314
PSP_CoreParameter().pixelHeight = (int)(height * scale);
315
if (Native_UpdateScreenScale((int)width, (int)height, UIScaleFactorToMultiplier(g_Config.iUIScaleFactor))) {
316
System_PostUIMessage(UIMessage::GPU_DISPLAY_RESIZED);
317
}
318
}
319
320
void App::OnVisibilityChanged(const CoreWindow& sender, const VisibilityChangedEventArgs& args) {
321
m_windowVisible = args.Visible();
322
}
323
324
void App::OnWindowClosed(const CoreWindow& sender, const CoreWindowEventArgs& args) {
325
m_windowClosed = true;
326
}
327
328
// DisplayInformation event handlers.
329
void App::OnDpiChanged(const DisplayInformation& sender, const IInspectable& args) {
330
// Note: The value for LogicalDpi retrieved here may not match the effective DPI of the app
331
// if it is being scaled for high resolution devices. Once the DPI is set on DeviceResources,
332
// you should always retrieve it using the GetDpi method.
333
// See DeviceResources.cpp for more details.
334
m_deviceResources->SetDpi(sender.LogicalDpi());
335
m_main->CreateWindowSizeDependentResources();
336
}
337
338
void App::OnOrientationChanged(const DisplayInformation& sender, const IInspectable& args) {
339
m_deviceResources->SetCurrentOrientation(sender.CurrentOrientation());
340
m_main->CreateWindowSizeDependentResources();
341
}
342
343
void App::OnDisplayContentsInvalidated(const DisplayInformation& sender, const IInspectable& args) {
344
m_deviceResources->ValidateDevice();
345
}
346
347