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