2022-01-24 21:29:16 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include <interface/powertoy_module_interface.h>
|
|
|
|
#include <common/SettingsAPI/settings_objects.h>
|
|
|
|
#include "trace.h"
|
2022-01-26 22:01:24 +08:00
|
|
|
#include "InclusiveCrosshairs.h"
|
2022-01-24 21:29:16 +08:00
|
|
|
#include "common/utils/color.h"
|
|
|
|
|
|
|
|
// Non-Localizable strings
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
const wchar_t JSON_KEY_PROPERTIES[] = L"properties";
|
|
|
|
const wchar_t JSON_KEY_VALUE[] = L"value";
|
|
|
|
const wchar_t JSON_KEY_ACTIVATION_SHORTCUT[] = L"activation_shortcut";
|
2022-01-26 22:01:24 +08:00
|
|
|
const wchar_t JSON_KEY_CROSSHAIRS_COLOR[] = L"crosshairs_color";
|
|
|
|
const wchar_t JSON_KEY_CROSSHAIRS_OPACITY[] = L"crosshairs_opacity";
|
|
|
|
const wchar_t JSON_KEY_CROSSHAIRS_RADIUS[] = L"crosshairs_radius";
|
|
|
|
const wchar_t JSON_KEY_CROSSHAIRS_THICKNESS[] = L"crosshairs_thickness";
|
|
|
|
const wchar_t JSON_KEY_CROSSHAIRS_BORDER_COLOR[] = L"crosshairs_border_color";
|
|
|
|
const wchar_t JSON_KEY_CROSSHAIRS_BORDER_SIZE[] = L"crosshairs_border_size";
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" IMAGE_DOS_HEADER __ImageBase;
|
|
|
|
|
|
|
|
HMODULE m_hModule;
|
|
|
|
|
|
|
|
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:
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
Trace::UnregisterProvider();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The PowerToy name that will be shown in the settings.
|
2022-01-26 22:01:24 +08:00
|
|
|
const static wchar_t* MODULE_NAME = L"MousePointerCrosshairs";
|
2022-01-24 21:29:16 +08:00
|
|
|
// Add a description that will we shown in the module settings page.
|
|
|
|
const static wchar_t* MODULE_DESC = L"<no description>";
|
|
|
|
|
|
|
|
// Implement the PowerToy Module Interface and all the required methods.
|
2022-01-26 22:01:24 +08:00
|
|
|
class MousePointerCrosshairs : public PowertoyModuleIface
|
2022-01-24 21:29:16 +08:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
// The PowerToy state.
|
|
|
|
bool m_enabled = false;
|
|
|
|
|
|
|
|
// Hotkey to invoke the module
|
|
|
|
HotkeyEx m_hotkey;
|
|
|
|
|
2022-01-26 22:01:24 +08:00
|
|
|
// Mouse Pointer Crosshairs specific settings
|
|
|
|
InclusiveCrosshairsSettings m_inclusiveCrosshairsSettings;
|
2022-01-24 21:29:16 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Constructor
|
2022-01-26 22:01:24 +08:00
|
|
|
MousePointerCrosshairs()
|
2022-01-24 21:29:16 +08:00
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
LoggerHelpers::init_logger(MODULE_NAME, L"ModuleInterface", LogSettings::mousePointerCrosshairsLoggerName);
|
2022-01-24 21:29:16 +08:00
|
|
|
init_settings();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Destroy the powertoy and free memory
|
|
|
|
virtual void destroy() override
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the localized display name of the powertoy
|
|
|
|
virtual const wchar_t* get_name() override
|
|
|
|
{
|
|
|
|
return MODULE_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the non localized key of the powertoy, this will be cached by the runner
|
|
|
|
virtual const wchar_t* get_key() override
|
|
|
|
{
|
|
|
|
return MODULE_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return JSON with the configuration options.
|
|
|
|
virtual bool get_config(wchar_t* buffer, int* buffer_size) override
|
|
|
|
{
|
|
|
|
HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);
|
|
|
|
|
|
|
|
PowerToysSettings::Settings settings(hinstance, get_name());
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by the runner to pass the updated settings values as a serialized JSON.
|
|
|
|
virtual void set_config(const wchar_t* config) override
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse the input JSON string.
|
|
|
|
PowerToysSettings::PowerToyValues values =
|
|
|
|
PowerToysSettings::PowerToyValues::from_json_string(config, get_key());
|
|
|
|
|
|
|
|
parse_settings(values);
|
|
|
|
|
2022-01-26 22:01:24 +08:00
|
|
|
InclusiveCrosshairsApplySettings(m_inclusiveCrosshairsSettings);
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::error("Invalid json when trying to parse Mouse Pointer Crosshairs settings json.");
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable the powertoy
|
|
|
|
virtual void enable()
|
|
|
|
{
|
|
|
|
m_enabled = true;
|
2022-01-26 22:01:24 +08:00
|
|
|
Trace::EnableMousePointerCrosshairs(true);
|
|
|
|
std::thread([=]() { InclusiveCrosshairsMain(m_hModule, m_inclusiveCrosshairsSettings); }).detach();
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disable the powertoy
|
|
|
|
virtual void disable()
|
|
|
|
{
|
|
|
|
m_enabled = false;
|
2022-01-26 22:01:24 +08:00
|
|
|
Trace::EnableMousePointerCrosshairs(false);
|
|
|
|
InclusiveCrosshairsDisable();
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::optional<HotkeyEx> GetHotkeyEx() override
|
|
|
|
{
|
|
|
|
return m_hotkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnHotkeyEx() override
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
InclusiveCrosshairsSwitch();
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
// Load the settings file.
|
|
|
|
void init_settings()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load and parse the settings file for this PowerToy.
|
|
|
|
PowerToysSettings::PowerToyValues settings =
|
2022-01-26 22:01:24 +08:00
|
|
|
PowerToysSettings::PowerToyValues::load_from_settings_file(MousePointerCrosshairs::get_key());
|
2022-01-24 21:29:16 +08:00
|
|
|
parse_settings(settings);
|
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::error("Invalid json when trying to load the Mouse Pointer Crosshairs settings json from file.");
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_settings(PowerToysSettings::PowerToyValues& settings)
|
|
|
|
{
|
|
|
|
// TODO: refactor to use common/utils/json.h instead
|
|
|
|
auto settingsObject = settings.get_raw_json();
|
2022-01-26 22:01:24 +08:00
|
|
|
InclusiveCrosshairsSettings inclusiveCrosshairsSettings;
|
2022-01-24 21:29:16 +08:00
|
|
|
if (settingsObject.GetView().Size())
|
|
|
|
{
|
|
|
|
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 = hotkey.get_code();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::warn("Failed to initialize Mouse Pointer Crosshairs activation shortcut");
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse Opacity
|
2022-01-26 22:01:24 +08:00
|
|
|
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_CROSSHAIRS_OPACITY);
|
|
|
|
inclusiveCrosshairsSettings.crosshairsOpacity = (uint8_t)jsonPropertiesObject.GetNamedNumber(JSON_KEY_VALUE);
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Logger::warn("Failed to initialize Opacity from settings. Will use default value");
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
// Parse crosshairs color
|
|
|
|
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_CROSSHAIRS_COLOR);
|
|
|
|
auto crosshairsColor = (std::wstring)jsonPropertiesObject.GetNamedString(JSON_KEY_VALUE);
|
2022-01-24 21:29:16 +08:00
|
|
|
uint8_t r, g, b;
|
2022-01-26 22:01:24 +08:00
|
|
|
if (!checkValidRGB(crosshairsColor, &r, &g, &b))
|
2022-01-24 21:29:16 +08:00
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::error("Crosshairs color RGB value is invalid. Will use default value");
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
inclusiveCrosshairsSettings.crosshairsColor = winrt::Windows::UI::ColorHelper::FromArgb(255, r, g, b);
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::warn("Failed to initialize crosshairs color from settings. Will use default value");
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse Radius
|
2022-01-26 22:01:24 +08:00
|
|
|
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_CROSSHAIRS_RADIUS);
|
|
|
|
inclusiveCrosshairsSettings.crosshairsRadius = (UINT)jsonPropertiesObject.GetNamedNumber(JSON_KEY_VALUE);
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Logger::warn("Failed to initialize Radius from settings. Will use default value");
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse Thickness
|
2022-01-26 22:01:24 +08:00
|
|
|
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_CROSSHAIRS_THICKNESS);
|
|
|
|
inclusiveCrosshairsSettings.crosshairsThickness = (UINT)jsonPropertiesObject.GetNamedNumber(JSON_KEY_VALUE);
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Logger::warn("Failed to initialize Thickness from settings. Will use default value");
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
// Parse crosshairs border color
|
|
|
|
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_CROSSHAIRS_BORDER_COLOR);
|
|
|
|
auto crosshairsBorderColor = (std::wstring)jsonPropertiesObject.GetNamedString(JSON_KEY_VALUE);
|
2022-01-24 21:29:16 +08:00
|
|
|
uint8_t r, g, b;
|
2022-01-26 22:01:24 +08:00
|
|
|
if (!checkValidRGB(crosshairsBorderColor, &r, &g, &b))
|
2022-01-24 21:29:16 +08:00
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::error("Crosshairs border color RGB value is invalid. Will use default value");
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
inclusiveCrosshairsSettings.crosshairsBorderColor = winrt::Windows::UI::ColorHelper::FromArgb(255, r, g, b);
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::warn("Failed to initialize crosshairs border color from settings. Will use default value");
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse border size
|
2022-01-26 22:01:24 +08:00
|
|
|
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_CROSSHAIRS_BORDER_SIZE);
|
|
|
|
inclusiveCrosshairsSettings.crosshairsBorderSize = (UINT)jsonPropertiesObject.GetNamedNumber(JSON_KEY_VALUE);
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Logger::warn("Failed to initialize border color from settings. Will use default value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::info("Mouse Pointer Crosshairs settings are empty");
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
if (!m_hotkey.modifiersMask)
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
Logger::info("Mouse Pointer Crosshairs is going to use default shortcut");
|
2022-01-24 21:29:16 +08:00
|
|
|
m_hotkey.modifiersMask = MOD_CONTROL | MOD_ALT;
|
|
|
|
m_hotkey.vkCode = 0x50; // P key
|
|
|
|
}
|
2022-01-26 22:01:24 +08:00
|
|
|
m_inclusiveCrosshairsSettings = inclusiveCrosshairsSettings;
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
|
|
|
|
{
|
2022-01-26 22:01:24 +08:00
|
|
|
return new MousePointerCrosshairs();
|
2022-01-24 21:29:16 +08:00
|
|
|
}
|