app/layers: consolidate signed/unsigned usage
[staging/windowmanager.git] / src / layers.hpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef TMCAGLWM_LAYERS_H
18 #define TMCAGLWM_LAYERS_H
19
20 #include <json.hpp>
21
22 #include <regex>
23 #include <set>
24 #include <string>
25
26 #include "result.hpp"
27 #include "wayland.hpp"
28
29 namespace wm {
30
31 struct split_layout {
32    std::string name;
33    std::string main_match;
34    std::string sub_match;
35    int prio;  // no entirely sure we will use this
36 };
37
38 struct layer {
39    using json = nlohmann::json;
40
41    // Min and max surface ID mapped to this layer
42    int id_min = -1;
43    int id_max = -1;
44    // A more or less descriptive name?
45    std::string name = "";
46    // The actual layer ID
47    int layer_id = -1;
48    // The rectangular region surfaces are allowed to draw on
49    // this layer, note however, width and hieght of the rect
50    // can be negative, in which case they specify that
51    // the actual value is computed using MAX + 1 - w
52    // That is; allow us to specify dimensions dependent on
53    // e.g. screen dimension, w/o knowing the actual screen size.
54    genivi::rect rect;
55    // Specify a role prefix for surfaces that should be
56    // put on this layer.
57    std::string role;
58    // XXX perhaps a zorder is needed here?
59    std::vector<struct split_layout> layouts;
60
61    explicit layer(nlohmann::json const &j);
62
63    bool operator<(struct layer const &rhs) const {
64       return this->id_max < rhs.id_max;
65    }
66
67    json to_json() const;
68 };
69
70 struct layer_map {
71    using json = nlohmann::json;
72
73    using storage_type = std::set<struct layer>;
74    using layers_type = std::vector<uint32_t>;
75    using role_to_layer_map = std::vector<std::pair<std::string, int>>;
76    using addsurf_layer_map = std::map<int, int>;
77
78    // XXX: we also will need a layer_id to layer map, perhaps
79    // make this the primary map, and the surface_id->layer a
80    // secondary map.
81
82    storage_type mapping;  // map surface_id to layer
83    layers_type layers;    // the actual layer IDs we have
84    int main_surface;
85    std::string main_surface_name;
86    role_to_layer_map roles;
87    addsurf_layer_map surfaces;  // additional surfaces on layers
88
89    optional<int> get_layer_id(int surface_id);
90    optional<int> get_layer_id(std::string const &role);
91    optional<struct layer> get_layer(int layer_id) {
92       auto i = std::find_if(
93          std::cbegin(this->mapping), std::cend(this->mapping),
94          [layer_id](struct layer const &l) { return layer_id == l.layer_id; });
95       return i == this->mapping.end() ? nullopt : optional<struct layer>(*i);
96    }
97
98    optional<genivi::rect> get_layer_rect(int surface_id);
99    layers_type::size_type get_layers_count() const {
100       return this->layers.size();
101    }
102
103    void add_surface(int surface_id, int layer_id) {
104       this->surfaces[surface_id] = layer_id;
105    }
106
107    json to_json() const;
108 };
109
110 struct result<struct layer_map> to_layer_map(nlohmann::json const &j);
111
112 }  // namespace wm
113
114 #endif  // TMCAGLWM_LAYERS_H