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