2 * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "json_helper.hpp"
25 using json = nlohmann::json;
27 layer::layer(nlohmann::json const &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"]);
33 this->id_min = this->id_max = get<int>(j["surface_id"]);
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"]),
47 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
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) {
57 stl.layers.push_back(unsigned(k.layer_id));
60 // XXX need to sort layers?
61 for (auto i : stl.mapping) {
63 return Err<struct layer_map>("Found mapping w/o name");
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");
70 auto msi = j.find("main_surface");
72 stl.main_surface = get<int>((*msi)["surface_id"]);
76 auto jtests = j.value("tests", json());
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"]));
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!");
100 } catch (std::exception &e) {
101 return Err<struct layer_map>(e.what());
105 // Helper to allow std::lower_bound with a int key only
107 operator<(struct layer const &a, int b) {
112 optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
114 auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
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);
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;
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;
139 json layer::to_json() const {
140 auto is_full = this->rect == genivi::full_rect;
144 r = {{"type", "full"}};
146 r = {{"type", "rect"},
148 {{"x", this->rect.x},
150 {"width", this->rect.w},
151 {"height", this->rect.h}}}};
155 {"id_min", this->id_min},
156 {"id_max", this->id_max},
157 {"name", this->name},
158 {"layer_id", this->layer_id},
163 json layer_map::to_json() const {
165 for (auto const &i: this->mapping) {
166 j.push_back(i.to_json());