2 // Created by m on 7/27/17.
7 #include "json_helper.hpp"
13 using json = nlohmann::json;
15 surface_id_to_layer::surface_id_to_layer(nlohmann::json const &j) {
17 if (j["type"] == "range") {
18 this->id_min = get<int>(j["first_surface_id"]);
19 this->id_max = get<int>(j["last_surface_id"]);
21 this->id_min = this->id_max = get<int>(j["surface_id"]);
23 this->name = j["name"].get<std::string>();
24 this->layer_id = get<int>(j["layer_id"]);
25 this->rect = genivi::rect{-1, -1, 0, 0};
26 if (j["area"]["type"] == "rect") {
27 auto jr = j["area"]["rect"];
28 this->rect = genivi::rect{
29 get<int32_t>(jr["width"]), get<int32_t>(jr["height"]),
30 get<int32_t>(jr["x"]), get<int32_t>(jr["y"]),
35 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
36 nlohmann::json const &j) {
39 surface_id_to_layer_map stl{};
40 auto m = j["mappings"];
41 stl.layers.reserve(m.size());
42 std::transform(std::cbegin(m), std::cend(m),
43 std::inserter(stl.mapping, stl.mapping.end()),
44 [&stl](nlohmann::json const &j) {
45 auto k = surface_id_to_layer(j);
46 stl.layers.push_back(unsigned(k.layer_id));
49 // XXX need to sort layers?
50 for (auto i : stl.mapping) {
52 return Err<struct surface_id_to_layer_map>(
53 "Found mapping w/o name");
55 if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
56 return Err<struct surface_id_to_layer_map>(
57 "Found invalid/unset IDs in mapping");
62 auto jtests = j.value("tests", json());
64 if (!jtests.empty()) {
65 DB("Embedded tests...");
66 std::vector<std::pair<int, int>> tests;
67 tests.reserve(jtests.size());
68 std::transform(std::cbegin(jtests), std::cend(jtests),
69 std::back_inserter(tests), [](json const &j) {
70 return std::make_pair(
71 get<int>(j["surface_id"]),
72 get<int>(j["expect_layer_id"]));
75 for (auto sid : tests) {
76 int lid = stl.get_layer_for_surface(sid.first).value_or(-1);
77 DB("this=" << sid.first << ", that=" << lid
78 << ", expect=" << sid.second);
79 if (lid != sid.second) {
80 return Err<surface_id_to_layer_map>(
81 "ID Map embedded test failed!");
87 } catch (std::exception &e) {
88 return Err<struct surface_id_to_layer_map>(e.what());
92 // Helper to allow std::lower_bound with a int key only
94 operator<(struct surface_id_to_layer const &a, int b) {
99 optional<surface_id_to_layer> get_surface_id_to_layer(
100 struct surface_id_to_layer_map const *s2l, int surface_id) {
101 auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
104 if (i != s2l->mapping.end()) {
105 // std::less only checks for surface_id_to_layer::id_max, so check
106 // that we are actually inside of an interval here.
107 if (i->id_min <= surface_id) {
108 return optional<surface_id_to_layer>(*i);
116 optional<int> surface_id_to_layer_map::get_layer_for_surface(int surface_id) {
117 auto e = get_surface_id_to_layer(this, surface_id);
118 return e ? optional<int>(e->layer_id) : nullopt;
121 optional<genivi::rect> surface_id_to_layer_map::get_rect_for_surface(
123 auto e = get_surface_id_to_layer(this, surface_id);
124 return e ? optional<genivi::rect>(e->rect) : nullopt;