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