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"],
47 sl.value<int>("priority", 0)
50 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
51 "\"%s\") (prio: %d)", this->layer_id,
52 l.name.c_str(), l.main_match.c_str(),
53 l.sub_match.c_str(), l.prio);
56 //std::sort(std::begin(this->layouts), std::end(this->layouts),
57 // [](struct split_layout const &a, struct split_layout const &b) {
58 // return a.prio < b.prio;
63 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
66 auto m = j["mappings"];
68 std::transform(std::cbegin(m), std::cend(m),
69 std::inserter(stl.mapping, stl.mapping.end()),
70 [](nlohmann::json const &j) {
71 return std::pair<int, struct layer>(
72 j.value("layer_id", -1), layer(j));
75 // XXX: add sanity checks here?
76 // * check for double IDs
77 // * check for double names/roles
79 stl.layers.reserve(m.size());
80 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
81 std::back_inserter(stl.layers),
82 [&stl](std::pair<int, struct layer> const &k) {
83 stl.roles.emplace_back(
84 std::make_pair(k.second.role, k.second.layer_id));
85 return unsigned(k.second.layer_id);
88 std::sort(stl.layers.begin(), stl.layers.end());
90 for (auto i : stl.mapping) {
91 if (i.second.name.empty()) {
92 return Err<struct layer_map>("Found mapping w/o name");
94 if (i.second.layer_id == -1) {
95 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
99 auto msi = j.find("main_surface");
100 if (msi != j.end()) {
101 stl.main_surface_name = msi->value("surface_role", "");
102 stl.main_surface = -1;
106 } catch (std::exception &e) {
107 return Err<struct layer_map>(e.what());
111 optional<int> layer_map::get_layer_id(int surface_id) {
112 auto i = this->surfaces.find(surface_id);
113 if (i != this->surfaces.end()) {
114 return optional<int>(i->second);
119 optional<int> layer_map::get_layer_id(std::string const &role) {
120 for (auto const &r : this->roles) {
121 auto re = std::regex(r.first);
122 if (std::regex_match(role, re)) {
123 logdebug("role %s matches layer %d", role.c_str(), r.second);
124 return optional<int>(r.second);
127 logdebug("role %s does NOT match any layer", role.c_str());
131 json layer::to_json() const {
132 auto is_full = this->rect == genivi::full_rect;
136 r = {{"type", "full"}};
138 r = {{"type", "rect"},
140 {{"x", this->rect.x},
142 {"width", this->rect.w},
143 {"height", this->rect.h}}}};
147 {"name", this->name}, {"role", this->role},
148 {"layer_id", this->layer_id}, {"area", r},
152 json layer_map::to_json() const {
154 for (auto const &i : this->mapping) {
155 j.push_back(i.second.to_json());