layers: remove last remnants of get<T> to read ints from json
[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
19 #include "json_helper.hpp"
20 #include "layers.hpp"
21 #include "util.hpp"
22
23 namespace wm {
24
25 using json = nlohmann::json;
26
27 layer::layer(nlohmann::json const &j) {
28    if (j["type"] == "range") {
29       this->id_min = j["first_surface_id"];
30       this->id_max = j["last_surface_id"];
31    } else {
32       this->id_min = this->id_max = j["surface_id"];
33    }
34    this->name = j["name"].get<std::string>();
35    this->layer_id = j["layer_id"];
36    this->rect = genivi::full_rect;
37    if (j["area"]["type"] == "rect") {
38       auto jr = j["area"]["rect"];
39       this->rect = genivi::rect{
40          jr["width"], jr["height"],
41          jr["x"], jr["y"],
42       };
43    }
44 }
45
46 struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
47    try {
48       layer_map stl{};
49       auto m = j["mappings"];
50       stl.layers.reserve(m.size());
51       std::transform(std::cbegin(m), std::cend(m),
52                      std::inserter(stl.mapping, stl.mapping.end()),
53                      [&stl](nlohmann::json const &j) {
54                         auto k = layer(j);
55                         stl.layers.push_back(unsigned(k.layer_id));
56                         return k;
57                      });
58       // XXX need to sort layers?
59       for (auto i : stl.mapping) {
60          if (i.name.empty()) {
61             return Err<struct layer_map>("Found mapping w/o name");
62          }
63          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
64             return Err<struct layer_map>("Found invalid/unset IDs in mapping");
65          }
66       }
67
68       auto msi = j.find("main_surface");
69       if (msi != j.end()) {
70          stl.main_surface = (*msi)["surface_id"];
71       }
72
73       // Check lookup
74       auto jtests = j.value("tests", json());
75
76       if (!jtests.empty()) {
77          logdebug("Embedded tests...");
78          std::vector<std::pair<int, int>> tests;
79          tests.reserve(jtests.size());
80          std::transform(std::cbegin(jtests), std::cend(jtests),
81                         std::back_inserter(tests), [](json const &j) {
82                            return std::make_pair(
83                               j["surface_id"],
84                               j["expect_layer_id"]);
85                         });
86
87          for (auto sid : tests) {
88             int lid = stl.get_layer_id(sid.first).value_or(-1);
89             logdebug("this=%d, that=%d, expect=%d", sid.first, lid, sid.second);
90             if (lid != sid.second) {
91                return Err<layer_map>("ID Map embedded test failed!");
92             }
93          }
94       }
95
96       return Ok(stl);
97    } catch (std::exception &e) {
98       return Err<struct layer_map>(e.what());
99    }
100 }
101
102 // Helper to allow std::lower_bound with a int key only
103 inline bool
104    operator<(struct layer const &a, int b) {
105    return a.id_max < b;
106 }
107
108 namespace {
109 optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
110                                         int surface_id) {
111    auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
112                              surface_id);
113
114    if (i != s2l->mapping.end()) {
115       // std::less only checks for layer::id_max, so check
116       // that we are actually inside of an interval here.
117       if (i->id_min <= surface_id) {
118          return optional<layer>(*i);
119       }
120    }
121
122    return nullopt;
123 }
124 }
125
126 optional<int> layer_map::get_layer_id(int surface_id) {
127    auto e = get_surface_id_to_layer(this, surface_id);
128    return e ? optional<int>(e->layer_id) : nullopt;
129 }
130
131 optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {
132    auto e = get_surface_id_to_layer(this, surface_id);
133    return e ? optional<genivi::rect>(e->rect) : nullopt;
134 }
135
136 json layer::to_json() const {
137    auto is_full = this->rect == genivi::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       {"id_min", this->id_min},
153       {"id_max", this->id_max},
154       {"name", this->name},
155       {"layer_id", this->layer_id},
156       {"area", r},
157    };
158 }
159
160 json layer_map::to_json() const {
161    json j{};
162    for (auto const &i : this->mapping) {
163       j.push_back(i.to_json());
164    }
165    return j;
166 }
167
168 }  // namespace wm