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