swaymux/sway_bindings/swayoutputs.h

59 lines
1.4 KiB
C
Raw Normal View History

//
// Created by grimmauld on 08.03.24.
//
#ifndef SWAYMUX_SWAYOUTPUTS_H
#define SWAYMUX_SWAYOUTPUTS_H
#include <string>
#include <nlohmann/json.hpp>
#include "Sway.h"
#include "../tree/swaytree.h"
using json = nlohmann::json;
struct SwayOutput {
explicit SwayOutput() : id(0), name("none"), active(false), power(false) {};
explicit SwayOutput(const json &rep) :
id(rep["id"]),
name(rep["name"].is_null() ? "none" : rep["name"]),
active(rep["active"]),
power(rep["power"]) {};
const std::string name;
int id;
const bool active;
const bool power;
[[nodiscard]] bool isValid() const {
return active && power;
}
};
class SwayOutputList {
public:
explicit SwayOutputList(const json &rep) {
for (const auto &o: rep) {
outputs.emplace_back(o);
}
}
std::vector<SwayOutput> outputs;
explicit SwayOutputList(const Sway &sway) : SwayOutputList(json::parse(sway.sendIPC(SWAY_GET_OUTPUTS).msg)) {};
[[nodiscard]] bool isOutputValid(const SwayTreeNode *test) {
if (test == nullptr)
return false;
if (test->node.type != NodeType::output)
return false;
return std::ranges::any_of(outputs.begin(), outputs.end(), [test](const auto &o) {
return o.name == test->node.name && o.isValid();
});
}
};
#endif //SWAYMUX_SWAYOUTPUTS_H