2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "powertoy_module.h"
|
2020-09-21 18:44:16 +08:00
|
|
|
#include "centralized_kb_hook.h"
|
2021-09-23 21:23:22 +08:00
|
|
|
#include "centralized_hotkeys.h"
|
2021-03-27 01:17:26 +08:00
|
|
|
#include <common/logger/logger.h>
|
2021-05-20 20:07:34 +08:00
|
|
|
#include <common/utils/winapi_error.h>
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2020-03-26 18:51:05 +08:00
|
|
|
std::map<std::wstring, PowertoyModule>& modules()
|
2019-12-27 00:26:11 +08:00
|
|
|
{
|
2020-03-26 18:51:05 +08:00
|
|
|
static std::map<std::wstring, PowertoyModule> modules;
|
2019-12-27 00:26:11 +08:00
|
|
|
return modules;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 18:01:33 +08:00
|
|
|
PowertoyModule load_powertoy(const std::wstring_view filename)
|
2019-12-27 00:26:11 +08:00
|
|
|
{
|
2020-06-22 18:01:33 +08:00
|
|
|
auto handle = winrt::check_pointer(LoadLibraryW(filename.data()));
|
2019-12-27 00:26:11 +08:00
|
|
|
auto create = reinterpret_cast<powertoy_create_func>(GetProcAddress(handle, "powertoy_create"));
|
|
|
|
if (!create)
|
|
|
|
{
|
|
|
|
FreeLibrary(handle);
|
|
|
|
winrt::throw_last_error();
|
|
|
|
}
|
2021-01-09 02:26:38 +08:00
|
|
|
auto pt_module = create();
|
|
|
|
if (!pt_module)
|
2019-12-27 00:26:11 +08:00
|
|
|
{
|
|
|
|
FreeLibrary(handle);
|
2020-09-18 21:18:01 +08:00
|
|
|
winrt::throw_hresult(winrt::hresult(E_POINTER));
|
2019-12-27 00:26:11 +08:00
|
|
|
}
|
2021-01-09 02:26:38 +08:00
|
|
|
return PowertoyModule(pt_module, handle);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2019-12-06 16:40:23 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
json::JsonObject PowertoyModule::json_config() const
|
|
|
|
{
|
|
|
|
int size = 0;
|
2021-01-09 02:26:38 +08:00
|
|
|
pt_module->get_config(nullptr, &size);
|
2019-12-27 00:26:11 +08:00
|
|
|
std::wstring result;
|
2022-10-26 00:33:23 +08:00
|
|
|
result.resize(static_cast<size_t>(size) - 1);
|
2021-01-09 02:26:38 +08:00
|
|
|
pt_module->get_config(result.data(), &size);
|
2019-12-27 00:26:11 +08:00
|
|
|
return json::JsonObject::Parse(result);
|
2019-12-06 16:40:23 +08:00
|
|
|
}
|
2020-03-13 17:55:15 +08:00
|
|
|
|
2021-01-09 02:26:38 +08:00
|
|
|
PowertoyModule::PowertoyModule(PowertoyModuleIface* pt_module, HMODULE handle) :
|
|
|
|
handle(handle), pt_module(pt_module)
|
2020-03-13 17:55:15 +08:00
|
|
|
{
|
2021-01-09 02:26:38 +08:00
|
|
|
if (!pt_module)
|
2020-03-13 17:55:15 +08:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Module not initialized");
|
|
|
|
}
|
2020-09-21 18:44:16 +08:00
|
|
|
|
|
|
|
update_hotkeys();
|
2021-05-20 20:07:34 +08:00
|
|
|
UpdateHotkeyEx();
|
2020-09-21 18:44:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PowertoyModule::update_hotkeys()
|
|
|
|
{
|
2021-01-09 02:26:38 +08:00
|
|
|
CentralizedKeyboardHook::ClearModuleHotkeys(pt_module->get_key());
|
2020-09-21 18:44:16 +08:00
|
|
|
|
2021-01-09 02:26:38 +08:00
|
|
|
size_t hotkeyCount = pt_module->get_hotkeys(nullptr, 0);
|
2020-09-21 18:44:16 +08:00
|
|
|
std::vector<PowertoyModuleIface::Hotkey> hotkeys(hotkeyCount);
|
2021-01-09 02:26:38 +08:00
|
|
|
pt_module->get_hotkeys(hotkeys.data(), hotkeyCount);
|
2020-09-21 18:44:16 +08:00
|
|
|
|
2021-01-09 02:26:38 +08:00
|
|
|
auto modulePtr = pt_module.get();
|
2020-09-21 18:44:16 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < hotkeyCount; i++)
|
|
|
|
{
|
2021-01-09 02:26:38 +08:00
|
|
|
CentralizedKeyboardHook::SetHotkeyAction(pt_module->get_key(), hotkeys[i], [modulePtr, i] {
|
2021-03-27 01:17:26 +08:00
|
|
|
Logger::trace(L"{} hotkey is invoked from Centralized keyboard hook", modulePtr->get_key());
|
2020-09-21 18:44:16 +08:00
|
|
|
return modulePtr->on_hotkey(i);
|
|
|
|
});
|
|
|
|
}
|
2020-03-13 17:55:15 +08:00
|
|
|
}
|
2021-05-20 20:07:34 +08:00
|
|
|
|
|
|
|
void PowertoyModule::UpdateHotkeyEx()
|
|
|
|
{
|
|
|
|
CentralizedHotkeys::UnregisterHotkeysForModule(pt_module->get_key());
|
|
|
|
auto container = pt_module->GetHotkeyEx();
|
2022-02-02 20:17:37 +08:00
|
|
|
if (container.has_value() && pt_module->is_enabled())
|
2021-05-20 20:07:34 +08:00
|
|
|
{
|
|
|
|
auto hotkey = container.value();
|
|
|
|
auto modulePtr = pt_module.get();
|
2022-11-09 22:41:14 +08:00
|
|
|
auto action = [modulePtr](WORD /*modifiersMask*/, WORD /*vkCode*/) {
|
2022-02-02 20:17:37 +08:00
|
|
|
Logger::trace(L"{} hotkey Ex is invoked from Centralized keyboard hook", modulePtr->get_key());
|
2021-05-20 20:07:34 +08:00
|
|
|
modulePtr->OnHotkeyEx();
|
|
|
|
};
|
|
|
|
|
|
|
|
CentralizedHotkeys::AddHotkeyAction({ hotkey.modifiersMask, hotkey.vkCode }, { pt_module->get_key(), action });
|
|
|
|
}
|
2021-09-23 21:23:22 +08: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 20:07:34 +08:00
|
|
|
}
|