From da50e7b8fad12e4d73b5074e8d0022f240a29780 Mon Sep 17 00:00:00 2001 From: LordGrimmauld Date: Mon, 11 Mar 2024 17:12:39 +0100 Subject: [PATCH] Remove old process tree experiments --- CMakeLists.txt | 2 - mainwindow.h | 1 - tree/PsTreeModel.cpp | 20 ------ tree/PsTreeModel.h | 35 ---------- tree/SwayTreeModel.h | 1 - tree/pstree.cpp | 153 ------------------------------------------- tree/pstree.h | 79 ---------------------- 7 files changed, 291 deletions(-) delete mode 100644 tree/PsTreeModel.cpp delete mode 100644 tree/PsTreeModel.h delete mode 100644 tree/pstree.cpp delete mode 100644 tree/pstree.h diff --git a/CMakeLists.txt b/CMakeLists.txt index da1d46d..80fe64d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,8 +20,6 @@ set(PROJECT_SOURCES mainwindow.h mainwindow.ui # ${TS_FILES} - tree/pstree.cpp - tree/PsTreeModel.cpp sway_bindings/Formatter.h sway_bindings/Sway.cpp tree/swaytree.h diff --git a/mainwindow.h b/mainwindow.h index 476c4ee..9f18a92 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,7 +2,6 @@ #define MAINWINDOW_H #include -#include "tree/PsTreeModel.h" #include "tree/SwayTreeModel.h" #include "Keys/KeyHandler.h" diff --git a/tree/PsTreeModel.cpp b/tree/PsTreeModel.cpp deleted file mode 100644 index 4ed349e..0000000 --- a/tree/PsTreeModel.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by grimmauld on 25.02.24. -// - -#include "PsTreeModel.h" - -void PsTreeModel::update() { - /* - auto* root = getRoot(); - auto changes = update_process_records(root); - - QList changedParents{}; - for(auto* node: changes) { - changedParents.push_back(createIndex(node->row(), 0, node)); - } - layoutAboutToBeChanged(changedParents); - */ -} - -PsTreeModel::PsTreeModel(QObject *parent) : PsTreeModel(get_process_records(), parent) {} diff --git a/tree/PsTreeModel.h b/tree/PsTreeModel.h deleted file mode 100644 index f46aec8..0000000 --- a/tree/PsTreeModel.h +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by grimmauld on 25.02.24. -// - -#ifndef SWAYMUX_PSTREEMODEL_H -#define SWAYMUX_PSTREEMODEL_H - - -#include -#include "pstree.h" -#include "AbstractTreeModel.h" - -class PsTreeModel : public AbstractTreeModel { -Q_OBJECT -public: - Q_DISABLE_COPY_MOVE(PsTreeModel) - - explicit PsTreeModel(QObject *parent = nullptr); - - ~PsTreeModel() override = default; - - explicit PsTreeModel(ProcessTreeNode * rootItem, QObject *parent = nullptr) : rootItem(rootItem), AbstractTreeModel(parent) {}; - - void update(); - - [[nodiscard]] const ProcessTreeNode *getRoot() const override { - return rootItem; - }; - -private: - const ProcessTreeNode* rootItem; -}; - - -#endif //SWAYMUX_PSTREEMODEL_H diff --git a/tree/SwayTreeModel.h b/tree/SwayTreeModel.h index 83d66f4..56186a5 100644 --- a/tree/SwayTreeModel.h +++ b/tree/SwayTreeModel.h @@ -7,7 +7,6 @@ #include #include -#include "pstree.h" #include "AbstractTreeModel.h" #include "swaytree.h" #include "../sway_bindings/Sway.h" diff --git a/tree/pstree.cpp b/tree/pstree.cpp deleted file mode 100644 index 65b6be0..0000000 --- a/tree/pstree.cpp +++ /dev/null @@ -1,153 +0,0 @@ -// -// Created by grimmauld on 25.02.24. -// - -#include "pstree.h" -#include "../util/StringUtil.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -const char *procFileDelim = ":"; - -void populateTree(ProcessTreeNode *node, std::map> &ppid_map, std::set& changedParents) { - if (node == nullptr) - return; - - for (int i = (int) node->childCount() - 1; i >= 0; i--) { // backwards to avoid changing stuff that might change later - std::set children = ppid_map[node->proc.ppid]; - auto* c = node->child(i); - if (c->proc.pid == 0) - continue; - - if (children.find(c->proc) == children.end()) { - // node->removeChild(i); // fixme: segfaults - changedParents.insert(node); - } - } - - for (const auto &child: ppid_map[node->proc.pid]) { - // check child exists, if so no need to update - auto* existingChildNode = node->findChild(ProcessTreeNode::matchingRecord(child)); - if (existingChildNode != nullptr) { - populateTree(existingChildNode, ppid_map, changedParents); - continue; - } - - // else add the new child node, keep track in notifier - changedParents.insert(node); - auto childNode = std::make_unique(child, node); - populateTree(childNode.get(), ppid_map, changedParents); - node->appendChild(childNode); - } -} - -ProcessTreeNode * get_process_records() { - auto* root = new ProcessTreeNode(); // rootPrc - update_process_records(root); - return root; -} - -std::set update_process_records(ProcessTreeNode *node) { - std::map pid_map; - std::map> ppid_map; - std::set changedParents{}; - - const auto proc_fs = fs::path(PROCFS_ROOT); - auto content = fs::directory_iterator(proc_fs); - for (const auto &proc_dir: content) { - if (fs::is_directory(proc_dir)) { - insert_process_record(proc_dir.path() / "status", pid_map); - } - } - - pid_t rootPid = INT32_MAX; - for (auto &pair: pid_map) { - auto proc = pair.second; - if (proc.pid < rootPid) { - rootPid = proc.pid; - } - ppid_map[proc.ppid].insert(proc); - } - - // auto rootPrc = pid_map[rootPid]; - // ProcessTreeNode root = ProcessTreeNode(); // rootPrc - populateTree(node, ppid_map, changedParents); - return changedParents; -} - -void insert_process_record(const std::filesystem::path &status_path, std::map &buff) { - if (!fs::exists(status_path) || !fs::is_regular_file(status_path)) - return; - - std::string proc_name; - pid_t pid, ppid; - bool foundPid, foundPPid, foundName; - - auto reader = std::ifstream{status_path, std::ios::in}; - std::string line; - while (std::getline(reader, line)) { - auto data = line.data(); - auto name = strtok(data, procFileDelim); - auto val = strtok(nullptr, procFileDelim); - if (name == nullptr || val == nullptr) - continue; - - if (strcmp(name, "Name") == 0) { - proc_name = std::string(val); - trim(proc_name); - foundName = true; - } else if (strcmp(name, "Pid") == 0) { - pid = std::stoi(val); - foundPid = true; - } else if (strcmp(name, "PPid") == 0) { - ppid = std::stoi(val); - foundPPid = true; - } - } - - if (foundPPid && foundName && foundPid) { - buff.insert({pid, ProcessRecord(proc_name, pid, ppid)}); - } -} - -void printTree(const ProcessTreeNode &node, const std::string &prefix) { - std::cout << prefix << node.proc.name << ": " << node.proc.pid << "\n"; - for (const auto &child: node.children) { - printTree(*child, prefix + "\t"); - } -} - -void printTree() { - auto* tree = get_process_records(); - printTree(*tree, ""); - delete tree; -} - -QVariant ProcessTreeNode::data(int column) const { - switch (column) { - case 0: - return QString::fromStdString(std::to_string(proc.pid)); - case 1: - return QString::fromStdString(proc.name); - default: - throw std::exception(); - } -} - -QVariant ProcessTreeNode::headerData(int column) const { - switch (column) { - case 0: - return "PID"; - case 1: - return "Process Name"; - default: - return QVariant{}; - } -} \ No newline at end of file diff --git a/tree/pstree.h b/tree/pstree.h deleted file mode 100644 index 2b63909..0000000 --- a/tree/pstree.h +++ /dev/null @@ -1,79 +0,0 @@ -// -// Created by grimmauld on 25.02.24. -// - -#ifndef SWAYMUX_PSTREE_H -#define SWAYMUX_PSTREE_H - -#define PROCFS_ROOT "/proc" - - -#include -#include -#include -#include -#include -#include -#include -#include "AbstractTreeNode.h" - -void printTree(); - -namespace fs = std::filesystem; - -class ProcessRecord { -public: - const std::string name; - const pid_t pid; - const pid_t ppid; - - ProcessRecord(std::string name, pid_t pid, pid_t ppid) : name(std::move(name)), pid(pid), ppid(ppid) {} - - ProcessRecord() : name("rootItem"), pid(0), ppid(-1) {} - - [[nodiscard]] bool operator<(const ProcessRecord &other) const { - return this->pid < other.pid; - } - - [[nodiscard]] bool operator==(const ProcessRecord &other) const { - return this->pid == other.pid && this->ppid == other.ppid && this->name == other.name; - } -}; - - -class ProcessTreeNode : public AbstractTreeNode { -public: - ProcessTreeNode(ProcessTreeNode& node) = delete; // default constructor to make the static asserts shut up - ProcessTreeNode(const ProcessTreeNode& node) = delete; // default constructor to make the static asserts shut up - - explicit ProcessTreeNode(ProcessTreeNode *parent = nullptr) : proc(ProcessRecord()), AbstractTreeNode(parent) {} - - const ProcessRecord proc; - - explicit ProcessTreeNode(ProcessRecord proc, ProcessTreeNode *parent = nullptr) : proc(std::move(proc)), - AbstractTreeNode(parent) {} - - [[nodiscard]] bool operator<(const ProcessTreeNode &other) const { - return this->proc < other.proc; - } - - [[nodiscard]] QVariant data(int column) const override; - - [[nodiscard]] QVariant headerData(int column) const override; - - [[nodiscard]] int columnCount() const override { return 2; }; - - std::function static matchingRecord(const ProcessRecord &other) { - return [&other](const ProcessTreeNode &node) { - return node.proc == other; - }; - }; -}; - -ProcessTreeNode * get_process_records(); - -void insert_process_record(const std::filesystem::path &status_path, std::map &buff); - -std::set update_process_records(ProcessTreeNode *node); - -#endif //SWAYMUX_PSTREE_H