[MouseJump]Long lived background process (#28380)

* [MouseJump] Long lived background exe

* [MouseJump] Long lived background exe

* [MouseJump] Long lived background exe

* [MouseJump] Long lived background exe

* [MouseJump] Close long lived background exe when parent runner exits

* [MouseJump] Close long lived background exe when parent runner exits

* [MouseJump] long lived background exe - fixing build

* [MouseJump] - add FileSystemWatcher for config (#26703)

* Fix telemetry event
This commit is contained in:
Michael Clayton
2023-10-11 15:58:19 +01:00
committed by GitHub
parent 602a3ff090
commit 93d80f542c
10 changed files with 359 additions and 73 deletions

View File

@@ -1,32 +1,32 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include <interface/powertoy_module_interface.h>
//#include <interface/lowlevel_keyboard_event_data.h>
//#include <interface/win_hook_event_data.h>
#include <common/SettingsAPI/settings_objects.h>
#include "trace.h"
#include <common/utils/winapi_error.h>
#include <common/SettingsAPI/settings_objects.h>
#include <common/utils/resources.h>
#include <common/interop/shared_constants.h>
#include <common/utils/logger_helper.h>
#include <common/utils/winapi_error.h>
extern "C" IMAGE_DOS_HEADER __ImageBase;
HMODULE m_hModule;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
BOOL APIENTRY DllMain(HMODULE /*hModule*/,
DWORD ul_reason_for_call,
LPVOID /*lpReserved*/)
{
m_hModule = hModule;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
Trace::RegisterProvider();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
Trace::UnregisterProvider();
break;
}
return TRUE;
}
@@ -54,9 +54,17 @@ private:
bool m_enabled = false;
// Hotkey to invoke the module
Hotkey m_hotkey;
HANDLE m_hProcess;
// Time to wait for process to close after sending WM_CLOSE signal
static const int MAX_WAIT_MILLISEC = 10000;
Hotkey m_hotkey;
// Handle to event used to invoke PowerOCR
HANDLE m_hInvokeEvent;
void parse_hotkey(PowerToysSettings::PowerToyValues& settings)
{
auto settingsObject = settings.get_raw_json();
@@ -123,11 +131,21 @@ private:
}
// Load initial settings from the persisted values.
void init_settings();
void terminate_process()
void init_settings()
{
TerminateProcess(m_hProcess, 1);
try
{
// Load and parse the settings file for this PowerToy.
PowerToysSettings::PowerToyValues settings =
PowerToysSettings::PowerToyValues::load_from_settings_file(get_key());
parse_hotkey(settings);
}
catch (std::exception&)
{
Logger::warn(L"An exception occurred while loading the settings file");
// Error while loading from the settings file. Let default values stay as they are.
}
}
public:
@@ -135,6 +153,7 @@ public:
MouseJump()
{
LoggerHelpers::init_logger(MODULE_NAME, L"ModuleInterface", LogSettings::mouseJumpLoggerName);
m_hInvokeEvent = CreateDefaultEvent(CommonSharedConstants::MOUSE_JUMP_SHOW_PREVIEW_EVENT);
init_settings();
};
@@ -142,7 +161,6 @@ public:
{
if (m_enabled)
{
terminate_process();
}
m_enabled = false;
}
@@ -150,6 +168,7 @@ public:
// Destroy the powertoy and free memory
virtual void destroy() override
{
Logger::trace("MouseJump::destroy()");
delete this;
}
@@ -180,12 +199,14 @@ public:
PowerToysSettings::Settings settings(hinstance, get_name());
settings.set_description(MODULE_DESC);
settings.set_overview_link(L"https://aka.ms/PowerToysOverview_MouseUtilities/#mouse-jump");
return settings.serialize_to_buffer(buffer, buffer_size);
}
// Signal from the Settings editor to call a custom action.
// This can be used to spawn more complex editors.
virtual void call_custom_action(const wchar_t* action) override
virtual void call_custom_action(const wchar_t* /*action*/) override
{
}
@@ -199,7 +220,6 @@ public:
PowerToysSettings::PowerToyValues::from_json_string(config, get_key());
parse_hotkey(values);
values.save_to_settings_file();
}
catch (std::exception&)
@@ -211,6 +231,9 @@ public:
// Enable the powertoy
virtual void enable()
{
Logger::trace("MouseJump::enable()");
ResetEvent(m_hInvokeEvent);
launch_process();
m_enabled = true;
Trace::EnableJumpTool(true);
}
@@ -218,33 +241,29 @@ public:
// Disable the powertoy
virtual void disable()
{
Logger::trace("MouseJump::disable()");
if (m_enabled)
{
terminate_process();
ResetEvent(m_hInvokeEvent);
TerminateProcess(m_hProcess, 1);
}
m_enabled = false;
Trace::EnableJumpTool(false);
}
// Returns if the powertoys is enabled
virtual bool is_enabled() override
{
return m_enabled;
}
virtual bool on_hotkey(size_t /*hotkeyId*/) override
{
if (m_enabled)
{
Logger::trace(L"MouseJump hotkey pressed");
Trace::InvokeJumpTool();
if (is_process_running())
if (!is_process_running())
{
terminate_process();
launch_process();
}
launch_process();
SetEvent(m_hInvokeEvent);
return true;
}
@@ -268,6 +287,12 @@ public:
}
}
// Returns if the powertoys is enabled
virtual bool is_enabled() override
{
return m_enabled;
}
// Returns whether the PowerToys should be enabled by default
virtual bool is_enabled_by_default() const override
{
@@ -276,26 +301,6 @@ public:
};
// Load the settings file.
void MouseJump::init_settings()
{
try
{
// Load and parse the settings file for this PowerToy.
PowerToysSettings::PowerToyValues settings =
PowerToysSettings::PowerToyValues::load_from_settings_file(MouseJump::get_name());
parse_hotkey(settings);
}
catch (std::exception&)
{
Logger::warn(L"An exception occurred while loading the settings file");
// Error while loading from the settings file. Let default values stay as they are.
}
}
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
{
return new MouseJump();