Enable scaling
[apps/agl-service-windowmanager.git] / src / layers.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <algorithm>
18 #include <regex>
19
20 #include "json_helper.hpp"
21 #include "layers.hpp"
22 #include "util.hpp"
23 #include "hmi-debug.h"
24
25 namespace wm {
26
27 const int default_main_width = 1080;
28 const int default_main_height = 1920;
29
30 using json = nlohmann::json;
31
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"],
41       };
42    }
43
44    // Init flag of normal layout only
45    this->is_normal_layout_only = true;
46
47    auto split_layouts = j.find("split_layouts");
48    if (split_layouts != j.end()) {
49
50       // Clear flag of normal layout only
51       this->is_normal_layout_only = false;
52
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"] };
59                         HMI_DEBUG("wm",
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(),
63                            l.sub_match.c_str());
64                         return l;
65                      });
66    }
67    HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
68       this->layer_id, this->is_normal_layout_only);
69 }
70
71 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
72    try {
73       layer_map stl{};
74       auto m = j["mappings"];
75
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));
81                      });
82
83       // TODO: add sanity checks here?
84       // * check for double IDs
85       // * check for double names/roles
86
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);
94                      });
95
96       std::sort(stl.layers.begin(), stl.layers.end());
97
98       for (auto i : stl.mapping) {
99          if (i.second.name.empty()) {
100             return Err<struct layer_map>("Found mapping w/o name");
101          }
102          if (i.second.layer_id == -1) {
103             return Err<struct layer_map>("Found invalid/unset IDs in mapping");
104          }
105       }
106
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;
113       }
114
115       return Ok(stl);
116    } catch (std::exception &e) {
117       return Err<struct layer_map>(e.what());
118    }
119 }
120
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);
125    }
126    return nullopt;
127 }
128
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);
135       }
136    }
137    HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
138    return nullopt;
139 }
140
141 json layer::to_json() const {
142    auto is_full = this->rect == compositor::full_rect;
143
144    json r{};
145    if (is_full) {
146       r = {{"type", "full"}};
147    } else {
148       r = {{"type", "rect"},
149            {"rect",
150             {{"x", this->rect.x},
151              {"y", this->rect.y},
152              {"width", this->rect.w},
153              {"height", this->rect.h}}}};
154    }
155
156    return {
157       {"name", this->name},         {"role", this->role},
158       {"layer_id", this->layer_id}, {"area", r},
159    };
160 }
161
162 json layer_map::to_json() const {
163    json j{};
164    for (auto const &i : this->mapping) {
165       j.push_back(i.second.to_json());
166    }
167    return j;
168 }
169
170 }  // namespace wm