swaymux/tree/pstree.h
2024-03-04 00:27:05 +01:00

80 lines
2.3 KiB
C++

//
// 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