app/layers: move embedded test run to layers
[staging/windowmanager.git] / src / layers.cpp
1 //
2 // Created by m on 7/27/17.
3 //
4
5 #include <algorithm>
6
7 #include "json_helper.hpp"
8 #include "layers.hpp"
9 #include "util.hpp"
10
11 namespace wm {
12
13 using json = nlohmann::json;
14
15 surface_id_to_layer::surface_id_to_layer(nlohmann::json const &j) {
16    // DB(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"]);
20    } else {
21       this->id_min = this->id_max = get<int>(j["surface_id"]);
22    }
23    this->name = j["name"].get<std::string>();
24    this->layer_id = get<int>(j["layer_id"]);
25 }
26
27 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
28    nlohmann::json const &j) {
29    DB(j);
30    try {
31       surface_id_to_layer_map stl{};
32       auto m = j["mappings"];
33       std::transform(
34          std::cbegin(m), std::cend(m),
35          std::inserter(stl.mapping, stl.mapping.end()),
36          [](nlohmann::json const &j) { return surface_id_to_layer(j); });
37       for (auto i : stl.mapping) {
38          if (i.name.empty()) {
39             return Err<struct surface_id_to_layer_map>(
40                "Found mapping w/o name");
41          }
42          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
43             return Err<struct surface_id_to_layer_map>(
44                "Found invalid/unset IDs in mapping");
45          }
46       }
47
48       // Check lookup
49       auto jtests = j.value("tests", json());
50
51       if (! jtests.empty()) {
52          DB("Embedded tests...");
53          std::vector<std::pair<int, int>> tests;
54          tests.reserve(jtests.size());
55          std::transform(std::cbegin(jtests), std::cend(jtests),
56                         std::back_inserter(tests), [](json const &j) {
57                     return std::make_pair(get<int>(j["surface_id"]),
58                                           get<int>(j["expect_layer_id"]));
59                  });
60
61          for (auto sid : tests) {
62             int lid = stl.get_layer_for_surface(sid.first).value_or(-1);
63             DB("this=" << sid.first << ", that=" << lid
64                        << ", expect=" << sid.second);
65             if (lid != sid.second) {
66                return Err<surface_id_to_layer_map>(
67                        "ID Map embedded test failed!");
68             }
69          }
70       }
71
72       return Ok(stl);
73    } catch (std::exception &e) {
74       return Err<struct surface_id_to_layer_map>(e.what());
75    }
76 }
77
78 // Helper to allow std::lower_bound with a int key only
79 inline bool
80    operator<(struct surface_id_to_layer const &a, int b) {
81    return a.id_max < b;
82 }
83
84 optional<int> surface_id_to_layer_map::get_layer_for_surface(int surface_id) {
85    auto i = std::lower_bound(std::cbegin(this->mapping),
86                              std::cend(this->mapping), surface_id);
87
88    if (i != this->mapping.end()) {
89       // std::less only checks for surface_id_to_layer::id_max, so check
90       // that we are actually inside of an interval here.
91       if (i->id_min <= surface_id) {
92          return optional<int>(i->layer_id);
93       }
94    }
95
96    return nullopt;
97 }
98
99 }  // namespace wm