[FindMyMouse]Support different activation modes (#28096)

* multiple activation modes for find my mouse

* use custom shortcut
This commit is contained in:
Davide Giacometti
2023-09-04 17:58:37 +02:00
committed by GitHub
parent 752c298ef3
commit 83f0625427
13 changed files with 222 additions and 36 deletions

View File

@@ -2,6 +2,7 @@
//
#include "pch.h"
#include "FindMyMouse.h"
#include "WinHookEventIDs.h"
#include "trace.h"
#include "common/utils/game_mode.h"
#include "common/utils/process_path.h"
@@ -246,6 +247,18 @@ LRESULT SuperSonar<D>::BaseWndProc(UINT message, WPARAM wParam, LPARAM lParam) n
return HTTRANSPARENT;
}
if (message == WM_PRIV_SHORTCUT)
{
if (m_sonarStart == NoSonar)
{
StartSonar();
}
else
{
StopSonar();
}
}
return DefWindowProc(m_hwnd, message, wParam, lParam);
}
@@ -291,26 +304,27 @@ void SuperSonar<D>::OnSonarInput(WPARAM flags, HRAWINPUT hInput)
template<typename D>
void SuperSonar<D>::OnSonarKeyboardInput(RAWINPUT const& input)
{
if ( m_activationMethod != FindMyMouseActivationMethod::DoubleControlKey || input.data.keyboard.VKey != VK_CONTROL)
// Don't stop the sonar when the shortcut is released
if (m_activationMethod == FindMyMouseActivationMethod::Shortcut && (input.data.keyboard.Flags & RI_KEY_BREAK) != 0)
{
return;
}
if ((m_activationMethod != FindMyMouseActivationMethod::DoubleRightControlKey && m_activationMethod != FindMyMouseActivationMethod::DoubleLeftControlKey)
|| input.data.keyboard.VKey != VK_CONTROL)
{
StopSonar();
return;
}
bool pressed = (input.data.keyboard.Flags & RI_KEY_BREAK) == 0;
bool rightCtrl = (input.data.keyboard.Flags & RI_KEY_E0) != 0;
// Deal with rightCtrl first.
if (rightCtrl)
bool leftCtrlPressed = (input.data.keyboard.Flags & RI_KEY_E0) == 0;
bool rightCtrlPressed = (input.data.keyboard.Flags & RI_KEY_E0) != 0;
if ((m_activationMethod == FindMyMouseActivationMethod::DoubleRightControlKey && !rightCtrlPressed)
|| (m_activationMethod == FindMyMouseActivationMethod::DoubleLeftControlKey && !leftCtrlPressed))
{
/*
* SuperSonar originally exited when pressing right control after pressing left control twice.
* We take care of exiting FindMyMouse through module disabling in PowerToys settings instead.
if (m_sonarState == SonarState::ControlUp2)
{
Terminate();
}
*/
StopSonar();
return;
}
@@ -644,6 +658,11 @@ struct CompositionSpotlight : SuperSonar<CompositionSpotlight>
}
}
HWND GetHwnd() noexcept
{
return m_hwnd;
}
private:
bool OnCompositionCreate()
try
@@ -1057,6 +1076,8 @@ int FindMyMouseMain(HINSTANCE hinst, const FindMyMouseSettings& settings)
m_sonar = &sonar;
Logger::info("Initialized the sonar instance.");
InitializeWinhookEventIds();
MSG msg;
// Main message loop:
@@ -1072,4 +1093,14 @@ int FindMyMouseMain(HINSTANCE hinst, const FindMyMouseSettings& settings)
return (int)msg.wParam;
}
HWND GetSonarHwnd() noexcept
{
if (m_sonar != nullptr)
{
return m_sonar->GetHwnd();
}
return nullptr;
}
#pragma endregion Super_Sonar_API

View File

@@ -3,9 +3,11 @@
enum struct FindMyMouseActivationMethod : int
{
DoubleControlKey = 0,
ShakeMouse = 1,
EnumElements = 2, // number of elements in the enum, not counting this
DoubleLeftControlKey = 0,
DoubleRightControlKey = 1,
ShakeMouse = 2,
Shortcut = 3,
EnumElements = 4, // number of elements in the enum, not counting this
};
constexpr bool FIND_MY_MOUSE_DEFAULT_DO_NOT_ACTIVATE_ON_GAME_MODE = true;
@@ -15,7 +17,7 @@ constexpr int FIND_MY_MOUSE_DEFAULT_OVERLAY_OPACITY = 50;
constexpr int FIND_MY_MOUSE_DEFAULT_SPOTLIGHT_RADIUS = 100;
constexpr int FIND_MY_MOUSE_DEFAULT_ANIMATION_DURATION_MS = 500;
constexpr int FIND_MY_MOUSE_DEFAULT_SPOTLIGHT_INITIAL_ZOOM = 9;
constexpr FindMyMouseActivationMethod FIND_MY_MOUSE_DEFAULT_ACTIVATION_METHOD = FindMyMouseActivationMethod::DoubleControlKey;
constexpr FindMyMouseActivationMethod FIND_MY_MOUSE_DEFAULT_ACTIVATION_METHOD = FindMyMouseActivationMethod::DoubleLeftControlKey;
constexpr int FIND_MY_MOUSE_DEFAULT_SHAKE_MINIMUM_DISTANCE = 1000;
struct FindMyMouseSettings
@@ -36,3 +38,4 @@ int FindMyMouseMain(HINSTANCE hinst, const FindMyMouseSettings& settings);
void FindMyMouseDisable();
bool FindMyMouseIsEnabled();
void FindMyMouseApplySettings(const FindMyMouseSettings& settings);
HWND GetSonarHwnd() noexcept;

