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-05-20 20:07:34 +08:00
|
|
|
#include "CentralizedHotkeys.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;
|
|
|
|
result.resize(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();
|
|
|
|
if (container.has_value())
|
|
|
|
{
|
|
|
|
auto hotkey = container.value();
|
|
|
|
auto modulePtr = pt_module.get();
|
|
|
|
auto action = [modulePtr](WORD modifiersMask, WORD vkCode) {
|
|
|
|
modulePtr->OnHotkeyEx();
|
|
|
|
};
|
|
|
|
|
|
|
|
CentralizedHotkeys::AddHotkeyAction({ hotkey.modifiersMask, hotkey.vkCode }, { pt_module->get_key(), action });
|
|
|
|
}
|
|
|
|
}
|