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