clang-tidy
[staging/windowmanager.git] / src / layers.cpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <algorithm>
18 #include <regex>
19
20 #include "json_helper.hpp"
21 #include "layers.hpp"
22 #include "util.hpp"
23
24 namespace wm {
25
26 using json = nlohmann::json;
27
28 layer::layer(nlohmann::json const &j) {
29    if (j["type"] == "range") {
30       this->id_min = j["first_surface_id"];
31       this->id_max = j["last_surface_id"];
32    } else {
33       this->id_min = this->id_max = j["surface_id"];
34    }
35    this->role = j["role"];
36    this->name = j["name"];
37    this->layer_id = j["layer_id"];
38    this->rect = genivi::full_rect;
39    if (j["area"]["type"] == "rect") {
40       auto jr = j["area"]["rect"];
41       this->rect = genivi::rect{
42          jr["width"], jr["height"], jr["x"], jr["y"],
43       };
44    }
45 }
46
47 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
48    try {
49       layer_map stl{};
50       auto m = j["mappings"];
51
52       std::transform(std::cbegin(m), std::cend(m),
53                      std::inserter(stl.mapping, stl.mapping.end()),
54                      [](nlohmann::json const &j) { return layer(j); });
55
56       // XXX: add sanity checks here?
57       // * check for double IDs
58       // * check for double names/roles
59
60       stl.layers.reserve(m.size());
61       std::transform(
62          std::cbegin(stl.mapping), std::cend(stl.mapping),
63          std::back_inserter(stl.layers), [&stl](struct layer const &k) {
64             stl.roles.emplace_back(std::make_pair(k.role, k.layer_id));
65             return unsigned(k.layer_id);
66          });
67
68       // XXX need to sort layers?
69       for (auto i : stl.mapping) {
70          if (i.name.empty()) {
71             return Err<struct layer_map>("Found mapping w/o name");
72          }
73          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
74             return Err<struct layer_map>("Found invalid/unset IDs in mapping");
75          }
76       }
77
78       auto msi = j.find("main_surface");
79       if (msi != j.end()) {
80          stl.main_surface_name = msi->value("surface_role", "");
81          stl.main_surface =
82             stl.main_surface_name.empty() ? int((*msi)["surface_id"]) : -1;
83       }
84
85       // Check lookup
86       auto jtests = j.value("tests", json());
87
88       if (!jtests.empty()) {
89          logdebug("Embedded tests...");
90          std::vector<std::pair<int, int>> tests;
91          tests.reserve(jtests.size());
92          std::transform(std::cbegin(jtests), std::cend(jtests),
93                         std::back_inserter(tests), [](json const &j) {
94                            return std::make_pair(j["surface_id"],
95                                                  j["expect_layer_id"]);
96                         });
97
98          for (auto sid : tests) {
99             int lid = stl.get_layer_id(sid.first).value_or(-1);
100             logdebug("this=%d, that=%d, expect=%d", sid.first, lid, sid.second);
101             if (lid != sid.second) {
102                return Err<layer_map>("ID Map embedded test failed!");
103             }
104          }
105       }
106
107       return Ok(stl);
108    } catch (std::exception &e) {
109       return Err<struct layer_map>(e.what());
110    }
111 }
112
113 // Helper to allow std::lower_bound with a int key only
114 inline bool
115    operator<(struct layer const &a, int b) {
116    return a.id_max < b;
117 }
118
119 namespace {
120 optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
121                                         int surface_id) {
122    auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
123                              surface_id);
124
125    if (i != s2l->mapping.end()) {
126       // std::less only checks for layer::id_max, so check
127       // that we are actually inside of an interval here.
128       if (i->id_min <= surface_id) {
129          return optional<layer>(*i);
130       }
131    }
132
133    return nullopt;
134 }
135 }  // namespace
136
137 optional<int> layer_map::get_layer_id(int surface_id) {
138    auto e = get_surface_id_to_layer(this, surface_id);
139    if (!e) {
140       auto i = this->surfaces.find(surface_id);
141       if (i != this->surfaces.end()) {
142          return optional<int>(int(i->second));
143       }
144       return nullopt;
145    }
146    return optional<int>(e->layer_id);
147 }
148
149 optional<int> layer_map::get_layer_id(std::string const &role) {
150    for (auto const &r : this->roles) {
151       auto re = std::regex(r.first);
152       if (std::regex_match(role, re)) {
153          logdebug("role %s matches layer %d", role.c_str(), r.second);
154          return optional<int>(r.second);
155       }
156    }
157    logdebug("role %s does NOT match any layer", role.c_str());
158    return nullopt;
159 }
160
161 optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {
162    auto e = get_surface_id_to_layer(this, surface_id);
163    return e ? optional<genivi::rect>(e->rect) : nullopt;
164 }
165
166 json layer::to_json() const {
167    auto is_full = this->rect == genivi::full_rect;
168
169    json r{};
170    if (is_full) {
171       r = {{"type", "full"}};
172    } else {
173       r = {{"type", "rect"},
174            {"rect",
175             {{"x", this->rect.x},
176              {"y", this->rect.y},
177              {"width", this->rect.w},
178              {"height", this->rect.h}}}};
179    }
180
181    return {
182       {"id_min", this->id_min},
183       {"id_max", this->id_max},
184       {"name", this->name},
185       {"layer_id", this->layer_id},
186       {"area", r},
187    };
188 }
189
190 json layer_map::to_json() const {
191    json j{};
192    for (auto const &i : this->mapping) {
193       j.push_back(i.to_json());
194    }
195    return j;
196 }
197
198 }  // namespace wm