Bug Fix : role in WMClientContext is not set
[apps/agl-service-windowmanager.git] / src / layers.cpp
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 #include <algorithm>
18 #include <regex>
19
20 #include "json_helper.hpp"
21 #include "layers.hpp"
22 #include "util.hpp"
23 #include "hmi-debug.h"
24
25 namespace wm
26 {
27
28 using json = nlohmann::json;
29
30 layer::layer(nlohmann::json const &j)
31 {
32       this->role = j["role"];
33       this->name = j["name"];
34       this->layer_id = j["layer_id"];
35       this->rect = compositor::full_rect;
36       if (j["area"]["type"] == "rect")
37       {
38             auto jr = j["area"]["rect"];
39             this->rect = compositor::rect{
40                 jr["width"],
41                 jr["height"],
42                 jr["x"],
43                 jr["y"],
44             };
45       }
46
47       // Init flag of normal layout only
48       this->is_normal_layout_only = true;
49
50       auto split_layouts = j.find("split_layouts");
51       if (split_layouts != j.end())
52       {
53
54             // Clear flag of normal layout only
55             this->is_normal_layout_only = false;
56
57             auto &sls = j["split_layouts"];
58             // this->layouts.reserve(sls.size());
59             std::transform(std::cbegin(sls), std::cend(sls),
60                            std::back_inserter(this->layouts), [this](json const &sl) {
61                                  struct split_layout l
62                                  {
63                                        sl["name"], sl["main_match"], sl["sub_match"]
64                                  };
65                                  HMI_DEBUG("wm",
66                                            "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
67                                            "\"%s\")",
68                                            this->layer_id,
69                                            l.name.c_str(), l.main_match.c_str(),
70                                            l.sub_match.c_str());
71                                  return l;
72                            });
73       }
74       HMI_DEBUG("wm", "layer_id:%d is_normal_layout_only:%d\n",
75                 this->layer_id, this->is_normal_layout_only);
76 }
77
78 struct result<struct layer_map> to_layer_map(nlohmann::json const &j)
79 {
80       try
81       {
82             layer_map stl{};
83             auto m = j["mappings"];
84
85             std::transform(std::cbegin(m), std::cend(m),
86                            std::inserter(stl.mapping, stl.mapping.end()),
87                            [](nlohmann::json const &j) {
88                                  return std::pair<int, struct layer>(
89                                      j.value("layer_id", -1), layer(j));
90                            });
91
92             // TODO: add sanity checks here?
93             // * check for double IDs
94             // * check for double names/roles
95
96             stl.layers.reserve(m.size());
97             std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping),
98                            std::back_inserter(stl.layers),
99                            [&stl](std::pair<int, struct layer> const &k) {
100                                  stl.roles.emplace_back(
101                                      std::make_pair(k.second.role, k.second.layer_id));
102                                  return unsigned(k.second.layer_id);
103                            });
104
105             std::sort(stl.layers.begin(), stl.layers.end());
106
107             for (auto i : stl.mapping)
108             {
109                   if (i.second.name.empty())
110                   {
111                         return Err<struct layer_map>("Found mapping w/o name");
112                   }
113                   if (i.second.layer_id == -1)
114                   {
115                         return Err<struct layer_map>("Found invalid/unset IDs in mapping");
116                   }
117             }
118
119             auto msi = j.find("main_surface");
120             if (msi != j.end())
121             {
122                   stl.main_surface_name = msi->value("surface_role", "");
123                   stl.main_surface = -1;
124             }
125
126             return Ok(stl);
127       }
128       catch (std::exception &e)
129       {
130             return Err<struct layer_map>(e.what());
131       }
132 }
133
134 optional<int>
135 layer_map::get_layer_id(int surface_id)
136 {
137       auto i = this->surfaces.find(surface_id);
138       if (i != this->surfaces.end())
139       {
140             return optional<int>(i->second);
141       }
142       return nullopt;
143 }
144
145 optional<int> layer_map::get_layer_id(std::string const &role)
146 {
147       for (auto const &r : this->roles)
148       {
149             auto re = std::regex(r.first);
150             if (std::regex_match(role, re))
151             {
152                   HMI_DEBUG("wm", "role %s matches layer %d", role.c_str(), r.second);
153                   return optional<int>(r.second);
154             }
155       }
156       HMI_DEBUG("wm", "role %s does NOT match any layer", role.c_str());
157       return nullopt;
158 }
159
160 json layer::to_json() const
161 {
162       auto is_full = this->rect == compositor::full_rect;
163
164       json r{};
165       if (is_full)
166       {
167             r = {{"type", "full"}};
168       }
169       else
170       {
171             r = {{"type", "rect"},
172                  {"rect",
173                   {{"x", this->rect.x},
174                    {"y", this->rect.y},
175                    {"width", this->rect.w},
176                    {"height", this->rect.h}}}};
177       }
178
179       return {
180           {"name", this->name},
181           {"role", this->role},
182           {"layer_id", this->layer_id},
183           {"area", r},
184       };
185 }
186
187 json layer_map::to_json() const
188 {
189       json j{};
190       for (auto const &i : this->mapping)
191       {
192             j.push_back(i.second.to_json());
193       }
194       return j;
195 }
196
197 } // namespace wm