5b28c1518adf5bce56a5dd79cda06d27d5dc8f6a
[staging/windowmanager.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
24 namespace wm {
25
26 using json = nlohmann::json;
27
28 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 = genivi::full_rect;
33    if (j["area"]["type"] == "rect") {
34       auto jr = j["area"]["rect"];
35       this->rect = genivi::rect{
36          jr["width"], jr["height"], jr["x"], jr["y"],
37       };
38    }
39    auto split_layouts = j.find("split_layouts");
40    if (split_layouts != j.end()) {
41       auto &sls = j["split_layouts"];
42       // this->layouts.reserve(sls.size());
43       std::transform(std::cbegin(sls), std::cend(sls),
44                      std::back_inserter(this->layouts), [this](json const &sl) {
45                         struct split_layout l {
46                            sl["name"], sl["main_match"], sl["sub_match"],
47                               sl.value<int>("priority", 0)
48                         };
49                         logdebug(
50                            "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
51                            "\"%s\") (prio: %d)", this->layer_id,
52                            l.name.c_str(), l.main_match.c_str(),
53                            l.sub_match.c_str(), l.prio);
54                         return l;
55                      });
56       //std::sort(std::begin(this->layouts), std::end(this->layouts),
57       //          [](struct split_layout const &a, struct split_layout const &b) {
58       //             return a.prio < b.prio;
59       //          });
60    }
61 }
62
63 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
64    try {
65       layer_map stl{};
66       auto m = j["mappings"];
67
68       std::transform(std::cbegin(m), std::cend(m),
69                      std::inserter(stl.mapping, stl.mapping.end()),
70                      [](nlohmann::json const &j) {
71                         return std::pair<int, struct layer>(
72                            j.value("layer_id", -1), layer(j));
73                      });
74
75       // XXX: add sanity checks here?
76       // * check for double IDs
77       // * check for double names/roles
78
79       stl.layers.reserve(m.size());
80       std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
81                      std::back_inserter(stl.layers),
82                      [&stl](std::pair<int, struct layer> const &k) {
83                         stl.roles.emplace_back(
84                            std::make_pair(k.second.role, k.second.layer_id));
85                         return unsigned(k.second.layer_id);
86                      });
87
88       std::sort(stl.layers.begin(), stl.layers.end());
89
90       for (auto i : stl.mapping) {
91          if (i.second.name.empty()) {
92             return Err<struct layer_map>("Found mapping w/o name");
93          }
94          if (i.second.layer_id == -1) {
95             return Err<struct layer_map>("Found invalid/unset IDs in mapping");
96          }
97       }
98
99       auto msi = j.find("main_surface");
100       if (msi != j.end()) {
101          stl.main_surface_name = msi->value("surface_role", "");
102          stl.main_surface = -1;
103       }
104
105       return Ok(stl);
106    } catch (std::exception &e) {
107       return Err<struct layer_map>(e.what());
108    }
109 }
110
111 optional<int> layer_map::get_layer_id(int surface_id) {
112    auto i = this->surfaces.find(surface_id);
113    if (i != this->surfaces.end()) {
114       return optional<int>(i->second);
115    }
116    return nullopt;
117 }
118
119 optional<int> layer_map::get_layer_id(std::string const &role) {
120    for (auto const &r : this->roles) {
121       auto re = std::regex(r.first);
122       if (std::regex_match(role, re)) {
123          logdebug("role %s matches layer %d", role.c_str(), r.second);
124          return optional<int>(r.second);
125       }
126    }
127    logdebug("role %s does NOT match any layer", role.c_str());
128    return nullopt;
129 }
130
131 json layer::to_json() const {
132    auto is_full = this->rect == genivi::full_rect;
133
134    json r{};
135    if (is_full) {
136       r = {{"type", "full"}};
137    } else {
138       r = {{"type", "rect"},
139            {"rect",
140             {{"x", this->rect.x},
141              {"y", this->rect.y},
142              {"width", this->rect.w},
143              {"height", this->rect.h}}}};
144    }
145
146    return {
147       {"name", this->name},         {"role", this->role},
148       {"layer_id", this->layer_id}, {"area", r},
149    };
150 }
151
152 json layer_map::to_json() const {
153    json j{};
154    for (auto const &i : this->mapping) {
155       j.push_back(i.second.to_json());
156    }
157    return j;
158 }
159
160 }  // namespace wm