2020-03-05 15:52:59 -08:00
|
|
|
#include "pch.h"
|
|
|
|
|
#include <interface/powertoy_module_interface.h>
|
2020-12-15 15:16:09 +03:00
|
|
|
#include <common/SettingsAPI/settings_objects.h>
|
2021-04-26 22:01:38 +03:00
|
|
|
#include <common/utils/resources.h>
|
2020-08-24 15:10:50 -07:00
|
|
|
#include "Generated Files/resource.h"
|
2020-04-20 08:22:36 -07:00
|
|
|
#include <keyboardmanager/common/KeyboardManagerConstants.h>
|
2020-12-15 15:16:09 +03:00
|
|
|
#include <common/utils/winapi_error.h>
|
2021-04-26 22:01:38 +03:00
|
|
|
#include <keyboardmanager/dll/trace.h>
|
|
|
|
|
#include <shellapi.h>
|
|
|
|
|
#include <common/utils/logger_helper.h>
|
2020-03-05 15:52:59 -08:00
|
|
|
|
|
|
|
|
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implement the PowerToy Module Interface and all the required methods.
|
2020-03-30 11:05:29 -07:00
|
|
|
class KeyboardManager : public PowertoyModuleIface
|
2020-03-05 15:52:59 -08:00
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
// The PowerToy state.
|
|
|
|
|
bool m_enabled = false;
|
|
|
|
|
|
2020-03-30 11:05:29 -07:00
|
|
|
// The PowerToy name that will be shown in the settings.
|
|
|
|
|
const std::wstring app_name = GET_RESOURCE_STRING(IDS_KEYBOARDMANAGER);
|
|
|
|
|
|
2020-10-19 16:07:02 -07:00
|
|
|
//contains the non localized key of the powertoy
|
|
|
|
|
std::wstring app_key = KeyboardManagerConstants::ModuleName;
|
|
|
|
|
|
2021-04-26 22:01:38 +03:00
|
|
|
HANDLE m_hProcess = nullptr;
|
2020-03-05 15:52:59 -08:00
|
|
|
public:
|
|
|
|
|
// Constructor
|
2020-03-30 11:05:29 -07:00
|
|
|
KeyboardManager()
|
2020-03-05 15:52:59 -08:00
|
|
|
{
|
2021-04-26 22:01:38 +03:00
|
|
|
LoggerHelpers::init_logger(KeyboardManagerConstants::ModuleName, L"ModuleInterface", LogSettings::keyboardManagerLoggerName);
|
|
|
|
|
|
|
|
|
|
std::filesystem::path oldLogPath(PTSettingsHelper::get_module_save_folder_location(app_key));
|
|
|
|
|
oldLogPath.append("Logs");
|
|
|
|
|
LoggerHelpers::delete_old_log_folder(oldLogPath);
|
2020-03-05 15:52:59 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Destroy the powertoy and free memory
|
|
|
|
|
virtual void destroy() override
|
|
|
|
|
{
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 16:07:02 -07:00
|
|
|
// Return the localized display name of the powertoy
|
2020-03-05 15:52:59 -08:00
|
|
|
virtual const wchar_t* get_name() override
|
|
|
|
|
{
|
2020-03-30 11:05:29 -07:00
|
|
|
return app_name.c_str();
|
2020-03-05 15:52:59 -08:00
|
|
|
}
|
|
|
|
|
|
2020-10-19 16:07:02 -07:00
|
|
|
// Return the non localized key of the powertoy, this will be cached by the runner
|
|
|
|
|
virtual const wchar_t* get_key() override
|
|
|
|
|
{
|
|
|
|
|
return app_key.c_str();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 14:02:31 +01:00
|
|
|
// Return the configured status for the gpo policy for the module
|
|
|
|
|
virtual powertoys_gpo::gpo_rule_configured_t gpo_policy_enabled_configuration() override
|
|
|
|
|
{
|
|
|
|
|
return powertoys_gpo::getConfiguredKeyboardManagerEnabledValue();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-05 15:52:59 -08:00
|
|
|
// Return JSON with the configuration options.
|
|
|
|
|
virtual bool get_config(wchar_t* buffer, int* buffer_size) override
|
|
|
|
|
{
|
|
|
|
|
HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);
|
|
|
|
|
|
|
|
|
|
// Create a Settings object.
|
|
|
|
|
PowerToysSettings::Settings settings(hinstance, get_name());
|
2020-03-30 11:05:29 -07:00
|
|
|
settings.set_description(IDS_SETTINGS_DESCRIPTION);
|
2020-06-11 10:16:39 -07:00
|
|
|
settings.set_overview_link(L"https://aka.ms/PowerToysOverview_KeyboardManager");
|
2020-03-05 15:52:59 -08:00
|
|
|
|
|
|
|
|
return settings.serialize_to_buffer(buffer, buffer_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Signal from the Settings editor to call a custom action.
|
|
|
|
|
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 =
|
2020-10-19 16:07:02 -07:00
|
|
|
PowerToysSettings::PowerToyValues::from_json_string(config, get_key());
|
2020-03-05 15:52:59 -08:00
|
|
|
|
|
|
|
|
// If you don't need to do any custom processing of the settings, proceed
|
|
|
|
|
// to persists the values calling:
|
|
|
|
|
values.save_to_settings_file();
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception&)
|
|
|
|
|
{
|
|
|
|
|
// Improper JSON.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enable the powertoy
|
|
|
|
|
virtual void enable()
|
|
|
|
|
{
|
|
|
|
|
m_enabled = true;
|
2020-05-05 12:30:50 -07:00
|
|
|
// Log telemetry
|
|
|
|
|
Trace::EnableKeyboardManager(true);
|
2021-04-26 22:01:38 +03:00
|
|
|
|
|
|
|
|
unsigned long powertoys_pid = GetCurrentProcessId();
|
|
|
|
|
std::wstring executable_args = L"";
|
|
|
|
|
executable_args.append(std::to_wstring(powertoys_pid));
|
|
|
|
|
|
|
|
|
|
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
|
|
|
|
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
|
|
|
|
|
sei.lpFile = L"modules\\KeyboardManager\\KeyboardManagerEngine\\PowerToys.KeyboardManagerEngine.exe";
|
|
|
|
|
sei.nShow = SW_SHOWNORMAL;
|
|
|
|
|
sei.lpParameters = executable_args.data();
|
|
|
|
|
if (ShellExecuteExW(&sei) == false)
|
|
|
|
|
{
|
|
|
|
|
Logger::error(L"Failed to start keyboard manager engine");
|
|
|
|
|
auto message = get_last_error_message(GetLastError());
|
|
|
|
|
if (message.has_value())
|
|
|
|
|
{
|
|
|
|
|
Logger::error(message.value());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_hProcess = sei.hProcess;
|
2021-04-27 11:13:30 -07:00
|
|
|
if (m_hProcess)
|
|
|
|
|
{
|
|
|
|
|
SetPriorityClass(m_hProcess, REALTIME_PRIORITY_CLASS);
|
|
|
|
|
}
|
2021-04-26 22:01:38 +03:00
|
|
|
}
|
2020-03-05 15:52:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disable the powertoy
|
|
|
|
|
virtual void disable()
|
|
|
|
|
{
|
|
|
|
|
m_enabled = false;
|
2020-05-05 12:30:50 -07:00
|
|
|
// Log telemetry
|
|
|
|
|
Trace::EnableKeyboardManager(false);
|
2020-03-05 15:52:59 -08:00
|
|
|
|
2021-04-26 22:01:38 +03:00
|
|
|
if (m_hProcess)
|
2020-03-05 15:52:59 -08:00
|
|
|
{
|
2021-04-26 22:01:38 +03:00
|
|
|
TerminateProcess(m_hProcess, 0);
|
|
|
|
|
m_hProcess = nullptr;
|
2020-03-05 15:52:59 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 22:01:38 +03:00
|
|
|
// Returns if the powertoys is enabled
|
|
|
|
|
virtual bool is_enabled() override
|
2020-03-05 15:52:59 -08:00
|
|
|
{
|
2021-04-26 22:01:38 +03:00
|
|
|
return m_enabled;
|
2020-03-05 15:52:59 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
|
|
|
|
|
{
|
2020-03-30 11:05:29 -07:00
|
|
|
return new KeyboardManager();
|
2020-03-05 15:52:59 -08:00
|
|
|
}
|