From 4e804e59c73c82323fab1baa8f7307f519cfe379 Mon Sep 17 00:00:00 2001 From: Reece Date: Sat, 23 Oct 2021 19:42:05 +0100 Subject: [PATCH] [+] Rewrite win32 file/directory opener --- Include/Aurora/Processes/Open.hpp | 2 +- Source/Entrypoint.cpp | 2 ++ Source/Processes/Open.Win32.cpp | 58 +++++++++++++++++++++++++++++-- Source/Processes/Open.Win32.hpp | 7 ++++ Source/Processes/Processes.cpp | 9 +++++ Source/Processes/Processes.hpp | 1 + 6 files changed, 76 insertions(+), 3 deletions(-) diff --git a/Include/Aurora/Processes/Open.hpp b/Include/Aurora/Processes/Open.hpp index 0099c004..69d38dbb 100644 --- a/Include/Aurora/Processes/Open.hpp +++ b/Include/Aurora/Processes/Open.hpp @@ -11,7 +11,7 @@ namespace Aurora::Processes { /// opens a standard uri formatted string (eg, protocol://endpoint/path) AUKN_SYM void OpenUri(const AuString &uri); - + /// opens a file given a relative path or abs path AUKN_SYM void OpenFile(const AuString &file); } \ No newline at end of file diff --git a/Source/Entrypoint.cpp b/Source/Entrypoint.cpp index b5c38d47..f76dc3b1 100644 --- a/Source/Entrypoint.cpp +++ b/Source/Entrypoint.cpp @@ -5,6 +5,7 @@ Date: 2021-5-13 Author: Reece ***/ +#define _AURORA_RUNTIME_BUILD_API_INTERFACES #include #include #include "RuntimeInternal.hpp" @@ -51,6 +52,7 @@ static void Deinit() Aurora::RNG::Release(); Aurora::Async::ShutdownAsync(); Aurora::Console::Exit(); + Aurora::Processes::Deinit(); } namespace Aurora diff --git a/Source/Processes/Open.Win32.cpp b/Source/Processes/Open.Win32.cpp index c9a1f6ed..0c2531d3 100644 --- a/Source/Processes/Open.Win32.cpp +++ b/Source/Processes/Open.Win32.cpp @@ -12,15 +12,69 @@ #include #include +#include +#include "Objbase.h" +#include "shellapi.h" + namespace Aurora::Processes { + static AuList gOpenItems; + static AuThreadPrimitives::ConditionMutexUnique_t gCondMutex; + static AuThreadPrimitives::ConditionVariableUnique_t gCondVariable; + static AuThreading::Threads::ThreadUnique_t gOpenerThread; + + static void RunTasks() + { + AU_LOCK_GUARD(gCondMutex); + + while (!gOpenerThread->Exiting()) + { + for (const auto &open : gOpenItems) + { + ShellExecuteW(NULL, IO::FS::DirExists(open) ? L"explore" : NULL, Locale::ConvertFromUTF8(open).c_str(), NULL, NULL, SW_SHOWNORMAL); + } + gOpenItems.clear(); + gCondVariable->WaitForSignal(); + } + } + + void OpenerThread() + { + CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + RunTasks(); + CoUninitialize(); + } + + void InitWin32Opener() + { + gCondMutex = AuThreadPrimitives::ConditionMutexUnique(); + gCondVariable = AuThreadPrimitives::ConditionVariableUnique(AuUnsafeRaiiToShared(gCondMutex)); + gOpenerThread = AuThreading::Threads::ThreadUnique(AuThreading::Threads::ThreadInfo( + AuMakeShared(AuThreading::Threads::IThreadVectorsFunctional::OnEntry_t(std::bind(OpenerThread)), + AuThreading::Threads::IThreadVectorsFunctional::OnExit_t{}), + "COM ShellExecute Runner" + )); + gOpenerThread->Run(); + } + + void DeinitWin32Opener() + { + gOpenerThread->SendExitSignal(); + gCondVariable->Broadcast(); + gOpenerThread.reset(); + gCondVariable.reset(); + gCondMutex.reset(); + } + AUKN_SYM void OpenUri(const AuString &uri) { - ShellExecuteW(NULL, NULL, Locale::ConvertFromUTF8(uri).c_str(), NULL, NULL, 0); + AU_LOCK_GUARD(gCondMutex); + gOpenItems.push_back(uri); + gCondVariable->Broadcast(); } AUKN_SYM void OpenFile(const AuString &file) { - OpenUri(file); + OpenUri(IO::FS::NormalizePathRet(file)); } } \ No newline at end of file diff --git a/Source/Processes/Open.Win32.hpp b/Source/Processes/Open.Win32.hpp index 46f82c26..992f473f 100644 --- a/Source/Processes/Open.Win32.hpp +++ b/Source/Processes/Open.Win32.hpp @@ -5,3 +5,10 @@ Date: 2021-6-12 Author: Reece ***/ +#pragma once + +namespace Aurora::Processes +{ + void InitWin32Opener(); + void DeinitWin32Opener(); +} \ No newline at end of file diff --git a/Source/Processes/Processes.cpp b/Source/Processes/Processes.cpp index f50c11d8..5e6c78eb 100644 --- a/Source/Processes/Processes.cpp +++ b/Source/Processes/Processes.cpp @@ -8,6 +8,7 @@ #include #include "Processes.hpp" #include "Process.Win32.hpp" +#include "Open.Win32.hpp" namespace Aurora::Processes { @@ -15,6 +16,14 @@ namespace Aurora::Processes { #if defined (AURORA_PLATFORM_WIN32) InitWin32(); + InitWin32Opener(); + #endif + } + + void Deinit() + { + #if defined (AURORA_PLATFORM_WIN32) + DeinitWin32Opener(); #endif } } \ No newline at end of file diff --git a/Source/Processes/Processes.hpp b/Source/Processes/Processes.hpp index 81d80c18..ecc2c903 100644 --- a/Source/Processes/Processes.hpp +++ b/Source/Processes/Processes.hpp @@ -10,4 +10,5 @@ namespace Aurora::Processes { void Init(); + void Deinit(); } \ No newline at end of file