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"
23 #include "hmi-debug.h"
27 using json = nlohmann::json;
29 layer::layer(nlohmann::json const &j) {
30 this->role = j["role"];
31 this->name = j["name"];
32 this->layer_id = j["layer_id"];
33 this->rect = genivi::full_rect;
34 if (j["area"]["type"] == "rect") {
35 auto jr = j["area"]["rect"];
36 this->rect = genivi::rect{
37 jr["width"], jr["height"], jr["x"], jr["y"],
40 auto split_layouts = j.find("split_layouts");
41 if (split_layouts != j.end()) {
42 auto &sls = j["split_layouts"];
43 // this->layouts.reserve(sls.size());
44 std::transform(std::cbegin(sls), std::cend(sls),
45 std::back_inserter(this->layouts), [this](json const &sl) {
46 struct split_layout l {
47 sl["name"], sl["main_match"], sl["sub_match"] };
49 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
50 "\"%s\")", this->layer_id,
51 l.name.c_str(), l.main_match.c_str(),
58 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
61 auto m = j["mappings"];
63 std::transform(std::cbegin(m), std::cend(m),
64 std::inserter(stl.mapping, stl.mapping.end()),
65 [](nlohmann::json const &j) {
66 return std::pair<int, struct layer>(
67 j.value("layer_id", -1), layer(j));
70 // XXX: add sanity checks here?
71 // * check for double IDs
72 // * check for double names/roles
74 stl.layers.reserve(m.size());
75 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
76 std::back_inserter(stl.layers),
77 [&stl](std::pair<int, struct layer> const &k) {
78 stl.roles.emplace_back(
79 std::make_pair(k.second.role, k.second.layer_id));
80 return unsigned(k.second.layer_id);
83 std::sort(stl.layers.begin(), stl.layers.end());
85 for (auto i : stl.mapping) {
86 if (i.second.name.empty()) {
87 return Err<struct layer_map>("Found mapping w/o name");
89 if (i.second.layer_id == -1) {
90 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
94 auto msi = j.find("main_surface");
96 stl.main_surface_name = msi->value("surface_role", "");
97 stl.main_surface = -1;
101 } catch (std::exception &e) {
102 return Err<struct layer_map>(e.what());
106 optional<int> layer_map::get_layer_id(int surface_id) {
107 auto i = this->surfaces.find(surface_id);
108 if (i != this->surfaces.end()) {
109 return optional<int>(i->second);
114 optional<int> layer_map::get_layer_id(std::string const &role) {
115 for (auto const &r : this->roles) {
116 auto re = std::regex(r.first);
117 if (std::regex_match(role, re)) {
118 HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
119 return optional<int>(r.second);
122 HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
126 json layer::to_json() const {
127 auto is_full = this->rect == genivi::full_rect;
131 r = {{"type", "full"}};
133 r = {{"type", "rect"},
135 {{"x", this->rect.x},
137 {"width", this->rect.w},
138 {"height", this->rect.h}}}};
142 {"name", this->name}, {"role", this->role},
143 {"layer_id", this->layer_id}, {"area", r},
147 json layer_map::to_json() const {
149 for (auto const &i : this->mapping) {
150 j.push_back(i.second.to_json());