6ecf2e73a7e519a5a278b923c8fec18001b78268
[apps/agl-service-windowmanager-2017.git] / src / layers.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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_ivi_wm.hpp"
29
30 namespace wm
31 {
32
33 struct split_layout
34 {
35     std::string name;
36     std::string main_match;
37     std::string sub_match;
38 };
39
40 struct layer
41 {
42     using json = nlohmann::json;
43
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     compositor::rect rect;
55     // Specify a role prefix for surfaces that should be
56     // put on this layer.
57     std::string role;
58     // TODO: perhaps a zorder is needed here?
59     std::vector<struct split_layout> layouts;
60
61     mutable struct LayoutState state;
62
63     // Flag of normal layout only
64     bool is_normal_layout_only;
65
66     explicit layer(nlohmann::json const &j);
67
68     json to_json() const;
69 };
70
71 struct layer_map
72 {
73     using json = nlohmann::json;
74
75     using storage_type = std::map<int, struct layer>;
76     using layers_type = std::vector<uint32_t>;
77     using role_to_layer_map = std::vector<std::pair<std::string, int>>;
78     using addsurf_layer_map = std::map<int, int>;
79
80     storage_type mapping; // map surface_id to layer
81     layers_type layers;   // the actual layer IDs we have
82     int main_surface;
83     std::string main_surface_name;
84     role_to_layer_map roles;
85     addsurf_layer_map surfaces; // additional surfaces on layers
86
87     optional<int> get_layer_id(int surface_id);
88     optional<int> get_layer_id(std::string const &role);
89     optional<struct LayoutState *> get_layout_state(int surface_id)
90     {
91         int layer_id = *this->get_layer_id(surface_id);
92         auto i = this->mapping.find(layer_id);
93         return i == this->mapping.end()
94                    ? nullopt
95                    : optional<struct LayoutState *>(&i->second.state);
96     }
97     optional<struct layer> get_layer(int layer_id)
98     {
99         auto i = this->mapping.find(layer_id);
100         return i == this->mapping.end() ? nullopt
101                                         : optional<struct layer>(i->second);
102     }
103
104     layers_type::size_type get_layers_count() const
105     {
106         return this->layers.size();
107     }
108
109     void add_surface(int surface_id, int layer_id)
110     {
111         this->surfaces[surface_id] = layer_id;
112     }
113
114     void remove_surface(int surface_id)
115     {
116         this->surfaces.erase(surface_id);
117     }
118
119     json to_json() const;
120 };
121
122 struct result<struct layer_map> to_layer_map(nlohmann::json const &j);
123
124 static const nlohmann::json default_layers_json = {
125    {"main_surface", {
126       {"surface_role", "HomeScreen"}
127    }},
128    {"mappings", {
129       {
130          {"role", "^HomeScreen$"},
131          {"name", "HomeScreen"},
132          {"layer_id", 1000},
133          {"area", {
134             {"type", "full"}
135          }}
136       },
137       {
138          {"role", "MediaPlayer|Radio|Phone|Navigation|HVAC|Settings|Dashboard|POI|Mixer"},
139          {"name", "apps"},
140          {"layer_id", 1001},
141          {"area", {
142             {"type", "rect"},
143             {"rect", {
144                {"x", 0},
145                {"y", 218},
146                {"width", -1},
147                {"height", -433}
148             }}
149          }}
150       },
151       {
152          {"role", "^OnScreen.*"},
153          {"name", "popups"},
154          {"layer_id", 9999},
155          {"area", {
156             {"type", "rect"},
157             {"rect", {
158                {"x", 0},
159                {"y", 760},
160                {"width", -1},
161                {"height", 400}
162             }}
163          }}
164       }
165    }}
166 };
167 } // namespace wm
168
169 #endif // TMCAGLWM_LAYERS_H