e37160cf5282b31be1af8653b10deb79d4a83988
[apps/agl-service-windowmanager-2017.git] / src / layers.cpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
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 using json = nlohmann::json;
28
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"],
38       };
39    }
40
41    // Init flag of normal layout only
42    this->is_normal_layout_only = true;
43
44    auto split_layouts = j.find("split_layouts");
45    if (split_layouts != j.end()) {
46
47       // Clear flag of normal layout only
48       this->is_normal_layout_only = false;
49
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"] };
56                         HMI_DEBUG("wm",
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(),
60                            l.sub_match.c_str());
61                         return l;
62                      });
63    }
64    HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
65       this->layer_id, this->is_normal_layout_only);
66 }
67
68 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
69    try {
70       layer_map stl{};
71       auto m = j["mappings"];
72
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));
78                      });
79
80       // XXX: add sanity checks here?
81       // * check for double IDs
82       // * check for double names/roles
83
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);
91                      });
92
93       std::sort(stl.layers.begin(), stl.layers.end());
94
95       for (auto i : stl.mapping) {
96          if (i.second.name.empty()) {
97             return Err<struct layer_map>("Found mapping w/o name");
98          }
99          if (i.second.layer_id == -1) {
100             return Err<struct layer_map>("Found invalid/unset IDs in mapping");
101          }
102       }
103
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;
108       }
109
110       return Ok(stl);
111    } catch (std::exception &e) {
112       return Err<struct layer_map>(e.what());
113    }
114 }
115
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);
120    }
121    return nullopt;
122 }
123
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);
130       }
131    }
132    HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
133    return nullopt;
134 }
135
136 json layer::to_json() const {
137    auto is_full = this->rect == compositor::full_rect;
138
139    json r{};
140    if (is_full) {
141       r = {{"type", "full"}};
142    } else {
143       r = {{"type", "rect"},
144            {"rect",
145             {{"x", this->rect.x},
146              {"y", this->rect.y},
147              {"width", this->rect.w},
148              {"height", this->rect.h}}}};
149    }
150
151    return {
152       {"name", this->name},         {"role", this->role},
153       {"layer_id", this->layer_id}, {"area", r},
154    };
155 }
156
157 json layer_map::to_json() const {
158    json j{};
159    for (auto const &i : this->mapping) {
160       j.push_back(i.second.to_json());
161    }
162    return j;
163 }
164
165 }  // namespace wm