2019-09-04 18:26:26 +02:00
|
|
|
#include "pch.h"
|
|
|
|
|
#include "powertoy_module.h"
|
2020-09-21 12:44:16 +02:00
|
|
|
#include "centralized_kb_hook.h"
|
2021-09-23 14:23:22 +01:00
|
|
|
#include "centralized_hotkeys.h"
|
2021-03-26 19:17:26 +02:00
|
|
|
#include <common/logger/logger.h>
|
2021-05-20 15:07:34 +03:00
|
|
|
#include <common/utils/winapi_error.h>
|
2019-09-04 18:26:26 +02:00
|
|
|
|
2020-03-26 11:51:05 +01:00
|
|
|
std::map<std::wstring, PowertoyModule>& modules()
|
2019-12-26 17:26:11 +01:00
|
|
|
{
|
2020-03-26 11:51:05 +01:00
|
|
|
static std::map<std::wstring, PowertoyModule> modules;
|
2019-12-26 17:26:11 +01:00
|
|
|
return modules;
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-22 13:01:33 +03:00
|
|
|
PowertoyModule load_powertoy(const std::wstring_view filename)
|
2019-12-26 17:26:11 +01:00
|
|
|
{
|
2020-06-22 13:01:33 +03:00
|
|
|
auto handle = winrt::check_pointer(LoadLibraryW(filename.data()));
|
2019-12-26 17:26:11 +01:00
|
|
|
auto create = reinterpret_cast<powertoy_create_func>(GetProcAddress(handle, "powertoy_create"));
|
|
|
|
|
if (!create)
|
|
|
|
|
{
|
|
|
|
|
FreeLibrary(handle);
|
|
|
|
|
winrt::throw_last_error();
|
|
|
|
|
}
|
2021-01-08 19:26:38 +01:00
|
|
|
auto pt_module = create();
|
|
|
|
|
if (!pt_module)
|
2019-12-26 17:26:11 +01:00
|
|
|
{
|
|
|
|
|
FreeLibrary(handle);
|
2020-09-18 15:18:01 +02:00
|
|
|
winrt::throw_hresult(winrt::hresult(E_POINTER));
|
2019-12-26 17:26:11 +01:00
|
|
|
}
|
2021-01-08 19:26:38 +01:00
|
|
|
return PowertoyModule(pt_module, handle);
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|
2019-12-06 11:40:23 +03:00
|
|
|
|
2019-12-26 17:26:11 +01:00
|
|
|
json::JsonObject PowertoyModule::json_config() const
|
|
|
|
|
{
|
|
|
|
|
int size = 0;
|
2021-01-08 19:26:38 +01:00
|
|
|
pt_module->get_config(nullptr, &size);
|
2019-12-26 17:26:11 +01:00
|
|
|
std::wstring result;
|
2022-10-25 17:33:23 +01:00
|
|
|
result.resize(static_cast<size_t>(size) - 1);
|
2021-01-08 19:26:38 +01:00
|
|
|
pt_module->get_config(result.data(), &size);
|
2019-12-26 17:26:11 +01:00
|
|
|
return json::JsonObject::Parse(result);
|
2019-12-06 11:40:23 +03:00
|
|
|
}
|
2020-03-13 12:55:15 +03:00
|
|
|
|
2021-01-08 19:26:38 +01:00
|
|
|
PowertoyModule::PowertoyModule(PowertoyModuleIface* pt_module, HMODULE handle) :
|
2025-08-20 09:31:52 +08:00
|
|
|
handle(handle), pt_module(pt_module), hkmng(HotkeyConflictDetector::HotkeyConflictManager::GetInstance())
|
2020-03-13 12:55:15 +03:00
|
|
|
{
|
2021-01-08 19:26:38 +01:00
|
|
|
if (!pt_module)
|
2020-03-13 12:55:15 +03:00
|
|
|
{
|
|
|
|
|
throw std::runtime_error("Module not initialized");
|
|
|
|
|
}
|
2020-09-21 12:44:16 +02:00
|
|
|
|
2025-08-20 09:31:52 +08:00
|
|
|
remove_hotkey_records();
|
2020-09-21 12:44:16 +02:00
|
|
|
update_hotkeys();
|
2021-05-20 15:07:34 +03:00
|
|
|
UpdateHotkeyEx();
|
2020-09-21 12:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowertoyModule::update_hotkeys()
|
|
|
|
|
{
|
2021-01-08 19:26:38 +01:00
|
|
|
CentralizedKeyboardHook::ClearModuleHotkeys(pt_module->get_key());
|
2020-09-21 12:44:16 +02:00
|
|
|
|
2021-01-08 19:26:38 +01:00
|
|
|
size_t hotkeyCount = pt_module->get_hotkeys(nullptr, 0);
|
2020-09-21 12:44:16 +02:00
|
|
|
std::vector<PowertoyModuleIface::Hotkey> hotkeys(hotkeyCount);
|
2021-01-08 19:26:38 +01:00
|
|
|
pt_module->get_hotkeys(hotkeys.data(), hotkeyCount);
|
2020-09-21 12:44:16 +02:00
|
|
|
|
2021-01-08 19:26:38 +01:00
|
|
|
auto modulePtr = pt_module.get();
|
2020-09-21 12:44:16 +02:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < hotkeyCount; i++)
|
|
|
|
|
{
|
2025-08-20 09:31:52 +08:00
|
|
|
if (hotkeys[i].isShown)
|
|
|
|
|
{
|
|
|
|
|
hkmng.AddHotkey(hotkeys[i], pt_module->get_key(), static_cast<int>(i), pt_module->is_enabled());
|
|
|
|
|
|
|
|
|
|
CentralizedKeyboardHook::SetHotkeyAction(pt_module->get_key(), hotkeys[i], [modulePtr, i] {
|
|
|
|
|
Logger::trace(L"{} hotkey is invoked from Centralized keyboard hook", modulePtr->get_key());
|
|
|
|
|
return modulePtr->on_hotkey(i);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-09-21 12:44:16 +02:00
|
|
|
}
|
2020-03-13 12:55:15 +03:00
|
|
|
}
|
2021-05-20 15:07:34 +03:00
|
|
|
|
|
|
|
|
void PowertoyModule::UpdateHotkeyEx()
|
|
|
|
|
{
|
|
|
|
|
CentralizedHotkeys::UnregisterHotkeysForModule(pt_module->get_key());
|
2025-08-20 09:31:52 +08:00
|
|
|
|
2021-05-20 15:07:34 +03:00
|
|
|
auto container = pt_module->GetHotkeyEx();
|
2022-02-02 12:17:37 +00:00
|
|
|
if (container.has_value() && pt_module->is_enabled())
|
2021-05-20 15:07:34 +03:00
|
|
|
{
|
2025-08-20 09:31:52 +08:00
|
|
|
hkmng.RemoveHotkeyByModule(pt_module->get_key());
|
|
|
|
|
|
2021-05-20 15:07:34 +03:00
|
|
|
auto hotkey = container.value();
|
|
|
|
|
auto modulePtr = pt_module.get();
|
2022-11-09 14:41:14 +00:00
|
|
|
auto action = [modulePtr](WORD /*modifiersMask*/, WORD /*vkCode*/) {
|
2022-02-02 12:17:37 +00:00
|
|
|
Logger::trace(L"{} hotkey Ex is invoked from Centralized keyboard hook", modulePtr->get_key());
|
2021-05-20 15:07:34 +03:00
|
|
|
modulePtr->OnHotkeyEx();
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-20 09:31:52 +08:00
|
|
|
HotkeyConflictDetector::Hotkey _hotkey = HotkeyConflictDetector::ShortcutToHotkey({ hotkey.modifiersMask, hotkey.vkCode });
|
|
|
|
|
hkmng.AddHotkey(_hotkey, pt_module->get_key(), 0, pt_module->is_enabled()); // This is the only one activation hotkey, so we use "0" as the name.
|
|
|
|
|
|
2021-05-20 15:07:34 +03:00
|
|
|
CentralizedHotkeys::AddHotkeyAction({ hotkey.modifiersMask, hotkey.vkCode }, { pt_module->get_key(), action });
|
|
|
|
|
}
|
2021-09-23 14:23:22 +01:00
|
|
|
|
|
|
|
|
// HACK:
|
|
|
|
|
// Just for enabling the shortcut guide legacy behavior of pressing the Windows Key.
|
|
|
|
|
// This is not the sort of behavior we'd like to have generalized on other modules.
|
|
|
|
|
// But this was a way to bring back the long windows key behavior that the community wanted back while maintaining the separate process.
|
|
|
|
|
if (pt_module->keep_track_of_pressed_win_key())
|
|
|
|
|
{
|
|
|
|
|
auto modulePtr = pt_module.get();
|
|
|
|
|
auto action = [modulePtr] {
|
|
|
|
|
modulePtr->OnHotkeyEx();
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
CentralizedKeyboardHook::AddPressedKeyAction(pt_module->get_key(), VK_LWIN, pt_module->milliseconds_win_key_must_be_pressed(), action);
|
|
|
|
|
CentralizedKeyboardHook::AddPressedKeyAction(pt_module->get_key(), VK_RWIN, pt_module->milliseconds_win_key_must_be_pressed(), action);
|
|
|
|
|
}
|
2021-05-20 15:07:34 +03:00
|
|
|
}
|