Add new class
[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 <string>
21
22 #include "../include/json.hpp"
23 #include "layout.hpp"
24 #include "result.hpp"
25 #include "wayland_ivi_wm.hpp"
26
27 namespace wm
28 {
29
30 struct split_layout
31 {
32     std::string name;
33     std::string main_match;
34     std::string sub_match;
35 };
36
37 struct layer
38 {
39     using json = nlohmann::json;
40
41     // A more or less descriptive name?
42     std::string name = "";
43     // The actual layer ID
44     int layer_id = -1;
45     // The rectangular region surfaces are allowed to draw on
46     // this layer, note however, width and hieght of the rect
47     // can be negative, in which case they specify that
48     // the actual value is computed using MAX + 1 - w
49     // That is; allow us to specify dimensions dependent on
50     // e.g. screen dimension, w/o knowing the actual screen size.
51     compositor::rect rect;
52     // Specify a role prefix for surfaces that should be
53     // put on this layer.
54     std::string role;
55     // TODO: perhaps a zorder is needed here?
56     std::vector<struct split_layout> layouts;
57
58     mutable struct LayoutState state;
59
60     // Flag of normal layout only
61     bool is_normal_layout_only;
62
63     explicit layer(nlohmann::json const &j);
64
65     json to_json() const;
66 };
67
68 /*
69  * WMLayer is the logical container of application/system application(such like HomeScreen)
70  * This is handled by Window Manager to classify the application.
71  */
72 class WMLayer
73 {
74   public:
75     enum MANAGEMENT_TYPE
76     {
77         TILE,
78         STACK
79     };
80
81     explicit WMLayer(const std::string& name, MANAGEMENT_TYPE type, unsigned begin, unsigned end);
82     virtual ~WMLayer();
83
84     const std::string& layerName();
85     MANAGEMENT_TYPE layerType();
86     void appendRole(const std::string& name);
87     void appendArea(const std::string& area);
88
89 /*     unsigned getNewID(const std::string& role);
90     void remove(unsigned ivi_layer_id);
91     void clear();
92     bool attach(unsigned ivi_layer_id, const std::string& area);
93     void stack(unsigned ivi_layer_id, const std::string& area);
94     bool updateRenderOrder(const std::vector<unsigned> list); */
95
96   private:
97     std::string _name = ""; // Layer name
98     MANAGEMENT_TYPE _type;
99     std::vector<std::string> _roles;
100     unsigned _id_begin;
101     unsigned _id_end;
102     // current holding apps. This dynamically changes.
103     std::vector<unsigned> _ivi_layer_id_list;
104     // std::map<std::string, unsigned> _render_order;
105 };
106
107 class LayerManager
108 {
109   public:
110     void setRenderOrder(const std::vector<unsigned> layer_render_order);
111     std::vector<unsigned> getAllRenderOrder();
112     std::vector<std::shared_ptr<WMLayer>>& getAllLayers();
113     std::vector<unsigned> getRenderOrder(const std::string& layer_name);
114 };
115
116 struct layer_map
117 {
118     using json = nlohmann::json;
119
120     using storage_type = std::map<int, struct layer>;
121     using layers_type = std::vector<uint32_t>;
122     using role_to_layer_map = std::vector<std::pair<std::string, int>>;
123     using addsurf_layer_map = std::map<int, int>;
124
125     storage_type mapping; // map surface_id to layer
126     layers_type layers;   // the actual layer IDs we have
127     int main_surface;
128     std::string main_surface_name;
129     role_to_layer_map roles;
130     addsurf_layer_map surfaces; // additional surfaces on layers
131
132     optional<int> get_layer_id(int surface_id);
133     optional<int> get_layer_id(std::string const &role);
134     optional<struct LayoutState *> get_layout_state(int surface_id)
135     {
136         int layer_id = *this->get_layer_id(surface_id);
137         auto i = this->mapping.find(layer_id);
138         return i == this->mapping.end()
139                    ? nullopt
140                    : optional<struct LayoutState *>(&i->second.state);
141     }
142     optional<struct layer> get_layer(int layer_id)
143     {
144         auto i = this->mapping.find(layer_id);
145         return i == this->mapping.end() ? nullopt
146                                         : optional<struct layer>(i->second);
147     }
148
149     layers_type::size_type get_layers_count() const
150     {
151         return this->layers.size();
152     }
153
154     void add_surface(int surface_id, int layer_id)
155     {
156         this->surfaces[surface_id] = layer_id;
157     }
158
159     void remove_surface(int surface_id)
160     {
161         this->surfaces.erase(surface_id);
162     }
163
164     json to_json() const;
165     compositor::rect getAreaSize(const std::string &area);
166     const compositor::rect getScaleDestRect(int output_w, int output_h, const std::string &aspect_setting);
167     int loadAreaDb();
168
169   private:
170     std::unordered_map<std::string, compositor::rect> area2size;
171
172     static const char *kDefaultAreaDb;
173 };
174
175 struct result<struct layer_map> to_layer_map(nlohmann::json const &j);
176
177 static const nlohmann::json default_layers_json = {
178    {"main_surface", {
179       {"surface_role", "HomeScreen"}
180    }},
181    {"mappings", {
182       {
183          {"role", "^HomeScreen$"},
184          {"name", "HomeScreen"},
185          {"layer_id", 1000},
186          {"area", {
187             {"type", "full"}
188          }}
189       },
190       {
191          {"role", "MediaPlayer|Radio|Phone|Navigation|HVAC|Settings|Dashboard|POI|Mixer"},
192          {"name", "apps"},
193          {"layer_id", 1001},
194          {"area", {
195             {"type", "rect"},
196             {"rect", {
197                {"x", 0},
198                {"y", 218},
199                {"width", -1},
200                {"height", -433}
201             }}
202          }}
203       },
204       {
205          {"role", "^OnScreen.*"},
206          {"name", "popups"},
207          {"layer_id", 9999},
208          {"area", {
209             {"type", "rect"},
210             {"rect", {
211                {"x", 0},
212                {"y", 760},
213                {"width", -1},
214                {"height", 400}
215             }}
216          }}
217       }
218    }}
219 };
220 } // namespace wm
221
222 #endif // TMCAGLWM_LAYERS_H