#include "ppsspp_config.h"
#include "pch.h"
#include "App.h"
#include <ppltasks.h>
#include <mutex>
#include "Common/Net/HTTPClient.h"
#include "Common/Net/Resolve.h"
#include "Common/File/VFS/VFS.h"
#include "Common/File/VFS/DirectoryReader.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/Input/InputState.h"
#include "Common/System/NativeApp.h"
#include "Common/System/System.h"
#include "Common/Log/LogManager.h"
#include "Core/System.h"
#include "Core/Config.h"
#include "Core/Core.h"
#include "UWPHelpers/LaunchItem.h"
#include <UWPUtil.h>
using namespace UWP;
using namespace concurrency;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Core;
using namespace Windows::UI::Input;
using namespace Windows::System;
using namespace Windows::Foundation;
using namespace Windows::Graphics::Display;
[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^) {
auto direct3DApplicationSource = ref new Direct3DApplicationSource();
CoreApplication::Run(direct3DApplicationSource);
return 0;
}
IFrameworkView^ Direct3DApplicationSource::CreateView() {
return ref new App();
}
App::App() :
m_windowClosed(false),
m_windowVisible(true)
{
}
void App::InitialPPSSPP() {
net::Init();
auto packageDirectory = Package::Current->InstalledPath;
const Path& exePath = Path(FromPlatformString(packageDirectory));
g_VFS.Register("", new DirectoryReader(exePath / "Content"));
g_VFS.Register("", new DirectoryReader(exePath));
g_Config.flash0Directory = exePath / "assets/flash0";
std::wstring internalDataFolderW = ApplicationData::Current->LocalFolder->Path->Data();
g_Config.internalDataDirectory = Path(internalDataFolderW);
g_Config.memStickDirectory = g_Config.internalDataDirectory;
CreateSysDirectories();
g_logManager.Init(&g_Config.bEnableLogging);
g_Config.SetSearchPath(GetSysDirectory(DIRECTORY_SYSTEM));
g_Config.Load();
if (g_Config.bFirstRun) {
g_Config.memStickDirectory.clear();
}
const char* argv[2] = { "fake", nullptr };
std::string cacheFolder = ConvertWStringToUTF8(ApplicationData::Current->TemporaryFolder->Path->Data());
NativeInit(1, argv, "", "", cacheFolder.c_str());
g_Config.iGPUBackend = (int)GPUBackend::DIRECT3D11;
m_deviceResources = std::make_shared<DX::DeviceResources>();
m_deviceResources->CreateWindowSizeDependentResources();
}
void App::Initialize(CoreApplicationView^ applicationView) {
applicationView->Activated +=
ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
CoreApplication::Suspending +=
ref new EventHandler<SuspendingEventArgs^>(this, &App::OnSuspending);
CoreApplication::Resuming +=
ref new EventHandler<Platform::Object^>(this, &App::OnResuming);
}
void App::SetWindow(CoreWindow^ window) {
window->SizeChanged +=
ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &App::OnWindowSizeChanged);
window->VisibilityChanged +=
ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
window->Closed +=
ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
currentDisplayInformation->DpiChanged +=
ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDpiChanged);
currentDisplayInformation->OrientationChanged +=
ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnOrientationChanged);
DisplayInformation::DisplayContentsInvalidated +=
ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDisplayContentsInvalidated);
window->KeyDown += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &App::OnKeyDown);
window->KeyUp += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &App::OnKeyUp);
window->CharacterReceived += ref new TypedEventHandler<CoreWindow^, CharacterReceivedEventArgs^>(this, &App::OnCharacterReceived);
window->PointerMoved += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerMoved);
window->PointerEntered += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerEntered);
window->PointerExited += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerExited);
window->PointerPressed += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerPressed);
window->PointerReleased += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerReleased);
window->PointerCaptureLost += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerCaptureLost);
window->PointerWheelChanged += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerWheelChanged);
if (Windows::Foundation::Metadata::ApiInformation::IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) {
m_hardwareButtons.insert(HardwareButton::BACK);
}
if (Windows::System::Profile::AnalyticsInfo::VersionInfo->DeviceFamily == "Windows.Mobile") {
m_isPhone = true;
}
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->
BackRequested += ref new Windows::Foundation::EventHandler<
Windows::UI::Core::BackRequestedEventArgs^>(
this, &App::App_BackRequested);
InitialPPSSPP();
}
bool App::HasBackButton() {
if (m_hardwareButtons.count(HardwareButton::BACK) != 0)
return true;
else
return false;
}
void App::App_BackRequested(Platform::Object^ sender, Windows::UI::Core::BackRequestedEventArgs^ e) {
if (m_isPhone) {
e->Handled = m_main->OnHardwareButton(HardwareButton::BACK);
} else {
e->Handled = true;
}
}
void App::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args) {
m_main->OnKeyDown(args->KeyStatus.ScanCode, args->VirtualKey, args->KeyStatus.RepeatCount);
}
void App::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args) {
m_main->OnKeyUp(args->KeyStatus.ScanCode, args->VirtualKey);
}
void App::OnCharacterReceived(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CharacterReceivedEventArgs^ args) {
m_main->OnCharacterReceived(args->KeyStatus.ScanCode, args->KeyCode);
}
void App::OnPointerMoved(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
int pointerId = touchMap_.TouchId(args->CurrentPoint->PointerId);
if (pointerId < 0)
return;
float X = args->CurrentPoint->Position.X;
float Y = args->CurrentPoint->Position.Y;
int64_t timestamp = args->CurrentPoint->Timestamp;
m_main->OnTouchEvent(TOUCH_MOVE, pointerId, X, Y, (double)timestamp);
}
void App::OnPointerEntered(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
}
void App::OnPointerExited(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
}
void App::OnPointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
int pointerId = touchMap_.TouchId(args->CurrentPoint->PointerId);
if (pointerId < 0)
pointerId = touchMap_.AddNewTouch(args->CurrentPoint->PointerId);
float X = args->CurrentPoint->Position.X;
float Y = args->CurrentPoint->Position.Y;
int64_t timestamp = args->CurrentPoint->Timestamp;
m_main->OnTouchEvent(TOUCH_DOWN|TOUCH_MOVE, pointerId, X, Y, (double)timestamp);
if (!m_isPhone) {
sender->SetPointerCapture();
}
}
void App::OnPointerReleased(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
int pointerId = touchMap_.RemoveTouch(args->CurrentPoint->PointerId);
if (pointerId < 0)
return;
float X = args->CurrentPoint->Position.X;
float Y = args->CurrentPoint->Position.Y;
int64_t timestamp = args->CurrentPoint->Timestamp;
m_main->OnTouchEvent(TOUCH_UP|TOUCH_MOVE, pointerId, X, Y, (double)timestamp);
if (!m_isPhone) {
sender->ReleasePointerCapture();
}
}
void App::OnPointerCaptureLost(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
}
void App::OnPointerWheelChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
int pointerId = 0;
float delta = (float)args->CurrentPoint->GetCurrentPoint(args->CurrentPoint->PointerId)->Properties->MouseWheelDelta;
m_main->OnMouseWheel(delta);
}
void App::Load(Platform::String^ entryPoint) {
if (m_main == nullptr) {
m_main = std::unique_ptr<PPSSPP_UWPMain>(new PPSSPP_UWPMain(this, m_deviceResources));
}
}
void App::Run() {
while (!m_windowClosed) {
if (m_windowVisible) {
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
m_main->Render();
} else {
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
}
}
}
void App::Uninitialize() {
}
void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args) {
CoreWindow::GetForCurrentThread()->Activate();
if (m_isPhone)
g_Config.iForceFullScreen = 1;
if (g_Config.UseFullScreen())
Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()->TryEnterFullScreenMode();
DetectLaunchItem(args);
}
void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args) {
SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
auto app = this;
create_task([app, deferral]() {
g_Config.Save("App::OnSuspending");
app->m_deviceResources->Trim();
deferral->Complete();
});
}
void App::OnResuming(Platform::Object^ sender, Platform::Object^ args) {
}
void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args) {
auto view = Windows::UI::ViewManagement::ApplicationView::GetForCurrentView();
g_Config.bFullScreen = view->IsFullScreenMode;
g_Config.iForceFullScreen = -1;
float width = sender->Bounds.Width;
float height = sender->Bounds.Height;
float scale = m_deviceResources->GetDpi() / 96.0f;
m_deviceResources->SetLogicalSize(Size(width, height));
if (m_main) {
m_main->CreateWindowSizeDependentResources();
}
PSP_CoreParameter().pixelWidth = (int)(width * scale);
PSP_CoreParameter().pixelHeight = (int)(height * scale);
if (Native_UpdateScreenScale((int)width, (int)height, UIScaleFactorToMultiplier(g_Config.iUIScaleFactor))) {
System_PostUIMessage(UIMessage::GPU_DISPLAY_RESIZED);
}
}
void App::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args) {
m_windowVisible = args->Visible;
}
void App::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args) {
m_windowClosed = true;
}
void App::OnDpiChanged(DisplayInformation^ sender, Object^ args) {
m_deviceResources->SetDpi(sender->LogicalDpi);
m_main->CreateWindowSizeDependentResources();
}
void App::OnOrientationChanged(DisplayInformation^ sender, Object^ args) {
m_deviceResources->SetCurrentOrientation(sender->CurrentOrientation);
m_main->CreateWindowSizeDependentResources();
}
void App::OnDisplayContentsInvalidated(DisplayInformation^ sender, Object^ args) {
m_deviceResources->ValidateDevice();
}