From 8d21e8901883b461007018158f063acd8da228ca Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Tue, 7 Apr 2026 22:39:24 +0200 Subject: [PATCH] Cleanup / style changes --- lib/advanced-scene-switcher.cpp | 5 +- lib/legacy/switch-executable.cpp | 3 +- lib/legacy/switch-window.cpp | 3 +- lib/linux/advanced-scene-switcher-nix.cpp | 54 ++++++--------------- lib/osx/advanced-scene-switcher-osx.mm | 44 ++++++----------- lib/platform-funcs.hpp | 9 ++-- lib/utils/first-run-wizard.cpp | 4 +- lib/utils/selection-helpers.cpp | 8 ++-- lib/win/advanced-scene-switcher-win.cpp | 58 +++++++++-------------- plugins/base/macro-action-window.cpp | 3 +- plugins/base/macro-condition-process.cpp | 11 ++--- plugins/base/macro-condition-window.cpp | 3 +- 12 files changed, 71 insertions(+), 134 deletions(-) diff --git a/lib/advanced-scene-switcher.cpp b/lib/advanced-scene-switcher.cpp index 52a93925..0e863c99 100644 --- a/lib/advanced-scene-switcher.cpp +++ b/lib/advanced-scene-switcher.cpp @@ -322,8 +322,7 @@ void SwitcherData::SetPreconditions() { // Window title lastTitle = currentTitle; - std::string title; - GetCurrentWindowTitle(title); + auto title = GetCurrentWindowTitle(); for (auto &window : ignoreWindowsSwitches) { bool equals = (title == window); bool matches = false; @@ -343,7 +342,7 @@ void SwitcherData::SetPreconditions() currentTitle = title; // Process name - GetForegroundProcessName(currentForegroundProcess); + currentForegroundProcess = GetForegroundProcessName(); // Macro InvalidateMacroTempVarValues(); diff --git a/lib/legacy/switch-executable.cpp b/lib/legacy/switch-executable.cpp index 8125e930..1eba3b0e 100644 --- a/lib/legacy/switch-executable.cpp +++ b/lib/legacy/switch-executable.cpp @@ -92,12 +92,11 @@ bool SwitcherData::checkExeSwitch(OBSWeakSource &scene, } std::string title = switcher->currentTitle; - QStringList runningProcesses; bool ignored = false; bool match = false; // Check for match - GetProcessList(runningProcesses); + const auto runningProcesses = GetProcessList(); for (ExecutableSwitch &s : executableSwitches) { if (!s.initialized()) { continue; diff --git a/lib/legacy/switch-window.cpp b/lib/legacy/switch-window.cpp index e0fd821f..8827ef70 100644 --- a/lib/legacy/switch-window.cpp +++ b/lib/legacy/switch-window.cpp @@ -230,8 +230,7 @@ bool SwitcherData::checkWindowTitleSwitch(OBSWeakSource &scene, std::string currentWindowTitle = switcher->currentTitle; bool match = false; - std::vector windowList; - GetWindowList(windowList); + const auto windowList = GetWindowList(); for (WindowSwitch &s : windowSwitches) { if (!s.initialized()) { diff --git a/lib/linux/advanced-scene-switcher-nix.cpp b/lib/linux/advanced-scene-switcher-nix.cpp index 5466a474..665f3d31 100644 --- a/lib/linux/advanced-scene-switcher-nix.cpp +++ b/lib/linux/advanced-scene-switcher-nix.cpp @@ -236,9 +236,9 @@ std::string getWindowName(Window window) return windowTitle; } -void GetWindowList(std::vector &windows) +std::vector GetWindowList() { - windows.resize(0); + std::vector windows; for (auto window : getTopLevelWindows()) { auto name = getWindowName(window); if (name.empty()) { @@ -246,18 +246,7 @@ void GetWindowList(std::vector &windows) } windows.emplace_back(name); } -} - -void GetWindowList(QStringList &windows) -{ - windows.clear(); - for (auto window : getTopLevelWindows()) { - auto name = getWindowName(window); - if (name.empty()) { - continue; - } - windows << QString::fromStdString(name); - } + return windows; } int getActiveWindow(Window *&window) @@ -281,29 +270,24 @@ int getActiveWindow(Window *&window) &bytes, (uint8_t **)&window); } -void GetCurrentWindowTitle(std::string &title) +std::string GetCurrentWindowTitle() { if (KWin) { - title = FocusNotifier::getActiveWindowTitle(); - return; + return FocusNotifier::getActiveWindowTitle(); } Window *data = 0; if (getActiveWindow(data) != Success || !data) { - return; + return {}; } if (!data[0]) { XFree(data); - return; + return {}; } auto name = getWindowName(data[0]); XFree(data); - - if (name.empty()) { - return; - } - title = name; + return name; } bool windowStatesAreSet(const std::string &windowTitle, @@ -412,16 +396,17 @@ static void getProcessListProcps2(QStringList &processes) #endif } -void GetProcessList(QStringList &processes) +QStringList GetProcessList() { - processes.clear(); + QStringList processes; if (libprocpsSupported) { getProcessListProcps(processes); - return; + return processes; } if (libprocps2Supported) { getProcessListProcps2(processes); } + return processes; } long getForegroundProcessPid() @@ -472,18 +457,10 @@ std::string getProcNameFromPid(long pid) return name; } -void GetForegroundProcessName(QString &proc) +std::string GetForegroundProcessName() { - std::string temp; - GetForegroundProcessName(temp); - proc = QString::fromStdString(temp); -} - -void GetForegroundProcessName(std::string &proc) -{ - proc.resize(0); auto pid = getForegroundProcessPid(); - proc = getProcNameFromPid(pid); + return getProcNameFromPid(pid); } static std::string getProcessPathFromPid(long pid) @@ -556,8 +533,7 @@ QStringList GetProcessPathsFromName(const QString &name) bool IsInFocus(const QString &executable) { - std::string current; - GetForegroundProcessName(current); + const auto current = GetForegroundProcessName(); // True if executable switch equals current window bool equals = (executable.toStdString() == current); diff --git a/lib/osx/advanced-scene-switcher-osx.mm b/lib/osx/advanced-scene-switcher-osx.mm index 1abc7826..4f592041 100644 --- a/lib/osx/advanced-scene-switcher-osx.mm +++ b/lib/osx/advanced-scene-switcher-osx.mm @@ -14,10 +14,9 @@ namespace advss { -void GetWindowList(std::vector &windows) +std::vector GetWindowList() { - windows.resize(0); - + std::vector windows; @autoreleasepool { CFArrayRef cfApps = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID); @@ -49,21 +48,12 @@ void GetWindowList(std::vector &windows) apps = nil; CFRelease(cfApps); } + return windows; } -void GetWindowList(QStringList &windows) +std::string GetCurrentWindowTitle() { - windows.clear(); - std::vector temp; - GetWindowList(temp); - for (auto &w : temp) { - windows << QString::fromStdString(w); - } -} - -void GetCurrentWindowTitle(std::string &title) -{ - title.resize(0); + std::string title; @autoreleasepool { CFArrayRef cfApps = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID); @@ -107,6 +97,7 @@ void GetCurrentWindowTitle(std::string &title) apps = nil; CFRelease(cfApps); } + return title; } bool isWindowOriginOnScreen(NSDictionary *app, NSScreen *screen, @@ -273,9 +264,9 @@ int SecondsSinceLastInput() return (int)time; } -void GetProcessList(QStringList &list) +QStringList GetProcessList() { - list.clear(); + QStringList list; @autoreleasepool { NSWorkspace *ws = [NSWorkspace sharedWorkspace]; NSArray *array = [ws runningApplications]; @@ -291,11 +282,11 @@ void GetProcessList(QStringList &list) } } } + return list; } -void GetForegroundProcessName(std::string &proc) +std::string GetForegroundProcessName() { - proc.resize(0); @autoreleasepool { NSWorkspace *ws = [NSWorkspace sharedWorkspace]; NSArray *array = [ws runningApplications]; @@ -308,17 +299,13 @@ void GetForegroundProcessName(std::string &proc) break; } const char *str = name.UTF8String; - proc = std::string(str); + if (str) { + return str; + } break; } } -} - -void GetForegroundProcessName(QString &proc) -{ - std::string temp; - GetForegroundProcessName(temp); - proc = QString::fromStdString(temp); + return {}; } std::string GetForegroundProcessPath() @@ -378,8 +365,7 @@ QStringList GetProcessPathsFromName(const QString &name) bool IsInFocus(const QString &executable) { - std::string current; - GetForegroundProcessName(current); + const auto current = GetForegroundProcessName(); // True if executable switch equals current window bool equals = (executable.toStdString() == current); diff --git a/lib/platform-funcs.hpp b/lib/platform-funcs.hpp index f33abfd3..6d024a2e 100644 --- a/lib/platform-funcs.hpp +++ b/lib/platform-funcs.hpp @@ -11,15 +11,14 @@ namespace advss { enum class HotkeyType; -EXPORT void GetWindowList(std::vector &windows); -EXPORT void GetWindowList(QStringList &windows); -EXPORT void GetCurrentWindowTitle(std::string &title); +EXPORT std::vector GetWindowList(); +EXPORT std::string GetCurrentWindowTitle(); EXPORT bool IsFullscreen(const std::string &title); EXPORT bool IsMaximized(const std::string &title); EXPORT std::optional GetTextInWindow(const std::string &window); EXPORT int SecondsSinceLastInput(); -EXPORT void GetProcessList(QStringList &processes); -EXPORT void GetForegroundProcessName(std::string &name); +EXPORT QStringList GetProcessList(); +EXPORT std::string GetForegroundProcessName(); EXPORT std::string GetForegroundProcessPath(); EXPORT QStringList GetProcessPathsFromName(const QString &name); EXPORT bool IsInFocus(const QString &executable); diff --git a/lib/utils/first-run-wizard.cpp b/lib/utils/first-run-wizard.cpp index af40ae79..c0be4ebe 100644 --- a/lib/utils/first-run-wizard.cpp +++ b/lib/utils/first-run-wizard.cpp @@ -55,9 +55,7 @@ static void WriteFirstRun(bool value) static QString DetectFocusedWindow() { - std::string title; - GetCurrentWindowTitle(title); - return QString::fromStdString(title); + return QString::fromStdString(GetCurrentWindowTitle()); } // =========================================================================== diff --git a/lib/utils/selection-helpers.cpp b/lib/utils/selection-helpers.cpp index 391ee3e9..b27de7eb 100644 --- a/lib/utils/selection-helpers.cpp +++ b/lib/utils/selection-helpers.cpp @@ -173,10 +173,9 @@ void PopulateTransitionSelection(QComboBox *sel, bool addCurrent, bool addAny, void PopulateWindowSelection(QComboBox *sel, bool addSelect) { - std::vector windows; - GetWindowList(windows); + const auto windows = GetWindowList(); - for (std::string &window : windows) { + for (const std::string &window : windows) { sel->addItem(window.c_str()); } @@ -257,8 +256,7 @@ void PopulateMediaSelection(QComboBox *sel, bool addSelect) void PopulateProcessSelection(QComboBox *sel, bool addSelect) { - QStringList processes; - GetProcessList(processes); + auto processes = GetProcessList(); processes.sort(); for (QString &process : processes) { sel->addItem(process); diff --git a/lib/win/advanced-scene-switcher-win.cpp b/lib/win/advanced-scene-switcher-win.cpp index 4d8c0421..2ba00294 100644 --- a/lib/win/advanced-scene-switcher-win.cpp +++ b/lib/win/advanced-scene-switcher-win.cpp @@ -133,9 +133,9 @@ const std::vector getOBSWindows() return lastDoneHelper->windows; } -void GetWindowList(std::vector &windows) +std::vector GetWindowList() { - windows.resize(0); + std::vector windows; EnumWindowsWithMetro(GetTitleCB, reinterpret_cast(&windows)); // Also add OBS windows @@ -147,20 +147,10 @@ void GetWindowList(std::vector &windows) // Add entry for OBS Studio itself - see GetCurrentWindowTitle() windows.emplace_back("OBS"); + return windows; } -void GetWindowList(QStringList &windows) -{ - windows.clear(); - - std::vector w; - GetWindowList(w); - for (auto window : w) { - windows << QString::fromStdString(window); - } -} - -void GetCurrentWindowTitle(std::string &title) +std::string GetCurrentWindowTitle() { HWND window = GetForegroundWindow(); DWORD pid; @@ -178,15 +168,15 @@ void GetCurrentWindowTitle(std::string &title) // // So instead rely on Qt to get the title of the active window. if (GetCurrentProcessId() == pid) { - auto window = QApplication::activeWindow(); - if (window) { - title = window->windowTitle().toStdString(); - } else { - title = "OBS"; + auto obsWindow = QApplication::activeWindow(); + if (obsWindow) { + return obsWindow->windowTitle().toStdString(); } - return; + return "OBS"; } + std::string title; GetWindowTitle(window, title); + return title; } static HWND getHWNDfromTitle(const std::string &title) @@ -350,22 +340,22 @@ bool IsFullscreen(const std::string &title) return false; } -void GetProcessList(QStringList &processes) +QStringList GetProcessList() { - + QStringList processes; HANDLE procSnapshot; PROCESSENTRY32 procEntry; procSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (procSnapshot == INVALID_HANDLE_VALUE) { - return; + return processes; } procEntry.dwSize = sizeof(PROCESSENTRY32); if (!Process32First(procSnapshot, &procEntry)) { CloseHandle(procSnapshot); - return; + return processes; } do { @@ -383,9 +373,10 @@ void GetProcessList(QStringList &processes) } while (Process32Next(procSnapshot, &procEntry)); CloseHandle(procSnapshot); + return processes; } -static void GetForegroundProcessName(QString &proc) +static QString getForegroundProcessNameStr() { // only checks if the current foreground window is from the same executable, // may return true for any window from a program @@ -396,23 +387,21 @@ static void GetForegroundProcessName(QString &proc) HANDLE process = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId); if (process == NULL) { - return; + return {}; } WCHAR executablePath[600]; GetModuleFileNameEx(process, 0, executablePath, 600); CloseHandle(process); - proc = QString::fromWCharArray(executablePath) - .split(QRegularExpression("(/|\\\\)")) - .back(); + return QString::fromWCharArray(executablePath) + .split(QRegularExpression("(/|\\\\)")) + .back(); } -void GetForegroundProcessName(std::string &proc) +std::string GetForegroundProcessName() { - QString temp; - GetForegroundProcessName(temp); - proc = temp.toStdString(); + return getForegroundProcessNameStr().toStdString(); } std::string GetForegroundProcessPath() @@ -481,8 +470,7 @@ bool IsInFocus(const QString &executable) { // only checks if the current foreground window is from the same executable, // may return true for any window from a program - QString foregroundProc; - GetForegroundProcessName(foregroundProc); + const auto foregroundProc = getForegroundProcessNameStr(); // True if executable switch equals current window bool equals = (executable == foregroundProc); diff --git a/plugins/base/macro-action-window.cpp b/plugins/base/macro-action-window.cpp index ee633f95..aa46abf9 100644 --- a/plugins/base/macro-action-window.cpp +++ b/plugins/base/macro-action-window.cpp @@ -42,8 +42,7 @@ void CloseWindow(const std::string &) {} std::optional MacroActionWindow::GetMatchingWindow() const { - std::vector windowList; - GetWindowList(windowList); + const auto windowList = GetWindowList(); if (!_regex.Enabled()) { if (std::find(windowList.begin(), windowList.end(), diff --git a/plugins/base/macro-condition-process.cpp b/plugins/base/macro-condition-process.cpp index e79a7c06..10eb251e 100644 --- a/plugins/base/macro-condition-process.cpp +++ b/plugins/base/macro-condition-process.cpp @@ -16,8 +16,7 @@ bool MacroConditionProcess::_registered = MacroConditionFactory::Register( bool MacroConditionProcess::CheckCondition() { - std::string foregroundProcessName; - GetForegroundProcessName(foregroundProcessName); + const auto foregroundProcessName = GetForegroundProcessName(); SetVariableValue(foregroundProcessName); const QString proc = QString::fromStdString(_process); @@ -58,8 +57,7 @@ bool MacroConditionProcess::CheckCondition() return true; } - QStringList runningProcesses; - GetProcessList(runningProcesses); + const auto runningProcesses = GetProcessList(); for (const auto &process : runningProcesses) { bool nameMatches = _regex.Enabled() @@ -275,9 +273,8 @@ void MacroConditionProcessEdit::PathRegexChanged(const RegexConfig &conf) void MacroConditionProcessEdit::UpdateFocusProcess() { - std::string name; - GetForegroundProcessName(name); - _focusProcess->setText(QString::fromStdString(name)); + _focusProcess->setText( + QString::fromStdString(GetForegroundProcessName())); } void MacroConditionProcessEdit::SetWidgetVisibility() diff --git a/plugins/base/macro-condition-window.cpp b/plugins/base/macro-condition-window.cpp index 6e65e920..37d72023 100644 --- a/plugins/base/macro-condition-window.cpp +++ b/plugins/base/macro-condition-window.cpp @@ -135,8 +135,7 @@ static bool foregroundWindowChanged() bool MacroConditionWindow::CheckCondition() { - std::vector windowList; - GetWindowList(windowList); + const auto windowList = GetWindowList(); bool match = false; if (_windowRegex.Enabled()) { match = WindowRegexMatches(windowList);