Skip to content

Commit

Permalink
Betting logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflord committed Dec 4, 2023
1 parent 354a620 commit 43a4ed1
Showing 1 changed file with 100 additions and 85 deletions.
185 changes: 100 additions & 85 deletions src/runner/centralized_kb_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ namespace CentralizedKeyboardHook

void RefreshConfig()
{
// just do this lazy style, mark as dirty
getConfigInit = false;
runProgramSpecs.clear();
}
Expand All @@ -429,6 +430,7 @@ namespace CentralizedKeyboardHook
{
if (!getConfigInit)
{
Logger::trace(L"CKBH:SetupConfig running");
PowerToysSettings::PowerToyValues settings = PowerToysSettings::PowerToyValues::load_from_settings_file(KeyboardManagerConstants::ModuleName);
auto current_config = settings.get_string_value(KeyboardManagerConstants::ActiveConfigurationSettingName);

Expand Down Expand Up @@ -467,12 +469,14 @@ namespace CentralizedKeyboardHook
}
catch (...)
{
Logger::error(L"Improper Key Data JSON. Try the next remap.");
Logger::error(L"CKBH:Improper Key Data JSON. Try the next remap.");
}
}
}

getConfigInit = true;

Logger::trace(L"CKBH:SetupConfig done, found {} runPrograms.", runProgramSpecs.size());
}
}

Expand Down Expand Up @@ -553,123 +557,134 @@ namespace CentralizedKeyboardHook

