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