layers: introduce layers.hpp and .cpp for layer related code
[staging/windowmanager.git] / src / layers.cpp
1 //
2 // Created by m on 7/27/17.
3 //
4
5 #include "layers.hpp"
6 #include "json_helper.hpp"
7 #include "util.hpp"
8
9 namespace wm {
10
11 surface_id_to_layer::surface_id_to_layer(nlohmann::json const &j) {
12    DB(j);
13    if (j["type"] == "range") {
14       this->id_min = get<int>(j["first_surface_id"]);
15       this->id_max = get<int>(j["last_surface_id"]);
16    } else {
17       this->id_min = this->id_max = get<int>(j["surface_id"]);
18    }
19    this->name = j["name"].get<std::string>();
20    this->layer_id = get<int>(j["layer_id"]);
21 }
22
23 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
24    nlohmann::json const &j) {
25    DB(j);
26    try {
27       surface_id_to_layer_map stl{};
28       std::transform(
29          std::cbegin(j), std::cend(j),
30          std::inserter(stl.mapping, stl.mapping.end()),
31          [](nlohmann::json const &j) { return surface_id_to_layer(j); });
32       for (auto i : stl.mapping) {
33          if (i.name.empty()) {
34             return Err<struct surface_id_to_layer_map>(
35                "Found mapping w/o name");
36          }
37          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
38             return Err<struct surface_id_to_layer_map>(
39                "Found invalid/unset IDs in mapping");
40          }
41       }
42       return Ok(stl);
43    } catch (std::exception &e) {
44       return Err<struct surface_id_to_layer_map>(e.what());
45    }
46 }
47
48 }  // namespace wm