if (runProgram)
{
Logger::trace(L"runProgram {}", runProgram);
auto fileNamePart = GetFileNameFromPath(runProgramSpec.path);

Logger::trace(L"CKBH:{}, trying to run {}", fileNamePart, runProgramSpec.path);
lastKeyInChord = 0;

if (runProgramSpec.path.compare(L"RefreshConfig") == 0)
DWORD targetPid = 0;

if (fileNamePart != L"explorer.exe" && fileNamePart != L"powershell.exe" && fileNamePart != L"cmd.exe")
{
RefreshConfig();
targetPid = GetProcessIdByName(fileNamePart);
}
else

if (targetPid != 0)
{
auto fileNamePart = GetFileNameFromPath(runProgramSpec.path);
DWORD targetPid = 0;
Logger::trace(L"CKBH:{}, already running, pid:{}", fileNamePart, targetPid);

if (fileNamePart != L"explorer.exe" && fileNamePart != L"powershell.exe" && fileNamePart != L"cmd.exe")
{
targetPid = GetProcessIdByName(fileNamePart);
}
// a good place to look for this...
// https://github.com/ritchielawrence/cmdow

if (targetPid != 0)
// try by main window.
HWND hwnd = FindMainWindow(targetPid);
if (hwnd != NULL)
{
// a good place to look for this...
// https://github.com/ritchielawrence/cmdow
Logger::trace(L"CKBH:{}, got hwnd from FindMainWindow", fileNamePart);

// try by main window.
HWND hwnd = FindMainWindow(targetPid);
if (hwnd != NULL)
if (hwnd == GetForegroundWindow())
{
if (hwnd == GetForegroundWindow())
Logger::trace(L"CKBH:{}, got GetForegroundWindow, doing SW_MINIMIZE", fileNamePart);
ShowWindow(hwnd, SW_MINIMIZE);
return;
}
else
{
Logger::trace(L"CKBH:{}, no GetForegroundWindow, doing SW_RESTORE", fileNamePart);
ShowWindow(hwnd, SW_RESTORE);

if (!SetForegroundWindow(hwnd))
{
ShowWindow(hwnd, SW_MINIMIZE);
return;
auto errorCode = GetLastError();
Logger::warn(L"CKBH:{}, failed to SetForegroundWindow, {}", fileNamePart, errorCode);
}
else
{
ShowWindow(hwnd, SW_RESTORE);

if (!SetForegroundWindow(hwnd))
{
auto errorCode = GetLastError();
Logger::error(L"SetForegroundWindow", errorCode, L"PowerToys - Interop");
}
else
{
return;
}
Logger::trace(L"CKBH:{}, success on SetForegroundWindow", fileNamePart);
return;
}
}
}

// try by console.
hwnd = FindWindow(nullptr, nullptr);
if (AttachConsole(targetPid))
// try by console.

hwnd = FindWindow(nullptr, nullptr);
if (AttachConsole(targetPid))
{
Logger::trace(L"CKBH:{}, success on AttachConsole", fileNamePart);

// Get the console window handle
hwnd = GetConsoleWindow();
auto showByConsoleSuccess = false;
if (hwnd != NULL)
{
// Get the console window handle
hwnd = GetConsoleWindow();
auto showByConsoleSuccess = false;
if (hwnd != NULL)
{
ShowWindow(hwnd, SW_RESTORE);
Logger::trace(L"CKBH:{}, success on GetConsoleWindow, doing SW_RESTORE", fileNamePart);

if (!SetForegroundWindow(hwnd))
{
auto errorCode = GetLastError();
Logger::error(L"SetForegroundWindow", errorCode, L"PowerToys - Interop");
}
else
{
showByConsoleSuccess = true;
}
}
ShowWindow(hwnd, SW_RESTORE);

// Detach from the console
FreeConsole();
if (showByConsoleSuccess)
if (!SetForegroundWindow(hwnd))
{
return;
auto errorCode = GetLastError();
Logger::warn(L"CKBH:{}, failed to SetForegroundWindow, {}", fileNamePart, errorCode);
}
else
{
Logger::trace(L"CKBH:{}, success on SetForegroundWindow", fileNamePart);
showByConsoleSuccess = true;
}
}

// try to just show them all (if they have a title)!.
hwnd = FindWindow(nullptr, nullptr);
// Detach from the console
FreeConsole();
if (showByConsoleSuccess)
{
return;
}
}

// try to just show them all (if they have a title)!.
hwnd = FindWindow(nullptr, nullptr);

while (hwnd)
while (hwnd)
{
DWORD pidForHwnd;
GetWindowThreadProcessId(hwnd, &pidForHwnd);
if (targetPid == pidForHwnd)
{
DWORD pidForHwnd;
GetWindowThreadProcessId(hwnd, &pidForHwnd);
if (targetPid == pidForHwnd)
Logger::trace(L"CKBH:{}:{}, FindWindow (show all mode)", fileNamePart, targetPid);

int length = GetWindowTextLength(hwnd);

if (length > 0)
{
int length = GetWindowTextLength(hwnd);
ShowWindow(hwnd, SW_RESTORE);

if (length > 0)
// hwnd is the window handle with targetPid
if (!SetForegroundWindow(hwnd))
{
ShowWindow(hwnd, SW_RESTORE);

// hwnd is the window handle with targetPid
if (!SetForegroundWindow(hwnd))
{
auto errorCode = GetLastError();
Logger::error(L"SetForegroundWindow", errorCode, L"PowerToys - Interop");
}
auto errorCode = GetLastError();
Logger::warn(L"CKBH:{}, failed to SetForegroundWindow, {}", fileNamePart, errorCode);
}
}
hwnd = FindWindowEx(NULL, hwnd, NULL, NULL);
}
hwnd = FindWindowEx(NULL, hwnd, NULL, NULL);
}
else
}
else
{
std::wstring executable_and_args = fmt::format(L"\"{}\" {}", runProgramSpec.path, runProgramSpec.args);
STARTUPINFO startup_info = { sizeof(startup_info) };
PROCESS_INFORMATION process_info = { 0 };

auto currentDir = runProgramSpec.dir.c_str();
if (runProgramSpec.dir == L"")
{
std::wstring executable_and_args = fmt::format(L"\"{}\" {}", runProgramSpec.path, runProgramSpec.args);
STARTUPINFO startup_info = { sizeof(startup_info) };
PROCESS_INFORMATION process_info = { 0 };
currentDir = nullptr;
}

auto currentDir = runProgramSpec.dir.c_str();
if (runProgramSpec.dir == L"")
{
currentDir = nullptr;
}
Logger::trace(L"CKBH:{}, CreateProcessW starting {}", fileNamePart, executable_and_args);

CreateProcessW(nullptr, executable_and_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 Expand Up @@ -698,7 +713,7 @@ namespace CentralizedKeyboardHook
handle_data& data = *reinterpret_cast<handle_data*>(lParam);
unsigned long process_id = 0;
GetWindowThreadProcessId(handle, &process_id);

if (data.process_id != process_id || !(GetWindow(handle, GW_OWNER) == static_cast<HWND>(0) && IsWindowVisible(handle)))
{
return TRUE;
Expand Down Expand Up @@ -750,7 +765,7 @@ namespace CentralizedKeyboardHook

void SetHotkeyAction(const std::wstring& moduleName, const Hotkey& hotkey, std::function<bool()>&& action) noexcept
{
Logger::trace(L"Register hotkey action for {}", moduleName);
Logger::trace(L"CKBH:Register hotkey action for {}", moduleName);
std::unique_lock lock{ mutex };
hotkeyDescriptors.insert({ .hotkey = hotkey, .moduleName = moduleName, .action = std::move(action) });
}
Expand All @@ -768,7 +783,7 @@ namespace CentralizedKeyboardHook

void ClearModuleHotkeys(const std::wstring& moduleName) noexcept
{
Logger::trace(L"UnRegister hotkey action for {}", moduleName);
Logger::trace(L"CKBH:UnRegister hotkey action for {}", moduleName);
{
std::unique_lock lock{ mutex };
auto it = hotkeyDescriptors.begin();
Expand Down

1 comment on commit 43a4ed1

@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 or 📝 job summary for details.

Unrecognized words (19)
ARGSFORPROGRAM
Chrods
CKBH
cmdow
direcotry
myar
myarg
mycool
pathis
PATHTOPROGRAM
PROCESSENTRY
ritchielawrence
SNAPPROCESS
splitw
STARTINDIRFORPROGRAM
textboxes
tlhelp
Toolhelp
wcsicmp
Previously acknowledged words that are now absent CHT constexpr DEU hashcode HEB JPN nodiscard pcs qps roundf RUS RValue SVE wifi 🫥
To accept these unrecognized words as correct and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the [email protected]:jefflord/PowerToys.git repository
on the launch-apps-3350 branch (ℹ️ how do I use this?):

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

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

Dictionary Entries Covers Uniquely
cspell:r/src/r.txt 543 1 1
cspell:cpp/src/people.txt 23 1
cspell:cpp/src/ecosystem.txt 51 1

Consider adding them (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

      with:
        extra_dictionaries:
          cspell:r/src/r.txt
          cspell:cpp/src/people.txt
          cspell:cpp/src/ecosystem.txt

To stop checking additional dictionaries, add (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

check_extra_dictionaries: ''
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.