Skip to content

Commit

Permalink
[MouseJump]Long lived background process (#28380)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
mikeclayton committed Oct 11, 2023
1 parent 602a3ff commit 93d80f5
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 73 deletions.
4 changes: 4 additions & 0 deletions src/common/interop/interop.cpp
Expand Up @@ -203,6 +203,10 @@ public
return gcnew String(CommonSharedConstants::SHOW_POWEROCR_SHARED_EVENT);
}

static String ^ MouseJumpShowPreviewEvent() {
return gcnew String(CommonSharedConstants::MOUSE_JUMP_SHOW_PREVIEW_EVENT);
}

static String ^ AwakeExitEvent() {
return gcnew String(CommonSharedConstants::AWAKE_EXIT_EVENT);
}
Expand Down
3 changes: 3 additions & 0 deletions src/common/interop/shared_constants.h
Expand Up @@ -50,6 +50,9 @@ namespace CommonSharedConstants
// Path to the event used by PowerOCR
const wchar_t SHOW_POWEROCR_SHARED_EVENT[] = L"Local\\PowerOCREvent-dc864e06-e1af-4ecc-9078-f98bee745e3a";

// Path to the events used by Mouse Jump
const wchar_t MOUSE_JUMP_SHOW_PREVIEW_EVENT[] = L"Local\\MouseJumpEvent-aa0be051-3396-4976-b7ba-1a9cc7d236a5";

// Path to the event used by RegistryPreview
const wchar_t REGISTRY_PREVIEW_TRIGGER_EVENT[] = L"Local\\RegistryPreviewEvent-4C559468-F75A-4E7F-BC4F-9C9688316687";

Expand Down
56 changes: 56 additions & 0 deletions src/modules/MouseUtils/MouseJump/MouseJump.vcxproj.filters
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Generated Files">
<UniqueIdentifier>{875a08c6-f610-4667-bd0f-80171ed96072}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Generated Files\resource.h">
<Filter>Generated Files</Filter>
</ClInclude>
<ClInclude Include="trace.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="trace.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="resource.h">
<Filter>Header Files</Filter>
</None>
<None Include="MouseJump.rc">
<Filter>Resource Files</Filter>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Generated Files\MouseJump.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
</Project>
101 changes: 53 additions & 48 deletions src/modules/MouseUtils/MouseJump/dllmain.cpp
@@ -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;
}

Expand Down Expand Up @@ -54,9 +54,17 @@ class MouseJump : public PowertoyModuleIface
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();
Expand Down Expand Up @@ -123,33 +131,44 @@ class MouseJump : public PowertoyModuleIface
}

// 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:
// Constructor
MouseJump()
{
LoggerHelpers::init_logger(MODULE_NAME, L"ModuleInterface", LogSettings::mouseJumpLoggerName);
m_hInvokeEvent = CreateDefaultEvent(CommonSharedConstants::MOUSE_JUMP_SHOW_PREVIEW_EVENT);
init_settings();
};

~MouseJump()
{
if (m_enabled)
{
terminate_process();
}
m_enabled = false;
}

// Destroy the powertoy and free memory
virtual void destroy() override
{
Logger::trace("MouseJump::destroy()");
delete this;
}

Expand Down Expand Up @@ -180,12 +199,14 @@ class MouseJump : public PowertoyModuleIface
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
{
}

Expand All @@ -199,7 +220,6 @@ class MouseJump : public PowertoyModuleIface
PowerToysSettings::PowerToyValues::from_json_string(config, get_key());

parse_hotkey(values);

values.save_to_settings_file();
}
catch (std::exception&)
Expand All @@ -211,40 +231,39 @@ class MouseJump : public PowertoyModuleIface
// Enable the powertoy
virtual void enable()
{
Logger::trace("MouseJump::enable()");
ResetEvent(m_hInvokeEvent);
launch_process();
m_enabled = true;
Trace::EnableJumpTool(true);
}

// 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;
}

Expand All @@ -268,6 +287,12 @@ class MouseJump : public PowertoyModuleIface
}
}

// 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
{
Expand All @@ -276,26 +301,6 @@ class MouseJump : public PowertoyModuleIface

};

// 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();
Expand Down

0 comments on commit 93d80f5

Please sign in to comment.