2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 = compositor::full_rect;
34 if (j["area"]["type"] == "rect") {
35 auto jr = j["area"]["rect"];
36 this->rect = compositor::rect{
37 jr["width"], jr["height"], jr["x"], jr["y"],
41 // Init flag of normal layout only
42 this->is_normal_layout_only = true;
44 auto split_layouts = j.find("split_layouts");
45 if (split_layouts != j.end()) {
47 // Clear flag of normal layout only
48 this->is_normal_layout_only = false;
50 auto &sls = j["split_layouts"];
51 // this->layouts.reserve(sls.size());
52 std::transform(std::cbegin(sls), std::cend(sls),
53 std::back_inserter(this->layouts), [this](json const &sl) {
54 struct split_layout l {
55 sl["name"], sl["main_match"], sl["sub_match"] };
57 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
58 "\"%s\")", this->layer_id,
59 l.name.c_str(), l.main_match.c_str(),
64 HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
65 this->layer_id, this->is_normal_layout_only);
68 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
71 auto m = j["mappings"];
73 std::transform(std::cbegin(m), std::cend(m),
74 std::inserter(stl.mapping, stl.mapping.end()),
75 [](nlohmann::json const &j) {
76 return std::pair<int, struct layer>(
77 j.value("layer_id", -1), layer(j));
80 // TODO: add sanity checks here?
81 // * check for double IDs
82 // * check for double names/roles
84 stl.layers.reserve(m.size());
85 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
86 std::back_inserter(stl.layers),
87 [&stl](std::pair<int, struct layer> const &k) {
88 stl.roles.emplace_back(
89 std::make_pair(k.second.role, k.second.layer_id));
90 return unsigned(k.second.layer_id);
93 std::sort(stl.layers.begin(), stl.layers.end());
95 for (auto i : stl.mapping) {
96 if (i.second.name.empty()) {
97 return Err<struct layer_map>("Found mapping w/o name");
99 if (i.second.layer_id == -1) {
100 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
104 auto msi = j.find("main_surface");
105 if (msi != j.end()) {
106 stl.main_surface_name = msi->value("surface_role", "");
107 stl.main_surface = -1;
111 } catch (std::exception &e) {
112 return Err<struct layer_map>(e.what());
116 optional<int> layer_map::get_layer_id(int surface_id) {
117 auto i = this->surfaces.find(surface_id);
118 if (i != this->surfaces.end()) {
119 return optional<int>(i->second);
124 optional<int> layer_map::get_layer_id(std::string const &role) {
125 for (auto const &r : this->roles) {
126 auto re = std::regex(r.first);
127 if (std::regex_match(role, re)) {
128 HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
129 return optional<int>(r.second);
132 HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
136 json layer::to_json() const {
137 auto is_full = this->rect == compositor::full_rect;
141 r = {{"type", "full"}};
143 r = {{"type", "rect"},
145 {{"x", this->rect.x},
147 {"width", this->rect.w},
148 {"height", this->rect.h}}}};
152 {"name", this->name}, {"role", this->role},
153 {"layer_id", this->layer_id}, {"area", r},
157 json layer_map::to_json() const {
159 for (auto const &i : this->mapping) {
160 j.push_back(i.second.to_json());