remove some of th more verbose debug messages
[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 surface_id_to_layer::surface_id_to_layer(nlohmann::json const &j) {
14    // DB(j);
15    if (j["type"] == "range") {
16       this->id_min = get<int>(j["first_surface_id"]);
17       this->id_max = get<int>(j["last_surface_id"]);
18    } else {
19       this->id_min = this->id_max = get<int>(j["surface_id"]);
20    }
21    this->name = j["name"].get<std::string>();
22    this->layer_id = get<int>(j["layer_id"]);
23 }
24
25 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
26    nlohmann::json const &j) {
27    DB(j);
28    try {
29       surface_id_to_layer_map stl{};
30       std::transform(
31          std::cbegin(j), std::cend(j),
32          std::inserter(stl.mapping, stl.mapping.end()),
33          [](nlohmann::json const &j) { return surface_id_to_layer(j); });
34       for (auto i : stl.mapping) {
35          if (i.name.empty()) {
36             return Err<struct surface_id_to_layer_map>(
37                "Found mapping w/o name");
38          }
39          if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
40             return Err<struct surface_id_to_layer_map>(
41                "Found invalid/unset IDs in mapping");
42          }
43       }
44       return Ok(stl);
45    } catch (std::exception &e) {
46       return Err<struct surface_id_to_layer_map>(e.what());
47    }
48 }
49
50 // Helper to allow std::lower_bound with a int key only
51 inline bool
52    operator<(struct surface_id_to_layer const &a, int b) {
53    return a.id_max < b;
54 }
55
56 optional<int> surface_id_to_layer_map::get_layer_for_surface(int surface_id) {
57    auto i = std::lower_bound(std::cbegin(this->mapping),
58                              std::cend(this->mapping), surface_id);
59
60    if (i != this->mapping.end()) {
61       // std::less only checks for surface_id_to_layer::id_max, so check
62       // that we are actually inside of an interval here.
63       if (i->id_min <= surface_id) {
64          return optional<int>(i->layer_id);
65       }
66    }
67
68    return nullopt;
69 }
70
71 }  // namespace wm