View File

@@ -93,6 +93,7 @@
<ClInclude Include="pch.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="trace.h" />
<ClInclude Include="WinHookEventIDs.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
@@ -101,6 +102,7 @@
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="trace.cpp" />
<ClCompile Include="WinHookEventIDs.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\common\logger\logger.vcxproj">

View File

@@ -30,6 +30,9 @@
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="WinHookEventIDs.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
@@ -44,6 +47,9 @@
<ClInclude Include="resource.h">
<Filter>Resource Files</Filter>
</ClInclude>
<ClInclude Include="WinHookEventIDs.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="FindMyMouse.rc">

View File

@@ -0,0 +1,14 @@
#include "pch.h"
#include "WinHookEventIDs.h"
UINT WM_PRIV_SHORTCUT;
std::once_flag init_flag;
void InitializeWinhookEventIds()
{
std::call_once(init_flag, [&] {
WM_PRIV_SHORTCUT = RegisterWindowMessage(L"{1365FFC7-A44E-4171-9692-A3EEF378AE60}");
});
}

View File

@@ -0,0 +1,5 @@
#pragma once
extern UINT WM_PRIV_SHORTCUT; // Shortcut is pressed
void InitializeWinhookEventIds();

View File

@@ -3,6 +3,7 @@
#include <common/SettingsAPI/settings_objects.h>
#include "trace.h"
#include "FindMyMouse.h"
#include "WinHookEventIDs.h"
#include <thread>
#include <common/utils/logger_helper.h>
#include <common/utils/color.h>
@@ -22,6 +23,7 @@ namespace
const wchar_t JSON_KEY_SPOTLIGHT_INITIAL_ZOOM[] = L"spotlight_initial_zoom";
const wchar_t JSON_KEY_EXCLUDED_APPS[] = L"excluded_apps";
const wchar_t JSON_KEY_SHAKING_MINIMUM_DISTANCE[] = L"shaking_minimum_distance";
const wchar_t JSON_KEY_ACTIVATION_SHORTCUT[] = L"activation_shortcut";
}
extern "C" IMAGE_DOS_HEADER __ImageBase;
@@ -58,6 +60,9 @@ private:
// The PowerToy state.
bool m_enabled = false;
// Hotkey to invoke the module
HotkeyEx m_hotkey;
// Find My Mouse specific settings
FindMyMouseSettings m_findMyMouseSettings;
@@ -157,6 +162,27 @@ public:
{
return m_enabled;
}
virtual std::optional<HotkeyEx> GetHotkeyEx() override
{
Logger::trace("GetHotkeyEx()");
if (m_findMyMouseSettings.activationMethod == FindMyMouseActivationMethod::Shortcut)
{
return m_hotkey;
}
return std::nullopt;
}
virtual void OnHotkeyEx() override
{
Logger::trace("OnHotkeyEx()");
HWND hwnd = GetSonarHwnd();
if (hwnd != nullptr)
{
PostMessageW(hwnd, WM_PRIV_SHORTCUT, NULL, NULL);
}
}
};
// Load the settings file.
@@ -188,7 +214,15 @@ void FindMyMouse::parse_settings(PowerToysSettings::PowerToyValues& settings)
int value = static_cast<int>(jsonPropertiesObject.GetNamedNumber(JSON_KEY_VALUE));
if (value < static_cast<int>(FindMyMouseActivationMethod::EnumElements) && value >= 0)
{
findMyMouseSettings.activationMethod = static_cast<FindMyMouseActivationMethod>(value);
std::wstring version = (std::wstring)settingsObject.GetNamedString(L"version");
if (version == L"1.0" && value == 1)
{
findMyMouseSettings.activationMethod = FindMyMouseActivationMethod::ShakeMouse;
}
else
{
findMyMouseSettings.activationMethod = static_cast<FindMyMouseActivationMethod>(value);
}
}
else
{
@@ -362,6 +396,46 @@ void FindMyMouse::parse_settings(PowerToysSettings::PowerToyValues& settings)
{
Logger::warn("Failed to initialize Shaking Minimum Distance from settings. Will use default value");
}
try
{
// Parse HotKey
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_ACTIVATION_SHORTCUT);
auto hotkey = PowerToysSettings::HotkeyObject::from_json(jsonPropertiesObject);
m_hotkey = HotkeyEx();
if (hotkey.win_pressed())
{
m_hotkey.modifiersMask |= MOD_WIN;
}
if (hotkey.ctrl_pressed())
{
m_hotkey.modifiersMask |= MOD_CONTROL;
}
if (hotkey.shift_pressed())
{
m_hotkey.modifiersMask |= MOD_SHIFT;
}
if (hotkey.alt_pressed())
{
m_hotkey.modifiersMask |= MOD_ALT;
}
m_hotkey.vkCode = static_cast<WORD>(hotkey.get_code());
}
catch (...)
{
Logger::warn("Failed to initialize Activation Shortcut from settings. Will use default value");
}
if (!m_hotkey.modifiersMask)
{
Logger::info("Using default Activation Shortcut");
m_hotkey.modifiersMask = MOD_SHIFT | MOD_WIN;
m_hotkey.vkCode = 0x46; // F key
}
}
else
{