2019-09-05 00:26:26 +08:00
|
|
|
#pragma once
|
|
|
|
#include "powertoys_events.h"
|
2019-11-12 18:48:14 +08:00
|
|
|
#include "system_menu_helper.h"
|
2019-09-05 00:26:26 +08:00
|
|
|
#include <interface/powertoy_module_interface.h>
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
class PowertoyModule;
|
|
|
|
|
|
|
|
struct PowertoyModuleDeleter {
|
2019-09-11 18:38:20 +08:00
|
|
|
void operator()(PowertoyModuleIface* module) const {
|
2019-09-05 00:26:26 +08:00
|
|
|
if (module) {
|
2019-11-12 18:48:14 +08:00
|
|
|
powertoys_events().unregister_system_menu_action(module);
|
2019-09-05 00:26:26 +08:00
|
|
|
powertoys_events().unregister_receiver(module);
|
|
|
|
module->destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PowertoyModuleDLLDeleter {
|
|
|
|
using pointer = HMODULE;
|
2019-09-11 18:38:20 +08:00
|
|
|
void operator()(HMODULE handle) const {
|
2019-09-05 00:26:26 +08:00
|
|
|
FreeLibrary(handle);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PowertoyModule {
|
|
|
|
public:
|
|
|
|
PowertoyModule(PowertoyModuleIface* module, HMODULE handle) : handle(handle), module(module) {
|
2019-10-22 14:11:23 +08:00
|
|
|
if (!module) {
|
2019-09-05 00:26:26 +08:00
|
|
|
throw std::runtime_error("Module not initialized");
|
2019-10-22 14:11:23 +08:00
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
name = module->get_name();
|
|
|
|
auto want_signals = module->get_events();
|
|
|
|
if (want_signals) {
|
|
|
|
for (; *want_signals; ++want_signals) {
|
|
|
|
powertoys_events().register_receiver(*want_signals, module);
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 18:48:14 +08:00
|
|
|
if (SystemMenuHelperInstace().HasCustomConfig(module)) {
|
|
|
|
powertoys_events().register_system_menu_action(module);
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
const std::wstring& get_name() const {
|
|
|
|
return name;
|
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
const std::wstring get_config() const {
|
|
|
|
std::wstring result;
|
|
|
|
int size = 0;
|
|
|
|
module->get_config(nullptr, &size);
|
|
|
|
wchar_t *buffer = new wchar_t[size];
|
|
|
|
if (module->get_config(buffer, &size)) {
|
|
|
|
result.assign(buffer);
|
|
|
|
}
|
|
|
|
delete[] buffer;
|
|
|
|
return result;
|
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
void set_config(const std::wstring& config) {
|
|
|
|
module->set_config(config.c_str());
|
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
void call_custom_action(const std::wstring& action) {
|
|
|
|
module->call_custom_action(action.c_str());
|
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
intptr_t signal_event(const std::wstring& signal_event, intptr_t data) {
|
|
|
|
return module->signal_event(signal_event.c_str(), data);
|
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
bool is_enabled() {
|
|
|
|
return module->is_enabled();
|
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
void enable() {
|
|
|
|
module->enable();
|
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
void disable() {
|
|
|
|
module->disable();
|
|
|
|
}
|
2019-10-22 14:11:23 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
private:
|
|
|
|
std::unique_ptr<HMODULE, PowertoyModuleDLLDeleter> handle;
|
|
|
|
std::unique_ptr<PowertoyModuleIface, PowertoyModuleDeleter> module;
|
|
|
|
std::wstring name;
|
|
|
|
};
|
|
|
|
|
|
|
|
PowertoyModule load_powertoy(const std::wstring& filename);
|
2019-10-22 14:11:23 +08:00
|
|
|
std::unordered_map<std::wstring, PowertoyModule>& modules();
|