WIP split layouts, reading config, defining data layout.
[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    auto split_layouts = j.find("split_layouts");
46    if (split_layouts != j.end()) {
47       auto &sls = j["split_layouts"];
48       this->layouts.reserve(sls.size());
49       std::transform(std::cbegin(sls), std::cend(sls),
50                      std::back_inserter(this->layouts), [](json const &sl) {
51                         struct split_layout l {};
52                         l.name = sl["name"];
53                         l.main_match = sl["main_match"];
54                         l.sub_match = sl["sub_match"];
55                         l.prio = sl.value<int>("priority", 0);
56                         logdebug(
57                            "Added split_layout \"%s\" (main: \"%s\") (sub: "
58                            "\"%s\") (prio: %d)",
59                            l.name.c_str(), l.main_match.c_str(),
60                            l.sub_match.c_str(), l.prio);
61                         return l;
62                      });
63       std::sort(std::begin(this->layouts), std::end(this->layouts),
64                 [](struct split_layout const &a, struct split_layout const &b) {
65                    return a.prio < b.prio;
66                 });
67    }
68 }
69
70 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
71    try {
72       layer_map stl{};
73       auto m = j["mappings"];
74
75       std::transform(std::cbegin(m), std::cend(m),
76                      std::inserter(stl.mapping, stl.mapping.end()),
77                      [](nlohmann::json const &j) { return layer(j); });
78
79       // XXX: add sanity checks here?
80       // * check for double IDs
81       // * check for double names/roles
82
83       stl.layers.reserve(m.size());
84       std::transform(
85          std::cbegin(stl.mapping), std::cend(stl.mapping),
86          std::back_inserter(stl.layers), [&stl](struct layer const &k) {
87             stl.roles.emplace_back(std::make_pair(k.role, k.layer_id));
88             return unsigned(k.layer_id);
89          });
90
91       // XXX need to sort layers?
92       for (auto i : stl.mapping) {
93          if (i.name.empty()) {
94             return Err<struct layer_map>("Found mapping w/o name");
95          }
96          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
97             return Err<struct layer_map>("Found invalid/unset IDs in mapping");
98          }
99       }
100
101       auto msi = j.find("main_surface");
102       if (msi != j.end()) {
103          stl.main_surface_name = msi->value("surface_role", "");
104          stl.main_surface =
105             stl.main_surface_name.empty() ? int((*msi)["surface_id"]) : -1;
106       }
107
108       // Check lookup
109       auto jtests = j.value("tests", json());
110
111       if (!jtests.empty()) {
112          logdebug("Embedded tests...");
113          std::vector<std::pair<int, int>> tests;
114          tests.reserve(jtests.size());
115          std::transform(std::cbegin(jtests), std::cend(jtests),
116                         std::back_inserter(tests), [](json const &j) {
117                            return std::make_pair(j["surface_id"],
118                                                  j["expect_layer_id"]);
119                         });
120
121          for (auto sid : tests) {
122             int lid = stl.get_layer_id(sid.first).value_or(-1);
123             logdebug("this=%d, that=%d, expect=%d", sid.first, lid, sid.second);
124             if (lid != sid.second) {
125                return Err<layer_map>("ID Map embedded test failed!");
126             }
127          }
128       }
129
130       return Ok(stl);
131    } catch (std::exception &e) {
132       return Err<struct layer_map>(e.what());
133    }
134 }
135
136 // Helper to allow std::lower_bound with a int key only
137 inline bool
138    operator<(struct layer const &a, int b) {
139    return a.id_max < b;
140 }
141
142 namespace {
143 optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
144                                         int surface_id) {
145    auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
146                              surface_id);
147
148    if (i != s2l->mapping.end()) {
149       // std::less only checks for layer::id_max, so check
150       // that we are actually inside of an interval here.
151       if (i->id_min <= surface_id) {
152          return optional<layer>(*i);
153       }
154    }
155
156    return nullopt;
157 }
158 }  // namespace
159
160 optional<int> layer_map::get_layer_id(int surface_id) {
161    auto e = get_surface_id_to_layer(this, surface_id);
162    if (!e) {
163       auto i = this->surfaces.find(surface_id);
164       if (i != this->surfaces.end()) {
165          return optional<int>(int(i->second));
166       }
167       return nullopt;
168    }
169    return optional<int>(e->layer_id);
170 }
171
172 optional<int> layer_map::get_layer_id(std::string const &role) {
173    for (auto const &r : this->roles) {
174       auto re = std::regex(r.first);
175       if (std::regex_match(role, re)) {
176          logdebug("role %s matches layer %d", role.c_str(), r.second);
177          return optional<int>(r.second);
178       }
179    }
180    logdebug("role %s does NOT match any layer", role.c_str());
181    return nullopt;
182 }
183
184 optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {
185    auto e = get_surface_id_to_layer(this, surface_id);
186    return e ? optional<genivi::rect>(e->rect) : nullopt;
187 }
188
189 json layer::to_json() const {
190    auto is_full = this->rect == genivi::full_rect;
191
192    json r{};
193    if (is_full) {
194       r = {{"type", "full"}};
195    } else {
196       r = {{"type", "rect"},
197            {"rect",
198             {{"x", this->rect.x},
199              {"y", this->rect.y},
200              {"width", this->rect.w},
201              {"height", this->rect.h}}}};
202    }
203
204    return {
205       {"id_min", this->id_min},     {"id_max", this->id_max},
206       {"name", this->name},         {"role", this->role},
207       {"layer_id", this->layer_id}, {"area", r},
208    };
209 }
210
211 json layer_map::to_json() const {
212    json j{};
213    for (auto const &i : this->mapping) {
214       j.push_back(i.to_json());
215    }
216    return j;
217 }
218
219 }  // namespace wm