From 5494d99e4ca42f71180153322d268c000e8bfa91 Mon Sep 17 00:00:00 2001 From: SlyFabi Date: Mon, 10 Oct 2022 14:31:12 +0200 Subject: [PATCH] Improve App Category tree --- src/api/process/ProcessManager.cpp | 38 +++++++++++++++++++++++++++++- src/api/process/ProcessManager.h | 1 + src/api/process/ProcessNode.cpp | 8 +++---- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/api/process/ProcessManager.cpp b/src/api/process/ProcessManager.cpp index 513fb06..08964f8 100644 --- a/src/api/process/ProcessManager.cpp +++ b/src/api/process/ProcessManager.cpp @@ -15,6 +15,7 @@ std::map ProcessManager::m_PidCpuTimes; std::map ProcessManager::m_PidIOUsages; std::vector g_AppBlacklist = {"gnome-shell", "plasmashell"}; // NOLINT(cert-err58-cpp) +std::vector g_AppParentBlacklist = {"bash", "bwrap"}; inline bool is_in_app_blacklist(ProcessNode *node) { return Utils::vectorContains(g_AppBlacklist, node->GetName()); @@ -95,7 +96,7 @@ std::vector ProcessManager::GetProcessesByCategory(int categoryId windowPids = CGUtils::GetAllPidsWithWindows(); filterFunc = [&](ProcessNode *proc) -> bool { return proc->GetUserIds().uid == getuid() && !is_in_app_blacklist(proc) && Utils::vectorContains(windowPids, proc->GetPid()); }; - result = ProcessManager::GetProcessesByFilter(filterFunc, !AppSettings::Get().displayProcList); + result = ProcessManager::GetAppProcessesByFilter(filterFunc, !AppSettings::Get().displayProcList); break; } case PROCESSES_VIEW_CATEGORY_WINE: { @@ -154,6 +155,41 @@ std::vector ProcessManager::GetProcessesByFilter(const std::funct return result; } +std::vector ProcessManager::GetAppProcessesByFilter(const std::function& filterFunc, bool withChildren) { + std::vector result; + std::vector pids; + + auto procList = GetAllProcesses(); + for(auto proc : procList) { + if(filterFunc(proc)) { + auto realProc = proc; + auto minDepth = INT32_MAX; + for(auto child : proc->FlatTree()) { + if(Utils::vectorContains(g_AppParentBlacklist, child->GetName())) + continue; + + auto childDepth = child->GetDepth(); + if(childDepth < minDepth) { + realProc = child; + minDepth = childDepth; + } + } + + for(auto child : realProc->FlatTree()) { + if(Utils::vectorContains(pids, child->GetPid())) + continue; + if(!withChildren && realProc->GetName() != child->GetName()) + continue; + + result.push_back(child->Copy()); + pids.push_back(child->GetPid()); + } + } + } + + return result; +} + std::vector ProcessManager::GetAllProcesses() { if(!m_AllProcessesCache.empty()) return m_AllProcessesCache; diff --git a/src/api/process/ProcessManager.h b/src/api/process/ProcessManager.h index 2a711c3..2b99036 100644 --- a/src/api/process/ProcessManager.h +++ b/src/api/process/ProcessManager.h @@ -31,6 +31,7 @@ class ProcessManager { private: static std::vector GetProcessesByCategory(int categoryId); static std::vector GetProcessesByFilter(const std::function& filterFunc, bool withChildren = true); + static std::vector GetAppProcessesByFilter(const std::function& filterFunc, bool withChildren = true); static std::vector GetAllProcesses(); static double GetCPUUsageForProc(const ProcessInfo& proc, CPUTimes cpuTimes); diff --git a/src/api/process/ProcessNode.cpp b/src/api/process/ProcessNode.cpp index 85139f1..b08424f 100644 --- a/src/api/process/ProcessNode.cpp +++ b/src/api/process/ProcessNode.cpp @@ -54,12 +54,10 @@ std::vector ProcessNode::GetChildren() { } int ProcessNode::GetDepth() { - int depth = 0; - for(auto child : m_Children) { - depth += child->GetDepth(); - } + if(m_ParentPid == -1 || m_Parent == nullptr) + return 0; - return depth; + return m_Parent->GetDepth() + 1; } int ProcessNode::GetPid() const {