layers: rename surface_id_to_layer and surface_id_to_layer_map
[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 layer::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    this->rect = genivi::rect{-1, -1, 0, 0};
26    if (j["area"]["type"] == "rect") {
27       auto jr = j["area"]["rect"];
28       this->rect = genivi::rect{
29          get<int32_t>(jr["width"]), get<int32_t>(jr["height"]),
30          get<int32_t>(jr["x"]), get<int32_t>(jr["y"]),
31       };
32    }
33 }
34
35 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
36    DB(j);
37    try {
38       layer_map stl{};
39       auto m = j["mappings"];
40       stl.layers.reserve(m.size());
41       std::transform(std::cbegin(m), std::cend(m),
42                      std::inserter(stl.mapping, stl.mapping.end()),
43                      [&stl](nlohmann::json const &j) {
44                         auto k = layer(j);
45                         stl.layers.push_back(unsigned(k.layer_id));
46                         return k;
47                      });
48       // XXX need to sort layers?
49       for (auto i : stl.mapping) {
50          if (i.name.empty()) {
51             return Err<struct layer_map>("Found mapping w/o name");
52          }
53          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
54             return Err<struct layer_map>("Found invalid/unset IDs in mapping");
55          }
56       }
57
58       // Check lookup
59       auto jtests = j.value("tests", json());
60
61       if (!jtests.empty()) {
62          DB("Embedded tests...");
63          std::vector<std::pair<int, int>> tests;
64          tests.reserve(jtests.size());
65          std::transform(std::cbegin(jtests), std::cend(jtests),
66                         std::back_inserter(tests), [](json const &j) {
67                            return std::make_pair(
68                               get<int>(j["surface_id"]),
69                               get<int>(j["expect_layer_id"]));
70                         });
71
72          for (auto sid : tests) {
73             int lid = stl.get_layer_id(sid.first).value_or(-1);
74             DB("this=" << sid.first << ", that=" << lid
75                        << ", expect=" << sid.second);
76             if (lid != sid.second) {
77                return Err<layer_map>("ID Map embedded test failed!");
78             }
79          }
80       }
81
82       return Ok(stl);
83    } catch (std::exception &e) {
84       return Err<struct layer_map>(e.what());
85    }
86 }
87
88 // Helper to allow std::lower_bound with a int key only
89 inline bool
90    operator<(struct layer const &a, int b) {
91    return a.id_max < b;
92 }
93
94 namespace {
95 optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
96                                         int surface_id) {
97    auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
98                              surface_id);
99
100    if (i != s2l->mapping.end()) {
101       // std::less only checks for layer::id_max, so check
102       // that we are actually inside of an interval here.
103       if (i->id_min <= surface_id) {
104          return optional<layer>(*i);
105       }
106    }
107
108    return nullopt;
109 }
110 }
111
112 optional<int> layer_map::get_layer_id(int surface_id) {
113    auto e = get_surface_id_to_layer(this, surface_id);
114    return e ? optional<int>(e->layer_id) : nullopt;
115 }
116
117 optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {
118    auto e = get_surface_id_to_layer(this, surface_id);
119    return e ? optional<genivi::rect>(e->rect) : nullopt;
120 }
121
122 }  // namespace wm