Remove old process tree experiments
This commit is contained in:
parent
db6d7c66b8
commit
da50e7b8fa
7 changed files with 0 additions and 291 deletions
|
@ -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
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include "tree/PsTreeModel.h"
|
||||
#include "tree/SwayTreeModel.h"
|
||||
#include "Keys/KeyHandler.h"
|
||||
|
||||
|
|
|
@ -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<QPersistentModelIndex> changedParents{};
|
||||
for(auto* node: changes) {
|
||||
changedParents.push_back(createIndex(node->row(), 0, node));
|
||||
}
|
||||
layoutAboutToBeChanged(changedParents);
|
||||
*/
|
||||
}
|
||||
|
||||
PsTreeModel::PsTreeModel(QObject *parent) : PsTreeModel(get_process_records(), parent) {}
|
|
@ -1,35 +0,0 @@
|
|||
//
|
||||
// Created by grimmauld on 25.02.24.
|
||||
//
|
||||
|
||||
#ifndef SWAYMUX_PSTREEMODEL_H
|
||||
#define SWAYMUX_PSTREEMODEL_H
|
||||
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include "pstree.h"
|
||||
#include "AbstractTreeModel.h"
|
||||
|
||||
class PsTreeModel : public AbstractTreeModel<ProcessTreeNode> {
|
||||
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<ProcessTreeNode>(parent) {};
|
||||
|
||||
void update();
|
||||
|
||||
[[nodiscard]] const ProcessTreeNode *getRoot() const override {
|
||||
return rootItem;
|
||||
};
|
||||
|
||||
private:
|
||||
const ProcessTreeNode* rootItem;
|
||||
};
|
||||
|
||||
|
||||
#endif //SWAYMUX_PSTREEMODEL_H
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#include <QAbstractItemModel>
|
||||
#include <utility>
|
||||
#include "pstree.h"
|
||||
#include "AbstractTreeModel.h"
|
||||
#include "swaytree.h"
|
||||
#include "../sway_bindings/Sway.h"
|
||||
|
|
153
tree/pstree.cpp
153
tree/pstree.cpp
|
@ -1,153 +0,0 @@
|
|||
//
|
||||
// Created by grimmauld on 25.02.24.
|
||||
//
|
||||
|
||||
#include "pstree.h"
|
||||
#include "../util/StringUtil.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
const char *procFileDelim = ":";
|
||||
|
||||
void populateTree(ProcessTreeNode *node, std::map<pid_t, std::set<ProcessRecord>> &ppid_map, std::set<ProcessTreeNode*>& 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<ProcessRecord> 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<ProcessTreeNode>(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<ProcessTreeNode*> update_process_records(ProcessTreeNode *node) {
|
||||
std::map<pid_t, ProcessRecord> pid_map;
|
||||
std::map<pid_t, std::set<ProcessRecord>> ppid_map;
|
||||
std::set<ProcessTreeNode*> 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<pid_t, ProcessRecord> &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{};
|
||||
}
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
//
|
||||
// Created by grimmauld on 25.02.24.
|
||||
//
|
||||
|
||||
#ifndef SWAYMUX_PSTREE_H
|
||||
#define SWAYMUX_PSTREE_H
|
||||
|
||||
#define PROCFS_ROOT "/proc"
|
||||
|
||||
|
||||
#include <filesystem>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <QVariant>
|
||||
#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<ProcessTreeNode> {
|
||||
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<bool(const ProcessTreeNode &)> 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<pid_t, ProcessRecord> &buff);
|
||||
|
||||
std::set<ProcessTreeNode*> update_process_records(ProcessTreeNode *node);
|
||||
|
||||
#endif //SWAYMUX_PSTREE_H
|
Loading…
Reference in a new issue