clang formatting
[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
19 #include "json_helper.hpp"
20 #include "layers.hpp"
21 #include "util.hpp"
22
23 namespace wm {
24
25 using json = nlohmann::json;
26
27 layer::layer(nlohmann::json const &j) {
28    DB(j);
29    if (j["type"] == "range") {
30       this->id_min = get<int>(j["first_surface_id"]);
31       this->id_max = get<int>(j["last_surface_id"]);
32    } else {
33       this->id_min = this->id_max = get<int>(j["surface_id"]);
34    }
35    this->name = j["name"].get<std::string>();
36    this->layer_id = get<int>(j["layer_id"]);
37    this->rect = genivi::full_rect;
38    if (j["area"]["type"] == "rect") {
39       auto jr = j["area"]["rect"];
40       this->rect = genivi::rect{
41          get<int32_t>(jr["width"]), get<int32_t>(jr["height"]),
42          get<int32_t>(jr["x"]), get<int32_t>(jr["y"]),
43       };
44    }
45 }
46
47 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
48    DB(j);
49    try {
50       layer_map stl{};
51       auto m = j["mappings"];
52       stl.layers.reserve(m.size());
53       std::transform(std::cbegin(m), std::cend(m),
54                      std::inserter(stl.mapping, stl.mapping.end()),
55                      [&stl](nlohmann::json const &j) {
56                         auto k = layer(j);
57                         stl.layers.push_back(unsigned(k.layer_id));
58                         return k;
59                      });
60       // XXX need to sort layers?
61       for (auto i : stl.mapping) {
62          if (i.name.empty()) {
63             return Err<struct layer_map>("Found mapping w/o name");
64          }
65          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
66             return Err<struct layer_map>("Found invalid/unset IDs in mapping");
67          }
68       }
69
70       auto msi = j.find("main_surface");
71       if (msi != j.end()) {
72          stl.main_surface = get<int>((*msi)["surface_id"]);
73       }
74
75       // Check lookup
76       auto jtests = j.value("tests", json());
77
78       if (!jtests.empty()) {
79          DB("Embedded tests...");
80          std::vector<std::pair<int, int>> tests;
81          tests.reserve(jtests.size());
82          std::transform(std::cbegin(jtests), std::cend(jtests),
83                         std::back_inserter(tests), [](json const &j) {
84                            return std::make_pair(
85                               get<int>(j["surface_id"]),
86                               get<int>(j["expect_layer_id"]));
87                         });
88
89          for (auto sid : tests) {
90             int lid = stl.get_layer_id(sid.first).value_or(-1);
91             DB("this=" << sid.first << ", that=" << lid
92                        << ", expect=" << sid.second);
93             if (lid != sid.second) {
94                return Err<layer_map>("ID Map embedded test failed!");
95             }
96          }
97       }
98
99       return Ok(stl);
100    } catch (std::exception &e) {
101       return Err<struct layer_map>(e.what());
102    }
103 }
104
105 // Helper to allow std::lower_bound with a int key only
106 inline bool
107    operator<(struct layer const &a, int b) {
108    return a.id_max < b;
109 }
110
111 namespace {
112 optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
113                                         int surface_id) {
114    auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
115                              surface_id);
116
117    if (i != s2l->mapping.end()) {
118       // std::less only checks for layer::id_max, so check
119       // that we are actually inside of an interval here.
120       if (i->id_min <= surface_id) {
121          return optional<layer>(*i);
122       }
123    }
124
125    return nullopt;
126 }
127 }
128
129 optional<int> layer_map::get_layer_id(int surface_id) {
130    auto e = get_surface_id_to_layer(this, surface_id);
131    return e ? optional<int>(e->layer_id) : nullopt;
132 }
133
134 optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {
135    auto e = get_surface_id_to_layer(this, surface_id);
136    return e ? optional<genivi::rect>(e->rect) : nullopt;
137 }
138
139 json layer::to_json() const {
140    auto is_full = this->rect == genivi::full_rect;
141
142    json r{};
143    if (is_full) {
144       r = {{"type", "full"}};
145    } else {
146       r = {{"type", "rect"},
147            {"rect",
148             {{"x", this->rect.x},
149              {"y", this->rect.y},
150              {"width", this->rect.w},
151              {"height", this->rect.h}}}};
152    }
153
154    return {
155       {"id_min", this->id_min},
156       {"id_max", this->id_max},
157       {"name", this->name},
158       {"layer_id", this->layer_id},
159       {"area", r},
160    };
161 }
162
163 json layer_map::to_json() const {
164    json j{};
165    for (auto const &i : this->mapping) {
166       j.push_back(i.to_json());
167    }
168    return j;
169 }
170
171 }  // namespace wm