Skip to content

Commit

Permalink
Added WatchKeyboardManagerConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflord committed Oct 30, 2023
1 parent 6a7f5da commit 4a55478
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/runner/centralized_kb_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,10 @@ namespace CentralizedKeyboardHook
}
else
{
std::wstring executable_path = runProgramSpec.path;
auto fileNamePart = GetFileNameFromPath(executable_path);
auto fileNamePart = GetFileNameFromPath(runProgramSpec.path);
DWORD targetPid = 0;

if (fileNamePart != L"explorer.exe")
if (fileNamePart != L"explorer.exe" && fileNamePart != L"powershell.exe" && fileNamePart != L"cmd.exe")
{
targetPid = GetProcessIdByName(fileNamePart);
}
Expand Down Expand Up @@ -476,7 +475,7 @@ namespace CentralizedKeyboardHook
}
else
{
std::wstring executable_args = fmt::format(L"\"{}\" \"{}\"", runProgramSpec.args, runProgramSpec.args);
std::wstring executable_and_args = fmt::format(L"\"{}\" {}", runProgramSpec.path, runProgramSpec.args);
STARTUPINFO startup_info = { sizeof(startup_info) };
PROCESS_INFORMATION process_info = { 0 };

Expand All @@ -486,7 +485,7 @@ namespace CentralizedKeyboardHook
currentDir = nullptr;
}

CreateProcessW(executable_path.c_str(), executable_args.data(), nullptr, nullptr, FALSE, 0, nullptr, currentDir, &startup_info, &process_info);
CreateProcessW(nullptr, executable_and_args.data(), nullptr, nullptr, FALSE, 0, nullptr, currentDir, &startup_info, &process_info);
}
}
}
Expand Down
74 changes: 74 additions & 0 deletions src/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <common/utils/window.h>
#include <common/version/version.h>
#include <common/utils/string_utils.h>
#include <modules/keyboardmanager/common/KeyboardManagerConstants.h>

// disabling warning 4458 - declaration of 'identifier' hides class member
// to avoid warnings from GDI files - can't add winRT directory to external code
Expand Down Expand Up @@ -89,6 +90,73 @@ void open_menu_from_another_instance(std::optional<std::string> settings_window)
PostMessageW(hwnd_main, WM_COMMAND, ID_SETTINGS_MENU_COMMAND, msg);
}

//HANDLE PowertoyModule::hWatchDirectory = nullptr;

DWORD WINAPI WatchKeyboardManagerConfigs(LPVOID lpParameter)
{
// don't need but can't not use
if (lpParameter)
{
}

//HANDLE PowertoyModule::hWatchDirectory = nullptr;

//// make sure we don't have more than one
//if (PowertoyModule::hWatchDirectory != nullptr)
//{
// return 1;
//}

auto directoryPath = PTSettingsHelper::get_module_save_folder_location(KeyboardManagerConstants::ModuleName);

auto hWatchDirectory = CreateFile(
directoryPath.c_str(),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);

if (hWatchDirectory == INVALID_HANDLE_VALUE)
{
Logger::trace(L"Error: Unable to open directory {}", directoryPath);
return 1;
}

DWORD dwBufferSize = 4096;
BYTE* pbBuffer = new BYTE[dwBufferSize];
DWORD bytesReturned;

while (true)
{
// ReadDirectoryChangesW is fired twice per write and in this case it does not matter since we're just doing a reset of data, no real work.
if (ReadDirectoryChangesW(
hWatchDirectory,
pbBuffer,
sizeof(pbBuffer),
FALSE,
FILE_NOTIFY_CHANGE_LAST_WRITE,
&bytesReturned,
NULL,
NULL))
{
// we don't really care what changed, just refresh
CentralizedKeyboardHook::RefreshConfig();
continue;
}
else
{
Logger::trace(L"Error: Unable to read directory changes.");
break;
}
}

CloseHandle(hWatchDirectory);

return 0;
}

