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