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