From 75ed9c7b12118ba65267991cceaaf1ccc842362f Mon Sep 17 00:00:00 2001 From: Den Delimarsky <1389609+dend@users.noreply.github.com> Date: Sat, 1 May 2021 10:32:36 -0700 Subject: [PATCH] Adding code to launch Espresso --- src/common/SettingsAPI/settings_helpers.h | 1 + .../espresso/Espresso.Shell/Core/APIHelper.cs | 2 +- src/modules/espresso/Espresso/dllmain.cpp | 64 +++++++++++++++---- src/modules/espresso/Espresso/pch.h | 1 - 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/common/SettingsAPI/settings_helpers.h b/src/common/SettingsAPI/settings_helpers.h index 1174032fe2..cd8f39432b 100644 --- a/src/common/SettingsAPI/settings_helpers.h +++ b/src/common/SettingsAPI/settings_helpers.h @@ -8,6 +8,7 @@ namespace PTSettingsHelper { constexpr inline const wchar_t* log_settings_filename = L"log_settings.json"; + std::wstring get_module_save_file_location(std::wstring_view powertoy_key); std::wstring get_module_save_folder_location(std::wstring_view powertoy_name); std::wstring get_root_save_folder_location(); diff --git a/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs b/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs index 0e2af67c9d..5d947f359c 100644 --- a/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs +++ b/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs @@ -152,8 +152,8 @@ namespace Espresso.Shell.Core } catch (OperationCanceledException ex) { - log.Debug($"Background thread termination. Message: {ex.Message}"); // Task was clearly cancelled. + log.Debug($"Background thread termination. Message: {ex.Message}"); return success; } } diff --git a/src/modules/espresso/Espresso/dllmain.cpp b/src/modules/espresso/Espresso/dllmain.cpp index 863c337105..f285318e84 100644 --- a/src/modules/espresso/Espresso/dllmain.cpp +++ b/src/modules/espresso/Espresso/dllmain.cpp @@ -44,16 +44,6 @@ const static wchar_t* MODULE_DESC = L""; // These are the properties shown in the Settings page. struct ModuleSettings { - // Add the PowerToy module properties with default values. - // Currently available types: - // - int - // - bool - // - string - - //bool bool_prop = true; - //int int_prop = 10; - //std::wstring string_prop = L"The quick brown fox jumps over the lazy dog"; - //std::wstring color_prop = L"#1212FF"; } g_settings; @@ -68,9 +58,48 @@ private: // The PowerToy state. bool m_enabled = false; + HANDLE m_hProcess; + + HANDLE send_telemetry_event; + + // Handle to event used to invoke Espresso + HANDLE m_hInvokeEvent; + // Load initial settings from the persisted values. void init_settings(); + bool is_process_running() + { + return WaitForSingleObject(m_hProcess, 0) == WAIT_TIMEOUT; + } + + void launch_process() + { + Logger::trace(L"Launching Espresso process"); + unsigned long powertoys_pid = GetCurrentProcessId(); + + // Get the configuration file that will be passed to the process. + std::wstring espresso_settings_location = PTSettingsHelper::get_module_save_file_location(MODULE_NAME); + + std::wstring executable_args = L"--config " + espresso_settings_location; + executable_args.append(std::to_wstring(powertoys_pid)); + + SHELLEXECUTEINFOW sei{ sizeof(sei) }; + sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI }; + sei.lpFile = L"modules\\Espresso\\Espresso.exe"; + sei.nShow = SW_SHOWNORMAL; + sei.lpParameters = executable_args.data(); + if (!ShellExecuteExW(&sei)) + { + DWORD error = GetLastError(); + std::wstring message = L"Espresso failed to start with error = "; + message += std::to_wstring(error); + Logger::error(message); + } + + m_hProcess = sei.hProcess; + } + public: // Constructor Espresso() @@ -96,6 +125,7 @@ public: return MODULE_NAME; } + //// Return array of the names of all events that this powertoy listens for, with //// nullptr as the last element of the array. Nullptr can also be retured for empty //// list. @@ -239,15 +269,23 @@ public: } } - // Enable the powertoy virtual void enable() { + ResetEvent(send_telemetry_event); + ResetEvent(m_hInvokeEvent); + launch_process(); m_enabled = true; - } + }; - // Disable the powertoy virtual void disable() { + if (m_enabled) + { + ResetEvent(send_telemetry_event); + ResetEvent(m_hInvokeEvent); + TerminateProcess(m_hProcess, 1); + } + m_enabled = false; } diff --git a/src/modules/espresso/Espresso/pch.h b/src/modules/espresso/Espresso/pch.h index 46fc911b44..eddac0fdc1 100644 --- a/src/modules/espresso/Espresso/pch.h +++ b/src/modules/espresso/Espresso/pch.h @@ -1,4 +1,3 @@ -#pragma once #define WIN32_LEAN_AND_MEAN #include #include