layers: introduce layers.hpp and .cpp for layer related code
[staging/windowmanager.git] / src / layers.hpp
1 //
2 // Created by m on 7/27/17.
3 //
4
5 #ifndef TMCAGLWM_LAYERS_H
6 #define TMCAGLWM_LAYERS_H
7
8 #include <algorithm>
9 #include <experimental/optional>
10 #include <json.hpp>
11 #include <set>
12 #include <string>
13
14 #include "result.hpp"
15
16 namespace wm {
17
18 struct surface_id_to_layer {
19    int id_min = -1;
20    int id_max = -1;
21    std::string name = "";
22    int layer_id = -1;
23
24    explicit surface_id_to_layer(nlohmann::json const &j);
25
26    bool operator<(struct surface_id_to_layer const &rhs) const {
27       return this->id_max < rhs.id_max;
28    }
29 };
30
31 inline bool operator<(struct surface_id_to_layer const &a, int b) {
32    return a.id_max < b;
33 }
34
35 struct surface_id_to_layer_map {
36    typedef std::set<struct surface_id_to_layer> map_type;
37
38    map_type mapping;
39
40    std::experimental::optional<int> get_layer_for_surface(int surface_id) {
41       auto i = std::lower_bound(std::cbegin(this->mapping),
42                                 std::cend(this->mapping), surface_id);
43
44       if (i != this->mapping.end()) {
45          if (i->id_min <= surface_id) {
46             return std::experimental::optional<int>(i->layer_id);
47          }
48       }
49
50       return std::experimental::nullopt;
51    }
52 };
53
54 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
55    nlohmann::json const &j);
56
57 }  // namespace wm
58
59 #endif  // TMCAGLWM_LAYERS_H