swaymux/tree/pstree.h

77 lines
2.1 KiB
C
Raw Normal View History

2024-03-02 22:33:27 +01:00
//
// 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("root"), 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:
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