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