CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/UWP/UWPHelpers/LaunchItem.cpp
Views: 1401
// Copyright (c) 2023- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "pch.h"18#include <io.h>19#include <fcntl.h>20#include <regex>2122#include "LaunchItem.h"23#include "StorageAccess.h"2425#include "Common/Log.h"26#include "Common/System/System.h"27#include "Common/File/Path.h"28#include "UWPUtil.h"2930#include <ppl.h>31#include <ppltasks.h>3233#pragma region LaunchItemClass34class LaunchItem {35public:36LaunchItem() {37}3839~LaunchItem() {40delete storageFile;41}4243void Activate(IStorageFile^ file) {44storageFile = file;45AddItemToFutureList(storageFile);46launchPath = std::string();47launchOnExit = std::string();48}4950void Activate(ProtocolActivatedEventArgs^ args) {51try {52unsigned i;53Windows::Foundation::WwwFormUrlDecoder^ query = args->Uri->QueryParsed;5455for (i = 0; i < query->Size; i++)56{57IWwwFormUrlDecoderEntry^ arg = query->GetAt(i);5859if (arg->Name == "cmd")60{61auto command = FromPlatformString(arg->Value);62DEBUG_LOG(Log::FileSystem, "Launch command %s", command.c_str());6364std::regex rgx("\"(.+[^\\/]+)\"");65std::smatch match;6667if (std::regex_search(command, match, rgx)) {68try69{70launchPath = match[1];71}72catch (...) {73launchPath = match[0];74}75DEBUG_LOG(Log::FileSystem, "Launch target %s", launchPath.c_str());76}77}78else if (arg->Name == "launchOnExit") {79launchOnExit = FromPlatformString(arg->Value);80DEBUG_LOG(Log::FileSystem, "On exit URI %s", launchOnExit.c_str());81}82}83}84catch (...) {8586}87storageFile = nullptr;88}8990void Start() {91if (IsValid()) {92concurrency::create_task([&] {93SetState(true);94std::string path = GetFilePath();95// Delay to be able to launch on startup too96std::this_thread::sleep_for(std::chrono::milliseconds(100));97System_PostUIMessage(UIMessage::REQUEST_GAME_BOOT, path);98});99}100}101102bool IsHandled() {103return handled;104}105void SetState(bool fileHandled) {106handled = fileHandled;107}108109bool IsValid() {110return storageFile != nullptr || !launchPath.empty();111}112113std::string GetFilePath() {114std::string path = launchPath;115if (storageFile != nullptr) {116path = FromPlatformString(storageFile->Path);117}118return path;119}120121void Close(bool callLaunchOnExit) {122storageFile = nullptr;123launchPath = std::string();124handled = false;125126if (!launchOnExit.empty()) {127if (callLaunchOnExit) {128DEBUG_LOG(Log::FileSystem, "Calling back %s", launchOnExit.c_str());129auto uri = ref new Windows::Foundation::Uri(ToPlatformString(launchOnExit));130Windows::System::Launcher::LaunchUriAsync(uri);131}132else {133DEBUG_LOG(Log::FileSystem, "Ignoring callback %s, due to callLaunchOnExit is false", launchOnExit.c_str());134}135}136launchOnExit = std::string();137}138139private:140IStorageFile^ storageFile;141std::string launchPath;142std::string launchOnExit;143bool handled = false;144};145#pragma endregion146147LaunchItem launchItemHandler;148void DetectLaunchItem(IActivatedEventArgs^ activateArgs, bool onlyActivate) {149if (activateArgs != nullptr) {150if (!launchItemHandler.IsHandled()) {151if (activateArgs->Kind == ActivationKind::File) {152FileActivatedEventArgs^ fileArgs = dynamic_cast<FileActivatedEventArgs^>(activateArgs);153launchItemHandler.Activate((StorageFile^)fileArgs->Files->GetAt(0));154}155else if (activateArgs->Kind == ActivationKind::Protocol)156{157ProtocolActivatedEventArgs^ protocolArgs = dynamic_cast<ProtocolActivatedEventArgs^>(activateArgs);158launchItemHandler.Activate(protocolArgs);159}160if (!onlyActivate) {161launchItemHandler.Start();162}163}164}165}166167std::string GetLaunchItemPath(IActivatedEventArgs^ activateArgs) {168DetectLaunchItem(activateArgs, true); // Just activate169if (launchItemHandler.IsValid()) {170// Expected that 'GetLaunchItemPath' called to handle startup item171// it should be marked as handled by default172launchItemHandler.SetState(true);173}174return launchItemHandler.GetFilePath();175}176177void CloseLaunchItem(bool launchOnExit) {178if (launchItemHandler.IsValid() && launchItemHandler.IsHandled()) {179launchItemHandler.Close(launchOnExit);180}181}182183184