Improve output of multiple screen resolution
[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 struct layer_map
69 {
70     using json = nlohmann::json;
71
72     using storage_type = std::map<int, struct layer>;
73     using layers_type = std::vector<uint32_t>;
74     using role_to_layer_map = std::vector<std::pair<std::string, int>>;
75     using addsurf_layer_map = std::map<int, int>;
76
77     storage_type mapping; // map surface_id to layer
78     layers_type layers;   // the actual layer IDs we have
79     int main_surface;
80     std::string main_surface_name;
81     role_to_layer_map roles;
82     addsurf_layer_map surfaces; // additional surfaces on layers
83
84     optional<int> get_layer_id(int surface_id);
85     optional<int> get_layer_id(std::string const &role);
86     optional<struct LayoutState *> get_layout_state(int surface_id)
87     {
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     {
96         auto i = this->mapping.find(layer_id);
97         return i == this->mapping.end() ? nullopt
98                                         : optional<struct layer>(i->second);
99     }
100
101     layers_type::size_type get_layers_count() const
102     {
103         return this->layers.size();
104     }
105
106     void add_surface(int surface_id, int layer_id)
107     {
108         this->surfaces[surface_id] = layer_id;
109     }
110
111     void remove_surface(int surface_id)
112     {
113         this->surfaces.erase(surface_id);
114     }
115
116     json to_json() const;
117     void setupArea(double scaling);
118     compositor::rect getAreaSize(const std::string &area);
119     int loadAreaDb();
120
121   private:
122     std::unordered_map<std::string, compositor::rect> area2size;
123
124     static const char *kDefaultAreaDb;
125 };
126
127 struct result<struct layer_map> to_layer_map(nlohmann::json const &j);
128
129 static const nlohmann::json default_layers_json = {
130    {"main_surface", {
131       {"surface_role", "HomeScreen"}
132    }},
133    {"mappings", {
134       {
135          {"role", "^HomeScreen$"},
136          {"name", "HomeScreen"},
137          {"layer_id", 1000},
138          {"area", {
139             {"type", "full"}
140          }}
141       },
142       {
143          {"role", "MediaPlayer|Radio|Phone|Navigation|HVAC|Settings|Dashboard|POI|Mixer"},
144          {"name", "apps"},
145          {"layer_id", 1001},
146          {"area", {
147             {"type", "rect"},
148             {"rect", {
149                {"x", 0},
150                {"y", 218},
151                {"width", -1},
152                {"height", -433}
153             }}
154          }}
155       },
156       {
157          {"role", "^OnScreen.*"},
158          {"name", "popups"},
159          {"layer_id", 9999},
160          {"area", {
161             {"type", "rect"},
162             {"rect", {
163                {"x", 0},
164                {"y", 760},
165                {"width", -1},
166                {"height", 400}
167             }}
168          }}
169       }
170    }}
171 };
172 } // namespace wm
173
174 #endif // TMCAGLWM_LAYERS_H