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"]),
30 get<int32_t>(jr["height"]),
31 get<int32_t>(jr["x"]),
32 get<int32_t>(jr["y"]),
37 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
38 nlohmann::json const &j) {
41 surface_id_to_layer_map stl{};
42 auto m = j["mappings"];
43 stl.layers.reserve(m.size());
44 std::transform(std::cbegin(m), std::cend(m),
45 std::inserter(stl.mapping, stl.mapping.end()),
46 [&stl](nlohmann::json const &j) {
47 auto k = surface_id_to_layer(j);
48 stl.layers.push_back(unsigned(k.layer_id));
51 // XXX need to sort layers?
52 for (auto i : stl.mapping) {
54 return Err<struct surface_id_to_layer_map>(
55 "Found mapping w/o name");
57 if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
58 return Err<struct surface_id_to_layer_map>(
59 "Found invalid/unset IDs in mapping");
64 auto jtests = j.value("tests", json());
66 if (!jtests.empty()) {
67 DB("Embedded tests...");
68 std::vector<std::pair<int, int>> tests;
69 tests.reserve(jtests.size());
70 std::transform(std::cbegin(jtests), std::cend(jtests),
71 std::back_inserter(tests), [](json const &j) {
72 return std::make_pair(
73 get<int>(j["surface_id"]),
74 get<int>(j["expect_layer_id"]));
77 for (auto sid : tests) {
78 int lid = stl.get_layer_for_surface(sid.first).value_or(-1);
79 DB("this=" << sid.first << ", that=" << lid
80 << ", expect=" << sid.second);
81 if (lid != sid.second) {
82 return Err<surface_id_to_layer_map>(
83 "ID Map embedded test failed!");
89 } catch (std::exception &e) {
90 return Err<struct surface_id_to_layer_map>(e.what());
94 // Helper to allow std::lower_bound with a int key only
96 operator<(struct surface_id_to_layer const &a, int b) {
101 optional<surface_id_to_layer> get_surface_id_to_layer(struct surface_id_to_layer_map const *s2l, int surface_id) {
102 auto i = std::lower_bound(std::cbegin(s2l->mapping),
103 std::cend(s2l->mapping), surface_id);
105 if (i != s2l->mapping.end()) {
106 // std::less only checks for surface_id_to_layer::id_max, so check
107 // that we are actually inside of an interval here.
108 if (i->id_min <= surface_id) {
109 return optional<surface_id_to_layer>(*i);
117 optional<int> surface_id_to_layer_map::get_layer_for_surface(int surface_id) {
118 auto e = get_surface_id_to_layer(this, surface_id);
119 return e ? optional<int>(e->layer_id) : nullopt;
122 optional<genivi::rect> surface_id_to_layer_map::get_rect_for_surface(int surface_id) {
123 auto e = get_surface_id_to_layer(this, surface_id);
124 return e ? optional<genivi::rect>(e->rect) : nullopt;