layers/app: properly remove surfs, deactivate main_surface
[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    // Min and max surface ID mapped to this layer
43    int id_min = -1;
44    int id_max = -1;
45    // A more or less descriptive name?
46    std::string name = "";
47    // The actual layer ID
48    int layer_id = -1;
49    // The rectangular region surfaces are allowed to draw on
50    // this layer, note however, width and hieght of the rect
51    // can be negative, in which case they specify that
52    // the actual value is computed using MAX + 1 - w
53    // That is; allow us to specify dimensions dependent on
54    // e.g. screen dimension, w/o knowing the actual screen size.
55    genivi::rect rect;
56    // Specify a role prefix for surfaces that should be
57    // put on this layer.
58    std::string role;
59    // XXX perhaps a zorder is needed here?
60    std::vector<struct split_layout> layouts;
61    // XXX need to change the way we store these things...
62    mutable struct LayoutState state;
63
64    explicit layer(nlohmann::json const &j);
65
66    bool operator<(struct layer const &rhs) const {
67       return this->id_max < rhs.id_max;
68    }
69
70    json to_json() const;
71 };
72
73 struct layer_map {
74    using json = nlohmann::json;
75
76    using storage_type = std::set<struct layer>;
77    using layers_type = std::vector<uint32_t>;
78    using role_to_layer_map = std::vector<std::pair<std::string, int>>;
79    using addsurf_layer_map = std::map<int, int>;
80
81    // XXX: we also will need a layer_id to layer map, perhaps
82    // make this the primary map, and the surface_id->layer a
83    // secondary map.
84
85    storage_type mapping;  // map surface_id to layer
86    layers_type layers;    // the actual layer IDs we have
87    int main_surface;
88    std::string main_surface_name;
89    role_to_layer_map roles;
90    addsurf_layer_map surfaces;  // additional surfaces on layers
91
92    optional<int> get_layer_id(int surface_id);
93    optional<int> get_layer_id(std::string const &role);
94    optional<struct LayoutState*> get_layout_state(int surface_id) {
95       int layer_id = *this->get_layer_id(surface_id);
96       auto i = std::find_if(
97               std::begin(this->mapping), std::end(this->mapping),
98               [layer_id](struct layer const &l) { return layer_id == l.layer_id; });
99       return i == this->mapping.end() ? nullopt : optional<struct LayoutState *>(&i->state);
100    }
101    optional<struct layer> get_layer(int layer_id) {
102       auto i = std::find_if(
103          std::cbegin(this->mapping), std::cend(this->mapping),
104          [layer_id](struct layer const &l) { return layer_id == l.layer_id; });
105       return i == this->mapping.end() ? nullopt : optional<struct layer>(*i);
106    }
107
108    optional<genivi::rect> get_layer_rect(int surface_id);
109    layers_type::size_type get_layers_count() const {
110       return this->layers.size();
111    }
112
113    void add_surface(int surface_id, int layer_id) {
114       this->surfaces[surface_id] = layer_id;
115    }
116
117    void remove_surface(int surface_id) {
118       this->surfaces.erase(surface_id);
119    }
120
121    json to_json() const;
122 };
123
124 struct result<struct layer_map> to_layer_map(nlohmann::json const &j);
125
126 }  // namespace wm
127
128 #endif  // TMCAGLWM_LAYERS_H