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"
28 using json = nlohmann::json;
30 layer::layer(nlohmann::json const &j)
32 this->role = j["role"];
33 this->name = j["name"];
34 this->layer_id = j["layer_id"];
35 this->rect = compositor::full_rect;
36 if (j["area"]["type"] == "rect")
38 auto jr = j["area"]["rect"];
39 this->rect = compositor::rect{
47 // Init flag of normal layout only
48 this->is_normal_layout_only = true;
50 auto split_layouts = j.find("split_layouts");
51 if (split_layouts != j.end())
54 // Clear flag of normal layout only
55 this->is_normal_layout_only = false;
57 auto &sls = j["split_layouts"];
58 // this->layouts.reserve(sls.size());
59 std::transform(std::cbegin(sls), std::cend(sls),
60 std::back_inserter(this->layouts), [this](json const &sl) {
63 sl["name"], sl["main_match"], sl["sub_match"]
66 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
69 l.name.c_str(), l.main_match.c_str(),
74 HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
75 this->layer_id, this->is_normal_layout_only);
78 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
83 auto m = j["mappings"];
85 std::transform(std::cbegin(m), std::cend(m),
86 std::inserter(stl.mapping, stl.mapping.end()),
87 [](nlohmann::json const &j) {
88 return std::pair<int, struct layer>(
89 j.value("layer_id", -1), layer(j));
92 // TODO: add sanity checks here?
93 // * check for double IDs
94 // * check for double names/roles
96 stl.layers.reserve(m.size());
97 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
98 std::back_inserter(stl.layers),
99 [&stl](std::pair<int, struct layer> const &k) {
100 stl.roles.emplace_back(
101 std::make_pair(k.second.role, k.second.layer_id));
102 return unsigned(k.second.layer_id);
105 std::sort(stl.layers.begin(), stl.layers.end());
107 for (auto i : stl.mapping)
109 if (i.second.name.empty())
111 return Err<struct layer_map>("Found mapping w/o name");
113 if (i.second.layer_id == -1)
115 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
119 auto msi = j.find("main_surface");
122 stl.main_surface_name = msi->value("surface_role", "");
123 stl.main_surface = -1;
128 catch (std::exception &e)
130 return Err<struct layer_map>(e.what());
135 layer_map::get_layer_id(int surface_id)
137 auto i = this->surfaces.find(surface_id);
138 if (i != this->surfaces.end())
140 return optional<int>(i->second);
145 optional<int> layer_map::get_layer_id(std::string const &role)
147 for (auto const &r : this->roles)
149 auto re = std::regex(r.first);
150 if (std::regex_match(role, re))
152 HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
153 return optional<int>(r.second);
156 HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
160 json layer::to_json() const
162 auto is_full = this->rect == compositor::full_rect;
167 r = {{"type", "full"}};
171 r = {{"type", "rect"},
173 {{"x", this->rect.x},
175 {"width", this->rect.w},
176 {"height", this->rect.h}}}};
180 {"name", this->name},
181 {"role", this->role},
182 {"layer_id", this->layer_id},
187 json layer_map::to_json() const
190 for (auto const &i : this->mapping)
192 j.push_back(i.second.to_json());
197 void layer_map::setupArea(int output_w, int output_h)
199 compositor::rect rct;
201 std::string area = "normal.full";
202 std::string role = "Fallback";
203 auto l_id = this->get_layer_id(role);
204 auto l = this->get_layer(*l_id);
207 rct.w = output_w + 1 + rct.w;
209 rct.h = output_h + 1 + rct.h;
210 this->area2size[area] = rct;
215 this->area2size[area] = rct;
219 rct.y = rct.y + rct.h;
220 this->area2size[area] = rct;
225 rct = compositor::full_rect;
227 rct.w = output_w + rct.w;
229 rct.h = output_h + rct.h;
230 this->area2size[area] = rct;
235 auto ons_id = this->get_layer_id(role);
236 auto l_ons = this->get_layer(*ons_id);
239 rct.w = output_w + 1 + rct.w;
241 rct.h = output_h + 1 + rct.h;
242 this->area2size[area] = rct;
245 compositor::rect layer_map::getAreaSize(const std::string &area)
247 return area2size[area];