Add gitlab issue/merge request templates
[apps/agl-service-windowmanager.git] / src / wm_layer.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 <regex>
18 #include <ilm/ilm_control.h>
19 #include <stdlib.h>
20 #include "wm_client.hpp"
21 #include "wm_layer.hpp"
22 #include "json_helper.hpp"
23 #include "util.hpp"
24
25 using std::string;
26 using std::vector;
27 using std::unordered_map;
28
29 #define BG_LAYER_NAME "BackGroundLayer"
30
31 namespace wm
32 {
33
34 LayerState::LayerState()
35     :  render_order(),
36        area2appid()
37 {}
38
39 const unordered_map<string, string> LayerState::getCurrentState()
40 {
41     return this->area2appid;
42 }
43
44 const vector<unsigned> LayerState::getIviIdList()
45 {
46     return this->render_order;
47 }
48
49 void LayerState::addLayer(unsigned layer)
50 {
51     auto result = std::find(this->render_order.begin(), this->render_order.end(), layer);
52     if(result == this->render_order.end())
53         this->render_order.push_back(layer);
54 }
55
56 void LayerState::removeLayer(unsigned layer)
57 {
58     auto fwd_itr = std::remove_if(
59         this->render_order.begin(), this->render_order.end(),
60         [layer](unsigned elm) {
61             if(elm == layer)
62                 HMI_DEBUG("remove layer %d", elm);
63             return elm == layer;
64         }
65     );
66     this->render_order.erase(fwd_itr, this->render_order.end());
67 }
68
69 void LayerState::attachAppToArea(const string& app, const string& area)
70 {
71     this->area2appid[area] = app;
72 }
73
74 void LayerState::dump()
75 {
76     std::string ids, apps;
77     for(const auto& ro : this->render_order)
78     {
79         ids += std::to_string(ro);
80         ids += ",";
81     }
82     for(const auto& area : this->area2appid)
83     {
84         apps += area.first;
85         apps += ":";
86         apps += area.second;
87         apps += ",";
88     }
89     DUMP("    render order : %s", ids.c_str());
90     DUMP("    area, app    : %s", apps.c_str());
91 }
92
93 WMLayer::WMLayer(json_object* j, unsigned wm_layer_id) : tmp_state(), state(), wm_layer_id(wm_layer_id)
94 {
95     this->name = jh::getStringFromJson(j, "name");
96     this->role_list = jh::getStringFromJson(j, "role");
97     this->id_begin = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_begin"));
98     this->id_end = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_end"));
99
100     if (name.empty())
101     {
102         HMI_ERROR("Parse Error!!");
103         exit(1);
104     }
105     if(this->id_begin > this->id_end)
106     {
107         HMI_ERROR("INVALID");
108         exit(1);
109     }
110 }
111
112 unsigned WMLayer::getNewLayerID(const string& role)
113 {
114     unsigned ret = 0;
115     if(this->name == BG_LAYER_NAME)
116         return ret;
117
118     // generate new layer id;
119     if(this->hasRole(role))
120     {
121         if(this->id_list.size() == 0)
122         {
123             ret = this->idBegin();
124             this->id_list.push_back(ret);
125         }
126         else
127         {
128             ret = this->id_list.back() + 1;
129         }
130         HMI_INFO("Generate new id: %d", ret);
131     }
132     else
133     {
134         return ret;
135     }
136
137     size_t count = std::count(id_list.begin(), id_list.end(), ret);
138     if( (ret > this->idEnd()) || (count > 1))
139     {
140         HMI_NOTICE("id %d is not available then generate new id", ret);
141         ret = 0; // reset
142         for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
143         {
144             auto ret_found = std::find(id_list.begin(), id_list.end(), i);
145             if(ret_found == id_list.cend())
146             {
147                 HMI_INFO("set new id: %d", i);
148                 ret = i;
149                 break;
150             }
151         }
152     }
153
154     if(ret != 0)
155     {
156         id_list.push_back(ret);
157     }
158     else
159     {
160         HMI_ERROR("failed to get New ID");
161     }
162     return ret;
163 }
164
165 const string& WMLayer::layerName()
166 {
167     return this->name;
168 }
169
170 WMError WMLayer::setLayerState(const LayerState& l)
171 {
172     this->tmp_state = l;
173     return WMError::SUCCESS;
174 }
175
176 void WMLayer::addLayerToState(unsigned layer)
177 {
178     this->tmp_state.addLayer(layer);
179 }
180
181 void WMLayer::removeLayerFromState(unsigned layer)
182 {
183     this->tmp_state.removeLayer(layer);
184 }
185
186 void WMLayer::attachAppToArea(const string& app, const string& area)
187 {
188     this->tmp_state.attachAppToArea(app, area);
189 }
190
191 string WMLayer::attachedApp(const string& area)
192 {
193     string ret;
194     auto list = this->state.getCurrentState();
195     auto app = list.find(area);
196     if(app != list.end())
197     {
198         ret = app->second;
199     }
200     return ret;
201 }
202
203 void WMLayer::appendArea(const string& area)
204 {
205     this->area_list.push_back(area);
206 }
207
208 void WMLayer::appTerminated(unsigned id)
209 {
210     auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
211         [id](unsigned elm) {
212             return elm == id;
213         });
214     this->id_list.erase(fwd_itr, this->id_list.end());
215     this->tmp_state.removeLayer(id);
216     this->state.removeLayer(id);
217     ilm_layerRemove(id);
218 }
219
220 bool WMLayer::hasLayerID(unsigned id)
221 {
222     bool ret = (id >= this->idBegin() && id <= this->idEnd());
223     if(!ret)
224         return ret;
225     auto itr = std::find(this->id_list.begin(), this->id_list.end(), id);
226     return (itr != this->id_list.end()) ? true : false;
227 }
228
229 bool WMLayer::hasRole(const string& role)
230 {
231     auto re = std::regex(this->role_list);
232     if (std::regex_match(role, re))
233     {
234         HMI_DEBUG("role %s matches layer %s", role.c_str(), this->name.c_str());
235         return true;
236     }
237     return false;
238 }
239
240 void WMLayer::update()
241 {
242     this->state = this->tmp_state;
243 }
244
245 void WMLayer::undo()
246 {
247     this->tmp_state = this->state;
248 }
249
250 void WMLayer::dump()
251 {
252     DUMP("===== wm layer status =====");
253     DUMP("Layer :%s", this->name.c_str());
254     DUMP("  [Current]");
255     this->state.dump();
256     DUMP("  [To be]");
257     this->tmp_state.dump();
258     DUMP("===== wm layer status end =====");
259
260 }
261
262 } // namespace wm