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