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.
20 #include "json_helper.hpp"
26 using json = nlohmann::json;
28 layer::layer(nlohmann::json const &j) {
29 this->role = j["role"];
30 this->name = j["name"];
31 this->layer_id = j["layer_id"];
32 this->rect = genivi::full_rect;
33 if (j["area"]["type"] == "rect") {
34 auto jr = j["area"]["rect"];
35 this->rect = genivi::rect{
36 jr["width"], jr["height"], jr["x"], jr["y"],
39 auto split_layouts = j.find("split_layouts");
40 if (split_layouts != j.end()) {
41 auto &sls = j["split_layouts"];
42 // this->layouts.reserve(sls.size());
43 std::transform(std::cbegin(sls), std::cend(sls),
44 std::back_inserter(this->layouts), [this](json const &sl) {
45 struct split_layout l {
46 sl["name"], sl["main_match"], sl["sub_match"] };
48 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
49 "\"%s\")", this->layer_id,
50 l.name.c_str(), l.main_match.c_str(),
57 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
60 auto m = j["mappings"];
62 std::transform(std::cbegin(m), std::cend(m),
63 std::inserter(stl.mapping, stl.mapping.end()),
64 [](nlohmann::json const &j) {
65 return std::pair<int, struct layer>(
66 j.value("layer_id", -1), layer(j));
69 // XXX: add sanity checks here?
70 // * check for double IDs
71 // * check for double names/roles
73 stl.layers.reserve(m.size());
74 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
75 std::back_inserter(stl.layers),
76 [&stl](std::pair<int, struct layer> const &k) {
77 stl.roles.emplace_back(
78 std::make_pair(k.second.role, k.second.layer_id));
79 return unsigned(k.second.layer_id);
82 std::sort(stl.layers.begin(), stl.layers.end());
84 for (auto i : stl.mapping) {
85 if (i.second.name.empty()) {
86 return Err<struct layer_map>("Found mapping w/o name");
88 if (i.second.layer_id == -1) {
89 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
93 auto msi = j.find("main_surface");
95 stl.main_surface_name = msi->value("surface_role", "");
96 stl.main_surface = -1;
100 } catch (std::exception &e) {
101 return Err<struct layer_map>(e.what());
105 optional<int> layer_map::get_layer_id(int surface_id) {
106 auto i = this->surfaces.find(surface_id);
107 if (i != this->surfaces.end()) {
108 return optional<int>(i->second);
113 optional<int> layer_map::get_layer_id(std::string const &role) {
114 for (auto const &r : this->roles) {
115 auto re = std::regex(r.first);
116 if (std::regex_match(role, re)) {
117 logdebug("role %s matches layer %d", role.c_str(), r.second);
118 return optional<int>(r.second);
121 logdebug("role %s does NOT match any layer", role.c_str());
125 json layer::to_json() const {
126 auto is_full = this->rect == genivi::full_rect;
130 r = {{"type", "full"}};
132 r = {{"type", "rect"},
134 {{"x", this->rect.x},
136 {"width", this->rect.w},
137 {"height", this->rect.h}}}};
141 {"name", this->name}, {"role", this->role},
142 {"layer_id", this->layer_id}, {"area", r},
146 json layer_map::to_json() const {
148 for (auto const &i : this->mapping) {
149 j.push_back(i.second.to_json());