2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <ilm/ilm_control.h>
20 #include "wm_client.hpp"
21 #include "wm_layer.hpp"
22 #include "json_helper.hpp"
27 using std::unordered_map;
29 #define BG_LAYER_NAME "BackGroundLayer"
34 LayerState::LayerState()
39 const unordered_map<string, string> LayerState::getCurrentState()
41 return this->area2appid;
44 const vector<unsigned> LayerState::getIviIdList()
46 return this->render_order;
49 void LayerState::addLayer(unsigned layer)
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);
56 void LayerState::removeLayer(unsigned layer)
58 auto fwd_itr = std::remove_if(
59 this->render_order.begin(), this->render_order.end(),
60 [layer](unsigned elm) {
62 HMI_DEBUG("remove layer %d", elm);
66 this->render_order.erase(fwd_itr, this->render_order.end());
69 void LayerState::attachAppToArea(const string& app, const string& area)
71 this->area2appid[area] = app;
74 void LayerState::dump()
76 std::string ids, apps;
77 for(const auto& ro : this->render_order)
79 ids += std::to_string(ro);
82 for(const auto& area : this->area2appid)
89 DUMP(" render order : %s", ids.c_str());
90 DUMP(" area, app : %s", apps.c_str());
93 WMLayer::WMLayer(json_object* j, unsigned wm_layer_id) : tmp_state(), state(), wm_layer_id(wm_layer_id)
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"));
102 HMI_ERROR("Parse Error!!");
105 if(this->id_begin > this->id_end)
107 HMI_ERROR("INVALID");
112 unsigned WMLayer::getNewLayerID(const string& role)
115 if(this->name == BG_LAYER_NAME)
118 // generate new layer id;
119 if(this->hasRole(role))
121 if(this->id_list.size() == 0)
123 ret = this->idBegin();
124 this->id_list.push_back(ret);
128 ret = this->id_list.back() + 1;
130 HMI_INFO("Generate new id: %d", ret);
137 size_t count = std::count(id_list.begin(), id_list.end(), ret);
138 if( (ret > this->idEnd()) || (count > 1))
140 HMI_NOTICE("id %d is not available then generate new id", ret);
142 for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
144 auto ret_found = std::find(id_list.begin(), id_list.end(), i);
145 if(ret_found == id_list.cend())
147 HMI_INFO("set new id: %d", i);
156 id_list.push_back(ret);
160 HMI_ERROR("failed to get New ID");
165 const string& WMLayer::layerName()
170 WMError WMLayer::setLayerState(const LayerState& l)
173 return WMError::SUCCESS;
176 void WMLayer::addLayerToState(unsigned layer)
178 this->tmp_state.addLayer(layer);
181 void WMLayer::removeLayerFromState(unsigned layer)
183 this->tmp_state.removeLayer(layer);
186 void WMLayer::attachAppToArea(const string& app, const string& area)
188 this->tmp_state.attachAppToArea(app, area);
191 string WMLayer::attachedApp(const string& area)
194 auto list = this->state.getCurrentState();
195 auto app = list.find(area);
196 if(app != list.end())
203 void WMLayer::appendArea(const string& area)
205 this->area_list.push_back(area);
208 void WMLayer::appTerminated(unsigned id)
210 auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
214 this->id_list.erase(fwd_itr, this->id_list.end());
215 this->tmp_state.removeLayer(id);
216 this->state.removeLayer(id);
220 bool WMLayer::hasLayerID(unsigned id)
222 bool ret = (id >= this->idBegin() && id <= this->idEnd());
225 auto itr = std::find(this->id_list.begin(), this->id_list.end(), id);
226 return (itr != this->id_list.end()) ? true : false;
229 bool WMLayer::hasRole(const string& role)
231 auto re = std::regex(this->role_list);
232 if (std::regex_match(role, re))
234 HMI_DEBUG("role %s matches layer %s", role.c_str(), this->name.c_str());
240 void WMLayer::update()
242 this->state = this->tmp_state;
247 this->tmp_state = this->state;
252 DUMP("===== wm layer status =====");
253 DUMP("Layer :%s", this->name.c_str());
254 this->tmp_state.dump();
256 DUMP("===== wm layer status end =====");