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