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 const int default_main_width = 1080;
28 const int default_main_height = 1920;
30 using json = nlohmann::json;
32 layer::layer(nlohmann::json const &j) {
33 this->role = j["role"];
34 this->name = j["name"];
35 this->layer_id = j["layer_id"];
36 this->rect = compositor::full_rect;
37 if (j["area"]["type"] == "rect") {
38 auto jr = j["area"]["rect"];
39 this->rect = compositor::rect{
40 jr["width"], jr["height"], jr["x"], jr["y"],
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()) {
50 // Clear flag of normal layout only
51 this->is_normal_layout_only = false;
53 auto &sls = j["split_layouts"];
54 // this->layouts.reserve(sls.size());
55 std::transform(std::cbegin(sls), std::cend(sls),
56 std::back_inserter(this->layouts), [this](json const &sl) {
57 struct split_layout l {
58 sl["name"], sl["main_match"], sl["sub_match"] };
60 "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
61 "\"%s\")", this->layer_id,
62 l.name.c_str(), l.main_match.c_str(),
67 HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
68 this->layer_id, this->is_normal_layout_only);
71 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
74 auto m = j["mappings"];
76 std::transform(std::cbegin(m), std::cend(m),
77 std::inserter(stl.mapping, stl.mapping.end()),
78 [](nlohmann::json const &j) {
79 return std::pair<int, struct layer>(
80 j.value("layer_id", -1), layer(j));
83 // TODO: add sanity checks here?
84 // * check for double IDs
85 // * check for double names/roles
87 stl.layers.reserve(m.size());
88 std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
89 std::back_inserter(stl.layers),
90 [&stl](std::pair<int, struct layer> const &k) {
91 stl.roles.emplace_back(
92 std::make_pair(k.second.role, k.second.layer_id));
93 return unsigned(k.second.layer_id);
96 std::sort(stl.layers.begin(), stl.layers.end());
98 for (auto i : stl.mapping) {
99 if (i.second.name.empty()) {
100 return Err<struct layer_map>("Found mapping w/o name");
102 if (i.second.layer_id == -1) {
103 return Err<struct layer_map>("Found invalid/unset IDs in mapping");
107 auto msi = j.find("main_surface");
108 if (msi != j.end()) {
109 stl.main_surface_name = msi->value("surface_role", "");
110 stl.main_surface_width = msi->value("width", default_main_width);
111 stl.main_surface_height = msi->value("height", default_main_height);
112 stl.main_surface = -1;
116 } catch (std::exception &e) {
117 return Err<struct layer_map>(e.what());
121 optional<int> layer_map::get_layer_id(int surface_id) {
122 auto i = this->surfaces.find(surface_id);
123 if (i != this->surfaces.end()) {
124 return optional<int>(i->second);
129 optional<int> layer_map::get_layer_id(std::string const &role) {
130 for (auto const &r : this->roles) {
131 auto re = std::regex(r.first);
132 if (std::regex_match(role, re)) {
133 HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
134 return optional<int>(r.second);
137 HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
141 json layer::to_json() const {
142 auto is_full = this->rect == compositor::full_rect;
146 r = {{"type", "full"}};
148 r = {{"type", "rect"},
150 {{"x", this->rect.x},
152 {"width", this->rect.w},
153 {"height", this->rect.h}}}};
157 {"name", this->name}, {"role", this->role},
158 {"layer_id", this->layer_id}, {"area", r},
162 json layer_map::to_json() const {
164 for (auto const &i : this->mapping) {
165 j.push_back(i.second.to_json());