Adding code to launch Espresso

This commit is contained in:
Den Delimarsky 2021-05-01 10:32:36 -07:00
parent a13a5fde84
commit 75ed9c7b12
No known key found for this signature in database
GPG Key ID: E1BE1355085F0BCF
4 changed files with 53 additions and 15 deletions

View File

@ -8,6 +8,7 @@ namespace PTSettingsHelper
{ {
constexpr inline const wchar_t* log_settings_filename = L"log_settings.json"; 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_module_save_folder_location(std::wstring_view powertoy_name);
std::wstring get_root_save_folder_location(); std::wstring get_root_save_folder_location();

View File

@ -152,8 +152,8 @@ namespace Espresso.Shell.Core
} }
catch (OperationCanceledException ex) catch (OperationCanceledException ex)
{ {
log.Debug($"Background thread termination. Message: {ex.Message}");
// Task was clearly cancelled. // Task was clearly cancelled.
log.Debug($"Background thread termination. Message: {ex.Message}");
return success; return success;
} }
} }

View File

@ -44,16 +44,6 @@ const static wchar_t* MODULE_DESC = L"<no description>";
// These are the properties shown in the Settings page. // These are the properties shown in the Settings page.
struct ModuleSettings 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; } g_settings;
@ -68,9 +58,48 @@ private:
// The PowerToy state. // The PowerToy state.
bool m_enabled = false; 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. // Load initial settings from the persisted values.
void init_settings(); 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: public:
// Constructor // Constructor
Espresso() Espresso()
@ -96,6 +125,7 @@ public:
return MODULE_NAME; return MODULE_NAME;
} }
//// Return array of the names of all events that this powertoy listens for, with //// 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 //// nullptr as the last element of the array. Nullptr can also be retured for empty
//// list. //// list.
@ -239,15 +269,23 @@ public:
} }
} }
// Enable the powertoy
virtual void enable() virtual void enable()
{ {
ResetEvent(send_telemetry_event);
ResetEvent(m_hInvokeEvent);
launch_process();
m_enabled = true; m_enabled = true;
} };
// Disable the powertoy
virtual void disable() virtual void disable()
{ {
if (m_enabled)
{
ResetEvent(send_telemetry_event);
ResetEvent(m_hInvokeEvent);
TerminateProcess(m_hProcess, 1);
}
m_enabled = false; m_enabled = false;
} }

View File

@ -1,4 +1,3 @@
#pragma once
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.h>