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 "hmi-debug.h"
25 using json = nlohmann::json;
27 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 = compositor::full_rect;
33 if (j["area"]["type"] == "rect")
35 auto jr = j["area"]["rect"];
36 this->rect = compositor::rect{
44 // Init flag of normal layout only
45 this->is_normal_layout_only = true;
47 auto split_layouts = j.find("split_layouts");
48 if (split_layouts != j.end())
51 // Clear flag of normal layout only
52 this->is_normal_layout_only = false;
54 auto &sls = j["split_layouts"];
55 // this->layouts.reserve(sls.size());
56 std::transform(std::cbegin(sls), std::cend(sls),
57 std::back_inserter(this->layouts), [this](json const &sl) {
60 sl["name"], sl["main_match"], sl["sub_match"]
63 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
66 l.name.c_str(), l.main_match.c_str(),
71 HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
72 this->layer_id, this->is_normal_layout_only);
75 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
80 auto m = j["mappings"];
82 std::transform(std::cbegin(m), std::cend(m),
83 std::inserter(stl.mapping, stl.mapping.end()),
84 [](nlohmann::json const &j) {
85 return std::pair<int, struct layer>(
86 j.value("layer_id", -1), layer(j));
89 // TODO: add sanity checks here?
90 // * check for double IDs
91 // * check for double names/roles
93 stl.layers.reserve(m.size());
94 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
95 std::back_inserter(stl.layers),
96 [&stl](std::pair<int, struct layer> const &k) {
97 stl.roles.emplace_back(
98 std::make_pair(k.second.role, k.second.layer_id));
99 return unsigned(k.second.layer_id);
102 std::sort(stl.layers.begin(), stl.layers.end());
104 for (auto i : stl.mapping)
106 if (i.second.name.empty())
108 return Err<struct layer_map>("Found mapping w/o name");
110 if (i.second.layer_id == -1)
112 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
116 auto msi = j.find("main_surface");
119 stl.main_surface_name = msi->value("surface_role", "");
120 stl.main_surface = -1;
125 catch (std::exception &e)
127 return Err<struct layer_map>(e.what());
132 layer_map::get_layer_id(int surface_id)
134 auto i = this->surfaces.find(surface_id);
135 if (i != this->surfaces.end())
137 return optional<int>(i->second);
142 optional<int> layer_map::get_layer_id(std::string const &role)
144 for (auto const &r : this->roles)
146 auto re = std::regex(r.first);
147 if (std::regex_match(role, re))
149 HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
150 return optional<int>(r.second);
153 HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
157 json layer::to_json() const
159 auto is_full = this->rect == compositor::full_rect;
164 r = {{"type", "full"}};
168 r = {{"type", "rect"},
170 {{"x", this->rect.x},
172 {"width", this->rect.w},
173 {"height", this->rect.h}}}};
177 {"name", this->name},
178 {"role", this->role},
179 {"layer_id", this->layer_id},
184 json layer_map::to_json() const
187 for (auto const &i : this->mapping)
189 j.push_back(i.second.to_json());