int runner(bool isProcessElevated, bool openSettings, std::string settingsWindow, bool openOobe, bool openScoobe)
{
Logger::info("Runner is starting. Elevated={} openOobe={} openScoobe={}", isProcessElevated, openOobe, openScoobe);
Expand Down Expand Up @@ -210,7 +278,13 @@ int runner(bool isProcessElevated, bool openSettings, std::string settingsWindow
}

settings_telemetry::init();

// start WatchKeyboardManagerConfigs
CreateThread(NULL, 0, WatchKeyboardManagerConfigs, 0, 0, NULL);

result = run_message_loop();


}
catch (std::runtime_error& err)
{
Expand Down
4 changes: 1 addition & 3 deletions src/runner/powertoy_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
#include <common/SettingsAPI/settings_helpers.h>
#include <modules/keyboardmanager/common/Shortcut.h>
#include <modules/keyboardmanager/common/RemapShortcut.h>
//#include <modules/keyboardmanager/common/RunProgramSpec.h>
//#include <modules/keyboardmanager/common/RunProgramSpec2.h>
//#include "modules/keyboardmanager/common/RunProgramSpec2.h"
#include <modules/keyboardmanager/common/KeyboardManagerConstants.h>
#include <common/interop/shared_constants.h>
#include <thread>

std::map<std::wstring, PowertoyModule>& modules()
{
Expand Down
4 changes: 4 additions & 0 deletions src/runner/powertoy_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ class PowertoyModule
void add_run_program_shortcuts();
void UpdateHotkeyEx();

static HANDLE PowertoyModule::hWatchDirectory;

private:
std::unique_ptr<HMODULE, PowertoyModuleDLLDeleter> handle;
std::unique_ptr<PowertoyModuleIface, PowertoyModuleDeleter> pt_module;


};

PowertoyModule load_powertoy(const std::wstring_view filename);
Expand Down

1 comment on commit 4a55478

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log for details.

Unrecognized words (18)
APPTORUN
APPTORUNARGS
APPTORUNSTARTINDIR
cmdow
EDITRUNPROGRAMS
EDITRUNPROGRAMSHORTCUTS
PROCESSENTRY
PROGRAMNAME
Programtool
ritchielawrence
RUNPROGRAMSHORTCUTS
RUNPROGRAMSPEC
shorcut
SNAPPROCESS
splitw
tlhelp
Toolhelp
wcsicmp
Previously acknowledged words that are now absent administra Changemove RSAT systemroot sysvol :arrow_right:
To accept ✔️ these unrecognized words as correct and remove the previously acknowledged and now absent words, run the following commands

... in a clone of the git@github.com:jefflord/PowerToys.git repository
on the run-program-shortcuts-3350 branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.21/apply.pl' |
perl - 'https://github.com/jefflord/PowerToys/actions/runs/6692199155/attempts/1'
Available 📚 dictionaries could cover words not in the 📘 dictionary

This includes both expected items (2293) from .github/actions/spell-check/expect.txt and unrecognized words (18)

Dictionary Entries Covers
cspell:win32/src/win32.txt 53509 140
cspell:cpp/src/cpp.txt 30216 132
cspell:python/src/python/python-lib.txt 3873 29
cspell:php/php.txt 2597 20
cspell:node/node.txt 1768 15
cspell:typescript/typescript.txt 1211 13
cspell:java/java.txt 7642 12
cspell:python/src/python/python.txt 453 10
cspell:aws/aws.txt 218 9
cspell:python/src/common/extra.txt 741 8

Consider adding them using (in .github/workflows/spelling2.yml):

      with:
        extra_dictionaries:
          cspell:win32/src/win32.txt
          cspell:cpp/src/cpp.txt
          cspell:python/src/python/python-lib.txt
          cspell:php/php.txt
          cspell:node/node.txt
          cspell:typescript/typescript.txt
          cspell:java/java.txt
          cspell:python/src/python/python.txt
          cspell:aws/aws.txt
          cspell:python/src/common/extra.txt

To stop checking additional dictionaries, add:

      with:
        check_extra_dictionaries: ''
Errors (1)

See the 📜action log for details.

❌ Errors Count
❌ check-file-path 1

See ❌ Event descriptions for more information.

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.