layers: provide a layers-only vector
[staging/windowmanager.git] / src / layers.cpp
1 //
2 // Created by m on 7/27/17.
3 //
4
5 #include <algorithm>
6
7 #include "json_helper.hpp"
8 #include "layers.hpp"
9 #include "util.hpp"
10
11 namespace wm {
12
13 using json = nlohmann::json;
14
15 surface_id_to_layer::surface_id_to_layer(nlohmann::json const &j) {
16    // DB(j);
17    if (j["type"] == "range") {
18       this->id_min = get<int>(j["first_surface_id"]);
19       this->id_max = get<int>(j["last_surface_id"]);
20    } else {
21       this->id_min = this->id_max = get<int>(j["surface_id"]);
22    }
23    this->name = j["name"].get<std::string>();
24    this->layer_id = get<int>(j["layer_id"]);
25 }
26
27 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
28    nlohmann::json const &j) {
29    DB(j);
30    try {
31       surface_id_to_layer_map stl{};
32       auto m = j["mappings"];
33       stl.layers.reserve(m.size());
34       std::transform(
35          std::cbegin(m), std::cend(m),
36          std::inserter(stl.mapping, stl.mapping.end()),
37          [&stl](nlohmann::json const &j) {
38             auto k = surface_id_to_layer(j);
39             stl.layers.push_back(unsigned(k.layer_id));
40             return k;
41          });
42       // XXX need to sort layers?
43       for (auto i : stl.mapping) {
44          if (i.name.empty()) {
45             return Err<struct surface_id_to_layer_map>(
46                "Found mapping w/o name");
47          }
48          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
49             return Err<struct surface_id_to_layer_map>(
50                "Found invalid/unset IDs in mapping");
51          }
52       }
53
54       // Check lookup
55       auto jtests = j.value("tests", json());
56
57       if (! jtests.empty()) {
58          DB("Embedded tests...");
59          std::vector<std::pair<int, int>> tests;
60          tests.reserve(jtests.size());
61          std::transform(std::cbegin(jtests), std::cend(jtests),
62                         std::back_inserter(tests), [](json const &j) {
63                     return std::make_pair(get<int>(j["surface_id"]),
64                                           get<int>(j["expect_layer_id"]));
65                  });
66
67          for (auto sid : tests) {
68             int lid = stl.get_layer_for_surface(sid.first).value_or(-1);
69             DB("this=" << sid.first << ", that=" << lid
70                        << ", expect=" << sid.second);
71             if (lid != sid.second) {
72                return Err<surface_id_to_layer_map>(
73                        "ID Map embedded test failed!");
74             }
75          }
76       }
77
78       return Ok(stl);
79    } catch (std::exception &e) {
80       return Err<struct surface_id_to_layer_map>(e.what());
81    }
82 }
83
84 // Helper to allow std::lower_bound with a int key only
85 inline bool
86    operator<(struct surface_id_to_layer const &a, int b) {
87    return a.id_max < b;
88 }
89
90 optional<int> surface_id_to_layer_map::get_layer_for_surface(int surface_id) {
91    auto i = std::lower_bound(std::cbegin(this->mapping),
92                              std::cend(this->mapping), surface_id);
93
94    if (i != this->mapping.end()) {
95       // std::less only checks for surface_id_to_layer::id_max, so check
96       // that we are actually inside of an interval here.
97       if (i->id_min <= surface_id) {
98          return optional<int>(i->layer_id);
99       }
100    }
101
102    return nullopt;
103 }
104
105 }  // namespace wm