Add regex matching to reverted isInFocus() for Windows

Reverts part of a4de8c0c59
This commit is contained in:
Myned 2020-05-21 00:42:58 -04:00
parent de5b6f8eb4
commit 70894f9302
No known key found for this signature in database
GPG Key ID: 24318A323F309244

View File

@ -131,15 +131,27 @@ void GetProcessList(QStringList &processes) {
CloseHandle(procSnapshot);
}
bool isInFocus(const QString &exeToCheck)
bool isInFocus(const QString &executable)
{
string curWindow;
GetCurrentWindowTitle(curWindow);
// only checks if the current foreground window is from the same executable,
// may return true for any window from a program
HWND foregroundWindow = GetForegroundWindow();
DWORD processId = 0;
GetWindowThreadProcessId(foregroundWindow, &processId);
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (process == NULL) return false;
WCHAR executablePath[600];
GetModuleFileNameEx(process, 0, executablePath, 600);
CloseHandle(process);
QString file = QString::fromWCharArray(executablePath).split(QRegularExpression("(/|\\\\)")).back();
// True if executable switch equals current window
bool equals = (exeToCheck.toStdString() == curWindow);
bool equals = (executable == file);
// True if executable switch matches current window
bool matches = QString::fromStdString(curWindow).contains(QRegularExpression(exeToCheck));
bool matches = file.contains(QRegularExpression(executable));
return (equals || matches);
}