5d085a0d28f3417afed977cb9ef5ef4742e73eb5
[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 "layout.hpp"
27 #include "result.hpp"
28 #include "wayland.hpp"
29
30 namespace wm {
31
32 struct split_layout {
33    std::string name;
34    std::string main_match;
35    std::string sub_match;
36    int prio;  // no entirely sure we will use this
37 };
38
39 struct layer {
40    using json = nlohmann::json;
41
42    // A more or less descriptive name?
43    std::string name = "";
44    // The actual layer ID
45    int layer_id = -1;
46    // The rectangular region surfaces are allowed to draw on
47    // this layer, note however, width and hieght of the rect
48    // can be negative, in which case they specify that
49    // the actual value is computed using MAX + 1 - w
50    // That is; allow us to specify dimensions dependent on
51    // e.g. screen dimension, w/o knowing the actual screen size.
52    genivi::rect rect;
53    // Specify a role prefix for surfaces that should be
54    // put on this layer.
55    std::string role;
56    // XXX perhaps a zorder is needed here?
57    std::vector<struct split_layout> layouts;
58    // XXX need to change the way we store these things...
59    mutable struct LayoutState state;
60
61    explicit layer(nlohmann::json const &j);
62
63    json to_json() const;
64 };
65
66 struct layer_map {
67    using json = nlohmann::json;
68
69    using storage_type = std::map<int, struct layer>;
70    using layers_type = std::vector<uint32_t>;
71    using role_to_layer_map = std::vector<std::pair<std::string, int>>;
72    using addsurf_layer_map = std::map<int, int>;
73
74    // XXX: we also will need a layer_id to layer map, perhaps
75    // make this the primary map, and the surface_id->layer a
76    // secondary map.
77
78    storage_type mapping;  // map surface_id to layer
79    layers_type layers;    // the actual layer IDs we have
80    int main_surface;
81    std::string main_surface_name;
82    role_to_layer_map roles;
83    addsurf_layer_map surfaces;  // additional surfaces on layers
84
85    optional<int> get_layer_id(int surface_id);
86    optional<int> get_layer_id(std::string const &role);
87    optional<struct LayoutState*> get_layout_state(int surface_id) {
88       int layer_id = *this->get_layer_id(surface_id);
89       auto i = this->mapping.find(layer_id);
90       return i == this->mapping.end()
91                 ? nullopt
92                 : optional<struct LayoutState *>(&i->second.state);
93    }
94    optional<struct layer> get_layer(int layer_id) {
95       auto i = this->mapping.find(layer_id);
96       return i == this->mapping.end() ? nullopt
97                                       : optional<struct layer>(i->second);
98    }
99
100    layers_type::size_type get_layers_count() const {
101       return this->layers.size();
102    }
103
104    void add_surface(int surface_id, int layer_id) {
105       this->surfaces[surface_id] = layer_id;
106    }
107
108    void remove_surface(int surface_id) {
109       this->surfaces.erase(surface_id);
110    }
111
112    json to_json() const;
113 };
114
115 struct result<struct layer_map> to_layer_map(nlohmann::json const &j);
116
117 }  // namespace wm
118
119 #endif  // TMCAGLWM_LAYERS_H